-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.py
129 lines (108 loc) · 3.84 KB
/
array.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""Utility functions to process array-like data and csv file."""
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings("ignore", category=np.VisibleDeprecationWarning)
def collect_data(par_list: list):
"""Collect data from detection.
Args:
par_list: List of recorded parameter.
Return:
par_arr: Numpy array of parameter
par_count: Numpy array of counter
"""
# Convert list to numpy array
par_arr = np.array(par_list)
# Create list of ratio between zero and non-zero values
non_zero = list(par_arr[par_arr != 0])
all_zero = list(par_arr[par_arr == 0])
par_ratio = [len(non_zero), len(all_zero)]
# Create array of non-zero values
non_zero.insert(0, 0)
par_arr = np.array(non_zero)
# Create array to count number of detection
par_count = np.arange(0, len(par_arr), 1)
# Return numpy arrays
return par_arr, par_count, par_ratio
def stack_array(list_a: list, list_b: list, *args):
"""Stack multiple list together and convert it to array.
Args:
list_a: the first list to be stacked
list_b: the second list to be stacked
Return:
A numpy array stack of inputs
"""
array_a = np.array(list_a)
array_b = np.array(list_b)
final_stack = np.stack((array_a, array_b), axis=-1)
for other_list in args:
array_c = np.array(other_list)
# Checks if list contains another iterables
try:
inside_list_size = len(other_list[0])
except TypeError:
array_c = array_c.reshape(len(array_c), 1)
else:
if isinstance(other_list[0], str):
array_c = array_c.reshape(len(array_c), 1)
else:
array_c = array_c.reshape(len(array_c), inside_list_size)
final_stack = np.hstack((final_stack, array_c))
return final_stack
def create_csv(stacked_arr, method: str, det_type: str, filename: str):
"""Create a CSV file from stacked array.
Args:
stacked_arr: Array of stacked parameters.
method: computer vision method (traditional or deeplearning)
det_type: Type of detections (color, shape, or category)
filename: Name of the CSV file.
"""
# Define CSV Columns
columns = ["Delay", "FPS", "Latency (Regs)", "Latency (Coils)", "Detected"]
if method == "deeplearning":
columns.extend(["Probability"])
if det_type == "color":
columns.extend(
[
"Blue (Pred)",
"Green (Pred)",
"Red (Pred)",
"Blue (True)",
"Green (True)",
"Red (True)",
]
)
elif det_type == "shape":
columns.extend(
[
"Height (Pred)",
"Width (Pred)",
"Size (Pred)",
"Height (True)",
"Width (True)",
"Size (True)",
]
)
else:
columns.extend(["Prediction", "True Label"])
elif method == "traditional":
if det_type == "color":
columns.extend(["Blue", "Green", "Red"])
elif det_type == "shape":
columns.extend(["Area", "Points"])
columns.extend(["Prediction", "Label"])
# Create DataFrame and export to CSV
recorded_data = pd.DataFrame(stacked_arr, columns=columns)
recorded_data.to_csv(filename, index=False)
def reset_list(*args):
"""Clear the content of all lists."""
for this_list in args:
this_list.clear()
def update_list(all_list: list, all_values: list):
"""Update list with newest value.
Args:
all_list: all list to be updated
all_values: values to update list
"""
for i, this_list in enumerate(all_list):
this_list.append(all_values[i])