Skip to content

Commit

Permalink
Merge pull request #109 from clbarnes/default_tags
Browse files Browse the repository at this point in the history
Add tags=None to TreeNeurons
  • Loading branch information
clbarnes authored Nov 22, 2022
2 parents acc81af + 4e7b999 commit 16b6656
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions navis/core/skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class TreeNeuron(BaseNeuron):
# Set default function for soma finding. Default = :func:`navis.morpho.find_soma`
_soma: Union[Callable[['TreeNeuron'], Sequence[int]], int] = morpho.find_soma

tags: Optional[Dict[str, List[int]]] = None

#: Attributes to be used when comparing two neurons.
EQ_ATTRIBUTES = ['n_nodes', 'n_connectors', 'soma', 'root',
'n_branches', 'n_leafs', 'cable_length', 'name']
Expand Down
5 changes: 5 additions & 0 deletions navis/graph/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,9 @@ def reroot_skeleton(x: 'core.NeuronObject',

# If new root is a tag, rather than a ID, try finding that node
if isinstance(root, str):
if x.tags is None:
raise ValueError("Neuron does not have tags")

if root not in x.tags:
raise ValueError(f'#{x.id}: Found no nodes with tag {root}'
' - please double check!')
Expand Down Expand Up @@ -1416,6 +1419,8 @@ def cut_skeleton(x: 'core.NeuronObject',
for cn in where:
# If cut_node is a tag (rather than an ID), try finding that node
if isinstance(cn, str):
if x.tags is None:
raise ValueError(f"Neuron {x.id} has no tags")
if cn not in x.tags:
raise ValueError(f'#{x.id}: Found no node with tag {cn}'
' - please double check!')
Expand Down
4 changes: 2 additions & 2 deletions navis/morpho/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ def stitch_skeletons(*x: Union[Sequence[NeuronObject], 'core.NeuronList'],
if n.has_connectors:
n.connectors['node_id'] = n.connectors.node_id.map(lambda x: new_map.get(x, x))

if hasattr(n, 'tags'):
if getattr(n, 'tags', None) is not None:
n.tags = {new_map.get(k, k): v for k, v in n.tags.items()} # type: ignore

# Remap parent IDs
Expand All @@ -1110,7 +1110,7 @@ def stitch_skeletons(*x: Union[Sequence[NeuronObject], 'core.NeuronList'],
m.tags = {} # type: ignore # TreeNeuron has no tags

for n in nl:
for k, v in getattr(n, 'tags', {}).items():
for k, v in (getattr(n, 'tags', None) or {}).items():
m.tags[k] = m.tags.get(k, []) + list(utils.make_iterable(v))

# Reset temporary attributes of our final neuron
Expand Down
2 changes: 1 addition & 1 deletion navis/morpho/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def _subset_treeneuron(x, subset, keep_disc_cn, prevent_fragments):
x._connectors = x.connectors[x.connectors.node_id.isin(subset)]
x._connectors.reset_index(inplace=True, drop=True)

if hasattr(x, 'tags'):
if getattr(x, 'tags', None) is not None:
# Filter tags
x.tags = {t: [tn for tn in x.tags[t] if tn in subset] for t in x.tags} # type: ignore # TreeNeuron has no tags

Expand Down

0 comments on commit 16b6656

Please sign in to comment.