Skip to content

Commit

Permalink
Initial implementation for Instrument
Browse files Browse the repository at this point in the history
The model properties will not work yet
  • Loading branch information
Rastislav Turanyi committed Jan 17, 2025
1 parent ffdb9a0 commit 42fb663
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions src/resolution_functions/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,7 @@ def _get_model_data(self, model_name: Optional[str] = None, **kwargs) -> tuple[M
if model_name is None:
model_name = self.default_model

try:
model = self._models[model_name]
except KeyError:
raise InvalidModelError(model_name, self)
model = self._resolve_model(model_name)

available_configurations = model['configurations']

Expand All @@ -449,6 +446,33 @@ def _get_model_data(self, model_name: Optional[str] = None, **kwargs) -> tuple[M
**ChainMap(*configurations, model['parameters']))
return model, model_name

def _resolve_model(self, model_name: str) -> dict:
"""
Returns the data for the `model_name` model, taking into account aliases.
Parameters
----------
model_name
The name of the model whose data to retrieve.
Returns
-------
model_data
The dictionary with the model's data.
"""
try:
model = self._models[model_name]
except KeyError:
raise InvalidModelError(model_name, self)

if isinstance(model, str):
try:
model = self._models[model]
except KeyError:
raise InvalidModelError(model, self)

return model

def get_resolution_function(self, model_name: Optional[str] = None, **kwargs) -> InstrumentModel:
"""
Generates a :term:`resolution function`, as modelled by the `model_name` :term:`model`.
Expand Down Expand Up @@ -590,7 +614,7 @@ def get_model_signature(self, model_name: Optional[str] = None) -> Signature:
annotation=Optional[str])
}

for configuration_name, options in self._models[model_name]['configurations'].items():
for configuration_name, options in model_data['configurations'].items():
option_names = self._get_options(options)
params[configuration_name] = Parameter(configuration_name,
Parameter.KEYWORD_ONLY,
Expand Down Expand Up @@ -685,12 +709,7 @@ def possible_configurations_for_model(self, model_name: str) -> list[str]:
InvalidModelError
If the provided `model_name` is not supported for this version of this instrument.
"""
try:
model = self._models[model_name]
except KeyError:
raise InvalidModelError(model_name, self)

return list(model['configurations'].keys())
return list(self._resolve_model(model_name)['configurations'].keys())

def possible_options_for_model(self, model_name: str) -> dict[str, list[str]]:
"""
Expand All @@ -713,10 +732,7 @@ def possible_options_for_model(self, model_name: str) -> dict[str, list[str]]:
InvalidModelError
If the provided `model_name` is not supported for this version of this instrument.
"""
try:
model = self._models[model_name]
except KeyError:
raise InvalidModelError(model_name, self)
model = self._resolve_model(model_name)

return {config: self._get_options(value)
for config, value in model['configurations'].items()}
Expand Down Expand Up @@ -748,12 +764,7 @@ def possible_options_for_model_and_configuration(self,
If the provided `configuration` is not supported for the `model_name` model of this
instrument.
"""
try:
model = self._models[model_name]
except KeyError:
raise InvalidModelError(model_name, self)

configurations = model['configurations']
configurations = self._resolve_model(model_name)['configurations']

try:
configurations = configurations[configuration]
Expand Down Expand Up @@ -809,12 +820,7 @@ def default_option_for_configuration(self, model_name: str, configuration: str)
If the provided `configuration` is not supported for the `model_name` model of this
instrument.
"""
try:
model = self._models[model_name]
except KeyError:
raise InvalidModelError(model_name, self)

configurations = model['configurations']
configurations = self._resolve_model(model_name)['configurations']

try:
configurations = configurations[configuration]
Expand Down

0 comments on commit 42fb663

Please sign in to comment.