-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature.py
More file actions
300 lines (270 loc) · 9.54 KB
/
feature.py
File metadata and controls
300 lines (270 loc) · 9.54 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import pandas as pd
import numpy as np
class Feature:
def __init__(
self,
subject_id,
data,
start_time,
end_time,
labeled_data,
prepared_used=False) -> None:
self.__subject_id = subject_id
self.__data = data
self.__start_time = start_time
self.__end_time = end_time
self.__labeled_data = labeled_data
if prepared_used:
self._normalized_data = data.copy()
@property
def subject_id(self):
return self.__subject_id
@property
def labeled_data(self):
return self.__labeled_data
@property
def normalized_data(self):
if hasattr(self, '_normalized_data'):
return self._normalized_data
else:
self._normalized_data = self.normalize()
return self._normalized_data
def __insert_new_data(self, data, idx, time, val):
"""Inserts new row into DataFrame
Parameters
----------
data : pd.DataFrame
used DataFrame
idx : int
row index for insertion
time : float/int
time/first value
val : list
rest of the (feature) values
Returns
-------
pd.DataFrame
DataFrame with inserted row
"""
if idx == -1:
idx = data.shape[0] + 1
line = pd.DataFrame([[time, *val]])
df2 = pd.concat([data.iloc[:idx], line, data.iloc[idx:]]
).reset_index(drop=True)
return df2
def _parse_data(self):
"""Cuts plus values, if there is no exact start/end, automatically fills its place with the gven interval limit
and the value will be equal with the next found value
Returns
-------
_type_
_description_
"""
df = self.__data.loc[(self.__data[0] >= self.__start_time) & (
self.__data[0] <= self.__end_time)]
if df.shape[0] > 0:
if df.iloc[0][0] != self.__start_time:
df = self.__insert_new_data(df, 0, self.__start_time, [
i for i in df.iloc[0][1:]])
if df.iloc[-1][0] != self.__end_time:
df = self.__insert_new_data(
df, -1, self.__end_time, [i for i in df.iloc[-1][1:]])
return df
def __merge_with_labeled(self, data):
"""Merges feature and labeled DataFrames, so if a labeled time does not exist in the feature DataFrame, a new row will be inserted (where time will be from the labeled set, values will be the duplicates of the previous row)
Parameters
----------
data : pd.DataFrame
feature data
Returns
-------
pd.DataFrame
merged data
"""
if data.shape[0] > 0:
times = list(self.__labeled_data[0])
if data.shape[1] == 2:
data.columns = ['A', 'B']
else:
data.columns = ['A', 'B', 'C', 'D']
# Create a new DataFrame with the new 'A' values and matching 'B'
# values
new_df = pd.DataFrame({'A': times})
# Concatenate the original DataFrame with the new DataFrame
concatenated_df = pd.concat([data, new_df]).sort_values(by='A')
# Fill missing values using forward fill
for c in data.columns[1:]:
concatenated_df[c] = concatenated_df[c].fillna(method='ffill')
concatenated_df = concatenated_df.drop_duplicates()
return concatenated_df
else:
data = self.__labeled_data.copy()
for idx in range(data.shape[0]):
data.loc[idx, list(range(data.shape[1]))[1:]] = 0
if data.shape[1] == 2:
data.columns = ['A', 'B']
else:
data.columns = ['A', 'B', 'C', 'D']
return data
def __prepare_data(self):
data = self._parse_data()
return self.__merge_with_labeled(data)
def normalize(self):
"""Calculates the mean of features between label timestamps and normalizes the calculated set
Returns
-------
pd.DataFrame
normalized data with mean values
"""
data = self.__prepare_data()
times = list(self.__labeled_data[0])
lim1 = times[0]
lim2 = times[1]
temp_dict = {lim1: [lim1, data.loc[data['A'] == lim1]['B'].values[0]]}
for i in range(2, len(times)):
temp_dict[lim2] = [
lim2, data[(data['A'] < lim2) & (data['A'] >= lim1)]['B'].mean()]
lim1 = lim2
lim2 = times[i]
temp_dict[lim2] = [
lim2, data[(data['A'] < lim2) & (data['A'] >= lim1)]['B'].mean()]
# scaler = MinMaxScaler(feature_range=(0, 1))
temp_df = pd.DataFrame(temp_dict).astype("float").transpose()
# dataset = scaler.fit_transform(np.array(temp_df.iloc[:, -1]).reshape(-1, 1))
# temp_df.iloc[:, -1] = dataset.flatten()
return temp_df
def find_last_measured_data(self, val):
measurements = list(self.normalized_data.iloc[:, -1])
found = False
for i in range(1, len(measurements), 3):
temp_list = measurements[i:]
if all(p == val for p in temp_list) and len(temp_list) > 0:
found = True
break
if not found:
i = len(measurements)
return i
class HeartRate(Feature):
def __init__(
self,
subject_id,
data,
start_time,
end_time,
labeled_data,
prepared_used=False) -> None:
"""Heart rate class
Parameters
----------
subject_id : int
data : pd.DataFrame
start_time : int
first time value, where legit label was found (use Label_obj.start_time)
end_time : int
last time value, where legit label was found (use Label_obj.start_time)
labeled_data : pd.DataFrame
prepared label data (use Label_obj.prepared_data)
prepared_used : bool, optional
if True loaded data will be saved automatically as prepared, by default False
"""
super().__init__(
subject_id,
data,
start_time,
end_time,
labeled_data,
prepared_used)
class Step(Feature):
def __init__(
self,
subject_id,
data,
start_time,
end_time,
labeled_data,
prepared_used=False) -> None:
"""Step class
Parameters
----------
subject_id : int
data : pd.DataFrame
start_time : int
first time value, where legit label was found (use Label_obj.start_time)
end_time : int
last time value, where legit label was found (use Label_obj.start_time)
labeled_data : pd.DataFrame
prepared label data (use Label_obj.prepared_data)
prepared_used : bool, optional
if True loaded data will be saved automatically as prepared, by default False
"""
super().__init__(
subject_id,
data,
start_time,
end_time,
labeled_data,
prepared_used)
class Motion(Feature):
def __init__(
self,
subject_id,
data,
start_time,
end_time,
labeled_data,
prepared_used=False) -> None:
"""Motion class
Parameters
----------
subject_id : int
data : pd.DataFrame
start_time : int
first time value, where legit label was found (use Label_obj.start_time)
end_time : int
last time value, where legit label was found (use Label_obj.start_time)
labeled_data : pd.DataFrame
prepared label data (use Label_obj.prepared_data)
prepared_used : bool, optional
if True loaded data will be saved automatically as prepared, by default False
"""
super().__init__(
subject_id,
data,
start_time,
end_time,
labeled_data,
prepared_used)
def normalize(self):
"""Calculates the sum of distance between points of features between label timestamps and normalizes the calculated set
Returns
-------
pd.DataFrame
normalized data with sum of distances values
"""
data = self._parse_data()
times = list(self.labeled_data[0])
def dist(x, y): return np.linalg.norm(x - y)
temp_dict = {times[0]: [times[0], 0]}
times_idx = 1
lim = times[times_idx]
p1 = np.array(data.iloc[0][1:])
p2 = np.array(data.iloc[1][1:])
s = 0
for _, row in data.iloc[2:].iterrows():
p2 = np.array(row[1:])
if row[0] <= lim:
s += dist(p1, p2)
else:
temp_dict[lim] = [lim, s]
s = 0
times_idx += 1
lim = times[times_idx]
p1 = p2
for i in range(times_idx, len(times)):
lim = times[i]
temp_dict[lim] = [lim, 0]
# scaler = MinMaxScaler(feature_range=(0, 1))
temp_df = pd.DataFrame(temp_dict).astype("float").transpose()
# dataset = scaler.fit_transform(np.array(temp_df.iloc[:, -1]).reshape(-1, 1))
# temp_df.iloc[:, -1] = dataset.flatten()
return temp_df