sage_analysis.utils

sage_analysis.utils.find_closest_indices(values: List[float], target_values: List[float]) → List[int][source]

Finds the indices in values that result in values closest to target_values.

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])]

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]

Select a random subset of indices if the total number of indices (across all files) is known. This function is used if selecting (e.g.,) 100 galaxies from a sample of 10,000.

However, if the total number of indices is NOT known, then this function is not valid. For example, if one wanted to select 100 spiral galaxies, we may not know how many spiral galaxies are present across all files. In such scenarios, select_random_indices_assumed_equal_distribution() should be used.

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_inds – Values chosen.

Return type:

ndarray of values