-
Notifications
You must be signed in to change notification settings - Fork 0
/
attributeGraph.py
executable file
·65 lines (55 loc) · 2.15 KB
/
attributeGraph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
import numpy as np
import networkx as nx
from TreeOfShapes import *
def createAttributeGraph(tree1, im):
"""
Graph: construct hierarchy using NetworkX adding attribute
"""
num_x = im.shape[1]
num_y = im.shape[0]
g = nx.Graph()
nodePixMap = np.zeros((im.shape[0]-2, im.shape[1]-2), dtype=int)
g.graph['leaves'] = []
# creating tree
for i in tree1.iterateFromRootToLeaves(False):
# not root node
if tree1.data[i] != -1:
g.add_edge(i, tree1.data[i])
g.node[i]['grayLevel'] = tree1.levels[i]
g.node[i]['area'] = tree1.area[i]
if 'children' in g.node[tree1.data[i]]: # exist
g.node[tree1.data[i]]['children'].append(i)
else: # not exist
g.node[tree1.data[i]]['children'] = [i]
if 'parent' in g.node[i]:
g.node[i]['parent'].append(tree1.data[i])
else:
g.node[i]['parent'] = [tree1.data[i]]
# root node
else:
g.add_node(i)
g.node[i]['parent'] = -1
g.node[i]['grayLevel'] = tree1.levels[i]
g.node[i]['area'] = tree1.area[i]
# adding node attribution
for i in tree1.iterateFromRootToLeaves(False):
# in case of the node has children attribute (i.e. not leaf node)
if 'children' in g.node[i]:
g.node[i]['leaf'] = False
g.node[i]['pixels'] = []
# no children attribute (i.e. leaf node)
else:
g.node[i]['leaf'] = True
g.graph['leaves'].append(i)
g.node[i]['pixels'] = []
# adding leaf
for y in range(3,num_y*2-2, 2):
for x in range(3, num_x*2-2, 2):
g.node[ tree1.data[(2*num_x+1)*y + x] ]['pixels'].append([(x-1)/2-1, (y-1)/2-1])
nodePixMap[(y-1)/2-1, (x-1)/2-1] = int(tree1.data[(2*num_x+1)*y + x])
# adding number of nodes
g.graph['n_nodes'] = ( max(g.nodes()) - min(g.nodes()) + 1 )
g.graph['orderNodes'] = tree1.iterateFromRootToLeaves(False)
return g, nodePixMap
# ======================================================