-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
50 lines (42 loc) · 1.98 KB
/
main.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
from brukeropusreader import read_file
import glob
import os
import shutil
from pathlib import Path
from decimal import Decimal, ROUND_HALF_EVEN
# Find manually converted .dpt file to take X-Axis values from
dptTemplate = glob.glob("dpt_template/*.dpt")[0]
# Read through .dpt template line by line, extracting the X-Axis values
with open(dptTemplate, "r") as file:
dptArray = file.readlines()
xValues = list(map(lambda line: line[0: line.index(",")], dptArray))
# Store all /raw_data subdirectories and files in lists
subdirs = [x[0] for x in os.walk("raw_data")]
filesAndDirs = list(map(lambda dir: glob.glob(dir + "/*"), subdirs))
flatFilesAndDirs = [item for sublist in filesAndDirs for item in sublist]
files = list(filter(lambda filtered: os.path.isfile(filtered), flatFilesAndDirs))
dirs = list(filter(lambda filtered: os.path.isdir(filtered), flatFilesAndDirs))
# Remove /converted_data folder
try:
shutil.rmtree("converted_data")
except:
print("could not remove /converted_data, directory not does exist")
# Calculate files and directories that need to be created
targetDirs = list(map(lambda dir: dir.replace("raw_data", "converted_data", 1), dirs))
targetFiles = list(map(lambda file: file.replace("raw_data", "converted_data", 1), files))
targetDirs.append("converted_data")
# Create all target directories
for dir in targetDirs:
try:
Path(dir).mkdir(parents=True, exist_ok=True)
except:
print("Could not create directory: " + dir)
# Read binary OPUS files and extract ScSm data, lining up each value with it's
# corresponding X-Axis value, and save each row as an X-Axis,Y-Axis pair
for fileIndex, binaryFile in enumerate(files):
opus_data = read_file(binaryFile)
newFileName = targetFiles[fileIndex] + ".dpt"
with open(newFileName, "w") as newFile:
for index, yValue in enumerate(opus_data["ScSm"]):
newFile.write(str(xValues[index]) + "," + str(Decimal(str(yValue)).quantize(Decimal('.00001'), rounding=ROUND_HALF_EVEN)) + "\n")
print("Created file: " + newFileName)