mertisreader v0.0.5
  • Home
  • Tutorials
  • API
  1. Lazy Loading

On this page

  • Lazy Loading
    • Helper Methods
    • Array Proxy
    • CSV Proxy
    • Notes
  • Report an issue

Lazy Loading

Lazy Loading

This page captures the utilities from nbs/01_lazy_loading.py.

Helper Methods

MERTISDataPackReader exposes convenience methods that hide the collect_data plumbing and return LazyArray instances ready to use.

from pathlib import Path

import mertisreader as mr

# Assume a CAL or PAR directory exists; the reader picks the processing level automatically.
candidate_roots = [Path.cwd().resolve(), Path.cwd().resolve().parent, Path.cwd().resolve().parent.parent]
repo_root = next(root for root in candidate_roots if (root / "data").exists())

input_path = (
    repo_root
    / "data"
    / "bcmer_tm_all_START-20200409T000000_END-20200410T000000"
    "_CRE-20240717T132010-ParamEventBootSciHK-short"
    / "cal"
)
output_path = Path("/tmp/")

reader = mr.MERTISDataPackReader(input_dir=input_path, output_dir=output_path, lazy=True)
reader.data_collector()

product = reader.get_tis_product()
print(f"Frames proxy: {product['frames']}")
print(f"Shape (no I/O): {product['frames'].shape}")
print(f"Path: {product['path'].name}")
Reading filetype: hk_default from mer_cal_hk_default_20200409_1-0651130766-12538__0_1
Processing label: /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/cal/mer_cal_hk_default_20200409_1-0651130766-12538__0_1.lblx
Now processing a Table_Delimited structure: MERTIS_CAL_DEFAULT_HOUSEKEEPING_DATA_TABLE
Reading filetype: hk_extended from mer_cal_hk_extended_20200409_1-0651130766-12595__0_1
Processing label: /home/kidpixo/work/esa/mertis_data_management/mertisreader/data/bcmer_tm_all_START-20200409T000000_END-20200410T000000_CRE-20240717T132010-ParamEventBootSciHK-short/cal/mer_cal_hk_extended_20200409_1-0651130766-12595__0_1.lblx
Now processing a Table_Delimited structure: MERTIS_CAL_EXTENDED_HOUSEKEEPING_DATA_TABLE
Reading filetype: tis from mer_cal_sc_tis_20200409_1-0651130819-21186__0_1
Frames proxy: LazyArray(shape=(40, 100, 21), dtype=>f8, status=lazy (memmap))
Shape (no I/O): (40, 100, 21)
Path: mer_cal_sc_tis_20200409_1-0651130819-21186__0_1.fits

Array Proxy

import numpy as np

from mertisreader.lazy_loading import LazyArray

base = np.arange(12, dtype=np.float64).reshape(3, 4)
lazy = LazyArray(base, header={"SIMPLE": True})

print(lazy)
print(lazy.shape)
print(lazy.dtype)
print(lazy.mean())
print(lazy.materialize())
LazyArray(shape=(3, 4), dtype=float64, status=lazy (memmap))
(3, 4)
float64
5.5
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]]

CSV Proxy

from pathlib import Path

from mertisreader.lazy_loading import LazyCSVLoader

sample_csv = Path("/tmp/mertisreader_lazy_loading_sample.csv")
sample_csv.write_text("a,b,c\n1,2,3\n4,5,6\n", encoding="utf-8")

loader = LazyCSVLoader(sample_csv)

print(loader)
print(loader.columns)
print(loader.materialize())
LazyCSVLoader(path=/tmp/mertisreader_lazy_loading_sample.csv, status=lazy (query plan))
['column_1', 'column_2', 'column_3']
  column_1 column_2 column_3
0        a        b        c
1        1        2        3
2        4        5        6

Notes

LazyArray keeps FITS-backed data accessible without forcing an immediate load, while LazyCSVLoader defers CSV reads until the data is explicitly collected.

  • Report an issue