Exemple #1
0
    def parse(self, network):
        """
        Populate Connectivity DataType from NetworkX object.
        Tested with results from Connectome Mapper Toolkit.

        :param network: NetworkX graph
        :return: Connectivity object
        """
        graph_size = len(network.nodes())

        weights_matrix = numpy.zeros((graph_size, graph_size))
        tract_matrix = numpy.zeros((graph_size, graph_size))
        labels_vector, positions, cortical, hemisphere = [], [], [], []

        try:
            for node in xrange(1, graph_size + 1):
                node_data = network.nodes[node]

                pos = self._find_value(node_data, self.KEY_NODE_COORDINATES)
                positions.append(list(pos))

                label = self._find_value(node_data, self.KEY_NODE_LABEL)
                labels_vector.append(str(label))

                if self.REGION_CORTICAL == self._find_value(
                        node_data, self.KEY_NODE_REGION):
                    cortical.append(1)
                else:
                    cortical.append(0)

                if self.HEMISPHERE_RIGHT == self._find_value(
                        node_data, self.KEY_NODE_HEMISPHERE):
                    hemisphere.append(True)
                else:
                    hemisphere.append(False)

            # Iterate over edges:
            for start, end in network.edges():
                weights_matrix[start - 1][end - 1] = self._find_value(
                    network.adj[start][end], self.KEY_EDGE_WEIGHT)
                tract_matrix[start - 1][end - 1] = self._find_value(
                    network.adj[start][end], self.KEY_EDGE_TRACT)

            result = Connectivity()
            result.storage_path = self.storage_path
            result.region_labels = labels_vector
            result.centres = positions
            result.set_metadata(
                {'description': 'Array Columns: labels, X, Y, Z'}, 'centres')
            result.hemispheres = hemisphere
            result.cortical = cortical
            result.weights = weights_matrix
            result.tract_lengths = tract_matrix
            return result

        except KeyError as err:
            self.logger.exception("Could not parse Connectivity")
            raise ParseException(err)
Exemple #2
0
    def parse(self, network):
        """
        Populate Connectivity DataType from NetworkX object.
        Tested with results from Connectome Mapper Toolkit.

        :param network: NetworkX graph
        :return: Connectivity object
        """
        graph_size = len(network.nodes())

        weights_matrix = numpy.zeros((graph_size, graph_size))
        tract_matrix = numpy.zeros((graph_size, graph_size))
        labels_vector, positions, cortical, hemisphere = [], [], [], []

        try:
            for node in network.nodes():
                node_data = network.node[node]

                pos = self._find_value(node_data, self.KEY_NODE_COORDINATES)
                positions.append(list(pos))

                label = self._find_value(node_data, self.KEY_NODE_LABEL)
                labels_vector.append(str(label))

                if self.REGION_CORTICAL == self._find_value(node_data, self.KEY_NODE_REGION):
                    cortical.append(1)
                else:
                    cortical.append(0)

                if self.HEMISPHERE_RIGHT == self._find_value(node_data, self.KEY_NODE_HEMISPHERE):
                    hemisphere.append(True)
                else:
                    hemisphere.append(False)

            # Iterate over edges:
            for start, end in network.edges():
                weights_matrix[start - 1][end - 1] = self._find_value(network.adj[start][end], self.KEY_EDGE_WEIGHT)
                tract_matrix[start - 1][end - 1] = self._find_value(network.adj[start][end], self.KEY_EDGE_TRACT)

            result = Connectivity()
            result.storage_path = self.storage_path
            result.region_labels = labels_vector
            result.centres = positions
            result.set_metadata({'description': 'Array Columns: labels, X, Y, Z'}, 'centres')
            result.hemispheres = hemisphere
            result.cortical = cortical
            result.weights = weights_matrix
            result.tract_lengths = tract_matrix
            return result

        except KeyError, err:
            self.logger.exception("Could not parse Connectivity")
            raise ParseException(err)
def networkx2connectivity(network_obj, storage_path):
    """
    Populate Connectivity DataType from NetworkX object.
    """
    network_obj.load()
    weights_matrix, tract_matrix, labels_vector = [], [], []
    positions, areas, orientation = [], [], []
    # Read all nodes
    graph_data = network_obj.data
    graph_size = len(graph_data.nodes())
    for node in graph_data.nodes():
        positions.append([
            graph_data.node[node][ct.KEY_POS_X],
            graph_data.node[node][ct.KEY_POS_Y],
            graph_data.node[node][ct.KEY_POS_Z]
        ])
        labels_vector.append(graph_data.node[node][ct.KEY_POS_LABEL])
        if ct.KEY_AREA in graph_data.node[node]:
            areas.append(graph_data.node[node][ct.KEY_AREA])
        if ct.KEY_ORIENTATION_AVG in graph_data.node[node]:
            orientation.append(graph_data.node[node][ct.KEY_ORIENTATION_AVG])
        weights_matrix.append([0.0] * graph_size)
        tract_matrix.append([0.0] * graph_size)
    # Read all edges
    for edge in network_obj.data.edges():
        start = edge[0]
        end = edge[1]
        weights_matrix[start][end] = graph_data.adj[start][end][ct.KEY_WEIGHT]
        tract_matrix[start][end] = graph_data.adj[start][end][ct.KEY_TRACT]

    meta = network_obj.get_metadata_as_dict()

    result = Connectivity()
    result.storage_path = storage_path
    result.nose_correction = meta[ct.KEY_NOSE] if ct.KEY_NOSE in meta else None
    result.weights = weights_matrix
    result.centres = positions
    result.region_labels = labels_vector
    result.set_metadata({'description': 'Array Columns: labels, X, Y, Z'},
                        'centres')
    result.orientations = orientation
    result.areas = areas
    result.tract_lengths = tract_matrix
    return result, (meta[ct.KEY_UID] if ct.KEY_UID in meta else None)
def networkx2connectivity(network_obj, storage_path):
    """
    Populate Connectivity DataType from NetworkX object.
    """
    network_obj.load()
    weights_matrix, tract_matrix, labels_vector = [], [], []
    positions, areas, orientation = [], [], []
    # Read all nodes
    graph_data = network_obj.data
    graph_size = len(graph_data.nodes())
    for node in graph_data.nodes():
        positions.append([graph_data.node[node][ct.KEY_POS_X], 
                          graph_data.node[node][ct.KEY_POS_Y],
                          graph_data.node[node][ct.KEY_POS_Z]])
        labels_vector.append(graph_data.node[node][ct.KEY_POS_LABEL])
        if ct.KEY_AREA in graph_data.node[node]:
            areas.append(graph_data.node[node][ct.KEY_AREA])
        if ct.KEY_ORIENTATION_AVG in graph_data.node[node]:
            orientation.append(graph_data.node[node][ct.KEY_ORIENTATION_AVG])
        weights_matrix.append([0.0] * graph_size)
        tract_matrix.append([0.0] * graph_size)
    # Read all edges
    for edge in network_obj.data.edges():
        start = edge[0]
        end = edge[1]
        weights_matrix[start][end] = graph_data.adj[start][end][ct.KEY_WEIGHT]
        tract_matrix[start][end] = graph_data.adj[start][end][ct.KEY_TRACT]

    meta = network_obj.get_metadata_as_dict()
    
    result = Connectivity()
    result.storage_path = storage_path
    result.nose_correction = meta[ct.KEY_NOSE] if ct.KEY_NOSE in meta else None
    result.weights = weights_matrix
    result.centres = positions
    result.region_labels = labels_vector
    result.set_metadata({'description':'Array Columns: labels, X, Y, Z'},'centres')
    result.orientations = orientation
    result.areas = areas
    result.tract_lengths = tract_matrix
    return result, (meta[ct.KEY_UID] if ct.KEY_UID in meta else None)