Example #1
0
def test_overwrite():
    tmp_dir = tempfile.mkdtemp()  # tempfile.TemporaryDirectory()
    output_dir = os.path.join(tmp_dir, 'output')
    sim_config = {
        'manifest': {
            '$HOME_DIR': tmp_dir
        },
        'output': {
            'output_dir': '${HOME_DIR}/output',
            'log_file': 'simulation.log',
            'log_to_console': False,
            'overwrite_output_dir': False
        }
    }

    cfg = SimulationConfig.from_dict(sim_config)
    cfg.build_env()
    assert(isinstance(cfg, SimulationConfig))
    assert(os.path.exists(output_dir))
    assert(os.path.exists(os.path.join(output_dir, 'simulation.log')))
    assert(os.path.exists(os.path.join(output_dir, 'sonata_config.json')))

    ## Changed behavior so that even if overwrite_output_dir is false and dir exists, it will still run
    # with pytest.raises(Exception):
    #     cfg = SimulationConfig.from_dict(sim_config)
    #     cfg.build_env()

    sim_config['output']['overwrite_output_dir'] = True
    cfg = SimulationConfig.from_dict(sim_config)
    cfg.build_env()
    assert(os.path.exists(output_dir))
    assert(os.path.exists(os.path.join(output_dir, 'simulation.log')))
    assert(os.path.exists(os.path.join(output_dir, 'sonata_config.json')))
def test_validate_components():
    cfg = SimulationConfig.from_dict({
        "components": {
            "synaptic_models_dir": tempfile.mkdtemp(),
            "morphologies_dir": tempfile.mkdtemp(),
            "biophysical_neuron_models_dir": tempfile.mkdtemp()
        }
    })
    assert (cfg.validate())

    cfg = SimulationConfig.from_dict({
        "components": {
            "point_neuron_models_dir": tempfile.mkdtemp(),
            "templates_dir": 'invaliddir',
        }
    })

    with pytest.raises(Exception):
        assert (cfg.validate())
def test_validate_network():
    # Check that validate() passes when all the files exists
    tmp_h5_file = tempfile.NamedTemporaryFile(suffix='.h5')
    tmp_csv_types_file = tempfile.NamedTemporaryFile(suffix='.csv')
    cfg = SimulationConfig.from_dict({
        "networks": {
            "nodes": [{
                "nodes_file": tmp_h5_file.name,
                "node_types_file": tmp_csv_types_file.name
            }],
            "edges": [{
                "edges_file": tmp_h5_file.name,
                "edge_types_file": tmp_csv_types_file.name
            }]
        }
    })
    assert (cfg.validate())

    cfg = SimulationConfig.from_dict({
        "networks": {
            "nodes": [{
                "nodes_file": tmp_h5_file.name,
                "node_types_file": 'nameoffilethatdoesntexists.csv'
            }]
        }
    })
    with pytest.raises(Exception):
        cfg.validate()

    # edges_file does not exist should raise error
    cfg = SimulationConfig.from_dict({
        "networks": {
            "edges": [{
                "edges_file": 'blah',
                "edge_types_file": tmp_csv_types_file.name
            }]
        }
    })
    with pytest.raises(Exception):
        cfg.validate()
def test_validate_inputs():
    spikes_file = tempfile.NamedTemporaryFile(suffix='.h5')
    cfg = SimulationConfig.from_dict({
        "inputs": {
            "valid_input": {
                "input_type": "spikes",
                "input_file": spikes_file.name
            }
        }
    })
    assert (cfg.validate())

    cfg = SimulationConfig.from_dict({
        "inputs": {
            "valid_input": {
                "input_type": "spikes",
                "input_file": 'notmyspikesfile.h5'
            }
        }
    })

    with pytest.raises(Exception):
        assert (cfg.validate())
Example #5
0
    def from_config(cls, conf, **properties):
        """Generates a graph structure from a json config file or dictionary.

        :param conf: name of json config file, or a dictionary with config parameters
        :param properties: optional properties.
        :return: A graph object of type cls
        """
        graph = cls(**properties)

        # The simulation run script should create a config-dict since it's likely to vary based on the simulator engine,
        # however in the case the user doesn't we will try a generic conversion from dict/json to ConfigDict
        if isinstance(conf, ConfigDict):
            config = conf
        else:
            try:
                config = ConfigDict.load(conf)
            except Exception as e:
                graph.io.log_exception('Could not convert {} (type "{}") to json.'.format(conf, type(conf)))

        if not config.with_networks:
            graph.io.log_exception('Could not find any network files. Unable to build network.')

        # TODO: These are simulator specific
        graph.spike_threshold = config.spike_threshold
        graph.dL = config.dL

        # load components
        for name, value in config.components.items():
            graph.add_component(name, value)
        graph._validate_components()

        # load nodes
        gid_map = config.gid_mappings
        for node_dict in config.nodes:
            nodes_net = sonata.File(data_files=node_dict['nodes_file'], data_type_files=node_dict['node_types_file'],
                                    gid_table=gid_map)
            graph.add_nodes(nodes_net)

        # load edges
        for edge_dict in config.edges:
            target_network = edge_dict['target'] if 'target' in edge_dict else None
            source_network = edge_dict['source'] if 'source' in edge_dict else None
            edge_net = sonata.File(data_files=edge_dict['edges_file'], data_type_files=edge_dict['edge_types_file'])
            graph.add_edges(edge_net, source_pop=target_network, target_pop=source_network)

        graph._node_sets['all'] = NodeSetAll(graph)
        for ns_name, ns_filter in conf.node_sets.items():
            graph._node_sets[ns_name] = NodeSet(ns_filter, graph)

        return graph
Example #6
0
def test_build_env():
    tmp_dir = tempfile.mkdtemp()  # tempfile.TemporaryDirectory()
    output_dir = os.path.join(tmp_dir, 'output')
    sim_config = {
        'manifest': {
            '$HOME_DIR': tmp_dir  # .name
        },
        'output': {
            'output_dir': '${HOME_DIR}/output',
            'log_file': 'simulation.log',
            'log_to_console': False
        }
    }

    cfg = SimulationConfig.from_dict(sim_config)
    cfg.build_env()
    assert(isinstance(cfg, SimulationConfig))
    assert(os.path.exists(output_dir))
    assert(os.path.exists(os.path.join(output_dir, 'simulation.log')))
    assert(os.path.exists(os.path.join(output_dir, 'sonata_config.json')))
Example #7
0
def from_json(config_file, validate=False):
    conf_dict = SimulationConfig.from_json(config_file)
    conf_dict.io = io
    return conf_dict