Example #1
0
def main():
    print "updating_rule module is the main code."

    edge_file = '../data/inputs/edges-init.dat'
    
    #############################
    ## Read in anterior nodes
    #############################
    node_file = '../data/inputs/ant-nodes-init.dat'
    desired_node_file = '../data/inputs/ant-nodes-final.dat'
    
    G = inet.read_network_from_file(edge_file, node_file)

    nodes_list = G.nodes()
    node_dict = nx.get_node_attributes(G,'state')
    init_dID = time_evol.binary_to_decimal(nodes_list, node_dict)

    #############################
    ## Read in posterior nodes
    #############################
    post_node_file = '../data/inputs/post-nodes-init.dat'
    post_desired_node_file = '../data/inputs/post-nodes-final.dat'

    G_post = inet.read_network_from_file(edge_file, post_node_file)

    post_nodes_list = G_post.nodes()
    post_node_dict = nx.get_node_attributes(G_post,'state')
    post_init_dID = time_evol.binary_to_decimal(post_nodes_list, post_node_dict)
    
    #############################
    # print G.nodes()
    # print G.edges()

    # prev_state = nx.get_node_attributes(G,'state') # dict of key=node, value=state of network G

    # print "network state @ prev step", OrderedDict(sorted(prev_state.items(), key=lambda t: t[0]))
    
    # curr_state = boolean_updating(G)

    # print "network state @ curr step", OrderedDict(sorted(curr_state.items(), key=lambda t: t[0]))

    # print prev_state
    # print curr_state

    ###### calc steady state probabilities #####################################################

    # init_dID,desired_dID = 0, 43
    init_dID,desired_dID = 20, 980
    attractor_dict, prob_desired_dID = full_steady_state_calc(G,nodes_list,init_dID,desired_dID)
    print attractor_dict
    print prob_desired_dID

    exit()

    #############################################################################################


    ## Create deterministic transition graph
    state_trans_dict = create_state_transitions_dict(G)
    G_state_trans = create_graph_from_transitions(state_trans_dict) # stochastic transition graph

    ## Calculate in and out degrees of transition graph
    out_deg_dict = G_state_trans.out_degree(G_state_trans.nodes())
    sorted_out_deg_dict = sorted(out_deg_dict.items(), key=operator.itemgetter(1))

    in_deg_dict = G_state_trans.in_degree(G_state_trans.nodes())
    sorted_in_deg_dict = sorted(in_deg_dict.items(), key=operator.itemgetter(1))

    largest = max(nx.strongly_connected_components(G_state_trans), key=len)
    # print len(largest) # this shows that graph of stochastic state transitions is strongly connected
    # attractors = time_evol.find_attractor_old(G_state_trans)

    create_transition_matrix(state_trans_dict)

    deterministic_transition_graph = time_evol.net_state_transition(G, nodes_list) # deterministic transition graph
    attractors = time_evol.find_attractor_old(deterministic_transition_graph) # this must only print out cycles for a subset of the graph...

    #############################
    ## Find control kernals
    #############################

    attractor_ID = 980

    # print find_len_1_control_kernals(deterministic_transition_graph, nodes_list, attractor_ID)

    # print find_len_2_control_kernals(deterministic_transition_graph, nodes_list, attractor_ID)

    print find_len_2_control_kernals_fo_real(deterministic_transition_graph, nodes_list, attractor_ID)

    exit()

    #############################
    ## Desired node states
    ##      Anterior
    G_desired = inet.read_network_from_file(edge_file, desired_node_file)
    desired_nodes_list = G_desired.nodes()
    desired_node_dict = nx.get_node_attributes(G_desired,'state')
    desired_dID = time_evol.binary_to_decimal(desired_nodes_list,desired_node_dict)
    print "desired_node_dict:", desired_node_dict
    print "desired dID:", desired_dID

    ##      Posterior
    G_desired_post = inet.read_network_from_file(edge_file, post_desired_node_file)
    post_desired_nodes_list = G_desired_post.nodes()
    post_desired_node_dict = nx.get_node_attributes(G_desired_post,'state')
    post_desired_dID = time_evol.binary_to_decimal(post_desired_nodes_list,post_desired_node_dict)
    print "post_desired_node_dict:", post_desired_node_dict
    print "desired dID:", post_desired_dID

    ## Check if path between desired init state and final state
    print nx.has_path(deterministic_transition_graph,init_dID,desired_dID)

    ## Print binary version of attractors
    print "attractors:"
    print attractors

    ## Network 980 is the steady state network, let's see if this is correct based on rules
    # steady_state_network = 
    print "end_ant:", desired_node_dict
    print "beg_ant:", node_dict

    print "end_ant_dID:", desired_dID
    print "beg_ant_dID:", init_dID

    print "end_post:", post_desired_node_dict
    print "beg_post:", post_node_dict

    print "end_post_dID:", post_desired_dID
    print "beg_post_dID:", post_init_dID    




    # print attractors

    # print G_state_trans.edges()
    # for line in nx.generate_edgelist(G_state_trans, data=False):
    #     print line
    # nx.draw(G_state_trans)
    # nx.write_graphml(G_state_trans,'g_state_trans.xml')
    # plt.show()

    # for graph in list(nx.weakly_connected_component_subgraphs(deterministic_transition_graph)):
    #     if 43 in graph.nodes():
    #         print [time_evol.decimal_to_binary(nodes_list, dID) for dID in graph.nodes()]

    # print "  0:", time_evol.decimal_to_binary(nodes_list, 0)
    # print "683:", time_evol.decimal_to_binary(nodes_list, 683)


    # nx.draw(deterministic_transition_graph)
    # nx.write_graphml(deterministic_transition_graph,'deterministic_transition_graph.xml')
    # plt.show()





    exit()
Example #2
0
def create_state_transitions_dict(G):
    """
    output a dictionary of key=network_ID, value=list of possible next network_IDs
    each of the next possible network_IDs is exactly 1 value different than the previous network ID
    this is working correctly- trust past harrison 4/13/16
    """
    state_trans_dict = {} # key=network_ID, value=list of next network IDs

    n_nodes = len(G.nodes())
    n_init_networks = time_evol.n_states**n_nodes

    for prev_network_ID in range(0, n_init_networks):

        nodes_list = G.nodes()

        prev_network_bID = time_evol.decimal_to_binary(nodes_list, prev_network_ID) #same as 'prev_state' within boolean_updating

        curr_network_bID = boolean_updating(G, prev_state=prev_network_bID)
        
        # get list of nodes that change when applying update rule
        altered_nodes = []
        for node in curr_network_bID:

            if prev_network_bID[node] != curr_network_bID[node]:
                altered_nodes.append(node)

        # print prev_network_ID, altered_nodes

        # look through altered nodes, and make list of all networks with nodes being altered 1 at a time
        state_trans_dict[prev_network_ID] = []
        for node in altered_nodes:
            
            possible_bID = copy.deepcopy(prev_network_bID)
            # print possible_bID #why is this not changing???

            not_flipped = True
            
            # print possible_bID[node]
            # if node was updated, flip it
            if prev_network_bID[node] == 0:
                possible_bID[node] = 1
                not_flipped = False
                # print "0, flipping"
            
            # print possible_bID[node]
             # if node was updated, flip it
            elif (prev_network_bID[node] == 1) and (not_flipped == True):
                possible_bID[node] = 0
                # print "1, flipping"

            # print possible_bID[node]
            else: 
                raise ValueError("Node %s of prev_state must be 0 or 1. Something bad happened."%node)

            # print possible_bID
            # nodes_list = [k for k in possible_bID]

            network_ID = time_evol.binary_to_decimal(nodes_list,possible_bID) #get network_ID of this possible next state

            state_trans_dict[prev_network_ID].append(network_ID)

        # if no nodes are altered, you're in a fixed stable state-- make state_trans_dict[stable_ID]=stable_ID
        if len(altered_nodes) == 0:
            state_trans_dict[prev_network_ID].append(prev_network_ID)
            # print 'prev network ID:',prev_network_ID

    return state_trans_dict