pypeit.par.util module

Utility functions for PypeIt parameter sets

pypeit.par.util._eval_ignore()[source]

Provides a list of strings that should not be evaluated.

pypeit.par.util.eval_tuple(inp)[source]

Evaluate the input to one or more tuples.

This allows conversion of one or more tuples provided to a configuration parameters.

Warning

  • Currently can only handle simple components that can also be evaluated (e.g., integers and floats).

Parameters:

inp (list) – A list of strings that are converted into a list of tuples. The parentheses must be within the list of elements.

Returns:

The list of tuples.

Return type:

list

pypeit.par.util.get_parset_list(cfg, pk, parsetclass)[source]

Create a list of ParSets based on a root keyword for a set of defined groups in the configuration file.

For example, the InstrumentPar group allows for a list of detectors (DetectorPar) with keywords like detector1, detector2, etc. This function parses the provided configuration object (cfg) to find any sections with detector (pk) as its root. The remainder of the section name must be able to be converted to an integer and the section itself must be able to setup an instance of parsetclass. The sections must be number sequentially from 1..N. E.g., the InstrumentPar configuration file cannot have dectector1 and detector3, but no detector2. The call to setup the detectors in the InstrumentPar is:

kwargs['detector'] = get_parset_list(cfg, 'detector', DetectorPar)
Parameters:
  • cfg (configobj, dict) – The top-level configuration that defines a list of sub-ParSets.

  • pk (str) – The root of the keywords used to set a list of sub-ParSets.

  • parsetclass (pypeit.par.parset.ParSet) – The class used to construct each element in the list of parameter subsets. The class must have a from_dict method that instantiates the pypeit.par.parset.ParSet based on the provide subsection/subdict from cfg.

Returns:

A list of instances of parsetclass parsed from the provided configuration data.

Return type:

list

Raises:

ValueError – Raised if the indices of the subsections are not sequential and 1-indexed.

pypeit.par.util.parset_to_dict(par)[source]

Convert the provided parset into a dictionary.

Parameters:

par (ParSet) –

Returns:

Converted ParSet

Return type:

dict

pypeit.par.util.recursive_dict_evaluate(d)[source]

Recursively run eval() on each element of the provided dictionary.

A raw read of a configuration file with configobj results in a dictionary that contains strings or lists of strings. However, when assigning the values for the various ParSets, the from_dict methods expect the dictionary values to have the appropriate type. E.g., the configobj will have something like d[‘foo’] = ‘1’, when the from_dict method expects the value to be an integer (d[‘foo’] = 1).

This function tries to evaluate all dictionary values, except for those listed above in the _eval_ignore() function. Any value in this list or where:

eval(d[k]) for k in d.keys()

raises an exception is returned as the original string.

This is currently only used in from_cfg_file(); see further comments there.

Parameters:

d (dict) – Dictionary of values to evaluate

Returns:

Identical to input dictionary, but with all string values replaced with the result of eval(d[k]) for all k in d.keys().

Return type:

dict