1

how to create the dotted line in the below NumPy array

import NumPy as np
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap

x=np.array( [ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
          [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
          [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]])

def make_figure(inp_arr: np.array, outputname):
    # create graphical output for visual check
    cmap = ListedColormap([ 'r','b','g'])
    plt.imshow(inp_arr, cmap=cmap)
    plt.grid(color='b', linestyle=':', linewidth=0.55)
    plt.savefig(input_folder + 'pics_' + str(outputname) + '.png', format='png', dpi=350)
    # plt.show()
    #plt.clf()

bh=make_figure(b,'gh')

requirement: how to convert element 1 into 0 with the step of two expected outputs is like I tried with a brute force algorithm, but I am not able to find the solution

enter image description here

output array looks like

 y=np.array( [ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0]])

for visual representation like making a dotted line

enter image description here

raja
  • 123
  • 7
  • I don't quite understand your question. You want to perform some operation(s) on `x` `np.array` to transform it into `y` `np.array`? – rickhg12hs Oct 14 '22 at 03:02
  • y array is the output of x array. like access the every 2nd consecutive element values 1 in an array x. – raja Oct 14 '22 at 07:53

1 Answers1

2

Here's one way to find the minimum weight full path, then take the first point, skip two points, and repeat until the end of the path.

import numpy as np
from sklearn.neighbors import radius_neighbors_graph
from scipy import sparse
import networkx as nx
x = np.array( [ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1] ] )

x_nonzeros = x.nonzero()
num_points = len(x_nonzeros[0])

x_coords = [[x_nonzeros[0][k], x_nonzeros[1][k]] for k in range(num_points)]

neighbors = radius_neighbors_graph(x_coords, radius=1.5, mode="distance")

G = nx.Graph(neighbors)

full_paths = [
  {"path": path, "weight": nx.classes.path_weight(G, path, weight="weight")}
  for path in nx.all_simple_paths(G, 0, 40) if len(path)==num_points
]

full_paths.sort(key=lambda rec: rec["weight"])

the_path = full_paths[0]["path"]

y_coords = [x_coords[coord] for coord in the_path[0::3]]

y = sparse.coo_array(([1]*len(y_coords),np.array(y_coords).T)).toarray()

print(y)
# [[1 0 0 0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 1 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 1 0 0 1 0 0 1]
#  [0 0 0 0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0 0 0 0]
#  [1 0 0 1 0 0 1 0 0 1 0 0 1]
#  [0 0 0 0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 0 0 0 0 0]
#  [0 0 0 1 0 0 1 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0 0 1 0 0 1 0]]
rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
  • thanks a lot for your help, Is any way to select an automatic target path points – raja Oct 16 '22 at 02:38
  • like endpoint we can select using np.count_nonzero(x)-1, is it possible to access non continues different paths – raja Oct 16 '22 at 02:57
  • 1
    @raja I'm not sure - I'm a graph theory noob. `nx.all_simple_paths(G, 0, 40)` is using the identifiers of the `start` and `target` nodes of the graph `G`. If we know the starting node `0`, we can get the farthest node like this: `farthest_node = reduce(lambda x,y: y if y[1]>x[1] else x, nx.single_source_shortest_path_length(G, 0).items())[0]`. – rickhg12hs Oct 16 '22 at 04:31