-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpostprocess_merge_duplicate_nodes.py
53 lines (43 loc) · 2.19 KB
/
postprocess_merge_duplicate_nodes.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
'''
Merge nodes that have identical paths by adding conditional statments to the attribute descriptions.
'''
import re
import sys
from collections import Counter
from dicom_standard import parse_lib as pl
DUPLICATE_PATH_EXCEPTIONS = ['enhanced-mr-image:300a012c']
def add_conditional_to_description(node):
conditional = node.get('conditional')
assert conditional is not None, f'Duplicate attribute (path: {node["path"]}) has no conditional statement.'
conditional = re.sub(r'\.$', ':', conditional)
formatted_conditional = f'<p style="font-weight: bold">{conditional[0].upper()}{conditional[1:]}</p>'
node['description'] = formatted_conditional + node['description']
def is_duplicate_node(path, node_list):
instances = filter(lambda n: n['path'] == path, node_list)
descriptions = map(lambda n: n['description'], instances)
return len(set(descriptions)) > 1
def merge_duplicate_nodes(node_list):
path_list = [d['path'] for d in node_list]
duplicate_paths = [k for k, v in Counter(path_list).items() if v > 1]
path_to_node = {}
for node in node_list:
path = node['path']
if path in path_to_node:
if path not in DUPLICATE_PATH_EXCEPTIONS:
# Add conditional to description only if the duplicates do not have identical descriptions
if is_duplicate_node(path, node_list):
add_conditional_to_description(node)
path_to_node[path]['description'] += node['description']
path_to_node[path]['externalReferences'].extend(node['externalReferences'])
else:
if path in duplicate_paths and path not in DUPLICATE_PATH_EXCEPTIONS:
# Add conditional to description only if the duplicates do not have identical descriptions
if is_duplicate_node(path, node_list):
add_conditional_to_description(node)
path_to_node[path] = node
path_to_node[path].pop('conditional', None)
return list(path_to_node.values())
if __name__ == "__main__":
node_list = pl.read_json_data(sys.argv[1])
processed_node_list = merge_duplicate_nodes(node_list)
pl.write_pretty_json(processed_node_list)