Mentions légales du service

Skip to content
Snippets Groups Projects
graph_extraction.py 3.61 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 networkx as nx_
import numpy as np_

from brick.component.soma import soma_t
from sklgraph.skl_graph import skl_graph_t
from typing import Tuple


def FindGraphsRoot(somas: Tuple[soma_t,...], ext_skl_graph: skl_graph_t):
    """
    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.
    """

    node_degree_bool = [degree == 1 for _, degree in ext_skl_graph.degree]
    node_coord = [node_coord for node_coord, _ in ext_skl_graph.degree]

    # get the list with the indexes of the degree 1 nodes in node_degree
    node_degree_1 = list(np_.where(node_degree_bool)[0])

    # get the coordinates of the nodes
    node_coord = GetNodesCoordinates(node_coord)
    #
    roots = []
    for soma in somas:
        for ext_id, ext_root in enumerate(soma.ext_root):
        roots.append((soma.uid, soma.extensions[ext_id].uid, ext_root[0]))

    for node in range(len(node_degree_1)):
        if node_degree_1[node]:

            # compare the coor with end points
            # TODO: use intersection of sets instead
            if soma.ext_root:  # the list must not be empty
                for root in soma.ext_root:
                    if root[0] == node_coord[node]:
                        # add the information 'root' to the extremity node
                        ext_skl_graph._node[node_coord]['root'] = True
                        node_coord.pop(node)

        else:
            node_coord = node_coord[node]
            ext_skl_graph._node[node_coord]['root'] = False

    return ext_skl_graph

    # TODO: add to every edge the id of the extension


def GetNodesCoordinates(node_coord: list):
    """
    Gets a list of coordinates - tuple (x,y,z) - from the nodes attributes  - str 'x-y-z' - .
    """
    for node in range(len(node_coord)):
        coord = node_coord[node]
        pattern = '\d+'
        c = re_.findall(pattern, coord)
        coor = []
        for i in range(3):
            coor.append(int(c[i]))
        coor = tuple(coor)
        node_coord[node] = coor

    return node_coord