-
Notifications
You must be signed in to change notification settings - Fork 0
/
CorpOrgVis-legacy.py
159 lines (122 loc) · 5.39 KB
/
CorpOrgVis-legacy.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 22 18:25:40 2020
@author: Administrator
Let us see if I can code something to create an automation tool
that can automatically visualise data from an excel sheet
Woot woot it works.
"""
#%% Importing libraries we need
import pandas as pd
import pyyed
import os
from datetime import datetime
import matplotlib
import sys
now = datetime.now()
dt_string = now.strftime("%Y.%m.%d - %H.%M")
print("Libraries imported and base variables defined.")
# Read folder for source file
# Make sure that when you're running this, it's referencing the correct folder
files = [f for f in os.listdir(os.curdir) if os.path.isfile(f) and f.endswith((".xls", ".xlsm", ".xlsx"))]
files.sort()
index_files = {i: files[i] for i in range(0, len(files))}
target = ''
target_ID = ''
if len(index_files) == 0:
print("No files found in current folder, please double check what it's currently referring to.")
sys.exit()
if len(index_files) == 1:
print("Only one file found in directory, assuming it to be target and importing directly.")
target_ID = 0
target = index_files[int(target_ID)]
# Importing dataframe
df = pd.read_excel(target, sheet_name = "Links")
print(f"\n{target} imported.")
if len(index_files) > 1:
for i in index_files:
print(i, index_files[i])
target_ID = input('Using the index number, which excel will we import from?: ')
if target_ID.isalpha() or int(target_ID) > len(files):
print('Please enter valid ID for import target.')
target_ID = ''
if KeyError or ValueError:
print('Please enter valid ID for import target.')
target_ID = ''
target = index_files[int(target_ID)]
# Importing dataframe
df = pd.read_excel(target, sheet_name = "Links")
print(f"\n{target} imported.")
# Defining the nature of the entities, refining those dfs to just relevant data
# Because pyyed is amazing and already can deal with these linkages
# Entity list generation
entity_list = df.iloc[:,0].values.tolist() + df.iloc[:,1].values.tolist()
entity_list = list(set(entity_list))
# Entity dict generation (Entity - Colour pairing). Filtering as well.
entity_colour = {k: g["Colour"].tolist() for k,g in df.groupby("Subsidiary")}
for v in entity_colour.values():
v[:] = list(set(v))
entity_colour = {k: str(v) for k, v in entity_colour.items()}
for k in entity_list:
if k not in entity_colour.keys():
entity_colour.update({k: ""})
for k, v in entity_colour.items():
entity_colour[k] = entity_colour[k].replace("[","")
entity_colour[k] = entity_colour[k].replace("nan","")
entity_colour[k] = entity_colour[k].replace("]","")
entity_colour[k] = entity_colour[k].replace("'","")
entity_colour[k] = entity_colour[k].replace(",","")
entity_colour[k] = entity_colour[k].replace(" ","")
for k, v in entity_colour.items():
if v == "":
entity_colour[k] = 'white'
for k, v in entity_colour.items():
try:
entity_colour[k] = matplotlib.colors.cnames[entity_colour[k]]
except:
entity_colour[k] = '#FFFFFF' #default background to white
# Creating nodes
target_name = os.path.splitext(target)[0]
g = pyyed.Graph()
for name, colour in entity_colour.items():
if name != 'Placeholder':
g.add_node(name, label=name, label_alignment="center",
shape="rectangle", shape_fill = colour)
else:
g.add_node(name, label="", label_alignment="center",
shape="rectangle", shape_fill = "#FFFFFF",
transparent = "true", edge_color = "#FFFFFF")
g.add_node("Milestone", label = f"Made {dt_string} \n Based on: {target}",
font_style = "bold")
# Sorting entities
style_df = df.iloc[:, [0,1,2]]
style_df = style_df.dropna(axis = 0) #any entities with special linkages? defining link style
label_df = df.iloc[:, [0,1,3]]
label_df = label_df.dropna(axis = 0) #defining link labels (e.g. % ownership)
label_df["Ownership"] = label_df["Ownership"].astype(str)
standard_link_df = df[df["Connection"].isna()]
standard_link_df = standard_link_df[standard_link_df["Ownership"].isna()]
# Creating links
for row in standard_link_df.itertuples():
g.add_edge(row.Parent, row.Subsidiary,
color="#000000", arrowhead = "standard", arrowfoot = "none",
line_type = "line")
for row in label_df.itertuples():
g.add_edge(row.Parent, row.Subsidiary, label = row.Ownership+'%',
color="#000000", arrowhead = "standard", arrowfoot = "none",
line_type = "line", )
for row in style_df.itertuples():
if row.Connection == 1:
g.add_edge(row.Parent, row.Subsidiary,
color="#000000", arrowhead = "standard",
arrowfoot = "standard", line_type = "dashed")
elif row.Connection == 0:
g.add_edge(row.Parent, row.Subsidiary,
color="#FFFFFF", arrowhead = "none",
arrowfoot = "none", line_type = "dotted")
g.add_edge("Placeholder", "Milestone",
color="#FFFFFF", arrowhead = "none",
arrowfoot = "none", line_type = "dotted")
#Exporting
g.write_graph(f'{target_name} Chart (ver. {dt_string}).graphml')
print(f"\nGraph exported as {target_name} Chart({dt_string}).graphml")