py21cmfast.input_serialization¶
Functions for handling InputParameters for various purposes, e.g. I/O and unstructuring.
Functions¶
|
Convert an InputParameters object to a dictionary, with various options. |
|
Construct a dictionary of InputStructs ready to be converted to InputParameters. |
|
Prepare an inputs class for serialization (to e.g. TOML, YAML or HDF5). |
Module Contents¶
- py21cmfast.input_serialization.convert_inputs_to_dict(inputs, mode='full', only_structs=True, camel=True, only_cstruct_params=False, use_aliases=True)[source]¶
Convert an InputParameters object to a dictionary, with various options.
While the
asdictmethod allows several options for “unstructuring” the inputs to a dictionary, this function is much more flexible, having options that cover a number of use-cases.- Parameters:
inputs – The input parameters to convert to a dict.
mode – Either ‘minimal’ to get the minimal dictionary required to specify the parameters (on top of defaults) or ‘full’ to keep all parameters.
only_structs – Whether to only return InputStruct objects (unstructured to dicts), otherwise also return other attributes (e.g. random_seed).
camel – Whether the keys of the returned dict should be camel-case, e.g. SimulationOptions. Otherwise, return as snake_case. Only applies to InputStruct attributes of the inputs (i.e. not node_redshifts or random_seed).
only_cstruct_params – Only return parameters that are part of the Cstruct, rather than all fields of the class. This is useful for pretty-printing.
use_aliases – If True, use correct aliases for parameters, which allows the dictionary to be passed back into the InputStruct constructors (e.g. use DIM instead of _DIM).
- Parameters:
mode (Literal['full', 'minimal'])
only_structs (bool)
camel (bool)
only_cstruct_params (bool)
use_aliases (bool)
- Return type:
dict[str, dict[str, Any]]
- py21cmfast.input_serialization.deserialize_inputs(dict_of_structdicts, safe=True, **loose_params)[source]¶
Construct a dictionary of InputStructs ready to be converted to InputParameters.
- Parameters:
dict_of_structdicts – A dictionary whose keys are names of InputStruct attributes of InputParameters (e.g. CosmoParams, SimulationOptions), and whose values are dictionaries of parameters specific to each struct. Not every parameter of every struct is required.
- Other Parameters:
All other parameters are considered “loose” parameters of one of the InputStructs,
and will *override* the parameter matching their name if it is found in one of the
structs.
- Returns:
dict_of_structs – A dictionary whose keys are attribute names of the InputParameters class, and whose values are InputStruct instances, ready to instantiate an InputParameters class, e.g.
InputParameters(**dict_of_structs, random_seed=1).- Parameters:
dict_of_structdicts (dict[str, Any])
safe (bool)
- Return type:
dict[str, py21cmfast.wrapper.inputs.InputStruct]
- py21cmfast.input_serialization.prepare_inputs_for_serialization(inputs, mode='full', only_structs=True, camel=True)[source]¶
Prepare an inputs class for serialization (to e.g. TOML, YAML or HDF5).
This is a thin wrapper around
convert_inputs_to_dict()that also ensures that ‘None’ values are removed (so long as their default is also None) and that the parameter names map back to aliases of InputStruct attributes.- Parameters:
mode (Literal['full', 'minimal'])
only_structs (bool)
camel (bool)
- Return type:
dict[str, dict[str, Any]]