Exemple #1
0
def generate_example_random_choice(positions, properties, k=26, plot=False):
    print('choice nn')
    idx_list = np.arange(len(positions))
    virtual_node_positions = positions[np.random.choice(idx_list, 1000, replace=False)]

    kdtree = cKDTree(virtual_node_positions)
    dist, indices = kdtree.query(positions)

    virtual_properties = np.zeros((len(np.bincount(indices)), len(properties[0])))

    mean_sum = [lambda x: np.bincount(indices, weights=x) / np.maximum(1., np.bincount(indices)),  # mean
                lambda x: np.bincount(indices, weights=x)]  # sum

    mean_sum_enc = [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1]

    for p, enc in zip(np.arange(len(properties[0])), mean_sum_enc):
        virtual_properties[:, p] = mean_sum[enc](properties[:, p])
        virtual_positions = virtual_properties[:, :3]

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

    n_nodes = virtual_positions.shape[0]

    pos = dict()  # for plotting node positions.
    edgelist = []
    for node, feature, position in zip(np.arange(n_nodes), virtual_properties, virtual_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=np.array([1., 0.]))
        graph.add_edge(v, u, features=np.array([1., 0.]))
        edgelist.append((u, v))
        edgelist.append((v, u))

    graph.graph["features"] = np.array([0.])
    # plotting

    print('len(pos) = {}\nlen(edgelist) = {}'.format(len(pos), len(edgelist)))
    if plot:
        fig, ax = plt.subplots(1, 1, figsize=(20, 20))
        draw(graph, ax=ax, pos=pos, node_color='blue', edge_color='red', node_size=10, width=0.1)

        image_dir = '/data2/hendrix/images/'
        graph_image_idx = len(glob.glob(os.path.join(image_dir, 'graph_image_*')))
        plt.savefig(os.path.join(image_dir, 'graph_image_{}'.format(graph_image_idx)))

    return networkxs_to_graphs_tuple([graph],
                                     node_shape_hint=[virtual_positions.shape[1] + virtual_properties.shape[1]],
                                     edge_shape_hint=[2])
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]])
Exemple #3
0
def generate_example(positions, properties, k_mean=26, plot=False):
    """
    Generate a geometric 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_mean: float
        plot: whether to plot graph.

    Returns: GraphTuple
    """
    graph = nx.DiGraph()
    sibling_edgelist = []
    parent_edgelist = []
    pos = dict()  # for plotting node positions.
    real_nodes = list(np.arange(positions.shape[0]))
    while positions.shape[0] > 1:
        # n_nodes, n_nodes
        dist = np.linalg.norm(positions[:, None, :] - positions[None, :, :], axis=-1)
        opt_screen_length = find_screen_length(dist, k_mean)
        print("Found optimal screening length {}".format(opt_screen_length))

        distance_matrix_no_loops = np.where(dist == 0., np.inf, dist)
        A = distance_matrix_no_loops < opt_screen_length

        senders, receivers = np.where(A)
        n_edge = senders.size
        # [1,0] for siblings, [0,1] for parent-child
        sibling_edges = np.tile([[1., 0.]], [n_edge, 1])

        # num_points, F0,...Fd
        # if positions is to be part of features then this should already be set in properties.
        # We don't concatentate here. Mainly because properties could be an image, etc.
        sibling_nodes = properties
        n_nodes = sibling_nodes.shape[0]

        sibling_node_offset = len(graph.nodes)
        for node, feature, position in zip(np.arange(sibling_node_offset, sibling_node_offset + n_nodes), sibling_nodes,
                                           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 + sibling_node_offset, receivers + sibling_node_offset):
            graph.add_edge(u, v, features=np.array([1., 0.]))
            graph.add_edge(v, u, features=np.array([1., 0.]))
            sibling_edgelist.append((u, v))
            sibling_edgelist.append((v, u))

        # for virtual nodes
        sibling_graph = GraphsTuple(nodes=None,  # sibling_nodes,
                                    edges=None,
                                    senders=senders,
                                    receivers=receivers,
                                    globals=None,
                                    n_node=np.array([n_nodes]),
                                    n_edge=np.array([n_edge]))

        sibling_graph = graphs_tuple_to_networkxs(sibling_graph)[0]
        # completely connect
        connected_components = sorted(nx.connected_components(nx.Graph(sibling_graph)), key=len)
        _positions = []
        _properties = []
        for connected_component in connected_components:
            print("Found connected component {}".format(connected_component))
            indices = list(sorted(list(connected_component)))
            virtual_position, virtual_property = make_virtual_node(positions[indices, :], properties[indices, ...])
            _positions.append(virtual_position)
            _properties.append(virtual_property)

        virtual_positions = np.stack(_positions, axis=0)
        virtual_properties = np.stack(_properties, axis=0)

        ###
        # add virutal nodes
        # num_parents, 3+F
        parent_nodes = virtual_properties
        n_nodes = parent_nodes.shape[0]
        parent_node_offset = len(graph.nodes)
        parent_indices = np.arange(parent_node_offset, parent_node_offset + n_nodes)
        # adding the nodes to global graph
        for node, feature, virtual_position in zip(parent_indices, parent_nodes, virtual_positions):
            graph.add_node(node, features=feature)
            print("new virtual {}".format(node))
            pos[node] = virtual_position[:2]

        for parent_idx, connected_component in zip(parent_indices, connected_components):

            child_node_indices = [idx + sibling_node_offset for idx in list(sorted(list(connected_component)))]
            for child_node_idx in child_node_indices:
                graph.add_edge(parent_idx, child_node_idx, features=np.array([0., 1.]))
                graph.add_edge(child_node_idx, parent_idx, features=np.array([0., 1.]))
                parent_edgelist.append((parent_idx, child_node_idx))
                parent_edgelist.append((child_node_idx, parent_idx))
                print("connecting {}<->{}".format(parent_idx, child_node_idx))

        positions = virtual_positions
        properties = virtual_properties

    # plotting

    virutal_nodes = list(set(graph.nodes) - set(real_nodes))
    if plot:
        fig, ax = plt.subplots(1, 1, figsize=(12, 12))
        draw(graph, ax=ax, pos=pos, node_color='green', edgelist=[], nodelist=real_nodes)
        draw(graph, ax=ax, pos=pos, node_color='purple', edgelist=[], nodelist=virutal_nodes)
        draw(graph, ax=ax, pos=pos, edge_color='blue', edgelist=sibling_edgelist, nodelist=[])
        draw(graph, ax=ax, pos=pos, edge_color='red', edgelist=parent_edgelist, nodelist=[])
        plt.show()

    return networkxs_to_graphs_tuple([graph],
                                     node_shape_hint=[positions.shape[1] + properties.shape[1]],
                                     edge_shape_hint=[2])
Exemple #4
0
def generate_example_nn(positions, properties, k=26, resolution=2, plot=False):
    print('example nn')

    resolution = 3.086e18 * resolution  # pc to cm

    node_features = []
    node_positions = []

    box_size = (np.max(positions), np.min(positions))  # box that encompasses all of the nodes
    axis = np.arange(box_size[1] + resolution, box_size[0], resolution)
    lists = [axis] * 3
    virtual_node_pos = [p for p in product(*lists)]
    virtual_kdtree = cKDTree(virtual_node_pos)
    particle_kdtree = cKDTree(positions)
    indices = virtual_kdtree.query_ball_tree(particle_kdtree, np.sqrt(3) / 2. * resolution)

    for i, p in enumerate(indices):
        if len(p) == 0:
            continue
        virt_pos, virt_prop = make_virtual_node(properties[p])
        node_positions.append(virt_pos)
        node_features.append(virt_prop)

    node_features = np.array(node_features)
    node_positions = np.array(node_positions)

    graph = nx.DiGraph()

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

    n_nodes = node_positions.shape[0]

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

    for node, feature, position in zip(np.arange(n_nodes), node_features, node_positions):
        graph.add_node(node, features=feature)
        pos[node] = (position[:2] - box_size[1]) / (box_size[0] - box_size[1])

    # 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.]))
        graph.add_edge(v, u, features=np.array([1., 0.]))
        edgelist.append((u, v))
        edgelist.append((v, u))

    graph.graph["features"] = np.array([0.])
    # plotting

    print('len(pos) = {}\nlen(edgelist) = {}'.format(len(pos), len(edgelist)))
    if plot:
        fig, ax = plt.subplots(1, 1, figsize=(12, 12))
        draw(graph, ax=ax, pos=pos, node_color='green', edge_color='red')

        image_dir = '/data2/hendrix/images/'
        graph_image_idx = len(glob.glob(os.path.join(image_dir, 'graph_image_*')))
        plt.savefig(os.path.join(image_dir, 'graph_image_{}'.format(graph_image_idx)))

    return networkxs_to_graphs_tuple([graph],
                                     node_shape_hint=[node_positions.shape[1] + node_features.shape[1]],
                                     edge_shape_hint=[2])
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()