-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_processing_utils.py
More file actions
57 lines (48 loc) · 1.86 KB
/
file_processing_utils.py
File metadata and controls
57 lines (48 loc) · 1.86 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
import numpy as np
import scipy.io
import os
import tqdm
# this function takes path to a file and returns category eg. 'bike' or 'car'
def category(filepath):
filename = os.path.basename(filepath)
dirname = os.path.dirname(filepath)
category = os.path.basename(dirname)
return filename, category
def get_file_len(path):
file = scipy.io.loadmat(path)
filename, vehicle_category = category(path)
return {
"filename": filename,
"content": file,
"len": len(file["Rcr"]),
"vehicle_category": vehicle_category,
}
# this function gets list of paths to .mat files and calculates max length of read (number of samples)
def get_max_len(files_to_process):
files = []
max_value = float("-inf")
categories = set()
for element_path in tqdm.tqdm(files_to_process, desc="Reading files"):
file = get_file_len(element_path)
files.append(file)
categories.add(file["vehicle_category"])
if file["len"] > max_value:
max_value = file["len"]
return max_value, files, categories
# this function gets loaded .mat file and needed size of read, pads arrays - return shape (1801, 12)
def expand_element(file, measured_quantity, needed_size):
meas_array = file[measured_quantity]
no_elements_to_pad = needed_size - len(meas_array[:, 0])
rows, cols = meas_array.shape
lisf_for_padded_cols = []
for i in range(cols):
lisf_for_padded_cols.append(
np.pad(
meas_array[:, i], (0, no_elements_to_pad), "constant", constant_values=0
)
)
return np.array(lisf_for_padded_cols)
def prepare_expanded_file(file, len):
expanded_reads_rcr = expand_element(file["content"], "Rcr", len)
expanded_reads_xcr = expand_element(file["content"], "Xcr", len)
return [file["filename"], expanded_reads_rcr, expanded_reads_xcr]