autoemx.calibrations package
Calibration Loader for SEM EDS Analysis
This module loads the calibrations of a microscope: its detector model, from the package folder of the microscope model (autoemx/calibrations/<microscope_ID>/), whose values are set as attributes of this module; and the detector channel parameters and experimental standards of the instrument, from its local calibration folder, ~/.autoemx/calibrations/<microscope_ID>/ (see autoemx._paths).
- Usage from other modules:
import autoemx.calibrations as calibs calibs.load_microscope_calibrations(microscope_ID =’PhenomXL’, meas_mode = ‘point’, load_detector_channel_params = True)
Author: Andrea Giunto Created on: Mon Jan 20 15:40:42 2025
- autoemx.calibrations.ensure_standards_model(payload: Any, meas_type: str, beam_energy: int) EDSStandardsFile[source]
Normalize arbitrary standards payloads into the typed schema model.
This keeps backward-compatibility conversion logic in one place for callers that may still hold legacy dict payloads in memory.
- autoemx.calibrations.load_detector_model(microscope_ID: str) DetectorModel[source]
Detector model of the microscope, from detector_model.json in the package folder of the microscope model (autoemx/calibrations/<microscope_ID>/).
- Raises:
ValueError – If the microscope has no calibration folder in the package, or no detector model.
- autoemx.calibrations.microscope_driver_name(microscope_ID: str) str[source]
Driver of the microscope: ‘driver’ in its detector model if set, otherwise the microscope ID.
- autoemx.calibrations.load_microscope_calibrations(microscope_ID: str, meas_mode: str | None = None, load_detector_channel_params: bool = False) None[source]
Load the calibrations of a given microscope and EDS mode.
The detector model of the microscope (see load_detector_model) is set as
detector_model, and each of its values and calibrated functions is set as an attribute of this module (e.g.calibs.elec_noise,calibs.get_calibrated_peak_shape_params).Optionally, it also loads the latest detector channel parameters. This is only needed during spectra acquisition, not when fitting or quantifying. In the latter cases, the detector channel parameters should be loaded from the measurement files directly.
- Parameters:
microscope_ID (str) – The name of the microscope (must match a folder in autoemx/calibrations, with its detector model).
meas_mode (str, Optional) – If provided, it checks whether it is included in the available_meas_modes of the detector model.
load_detector_channel_params (bool, optional) – If True, loads the latest detector channel parameters as well (default: False).
- Raises:
ValueError – If the calibrations or detector model cannot be found, or if the meas_mode is invalid.
FileNotFoundError – If no detector channel calibration file is found.
- autoemx.calibrations.load_latest_detector_channel_params(meas_mode)[source]
Load the latest detector channel parameters for a given measurement mode.
This function retrieves the most recent detector channel calibration parameters (such as beam current, energy scale, and offset) for the specified measurement mode. It ensures that all required calibration keys are present before returning.
The parameters are loaded via the get_latest_detector_channel_params() function, which must populate the global detector_channel_params dictionary.
- Parameters:
meas_mode (str) – The measurement mode for which detector channel parameters are requested. Must be one of the modes available in detector_channel_params.
- Raises:
RuntimeError – If detector_channel_params is not loaded into the global namespace.
ValueError – If the requested meas_mode is not present in detector_channel_params.
KeyError – If one or more required calibration keys are missing for the given meas_mode.
- autoemx.calibrations.get_latest_detector_channel_params(verbose: bool = True) None[source]
Load dictionary of detector channel calibration parameters for each meas_mode.
- Each entry contains:
offset: float, energy offset (keV) for channel 0
scale: float, energy bin width (keV/channel)
spot_size: float, related to beam current
These parameters should be recalibrated regularly.
The most recent file in the local calibration folder of the microscope is used (~/.autoemx/calibrations/<microscope_ID>/Detector_channel_params_calibs).
- Raises:
FileNotFoundError – If no detector channel calibration file is found.:
- autoemx.calibrations.update_detector_channel_params(meas_mode, new_offset, new_scale, verbose: bool = True)[source]
Update and save detector channel calibration parameters for a given measurement mode.
This function retrieves the latest detector channel parameters for the specified measurement mode, updates the offset and scale values, and saves the updated parameters to a timestamped JSON file in the local calibration folder of the microscope.
- Parameters:
meas_mode (str) – The measurement mode for which parameters will be updated. Must be present in the loaded detector_channel_params.
new_offset (float) – The new detector channel offset value to set.
new_scale (float) – The new detector channel scale value to set.
verbose (bool, optional) – If True (default), prints/logs the location of the saved calibration file.
- Raises:
RuntimeError – If detector channel parameters cannot be loaded.
ValueError – If the specified meas_mode does not exist in the parameters.
KeyError – If required calibration keys are missing from the parameters.
- autoemx.calibrations.load_standards_model(meas_type: str, beam_energy: int, std_f_dir: str | None = None) tuple[EDSStandardsFile, str][source]
Load standards data for a specified technique and beam energy.
Called when performing quantifications.
- Parameters:
meas_type (str) – Corresponds to MeasurementConfig.type
beam_energy (int) – The beam energy (in keV) for which to load the standards.
std_f_dir (str) – Directory of reference standards file. Default : None; uses the default directory Typically only specified when using a special reference file, such as the case of measuring the extent of precursor intermixing.
- Returns:
standards_file (EDSStandardsFile) – Typed standards model.
standards_dir (str) – Path to the standards file that was loaded.
- Raises:
FileNotFoundError – If the standards file does not exist.
ValueError – If the standards file cannot be parsed as JSON.
Notes
Expects a file named ‘{meas_type}_Stds_{beam_energy}keV.json’ in std_f_dir or, by default, in the local calibration folder of the microscope (~/.autoemx/calibrations/<microscope_ID>/), where the file of the package is copied the first time it is needed. beam_energy will be converted to an int for the file name’s purpose.