bmtk.utils.sonata.config package

Submodules

bmtk.utils.sonata.config.sonata_config module

class bmtk.utils.sonata.config.sonata_config.ConfigParser(**opts)[source]

Bases: object

Helper class for parsing all the configs. Use the parse() method to return a normal dictionary to save into SonataConfig class

parse(cfg_obj)[source]

Builds and validates a configuration json dictionary object. Best to directly use from_json when possible.

Parameters:

cfg_obj – Dictionary/json file containing a sonata configuration

Returns:

A dictionary, verified against json validator and with manifest variables resolved.

property usr_vars
class bmtk.utils.sonata.config.sonata_config.SonataConfig(*args, **kwargs)[source]

Bases: dict

A special type of dict designed specially to handle SONATA configuration files.

It handles things like manifest variables and combining network and circuit configurations into a single dictionary, as well as some (non-SONATA) special features. This class should be able to: * Combine circuit_config and simualation config files into a single dictionary automatically * resovlve manifest variables * Allow for custom and pre-defined variables for use in config * Make sure resolves the output_dir location

General Usage:

You can read in a root sonata config json file (or just the circuit or simulation files individual) by passing the file path or a file-pointer, which will return a dictionary like object you can use to build and run your simulation:

sim_params = SonataConfig.from_json('/path/to/sonata_config.json')
... = sim_params['run']['tstop']

You can also pass in a dictionary and use the from_dict() factory method:

sim_params = SonataConfig.from_dict({...})

JSON Variables:

You can add variables to your SONATA config file which can make reusing json files easiers. The standard way of doing this by using the ‘manifest’ section to set the variable name and value to be used in other parts of the json. If the json file looks like:

{
    'manifest': {'$CIRCUIT_HOME': '/home/john/cortex_simulation/network'},
    'network': {
        'nodes': '${CIRCUIT_HOME}/nodes.h5,
        'node_types': '${$CIRCUIT_HOME}/nodes_types.csv'
    }
}

SonataConfig will resolve the $CIRCUIT_HOME variable when accessed:

sim_params['network']['nodes'] == '/home/john/cortex_simulation/network/nodes.h5'
sim_params['network']['node_types'] == '/home/john/cortex_simulation/network/nodes_types.h5'

Users and Predefined variables:

You can also create your own predefined variables:

circuit_config = {
    'run': {
        "dt": "${time_step}",
        "overwrite_output_dir": "${overwrite}"
    }
    'target_simulator': "${simulator}"
}
cfg = SonataConfig.from_dict(circuit_config, time_step=0.001, overwrite=True, simlator='CoreNeuron')
cfg['run']['dt'] == 0.001
cfg['run']['overwrite_output_dir'] == True
cfg['target_simulator'] == 'CoreNeuron'

There are also a number of built in variables which you can add to your json

  • ${configdir} - location of the current json file

  • ${workingdir} - directory where the code/simulator is being ran

  • ${datetime} - date-time string in Y-M-d_H-M-S format

Validating the json file:

You can check if the schema is valid, making suring certain section variables that are required actually exists in the config and is of the right type:

cfg = SonataConfig.from_json('config.json')
try:
    is_valid = cfg.valid()
catch Exception e:
    is_valid = False

If a section (eg. ‘run’, ‘components’, ‘output’, etc) is missing then it will be skipped, meaning if valid() returns true it may still be missing parts of the config to run a complete simulation.

You can also add your own schema validator, just as long as it’s a class instance that has a validate(cfg) method. The easiest way to do this is by using jsonschema:

from jsonschema import Draft4Validator
validator = Drafter4Validator('/path/to/schema.json')

cfg = Sonata.from_json('config.json')
cfg.validator = validator
cfg.validate()
classmethod from_dict(config_dict, validator=None, **opts)[source]
classmethod from_json(config_file, validator=None, **opts)[source]
classmethod from_yaml(config_file, validator=None, **opts)[source]
classmethod load(config_file, validator=None, **opts)[source]
validate()[source]
property validator
bmtk.utils.sonata.config.sonata_config.copy_config(conf)[source]

Copy configuration file to different directory, with manifest variables resolved.

Parameters:

conf – configuration dictionary

bmtk.utils.sonata.config.sonata_config.from_dict(config_dict, validator=None, **opts)[source]
Parameters:
  • config_dict

  • validator

  • opts

Returns:

bmtk.utils.sonata.config.sonata_config.from_json(config_file, validator=None, **opts)[source]

Builds and validates a configuration json file.

Parameters:
  • config_file – File object or path to a json file.

  • validator – A SimConfigValidator object to validate json file. Won’t validate if set to None

  • opts

Returns:

A dictionary, verified against json validator and with manifest variables resolved.

bmtk.utils.sonata.config.sonata_config.from_yaml(config_file, validator=None, **opts)[source]
Parameters:
  • config_file

  • validator

  • opts

Returns:

Module contents