Esempio n. 1
0
def test_node_set_file():
    tmp_ns_file = tempfile.NamedTemporaryFile(suffix='.json')
    json.dump(
        {'bio_cells': {
            'model': 'biophysical',
            'locations': ['L4', 'L2/3']
        }}, open(tmp_ns_file.name, 'w'))

    cfg = SonataConfig.from_dict({
        'target_simulator': 'NEURON',
        'node_sets_file': tmp_ns_file.name
    })

    assert ('node_sets' in cfg)
    assert ('node_sets_file' in cfg)
    assert (set(cfg['node_sets'].keys()) == {'bio_cells'})
    assert (set(
        cfg['node_sets']['bio_cells'].keys()) == {'model', 'locations'})
    assert (cfg['node_sets']['bio_cells']['model'] == 'biophysical')
    assert (cfg['node_sets']['bio_cells']['locations'] == ['L4', 'L2/3'])

    cfg = SonataConfig.from_dict({
        'target_simulator': 'NEURON',
        'node_sets_file': tmp_ns_file.name,
        'node_sets': {
            'point_cells': {
                'key': 'val'
            }
        }
    })
    assert ('node_sets' in cfg)
    assert ('node_sets_file' in cfg)
    assert (set(cfg['node_sets']['point_cells'].keys()) == {'key'})
Esempio n. 2
0
def test_build_manifest_fail1():
    """Test exception occurs when variable is missing"""
    config_file = {
        'manifest': {
            '$BASE': '/base',
            '$TMP': '$VAR/Smat',
        }
    }
    with pytest.raises(Exception):
        SonataConfig.from_dict(config_file)
Esempio n. 3
0
def test_build_manifest_fail2():
    """Test recursive definition"""
    config_file = {
        'manifest': {
            '$BASE': '$TMP/share',
            '$TMP': '$BASE/share',
        }
    }
    with pytest.raises(Exception):
        SonataConfig.from_dict(config_file)
Esempio n. 4
0
def load_config(config):
    if isinstance(config, string_types):
        return ConfigDict.from_json(config)
    elif isinstance(config, dict):
        return ConfigDict.from_dict(config)
    else:
        raise Exception('Could not convert {} (type "{}") to json.'.format(config, type(config)))
Esempio n. 5
0
def test_inputs():
    _ = pytest.importorskip('jsonschema')
    # valid inputs section
    cfg = SonataConfig.from_dict({
        "inputs": {
            "input1": {
                'input_type': 'str1',
                'input_file': 'str2',
                'trial': 'str2',
                'module': 'str',
                'electrode_file': 'str',
                'node_set': 'str',
                'random_seed': 100
            }
        }
    })
    assert (cfg.validate())

    # Base inputs
    cfg = SonataConfig.from_dict(
        {"inputs": [{
            'input_type': 'spikes',
            'input_file': 'myspikes.csv'
        }]})
    with pytest.raises(Exception):
        cfg.validate()

    # missing input_type
    cfg = SonataConfig.from_dict({
        "inputs": {
            "input1": {
                'input_file': 'str2',
                'trial': 'str2',
                'module': 'myspikes.csv',
                'electrode_file': 'myspikes.csv',
                'node_set': 'myspikes.csv',
                'random_seed': 100
            }
        }
    })
    with pytest.raises(Exception):
        cfg.validate()
Esempio n. 6
0
def test_build_manifest2():
    config_file = {
        'manifest': {
            '$DIR_DATA': 'data',
            '$DIR_MAT': 'mat',
            '$APPS': '/${DIR_DATA}/$DIR_MAT/apps'
        }
    }

    manifest = SonataConfig.from_dict(config_file)['manifest']
    assert (manifest['APPS'] == '/data/mat/apps')
Esempio n. 7
0
def test_missing_nodes_file():
    _ = pytest.importorskip('jsonschema')
    cfg = SonataConfig.from_dict(
        {"networks": {
            "nodes": [
                {
                    "node_types_file": "node_types.csv"
                },
            ]
        }})

    with pytest.raises(Exception):
        cfg.validate()
Esempio n. 8
0
def test_build_manifest1():
    """Test simple manifest"""
    config_file = {
        'manifest': {
            '$BASE_DIR': '/base',
            '$TMP_DIR': '$BASE_DIR/tmp',
            '$SHARE_DIR': '${TMP_DIR}_1/share'
        }
    }

    manifest = SonataConfig.from_dict(config_file)['manifest']
    assert (manifest['BASE_DIR'] == '/base')
    assert (manifest['TMP_DIR'] == '/base/tmp')
    assert (manifest['SHARE_DIR'] == '/base/tmp_1/share')
Esempio n. 9
0
def test_speical_vars():
    cfg = SonataConfig.from_dict({
        'manifest': {
            '$VAR_DATETIME': '${datetime}'
        },
        'datetime': '${VAR_DATETIME}',
        'time': '${time}',
        'date': '${date}',
        'combined': 'myfile_${date}.csv'
    })

    assert (isinstance(datetime.strptime(cfg['datetime'], '%Y-%m-%d_%H-%M-%S'),
                       datetime))
    assert (isinstance(datetime.strptime(cfg['time'], '%H-%M-%S'), datetime))
    assert (isinstance(datetime.strptime(cfg['date'], '%Y-%m-%d'), datetime))
Esempio n. 10
0
def test_output_dir():
    cfg = SonataConfig.from_dict({
        'manifest': {
            '$OUTPUT_DIR': 'my/output'
        },
        'output': {
            'output_dir': '$OUTPUT_DIR',
            'log_file': 'log.txt',
            'spikes_file': 'tmp/spikes.h5',
            'spikes_file_csv':
            '/abs/path/to/spikes.csv',  # do not prepend to absolute paths
            'spikes_file_nwb': '$OUTPUT_DIR/spikes.nwb'  # do not prepend
        }
    })

    assert (cfg['output']['log_file'] == 'my/output/log.txt')
    assert (cfg['output']['spikes_file'] == 'my/output/tmp/spikes.h5')
    assert (cfg['output']['spikes_file_csv'] == '/abs/path/to/spikes.csv')
    assert (cfg['output']['spikes_file_nwb'] == 'my/output/spikes.nwb')
Esempio n. 11
0
def test_dict():
    sonata_dict = {
        'manifest': {
            '$BASE': '${configdir}',
            '$TMP_ATTR': 'mytest'
        },
        'myvar': '$TMP_ATTR/myvar'
    }

    config_dict = SonataConfig.from_dict(sonata_dict)
    assert (isinstance(config_dict, SonataConfig))
    assert (isinstance(config_dict, dict))
    assert (config_dict['myvar'] == 'mytest/myvar')

    config_dict = SonataConfig.load(sonata_dict)
    assert (isinstance(config_dict, SonataConfig))
    assert (isinstance(config_dict, dict))
    assert (config_dict['myvar'] == 'mytest/myvar')

    config_dict = from_dict(sonata_dict)
    assert (isinstance(config_dict, SonataConfig))
    assert (isinstance(config_dict, dict))
    assert (config_dict['myvar'] == 'mytest/myvar')
Esempio n. 12
0
def test_valid_config():
    _ = pytest.importorskip('jsonschema')
    cfg = SonataConfig.from_dict({
        "manifest": {
            "$BASE": "${configdir}"
        },
        "target_simulator": "NEURON",
        "target_simulator_version": ">=7.4",
        'run': {
            'tstop': 3000.0,
            'dt': 0.001
        },
        "networks": {
            "nodes": [{
                "nodes_file": "nodes.h5",
                "node_types_file": "node_types.csv"
            }, {
                "nodes_file": "nodes2.h5",
                "node_types_file": "node_types2.csv"
            }]
        },
        "output": {
            'output_dir': 'output',
            'spikes_file': "null"
        },
        "inputs": {
            "input1": {
                'input_type': 'spikes',
                'input_file': 'myspikes.csv'
            },
            "input2": {
                'input_type': 'voltage_clamp'
            }
        }
    })

    assert (cfg.validate())
Esempio n. 13
0
def test_user_vars():
    cfg = SonataConfig.from_dict(
        {
            'my_int': '${my_int}',
            'my_bool': '${my_bool}',
            'my_float': '${my_float}',
            'my_list': '${my_list}',
            'my_str': '${my_str}',
            'combined_strs': '${my_str}bar',
            'combined_int': 'file.${my_int}.txt'
        },
        my_int=100,
        my_bool=True,
        my_float=0.001,
        my_list=['a', 'b'],
        my_str='foo')

    assert (cfg['my_int'] == 100)
    assert (cfg['my_bool'] is True)
    assert (cfg['my_float'] == 0.001)
    assert (cfg['my_list'] == ['a', 'b'])
    assert (cfg['my_str'] == 'foo')
    assert (cfg['combined_strs'] == 'foobar')
    assert (cfg['combined_int'] == 'file.100.txt')
Esempio n. 14
0
def test_from_config():
    config = {
        'reports': {
            "membrane_potential": {
                "cells": 'some',
                "variable_name": "v",
                "module": "membrane_report",
                "sections": "soma",
                "enabled": True
            },
            "syn_report": {
                "cells": [0, 1],
                "variable_name": "tau1",
                "module": "netcon_report",
                "sections": "soma",
                "syn_type": "Exp2Syn"
            },
            "ecp": {
                "cells": 'all',
                "variable_name": "v",
                "module": "extracellular",
                "electrode_positions": "linear_electrode.csv",
                "file_name": "ecp.h5",
                "electrode_channels": "all",
                "contributions_dir": "ecp_contributions"
            },
            "spikes": {
                'cells': 'all',
                'module': 'spikes_report',
                'spikes_file': 'my_spikes.h5',
                'cache_to_disk': False
            }
        }
    }

    config_dict = SonataConfig.from_dict(config)
    reports = from_config(config_dict)

    assert (len(reports) == 4)
    assert ({r.report_name
             for r in reports
             } == {'spikes', 'ecp', 'membrane_potential', 'syn_report'})
    for report in reports:
        if report.report_name == 'spikes':
            assert (isinstance(report, SpikesReport))
            assert (report.params == {
                'cells': 'all',
                'spikes_file': 'my_spikes.h5',
                'cache_to_disk': False
            })

        elif report.report_name == 'ecp':
            assert (isinstance(report, ECPReport))
            assert (report.params == {
                'cells': 'all',
                'variable_name': 'v',
                'electrode_positions': 'linear_electrode.csv',
                'file_name': 'ecp.h5',
                'electrode_channels': 'all',
                'contributions_dir': 'ecp_contributions',
                'tmp_dir': '.'
            })

        elif report.report_name == 'membrane_potential':
            assert (isinstance(report, MembraneReport))
            assert (report.params == {
                'cells': 'some',
                'variable_name': ['v'],
                'sections': 'soma',
                'tmp_dir': '.',
                'file_name': 'membrane_potential.h5',
                'transform': {},
                'buffer_data': True
            })

        elif report.report_name == 'syn_report':
            assert (isinstance(report, MembraneReport))
            assert (report.params == {
                'cells': [0, 1],
                'variable_name': ['tau1'],
                'sections': 'soma',
                'syn_type': 'Exp2Syn',
                'tmp_dir': '.',
                'file_name': 'syn_report.h5',
                'transform': {},
                'buffer_data': True
            })
Esempio n. 15
0
def test_negative_tstop():
    _ = pytest.importorskip('jsonschema')
    cfg = SonataConfig.from_dict({'run': {'tstop': -1.0}})

    with pytest.raises(Exception):
        cfg.validate()