hispecdrp.primitives.base_primitive
Classes
|
- class BasePrimitive(dataset=None, config=None, context=None, log_config=None, parent=None, **kwargs)[source]
Bases:
ProcessingComponent- _on_exception(exc)[source]
Hook called when an exception occurs during processing. By default, it does nothing.
- _on_skip()[source]
Hook called when the primitive is skipped due to pre-condition check failing. By default, it does nothing.
- _pre_condition()[source]
Checks if all preconditions are met in order to run this primitive. The base class checks if
self.runisTrue. If not met, the primitive will be skipped. This can be overridden to implement custom pre-condition checks.- Returns:
pre_condition (bool) – The precondition status.
- add_calibration_entry(model, calibration, cal_role)[source]
Add a calibration to the DataModel’s calibrations table.
- add_data_ancestors(model, sources)[source]
- add_receipt_entry(model)[source]
Add an entry to the receipt table of the model summarizing the processing.
- Parameters:
model (DataModel) – The data model to add the receipt entry to.
- apply(dataset=None, **kwargs)[source]
The main method a user calls to run a primitive.
- Parameters:
input – The input data to process and is up to the subclass to decide what this represents, but is typically a datamodel, or simple containers of datamodels (or filepaths).
**kwargs – Additional keyword arguments to set or override for this primitive. These will override any arguments set during initialization.
- Returns:
dict – The main output of the primitive (self._output[‘output’]), which is typically a datamodel or a container of datamodels.
- classmethod call(dataset, config=None, context=None, parent=None, log_config=None, return_instance=False, **kwargs)[source]
Top level method to instantiate a primitive or pipeline and run it in one call.
- Parameters:
dataset (object) – The dataset to process.
context (object, optional) – The context in which the primitive is being called.
config (object, optional) – The configuration to use for the primitive. Can be a dict, a config file path, or a ConfigFile object.
parent (BasePipeline, optional) – The parent pipeline of this primitive.
log_config (dict, str, or None, optional) – The logging configuration to use for the primitive.
return_instance (bool, optional) – If True, return a tuple of (result, primitive_instance). If False, return only the result.
kwargs (dict) – Override primitive parameters.
- Returns:
results (Any) – The result of processing the input with the primitive.
- finalize_model(model)[source]
Finalize a single datamodel by adding receipt entries, used calibrations, and updating metadata.
- finalize_output()[source]
Hook called at the end of
primitive.applyto finalize the output. Seefinalize_modelfor details.
- classmethod from_config_file(config, **kwargs)[source]
Initialize a primitive from a configuration file.
- Parameters:
config (str, dict, or ConfigFile, optional.) –
- The configuration for the primitive. Can be one of:
str: A full path to a configuration file or a filename in the ConfigStore.
dict: A dictionary of configuration parameters.
ConfigFile: A ConfigFile instance.
If not provided, the primitive will attempt to auto-configure based on the input dataset and context. If the dataset is required for auto-configuration, it must be provided.
kwargs (dict) – Additional keyword arguments to pass to the primitive constructor.
- Returns:
BasePrimitive – An instance of the primitive with the configuration loaded.
- get_calibration(input_model, cal_role, selector=None, use_cached=None)[source]
Primary method for a primitive to call in order to get a calibration file in the context of a primitive.
- Parameters:
input (str or DataModel) – The filepath or datamodel to get a calibration for.
cal_role (str) – The role of calibration in this primitive, which should match a key in
self.CALIBRATIONS.selector (CalibrationSelector or None) – An optional selector to use instead of the default selector defined in
self.CALIBRATIONS.use_cached (bool or None) – If True, use cached calibrations if available. If None, use the default setting from the configuration.
- Returns:
tuple[str | DataModel, dict | None] – A tuple of (calibration_file_or_model, cal_record).
- get_effective_config()[source]
- get_output_model(dataset=None, output_model=None, **kwargs)[source]
Get the output model based on the input model and the specified output model type.
- Parameters:
dataset (Any, optional) – The dataset to use for creating the output model.
output_model (str | datamodels.DataModel | type[datamodels.DataModel], optional) –
The desired output model type or specification. It can take on several values:
datamodels.DataModel instance: Returns output_model as is.
’input’ (str): Returns input_model as is.
’input_copy’ (str): Returns a copy of input_model.
Valid filepath (str): Opens and returns the model from the specified file.
DataModel class (type[DataModel]): Instantiates and returns a new instance of the specified DataModel class using input_model as a template with
output_model.from_datamodels(input_model). If input_model.meta.instrument_name is set, it will attempt to use an instrument-specific subclass if available.DataModel class name for specific instrument or instrument agnostic class (str): Instantiates and returns a new instance of the specified DataModel class with
output_model.from_datamodels(input_model). If input_model.meta.instrument_name is set, it will attempt to use an instrument-specific subclass if available.None (default value): Falls back to
self.output_model.
**kwargs (Additional keyword arguments passed to DataModel.from_datamodels().)
- Returns:
datamodels.DataModel – The instantiated output DataModel class.
- get_pars_str_for_receipt()[source]
- init_calibrations()[source]
Process calibration configurations applying selector and kwargs logic.
Handles 4 cases for each calibration: 1. No entry in config -> use default selector and kwargs 2. Selector provided -> merge kwargs if same as default, else use user kwargs only 3. Kwargs only -> use default selector and merge kwargs 4. Both selector and kwargs -> validate and apply appropriately
- Parameters:
cal_config_dict (dict) – The calibrations config dictionary to process
- init_config(dataset=None, config=None, context=None)[source]
- init_parameters(**kwargs)[source]
- Initialize parameters including calibrations as top-level attributes with the following priorities:
self.PARAMETERS (lowest priority)
self.config.parameters
Direct attribute setting (highest priority)
- Parameters:
**kwargs – Direct attribute settings to override config and defaults. To override a calibration, provide the calibration name as a keyword argument. Selector logic is not run here.