python类set_node_attributes()的实例源码

tac_analysis.py 文件源码 项目:pythia 作者: elazarg 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def run_analysis(cfg):
    import graph_utils as gu
    import networkx as nx

    Analysis = ConsProp 
    def compute_transfer_function(): pass
    gu.node_data_map_inplace(cfg, attr='transfer',
                             f=lambda n, d: compute_transfer_function(d))
    nx.set_node_attributes(cfg, 'out', {n:Analysis() for n in cfg.nodes_iter()})
    dataflow(cfg, 0, {}, Analysis)
classes.py 文件源码 项目:binet 作者: crisjf 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def _project_CP(self,side):
        """
        Builds the projection of the bipartite network on to the chosen side.
        The projection is done using conditional probability.

        Parameters
        ----------
        side : int or str
            Tags for each side of the bipartite network.
        """
        self._check_side(side)
        aside = self.side if side == self.aside else self.aside
        net = self.edges(as_df=True)[[side,aside]]
        dis = merge(net,net,how='inner',left_on=aside,right_on=aside).groupby([side+'_x',side+'_y']).count().reset_index().rename(columns={aside:'n_both'})
        nodes = merge(self.nodes(side,as_df=True)[[side]].reset_index().rename(columns={'index':side+'_index'}),DataFrame(self.degree(side).items(),columns=[side,'n']))
        dis = merge(dis,nodes,how='left',right_on=side,left_on=side+'_x').drop(side,1)
        dis = merge(dis,nodes,how='left',right_on=side,left_on=side+'_y').drop(side,1)
        dis = dis[dis[side+'_index_x']>dis[side+'_index_y']].drop([side+'_index_x',side+'_index_y'],1)

        dis['p_x'] = dis['n_both']/dis['n_x'].astype(float)
        dis['p_y'] = dis['n_both']/dis['n_y'].astype(float)
        dis['fi'] = dis[['p_x','p_y']].min(1)
        dis = dis[[side+'_x',side+'_y','fi']]
        self.P[side] = gGraph(node_id=side)
        self.P[side].add_weighted_edges_from([val[1:] for val in dis.itertuples()])
        nodes = merge(self.P[side].nodes(as_df=True),self.nodes(side,as_df=True),how='left')
        properties = nodes.columns.values.tolist()
        properties.remove(side)
        for prop in properties:
            values = dict(zip(nodes[side].values,nodes[prop].values))
            set_node_attributes(self.P[side],prop,values)
build_splicegraph.py 文件源码 项目:exfi 作者: jlanga 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def build_splicegraph(exon_index):
    """Build the splicegraph from a dict of SeqRecords

    Splicegraph is a directed graph, whose nodes
        - are exon_ids,
        - attributes are
            - coordinates [(transcript1, start, end), ..., (transcriptN, start, end)]
            - sequence in str format
    and whose edges
        - are connected exons in any way
        - attributes are the overlap between them:
            - positive means there is an overlap of that number of bases
            - zero means no overlap
            - negative means a gap of that number of bases
    """
    # Initialize grpah
    splice_graph = nx.DiGraph()

    # Add nodes
    splice_graph.add_nodes_from(exon_index.keys())
    nx.set_node_attributes(
        G=splice_graph,
        name='coordinates',
        values= exon_to_coordinates(exon_index)
    )
    nx.set_node_attributes(
        G=splice_graph,
        name='sequence',
        values={exon.id : str(exon.seq) for exon in exon_index.values()}
    )

    # Edges
    transcript2path = transcript_to_path(exons_to_df(exon_index))
    for path in transcript2path.values():
        splice_graph.add_path(path)

    nx.set_edge_attributes(
        G=splice_graph,
        name='overlap',
        values = compute_edge_overlaps(splice_graph)
    )

    return splice_graph
molecule.py 文件源码 项目:InnerOuterRNN 作者: Chemoinformatics 项目源码 文件源码 阅读 44 收藏 0 点赞 0 评论 0
def reduce_graph_rings(self):
        '''
        :return:
        '''
        cycle_name_format = "R_{:}"
        index = 0
        cycle = self.get_cycle()

        while cycle:
            cycle_name = cycle_name_format.format(index)
            self.graph.add_node(cycle_name)

            # ebunch = zip(cycle, (cycle[1:] + cycle[:1]))
            self.graph.remove_edges_from(cycle)

            for node1, node2 in cycle:
                if isinstance(node1, six.string_types):
                    self.graph.add_edge(node1, cycle_name,
                                        attr_dict={"bond_features": Molecule.bond_features_between_contract_rings()})
                    continue

                neighbours = self.graph.neighbors(node1)
                if not neighbours:
                    continue
                for neighbour in neighbours:
                    edge_attrs = self.get_bond_features(neighbour, node1)
                    self.graph.add_edge(neighbour, cycle_name, attr_dict={
                        "bond_features": edge_attrs})
                    self.graph.remove_edge(node1, neighbour)

            nx.set_node_attributes(self.graph, "atom_features",
                                   values={cycle_name: Molecule.atom_features_of_contract_rings(0)})

            for node1, node2 in cycle:
                if not isinstance(node1, six.string_types):
                    self.graph.remove_node(node1)
            index += 1
            cycle = self.get_cycle()

        self.graph = nx.convert_node_labels_to_integers(self.graph,
                                                        first_label=0)

        nx.draw(self.graph)
        self.no_of_atoms = len(self.graph)
model.py 文件源码 项目:WNTR 作者: USEPA 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def add_tank(self, name, elevation=0.0, init_level=3.048,
                 min_level=0.0, max_level=6.096, diameter=15.24,
                 min_vol=None, vol_curve=None, coordinates=None):
        """
        Adds a tank to the water network model.

        Parameters
        -------------------
        name : string
            Name of the tank.
        elevation : float
            Elevation at the Tank.
        init_level : float
            Initial tank level.
        min_level : float
            Minimum tank level.
        max_level : float
            Maximum tank level.
        diameter : float
            Tank diameter.
        min_vol : float
            Minimum tank volume.
        vol_curve : Curve object
            Curve object
        coordinates : tuple of floats
            X-Y coordinates of the node location.

        Raises
        ------
        ValueError
            If `init_level` greater than `max_level` or less than `min_level`

        """
        elevation = float(elevation)
        init_level = float(init_level)
        min_level = float(min_level)
        max_level = float(max_level)
        diameter = float(diameter)
        if min_vol is not None:
            min_vol = float(min_vol)
        if init_level < min_level:
            raise ValueError("Initial tank level must be greater than or equal to the tank minimum level.")
        if init_level > max_level:
            raise ValueError("Initial tank level must be less than or equal to the tank maximum level.")
        if vol_curve and isinstance(vol_curve, six.string_types):
            vol_curve = self.get_curve(vol_curve)
        tank = Tank(name, elevation, init_level, min_level, max_level, diameter, min_vol, vol_curve)
        self._nodes[name] = tank
        self._tanks[name] = tank
        self._graph.add_node(name)
        if coordinates is not None:
            self.set_node_coordinates(name, coordinates)
        nx.set_node_attributes(self._graph, name='type', values={name: 'tank'})
        self._num_tanks += 1
graph.py 文件源码 项目:WNTR 作者: USEPA 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def weight_graph(self, node_attribute={}, link_attribute={}):
        """
        Return a weighted graph based on node and link attributes.
        The weighted graph changes the direction of the original link if the weight is negative.

        Parameters
        ----------
        G : graph
            A networkx graph
        node_attribute :  dict or pandas Series
            node attributes
        link_attribues : dict or pandas Series
            link attributes


        Returns
        -------
        G : weighted graph
            A networkx weighted graph
        """

        for node_name in self.nodes():
            try:
                value = node_attribute[node_name]

                nx.set_node_attributes(self, name='weight', values={node_name: value})
            except:
                pass

        for (node1, node2, link_name) in list(self.edges(keys=True)):
            try:
                value = link_attribute[link_name]

                if value < 0: # change the direction of the link and value
                    link_type = self[node1][node2][link_name]['type'] # 'type' should be the only other attribute on G.edge
                    self.remove_edge(node1, node2, link_name)
                    self.add_edge(node2, node1, link_name)
                    nx.set_edge_attributes(self, name='type', values={(node2, node1, link_name): link_type})
                    nx.set_edge_attributes(self, name='weight', values={(node2, node1, link_name): -value})
                else:
                    nx.set_edge_attributes(self, name='weight', values={(node1, node2, link_name): value})
            except:
                    pass
mnist_subgraphs.py 文件源码 项目:TextAsGraphClassification 作者: NightmareNyx 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test():
    mnist = input_data.read_data_sets("MINST_data", one_hot=False)
    train_data = mnist.train.images.astype(np.float32)
    fraction = 50
    train_labels = mnist.train._labels[:fraction]
    with open('sugbgraphs_labels.pickle', 'wb') as f:
        pickle.dump(train_labels, f)

    test_data = mnist.test.images.astype(np.float32)
    print(train_data.shape)
    patch_size = 4
    n_ids = range(patch_size * patch_size)
    A = np.ones((patch_size * patch_size, patch_size * patch_size))
    np.fill_diagonal(A, 0)
    cc = 0
    train = []

    bins = list(np.linspace(0.0, 1.0, 10))
    for sample in train_data[:fraction]:
        sample = sample.reshape((28, 28))
        sugbg = []
        patches = image.extract_patches_2d(sample, (patch_size, patch_size))
        cc += 1
        for p in patches:
            if np.sum(p) == 0:
                continue
            G1 = nx.from_numpy_matrix(A)
            dictionary = dict(zip(n_ids, np.digitize(p.flatten(), bins)))
            nx.set_node_attributes(G1, 'label', dictionary)
            sugbg.append(G1)
        train.append(sugbg)
        print(cc)

    with open('sugbgraphs_train.pickle', 'wb') as f:
        pickle.dump(train, f)

    del train
    test = []
    for sample in test_data[:5]:
        sample = sample.reshape((28, 28))
        sugbg = []
        patches = image.extract_patches_2d(sample, (patch_size, patch_size))
        for p in patches:
            if np.sum(p) == 0:
                continue

            G1 = nx.from_numpy_matrix(A)
            p = np.histogram(p.flatten(), bins=np.linspace(0.0, 1.0, 10))[0]
            dictionary = dict(zip(n_ids, p))
            nx.set_node_attributes(G1, 'label', dictionary)
            sugbg.append(G1)
        test.append(sugbg)
    with open('sugbgraphs_test.pickle', 'wb') as f:
        pickle.dump(sugbg, f)
graph_of_words.py 文件源码 项目:TextAsGraphClassification 作者: NightmareNyx 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def docs_to_networkx(dataset, cats, window_size=2, vocabulary_creation=True):
    ds = './datasets/%s/' % dataset
    Gs = []
    labels = []
    type_ = 2
    vocab_creation = vocabulary_creation
    words = []  # for vocabulary

    for doc in os.listdir(ds):
        if 'train.txt' in doc:
            type_ = 1

    if type_ == 1:
        if os.path.exists("ds/vocab.txt"):
            vocab_creation = False
        with open(ds + '/train.txt', 'r', encoding='iso-8859-1') as doc:
            dc = 1
            for line in doc:
                label = line[0]
                labels.append(label)
                terms = extract_terms_from_sentence(line[1:],
                                                    stopwords=stopwords.words('english'),
                                                    lemmatize=True,
                                                    stem=True,
                                                    only_N_J=True)
                if vocab_creation:
                    words.extend(terms)
                graph = terms_to_graph(terms, window_size)
                G = graph_to_networkx(graph, name=label + '_' + str(dc))
                # G = nx.convert_node_labels_to_integers(G, first_label=1, label_attribute='label')
                nx.set_node_attributes(G, 'label', dict(zip(G.nodes(), G.nodes())))
                Gs.append(G)
                dc += 1
    else:
        if os.path.exists("ds/vocab.txt"):
            vocab_creation = False
        for cat in cats.keys():
            for doc in os.listdir(ds + cat):
                terms = extract_terms_from_file(ds + cat + '/' + doc,
                                                stopwords=stopwords.words('english'),
                                                lemmatize=True,
                                                stem=True,
                                                only_N_J=True)
                if vocab_creation:
                    words.extend(terms)
                graph = terms_to_graph(terms, window_size)
                G = graph_to_networkx(graph, name=cat + doc.split('.')[0])
                # G = nx.convert_node_labels_to_integers(G, first_label=1, label_attribute='label')
                nx.set_node_attributes(G, name='label', values=dict(zip(G.nodes(), G.nodes())))
                Gs.append(G)
                labels.append(cats[cat])

    if vocab_creation:
        vocab = dict(Counter(words))
        create_vocabulary_file(fname, vocab)

    return Gs, labels


# needs fix or discard
fam_to_graph.py 文件源码 项目:pangenome_graphs 作者: aswarren 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def processFeatures(self):
        sys.stderr.write("parsing features and constructing kmer graph\n")  
        kmer_q=deque()
        prev_feature=None
        #loop through figfams to create kmers
        repeat_num=1
        update_repeats=[]
        for feature in self.feature_parser.parse():
            if prev_feature and prev_feature.group_id == feature.group_id and prev_feature.contig_id == feature.contig_id:
                repeat_num+=1
                update_repeats.append(prev_feature)
                if self.eat_repeats:
                    continue
            else:
                if repeat_num >1:
                    update_repeats.append(prev_feature)
                    for to_up in update_repeats:
                        to_up.repeat_num =repeat_num
                repeat_num=1
                update_repeats=[]
            feature.feature_id= len(self.feature_index)
            self.feature_index.append(feature)
            if prev_feature == None or prev_feature.genome_id != feature.genome_id:
                self.trackDiversity(feature.feature_id, self.all_diversity)
            if feature.genome_id not in self.replicon_map:
                self.replicon_map[feature.genome_id]=set()
            else:
                self.replicon_map[feature.genome_id].add(feature.contig_id)
            if(prev_feature and prev_feature.contig_id != feature.contig_id):
                kmer_q=deque()#clear kmer stack because switching replicons
                self.prev_node=None
                self.prev_indices=[]
            elif prev_feature and prev_feature.contig_id == feature.contig_id:
                if prev_feature.start > feature.start:
                    assert InputError
            #depending on the context populate the context bin with appropriate ids to detect duplicates
            if self.context:
                if(prev_feature and prev_feature.getContextValue(self.context) != feature.getContextValue(self.context)):
                    self.context_bin.clear()
            kmer_q.append(feature)#append the feature to the queue
            if(len(kmer_q)>self.ksize):
                kmer_q.popleft()
                self.addRFNode(kmer_q)
            elif(len(kmer_q)== self.ksize):
                self.addRFNode(kmer_q)#right now only passing in the last figfams information
            else:#kmer size is less than ksize
                kmer=None
            prev_feature=feature
        if self.debug: nx.set_node_attributes(self.rf_graph, "visit", "")


问题


面经


文章

微信
公众号

扫码关注公众号