Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cyclic-import issues #238

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions bluepysnap/circuit_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

"""Circuit ids."""
import abc
from collections import namedtuple

import numpy as np
import pandas as pd
Expand All @@ -26,9 +25,6 @@
from bluepysnap._doctools import AbstractDocSubstitutionMeta
from bluepysnap.exceptions import BluepySnapError

CircuitNodeId = namedtuple("CircuitNodeId", ("population", "id"))
CircuitEdgeId = namedtuple("CircuitEdgeId", ("population", "id"))


class CircuitIds(abc.ABC):
"""High performances CircuitID container.
Expand Down Expand Up @@ -324,11 +320,11 @@ def __init__(self, index, sort_index=True):
def __iter__(self):
"""Iterator on the CircuitNodeIds."""
for index in self.index:
yield CircuitNodeId(*index)
yield utils.CircuitNodeId(*index)
joni-herttuainen marked this conversation as resolved.
Show resolved Hide resolved

def __getitem__(self, item):
"""Getter on the CircuitNodeIds."""
return CircuitNodeId(*self.index[item])
return utils.CircuitNodeId(*self.index[item])


class CircuitEdgeIds(
Expand All @@ -353,8 +349,8 @@ def __init__(self, index, sort_index=True):
def __iter__(self):
"""Iterator on the CircuitEdgeIds."""
for index in self.index:
yield CircuitEdgeId(*index)
yield utils.CircuitEdgeId(*index)

def __getitem__(self, item):
"""Getter on the CircuitEdgeIds."""
return CircuitEdgeId(*self.index[item])
return utils.CircuitEdgeId(*self.index[item])
35 changes: 1 addition & 34 deletions bluepysnap/circuit_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from bluepysnap import schemas
from bluepysnap.config import Parser
from bluepysnap.exceptions import BluepySnapValidationError as Error
joni-herttuainen marked this conversation as resolved.
Show resolved Hide resolved
from bluepysnap.morph import EXTENSIONS_MAPPING
from bluepysnap.sonata_constants import DEFAULT_EDGE_TYPE, DEFAULT_NODE_TYPE
from bluepysnap.utils import load_json
Expand All @@ -20,40 +21,6 @@
MAX_MISSING_FILES_DISPLAY = 10


class Error:
"""Error used for reporting of validation errors."""

FATAL = "FATAL"
WARNING = "WARNING"
INFO = "INFO"

def __init__(self, level, message=None):
"""Error.

Args:
level (str): error level
message (str|None): message
"""
self.level = level
self.message = message

def __str__(self):
"""Returns only message by default."""
return str(self.message)

__repr__ = __str__

def __eq__(self, other):
"""Two errors are equal if inherit from Error and their level, message are equal."""
if not isinstance(other, Error):
return False
return self.level == other.level and self.message == other.message

def __hash__(self):
"""Hash. Errors with the same level and message give the same hash."""
return hash(self.level) ^ hash(self.message)


def fatal(message):
"""Shortcut for a fatal error.

Expand Down
4 changes: 2 additions & 2 deletions bluepysnap/edges/edge_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
from more_itertools import first

from bluepysnap import query, utils
from bluepysnap.circuit_ids import CircuitEdgeId, CircuitEdgeIds
from bluepysnap.circuit_ids import CircuitEdgeIds
from bluepysnap.exceptions import BluepySnapError
from bluepysnap.sonata_constants import DYNAMICS_PREFIX, ConstContainer, Edge
from bluepysnap.utils import IDS_DTYPE
from bluepysnap.utils import IDS_DTYPE, CircuitEdgeId


def _is_empty(xs):
Expand Down
5 changes: 3 additions & 2 deletions bluepysnap/edges/edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
import numpy as np

from bluepysnap._doctools import AbstractDocSubstitutionMeta
from bluepysnap.circuit_ids import CircuitEdgeIds, CircuitNodeId, CircuitNodeIds
from bluepysnap.edges import EdgePopulation
from bluepysnap.circuit_ids import CircuitEdgeIds, CircuitNodeIds
from bluepysnap.edges.edge_population import EdgePopulation
from bluepysnap.exceptions import BluepySnapError
from bluepysnap.network import NetworkObject
from bluepysnap.utils import CircuitNodeId


class Edges(
Expand Down
34 changes: 34 additions & 0 deletions bluepysnap/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,37 @@ class BluepySnapDeprecationError(Exception):

class BluepySnapDeprecationWarning(DeprecationWarning):
"""SNAP deprecation warning."""


class BluepySnapValidationError:
"""Error used for reporting of validation errors."""

FATAL = "FATAL"
WARNING = "WARNING"
INFO = "INFO"

def __init__(self, level, message=None):
"""Error.

Args:
level (str): error level
message (str|None): message
"""
self.level = level
self.message = message

def __str__(self):
"""Returns only message by default."""
return str(self.message)

__repr__ = __str__

def __eq__(self, other):
"""Two errors are equal if inherit from Error and their level, message are equal."""
if not isinstance(other, BluepySnapValidationError):
return False
return self.level == other.level and self.message == other.message

def __hash__(self):
"""Hash. Errors with the same level and message give the same hash."""
return hash(self.level) ^ hash(self.message)
2 changes: 1 addition & 1 deletion bluepysnap/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""Simulation input access."""
import libsonata

from bluepysnap import BluepySnapError
from bluepysnap.exceptions import BluepySnapError


class SynapseReplay:
Expand Down
6 changes: 3 additions & 3 deletions bluepysnap/nodes/node_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
from more_itertools import first

from bluepysnap import query, utils
from bluepysnap.circuit_ids import CircuitNodeId, CircuitNodeIds
from bluepysnap.circuit_ids import CircuitNodeIds
from bluepysnap.exceptions import BluepySnapError
from bluepysnap.node_sets import NodeSet
from bluepysnap.sonata_constants import DYNAMICS_PREFIX, ConstContainer, Node
Expand Down Expand Up @@ -429,7 +429,7 @@ def ids(self, group=None, limit=None, sample=None, raise_missing_property=True):
else:
result = utils.ensure_list(group)
# test if first value is a CircuitNodeId all values are all CircuitNodeId
if isinstance(first(result, None), CircuitNodeId):
if isinstance(first(result, None), utils.CircuitNodeId):
try:
result = [cid.id for cid in result if cid.population == self.name]
except AttributeError as e:
Expand Down Expand Up @@ -526,7 +526,7 @@ def get(self, group=None, properties=None, raise_missing_property=True):
if group is not None:
if isinstance(group, (int, np.integer)):
self._check_id(group)
elif isinstance(group, CircuitNodeId):
elif isinstance(group, utils.CircuitNodeId):
group = self.ids(group, raise_missing_property=raise_missing_property)[0]
else:
group = self.ids(group, raise_missing_property=raise_missing_property)
Expand Down
2 changes: 1 addition & 1 deletion bluepysnap/nodes/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from bluepysnap.circuit_ids import CircuitNodeIds
from bluepysnap.exceptions import BluepySnapError
from bluepysnap.network import NetworkObject
from bluepysnap.nodes import NodePopulation
from bluepysnap.nodes.node_population import NodePopulation


class Nodes(
Expand Down
3 changes: 2 additions & 1 deletion bluepysnap/schemas/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import referencing
import yaml

from bluepysnap.exceptions import BluepySnapValidationError as Error

DEFINITIONS = "definitions"


Expand Down Expand Up @@ -45,7 +47,6 @@ def _wrap_errors(filepath, schema_errors, join_str):
"""
# NOTE: would probably make more sense to have different parser for circuit,
# as datatype and attributes only exists in the case of h5 files.
from bluepysnap.circuit_validation import Error

warnings = []
errors = []
Expand Down
5 changes: 4 additions & 1 deletion bluepysnap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

import json
import warnings
from collections import namedtuple
from collections.abc import Iterable

import numpy as np

from bluepysnap.circuit_ids import CircuitEdgeId, CircuitNodeId
from bluepysnap.exceptions import (
BluepySnapDeprecationError,
BluepySnapDeprecationWarning,
Expand All @@ -36,6 +36,9 @@
# all returned node or edge ids.
IDS_DTYPE = np.int64

CircuitNodeId = namedtuple("CircuitNodeId", ("population", "id"))
CircuitEdgeId = namedtuple("CircuitEdgeId", ("population", "id"))


class Deprecate:
"""Class for the deprecations in BluepySnap."""
Expand Down
32 changes: 3 additions & 29 deletions tests/test_circuit_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import bluepysnap.circuit_ids as test_module
from bluepysnap.exceptions import BluepySnapError
from bluepysnap.utils import IDS_DTYPE
from bluepysnap.utils import IDS_DTYPE, CircuitEdgeId, CircuitNodeId

from utils import setup_tempdir

Expand All @@ -18,40 +18,14 @@ def _multi_index():
return pd.MultiIndex.from_arrays([["a", "a", "b", "a"], [0, 1, 0, 2]])


class TestCircuitNodeId:
def setup_method(self):
self.test_obj = test_module.CircuitNodeId("pop", 1)

def test_init(self):
assert isinstance(self.test_obj, test_module.CircuitNodeId)
assert isinstance(self.test_obj, tuple)

def test_accessors(self):
assert self.test_obj.population == "pop"
assert self.test_obj.id == 1


class TestCircuitEdgeId:
def setup_method(self):
self.test_obj = test_module.CircuitEdgeId("pop", 1)

def test_init(self):
assert isinstance(self.test_obj, test_module.CircuitEdgeId)
assert isinstance(self.test_obj, tuple)

def test_accessors(self):
assert self.test_obj.population == "pop"
assert self.test_obj.id == 1


class TestCircuitNodeIds:
@property
def ids_cls(self):
return test_module.CircuitNodeIds

@property
def id_cls(self):
return test_module.CircuitNodeId
return CircuitNodeId

@property
def id_name(self):
Expand Down Expand Up @@ -335,7 +309,7 @@ def ids_cls(self):

@property
def id_cls(self):
return test_module.CircuitEdgeId
return CircuitEdgeId

@property
def id_name(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_edge_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import bluepysnap.edges.edge_population as test_module
from bluepysnap.bbp import Synapse
from bluepysnap.circuit import Circuit
from bluepysnap.circuit_ids import CircuitEdgeId, CircuitEdgeIds, CircuitNodeIds
from bluepysnap.circuit_ids import CircuitEdgeIds, CircuitNodeIds
from bluepysnap.exceptions import BluepySnapError
from bluepysnap.sonata_constants import DEFAULT_EDGE_TYPE, Edge
from bluepysnap.utils import IDS_DTYPE
from bluepysnap.utils import IDS_DTYPE, CircuitEdgeId

from utils import PICKLED_SIZE_ADJUSTMENT, TEST_DATA_DIR, copy_test_data, edit_config

Expand Down
4 changes: 2 additions & 2 deletions tests/test_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import bluepysnap.edges as test_module
from bluepysnap.bbp import Synapse
from bluepysnap.circuit import Circuit
from bluepysnap.circuit_ids import CircuitEdgeId, CircuitEdgeIds, CircuitNodeId, CircuitNodeIds
from bluepysnap.circuit_ids import CircuitEdgeIds, CircuitNodeIds
from bluepysnap.exceptions import BluepySnapError
from bluepysnap.utils import IDS_DTYPE
from bluepysnap.utils import IDS_DTYPE, CircuitEdgeId, CircuitNodeId

from utils import PICKLED_SIZE_ADJUSTMENT, TEST_DATA_DIR

Expand Down
4 changes: 2 additions & 2 deletions tests/test_frame_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import bluepysnap.frame_report as test_module
from bluepysnap.bbp import Cell
from bluepysnap.circuit_ids import CircuitNodeId, CircuitNodeIds
from bluepysnap.circuit_ids import CircuitNodeIds
from bluepysnap.exceptions import BluepySnapError
from bluepysnap.simulation import Simulation
from bluepysnap.utils import IDS_DTYPE
from bluepysnap.utils import IDS_DTYPE, CircuitNodeId

from utils import TEST_DATA_DIR

Expand Down
2 changes: 1 addition & 1 deletion tests/test_morph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import bluepysnap.morph as test_module
from bluepysnap.circuit import Circuit
from bluepysnap.circuit_ids import CircuitNodeId
from bluepysnap.exceptions import BluepySnapError
from bluepysnap.sonata_constants import Node
from bluepysnap.utils import CircuitNodeId

from utils import TEST_DATA_DIR, copy_test_data, create_node_population, edit_config

Expand Down
2 changes: 1 addition & 1 deletion tests/test_neuron_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import bluepysnap.neuron_models as test_module
from bluepysnap.circuit import Circuit, CircuitConfig
from bluepysnap.circuit_ids import CircuitNodeId
from bluepysnap.exceptions import BluepySnapError
from bluepysnap.sonata_constants import Node
from bluepysnap.utils import CircuitNodeId

from utils import TEST_DATA_DIR, copy_config, copy_test_data, create_node_population, edit_config

Expand Down
4 changes: 2 additions & 2 deletions tests/test_node_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

from bluepysnap.bbp import Cell
from bluepysnap.circuit import Circuit
from bluepysnap.circuit_ids import CircuitNodeId, CircuitNodeIds
from bluepysnap.circuit_ids import CircuitNodeIds
from bluepysnap.exceptions import BluepySnapError
from bluepysnap.node_sets import NodeSets
from bluepysnap.sonata_constants import DEFAULT_NODE_TYPE, Node
from bluepysnap.utils import IDS_DTYPE
from bluepysnap.utils import IDS_DTYPE, CircuitNodeId

from utils import (
PICKLED_SIZE_ADJUSTMENT,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

import bluepysnap.nodes as test_module
from bluepysnap.circuit import Circuit
from bluepysnap.circuit_ids import CircuitNodeId, CircuitNodeIds
from bluepysnap.circuit_ids import CircuitNodeIds
from bluepysnap.exceptions import BluepySnapError
from bluepysnap.node_sets import NodeSets
from bluepysnap.utils import IDS_DTYPE
from bluepysnap.utils import IDS_DTYPE, CircuitNodeId

from utils import PICKLED_SIZE_ADJUSTMENT, TEST_DATA_DIR

Expand Down
Loading