def view_normalize(path, size_check):
    '''read graphs and normalize them'''
    f = [f for f in listdir(path) if isfile(join(path, f))]
    #f = f[0:2000]
    #print(f)
    arr = []
    shob = []
    for i in f :
        #print(i)
        gph = open(path + i, 'r')
        cont = gph.readlines()
        ls_node, ls_edge = gphtols_view(cont,False)
        if np.asarray(ls_node).any() > size_check or np.asarray(ls_node).any() < -size_check :
            print(i)
        #ls_node, ls_edge = gphtols(cont)
        #node = make_gph(ls_node, ls_edge, range(len(ls_node)))
        graph = make_graph(ls_node, ls_edge, range(len(ls_node)))
        node = graph.get_number_nodes()
        if node == 0 :
            continue

        #arr.append(node)
        #print(ls_node)
        for j in range(len(ls_node)):
            shob.append(ls_node[j])
            arr.append(ls_node[j][0])
    #arr = np.asarray(arr)
    #arr = arr+15000
    #shob = arr
    shob = np.asarray(shob)
    np.save('./data/numpy_arrays/nodes_out.npy', shob)
    plt.hist(shob,bins=200)
    plt.show()
    qt = QuantileTransformer(output_distribution='normal')
    shob = qt.fit_transform(shob)
    plt.hist(shob,bins=200)
    plt.show()
    #mean, std = mean_std(shob,'out_128')
    #shob = (shob-mean)/std
    #plt.hist(shob,bins=200)
    #plt.show()
    new_data, a, b = change_range(shob,'range')
    #first = (shob-mean)/std
    plt.hist(new_data,bins=200)
    plt.show()
    """ second = a*first + b
 def __init__(self,path,flip):
     '''plot graph from a text file
     Args : 
         - path : to the text files
     Returns : 
         - matplotlib plot of the graphs in the folder
     '''
     f = [f for f in listdir(path) if isfile(join(path, f))]
     #f = f[0:3]
     #print(f)
     for i in f :
         print(i)
         gph = open(path + i, 'r')
         cont = gph.readlines()
         ls_node, ls_edge = gphtols_view(cont,flip)
         graph = make_graph(ls_node, ls_edge, range(len(ls_node)))
         graph.show_graph()
예제 #3
0
def node_out_128(inp_dir, out_dir, out_coor):
    '''exclude the 128 and -128 values from the node attribute txt files and 
    write new node attribute files'''

    node_path = inp_dir
    f = [f for f in listdir(node_path) if isfile(join(node_path, f))]
    #f = f[0:10]
    for i in f:
        print(i)
        gph = open(node_path + i, 'r')
        cont = gph.readlines()
        ls_node, ls_edge = gphtols_view(cont, False)

        graph = make_graph(ls_node, ls_edge, range(len(ls_node)))
        nodes = np.asarray(ls_node)

        wh_128 = np.where(nodes == out_coor)
        wh_128 = list(wh_128[0])
        wh_128n = np.where(nodes == -out_coor)
        wh_128n = list(wh_128n[0])

        full_list = list(set(wh_128) | set(wh_128n))  #union of two lists

        for j in range(len(full_list)):
            graph.remove_n(full_list[j])

        adj = nx.attr_matrix(graph.get_graph())[0]
        edges = []

        for j in range(adj.shape[0]):
            for k in range(adj.shape[1]):
                if adj[j, k] == 1.:
                    edges.append([j, k])

        nodes = list(
            nx.get_node_attributes(graph.get_graph(), name='coor').values())

        #print(nodes,edges)

        write_gph(out_dir + i, nodes, edges)
def crop_to_gph(gph_path, outdir, flip, img_dir):
    '''crop graph txt according to given super img files
    Args :
        - gph_path : path of the supergraphs which needs cropping
        - outdir : output directory where the cropped graphs will be written
    '''

    path = [f for f in listdir(gph_path) if isfile(join(gph_path, f))]
    #path = path[0:1]

    for i in path :
        gph = open(gph_path + i, 'r')
        cont = gph.readlines()
        #print(len(cont))
        ls_node, ls_edge = gphtols_view(cont,flip)
        #ls_node, ls_edge = gphtols(cont)
        name = i.split('.')[0]
        print(name)
        #print(ls_edge)
        gph.close()
        #nodes, edges, index = gph_crop(ls_node, ls_edge, name)
        #nodes, edges, index = crop_p(ls_node, ls_edge, name)
        crop_gph_256(ls_node, ls_edge, name, outdir, 512, img_dir)
예제 #5
0
def fix_nodes(supimg_path, gph_path, output_dir, img_size):
    '''change node feature so that they are attributed based on the 
    center of each of the image crops instead of being attributed based
    on the center point of the superimage
    '''
    path = supimg_path
    path_list = [f for f in listdir(path) if isfile(join(path, f))]
    #path_list = path_list[0:1]

    gph_path = gph_path
    #gph_list = [f for f in listdir(path) if isfile(join(path, f))]
    #gph_list = gph_list[0:10]

    for i in range(len(path_list)):
        name = path_list[i].split('.')[0]
        print(name)
        img = cv2.imread(path + path_list[i], 0)
        height = img.shape[0]
        width = img.shape[1]

        row_times = height / img_size
        column_times = width / img_size
        #print(row_times,column_times)
        first_center = [
            -(width / 2) + (img_size / 2), (height / 2) - (img_size / 2)
        ]
        #print(first_center)
        for j in range(int(row_times)):
            for k in range(int(column_times)):
                gph_name = name + '_' + str(j) + '_' + str(k) + '.txt'
                #print(gph_name)
                gph = open(gph_path + gph_name, 'r')
                cont = gph.readlines()
                nodes, edges = gphtols_view(cont, False)
                #print(nodes)
                center = [
                    first_center[0] + (img_size * k),
                    first_center[1] - (img_size * j)
                ]
                """ if k<3 and j<3 :
                    print(gph_name)
                    print(nodes[0:3]) """
                #print(center)

                for l in range(len(nodes)):
                    x_abs = abs(nodes[l][0] - center[0])
                    y_abs = abs(nodes[l][1] - center[1])

                    if nodes[l][0] > center[0]:
                        x = x_abs
                    else:
                        x = -x_abs
                    if nodes[l][1] > center[1]:
                        y = y_abs
                    else:
                        y = -y_abs
                    if nodes[l][0] == center[0]:
                        x = 0.0
                    if nodes[l][1] == center[1]:
                        y = 0.0

                    nodes[l] = [x, y]
                    if x > img_size or x < -img_size or y > img_size or y < -img_size:
                        print(name)
                """ if k<3 and j<3 :
                    
                    print(center)
                    print(nodes[0:3]) """
                write_gph(output_dir + gph_name, nodes, edges)
예제 #6
0
def createDataRecord(out_filename, addrs_y, img_path, gph_path):
    array = np.load('./data/numpy_arrays/nodes_out.npy')
    qt = QuantileTransformer(output_distribution='normal')
    shob = qt.fit_transform(array)

    #mean = np.load('./data/numpy_arrays/fixed_node/mean.npy')
    #std = np.load('./data/numpy_arrays/fixed_node/std.npy')
    a = np.load('./data/numpy_arrays/range/a.npy')
    b = np.load('./data/numpy_arrays/range/b.npy')

    #an = np.load('./data/numpy_arrays/nodes/a.npy')
    #bn = np.load('./data/numpy_arrays/nodes/b.npy')

    #num_n = []
    writer = tf.io.TFRecordWriter(out_filename)
    for i in range(len(addrs_y)):
        print(i)
        if i == 0:
            print(addrs_y[i])
        img_y = cv2.imread(img_path + str(addrs_y[i]))
        img_y = img_y / 255
        img_y = np.asarray(
            img_y, dtype=np.float32
        )  #all data has to be converted to np.float32 before writing

        gph = open(gph_path + addrs_y[i].split('.')[0] + '.txt', 'r')
        cont = gph.readlines()
        ls_node, ls_edge = gphtols_view(cont, flip=False)
        if i == 0:
            print(ls_node)
        if len(ls_node) == 0:
            continue
        #node_attr = np.asarray(ls_node,dtype=np.float32)
        #print(ls_node)

        #node_attr = (a*((ls_node - mean)/std))+b
        node_attr = np.asarray(ls_node, dtype=np.float32)
        node_attr = qt.transform(node_attr)
        node_attr = (a * node_attr) + b

        node_attr = np.asarray(node_attr, dtype=np.float32)
        #print(node_attr)
        #ls_node, ls_edge = gphtols(cont)
        #node = make_gph(ls_node, ls_edge, range(len(ls_node)))
        graph = make_graph(ls_node, ls_edge, range(len(ls_node)))
        #num_nodes = graph.get_num_nodes()
        #num_nodes = np.log(num_nodes)
        #num_nodes = (an*num_nodes)+bn

        #num_nodes = np.asarray(num_nodes,dtype=np.float32)
        adj_mtx = graph.get_adj()
        adj_mtx = np.asarray(adj_mtx, dtype=np.float32)
        #num_n.append(num_nodes)

        if i == 0:
            print(node_attr, node_attr.shape)
            print(adj_mtx)
            #print(num_nodes)
            print(img_y)

        feature = {
            'image_y': _bytes_feature(img_y.tostring()),
            'gph_nodes': _bytes_feature(node_attr.tostring()),
            'gph_adj': _bytes_feature(adj_mtx.tostring())
            #'gph_node_num' : _bytes_feature(num_nodes.tostring())
        }

        example = tf.train.Example(features=tf.train.Features(feature=feature))

        writer.write(example.SerializeToString())

    writer.close()
    sys.stdout.flush()