def test6(): # sanity check edges of starting network network_spec = open("networkspec_with_selfrep.txt").read() starting_graph = gt.getGraphFromNetworkSpec(network_spec) try: ns.sanity_check_edges(network_spec, starting_graph) raise RuntimeError("Shouldn't pass this test.") except ValueError as v: assert ( str(v) == "Seed network has a self-repressing edge. Not currently supported by DSGRN." ) network_spec = open("networkspec_with_multiedges.txt").read() starting_graph = gt.getGraphFromNetworkSpec(network_spec) try: ns.sanity_check_edges(network_spec, starting_graph) raise RuntimeError("Shouldn't pass this test.") except ValueError as v: assert ( str(v) == "Seed network has a multiedge. Not currently supported by DSGRN.") network_spec = open("networkspec_with_multiedges2.txt").read() starting_graph = gt.getGraphFromNetworkSpec(network_spec) try: ns.sanity_check_edges(network_spec, starting_graph) raise RuntimeError("Shouldn't pass this test.") except ValueError as v: assert ( str(v) == "Seed network has a multiedge. Not currently supported by DSGRN.")
def test(): netspec = "A : (A + B) : E\nB : (B)(~A) : E\nC : (A) : E" assert(netspec == gt.createEssentialNetworkSpecFromGraph(gt.getGraphFromNetworkSpec(netspec))) assert(netspec == gt.nxgraph2netspec(gt.netspec2nxgraph(netspec))) netspec = "Z : (~x) : E\nx : (y)(~Z) : E\ny : (y) : E" assert(netspec == gt.createEssentialNetworkSpecFromGraph(gt.getGraphFromNetworkSpec(netspec))) assert(netspec == gt.nxgraph2netspec(gt.netspec2nxgraph(netspec)))
def symmetric_difference_graph_distance(netspec1, netspec2, normalized=False): ''' Find the symmetric difference graph distance measure for two DSGRN network specifications. This distance assumes that node names are unique identifiers for nodes. In other words, two nodes are counted as having a distance of zero if and only if they have the same node name. This function does NOT consider isomorphisms to be distance zero unless the node names are the same. Note that if a node is missing, then the distance counts the missing node AND all the inedges and outedges. :param netspec1: DSGRN network specification :param netspec2: DSGRN network specification :param normalized: True or False, whether or not to normalize by the size of the graphs :return: The sum of the difference between the node sets and edge sets for the two networks (the symmetric difference). ''' g1 = gt.getGraphFromNetworkSpec(netspec1) g2 = gt.getGraphFromNetworkSpec(netspec2) nodes1 = set([g1.vertex_label(v) for v in g1.vertices()]) nodes2 = set([g2.vertex_label(v) for v in g2.vertices()]) node_diff = nodes1.symmetric_difference(nodes2) edges1 = transform_edges_from_index_to_label(g1) edges2 = transform_edges_from_index_to_label(g2) edge_diff = edges1.symmetric_difference(edges2) diff = len(node_diff) + len(edge_diff) if normalized: denom = len(nodes1) + len(nodes2) + len(edges1) + len(edges2) return diff / denom else: return diff
def test3(): original = open("networkspec_X1X2X3.txt").read() original_graph = gt.getGraphFromNetworkSpec(original) networks = run("params_X1X2X3_C.json") assert (len(networks) == 9) conn, sconn, ff, numnodes, numedges, mininedges, maxinedges, minoutedges, maxoutedges, params = check_size( networks) assert (numedges.count(5) == 1) assert (all([e <= 5 for e in numedges])) assert (all([n <= 3 for n in numnodes])) assert (all([p <= 10000 for p in params])) for netspec in networks: graph = gt.getGraphFromNetworkSpec(netspec) assert (is_subgraph(original_graph, graph))
def test1(): original = open("networkspec_X1X2X3.txt").read() subgraph = gt.getGraphFromNetworkSpec(original) networks = run("params_X1X2X3_A.json") assert (len(networks) == 10) conn, sconn, ff, numnodes, numedges, mininedges, maxinedges, minoutedges, maxoutedges, params, issubgraph = check_size( networks, subgraph) assert (all([s[0] for s in conn])) assert (numedges.count(5) == 1) assert (issubgraph) assert (all([e >= 5 for e in numedges])) assert (all([n >= 3 for n in numnodes])) assert (all([p <= 100000 for p in params]))
def setup(params, network_spec): # make starting graph, make sure network_spec is essential, and add network_spec to list of networks if not network_spec or network_spec == "\n": starting_graph = graphtranslation.Graph() else: starting_graph = graphtranslation.getGraphFromNetworkSpec(network_spec) # set defaults params = set_defaults(params, starting_graph) # remove negative self-regulation from edgelist if params["edgelist"]: params["edgelist"] = filter_edgelist(params["edgelist"]) # make sure probabilities are normalized and take the cumsum params["probabilities"] = make_probability_vector(params["probabilities"]) # make range_operations end-point inclusive params["range_operations"] = [ params["range_operations"][0], params["range_operations"][1] + 1 ] random.seed(params["random_seed"]) return params, starting_graph
def test7(): # check node removal original = open("networkspec_X1X2X3.txt").read() original_graph = gt.getGraphFromNetworkSpec(original) networks = run("params_X1X2X3_E.json") # Note: 'X2 : \nX3 : (X2)\n' will not be produced using Shaun's repository (0 out-edges not allowed) assert (set(networks).issubset( set([ 'X1 : (X1)(~X3) : E\nX2 : (X1) : E\nX3 : (X1 + X2) : E', 'X1 : (X1)(~X3) : E\nX3 : (X1) : E', 'X1 : (X1) : E\nX2 : (X1) : E', 'X2 : : E\nX3 : (X2) : E' ]))) for _ in range(10): graph, numedges = ns.removeNodes(original_graph.clone(), 1) spec = gt.createEssentialNetworkSpecFromGraph(graph) if spec == 'X1 : (X1)(~X3) : E\nX3 : (X1) : E': assert (numedges == 2) elif spec == 'X1 : (X1) : E\nX2 : (X1) : E': assert (numedges == 3) elif 'X2 : : E\nX3 : (X2) : E': assert (numedges == 4)
def check_size(networks, original_graph=None): sconn = [] conn = [] ff = [] numedges = [] numnodes = [] maxinedges = 0 mininedges = 10 maxoutedges = 0 minoutedges = 10 params = [] for netspec in networks: graph = gt.getGraphFromNetworkSpec(netspec) conn.append(is_connected(graph)) sconn.append(is_strongly_connected(graph)) ff.append(is_feed_forward(graph)) numedges.append(len(graph.edges())) numnodes.append(len(graph.vertices())) maxinedges = max([ max([len(graph.inedges(v)) for v in graph.vertices()]), maxinedges ]) mininedges = min([ min([len(graph.inedges(v)) for v in graph.vertices()]), mininedges ]) maxoutedges = max([ max([len(graph.adjacencies(v)) for v in graph.vertices()]), maxoutedges ]) minoutedges = min([ min([len(graph.adjacencies(v)) for v in graph.vertices()]), minoutedges ]) params.append(DSGRN.ParameterGraph(DSGRN.Network(netspec)).size()) if original_graph: return conn, sconn, ff, numnodes, numedges, mininedges, maxinedges, minoutedges, maxoutedges, params, is_subgraph( graph, original_graph) else: return conn, sconn, ff, numnodes, numedges, mininedges, maxinedges, minoutedges, maxoutedges, params