Mentions légales du service

Skip to content
Snippets Groups Projects
graph_extraction.py 4.4 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) -> 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 = tuple(degree == 1 for _, degree in ext_skl_graph.degree)
    node_coord = tuple(xyz for xyz, _ in ext_skl_graph.degree)
    # get the coordinates of the nodes (x,y,z)
    coordinates = GetNodesCoordinates(node_coord)
    # get a list with elements = (soma_uid, extension_uid, root coordinates)
    roots = GetListRoots(somas)
    # 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[2] == coordinates[node]:
                    # add the attribute 'root' = True in the dict of nodes attributes
                    ext_skl_graph._node[node_coord[node]]['root'] = True
                else:
                    try:
                        # verify if the node has already been covered to avoid overwriting on a True value:
                        if ext_skl_graph._node[node_coord[node]]['root'] is not True:
                            ext_skl_graph._node[node_coord[node]]['root'] = False
                    except:
                        # add the attribute 'root' = False for non-root endpoints in the dict of nodes attributes
                        ext_skl_graph._node[node_coord[node]]['root'] = False
            # all the nodes with degree > 1 are not roots
            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: tuple) -> 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]
        coord_node = re_.findall(pattern, coord_node)

def GetListRoots(somas: 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 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