Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reading data_scale_factor and data_offset #40

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/nxmx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,53 @@ def signal(self) -> str | None:
"""
return self._handle.attrs.get("signal")

@cached_property
def data_scale_factor(self) -> str | None:
"""
An optional scaling factor to apply to the values in ``data``.

The elements in ``data`` are often stored as integers for efficiency reasons and need
further correction, generating floats. The two fields data_scaling_factor and
data_offset allow linear corrections using the following convention:

.. code-block::

corrected_data = (data + offset) * scaling_factor

This formula will derive the corrected value, when necessary.

data_scaling_factor is sometimes known as gain and data_offset is sometimes
known as pedestal or background, depending on the community.

Use these fields to specify constants that need to be applied
to the data to correct it to physical values. For example, if the detector gain
is 10 counts per photon and a constant background of 400 needs to be subtracted
off the pixels, specify data_scaling_factor as 0.1 and data_offset as -400 to
specify the required conversion from raw counts to corrected photons. It
is implied processing software will apply these corrections on-the-fly during processing.

The rank of these fields should either be a single value for the full dataset, a
single per-pixel array applied to every image (dimensions (i, j) or (i, j, k)),
or a per-image correction specified with an array whose slowest rank is nP (dimensions
(np, 1), (np, i, j) or (np, i, j, k)).

When omitted, the scaling factor is assumed to be 1.
"""
if "data_scale_factor" in self._handle:
return self._handle["data_scale_factor"][()]


@cached_property
def data_offset(self) -> str | None:
"""
An optional offset to apply to the values in data.

When omitted, the offset is assumed to be 0.
"""
if "data_offset" in self._handle:
return self._handle["data_offset"][()]



class NXtransformations(H5Mapping):
"""Collection of axis-based translations and rotations to describe a geometry.
Expand Down
Loading