Skip to content

Results

leopard_em.pydantic_models.results

Pydantic models for Leopard-EM program results.

CorrelationTable

Bases: BaseModel2DTM

Correlation table data structure storing possible detections along a 2DTM search.

Attributes:

  • correlation_threshold (float) –

    Pre-defined threshold a cross-correlation value must surpass to be included in the correlation table.

  • num_observations (int) –

    Total number of detections in the correlation table (number of search indices which surpassed the correlation threshold).

  • defocus_offsets (list[float]) –

    List of defocus offsets (in Angstroms) used in the search.

  • phi_theta_angles (list[tuple[float, float]]) –

    List out-of-plane rotation angles (in degrees, Euler angles phi and theta, in ZYZ convention) used in the search.

  • psi_angles (list[float]) –

    List of in-plane rotation angles (in degrees, Euler angle psi, in ZYZ convention) used in the search.

  • search_index (list[int]) –

    Global search index defining defocus offset, phi/theta angles, and psi angle for each detection. Calculated as i * (n_j * n_k) + j * n_k + k, where i is the index of the defocus offset, j is the index of the phi/theta angles, and k is the index of the psi angle. Length will be equal to num_observations.

  • x (list[int]) –

    List of x-coordinates (in pixels) of the detections in the micrograph.

  • y (list[int]) –

    List of y-coordinates (in pixels) of the detections in the micrograph.

  • correlation_value (list[float]) –

    List of cross-correlation values for each detection.

  • correlation_mean (list[float]) –

    List of mean cross-correlation values for each detection, calculated across all search indices for the same x/y coordinates.

  • correlation_variance (list[float]) –

    List of variance of cross-correlation values for each detection, calculated across all search indices for the same x/y coordinates.

Methods:

to_dataframe

to_dataframe() -> pd.DataFrame

Convert per-detection data to a DataFrame.

Search-space metadata is stored in df.attrs so that from_dataframe can reconstruct the full object.

Returns:

  • DataFrame

    One row per detection with columns: search_index, x, y, correlation_value, correlation_mean, correlation_variance.

from_dataframe

from_dataframe(df: DataFrame) -> CorrelationTable

Reconstruct a CorrelationTable from a DataFrame produced by to_dataframe.

Parameters:

  • df (DataFrame) –

    DataFrame with detection columns and search-space metadata in df.attrs.

Returns:

to_hdf5

to_hdf5(file_path: str) -> None

Write this CorrelationTable to an HDF5 file.

Layout::

/metadata              (attrs: correlation_threshold, num_observations)
/search_space/
    defocus_offsets    float32 1-D
    phi_theta_angles   float32 (n, 2)
    psi_angles         float32 1-D
/detections/
    search_index       int32 1-D
    x                  int32 1-D
    y                  int32 1-D
    correlation_value  float32 1-D
    correlation_mean   float32 1-D
    correlation_variance float32 1-D

Parameters:

  • file_path (str) –

    Destination HDF5 file path.

from_hdf5

from_hdf5(file_path: str) -> CorrelationTable

Load a CorrelationTable from an HDF5 file written by to_hdf5.

Parameters:

  • file_path (str) –

    Path to the HDF5 file.

Returns:

from_match_template_results

from_match_template_results(processed_correlation_table: dict, defocus_values: Tensor, euler_angles: Tensor, correlation_average: Tensor, correlation_variance_map: Tensor) -> CorrelationTable

Construct a CorrelationTable from backend outputs.

Parameters:

  • processed_correlation_table (dict) –

    Output of process_correlation_table with an additional global_idx key (list[int]). Expected keys: threshold, global_idx, x, y, correlation.

  • defocus_values (Tensor) –

    Defocus offsets used in the search. Shape (num_defocus,).

  • euler_angles (Tensor) –

    All Euler angles used in the search, shape (num_orientations, 3), in ZYZ convention (degrees). Must be ordered as a grid: all psi values for the first (phi, theta) pair, then all psi values for the second pair, etc.

  • correlation_average (Tensor) –

    Per-pixel mean cross-correlation, shape (H, W).

  • correlation_variance_map (Tensor) –

    Per-pixel standard deviation of cross-correlation, shape (H, W).

Returns:

MatchTemplateResultHDF5

Bases: _MatchTemplateResultBase

Bundles all result tensors and metadata into a single HDF5 file.

HDF5 file layout

All eight 2-D result tensors are stored as float32 datasets inside a /tensors group. When compress is True (the default) each dataset is compressed with gzip at level 4. Scalar metadata (total_projections, total_orientations, total_defocus) are stored as attributes on the HDF5 root group. No MRC paths are written to the file; the path to the HDF5 file itself is the only path required at load time.

/  (root)
│  attrs: leopard_em_version, total_projections,
│         total_orientations, total_defocus
└─ tensors/
       mip                  float32, shape (H, W), gzip-4 (if compress=True)
       scaled_mip           float32, shape (H, W), gzip-4
       correlation_average  float32, shape (H, W), gzip-4
       correlation_variance float32, shape (H, W), gzip-4
       orientation_psi      float32, shape (H, W), gzip-4
       orientation_theta    float32, shape (H, W), gzip-4
       orientation_phi      float32, shape (H, W), gzip-4
       relative_defocus     float32, shape (H, W), gzip-4

Attributes:

  • hdf5_path (str) –

    Path to the HDF5 output file.

  • allow_file_overwrite (bool) –

    Whether to allow overwriting an existing file. Default is False.

  • compress (bool) –

    Whether to apply gzip-4 compression to tensor datasets. Default is True. Disable for faster writes at the cost of larger files.

Methods:

  • validate_hdf5_path

    Validates write permissions and overwrite policy for hdf5_path.

  • to_hdf5

    Writes tensors and metadata to hdf5_path.

  • from_hdf5

    Class method that loads an instance from an existing HDF5 file.

validate_hdf5_path

validate_hdf5_path() -> Self

Validate hdf5_path for write permissions and overwriting.

Returns:

  • Self

Raises:

  • ValueError

    If the path is not writable or the file already exists and overwriting is disabled.

export_results

export_results() -> None

Write tensors and metadata to hdf5_path, plus the correlation table.

to_hdf5

to_hdf5() -> None

Write tensors and scalar metadata to hdf5_path.

Tensors are cast to float32 before writing. When self.compress is True, each dataset is compressed with gzip at level 4.

from_hdf5

from_hdf5(path: str | PathLike, allow_file_overwrite: bool = True, compress: bool = True) -> MatchTemplateResultHDF5

Load a MatchTemplateResultHDF5 from an existing HDF5 file.

Parameters:

  • path (str | PathLike) –

    Path to the HDF5 file written by to_hdf5.

  • allow_file_overwrite (bool, default: True ) –

    Passed to the constructor. Defaults to True so that the model validator does not reject the path of the file being loaded.

  • compress (bool, default: True ) –

    Passed to the constructor. Controls compression on any subsequent to_hdf5 call made on the returned instance. Default is True.

Returns:

MatchTemplateResultMRC

Bases: _MatchTemplateResultBase

Stores each result tensor in a separate MRC file.

Attributes:

  • allow_file_overwrite (bool) –

    Whether to allow overwriting of existing files. Default is False.

  • mip_path (str) –

    Output path for the maximum intensity projection MRC file.

  • scaled_mip_path (str) –

    Output path for the scaled MIP MRC file.

  • correlation_average_path (str) –

    Output path for the correlation average MRC file.

  • correlation_variance_path (str) –

    Output path for the correlation variance MRC file.

  • orientation_psi_path (str) –

    Output path for the orientation psi MRC file.

  • orientation_theta_path (str) –

    Output path for the orientation theta MRC file.

  • orientation_phi_path (str) –

    Output path for the orientation phi MRC file.

  • relative_defocus_path (str) –

    Output path for the relative defocus MRC file.

Methods:

validate_paths

validate_paths() -> Self

Validate output paths for write permissions and overwriting.

Returns:

  • Self

Raises:

  • ValueError

    If any path is not writable or already exists and overwriting is disabled.

load_tensors_from_paths

load_tensors_from_paths() -> None

Read MRC files from the held paths into the tensor attributes.

export_results

export_results() -> None

Write the held tensors to their respective MRC paths.