예제 #1
0
def name_estimation(graph, group, layer, graphreference, vectorizer, nameestimator, subgraphs):
    if subgraphs:
        map(remove_eden_annotation, subgraphs)
        try:
            data = vectorizer.transform(subgraphs)
        except:
            draw.graphlearn(subgraphs, contract= False)
        clusterids = nameestimator.predict(data)

        #for d, g in zip(data, subgraphs):
        #    g.graph['hash_title'] = hash_function(d)
        #draw.graphlearn(subgraphs,size=2, title_key='hash_title', edge_label='label')

        for sg, clid in zip(subgraphs, clusterids):
            for n in sg.nodes():
                graph.node[n][group] = '-' if clid == -1 else str(clid)

    # doing the contraction...
    graph = contraction([graph], contraction_attribute=group, modifiers=[],
                        nesting=False, dont_contract_attribute_symbol='-').next()

    # write labels
    def f(n, d):
        d['label'] = graphreference.node[max(d['contracted'])]['label'] \
            if d['label'] == '-' else "L%sC%s" % (layer, d['label'])

    node_operation(graph, f)
    return graph
예제 #2
0
    def transform(self, seqs, listof_start_end_weight_list=None):
        """Transforms the sequences in input into graphs.

        Parameters
        ----------
        seqs : iterable strings
            Input sequences.

        listof_start_end_weight_list : list of start_end_weight_lists
            Each element in listof_start_end_weight_list specifies the start_end_weight_list
            for a single graph.

        Returns
        -------
        graphs : iterable graphs in Networkx format
            List of graphs resulting from the transformation of sequences into folded structures.
        """

        graphs = rnaplfold_to_eden(seqs,
                                   window_size=self.window_size,
                                   max_bp_span=self.max_bp_span,
                                   avg_bp_prob_cutoff=self.avg_bp_prob_cutoff,
                                   max_num_edges=self.max_num_edges)

        if listof_start_end_weight_list is None:
            graphs = list_reweight(graphs, start_end_weight_list=self.start_end_weight_list)
            graphs = list_reweight(graphs, start_end_weight_list=self.start_end_weight_list,
                                   attribute='level')
        else:
            graphs = listof_list_reweight(graphs,
                                          listof_start_end_weight_list=listof_start_end_weight_list)
            graphs = listof_list_reweight(graphs,
                                          listof_start_end_weight_list=listof_start_end_weight_list,
                                          attribute='level')
        # annotate in node attribute 'type' the incident edges' labels
        graphs = vertex_attributes.incident_edge_label(graphs,
                                                       level=self.contraction_level,
                                                       output_attribute='type',
                                                       separator='.')
        # reduce all 'label' attributes of contracted nodes to a histogram to be written in the
        # 'label' attribute of the resulting graph
        label_modifier = contraction_modifier(attribute_in='type',
                                              attribute_out='label',
                                              reduction='set_categorical')
        # reduce all 'weight' attributes of contracted nodes using a sum to be written in the
        # 'weight' attribute of the resulting graph
        weight_modifier = contraction_modifier(attribute_in='weight',
                                               attribute_out='weight',
                                               reduction='average')
        modifiers = [label_modifier, weight_modifier]
        # contract the graph on the 'type' attribute
        graphs = contraction(graphs,
                             contraction_attribute='type',
                             modifiers=modifiers,
                             contraction_weight_scaling_factor=self.contraction_weight_scaling_factor,
                             nesting=True)
        return graphs
예제 #3
0
def edge_type_in_radius_abstraction(graph):
    '''
    # the function needs to set a 'contracted' attribute to each node with a set of vertices that
    # are contracted.
    :param graph: any graph   .. what kind? expanded? which flags musst be set?
    :return: an abstract graph with node annotations that refer to the node ids it is contracting
    '''
    # annotate in node attribute 'type' the incident edges' labels
    labeled_graph = vertex_attributes.incident_edge_label(
        [graph], level=2, output_attribute='type', separator='.').next()
    # do contraction
    contracted_graph = contraction(
        [labeled_graph], contraction_attribute='type', modifiers=[], nesting=False).next()
    return contracted_graph
예제 #4
0
def edge_type_in_radius_abstraction(graph):
    '''
    # the function needs to set a 'contracted' attribute to each node with a set of vertices that
    # are contracted.
    :param graph: any graph   .. what kind? expanded? which flags musst be set?
    :return: an abstract graph with node annotations that refer to the node ids it is contracting
    '''
    # annotate in node attribute 'type' the incident edges' labels
    labeled_graph = vertex_attributes.incident_edge_label(
        [graph], level=2, output_attribute='type', separator='.').next()
    # do contraction
    contracted_graph = contraction([labeled_graph],
                                   contraction_attribute='type',
                                   modifiers=[],
                                   nesting=False).next()
    return contracted_graph
예제 #5
0
def contract(graph):
    contracted_graph = contraction([graph], contraction_attribute="type", modifiers=[], nesting=False).next()
    return contracted_graph
예제 #6
0
    def abstract(self,
                 graph,
                 score_attribute='importance',
                 group='class',
                 debug=False):
        '''
        Parameters
        ----------
        score_attribute
        group

        Returns
        -------
        '''

        graph = self.vectorizer._edge_to_vertex_transform(graph)
        graph2 = self.vectorizer._revert_edge_to_vertex_transform(graph)

        if debug:
            print 'abstr here1'
            draw.graphlearn(graph2)

        graph2 = self.vectorizer.annotate(
            [graph2], estimator=self.rawgraph_estimator.estimator).next()

        for n, d in graph2.nodes(data=True):
            #d[group]=str(math.floor(d[score_attribute]))
            d[group] = str(self.kmeans.predict(d[score_attribute])[0])

        if debug:
            print 'abstr here'
            draw.graphlearn(graph2, vertex_label='class')

        graph2 = contraction([graph2],
                             contraction_attribute=group,
                             modifiers=[],
                             nesting=False).next()
        graph2 = self.vectorizer._edge_to_vertex_transform(graph2)

        # find out to which abstract node the edges belong
        # finding out where the edge-nodes belong, because the contractor cant possibly do this
        getabstr = {
            contra: node
            for node, d in graph2.nodes(data=True)
            for contra in d.get('contracted', [])
        }

        for n, d in graph.nodes(data=True):
            if 'edge' in d:
                # if we have found an edge node...
                # lets see whos left and right of it:
                n1, n2 = graph.neighbors(n)
                # case1: ok those belong to the same gang so we most likely also belong there.
                if getabstr[n1] == getabstr[n2]:
                    graph2.node[getabstr[n1]]['contracted'].add(n)

                # case2: neighbors belong to different gangs...
                else:
                    blub = set(graph2.neighbors(getabstr[n1])) & set(
                        graph2.neighbors(getabstr[n2]))
                    for blob in blub:
                        if 'contracted' in graph2.node[blob]:
                            graph2.node[blob]['contracted'].add(n)
                        else:
                            graph2.node[blob]['contracted'] = set([n])
        return graph2