Mentions légales du service

Skip to content
Snippets Groups Projects
graph_extraction.py 4.79 KiB
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
from sklgraph.skl_graph import skl_graph_t
from typing import Tuple
NADAL Morgane's avatar
NADAL Morgane committed
def FindGraphsRootWithEdges(somas: Tuple[soma_t,...], ext_skl_graph: skl_graph_t, ext_nfo) -> list:
NADAL Morgane's avatar
NADAL Morgane committed
    Finds the soma roots of the graph extension.
    """
    # For a given soma, find the roots of the graphs
    list_root_nodes = []

    for soma in somas:

        dict_root_nodes = {}
        root_nodes = {}

        # Finds the primary extensions
        primary_extension_uids = tuple(extension.uid for extension in soma.extensions)

        # List of the degree 1 nodes of the graph
        for node1_id, node2_id, edge_nfo in ext_skl_graph.edges.data('as_edge_t'):
            if (ext_skl_graph.degree[node1_id] == 1) or (ext_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
NADAL Morgane's avatar
NADAL Morgane committed

                # Save the root node candidates (one-degree nodes)
NADAL Morgane's avatar
NADAL Morgane committed
                if ext_uid in primary_extension_uids:
                    if ext_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)]
NADAL Morgane's avatar
NADAL Morgane committed

                    root_sites = tuple(
                        (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)

                    # Find the intersection between the extended root node candidate and the soma contour points
NADAL Morgane's avatar
NADAL Morgane committed
                    intersections = soma_contour_sites.intersection(root_sites)

                    # if the graph root sites are included in the soma extensions sites (non-nul intersection):
NADAL Morgane's avatar
NADAL Morgane committed
                    if len(intersections) > 0:
                        # Keep the info of the root node
                        root_nodes['Ext. ' + str(ext_uid)] = root_node  # TODO: delete qnd just put ext uid
                            # By construction, only one root node possible for an ext
NADAL Morgane's avatar
NADAL Morgane committed
        dict_root_nodes["Soma " + str(soma.uid)] = root_nodes
        list_root_nodes.append(dict_root_nodes)
        print(dict_root_nodes)
NADAL Morgane's avatar
NADAL Morgane committed

    return list_root_nodes


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), ...]
    for node in range(len(node_coord)):
        coord_node = coord[node]
        coord_node = re_.findall(pattern, coord_node)