sage_analysis.utils

sage_analysis.utils.generate_func_dict(plot_toggles, module_name, function_prefix, keyword_args={}) → Dict[str, Tuple[Callable, Dict[str, Any]]][source]

Generates a dictionary where the keys are the function name and the value is a list containing the function itself (0th element) and keyword arguments as a dictionary (1st element). All functions in the returned dictionary are expected to have the same call signature for non-keyword arguments. Functions are only added when the plot_toggles value is non-zero.

Functions are required to be named <module_name><function_prefix><plot_toggle_key> For example, the default calculation function are kept in the model.py module and are named calc_<toggle>. E.g., sage_analysis.model.calc_SMF(), sage_analysis.model.calc_BTF(), sage_analysis.model.calc_sSFR() etc.

Parameters:
  • plot_toggles (dict, [string, int]) – Dictionary specifying the name of each property/plot and whether the values will be generated + plotted. A value of 1 denotes plotting, whilst a value of 0 denotes not plotting. Entries with a value of 1 will be added to the function dictionary.
  • module_name (string) – Name of the module where the functions are located. If the functions are located in this module, pass an empty string “”.
  • function_prefix (string) – Prefix that is added to the start of each function.
  • keyword_args (dict [string, dict[string, variable]], optional) – Allows the adding of keyword aguments to the functions associated with the specified plot toggle. The name of each keyword argument and associated value is specified in the inner dictionary.
Returns:

func_dict – The key of this dictionary is the name of the function. The value is a list with the 0th element being the function and the 1st element being a dictionary of additional keyword arguments to be passed to the function. The inner dictionary is keyed by the keyword argument names with the value specifying the keyword argument value.

Return type:

dict [string, tuple(function, dict[string, variable])]

Examples

>>> import sage_analysis.example_calcs
>>> import sage_analysis.example_plots
>>> plot_toggles = {"SMF": 1}
>>> module_name = "sage_analysis.example_calcs"
>>> function_prefix = "calc_"
>>> generate_func_dict(plot_toggles, module_name, function_prefix) #doctest: +ELLIPSIS
{'calc_SMF': (<function calc_SMF at 0x...>, {})}
>>> module_name = "sage_analysis.example_plots"
>>> function_prefix = "plot_"
>>> generate_func_dict(plot_toggles, module_name, function_prefix) #doctest: +ELLIPSIS
{'plot_SMF': (<function plot_SMF at 0x...>, {})}
>>> import sage_analysis.example_plots
>>> plot_toggles = {"SMF": 1}
>>> module_name = "sage_analysis.example_plots"
>>> function_prefix = "plot_"
>>> keyword_args = {"SMF": {"plot_sub_populations": True}}
>>> generate_func_dict(plot_toggles, module_name, function_prefix, keyword_args) #doctest: +ELLIPSIS
{'plot_SMF': (<function plot_SMF at 0x...>, {'plot_sub_populations': True})}
>>> import sage_analysis.example_plots
>>> plot_toggles = {"SMF": 1, "quiescent": 1}
>>> module_name = "sage_analysis.example_plots"
>>> function_prefix = "plot_"
>>> keyword_args = {"SMF": {"plot_sub_populations": True},
...                 "quiescent": {"plot_output_format": "pdf", "plot_sub_populations": True}}
>>> generate_func_dict(plot_toggles, module_name, function_prefix, keyword_args) #doctest: +ELLIPSIS
{'plot_SMF': (<function plot_SMF at 0x...>, {'plot_sub_populations': True}), 'plot_quiescent': (<function plot_quiescent at 0x...>, {'plot_output_format': 'pdf', 'plot_sub_populations': True})}
sage_analysis.utils.read_generic_sage_params(sage_file_path: str) → Dict[str, Any][source]

Reads the SAGE parameter file values. This function is used for the default sage_binary and sage_hdf5 formats. If you have a custom format, you will need to write a read_sage_params function in your own data class.

Parameters:sage_file_path (string) – Path to the SAGE parameter file.
Returns:
  • model_dict (dict [str, var]) – Dictionary containing the parameter names and their values.
  • Errors
  • ——
  • FileNotFoundError – Raised if the specified SAGE parameter file is not found.
sage_analysis.utils.select_random_indices(inds: numpy.ndarray, global_num_inds_available: int, global_num_inds_requested: int, seed: Optional[int] = None) → numpy.ndarray[source]

Flag this with Manodeep to exactly use a descriptive docstring.

Parameters:
  • vals (ndarray of values) – Values that the random subset is selected from.
  • global_num_inds_available (int) – The total number of indices available across all files.
  • global_num_inds_requested (int) – The total number of indices requested across all files.
  • seed (int, optional) – If specified, seeds the random number generator with the specified seed.
Returns:

random_vals – Values chosen.

Return type:

ndarray of values

Examples

>>> import numpy as np
>>> seed = 666
>>> inds = np.arange(10)
>>> global_num_inds_available = 100
>>> global_num_inds_requested = 50 # Request less than the number of inds available
...                                # across all files, but more than is in this file.
>>> select_random_indices(inds, global_num_inds_available, global_num_inds_requested, seed) # Returns a random subset.
array([2, 6, 9, 4, 3])
>>> import numpy as np
>>> seed = 666
>>> inds = np.arange(30)
>>> global_num_inds_available = 100
>>> global_num_inds_requested = 10 # Request less than the number of inds available
...                                # across all files, and also less than what is
...                                # available in this file.
>>> select_random_indices(inds, global_num_inds_available, global_num_inds_requested, seed) # Returns a random subset.
array([12,  2, 13])
>>> import numpy as np
>>> inds = np.arange(10)
>>> global_num_inds_available = 100
>>> global_num_inds_requested = 500 # Request more than the number of inds available
...                                 # across all file.
>>> select_random_indices(inds, global_num_inds_available, global_num_inds_requested) # All input indices are returned.
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])