Newer
Older
# Copyright CNRS/Inria/UNS
# Contributor(s): Eric Debreuve (since 2019), Morgane Nadal (2020)
#
# eric.debreuve@cnrs.fr
#
# This software is governed by the CeCILL license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.
import re as re_
import numpy as np_
from brick.component.soma import soma_t
NADAL Morgane
committed
from brick.general.type import array_t
from sklgraph.skl_graph import skl_graph_t
NADAL Morgane
committed
from typing import Tuple, Dict, Union, Any
NADAL Morgane
committed
def FindGraphsRootWithEdges(soma: soma_t, ext_nfo: Dict[str, Union[array_t, Any]]) -> dict:
Finds the soma roots of the graph extension.
"""
# For a given soma, find the roots of the graphs
NADAL Morgane
committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
root_nodes = {}
# Finds the primary extensions
primary_extension_uids = tuple(extension.uid for extension in soma.extensions)
print(primary_extension_uids, '\nn = ', len(primary_extension_uids))
# List of the degree 1 nodes of the graph
for node1_id, node2_id, edge_nfo in soma.skl_graph.edges.data('as_edge_t'):
if (soma.skl_graph.degree[node1_id] == 1) or (soma.skl_graph.degree[node2_id] == 1):
# Find the pixels of the terminal extension
sites = ext_nfo['lmp'][edge_nfo.sites]
ext_uid = np_.unique(sites)[-1]
# sites > 0 because ext_nfo['lmp'] do not contain the connexions
# Save the root node candidates (one-degree nodes)
if ext_uid in primary_extension_uids:
if soma.skl_graph.degree[node1_id] == 1:
root_node = node1_id
else:
root_node = node2_id
# Get the node coordinates and extend them to the 26 neighboring voxels
root_node_coor = GetNodesCoordinates((root_node,))[0] # tuple('x-y-z') -> list[(x,y,z)]
root_sites = set(
(root_node_coor[0] + i, root_node_coor[1] + j, root_node_coor[2] + k)
for i in (-1, 0, 1)
for j in (-1, 0, 1)
for k in (-1, 0, 1)
if i != 0 or j != 0 or k != 0)
# soma_contour_sites = set(
# (soma.contour_points[0] + i, soma.contour_points[1] + j, soma.contour_points[2] + k)
# for i in (-1, 0, 1)
# for j in (-1, 0, 1)
# for k in (-1, 0, 1)
# if i != 0 or j != 0 or k != 0
# Find the intersection between the extended root node candidate and the soma contour points
intersections = soma_contour_sites.intersection(root_sites)
# if the graph root sites are included in the soma extensions sites (non-nul intersection):
if len(intersections) > 0:
# Keep the info of the root node. Key = ext uid, Value = root node
root_nodes[ext_uid] = root_node
## By construction, only one root node possible for an ext
return root_nodes
def FindGraphsRootWithNodes(soma: soma_t) -> list:
"""
Find the roots of the {extension+connexion} graphs to be lined to the soma.
Add a key "root" (bool) in the dict of nodes attributes.
"""
NADAL Morgane
committed
node_degree_bool = tuple(degree == 1 for _, degree in soma.skl_graph.degree)
node_coord = tuple(xyz for xyz, _ in soma.skl_graph.degree)
NADAL Morgane
committed
root_nodes = {}
NADAL Morgane
committed
# get the coordinates of the nodes (x,y,z)
coordinates = GetNodesCoordinates(node_coord)
NADAL Morgane
committed
# get a list with elements = (soma_uid, extension_uid, root coordinates)
roots = GetListRoots(soma)
NADAL Morgane
committed
# for each node in the graph, search among the degree 1 nodes the nodes that are roots (linked to soma)
for node in range(len(coordinates)):
if node_degree_bool[node]:
# compare the coor with end points
for ext_root in roots:
if ext_root[1] == coordinates[node]:
root_nodes[ext_root[0]] = node_coord[node]
NADAL Morgane
committed
return root_nodes
NADAL Morgane
committed
def GetListRoots(soma: soma_t) -> list:
"""
Gives a list containing the following information for all somas: [soma id: int, extension id: int, root = (x,y,z): tuple]
"""
roots = []
for ext_id, ext_root in enumerate(soma.ext_roots):
roots.append((soma.extensions[ext_id].uid, ext_root))
return roots
def GetNodesCoordinates(node_coord: Tuple[str, ...]) -> list:
"""
Input: nodes attributes -> Tuple('x1-y1-z1', 'x2-y2-z2', ...) .
Output: coordinates -> List[Tuple(x1,y1,z1), Tuple(x2,y2,z2), ...]
coord = []
for c in node_coord:
coord.append(c)
for node in range(len(node_coord)):
pattern = '\d+'
coord_node = re_.findall(pattern, coord_node)
coor = []
for i in range(3):
coor.append(int(coord_node[i]))
coor = tuple(coor)
coord[node] = coor
return coord