Skip to content

Commit

Permalink
Fix empty FixedPositions (#811)
Browse files Browse the repository at this point in the history
* fix `get_node_name` for unbooted components

* improve node naming, added tests

* fix 0 len FixedPositions

* test issue 802

* fix missing dot

* fix missing name concat
  • Loading branch information
Helveg committed Mar 7, 2024
1 parent b053e60 commit 4fc49a0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 11 deletions.
11 changes: 8 additions & 3 deletions bsb/config/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,16 +397,21 @@ def get_config_attributes(cls):

def _get_node_name(self):
name = ".<missing>"
if hasattr(self, "attr_name"):
if getattr(self, "attr_name", None) is not None:
name = "." + str(self.attr_name)
if hasattr(self, "_config_key"):
if getattr(self, "_config_key", None) is not None:
name = "." + str(self._config_key)
if hasattr(self, "_config_index"):
if self._config_index is None:
return "{removed}"
else:
name = "[" + str(self._config_index) + "]"
return self._config_parent.get_node_name() + name
if getattr(self, "name", None) is not None:
name = "." + self.name
if getattr(self, "_config_parent", None):
return self._config_parent.get_node_name() + name
else:
return "{standalone}" + name


def make_get_node_name(node_cls, root):
Expand Down
14 changes: 6 additions & 8 deletions bsb/placement/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@
from .. import config
from .._util import obj_str_insert
from ..config import refs, types
from ..config._attrs import cfgdict, cfglist
from ..exceptions import (
DistributorError,
EmptySelectionError,
MissingSourceError,
SourceQualityError,
)
from ..config._attrs import cfgdict
from ..exceptions import DistributorError, EmptySelectionError
from ..mixins import HasDependencies
from ..profiling import node_meter
from ..reporting import report
from ..reporting import report, warn
from ..storage import Chunk
from ..voxels import VoxelSet
from .distributor import DistributorsNode
Expand Down Expand Up @@ -171,6 +166,9 @@ def place(self, chunk, indicators):
raise ValueError(
f"Please set `.positions` on '{self.name}' before placement."
)
if not len(self.positions):
warn(f"No positions given to {self.get_node_name()}.")
return
for indicator in indicators.values():
inside_chunk = VoxelSet([chunk], chunk.dimensions).inside(self.positions)
self.place_cells(indicator, self.positions[inside_chunk], chunk)
Expand Down
49 changes: 49 additions & 0 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,55 @@ def test_booted_root(self):
self.assertIsNotNone(_attrs._booted_root(cfg), "now it should be booted")


class TestNodeClass(unittest.TestCase):
def test_standalone_node_name(self):
"""
Test the node name of a node without any name information
"""

@config.node
class Test:
pass

self.assertEqual("{standalone}.<missing>", Test().get_node_name())

def test_standalone_named_node_name(self):
"""
Test the node name of a node with name information
"""

@config.node
class Test:
name = "hello"

self.assertEqual("{standalone}.hello", Test().get_node_name())

def test_standalone_keyed_node_name(self):
"""
Test the node name of a node with key information
"""

@config.node
class Test:
other_key = config.attr(key=True)

@config.node
class Parent:
d = config.dict(type=Test)

self.assertEqual(
"{standalone}.<missing>.d.myname",
Parent(d={"myname": Test()}).d.myname.get_node_name(),
)

def test_root_node_name(self):
@config.root
class Test:
pass

self.assertEqual("{root}", Test().get_node_name())


class TestNodeComposition(unittest.TestCase):
def setUp(self):
@config.node
Expand Down
15 changes: 15 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
import unittest

from bsb import config
from bsb.cell_types import CellType
from bsb.config.refs import Reference
from bsb.exceptions import CfgReferenceError
from bsb.placement import FixedPositions
from bsb.placement.indicator import PlacementIndications
from bsb.storage import Chunk


def relative_to_tests_folder(path):
Expand Down Expand Up @@ -44,3 +48,14 @@ def test_430(self):
examples=dict(), extensions=dict(x=dict(ex_mut=4, ref="missing"))
)
print("ref", config.extensions.x.ref)

def test_802(self):
"""
Test fixed positions with 0 positions
"""
with self.assertWarns(UserWarning):
# Cobble some components together to test `positions=[]`
FixedPositions(positions=[], cell_types=[], partitions=[]).place(
Chunk((0, 0, 0), (100, 100, 100)),
{CellType(spatial=dict(radius=1, count=1)): PlacementIndications()},
)

0 comments on commit 4fc49a0

Please sign in to comment.