resins.instrument
The main entry-point to the library - contains the Instrument class used for managing all data and
computing resolution functions.
- class resins.instrument.Instrument(name: str, version: str, _models: dict[str, dict[str, str | dict[str, float | int | str | list[float] | dict] | dict[str, dict[str, float | int | str | list[float]]]]], default_model: str)
Bases:
objectInstrument is a representation of a physical INS instrument, containing all its associated data.
To be precise, it holds all information about one version of an instrument (for more about instrument versions, see Instruments), which makes it the centrepiece of this library; the data is necessary for computing the resolution functions.
However, this information is static and curated by the library, which is why
Instrumentis a frozen data class. It should never be instantiated directly; instead thefrom_defaultconstructor should be used. Similarly, it should not be inspected directly; a variety of methods and properties are provided for querying relevant information.Regardless, the most important function of Instrument is to construct a resolution function, which can be done by using the
get_resolution_functionmethod.- Parameters:
- name
The name of the INS instrument.
- version
The name of a particular version of that INS instrument.
- _models
A dictionary detailing all the models and their data available for this version of this instrument.
- default_model
The default model for this version of this instrument.
- Attributes:
- name
The name of the INS instrument represented by this instance.
- version
The version of the INS instrument represented by this instance.
- default_model
The name of the model for this version of this INS instrument that is used by default.
available_modelsA list of models available for this version of this instrument.
- available_models_and_configurations
- all_available_models_options
Methods
Lists all INS instruments currently available.
available_versions(instrument_name)Lists the names of all versions available for an INS instrument, as well as the default version.
default_option_for_configuration(model_name, ...)Returns the default option for the
configurationconfiguration of themodel_namemodel of this instrument.Formats all available models and configurations into a table.
Formats all available models, configurations, and options into a table.
from_default(name[, version])Instantiates an
Instrumentclass with the data of thenameINS instrument and itsversion.from_file(path[, version])Instantiates an
Instrumentfrom the data loaded from the file found atpath.get_model_data([model_name])Retrieves the physical parameters associated with the specified
model_name.get_model_signature([model_name])Constructs a call signature for the
get_resolution_functionmethod with a specific model.get_resolution_function([model_name])Generates a resolution function, as modelled by the
model_namemodel.possible_configurations_for_model(model_name)Returns all the configurations that the
model_namemodel supports.possible_options_for_model(model_name)Returns a dictionary mapping all the configurations of the
model_namemodel to their options.Lists each option that can be chosen for a given configuration of the
model_namemodel.- property all_available_models: list[str]
A list of all models available for this version of this instrument.
Includes both all the versions of all models (see
Instrument.available_unique_modelsand the recommended versions (seeInstrument.available_models).- Returns:
available_modelsA list of all available models.
- classmethod available_instruments() list[str]
Lists all INS instruments currently available.
- Returns:
instrument_listA list of names of INS instruments supported by this library.
- property available_models: list[str]
A list of models available for this version of this instrument.
Only includes the recommended version of each model - does not list all versions of all models.
- Returns:
available_modelsA list of available models.
- property available_unique_models: list[str]
A list of all unique models available for this version of this instrument.
Only includes the versioned models, i.e. lists all versions of all models, but does not list the recommended models.
- Returns:
available_modelsA list of all available unique models.
- classmethod available_versions(instrument_name: str) tuple[list[str], str]
Lists the names of all versions available for an INS instrument, as well as the default version.
- Parameters:
- instrument_name
The name of the INS instrument whose versions to retrieve.
- Returns:
available_versionsA list of the version names available for the instrument.
default_versionThe version of the instrument that is used by default.
Warning
This method performs an I/O (read) operation.
See also
available_instrumentsLists the available instruments
- default_option_for_configuration(model_name: str, configuration: str) str
Returns the default option for the
configurationconfiguration of themodel_namemodel of this instrument.- Parameters:
- model_name
The name of the model whose
configurationto look up.- configuration
The name of the configuration whose default option to retrieve.
- Returns:
default_optionThe default option for the
configurationconfiguration.
- Raises:
InvalidModelErrorIf the provided
model_nameis not supported for this version of this instrument.InvalidConfigurationErrorIf the provided
configurationis not supported for themodel_namemodel of this instrument.
- format_available_models_and_configurations() str
Formats all available models and configurations into a table.
The table shows each model and either which other model it is an alias for, or all the configurations required by that model.
- Returns:
strA string containing the nicely formatted table.
- format_available_models_options() str
Formats all available models, configurations, and options into a table.
The table shows each model and either which other model it is an alias for, or all the configurations required by that model. In the latter case, all the options for each configuration are also listed, and the default option is indicated.
- Returns:
strA string containing the nicely formatted table.
- classmethod from_default(name: str, version: str | None = None) Instrument
Instantiates an
Instrumentclass with the data of thenameINS instrument and itsversion.This is the primary, recommended way of instantiating the
Instrumentclass. It loads the instrument data as curated in this library for a particular version of an INS instrument.- Parameters:
- name
The name of the INS instrument to instantiate.
- version
The version of the
nameINS instrument to instantiate. If not provided, the default version of that instrument is instantiated.
- Returns:
instrumentAn instance of the
Instrumentclass containing the data corresponding tonameandversion.
- Raises:
InvalidInstrumentErrorIf the specified instrument
namedoes not exist.InvalidVersionErrorIf the specified
versionis not available for the specified instrumentname.
See also
available_instrumentsLists the available instruments
available_versionsLists the available versions of an instrument and its default version.
- classmethod from_file(path: str, version: str | None = None) Instrument
Instantiates an
Instrumentfrom the data loaded from the file found atpath.Please note that while this method is a part of the public API, it is marked as being for advanced use only. For most use cases, use the
from_defaultmethod.This method assumes that the data in the file follows the standard (please see … for more details). No validation is performed, so if there are any issues, either unhandled exceptions will be raised, or the errors will be silently propagated. For adding new instruments, versions, or models, please open an issue on our GitHub. For other purposes, please see … .
While the data file may contain multiple versions, only the one specified by
versionwill be saved in memory. If theversionparameter is not provided, the default version is read from the file.- Parameters:
- path
The path to the file containing the instrument data.
- version
The version of the instrument to load. If not provided,
the default_versionspecified in the file will be used.
- Returns:
instrumentAn instance of the
Instrumentclass containing the data found atpath
- Raises:
InvalidVersionErrorIf the file does not contain the provided
version.KeyErrorIf certain parts of the file are egregiously incorrect.
- get_model_data(model_name: str | None = None, **kwargs) ModelData
Retrieves the physical parameters associated with the specified
model_name.This method can be used for inspecting the parameters of a particular model, though it cannot be used to modify them. It returns a subclass of the
ModelDataclass corresponding to the particular model.Another use for this method is to inspect the default values for the model’s settings, as well as any restrictions that they might have, via the
defaultsandrestrictionsattributes.- Parameters:
- model_name
The name of the model whose parameters to retrieve. If not provided, the parameters of the default_model will be retrieved.
- **kwargs
Keyword arguments can be passed in to choose an option for each configuration specific to the
model_name. If not provided, default values are used. These are mainly useful when checking the instrument parameters, but for some models, the options may also affect thedefaultsandrestrictions.
- Returns:
model_dataThe data associated with
model_name.
See also
default_modelThe default model for this instrument.
available_modelsList of models available for this instrument
possible_configurations_for_modelList of configurations that can be chosen for this model
- get_model_signature(model_name: str | None = None) Signature
Constructs a call signature for the
get_resolution_functionmethod with a specific model.This method provides a programmatic way of inspecting the call signature of the
get_resolution_functionmethod required when calling it for themodel_namemodel. This is useful because its default signature uses the**kwargsconstruct to provide a unified interface, but in fact different models require different sets of values that have to be passed in through the keyword arguments.There are other methods and properties that can be used to inspect some of the options, but this method retrieves all the information and returns it as an
inspect.Signatureobject that can be used to examine the signature in detail. The only other comprehensive source of this information is the documentation for the relevant model.- Parameters:
- model_name
The name of the model whose signature to construct. If not provided, the signature of the
default_modelis constructed.
- Returns:
signatureThe call signature of the
get_resolution_methodfor themodel_namemodel.
- Raises:
InvalidModelErrorIf the provided
model_nameis not available for this version of this instrument.
See also
available_modelsList of models available for this version of this instrument.
get_model_dataAllows for checking the default values of and restrictions on model settings.
possible_options_for_modelLists the configurations and their options for a model.
Examples
>>> maps = Instrument.from_default('MAPS') >>> sig = maps.get_model_signature() >>> sig <Signature (model_name: Optional[str] = 'PyChop_fit', *, chopper_package: Literal['A', 'B', 'S'] = 'A', e_init: Annotated[ForwardRef('Optional[float]'), 'restriction=[0, 2000]'] = 500, chopper_frequency: Annotated[ForwardRef('Optional[int]'), 'restriction=[50, 601, 50]'] = 400, fitting_order: 'int' = 4, _) -> resins.models.pychop.PyChopModelFermi> >>> sig.parameters['e_init'] <Parameter "e_init: Annotated[ForwardRef('Optional[float]'), 'restriction=[0, 2000]'] = 500"> >>> sig.parameters['e_init'].kind <_ParameterKind.KEYWORD_ONLY: 3>
The
inspect.Signatureobject provides easy inspection of a call signature.
- get_resolution_function(model_name: str | None = None, **kwargs) InstrumentModel
Generates a resolution function, as modelled by the
model_namemodel.This method is the main use case of the
Instrumentclass. It generates a callable object that, when called, returns the resolution of the instrument at an energy and/or momentum value(s).However, while a simple, common interface is provided, different models (and sometimes the same model for different instruments!) require different configurations to be selected and different settings to be provided. All of these have to be passed in as keyword arguments (though sensible defaults are provided). These keyword arguments correspond to physical user choices made when running an INS experiment on the corresponding instrument. For example, direct instruments have a tunable incident energy, so their models usually require an
e_initsetting.For more information about a model, please see its corresponding documentation, or for programmatic querying, please see “How to programmatically query model”.
- Parameters:
- model_name
The name of the model to instantiate. If not provided, the
Instrument.default_modelis used.- **kwargs
Keyword arguments specifying the various configurations and settings of the
model_namemodel
- Returns:
modelAn instance of the requested
model_namemodel.
- Raises:
InvalidModelErrorIf the provided
model_nameis not available for this version of this instrument.InvalidInputErrorIf the model has restrictions on its inputs and these have been violated. The restrictions can be checked by using the
get_model_datamethod and then viewing therestrictionsattribute of the returned object.ExceptionOther model-specific exceptions may be raised.
Warning
If there are any mistakes in the model-specific parameters passed in as keyword arguments, they will be silently ignored and the default values for the missing parameters will be used.
See also
available_modelsList of models available for this version of this instrument
get_model_signatureConstructs a call signature for calling this method for a particular model.
Examples
>>> from resins import Instrument >>> tosca = Instrument.from_default('TOSCA', 'TOSCA') >>> print(tosca.get_resolution_function()) PolynomialModel1D(citation="")
If a model is not provided, the default model will be used - this differs between versions and instruments.
>>> print(tosca.get_resolution_function('vision')) VisionPaperModel(citation="https://doi.org/10.1016/j.nima.2009.03.204")
- possible_configurations_for_model(model_name: str) list[str]
Returns all the configurations that the
model_namemodel supports.- Parameters:
- model_name
The name of the model whose configurations to retrieve.
- Returns:
configurationsA list of configurations available for the
model_namemodel.
- Raises:
InvalidModelErrorIf the provided
model_nameis not supported for this version of this instrument.
- possible_options_for_model(model_name: str) dict[str, list[str]]
Returns a dictionary mapping all the configurations of the
model_namemodel to their options.- Parameters:
- model_name
The name of the model whose configurations to retrieve.
- Returns:
configurations_and_optionsAll the configurations available for the
model_namemodel and all their possible options.
- Raises:
InvalidModelErrorIf the provided
model_nameis not supported for this version of this instrument.
- possible_options_for_model_and_configuration(model_name: str, configuration: str) list[str]
Lists each option that can be chosen for a given configuration of the
model_namemodel.- Parameters:
- model_name
The name of the model to which the
configurationbelongs.- configuration
The name of the configuration whose options to retrieve.
- Returns:
optionsA list of options available for the
configurationandmodel_name.
- Raises:
InvalidModelErrorIf the provided
model_nameis not supported for this version of this instrument.InvalidConfigurationErrorIf the provided
configurationis not supported for themodel_namemodel of this instrument.
- exception resins.instrument.InvalidConfigurationError(provided_name: str, model_name: str, instrument: Instrument)
Bases:
ExceptionAn Exception representing an invalid user input for the configuration of a model of an instrument.
This class does not support an arbitrary message; instead the message is constructed in here from the provided information.
- Parameters:
- provided_name
The invalid name for the configuration that the user provided.
- model_name
The name of the model for which the
provided_namewas provided.- instrument
The instance of the
Instrumentobject in which theprovided_namewas used.
- exception resins.instrument.InvalidInstrumentError
Bases:
ExceptionAn Exception representing an invalid user input for the instrument name.
- exception resins.instrument.InvalidModelError(provided_name: str, instrument: Instrument)
Bases:
ExceptionAn Exception representing an invalid user input for the model of an instrument.
This class does not support an arbitrary message; instead the message is constructed in here from the provided information.
- Parameters:
- provided_name
The invalid name for the model that the user provided.
- instrument
The instance of the
Instrumentobject in which theprovided_namewas used.
- exception resins.instrument.InvalidVersionError
Bases:
ExceptionAn Exception representing an invalid user input for the version of an instrument.
- resins.instrument.INSTRUMENT_MAP: dict[str, tuple[str, None | str]]
A mapping of the name of an instrument to its data file and the version it’s aliasing, for all available instruments.
Has the form
{'instrument_name': ('file_name.yaml', alias_for)}whereinstrument_nameis the public, official name of the instrument that is used when creating it,file_name.yamlis the name of the YAML data file containing the data for that instrument (without the full path), andalias_foris either