예제 #1
0
파일: test_edges.py 프로젝트: markovg/snap
    def create_edge_population(filepath, pop_name):
        config = {
            'edges_file': filepath,
            'edge_types_file': None,
        }
        circuit = Mock()
        circuit.config = {}
        create_node_population(str(TEST_DATA_DIR / 'nodes.h5'), "default", circuit=circuit,
                               node_sets=NodeSets(str(TEST_DATA_DIR / 'node_sets.json')))
        storage = test_module.EdgeStorage(config, circuit)
        pop = storage.population(pop_name)

        # check if the source and target populations are in the circuit nodes
        assert pop.source.name in pop._edge_storage.circuit.nodes
        assert pop.target.name in pop._edge_storage.circuit.nodes
        return pop
예제 #2
0
def test_get_filepath_point():
    with copy_config() as config_copy_path:
        with edit_config(config_copy_path) as config:
            config["components"][
                "point_neuron_models_dir"] = "$COMPONENT_DIR/point_neuron_models"

        circuit = Circuit(config_copy_path)
        nodes = create_node_population(str(TEST_DATA_DIR / "nodes_points.h5"),
                                       "default", circuit)
        test_obj = test_module.NeuronModelsHelper(circuit.config["components"],
                                                  nodes)

        node_id = 0
        assert nodes.get(node_id,
                         properties=Node.MODEL_TEMPLATE) == "nml:empty_bio"
        actual = test_obj.get_filepath(node_id)
        expected = Path(
            circuit.config["components"]["point_neuron_models_dir"],
            "empty_bio.nml")
        assert actual == expected

        node_id = 1
        assert nodes.get(
            node_id,
            properties=Node.MODEL_TEMPLATE) == "nml:/abs/path/empty_bio"
        actual = test_obj.get_filepath(node_id)
        expected = Path("/abs/path/empty_bio.nml")
        assert actual == expected
예제 #3
0
def test_get_filepath_biophysical():
    nodes = create_node_population(str(TEST_DATA_DIR / 'nodes.h5'), "default")
    config = Config(TEST_DATA_DIR / 'circuit_config.json').resolve()
    test_obj = test_module.NeuronModelsHelper(config['components'], nodes)

    node_id = 0
    assert nodes.get(node_id,
                     properties=Node.MODEL_TEMPLATE) == "hoc:small_bio-A"
    actual = test_obj.get_filepath(node_id)
    expected = Path(config['components']['biophysical_neuron_models_dir'],
                    'small_bio-A.hoc')
    assert actual == expected

    node_id = CircuitNodeId('default', 0)
    assert nodes.get(node_id,
                     properties=Node.MODEL_TEMPLATE) == "hoc:small_bio-A"
    actual = test_obj.get_filepath(node_id)
    assert actual == expected

    node_id = CircuitNodeId('default', 2)
    assert nodes.get(node_id,
                     properties=Node.MODEL_TEMPLATE) == "hoc:small_bio-C"
    actual = test_obj.get_filepath(node_id)
    expected = Path(config['components']['biophysical_neuron_models_dir'],
                    'small_bio-C.hoc')
    assert actual == expected
예제 #4
0
    def test_node_ids_by_filter_complex_query(self):
        test_obj = create_node_population(str(TEST_DATA_DIR / 'nodes.h5'),
                                          "default")
        data = pd.DataFrame({
            Cell.MTYPE: ['L23_MC', 'L4_BP', 'L6_BP', 'L6_BPC'],
        })
        # replace the data using the __dict__ directly
        test_obj.__dict__["_data"] = data

        # only full match is accepted
        npt.assert_equal([1, 2],
                         test_obj.ids({
                             Cell.MTYPE: {
                                 '$regex': '.*BP'
                             },
                         }))
        # ...not 'startswith'
        npt.assert_equal([], test_obj.ids({Cell.MTYPE: {'$regex': 'L6'}}))
        # ...or 'endswith'
        npt.assert_equal([], test_obj.ids({Cell.MTYPE: {'$regex': 'BP'}}))
        # '$regex' is the only query modifier supported for the moment
        with pytest.raises(BluepySnapError) as e:
            test_obj.ids({Cell.MTYPE: {
                'err': '.*BP'
            }},
                         raise_missing_property=False)
        assert 'Unknown query modifier' in e.value.args[0]
        with pytest.raises(BluepySnapError) as e:
            test_obj.ids({Cell.MTYPE: {'err': '.*BP'}})
        assert 'Unknown node properties' in e.value.args[0]
예제 #5
0
    def test_get_morphology_standard_rotation(self):
        nodes = create_node_population(str(TEST_DATA_DIR / 'nodes.h5'),
                                       "default")
        test_obj = test_module.MorphHelper(str(self.morph_path), nodes)

        node_id = 0
        actual = test_obj.get(node_id, transform=True).points

        # check if the input node positions / orientation values are still the same
        pdt.assert_series_equal(
            self.nodes.positions(node_id),
            pd.Series([101., 102., 103.],
                      index=[Node.X, Node.Y, Node.Z],
                      name=0))
        npt.assert_almost_equal(nodes.orientations(node_id), [
            [0.738219, 0., 0.674560],
            [0., 1., 0.],
            [-0.674560, 0., 0.738219],
        ],
                                decimal=6)

        assert len(actual) == 32
        expected = [[100.7637696, 103, 103.2158592, 0.725],
                    [100.7637696, 102.9, 103.2158592, 0.820]]
        npt.assert_almost_equal(expected, actual[:2])
예제 #6
0
    def test_get_morphology_standard_rotation(self):
        nodes = create_node_population(str(TEST_DATA_DIR / "nodes.h5"), "default")
        test_obj = test_module.MorphHelper(str(self.morph_path), nodes)

        node_id = 0
        actual = test_obj.get(node_id, transform=True).points

        # check if the input node positions / orientation values are still the same
        pdt.assert_series_equal(
            self.nodes.positions(node_id),
            pd.Series([101.0, 102.0, 103.0], index=[Node.X, Node.Y, Node.Z], name=0),
        )
        npt.assert_almost_equal(
            nodes.orientations(node_id),
            [
                [0.738219, 0.0, 0.674560],
                [0.0, 1.0, 0.0],
                [-0.674560, 0.0, 0.738219],
            ],
            decimal=6,
        )

        assert len(actual) == 13
        expected = [[101.0, 107.0, 103.0], [102.47644, 111.0, 101.65088]]
        npt.assert_almost_equal(actual[:2], expected, decimal=6)
예제 #7
0
def test_get_filepath_biophysical():
    nodes = create_node_population(str(TEST_DATA_DIR / "nodes.h5"), "default")
    config = Config(TEST_DATA_DIR / "circuit_config.json").resolve()
    test_obj = test_module.NeuronModelsHelper(config["components"], nodes)

    node_id = 0
    assert nodes.get(node_id,
                     properties=Node.MODEL_TEMPLATE) == "hoc:small_bio-A"
    actual = test_obj.get_filepath(node_id)
    expected = Path(config["components"]["biophysical_neuron_models_dir"],
                    "small_bio-A.hoc")
    assert actual == expected

    actual = test_obj.get_filepath(np.int64(node_id))
    assert actual == expected
    actual = test_obj.get_filepath(np.uint64(node_id))
    assert actual == expected
    actual = test_obj.get_filepath(np.int32(node_id))
    assert actual == expected
    actual = test_obj.get_filepath(np.uint32(node_id))
    assert actual == expected

    node_id = CircuitNodeId("default", 0)
    assert nodes.get(node_id,
                     properties=Node.MODEL_TEMPLATE) == "hoc:small_bio-A"
    actual = test_obj.get_filepath(node_id)
    assert actual == expected

    node_id = CircuitNodeId("default", 2)
    assert nodes.get(node_id,
                     properties=Node.MODEL_TEMPLATE) == "hoc:small_bio-C"
    actual = test_obj.get_filepath(node_id)
    expected = Path(config["components"]["biophysical_neuron_models_dir"],
                    "small_bio-C.hoc")
    assert actual == expected
예제 #8
0
def test_get_invalid_node_id():
    nodes = create_node_population(str(TEST_DATA_DIR / "nodes.h5"), "default")
    config = Config(TEST_DATA_DIR / "circuit_config.json").resolve()
    test_obj = test_module.NeuronModelsHelper(config["components"], nodes)

    with pytest.raises(BluepySnapError) as e:
        test_obj.get_filepath("1")
    assert "node_id must be a int or a CircuitNodeId" in e.value.args[0]
예제 #9
0
 def test_population_type(self):
     test_obj = create_node_population(
         str(TEST_DATA_DIR / "nodes.h5"),
         "default",
         node_sets=NodeSets(str(TEST_DATA_DIR / "node_sets.json")),
         pop_type="fake_type",
     )
     assert test_obj.type == "fake_type"
예제 #10
0
def test_no_biophysical_dir():
    nodes = create_node_population(str(TEST_DATA_DIR / "nodes.h5"), "default")
    config = Config(TEST_DATA_DIR / "circuit_config.json").resolve()
    del config["components"]["biophysical_neuron_models_dir"]
    test_obj = test_module.NeuronModelsHelper(config["components"], nodes)

    with pytest.raises(BluepySnapError) as e:
        test_obj.get_filepath(0)
    assert "Missing 'biophysical_neuron_models_dir'" in e.value.args[0]
예제 #11
0
def test_no_point_dir():
    nodes = create_node_population(str(TEST_DATA_DIR / 'nodes_points.h5'),
                                   "default")
    config = Config(TEST_DATA_DIR / 'circuit_config.json').resolve()
    test_obj = test_module.NeuronModelsHelper(config['components'], nodes)

    with pytest.raises(BluepySnapError) as e:
        test_obj.get_filepath(0)
    assert "Missing 'point_neuron_models_dir'" in e.value.args[0]
예제 #12
0
 def test_property_values(self):
     assert (self.test_obj.property_values(Cell.LAYER) == {2, 6})
     assert (self.test_obj.property_values(
         Cell.MORPHOLOGY) == {'morph-A', 'morph-B', 'morph-C'})
     test_obj_library = create_node_population(
         str(TEST_DATA_DIR / 'nodes_with_library_small.h5'), "default")
     assert test_obj_library.property_values("categorical") == {
         "A", "B", "C"
     }
     assert test_obj_library.property_values("categorical",
                                             is_present=True) == {"A", "B"}
예제 #13
0
파일: test_nodes.py 프로젝트: MFSY/snap
    def test_as_edge_source_target_mock(self):
        def _mock_edge(name, source, target):
            edges = Mock()
            edges.source.name = source
            edges.target.name = target
            edges.name = name
            return edges

        circuit = Mock()
        circuit.edges = {
            "edge1": _mock_edge('edge1', "default", "nodeother"),
            "edge2": _mock_edge('edge2', "nodeother", "default"),
            "edge3": _mock_edge('edge3', "default", "nodeother")
        }
        create_node_population(str(TEST_DATA_DIR / 'nodes.h5'),
                               "default",
                               circuit=circuit)

        assert circuit.nodes['default'].source_in_edges() == {"edge1", "edge3"}
        assert circuit.nodes['default'].target_in_edges() == {"edge2"}
예제 #14
0
 def test_get_with_library_large_number_of_values(self):
     test_obj = create_node_population(
         str(TEST_DATA_DIR / 'nodes_with_library_large.h5'), "default")
     assert test_obj.property_names == {
         "categorical", "string", "int", "float"
     }
     res = test_obj.get(
         properties=["categorical", "string", "int", "float"])
     assert not is_categorical_dtype(res["categorical"])
     assert res["categorical"].tolist() == ['A', 'A', 'B', 'A']
     assert res["string"].tolist() == ["AA", "BB", "CC", "DD"]
     assert res["int"].tolist() == [0, 0, 1, 0]
     npt.assert_allclose(res["float"].tolist(), [0., 0., 1.1, 0.])
예제 #15
0
def test_invalid_model_type():
    """test that model type, other than 'biophysical' or 'point_neuron', throws an error"""
    with copy_circuit() as (circuit_copy_path, config_copy_path):
        config = Config(config_copy_path).resolve()
        nodes_file = config["networks"]["nodes"][0]["nodes_file"]
        with h5py.File(nodes_file, "r+") as h5f:
            grp_name = "nodes/default/0/model_type"
            data = h5f[grp_name][:]
            del h5f[grp_name]
            h5f.create_dataset(grp_name,
                               data=["virtual"] * data.shape[0],
                               dtype=h5py.string_dtype())
        nodes = create_node_population(nodes_file, "default")
        with pytest.raises(BluepySnapError) as e:
            test_module.NeuronModelsHelper({}, nodes)
        assert "biophysical or point node population" in e.value.args[0]
예제 #16
0
 def test_get_with_library_small_number_of_values(self):
     test_obj = create_node_population(
         str(TEST_DATA_DIR / "nodes_with_library_small.h5"), "default")
     assert test_obj.property_names == {
         "categorical", "string", "int", "float"
     }
     res = test_obj.get(
         properties=["categorical", "string", "int", "float"])
     assert is_categorical_dtype(res["categorical"])
     assert res["categorical"].tolist() == [
         "A", "A", "B", "A", "A", "A", "A"
     ]
     assert res["categorical"].cat.categories.tolist() == ["A", "B", "C"]
     assert res["categorical"].cat.codes.tolist() == [0, 0, 1, 0, 0, 0, 0]
     assert res["string"].tolist() == [
         "AA", "BB", "CC", "DD", "EE", "FF", "GG"
     ]
     assert res["int"].tolist() == [0, 0, 1, 0, 0, 0, 0]
     npt.assert_allclose(res["float"].tolist(),
                         [0.0, 0.0, 1.1, 0.0, 0.0, 0.0, 0.0])
예제 #17
0
 def setup(self):
     self.nodes = create_node_population(
         str(TEST_DATA_DIR / 'nodes_quaternions.h5'), "default")
     self.morph_path = TEST_DATA_DIR / 'morphologies'
     self.test_obj = test_module.MorphHelper(str(self.morph_path),
                                             self.nodes)
예제 #18
0
 def setup(self):
     self.test_obj = create_node_population(
         str(TEST_DATA_DIR / 'nodes.h5'),
         "default",
         node_sets=NodeSets(str(TEST_DATA_DIR / 'node_sets.json')))
예제 #19
0
    def test_orientations(self):
        _call = self.test_obj.orientations
        expected = [
            [0.738219, 0., 0.674560],
            [0., 1., 0.],
            [-0.674560, 0., 0.738219],
        ]
        npt.assert_almost_equal(_call(0), expected, decimal=6)
        npt.assert_almost_equal(_call(CircuitNodeId("default", 0)),
                                expected,
                                decimal=6)
        pdt.assert_series_equal(
            _call([2, 0, 1]),
            pd.Series([
                np.array([
                    [0.462986, 0., 0.886365],
                    [0., 1., 0.],
                    [-0.886365, 0., 0.462986],
                ]),
                np.array([
                    [0.738219, 0., 0.674560],
                    [0., 1., 0.],
                    [-0.674560, 0., 0.738219],
                ]),
                np.array([[-0.86768965, -0.44169042, 0.22808825],
                          [0.48942842, -0.8393853, 0.23641518],
                          [0.0870316, 0.31676788, 0.94450178]])
            ],
                      index=[2, 0, 1],
                      name='orientation'))

        # NodeCircuitIds
        pdt.assert_series_equal(
            _call(
                CircuitNodeIds.from_arrays(["default", "default", "default"],
                                           [2, 0, 1],
                                           sort_index=False)), _call([2, 0,
                                                                      1]))

        # NodePopulation without rotation_angle[x|z]
        _call_no_xz = create_node_population(
            str(TEST_DATA_DIR / 'nodes_no_xz_rotation.h5'),
            "default").orientations
        # 0 and 2 node_ids have x|z rotation angles equal to zero
        npt.assert_almost_equal(_call_no_xz(0), _call(0))
        npt.assert_almost_equal(_call_no_xz(2), _call(2))
        npt.assert_almost_equal(_call_no_xz(1),
                                [[0.97364046, -0., 0.22808825], [0., 1., -0.],
                                 [-0.22808825, 0., 0.97364046]],
                                decimal=6)

        # NodePopulation without rotation_angle
        _call_no_rot = create_node_population(
            str(TEST_DATA_DIR / 'nodes_no_rotation.h5'),
            "default").orientations

        pdt.assert_series_equal(
            _call_no_rot([2, 0, 1]),
            pd.Series([np.eye(3), np.eye(3), np.eye(3)],
                      index=[2, 0, 1],
                      name='orientation'))

        # NodePopulation with quaternions
        _call_quat = create_node_population(
            str(TEST_DATA_DIR / 'nodes_quaternions.h5'),
            "default").orientations

        npt.assert_almost_equal(_call_quat(0), [
            [1, 0., 0.],
            [0., 0, -1.],
            [0., 1., 0],
        ],
                                decimal=6)

        series = _call_quat([2, 0, 1])
        for i in range(len(series)):
            series.iloc[i] = np.around(series.iloc[i],
                                       decimals=1).astype(np.float64)

        pdt.assert_series_equal(
            series,
            pd.Series([
                np.array([
                    [0., -1., 0.],
                    [1., 0., 0.],
                    [0., 0., 1.],
                ]),
                np.array([
                    [1., 0., 0.],
                    [0., 0., -1.],
                    [0., 1., 0.],
                ]),
                np.array([
                    [0., 0., 1.],
                    [0., 1., 0.],
                    [-1., 0., 0.],
                ])
            ],
                      index=[2, 0, 1],
                      name='orientation'))

        _call_missing_quat = create_node_population(
            str(TEST_DATA_DIR / 'nodes_quaternions_w_missing.h5'),
            "default").orientations

        with pytest.raises(BluepySnapError):
            _call_missing_quat(0)