Source code for httomo.data.param_sweep_store

from __future__ import annotations
from os import PathLike
from pathlib import Path
from typing import Literal, Optional, Tuple

from mpi4py import MPI
import numpy as np
import h5py

from httomo.runner.dataset_store_backing import DataSetStoreBacking
from httomo.runner.auxiliary_data import AuxiliaryData
from httomo.sweep_runner.param_sweep_block import ParamSweepBlock
from httomo.sweep_runner.param_sweep_store_interfaces import ParamSweepSource


[docs] class ParamSweepReader(ParamSweepSource): """ Read parameter sweep results, extracting middle slices along the `detector_y` dim """ def __init__(self, source: ParamSweepWriter) -> None: self._no_of_sweeps = source.no_of_sweeps self._extract_dim: Literal[1] = 1 self._single_shape = source.single_shape self._total_shape = source.total_shape assert ( source._data is not None ), "Reader should be created from writer with data not `None`" self._data = source._data self._aux_data = source.aux_data self._h5filename: Optional[Path] = None self.h5file: Optional[h5py.File] = None if source.is_file_based: self._h5filename = source.filename self.h5file = h5py.File(source.filename, "r") self._data = self.h5file["/data"] else: self._data = source._data source.finalize() @property def no_of_sweeps(self) -> int: return self._no_of_sweeps @property def extract_dim(self) -> Literal[0, 1, 2]: return self._extract_dim @property def single_shape(self) -> Tuple[int, int, int]: return self._single_shape @property def total_shape(self) -> Tuple[int, int, int]: return self._total_shape @property def aux_data(self) -> AuxiliaryData: return self._aux_data
[docs] def read_sweep_results(self) -> ParamSweepBlock: slices = [slice(None, None, 1)] * 3 slices_per_sweep = self.single_shape[self.extract_dim] first_middle_slice_index = slices_per_sweep // 2 detector_y_slice = slice(first_middle_slice_index, None, slices_per_sweep) slices[self.extract_dim] = detector_y_slice return ParamSweepBlock( data=self._data[slices[0], slices[1], slices[2]], aux_data=self.aux_data, slicing_dim=self.extract_dim, )
[docs] def finalize(self): self._data = None if self.h5file is not None: self.h5file.close() self.h5file = None if self._h5filename is not None: self._h5filename.unlink() self._h5filename = None
@property def is_file_based(self) -> bool: return self._h5filename is not None @property def filename(self) -> Optional[Path]: return self._h5filename
[docs] class ParamSweepWriter: """Write parameter sweep results, concatenating them along the `detector_y` dim""" def __init__( self, no_of_sweeps: int, comm: MPI.Comm, temppath: PathLike, backing: DataSetStoreBacking, ) -> None: self._concat_dim: Literal[1] = 1 self._no_of_sweeps = no_of_sweeps self._no_of_sweeps_written: int = 0 self._single_shape: Optional[Tuple[int, int, int]] = None self._total_shape: Optional[Tuple[int, int, int]] = None self._data: Optional[np.ndarray | h5py.Dataset] = None self._slices_per_sweep: Optional[int] = None self._comm = comm self._backing = backing self._temppath = temppath self._h5filename: Optional[Path] = None self.h5file: Optional[h5py.File] = None if backing == DataSetStoreBacking.File: self._h5filename = ( Path(self._temppath) / f"sweep-output-rank-{self._comm.rank}.h5" ) self.h5file = h5py.File(self._h5filename, "w") @property def no_of_sweeps(self) -> int: return self._no_of_sweeps @property def concat_dim(self) -> Literal[0, 1, 2]: return self._concat_dim @property def single_shape(self) -> Tuple[int, int, int]: if self._single_shape is None: err_str = "Shape of single sweep result isn't known until the first write has occurred" raise ValueError(err_str) return self._single_shape @property def total_shape(self) -> Tuple[int, int, int]: if self._total_shape is None: err_str = ( "Shape of full array holding sweep results isn't known until the first " "write has occurred" ) raise ValueError(err_str) return self._total_shape
[docs] def make_reader(self) -> ParamSweepReader: if self._data is None: raise ValueError("Cannot make reader when no data has been written yet") return ParamSweepReader(self)
@property def no_of_sweeps_written(self) -> int: return self._no_of_sweeps_written
[docs] def increment_no_of_sweeps_written(self): self._no_of_sweeps_written += 1
@property def slices_per_sweep(self) -> int: if self._slices_per_sweep is None: raise ValueError( "Slices per sweep isn't known until the first write has occurred" ) return self._slices_per_sweep @property def aux_data(self) -> AuxiliaryData: return self._aux_data
[docs] def write_sweep_result(self, block: ParamSweepBlock): block.to_cpu() if self._data is None: self._single_shape = block.shape self._total_shape = ( self.single_shape[0], self.single_shape[1] * self.no_of_sweeps, self.single_shape[2], ) self._slices_per_sweep = self.single_shape[self.concat_dim] self._aux_data = block.aux_data if self._backing == DataSetStoreBacking.RAM: self._data = np.empty(shape=self.total_shape, dtype=block.data.dtype) elif self._backing == DataSetStoreBacking.File: assert self.h5file is not None self._data = self.h5file.create_dataset( "/data", shape=self.total_shape, dtype=block.data.dtype ) slices = [slice(None, None, 1)] * 3 sweep_res_start = self.no_of_sweeps_written * self.slices_per_sweep slices[self._concat_dim] = slice( sweep_res_start, sweep_res_start + self.slices_per_sweep, 1, ) assert ( self._data is not None ), "Numpy array or hdf5 file should be created prior to writing sweep result" self._data[slices[0], slices[1], slices[2]] = block.data self.increment_no_of_sweeps_written()
[docs] def finalize(self): self._data = None if self.h5file is not None: self.h5file.close() self.h5file = None
@property def is_file_based(self) -> bool: return self._backing is DataSetStoreBacking.File @property def filename(self) -> Optional[Path]: return self._h5filename