-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_file_data_json.py
More file actions
75 lines (66 loc) · 3.18 KB
/
create_file_data_json.py
File metadata and controls
75 lines (66 loc) · 3.18 KB
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
import json
import os
import numpy as np
import argparse
"""
This script processes composite files containing numerical data, extracts sense and anti values, and generates a structured JSON file.
It ensures that the numerical range (xmin, xmax) is consistently managed across multiple files and adjusts offsets accordingly.
The key functions in the script:
1. parse_composite
2. create_file_data_obj
3. main
"""
# Parse a composite file and extract sense and anti-sense data
def parse_composite(composite_fn):
xmin = None
xmax = None
offset = 0
with open(composite_fn, 'r') as f:
for line in f:
if not line.strip():
continue
fields = line.split('\t')
if fields[0] == '' or fields[0] == 'NAME':
xmin_curr = int(fields[1])
xmax_curr = int(fields[-1])
if xmin_curr == 0:
xmin_curr -= xmax_curr // 2
xmax_curr -= xmax_curr // 2
if xmin is None or xmax is None:
xmin = xmin_curr
xmax = xmax_curr
sense = np.zeros(xmax_curr - xmin_curr + 1, dtype=np.float64)
anti = np.zeros(xmax_curr - xmin_curr + 1, dtype=np.float64)
else:
if xmin_curr < xmin:
sense = np.concatenate((np.zeros(xmin - xmin_curr, dtype=np.float64), sense))
anti = np.concatenate((np.zeros(xmin - xmin_curr, dtype=np.float64), anti))
xmin = xmin_curr
if xmax_curr > xmax:
sense = np.concatenate((sense, np.zeros(xmax_curr - xmax, dtype=np.float64)))
anti = np.concatenate((anti, np.zeros(xmax_curr - xmax, dtype=np.float64)))
xmax = xmax_curr
offset = xmin_curr - xmin
elif 'sense' in fields[0].lower():
for i in range(1, len(fields)):
sense[offset + i - 1] += float(fields[i])
elif 'anti' in fields[0].lower():
for i in range(1, len(fields)):
anti[offset + i - 1] += float(fields[i])
else:
for i in range(1, len(fields)):
sense[offset + i - 1] += float(fields[i]) / 2
anti[offset + i - 1] += float(fields[i]) / 2
return {'xmin': xmin, 'xmax': xmax, 'sense': list(sense), 'anti': list(anti)}
# create a dictionary of file data by parsing multiple composite files
def create_file_data_obj(file_list):
return {os.path.basename(fn): parse_composite(fn) for fn in file_list}
# Adding argparse arguments for user flexibiity
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Create a file data JSON file')
parser.add_argument('--composite-files', type=str, nargs='*', required=True, help='List of composite .out files')
parser.add_argument('--output', type=str, required=True, help='Output JSON file name')
args = parser.parse_args()
file_data_obj = create_file_data_obj(args.composite_files)
with open(args.output, 'w') as f:
json.dump(file_data_obj, f, indent=4)