-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-extended-boolean-fault-tree-analysis.py
60 lines (51 loc) · 2.83 KB
/
2-extended-boolean-fault-tree-analysis.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
#!/usr/bin/python
import argparse
import networkx as nx
import json
import cutsets
from lib.probability_tools import node_probability
from lib.plot_tools import to_precision
def main():
parser = argparse.ArgumentParser(description="Generate canonical form, mocus, fit scores")
parser.add_argument("-H", "--Help",
help="2-extended-boolean-fault-tree-analysis.py -i <inputfile> [--no-mocus] [--top-only]",
required=False,
default="")
parser.add_argument("-i", "--input", help="graphml file for analysis", required=True, default="")
parser.add_argument("-n", "--no-mocus", help="disable mocus calculation", action='store_true')
parser.add_argument("-t", "--top-fit-only", help="only calculate system fit score", action='store_true')
argument = parser.parse_args()
# Extract tree from graphml file
tree = nx.read_graphml(argument.input)
# Extract nodes from tree, of form {'node key': {node name, FIT%, operator, [children]}, ...}
nodes = {key: {'name': value['name'], # Node name
'FIT%': value['FIT%'], # FIT% score
'operator': value['operator'], # Operator
'children': [edge[0] for edge in tree.edges() if edge[1] == key]} # Children
for key, value in zip(
[node[0] for node in tree.nodes(data=True)], # Node keys from graphml xml tag
[json.loads(node[1]['label']) for node in tree.nodes(data=True)])} # Node data from graphml labels
# Construct fault tree of form [(node id, node operator, [node parents]), ...]
fault_tree = [
(nodes[node]['name'], nodes[node]['operator'],
[nodes[edge[0]]['name'] for edge in tree.edges() if edge[1] == node])
for node in nodes if nodes[node]['operator'] != 'None']
if not argument.no_mocus:
# Calculate the smallest sets of nodes that, by failing, will bring the system down.
print("Minimum Cut Sets:", *cutsets.mocus(fault_tree), sep='\n')
# Calculate FIT% for non-leaf-nodes
while any([nodes[node]['FIT%'] == 'None' for node in nodes]):
for node in nodes:
if nodes[node]['FIT%'] == 'None' and all(
[nodes[child]['FIT%'] != 'None' for child in nodes[node]['children']]):
nodes[node]['FIT%'] = node_probability(nodes[node]['operator'],
[nodes[child]['FIT%'] for child in nodes[node]['children']])
if argument.top_fit_only:
for node in nodes:
if nodes[node]['name'] == "TOP":
print("System FIT%: ", to_precision(nodes[node]['FIT%'], 6))
else:
print("\nNode FIT% scores:",
*[nodes[node]['name'] + ' - ' + to_precision(nodes[node]['FIT%'], 4) for node in nodes], sep='\n')
if __name__ == "__main__":
main()