-
Notifications
You must be signed in to change notification settings - Fork 1
/
systemUtils.py
103 lines (87 loc) · 3.48 KB
/
systemUtils.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
import os, errno
import glob
import time as ti
import scipy.io as sio
from collections.abc import Mapping
import numpy as np
# Prints the sub directories for given directory, asks the user to select one and returns the selected path name
def dir_select(directory):
print("\nPlease select a directory")
idx = 0
dir_names = []
for name in os.listdir(path=directory):
if "calibration_files" in name:continue
if os.path.isdir(directory + "\\" + name):
dir_names.append(name)
print("[" + str(idx) + "]" + name)
idx += 1
selection = input()
if not selection: selection = 0
selected_directory = directory + "\\" + dir_names[int(selection)] + "\\"
return selected_directory
# Replaces the keys in a dictionary
def rec_key_replace(obj):
if isinstance(obj, Mapping):
return {key.replace('/', '_'): rec_key_replace(val) for key, val in obj.items()}
return obj
# Takes a dictonary and formats it so it can be saved to matLab correctly
def saveToMat(obj):
# obj = obj.item()
mat = rec_key_replace(obj)
return mat
# Creates a directory if it does not exist
def create_dir(directory):
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise
return
# Prints the .np[zy] files pressent in the specified directory and returns the file name selected by the user.
# If no selection, return the first filename of the list.
# if 'all', return a list with all the filenames.
def file_select(directory):
# File selection
print("\nPlease enter the name of the file with the raw data: ")
idx = 0
file_names = []
print(directory)
for name in sorted(glob.glob(directory + '/*.np[yz]')):
if "_Cal" in name: continue
file_names.append(name)
print("[" + str(idx) + "]" + name.rsplit('\\', 1)[1].rsplit('.', 1)[0])
idx += 1
selection = input()
if not selection: return [file_names[0]]
elif selection == "all": return file_names
else: return [file_names[int(selection)]]
def file_save(data, filename, directory, extention=[]):
currentDTime = ti.strftime('%y%m%d-%H%M')
currentDay = ti.strftime('%y%m%d')
# For the new measurement
if not extention:
# Main measurement directory creation
directory += currentDay
create_dir(directory)
# Filename creation for the current meassurement
postFilename = directory + "\\" + filename + "_" + currentDTime
np.save(postFilename, data)
# MatLab directory creation and export file
matsave = saveToMat(data)
mat_directory = directory + "\\matLab"
create_dir(mat_directory)
mat_filename = mat_directory + "\\" + filename + currentDTime
sio.savemat(mat_filename, {"object": matsave})
# For the postProcess scripts
else:
# Directory creation
directory += extention + "\\"
create_dir(directory)
mat_directory = directory + "\\matLab_" + extention
create_dir(mat_directory)
filename = directory + "\\" + filename.rsplit('\\', 1)[1].rsplit('.', 1)[0] + "_" + extention + "_" + currentDTime
mat_filename = mat_directory + "\\" + filename.rsplit('\\', 1)[1].rsplit('.', 1)[0] + "_" + extention + "_" + currentDTime
np.save(filename, data)
matsave = saveToMat(data)
sio.savemat(mat_filename, {"object": matsave})
return