Datasets

Base Dataset

class PyLorentz.dataset.base_dataset.BaseDataset(imshape: tuple | numpy.ndarray | None = None, data_dir: PathLike | None = None, scale: float | None = None, verbose: int | bool = 1)[source]

Bases: object

A base class for handling datasets, providing common attributes and methods.

__init__(imshape: tuple | numpy.ndarray | None = None, data_dir: PathLike | None = None, scale: float | None = None, verbose: int | bool = 1)[source]

Initialize the BaseDataset object.

Parameters:
  • imshape (tuple | np.ndarray | None, optional) – Shape of the image. Default is None.

  • data_dir (os.PathLike | None, optional) – Directory for data storage. Default is None.

  • scale (float | None, optional) – Scale factor for the dataset. Default is None.

  • verbose (int | bool, optional) – Verbosity level. Default is 1.

property shape

Get the shape of the image.

property data_dir

Get the data directory.

property scale

Get the scale factor.

crop()[source]

Placeholder for crop method.

property transforms

Get the transformation parameters.

vprint(*args, **kwargs)[source]

Print messages if verbose is enabled.

property fov

Get the field of view.

Defocused Dataset

class PyLorentz.dataset.defocused_dataset.DefocusedDataset(images: numpy.ndarray, scale: float | None = None, defvals: numpy.ndarray | None = None, beam_energy: float | None = None, data_files: List[PathLike] = [], simulated: bool = False, verbose: int | bool = 1)[source]

Bases: BaseDataset

A dataset class for handling defocused images and related metadata.

Parameters:
  • images (np.ndarray) – The set of defocused images.

  • scale (Optional[float]) – The scale of the images.

  • defvals (Optional[np.ndarray]) – The defocus values corresponding to the images.

  • beam_energy (Optional[float]) – The beam energy used during imaging.

  • data_files (List[os.PathLike]) – File paths of the data files.

  • simulated (bool) – Indicates if the data is simulated.

  • verbose (Union[int, bool]) – Verbosity level for logging.

__init__(images: numpy.ndarray, scale: float | None = None, defvals: numpy.ndarray | None = None, beam_energy: float | None = None, data_files: List[PathLike] = [], simulated: bool = False, verbose: int | bool = 1)[source]

Initialize the BaseDataset object.

Parameters:
  • imshape (tuple | np.ndarray | None, optional) – Shape of the image. Default is None.

  • data_dir (os.PathLike | None, optional) – Directory for data storage. Default is None.

  • scale (float | None, optional) – Scale factor for the dataset. Default is None.

  • verbose (int | bool, optional) – Verbosity level. Default is 1.

classmethod from_TFS()[source]

Convert a Through Focal Series (TFS) to a DefocusedDataset.

classmethod load(images: numpy.ndarray | PathLike | List[PathLike], metadata: PathLike | dict | None = None, **kwargs) DefocusedDataset[source]

Load images and metadata to create a DefocusedDataset instance.

Parameters:
  • images (Union[np.ndarray, os.PathLike, List[os.PathLike]]) – Image data or paths.

  • metadata (Optional[Union[os.PathLike, dict]]) – Metadata as a path or dict.

Returns:

The created dataset instance.

Return type:

DefocusedDataset

property images: numpy.ndarray
property image: numpy.ndarray
property defvals: numpy.ndarray
property shape: tuple

Get the shape of the image.

property energy: float | None
select_ROI(idx: int = 0, image: numpy.ndarray | None = None) None[source]

Select a Region of Interest (ROI) for processing.

Parameters:
  • idx (int) – Index of the image to use for ROI selection.

  • image (Optional[np.ndarray]) – Specific image to use for ROI selection.

apply_transforms() None[source]

Apply transformations (e.g., rotation, cropping) to the images.

preprocess(hotpix: bool = True, median_filter_size: int | None = None, fast: bool = True, **kwargs) None[source]

Preprocess the images by filtering hot pixels and applying a median filter.

Parameters:
  • hotpix (bool) – Whether to filter hot pixels.

  • median_filter_size (Optional[int]) – Size of the median filter.

  • fast (bool) – Whether to use a fast filtering method.

reset_transforms() None[source]

Reset all transformations applied to the images.

show_im(idx: int = 0, **kwargs) None[source]

Display an image with optional parameters.

Parameters:

idx (int) – Index of the image to display.

show_all(**kwargs) None[source]

Display all images.

Parameters:

idx (int) – Index of the image to display.

filter(q_lowpass: float | None = None, q_highpass: float | None = None, filter_type: str = 'butterworth', butterworth_order: int = 2, idx: int | List[int] | None = None, show: bool = False, v: int | None = None) None[source]

Apply bandpass filtering to the images.

Parameters:
  • q_lowpass (Optional[float]) – Lowpass filter cutoff.

  • q_highpass (Optional[float]) – Highpass filter cutoff.

  • filter_type (str) – Type of filter (‘butterworth’ or ‘gaussian’).

  • butterworth_order (int) – Order of the Butterworth filter.

  • idx (Optional[Union[int, List[int]]]) – Indices of images to filter.

  • show (bool) – Whether to display the filtered images.

  • v (Optional[int]) – Verbosity level.

copy() DefocusedDataset[source]

Create a deep copy of the dataset.

Returns:

A deep copy of the current dataset.

Return type:

DefocusedDataset

Through Focal Series

class PyLorentz.dataset.through_focal_series.ThroughFocalSeries(imstack: numpy.ndarray, flipstack: numpy.ndarray | None = None, flip: bool | None = False, scale: float | None = None, defvals: numpy.ndarray | None = None, beam_energy: float | None = None, use_mask: bool | None = True, simulated: bool | None = False, data_dir: PathLike | None = None, data_files: List[PathLike] = [], verbose: int | None = 1)[source]

Bases: BaseDataset

A class for handling through-focal series (TFS) datasets, including processing and visualization.

Parameters:
  • imstack (np.ndarray) – Stack of images in the TFS.

  • flipstack (Optional[np.ndarray]) – Stack of flipped images for comparison.

  • flip (Optional[bool]) – Indicates if the dataset includes flipped images.

  • scale (Optional[float]) – The scale of the images.

  • defvals (Optional[np.ndarray]) – The defocus values for the images.

  • beam_energy (Optional[float]) – The beam energy used during imaging.

  • use_mask (Optional[bool]) – Whether to use a mask in processing.

  • simulated (Optional[bool]) – Indicates if the data is simulated.

  • data_dir (Optional[os.PathLike]) – Directory where data is stored.

  • data_files (List[os.PathLike]) – List of file paths for the data.

  • verbose (Optional[int]) – Verbosity level for logging.

__init__(imstack: numpy.ndarray, flipstack: numpy.ndarray | None = None, flip: bool | None = False, scale: float | None = None, defvals: numpy.ndarray | None = None, beam_energy: float | None = None, use_mask: bool | None = True, simulated: bool | None = False, data_dir: PathLike | None = None, data_files: List[PathLike] = [], verbose: int | None = 1)[source]

Initialize the BaseDataset object.

Parameters:
  • imshape (tuple | np.ndarray | None, optional) – Shape of the image. Default is None.

  • data_dir (os.PathLike | None, optional) – Directory for data storage. Default is None.

  • scale (float | None, optional) – Scale factor for the dataset. Default is None.

  • verbose (int | bool, optional) – Verbosity level. Default is 1.

classmethod from_files(aligned_file: str | PathLike, aligned_flip_file: str | PathLike | None = None, metadata_file: str | PathLike | None = None, flip: bool | None = False, scale: float | None = None, defocus_values: List[float] | None = None, beam_energy: float | None = None, dump_metadata: bool | None = True, use_mask: bool | None = True, legacy_data_loc: str | PathLike | None = None, legacy_fls_filename: str | PathLike | None = None, verbose: int | bool | None = True) ThroughFocalSeries[source]

Create a ThroughFocalSeries instance from files.

Parameters:
  • aligned_file (Union[str, os.PathLike]) – Path to the aligned image stack.

  • aligned_flip_file (Optional[Union[str, os.PathLike]]) – Path to the flip image stack.

  • metadata_file (Optional[Union[str, os.PathLike]]) – Path to metadata file.

  • flip (Optional[bool]) – Indicates if the dataset includes flipped images.

  • scale (Optional[float]) – The scale of the images.

  • defocus_values (Optional[List[float]]) – Defocus values.

  • beam_energy (Optional[float]) – Beam energy used during imaging.

  • dump_metadata (Optional[bool]) – Whether to save metadata to a file.

  • use_mask (Optional[bool]) – Whether to use a mask in processing.

  • legacy_data_loc (Optional[Union[str, os.PathLike]]) – Path for legacy data.

  • legacy_fls_filename (Optional[Union[str, os.PathLike]]) – FLS filename for legacy data.

  • verbose (Optional[Union[int, bool]]) – Verbosity level for logging.

Returns:

The created TFS instance.

Return type:

ThroughFocalSeries

property imstack: numpy.ndarray
property flipstack: numpy.ndarray
property flip: bool
property defvals_index: numpy.ndarray
property defvals: numpy.ndarray
property beam_energy: float | None
property full_stack: numpy.ndarray
property full_defvals: numpy.ndarray
property infocus: numpy.ndarray
property orig_infocus: numpy.ndarray
property shape: tuple

Get the shape of the image.

property len_tfs: int
preprocess(hotpix: bool | None = True, median_filter_size: int | None = None, fast: bool | None = True, **kwargs) None[source]

Preprocess the images by filtering hot pixels and applying a median filter.

Parameters:
  • hotpix (Optional[bool]) – Whether to filter hot pixels.

  • median_filter_size (Optional[int]) – Size of the median filter.

  • fast (Optional[bool]) – Whether to use a fast filtering method.

filter(q_lowpass: float | None = None, q_highpass: float | None = None, filter_type: str = 'butterworth', butterworth_order: int = 2, show: bool | None = False, v: int | None = None) None[source]

Apply filtering to the image stack.

Parameters:
  • q_lowpass (Optional[float]) – Lowpass filter cutoff.

  • q_highpass (Optional[float]) – Highpass filter cutoff.

  • filter_type (str) – Type of filter (‘butterworth’ or ‘gaussian’).

  • butterworth_order (int) – Order of the Butterworth filter.

  • show (Optional[bool]) – Whether to show the filtered images.

  • v (Optional[int]) – Verbosity level.

apply_transforms(v: int = 1) None[source]

Apply image transformations, such as rotation and cropping.

Parameters:

v (int) – Verbosity level for logging.

Returns:

None

select_ROI(image: numpy.ndarray | None = None) None[source]

Select a region of interest (ROI) from the image.

Parameters:

image (Optional[np.ndarray]) – Image to select ROI from.

Returns:

None

show_tfs(**kwargs) None[source]

Display the through-focal series images.

Parameters:

**kwargs – Additional keyword arguments for display.

Returns:

None

reset_transforms() None[source]

Reset transformations applied to the images.

Returns:

None