"""Specify the configurable parameters for a calculator."""from__future__importannotationsfromcollections.abcimportIterablefromcollections.abcimportIteratorfromcollections.abcimportMutableMappingfromimportlibimportimport_modulefromtypingimportTYPE_CHECKINGfromtypingimportAnyfrommonty.jsonimportMSONableifTYPE_CHECKING:fromautojob.calculationimportparameters
[docs]classCalculatorConfiguration(MutableMapping,MSONable):"""A set of calculator parameters."""def__init__(self,calculator_parameters:Iterable[parameters.CalculatorParameter],values:Iterable|None=None,)->None:"""Create a ``CalculatorConfiguration`` from parameters and values. Args: calculator_parameters: An iterable of :class:`.parameters.CalculatorParameter` s from which to create the configuration. values: The values with which to initialize the configuration. Defaults to None, in which case, the defaults of the :class:`.parameters.CalculatorParameter` s are used. """ifvaluesisNone:self._dict=CalculatorConfiguration.initialize_values(calculator_parameters)else:self._dict=dict(zip(calculator_parameters,values,strict=False))
[docs]@staticmethoddefinitialize_values(calculator_parameters:Iterable[parameters.CalculatorParameter],)->dict[parameters.CalculatorParameter,Any]:"""Initialize the values of a ``CalculatorConfiguration``. Args: calculator_parameters: An iterable of :class:`.parameters.CalculatorParameter` s for which from which to initialize the ``CalculatorConfiguration``. Returns: A dictionary mapping :class:`.parameters.CalculatorParameter` s to their value in the configuration. """param_vals={}forcalculator_parameterincalculator_parameters:ifcalculator_parameter.defaultisnotNone:param_vals[calculator_parameter]=calculator_parameter.defaultelse:param_vals[calculator_parameter]=Nonereturnparam_vals
def__getitem__(self,__k:parameters.CalculatorParameter)->Any:"""Get the value of ``CalculatorParameter``."""returnself._dict[__k]def__setitem__(self,__k:parameters.CalculatorParameter,__v:Any):"""Set the value of ``CalculatorParameter``."""self._dict[__k]=__vdef__delitem__(self,__v:parameters.CalculatorParameter):"""Delete the value of ``CalculatorParameter``."""delself._dict[__v]def__iter__(self)->Iterator[parameters.CalculatorParameter]:"""Iterate over the ``CalculatorParameter`` s in the configuration."""returniter(self._dict)def__len__(self)->int:"""Get the number of ``CalculatorParameter`` s in the configuration."""returnlen(self._dict)def__eq__(self,__o:object)->bool:"""Returns True if underlying mappings are equal. False otherwise."""ifnotisinstance(__o,CalculatorConfiguration):returnFalsereturn__o._dict==self._dict
[docs]defas_dict(self)->dict[str,Any]:"""Convert the ``CalculatorConfiguration`` to a dictionary. The :class:`.parameters.CalculatorParameter` s are stored as dictionaries, and the underlying dictionary is stored as a list of 2-tuples where the first element corresponds to the ``CalculatorParameter`` and the second element corresponds to the value. """parameters_and_values=[(p.as_dict(),value)forp,valueinself._dict.items()]return{"parameters_and_values":parameters_and_values,"@module":self.__class__.__module__,"@class":self.__class__.__name__,}
[docs]@classmethoddeffrom_dict(cls,d)->CalculatorConfiguration:"""Return a ``CalculatorConfiguration`` from a dictionary. This method is implemented to enable round-trips from :meth:`.calculators.CalculatorConfiguration.as_dict` and :meth:`.calculators.CalculatorConfiguration.from_dict`. """# Extract parameter dictsparameter_dicts=[xforx,_ind["parameters_and_values"]]# Extract parameter typeparams=[]forparameter_dictinparameter_dicts:module=import_module(parameter_dict["@module"])parameter_type=getattr(module,parameter_dict["@class"])constructor=parameter_type.from_dictparams.append(constructor(parameter_dict))# Extract parameter valuevalues=[yfor_,yind["parameters_and_values"]]returncls(params,values)