general_functions module

Some general functions related to the convolution codebase.

Mostly unsorted, likely better placed in together with related functionality.

class syntheticstellarpopconvolve.general_functions.JsonCustomEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: JSONEncoder

Support for data types that JSON default encoder does not do.

This includes:

  • Numpy array or number

  • Complex number

  • Set

  • Bytes

  • astropy.UnitBase

  • astropy.Quantity

Examples

>>> import json
>>> import numpy as np
>>> from astropy.utils.misc import JsonCustomEncoder
>>> json.dumps(np.arange(3), cls=JsonCustomEncoder)
'[0, 1, 2]'

copied from astropy and extended

default(obj)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
syntheticstellarpopconvolve.general_functions.calculate_bin_edges(arr)[source]

Function to calculate the edge values given a bunch of centers

syntheticstellarpopconvolve.general_functions.calculate_bincenters(array, convert='linear')[source]

Function to calculate bincenters

TODO: allow other conversions

syntheticstellarpopconvolve.general_functions.check_required(config, required_list)[source]

Function to check if the keys in the required_list are present in the convolution_instruction dict

syntheticstellarpopconvolve.general_functions.create_job_dict(config, sfr_dict, data_dict, convolution_instruction, time_bin_info_dict, bin_number)[source]

Function to create the job dict

syntheticstellarpopconvolve.general_functions.create_time_bin_info_dict(config, convolution_instruction, bin_number, bin_center, bin_edge_lower, bin_size, bin_type)[source]

Function to set up the time bin info dict

syntheticstellarpopconvolve.general_functions.custom_json_serializer(obj)[source]

Custom serialiser for binary_c to use when functions are present in the dictionary that we want to export.

Function objects will be turned into str representations of themselves

Parameters:

obj – The object that might not be serialisable

Returns:

Either string representation of object if the object is a function, or the object itself

syntheticstellarpopconvolve.general_functions.extract_data(config, convolution_instruction)[source]

Function to extract the data from the correct table and store the information in the correct column.

Only extracts what is required by the data column dict

syntheticstellarpopconvolve.general_functions.extract_unit_dict(output_hdf5_file, key)[source]

FUnction to extract the unit dict from the hdf5 file

syntheticstellarpopconvolve.general_functions.generate_boilerplate_outputfile(outputfile_name)[source]

Function to generate a boilerplate output file structure so the user does not have to worry about including the correct groups.

syntheticstellarpopconvolve.general_functions.generate_data_dict(config, convolution_instruction)[source]

Function to generate the data dict.

syntheticstellarpopconvolve.general_functions.generate_group_name(convolution_instruction, sfr_dict)[source]

Function to generate the group name. Also provides layers

syntheticstellarpopconvolve.general_functions.get_normalized_yield_unit(config, convolution_instruction)[source]

Function to get the normalized yield unit either from config or from convolution_instruction

syntheticstellarpopconvolve.general_functions.get_physical_dimensions(unit)[source]

Return the physical dimensions of a unit in sorted [M][L][T] notation using SI base units.

syntheticstellarpopconvolve.general_functions.get_tmp_dir(config, convolution_instruction, sfr_dict=None)[source]

Function to get tmp dir

syntheticstellarpopconvolve.general_functions.get_username()[source]

Function to get the username of the user that spawned the current process

syntheticstellarpopconvolve.general_functions.handle_custom_scaling_or_conversion(config, data_layer_or_column_dict_entry, value)[source]

Function that handles multiplying the key of the ensemble with some value or with some function

syntheticstellarpopconvolve.general_functions.has_unit(parameter, fail_on_dimensionless=True)[source]

Function to check if a parameter has any unit assigned to it

syntheticstellarpopconvolve.general_functions.has_unit_dimensionless_okay(parameter, *, fail_on_dimensionless=False)

Function to check if a parameter has any unit assigned to it

syntheticstellarpopconvolve.general_functions.is_mass_unit(parameter)[source]

Function to check if a parameter has time-units

syntheticstellarpopconvolve.general_functions.is_time_unit(parameter)[source]

Function to check if a parameter has time-units

syntheticstellarpopconvolve.general_functions.maybe_strip_scaled_dimensionless(q)[source]
syntheticstellarpopconvolve.general_functions.pad_function(array, left_val, right_val, relative_to_edge_val, axis=0)[source]

Function to pad an array

syntheticstellarpopconvolve.general_functions.print_hdf5_structure(f, subkey=None, detailed=True)[source]
syntheticstellarpopconvolve.general_functions.sample_around_bin_center(bin_edges, values)[source]

Basic function to handle sampling around bincenter given bin edges and values.

Note: this does not handle values that fall outside of the bins well

syntheticstellarpopconvolve.general_functions.temp_dir(*child_dirs, clean_path=False)[source]

Function to create directory within the TMP directory of the file system, starting with /<TMP>/binary_c_python-<username>

Makes use of os.makedirs exist_ok which requires python 3.2+

Parameters:
  • *child_dirs (str) – str input where each next input will be a child of the previous full_path. e.g. temp_dir('tests', 'grid') will become '/tmp/binary_c_python-<username>/tests/grid'

  • *clean_path (optional) – Boolean to make sure that the directory is cleaned if it exists

Return type:

str

Returns:

the path of a sub directory called binary_c_python in the TMP of the file system

syntheticstellarpopconvolve.general_functions.vb(message, verbosity, minimal_verbosity)[source]

Shorthand for verbose_print

syntheticstellarpopconvolve.general_functions.verbose_print(message, verbosity, minimal_verbosity)[source]

Function that decides whether to print a message based on the current verbosity and its minimum verbosity

if verbosity is equal or higher than the minimum, then we print

Parameters:
  • message (str) – message to print

  • verbosity (int) – current verbosity level

  • minimal_verbosity (int) – threshold verbosity above which to print

Return type:

None