-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathet_tool.py
138 lines (111 loc) · 4.02 KB
/
et_tool.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
"""
===============================================================================
Event Tree Analysis Tool
(c) Umair Siddique
Licence: check licence
To DO - Tree Pruning, Probablistic Analysis, Mapping to FT, More Safety Aspects
===============================================================================
"""
from ete3 import Tree, TreeStyle
import pandas
import argparse
import itertools
def add_paranthesis (s):
"""
The function "add_paranthesis" takes a string "s" and
add paranthesis, i.e., "(s)"
"""
return('(' + s + ')')
def str_append_helper (s,l):
"""
This function take a string (e.g., 'abc') and list (e.g., ['s1', 's2']) and
append the elements of the list with the string in following format:
['(abc)s1', (abc)s2]
"""
out = []
for i in l:
out.append (add_paranthesis(s + i))
return(out)
def make_tree (sys):
"""
This function takes the system description in the following format (csv):
componenet 1, component 2
---------------------------
| Sensor_1 , Sensor_2 (first row for component names)
State 1 | open , misaligned
State 2 | short , low_voltage
State 3 | stuck , noisy
and convert this to "Newick Tree" format which can then be visualized in a
different ways using "ete3" library.
"""
sys.reverse()
head, *tail =sys
s = (tuple(head))
init_str = add_paranthesis(add_paranthesis (','.join(s)))
str_iter = [init_str]
for elements in tail:
intermediate_str = []
for st in str_iter:
intermediate_str = intermediate_str + str_append_helper(st,elements)
next_iter = add_paranthesis(add_paranthesis (','.join(intermediate_str)))
str_iter = [next_iter]
return(str_iter)
def get_system(name):
data = name
df = pandas.read_csv(data)
comp_states = []
components = df.columns.tolist()
for i in components:
comp_states.append(list(df[i]))
all_paths = list(itertools.product(*comp_states))
return (df,components,comp_states,all_paths)
"""
===============================================================================
Argument parsing and pretty-printing work
===============================================================================
"""
parser = argparse.ArgumentParser(description='Event Tree Analysis Tool')
parser.add_argument('-s', action="store", dest="system",
help='System Description -- components and associated states')
parser.add_argument('-o', action="store", dest="out",
help='Name of the output analysis file')
args = parser.parse_args()
sys_txt = '''\
*********************************************************************
System Description
*********************************************************************
'''
et_txt = '''\
*********************************************************************
Event Tree Graph
*********************************************************************
'''
paths_txt = '''\
*********************************************************************
All Paths
*********************************************************************
'''
some_space = '''\n \n'''
"""
===============================================================================
Analysis
===============================================================================
"""
gsystem = get_system (args.system)
t = make_tree(gsystem[2])
tre = t[0] + 'SYSTEM' + ';'
rtree = Tree(tre, format=1)
outfile = open(args.out, "w+")
outfile.write(sys_txt)
outfile.write(gsystem[0].to_string())
outfile.write(some_space)
outfile.write(et_txt)
outfile.write(rtree.get_ascii(show_internal=True))
outfile.write(some_space)
outfile.write(paths_txt)
outfile.write('\n')
m = 1
for j in gsystem[3]:
n = 'Path ' + str(m) + ' = '
outfile.write(n + str(j) + '\n')
m = m + 1