swiftemulator.io.swift module
I/O functions for reading in SWIFT simulation data.
Includes functions to read parameters to
instances of :class:ModelParameters, and functions
to read model data to instances of :class:ModelValues.
Also includes functions to write out :class:ModelParameters
as files, based on a base parameter file.
- swiftemulator.io.swift.load_pipeline_outputs(filenames: Dict[Hashable, Path], scaling_relations: List[str], log_independent: Optional[List[str]] = None, log_dependent: Optional[List[str]] = None, histogram_for_error: Optional[str] = None, sanitize_output: bool = False) Tuple[Dict[str, ModelValues], Dict[str, Dict[str, Union[str, bool]]]][source]
Loads the pipeline outputs from the provided files, for the given specified scaling relations, into a :class:
ModelValuescontainer.- Parameters:
filenames – Paths to files to load data from, with the keys in the dictionary the unique identifiers for the models to use throughout.
Dict[Hashable – Paths to files to load data from, with the keys in the dictionary the unique identifiers for the models to use throughout.
Path] – Paths to files to load data from, with the keys in the dictionary the unique identifiers for the models to use throughout.
scaling_relations (List[str]) – Top-level name for the scaling relations (i.e. the top-level item in the yaml file, e.g.
stellar_mass_function_100).log_independent (List[str], optional) – Scaling relations (the same as in
scaling_relations) where the independent values (given bycentersin the yaml files) should be log-scaled (useslog10).log_dependent (List[str], optional) – Scaling relations (the same as in
scaling_relations) where the dependent values (given byvaluesin the yaml files) should be log-scaled (useslog10).histogram_for_error (str, optional) – If provided, a histogram to use for sampling-based errors on each bin. This can be beneficial versus the scatter-based default errors when there is significant intrinsic scatter. Errors are reported as
dependent / sqrt(N)(orlog(1 + 1 / sqrt(N)in the case of log-scaled). Note that you _must_ ensure consistent binning between the other input scaling relations and this histogram, or there will be an error.sanitize_output (bool, optional) – If set to True, removes all zero-valued, infinity, and NaN dependent and independent values from the data. This helps sanitize input for the emulator, but is slower.
- Returns:
model_values, Dict[str, ModelValues] – Dictionary of
ModelValuescontainers for each scaling relation, read from the files. The keys are the names of the scaling relations.unit_dict, Dict[str, Dict[str, Union[str, bool]]] – Dictionary of symbolic units for each scaling relation. Has the structure:
{scaling_relation: {independent: "Msun", dependent: "kpc", log_independent: True, log_dependent: True}}.
- swiftemulator.io.swift.load_parameter_files(filenames: Dict[Hashable, Path], parameters: List[str], log_parameters: Optional[List[str]] = None, parameter_printable_names: Optional[List[str]] = None, parameter_limits: Optional[List[List[float]]] = None) Tuple[ModelSpecification, ModelParameters][source]
Loads information from the parameter files and returns the associated model specification and model parameters instances.
- Parameters:
filenames – Paths to the parameter files, keyed by their unique identifiers (i.e. those also used in
load_pipeline_outputs()).Dict[Hashable – Paths to the parameter files, keyed by their unique identifiers (i.e. those also used in
load_pipeline_outputs()).Path] – Paths to the parameter files, keyed by their unique identifiers (i.e. those also used in
load_pipeline_outputs()).parameters – Parameters to load from the yaml files. Should be specified in the same way as the
--paramoption in SWIFT, i.e. in the formatSectionName:ParameterName.List[str] – Parameters to load from the yaml files. Should be specified in the same way as the
--paramoption in SWIFT, i.e. in the formatSectionName:ParameterName.log_parameters – Which parameters in the list above should be scaled logarithmically.
List[str] – Which parameters in the list above should be scaled logarithmically.
optional – Which parameters in the list above should be scaled logarithmically.
parameter_printable_names – Optional ‘fancy’ names for your parameters. These strings will be used on any figures generated through swift-emulator. Can include LaTeX formatting as in
matplotlib.List[str] – Optional ‘fancy’ names for your parameters. These strings will be used on any figures generated through swift-emulator. Can include LaTeX formatting as in
matplotlib.optional – Optional ‘fancy’ names for your parameters. These strings will be used on any figures generated through swift-emulator. Can include LaTeX formatting as in
matplotlib.parameter_limits – The lower and upper limit of the input parameters. Should be the same length as
parameters, but each item is a list of length two, with a lower and upper bound. For example, in a two parameter model[[0.0, 1.0], [8.3, 9.3]]would mean that the first parameter would vary between 0.0 and 1.0, with the second parameter varying between 8.3 and 9.3. If not provided, these will be inferred from the data.List[List[float]] – The lower and upper limit of the input parameters. Should be the same length as
parameters, but each item is a list of length two, with a lower and upper bound. For example, in a two parameter model[[0.0, 1.0], [8.3, 9.3]]would mean that the first parameter would vary between 0.0 and 1.0, with the second parameter varying between 8.3 and 9.3. If not provided, these will be inferred from the data.optional – The lower and upper limit of the input parameters. Should be the same length as
parameters, but each item is a list of length two, with a lower and upper bound. For example, in a two parameter model[[0.0, 1.0], [8.3, 9.3]]would mean that the first parameter would vary between 0.0 and 1.0, with the second parameter varying between 8.3 and 9.3. If not provided, these will be inferred from the data.
- Returns:
model_specification, ModelSpecification – Specification for the model based on the parameters that have been passed to this function.
model_parameters, ModelParameters – Model parameter container corresponding to the SWIFT parameter files.
- swiftemulator.io.swift.write_parameter_files(filenames: Dict[Hashable, Path], model_parameters: ModelParameters, parameter_transforms: Optional[Dict[str, Callable]] = None, base_parameter_file: Optional[Path] = None)[source]
Writes parameter files, containing the parameters from a
ModelParametersinstance, based on a base parameter file.- Parameters:
filenames (Dict[Hashable, Path]) – Dictionary stating where to write each parameter file, based upon the unique identifiers of each run in
model_parameters.model_parameters (ModelParameters) – Varied parameters to write in the output files.
parameter_transforms (Dict[str, Callable], optional) – Parameter transformation functions for transforming parameter values before writing. Parameters may be generated (and emulated) in a space that is very different to their meaning in the code. Hence, this parameter allows for a transformation (for instance, a logrithmic transformation). For each parameter that should be transformed (keys) there should be a function taking the emulated value, transforming it into the code value. For instance, if a parameter is emulated in logarithmic space, this should be
lambda x: 10**x.base_parameter_file (Path, optional) – Base parameter file to read. The parameters specified in
model_parameterswill be overwritten when writing each individual file, but the rest will remain the same.
Notes
Also changes the value of
MetaData:run_nameto the unique identifiers.