예제 #1
0
    def to_networkxs(self):
        """Returns a networkx graph that contains the stored data.

        The node and edge features are placed in the networkx graph's nodes and edges attribute dictionaries
        under the "features" key. Edges are added in the order they are stored in the `self.edges` tensor.
        The global features are placed under the key "features" of the the networkx graph's property.

        :returns: An instance of `networkx.OrderedMultiDiGraph`.

        """
        output_graphs_nx = []
        for g in self.batch_iterator():
            graph_nx = nx.OrderedMultiDiGraph()
            graph_nx.graph[GRAPH_NX_FEATURES_KEY] = g.globals.numpy()[0]

            nodes_list = _unstack(g.nodes)
            for i, x in enumerate(nodes_list):
                graph_nx.add_node(i, **{GRAPH_NX_FEATURES_KEY: x})

            edges_features = [{
                "index": i,
                GRAPH_NX_FEATURES_KEY: x
            } for i, x in enumerate(_unstack(g.edges))]
            edges_data = zip(g.senders.numpy(), g.receivers.numpy(),
                             edges_features)
            graph_nx.add_edges_from(edges_data)

            output_graphs_nx.append(graph_nx)
        return output_graphs_nx
예제 #2
0
 def __init__(self, region_id, label=''):
     self.region_id = region_id
     # self.node_data = node_data
     # self.edge_data = edge_data
     self.label = Label(label)
     self.graph = nx.OrderedMultiDiGraph()
     self.path_node_ids = None
예제 #3
0
def generate_graph(raw_graph, edge_permutations, target_graph):
    num_nodes = len(raw_graph[1])
    num_edges = len(raw_graph[2])
    graph_nx = nx.OrderedMultiDiGraph()

    # Globals
    graph_nx.graph['features'] = raw_graph[0]

    # Nodes
    for node_id in range(num_nodes):
        graph_nx.add_node(raw_graph[1][node_id][0],
                          features=raw_graph[1][node_id][1])

    # Edges
    for edge_id in range(num_edges):
        graph_nx.add_edge(raw_graph[2][edge_id][0],
                          raw_graph[2][edge_id][1],
                          features=raw_graph[2][edge_id][2])
        if target_graph:
            graph_nx.add_edge(raw_graph[2][edge_id][1],
                              raw_graph[2][edge_id][0],
                              features=raw_graph[2][edge_id][2])
        else:
            graph_nx.add_edge(
                raw_graph[2][edge_id][1],
                raw_graph[2][edge_id][0],
                features=raw_graph[2][edge_id][2][edge_permutations])

    return graph_nx
예제 #4
0
def test_global_access_with_key():
    g = nx.OrderedMultiDiGraph()
    g.data = {}
    assert g.get_global("data") == {}
    g.get_global("data")["k"] = 1
    g.get_global("data").update({"u": 2})
    assert g.get_global("data") == {"k": 1, "u": 2}
예제 #5
0
    def test_from_networkx_no_edge(self, keys):
        kwargs = {"feature_key": "features", "global_attr_key": "data"}
        feature_key, global_attr_key = keys
        if feature_key is not None:
            kwargs["feature_key"] = feature_key
        else:
            del kwargs["feature_key"]
        if global_attr_key is not None:
            kwargs["global_attr_key"] = global_attr_key
        else:
            del kwargs["global_attr_key"]

        fkey = kwargs.get("feature_key", "features")
        gkey = kwargs.get("global_attr_key", None)

        g = nx.OrderedMultiDiGraph()
        g.add_node("node1", **{fkey: np.random.randn(5)})
        g.add_node("node2", **{fkey: np.random.randn(5)})
        g.ordered_edges = []
        # g.add_edge('node1', 'node2', **{fkey: torch.randn(4)})
        g.set_global({fkey: np.random.randn(3)}, gkey)

        data = GraphData.from_networkx(g, **kwargs)

        Comparator.data_to_nx(data, g, fkey, gkey)
예제 #6
0
파일: graph.py 프로젝트: zaf05/mayo
 def __init__(self, model):
     super().__init__()
     self.nx_graph = nx.OrderedMultiDiGraph()
     self._input_names = inputs = model.get('inputs', 'input')
     self._output_names = outputs = model.get('outputs', 'output')
     self._add_module(inputs, outputs, model['name'], model, [])
     self._optimize()
     self._validate()
예제 #7
0
 def __init__(self, nodes, edges, regions, dimensions):
     self.nodes = nodes
     self.edges = edges
     self.regions = regions
     self.dimensions = dimensions
     self.node_counts = {node_name: 0 for node_name in self.nodes}
     self.graph = nx.OrderedMultiDiGraph()
     self.graph_contains_loops = False
예제 #8
0
 def as_networkx_graph(self):
     import networkx as nx
     graph = nx.OrderedMultiDiGraph()
     # Not sure if we could just insert edges and have the nodes be inserted
     # automatically, not sure if order is properly preserved then
     graph.add_nodes_from(self.graph.keys())
     graph.add_edges_from(((u, v, { 'action': a }) \
                           for u, edges in self.graph.items() \
                           for a, v in edges.items()))
     return graph
예제 #9
0
    def deal_image(self, image):
        graph_nx = nx.OrderedMultiDiGraph()

        # Globals.
        graph_nx.graph["features"] = np.random.randn(36)

        # Nodes.
        for i in range(self.block_height):
            for j in range(self.block_width):
                graph_nx.add_node(
                    i * self.block_width + j,
                    features=image[i * self.stride:(i + 1) * self.stride,
                                   j * self.stride:(j + 1) *
                                   self.stride].flatten())

        # Edges.
        for i in range(self.block_height):
            for j in range(self.block_width):
                if i - 1 >= 0 and j >= 0:
                    graph_nx.add_edge(i * self.block_width + j,
                                      (i - 1) * self.block_width + j,
                                      features=np.random.randn(10))
                    graph_nx.add_edge((i - 1) * self.block_width + j,
                                      i * self.block_width + j,
                                      features=np.random.randn(10))

                if i + 1 >= 0 and j >= 0 and i + 1 <= self.block_height - 1:
                    graph_nx.add_edge(i * self.block_width + j,
                                      (i + 1) * self.block_width + j,
                                      features=np.random.randn(10))
                    graph_nx.add_edge((i + 1) * self.block_width + j,
                                      i * self.block_width + j,
                                      features=np.random.randn(10))

                if i >= 0 and j - 1 >= 0:
                    graph_nx.add_edge(i * self.block_width + j,
                                      i * self.block_width + j - 1,
                                      features=np.random.randn(10))
                    graph_nx.add_edge(i * self.block_width + j - 1,
                                      i * self.block_width + j,
                                      features=np.random.randn(10))

                if i >= 0 and j + 1 >= 0 and j + 1 <= self.block_width - 1:
                    graph_nx.add_edge(i * self.block_width + j,
                                      i * self.block_width + j + 1,
                                      features=np.random.randn(10))
                    graph_nx.add_edge(i * self.block_width + j + 1,
                                      i * self.block_width + j,
                                      features=np.random.randn(10))

        return graph_nx
def generate_example_nn(positions, properties, k=1, plot=False):
    """
    Generate a k-nn graph from positions.

    Args:
        positions: [num_points, 3] positions used for graph constrution.
        properties: [num_points, F0,...,Fd] each node will have these properties of shape [F0,...,Fd]
        k: int, k nearest neighbours are connected.
        plot: whether to plot graph.

    Returns: GraphTuple
    """
    graph = nx.OrderedMultiDiGraph()

    kdtree = cKDTree(positions)
    dist, idx = kdtree.query(positions, k=k + 1)
    receivers = idx[:, 1:]  #N,k
    senders = np.arange(positions.shape[0])  #N
    senders = np.tile(senders[:, None], [1, k])  #N,k
    receivers = receivers.flatten()
    senders = senders.flatten()

    n_nodes = positions.shape[0]

    pos = dict()  # for plotting node positions.
    edgelist = []

    for node, feature, position in zip(np.arange(n_nodes), properties,
                                       positions):
        graph.add_node(node, features=feature)
        pos[node] = position[:2]

    # edges = np.stack([senders, receivers], axis=-1) + sibling_node_offset
    for u, v in zip(senders, receivers):
        graph.add_edge(u, v, features=None)
        graph.add_edge(v, u, features=None)
        edgelist.append((u, v))
        edgelist.append((v, u))

    graph.graph['features'] = None

    # plotting

    if plot:
        fig, ax = plt.subplots(1, 1, figsize=(12, 12))
        draw(graph, ax=ax, pos=pos, node_color='green', edge_color='red')
        plt.show()

    return networkxs_to_graphs_tuple(
        [graph], node_shape_hint=[positions.shape[1] + properties.shape[1]])
예제 #11
0
def _single_data_dict_to_networkx(data_dict):
  graph_nx = nx.OrderedMultiDiGraph()
  if data_dict["nodes"].size > 0:
    for i, x in enumerate(data_dict["nodes"]):
      graph_nx.add_node(i, features=x)

  if data_dict["edges"].size > 0:
    edge_data = zip(data_dict["senders"], data_dict["receivers"], [{
        "features": x
    } for x in data_dict["edges"]])
    graph_nx.add_edges_from(edge_data)
  graph_nx.graph["features"] = data_dict["globals"]

  return graph_nx
예제 #12
0
def generate_singulation_graph(config, n_manipulable_objects):
    gripper_as_global = config.gripper_as_global

    #graph_nx = nx.DiGraph()
    graph_nx = nx.OrderedMultiDiGraph()

    if not gripper_as_global:
        offset = 1
    else:
        offset = 0
    """ adding features to nodes """
    # 120*160*4(seg+rgb)
    #graph_nx.add_node(0, type_name='container')
    #graph_nx.add_node(0, features=np.zeros(shape=(config.node_output_size,), dtype=np.float32))

    if not gripper_as_global:
        """ if gripper should be a global attribute, include its data in global features """
        # 120*160*4(seg+rgb)+3(pos)
        graph_nx.add_node(0, type_name='gripper')
        graph_nx.add_node(0,
                          features=np.zeros(shape=(config.node_output_size, ),
                                            dtype=np.float32))

    for i in range(offset, n_manipulable_objects):
        ''' no multiple features -> flatten and concatenate everything '''
        # 120*160*4(seg+rgb)+3(vel)+3(pos)
        object_str = str(i)
        graph_nx.add_node(i, type_name='manipulable_object_' + object_str)
        graph_nx.add_node(i,
                          features=np.zeros(shape=(config.node_output_size, ),
                                            dtype=np.float32))
    """ adding edges and features, the container does not have edges due to missing objpos """
    edge_tuples = [(a, b)
                   for a, b in product(range(0, graph_nx.number_of_nodes()),
                                       range(0, graph_nx.number_of_nodes()))
                   if a != b]

    for edge in edge_tuples:
        graph_nx.add_edge(*edge,
                          key=0,
                          features=np.zeros(shape=(config.edge_output_size, ),
                                            dtype=np.float32))
    """ adding global features """
    # 120*160*3(rgb)+120*160(seg)+120*160*3(depth)+1(timestep)+1(gravity)
    graph_nx.graph["features"] = np.zeros(shape=(config.global_output_size, ),
                                          dtype=np.float32)

    return graph_nx
예제 #13
0
  def test_networkxs_to_data_dict_raises_node_key_error(self):
    """If the nodes have keys not consistent with the order they were added."""
    graph_nx = nx.OrderedMultiDiGraph()
    graph_nx.add_node(0, features=None)
    graph_nx.add_node(1, features=None)
    graph_nx.add_node(3, features=None)

    with self.assertRaisesRegexp(
        ValueError, "found node with index 2 and key 3"):
      utils_np.networkx_to_data_dict(graph_nx)

    # Check that it is still raised even if there is a node with each key,
    # and only the order is wrong.
    graph_nx.add_node(2, features=None)
    with self.assertRaisesRegexp(
        ValueError, "found node with index 2 and key 3"):
      utils_np.networkx_to_data_dict(graph_nx)
예제 #14
0
def make_digraph(s,t,w=None):
    '''
    Makes a digraph with potential weights w
    It takes in two required arguments:
    s: A list indicating start nodes of all edges
    t: A list indicating end nodes of all edges
    '''
    # Set up a NetworkX graph
    G = nx.OrderedMultiDiGraph()

    # If weights are not specified, make the graph without weights
    if w is None:
        edge_list = list(zip(s,t))
        G.add_edges_from(edge_list)
    # Else make weighted edges
    else:
        edge_list = list(zip(s,t,w))
        G.add_weighted_edges_from(edge_list)
    return G
예제 #15
0
 def __init__(self, term: Term):
     # Our DAG is an ordered graph because the edges are directional
     # It is considered a MultiGraph due to our structured sharing.
     # It is a digraph since the edges are directed
     # One of the consequences is that finding the parent of a node is ambiguous
     self.dag = nx.OrderedMultiDiGraph()
     self.term = term
     self.edge_labels: Dict[Tuple[Term, Term], str] = {}
     self.node_labels: Dict[Term, Union[Variable, Function]] = {}
     self.dag.add_node(term)
     # If the term we're starting out from is just a variable or constant,
     # then we just make a single node graph.
     # Otherwise, we'll need to start recursing down the arguments and add edges to each one...
     if isinstance(term, FuncTerm):
         self.node_labels[term] = term.function
         # Go through each of the arguments and add it to the graph
         for index, t in enumerate(term.arguments):
             self._appendTermDAG(term, t, edge_label=str(index))
     else:
         self.node_labels[term] = term
 def test_networkxs_to_graphs_tuple_with_none_fields(self):
     graph_nx = nx.OrderedMultiDiGraph()
     data_dict = utils_np.networkx_to_data_dict(graph_nx,
                                                node_shape_hint=None,
                                                edge_shape_hint=None)
     self.assertEqual(None, data_dict["edges"])
     self.assertEqual(None, data_dict["globals"])
     self.assertEqual(None, data_dict["nodes"])
     graph_nx.add_node(0, features=None)
     data_dict = utils_np.networkx_to_data_dict(graph_nx,
                                                node_shape_hint=1,
                                                edge_shape_hint=None)
     self.assertEqual(None, data_dict["nodes"])
     graph_nx.add_edge(0, 0, features=None)
     data_dict = utils_np.networkx_to_data_dict(graph_nx,
                                                node_shape_hint=[1],
                                                edge_shape_hint=[1])
     self.assertEqual(None, data_dict["edges"])
     graph_nx.graph["features"] = None
     utils_np.networkx_to_data_dict(graph_nx)
     self.assertEqual(None, data_dict["globals"])
예제 #17
0
def draw_graph(edgeList, user_id, file_name):
    lis = []
    for i in edgeList:
        reshaped_text = arabic_reshaper.reshape(i[0])
        artext = get_display(reshaped_text)
        reshaped_text1 = arabic_reshaper.reshape(i[1])
        artext1 = get_display(reshaped_text1)
        lis.append((artext, artext1))
    G = nx.OrderedMultiDiGraph()
    G.add_edges_from(lis)
    pos = nx.spring_layout(G, k=0.99, iterations=50)
    nx.draw_networkx_nodes(G, pos, node_color='#AED6F1', node_size=2500)
    nx.draw_networkx_edges(G, pos, edgelist=G.edges(), edge_color='#95A5A6', arrows=True)
    nx.draw_networkx_labels(G, pos, font_size=20, font_family='Times New Roman')
    graph_path = os.path.join(os.path.join(BASE_DIR, 'static/media'), 'graph')
    user_path = os.path.join(graph_path, str(user_id))
    if not os.path.isdir(graph_path):
        os.mkdir(graph_path)
    if not os.path.isdir(user_path):
        os.mkdir(user_path)
    path = os.path.join(user_path, file_name)
    plt.tight_layout()
    plt.savefig(path+'.png', format="PNG")
    plt.show()
예제 #18
0
    def show(self):

        G = nx.OrderedMultiDiGraph()
        G.add_nodes_from(x.name for x in self.list_node)

        labels = {}

        for node in self.list_node:
            for edge in node.edges:
                # Fazer o graico ser aceito pra edges do mesmo ponto
                labels[(node.name, edge.node.name)] = edge.name
                G.add_edge(node.name, edge.node.name, length=5)

        print("Nodes do Grafo Inicial: ")
        print(self.node_init.name)
        print("Nodes do Grafo Finais: ")
        [print(node) for node in self.node_finals]
        print("Nodes do Grafo: ")
        print(G.nodes())
        print("Edges do Grafo: ")
        print(G.edges())
        print("Labels do Grafo: ")
        print(labels)
        # labels={}

        pos = nx.layout.spring_layout(G)

        plt.figure(figsize=(20, 20))

        nx.draw(G, pos, with_labels=True, edge_color='black', width=2,
                linewidths=1, node_size=250, node_color='green', alpha=0.9,)

        nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)

        plt.savefig("../graph.png")  # save as png
        plt.show()  # display
예제 #19
0
def data_dict_to_networkx(data_dict):
    """Returns a networkx graph that contains the stored data.

    Depending on the content of `data_dict`, the returned `networkx` instance has
    the following properties:

    - The nodes feature are placed in the nodes attribute dictionary under the
      "features" key. If the `NODES` fields is `None`, a `None` value is placed
      here;

    - If the `RECEIVERS` field is `None`, no edges are added to the graph.
      Otherwise, edges are added with the order in which they appeared in
      `data_dict` stored in the "index" field of their attributes dictionary;

    - The edges features are placed in the edges attribute dictionary under the
      "features" key. If the `EDGES` field is `None`, a `None` value is placed;

    - The global feature are placed under the key "features" of the graph
      property of the returned instance. If the `GLOBALS` field is `None`, a
      `None` global property is created.

    Args:
      data_dict: A graph `dict` of Numpy data.

    Returns:
      The `networkx.OrderedMultiDiGraph`.

    Raises:
      ValueError: If the `NODES` field of `data_dict` contains `None`, and
        `data_dict` does not have a `N_NODE` field.
    """
    graph_nx = nx.OrderedMultiDiGraph()
    data_dict = _populate_number_fields(data_dict)
    graph_nx.graph[GRAPH_NX_FEATURES_KEY] = data_dict[GLOBALS]

    if data_dict[NODES] is not None:
        if data_dict[NODES].shape[0] > 0:
            nodes_list = _unstack(data_dict[NODES])
            for i, x in enumerate(nodes_list):
                graph_nx.add_node(i, **{GRAPH_NX_FEATURES_KEY: x})
    elif data_dict[N_NODE] is not None:
        for i in range(data_dict[N_NODE]):
            graph_nx.add_node(i, **{GRAPH_NX_FEATURES_KEY: None})
    else:
        raise ValueError(
            "Cannot create a graph with unspecified number of nodes")

    if data_dict[EDGES] is not None and data_dict[EDGES].shape[0] > 0:
        edges_features = [
            {  # pylint: disable=g-complex-comprehension
                "index": i,
                GRAPH_NX_FEATURES_KEY: x
            } for i, x in enumerate(_unstack(data_dict[EDGES]))
        ]
        edges_data = zip(data_dict[SENDERS], data_dict[RECEIVERS],
                         edges_features)
        graph_nx.add_edges_from(edges_data)
    elif data_dict[RECEIVERS] is not None and data_dict[RECEIVERS].shape[0] > 0:
        edges_features = [
            {  # pylint: disable=g-complex-comprehension
                "index": i,
                GRAPH_NX_FEATURES_KEY: None
            } for i in range(data_dict[RECEIVERS].shape[0])
        ]
        edges_data = zip(data_dict[SENDERS], data_dict[RECEIVERS],
                         edges_features)
        graph_nx.add_edges_from(edges_data)

    return graph_nx
예제 #20
0
def graphs_from_nodes_edges_and_globals(graph_nodes, graph_edges,
                                        graph_globals, only_edge, target_graph,
                                        graph_nx_format):
    num_nodes = graph_nodes.shape[0]
    num_edges = graph_edges.shape[0]

    graph_nx = nx.OrderedMultiDiGraph()
    graph_no_nx = [0, [], []]

    # Exclude columns from features - either because they are no features or
    # because they are categorical
    exclude_cols = [
        'molecule_name', 'atom_index', 'atom_index_0', 'atom_index_1', 'atom',
        'atom_0', 'atom_1', 'type', 'bond_type', 'interm_0_atom',
        'interm_1_atom'
    ]

    # Target columns (they may not all be present)
    main_target_col = 'scalar_coupling_constant'
    target_cols = [main_target_col, 'fc', 'sd', 'pso', 'dso']
    target_cols = list(set(target_cols).intersection(set(graph_edges.columns)))
    # Make sure that scalar_coupling_constant is the first in the list
    if target_cols:
        scc_id = np.where(np.array(target_cols) == main_target_col)[0][0]
        if scc_id > 0:
            target_cols[scc_id] = target_cols[0]
            target_cols[0] = main_target_col

    # Globals
    if target_graph:
        global_features = np.array([])
    else:
        global_numeric = graph_globals.drop(exclude_cols, errors='ignore')
        global_features = global_numeric.values

    global_features = global_features.astype(np.float)
    if graph_nx_format:
        graph_nx.graph['features'] = global_features
    else:
        graph_no_nx[0] = global_features

    # Nodes
    assert (np.all(graph_nodes.atom_index.values == np.arange(num_nodes)))
    graph_node_numeric = graph_nodes.drop(exclude_cols,
                                          axis=1,
                                          errors='ignore')
    for node_id in range(num_nodes):
        if target_graph:
            node_features = np.array([])
        else:
            atom_onehot = ATOM_ONEHOTS[graph_nodes.atom.values[node_id]]
            node_numeric = graph_node_numeric.iloc[node_id].values
            node_features = np.hstack([atom_onehot, node_numeric])

        node_features = node_features.astype(np.float)
        if graph_nx_format:
            graph_nx.add_node(node_id, features=node_features)
        else:
            graph_no_nx[1].append((node_id, node_features))

    # Edges
    no_edge_cols = exclude_cols + target_cols
    graph_edge_numeric = graph_edges.drop(no_edge_cols,
                                          axis=1,
                                          errors='ignore')
    for edge_id in range(num_edges):
        first_node = graph_edges.atom_index_0.values[edge_id]
        second_node = graph_edges.atom_index_1.values[edge_id]
        if target_graph:
            binding_onehot = BINDING_ONEHOTS[
                graph_edges.bond_type.values[edge_id]]
            edge_type_id = np.where(binding_onehot == 1)[0][0]
            if edge_type_id < 8:
                target_values = graph_edges[target_cols].values[edge_id]
            else:
                target_values = np.empty((len(target_cols)))
                target_values[:] = np.nan
            edge_features = np.hstack([target_values, edge_type_id])
            other_edge_permutation = np.zeros(())
        else:
            atom_onehot_0 = ATOM_ONEHOTS[graph_edges.atom_0.values[edge_id]]
            atom_onehot_1 = ATOM_ONEHOTS[graph_edges.atom_1.values[edge_id]]
            binding_onehot = BINDING_ONEHOTS[
                graph_edges.bond_type.values[edge_id]]
            connect_0_3J_onehot = ATOM_3J_ONEHOTS[
                graph_edges.interm_0_atom.values[edge_id]]
            connect_1_3J_onehot = ATOM_3J_ONEHOTS[
                graph_edges.interm_1_atom.values[edge_id]]
            edge_numeric = graph_edge_numeric.iloc[edge_id].values
            edge_features = np.hstack([
                binding_onehot, atom_onehot_0, atom_onehot_1,
                connect_0_3J_onehot, connect_1_3J_onehot, edge_numeric
            ])

            if edge_id == 0:
                other_edge_permutation = get_other_edge_ids(([
                    binding_onehot.size, atom_onehot_0.size,
                    atom_onehot_1.size, connect_0_3J_onehot.size,
                    connect_1_3J_onehot.size
                ], [(1, 2), (3, 4)]), graph_edge_numeric.columns)

        edge_features = edge_features.astype(np.float)
        if graph_nx_format:
            graph_nx.add_edge(first_node, second_node, features=edge_features)
            graph_nx.add_edge(second_node, first_node, features=edge_features)
        else:
            # Make sure to create a bidirectional edge at graph construction time!!!
            graph_no_nx[2].append((first_node, second_node, edge_features))

    return_graph = graph_nx if graph_nx_format else graph_no_nx
    if only_edge:
        return_graph = convert_to_edge_features(return_graph)
    return (return_graph, other_edge_permutation)
예제 #21
0
 def test_multidigraph(self):
     G = nx.OrderedMultiDiGraph()
예제 #22
0
def test_global_access():
    g = nx.OrderedMultiDiGraph()
    assert g.get_global() == {}
    g.get_global().update({"u": 2})
    g.get_global()["x"] = 1
    assert g.get_global()["x"] == 1
예제 #23
0
    def constructGraph(logEntries):
        '''
		Constructs a directed graph from the supplied list of log entries
		'''

        # Create a new directed graph with support for parallel edges
        graph = nx.OrderedMultiDiGraph()

        # Maintain a list of function calls for which we've not yet seen a return value
        pending = []

        # Gather all LdrLoadDll() calls and process them last
        loadDllEntries = list(
            [e for e in logEntries if e['function'] == 'LdrLoadDll'])
        logEntries = list([
            e for e in logEntries if e['function'] != 'LdrLoadDll'
        ]) + loadDllEntries

        # Process each log entry in turn
        for entry in logEntries:

            # Determine if the log entry is for the start of a function call or its return value
            if entry['type'] == 'enter':

                # Add the entry to our stack of pending function calls
                pending.append(entry)

            elif entry['type'] == 'return':

                # Identify which pending function call this entry represents the return value for
                match = GraphHelpers.findMatch(pending, entry)
                if match is None:
                    OutputFormatting.printWarning(
                        'encountered a return value before the function call!')
                    continue

                # If this is a LdrLoadDll() call then examine the stack trace to determine the appropriate module to treat as the caller
                # (Note that we filter out the resolved module in addition to KERNELBASE.DLL and NTDLL.DLL, to help prevent self-loops that provide little valuable information)
                if entry['function'] == 'LdrLoadDll':
                    candidates = [
                        m for m in entry['stack']
                        if m != entry['result'] and m.upper() not in [
                            'C:\\WINDOWS\\SYSTEM32\\KERNELBASE.DLL',
                            'C:\\WINDOWS\\SYSTEM32\\NTDLL.DLL'
                        ]
                    ]
                    if len(candidates) > 0:
                        entry['module'] = candidates[0]

                # Create a vertex for the calling module if we don't already have one
                if entry['module'] not in graph:
                    graph.add_node(entry['module'], non_loadlibrary_calls=[])

                # Determine if this is a LoadLibrary function call
                if entry['function'].startswith(
                        'LoadLibrary') or entry['function'] == 'LdrLoadDll':

                    # Create a vertex for the module resolved by the LoadLibrary() call if we don't already have one
                    # (Note that this will create a vertex called "NULL" that all failed calls will have outbound edges pointing to)
                    if entry['result'] not in graph:
                        graph.add_node(entry['result'],
                                       non_loadlibrary_calls=[])

                    # Create an edge between the calling module vertex and the resolved module vertex, annotated with the call details
                    graph.add_edge(entry['module'],
                                   entry['result'],
                                   details=entry)

                else:

                    # For all other function calls, just add an entry to the list in the metadata for the vertex
                    nx.get_node_attributes(
                        graph,
                        'non_loadlibrary_calls')[entry['module']].append(entry)

            else:
                raise RuntimeError('unsupported log entry type "{}"!'.format(
                    entry['type']))

        # Print a warning if there were any function calls for which we did not encounter a return value
        if len(pending) > 0:
            for entry in pending:
                OutputFormatting.printWarning(
                    'return value not found for function call: {}'.format(
                        entry))

        return graph
def graph_img_plotting(plotting, virtual_properties, senders, receivers,
                       xray_image, positions, hot_gas_positions, base_data_dir,
                       center, vprime, dataset, sphere):
    max_pos = np.max(positions.T, axis=1)
    min_pos = np.min(positions.T, axis=1)
    box_size = max_pos - min_pos

    if plotting == 1:
        print('Plotting xray...')
        fig, ax = plt.subplots(1, 3, figsize=(18, 6))
        ax[0].scatter(center[0] + hot_gas_positions[:, 0],
                      center[1] + hot_gas_positions[:, 1],
                      s=10**(2.5 - np.log10(positions.shape[0])))
        ax[0].set_xlim(center[0] - 0.5 * box_size[0],
                       center[0] + 0.5 * box_size[0])
        ax[0].set_ylim(center[1] - 0.5 * box_size[1],
                       center[1] + 0.5 * box_size[1])
        ax[0].set_title('Hot gas particles')
        ax[1].imshow(xray_image)
        # fig.colorbar(xray_plot, ax=ax[1])
        ax[1].set_title('Xray')
        ax[2].scatter(center[0] + positions.T[0],
                      center[1] + positions.T[1],
                      s=10**(2.5 - np.log10(positions.shape[0])))
        ax[2].set_xlim(center[0] - 0.5 * box_size[0],
                       center[0] + 0.5 * box_size[0])
        ax[2].set_ylim(center[1] - 0.5 * box_size[1],
                       center[1] + 0.5 * box_size[1])
        ax[2].set_title('Gas particles')
        plt.savefig(os.path.join(base_data_dir, 'images/xray.png'))
        plt.show()

    if plotting > 1:
        graph = nx.OrderedMultiDiGraph()

        n_nodes = virtual_properties.shape[
            0]  # number of nodes is the number of positions

        virtual_box_size = (np.min(virtual_properties[:, :3]),
                            np.max(virtual_properties[:, :3]))

        pos = dict()  # for plotting node positions.
        edgelist = []

        # Now put the data in the directed graph: first the nodes with their positions and properties
        # pos just takes the x and y coordinates of the position so a 2D plot can be made
        for node, feature, position in zip(np.arange(n_nodes),
                                           virtual_properties,
                                           virtual_properties[:, :3]):
            graph.add_node(node, features=feature)
            pos[node] = (position[:2] - virtual_box_size[0]) / (
                virtual_box_size[1] - virtual_box_size[0])

        # Next add the edges using the receivers and senders arrays we just created
        # Note that an edge is added for both directions
        # The features of the edge are dummy arrays at the moment
        # The edgelist is for the plotting
        # edges = np.stack([senders, receivers], axis=-1) + sibling_node_offset
        for u, v in zip(senders, receivers):
            graph.add_edge(u, v, features=np.array([1., 0.]))
            edgelist.append((u, v))

        print(f'Particle graph nodes : {graph.number_of_nodes()}')
        print(f'Particle graph edges : {graph.number_of_edges()}')

        dens_list = []

        for n in list(graph.nodes.data('features')):
            dens_list.append(n[1][6])

        if plotting == 2:
            # Plotting
            fig, ax = plt.subplots(2, 2, figsize=(12, 12))
            print('Plotting multiplot...')
            draw(graph,
                 ax=ax[0, 0],
                 pos=pos,
                 edge_color='red',
                 node_size=10**(4 - np.log10(len(dens_list))),
                 width=0.1,
                 arrowstyle='-',
                 node_color=dens_list,
                 cmap='viridis')
            # draw(graph, ax=ax[0, 0], pos=pos, node_color='blue', edge_color='red', node_size=10, width=0.1)
            ax[0, 1].scatter(center[0] + hot_gas_positions[:, 0],
                             center[1] + hot_gas_positions[:, 1],
                             s=10**(2.5 - np.log10(positions.shape[0])))
            ax[0, 1].set_xlim(center[0] - 0.5 * box_size[0],
                              center[0] + 0.5 * box_size[0])
            ax[0, 1].set_ylim(center[1] - 0.5 * box_size[1],
                              center[1] + 0.5 * box_size[1])
            ax[0, 1].set_title('Hot gas particles')
            ax[1, 0].imshow(xray_image)
            # fig.colorbar(xray_plot, ax=ax[1])
            # ax[1].set_title('Xray')
            ax[1, 1].scatter(center[0] + positions.T[0],
                             center[1] + positions.T[1],
                             s=10**(2.5 - np.log10(positions.shape[0])))
            ax[1, 1].set_xlim(center[0] - 0.5 * box_size[0],
                              center[0] + 0.5 * box_size[0])
            ax[1, 1].set_ylim(center[1] - 0.5 * box_size[1],
                              center[1] + 0.5 * box_size[1])
            ax[1, 1].set_title('Gas particles')
            print('Multiplot done, showing...')
            plt.savefig(os.path.join(base_data_dir, 'images/multiplot.png'))
            plt.show()

        if plotting == 3:
            interp_virtual_positions = virtual_properties[:, :3] / 1e4
            fig, ax = plt.subplots(figsize=(8, 8))
            _x = np.linspace(np.min(interp_virtual_positions[:, 0]),
                             np.max(interp_virtual_positions[:, 0]), 300)
            _y = np.linspace(np.min(interp_virtual_positions[:, 1]),
                             np.max(interp_virtual_positions[:, 1]), 300)
            _z = np.linspace(np.min(interp_virtual_positions[:, 2]),
                             np.max(interp_virtual_positions[:, 2]), 300)
            x, y, z = np.meshgrid(_x, _y, _z, indexing='ij')

            interp = interpolate.griddata(
                (interp_virtual_positions[:, 0],
                 interp_virtual_positions[:, 1], interp_virtual_positions[:,
                                                                          2]),
                dens_list,
                xi=(x, y, z),
                fill_value=0.0)

            im = np.mean(interp, axis=2).T[::-1, ]
            im = np.log10(
                np.where(im / np.max(im) < 1e-3, 1e-3, im / np.max(im)))
            ax.imshow(im)
            plt.savefig(os.path.join(base_data_dir, 'images/interp.png'))
            plt.show()

        if plotting == 4:
            print('Plotting off-axis projection...')
            east_vector = vprime[:, 0]
            north_vector = vprime[:, 1]
            viewing_vec = vprime[:, 2]
            fig, ax = plt.subplots(figsize=(8, 8))
            off_axis_image = yt.off_axis_projection(data_source=dataset,
                                                    center=sphere.center,
                                                    normal_vector=viewing_vec,
                                                    width=0.01 *
                                                    dataset.domain_width,
                                                    item='Density',
                                                    resolution=[400, 400],
                                                    north_vector=east_vector)
            off_axis_image = np.log10(
                np.where(off_axis_image < 1e17, 1e17, off_axis_image))
            # off_axis_image = off_axis_image.to_ndarray() / np.max(off_axis_image.to_ndarray())
            # off_axis_image = np.log10(np.where(off_axis_image < 1e-5, 1e-5, off_axis_image))
            # print(f'Maximum : {np.max(off_axis_image)}')
            off_axis_image = ax.imshow(off_axis_image)
            fig.colorbar(off_axis_image, ax=ax)
            plt.savefig(os.path.join(base_data_dir,
                                     'images/off_axis_proj.png'))
            plt.show()
            # yt.write_image(np.log10(off_axis_image), os.path.join(base_data_dir, 'images/off_axis_proj.png'))

        if plotting == 5:
            print('Plotting 3D image...')
            mlab.points3d(virtual_properties.T[0],
                          virtual_properties.T[1],
                          virtual_properties.T[2],
                          dens_list,
                          resolution=8,
                          scale_factor=0.15,
                          scale_mode='none',
                          colormap='viridis')
            for u, v in zip(senders, receivers):
                mlab.plot3d(
                    [virtual_properties[u][0], virtual_properties[v][0]],
                    [virtual_properties[u][1], virtual_properties[v][1]],
                    [virtual_properties[u][2], virtual_properties[v][2]],
                    tube_radius=None,
                    tube_sides=3,
                    opacity=0.1)
            mlab.show()
예제 #25
0
    for quest, prereqs in quests.items():
        if prereqs:
            for prereq in prereqs:
                graph_data.append((quest, prereq))
        else:
            graph_data.append((quest, quest))

    # Creating Graph
    connections_are_quest_color = True  # or required quest color
    plt.figure(figsize=(60, 80))
    plt.title("Old School Runescape Quest Tree\n" +
              ("(colored by quest)"
               if connections_are_quest_color else "(colored by requirement)"),
              fontsize=150,
              fontweight="bold")
    g = nx.OrderedMultiDiGraph()
    colors = [
        'brown', 'orange', 'olive', 'green', 'cyan', 'blue', 'sienna', 'red',
        'black', 'black'
    ]

    # Edge colors
    edge_colors = []
    for d in graph_data:
        q1, q2 = d

        # Broken, temporary fix!
        q1 = q1.replace("recipe for disaster/", "")
        q2 = q2.replace("recipe for disaster/", "")

        g.add_edge(q1, q2)