Simulation Utilities¶
Utility functions for simulation setup, runs and handling data.
- sim_utils.check_sim_dup(sim_id: str, sim_num: int) bool¶
Checks if a simulation number exists in the HDF5 file for a given simulation ID.
- Parameters:
sim_id (int) – The ID of the simulation.
sim_num (int) – The number of the simulation to check for duplication.
- Returns:
True if the simulation number exists in the file, False otherwise.
- Return type:
bool
- sim_utils.find_sim_num(params: dict, param_check: dict) dict¶
Find simulation numbers that was run with a given set of paramters.
Useful for analysis. For e.g Finding the simulation number that was run with
si_peak=1.0- Parameters:
params (dict or Param) – Dictionary of all simulations generated after a run of multi simulation.
param_check (dict) – A dictionary of parameters to check for. E.g: {si_peak: 1}
- Returns:
- A subset of global dictoinary containing only the simulations
that match the given parameters in param_check
- Return type:
matches (dict)
- sim_utils.get_git_commit_hash()¶
Retrieves the current Git commit hash.
- Returns:
The Git commit hash as a string if the command is successful. None: If there is an error executing the command.
- Return type:
str
- sim_utils.get_module_from_path(file_path: str) str¶
Convert a file path to a module name.
Used to import specs file that is passed as as argument to the simulation setup scripts.
- Parameters:
file_path (str) – The relative file path (e.g., “specs/s_template.py”)
- Returns:
The corresponding module name (e.g., “specs.s_template”)
- Return type:
str
- sim_utils.get_multiples_with_remainder(N, k)¶
Returns a list of multiples of k upto N.
- Parameters:
N (int) – The upper limit (exclusive) for the multiples.
k (int) – The step size.
- sim_utils.get_sim_num(iters: tuple, n_iters: tuple) int¶
Calculate the simulation number for the running iterartor indices.
Useful for analysis.
- Parameters:
iters (tuple) – A tuple of current iteration indices.
n_iters (tuple) – A tuple of the total size for each iterator.
- Returns:
simulation number matching the given iterator indices.
- Return type:
int
- sim_utils.json_modify(obj: dict, fname: str)¶
Modify an entry in JSON file using the given dictionary.
If the file exists, it updates the file with the key-value pairs from the object. If the file does not exist, it creates a new file with the given object.
- Parameters:
obj (dict) – The dictionary object containing key-value pairs to be added or updated in the JSON file.
fname (str) – The file name (including path) of the JSON file to be modified.
- sim_utils.json_read(fname: str) dict¶
Wrapper function to read a JSON file and return the dictionary object.
- Parameters:
fname (str) – The file name to read the dictionary from.
- Returns:
The dictionary object read from the JSON file.
- Return type:
dict
- sim_utils.json_save(obj: dict, fname: str)¶
Wrapper function to save a dictionary object to a JSON file.
- Parameters:
obj (dict) – The dictionary object to save.
fname (str) – The file name to save the dictionary to.
- sim_utils.list_to_numpy(LoL: list, fill: float = nan) ndarray¶
Converts a list of lists (LoL) to a NumPy array, filling missing values with a specified fill value.
Used for to convert list spike times with non-homogeneous lengths to a NumPy array to save in hdf5 format.
- Parameters:
LoL (list of lists) – The input list of lists to be converted to a NumPy array.
fill (float, optional) – The value to use for filling missing values. Defaults to np.nan.
- Returns
numpy.ndarray: A NumPy array with the contents of the input list of lists, with missing values filled.
- sim_utils.load_data(sim_id: str, data_id: str, cell_n: int = 0, sim_num: str = 0) ndarray¶
Load Non-spiking data for a given simulation ID.
- Parameters:
sim_id (str) – Simulation ID to load data from.
data_id (str) – Data ID. e.g. ‘stell_v’
cell_n (int, optional) – Cell number to load data for, by default 0.
sim_num (str, optional) – Simulation number to load data from, by default 0.
- Returns:
The data array loaded from the HDF5 file
- Return type:
np.ndarray
- sim_utils.load_full_data(sim_id, data_id, sim_num=0)¶
Load Non-spiking data of all cells for a given simulation ID and number.
- Parameters:
sim_id (str) – Simulation ID to load data from.
data_id (str) – Data ID. e.g. ‘stell_v’
cell_n (int, optional) – Cell number to load data for, by default 0.
sim_num (str, optional) – Simulation number to load data from, by default 0.
- Returns:
The data array loaded from the HDF5 file.
- Return type:
np.ndarray
- sim_utils.load_sim_params(sim_id: str, file_path: str = None) dict¶
Load simulation parameters.
- Parameters:
sim_id (str) – Simulation ID to load the parameters for.
file_path (str, optional – Direct path to the JSON file containing the simulation parameters. If not provided, the parameters are located for the given sim_id.
- Returns:
The simulation parameters loaded from the JSON file.
- Return type:
dict
- sim_utils.load_spikes(sim_id: str, sim_num: int = 0) tuple¶
Load spike data for a given simulation ID.
This function looks for data in the following directories adn returns both stellate and interneuron spikes.
“data/” (local)
“/data/{user}/data/” (global)
- Parameters:
sim_id (str) – Simulation ID to load spikes from.
sim_num (int, optional) – Simulation number to load spikes from, by default 0 (single sim).
- Returns:
- tuple
A tuple containing two lists of lists: - stell_spikes_l: List of spike times for stellate cells. - intrnrn_spikes_l: List of spike times for interneurons.
- sim_utils.locate_data_dir(sim_id: str) str¶
Find the location of the data directory for a given simulation ID.
The function checks for the existence of the data directory in several predefined locations.
“data/” (local)
“/data/{user}/data/” (global)
The {user} placeholder is replaced with the current user’s username obtained from the environment variables “USERNAME” or “USER”.
- Parameters:
sim_id (str) – The sim ID to locate the data directory for.
- Returns:
The location of the data directory.
- Return type:
str
- sim_utils.log_from_rank_0(logger: Logger, rank: int, msg: str, level: int = 20)¶
Logs a message if the rank is 0.
- Parameters:
logger (logging.Logger) – The logger object to use for logging.
rank (int) – The rank of the process.
msg (str) – The message to be logged.
- sim_utils.network_intialize(params)¶
Initialize network and setup instrumentation
- Parameters:
params (dict or Param) – Parameter dictionary
- Returns:
Network object that includes cells and instrumentations
- Return type:
network
- sim_utils.process_data_root(data_root: str) str¶
Processes the given data root path to ensure it ends with a slash.
- Parameters:
data_root (str) – The root path to the data directory.
- Returns:
The data root path ending with a slash.
- Return type:
str
- sim_utils.remove_nodes_from_params(params)¶
Remove “-node*” suffix from sim_id in multi params dict.
- Parameters:
params (dict) – Multi params dict.
- Returns:
modified params dict without “-node*” suffix from sim_id.
- Return type:
dict
- sim_utils.sim_run_arg_parser() ArgumentParser¶
Set up the argument parser for the simulation run.
Argument parser is initialzed and processed here to avoid cluttering the main run file.
- Returns:
The argument parser with the arguments added.
- Return type:
argparse.ArgumentParser
- sim_utils.sim_setup_arg_parser() ArgumentParser¶
Set up the argument parser for the simulation.
Argument parser is initialzed and processed here to avoid cluttering the main setup file.
- Returns:
The argument parser with the arguments added.
- Return type:
argparse.ArgumentParser