Mentions légales du service

Skip to content
Snippets Groups Projects
graph_extraction.py 3.9 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 = [xyz for xyz, _ 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
    coordinates = GetNodesCoordinates(node_coord)
    # get a list with elements = (soma_uid, extension_uid, root coordinates)
    roots = GetListRoots(somas)
    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[2] == coordinates[node]:
                    ext_skl_graph._node[node_coord[node]]['root'] = True
                else:
                    try:
                        if ext_skl_graph._node[node_coord[node]]['root'] is not True:
                            ext_skl_graph._node[node_coord[node]]['root'] = False
                    except:
                        ext_skl_graph._node[node_coord[node]]['root'] = False
            ext_skl_graph._node[node_coord[node]]['root'] = False

    return ext_skl_graph

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


def GetNodesCoordinates(node_coord: list) -> list:  # TODO: pb with the list coord_node
    """
    Gets a list of coordinates - tuple (x,y,z) - from the nodes attributes  - str 'x-y-z' - .
    """
    coord = node_coord.copy()
    for node in range(len(node_coord)):
        coord_node = coord[node]
        coord_node = re_.findall(pattern, coord_node)
            coor.append(int(coord[i]))
        coord_node[node] = coor

    return coord_node

def GetListRoots(somas: soma_t) -> list:
    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]))
    return roots