-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsteps.py
1070 lines (924 loc) · 35.8 KB
/
steps.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from typing import Callable, List, Union
import numpy as np
import pandas as pd
from scipy import signal, interpolate
from scipy import constants
import tqdm
from typing import Tuple
import random
from scipy.interpolate import interp1d
# Set seed to numpy, random, pandas, and scipy for reproducibility
random.seed(42)
np.random.seed(42)
# pd.np.random.seed(42)
"""This module contains the classes that will be used to transform the data. The classes are callable objects, that is, they implement the __call__ method.
The __call__ method receives a dataframe as a parameter and returns a dataframe.
The classes are used to create a pipeline of transformations, which will be applied in the order they were added.
The pipeline is created using the Pipeline class, which receives a list of transformations as a parameter.
The transformations must be callable objects, that is, that implement the __call__ method.
The __call__ method must receive a dataframe as a parameter and return a dataframe.
"""
class FilterByCommonRows:
"""Filter the dataframe to only have rows that are present in both dataframes."""
def __init__(self, match_columns: Union[str, List[str]]):
self.match_columns = (
match_columns
if isinstance(match_columns, list)
else [match_columns]
)
"""Filter the dataframe to only have rows that are present in both dataframes.
Parameters
----------
match_columns : Union[str, List[str]]
Name of the column(s) to be used to filter the dataframe.
Returns
-------
Tuple[pd.DataFrame, pd.DataFrame]
The filtered dataframes.
"""
def __call__(
self, df1: pd.DataFrame, df2: pd.DataFrame
) -> Tuple[pd.DataFrame, pd.DataFrame]:
"""Filter the dataframe to only have rows that are present in both dataframes.
Parameters
----------
df1 : pd.DataFrame
First dataframe to be filtered.
df2 : pd.DataFrame
Second dataframe to be filtered.
Returns
-------
Tuple[pd.DataFrame, pd.DataFrame]
The filtered dataframes.
"""
common_rows = set(
df1[self.match_columns].itertuples(index=False, name=None)
) & set(df2[self.match_columns].itertuples(index=False, name=None))
df1_filtered = df1[
df1[self.match_columns].apply(tuple, axis=1).isin(common_rows)
]
df2_filtered = df2[
df2[self.match_columns].apply(tuple, axis=1).isin(common_rows)
]
return df1_filtered, df2_filtered
class SplitGuaranteeingAllClassesPerSplit:
"""Split the dataframe in a way that all classes are present in both splits."""
def __init__(
self,
column_to_split: str = "user",
class_column: str = "standard activity code",
train_size: float = 0.8,
random_state: int = None,
retries: int = 10,
):
""" "
Parameters
----------
column_to_split : str, optional
Name of the column to be used to split the dataframe, by default "user"
class_column : str, optional
Name of the column that contains the class, by default "standard activity code"
train_size : float, optional
Percentage of the dataframe that will be used for training, by default 0.8
random_state : int, optional
Random state to be used, by default None
retries : int, optional
Number of retries to be used, by default 10
Raises
------
ValueError
If it is not possible to split the dataframe in a way that all classes are present in both splits.
Returns
-------
Tuple[pd.DataFrame, pd.DataFrame]
The filtered dataframes.
"""
self.column_to_split = column_to_split
self.class_column = class_column
self.train_size = train_size
self.random_state = random_state
self.retries = retries
def __call__(
self, dataframe: pd.DataFrame
) -> Tuple[pd.DataFrame, pd.DataFrame]:
"""Split the dataframe in a way that all classes are present in both splits.
Parameters
----------
dataframe : pd.DataFrame
Dataframe to be splitted.
Raises
------
ValueError
If it is not possible to split the dataframe in a way that all classes are present in both splits.
Returns
-------
Tuple[pd.DataFrame, pd.DataFrame]
The filtered dataframes.
"""
random.seed(self.random_state)
split_values = dataframe[self.column_to_split].unique() # user ids
class_values = dataframe[self.class_column].unique() # activity codes
for _ in range(self.retries):
random.shuffle(split_values)
train_values = split_values[
: int(len(split_values) * self.train_size)
]
test_values = split_values[
int(len(split_values) * self.train_size) :
]
train_df = dataframe.loc[
dataframe[self.column_to_split].isin(train_values)
]
test_df = dataframe.loc[
dataframe[self.column_to_split].isin(test_values)
]
if len(train_df[self.class_column].unique()) != len(class_values):
continue
if len(test_df[self.class_column].unique()) != len(class_values):
continue
return train_df.reset_index(drop=True), test_df.reset_index(
drop=True
)
raise ValueError(
"Could not split dataframe in a way that all classes are present in both splits"
)
class BalanceToMinimumClassAndUser:
"""Balance the dataframe to the minimum class size per user. User without a minimum class size will be discarded."""
def __init__(
self,
class_column: str = "standard activity code",
filter_column: str = "user",
random_state: int = 42,
min_value: int = None,
):
"""
Parameters
----------
class_column : str, optional
Name of the column that contains the class, by default "standard activity code"
filter_column : str, optional
Name of the column that contains the user, by default "user"
random_state : int, optional
Random state to be used, by default 42
min_value : int, optional
Minimum size of the class, by default None
"""
self.class_column = class_column
self.random_state = random_state
self.min_value = min_value
self.filter_column = filter_column
def __call__(self, dataframe: pd.DataFrame) -> pd.DataFrame:
"""Balance the dataframe to the minimum class size per user. User without a minimum class size will be discarded.
Parameters
----------
dataframe : pd.DataFrame
Dataframe to be balanced.
Returns
-------
pd.DataFrame
Balanced dataframe.
"""
class_values = dataframe[self.class_column].unique()
min_value_size = self.min_value
# First we need to filter the dataframe to only have filter column values that are present in all classes
filter_values = dataframe[self.filter_column].unique()
filter_values_to_use = []
for filter_value in filter_values:
if sorted(
dataframe[dataframe[self.filter_column] == filter_value][
self.class_column
].unique()
) == sorted(class_values):
filter_values_to_use.append(filter_value)
df = dataframe[
dataframe[self.filter_column].isin(filter_values_to_use)
].copy()
# Now we can balance the dataframe
if self.min_value is None:
min_value_size = min(
[
len(
df.loc[
(df[self.class_column] == class_value)
& (df[self.filter_column] == filter_value)
]
)
for class_value in class_values
for filter_value in filter_values_to_use
]
)
balanced_df = pd.concat(
[
df.loc[
(df[self.class_column] == class_value)
& (df[self.filter_column] == filter_value)
].sample(min_value_size, random_state=self.random_state)
for class_value in class_values
for filter_value in filter_values_to_use
]
)
return balanced_df
class BalanceToMinimumClass:
"""Balance the dataframe to the minimum class size."""
def __init__(
self,
class_column: str = "standard activity code",
random_state: int = 42,
min_value: int = None,
):
"""
Parameters
----------
class_column : str, optional
Name of the column that contains the class, by default "standard activity code"
random_state : int, optional
Random state to be used, by default 42
min_value : int, optional
Minimum size of the class, by default None
"""
self.class_column = class_column
self.random_state = random_state
self.min_value = min_value
def __call__(self, dataframe: pd.DataFrame) -> pd.DataFrame:
"""Balance the dataframe to the minimum class size.
Parameters
----------
dataframe : pd.DataFrame
Dataframe to be balanced.
Returns
-------
pd.DataFrame
Balanced dataframe.
"""
class_values = dataframe[self.class_column].unique()
min_valuse_size = self.min_value
if min_valuse_size is None:
min_valuse_size = min(
[
len(
dataframe.loc[
dataframe[self.class_column] == class_value
]
)
for class_value in class_values
]
)
balanced_df = pd.concat(
[
dataframe.loc[
dataframe[self.class_column] == class_value
].sample(min_valuse_size, random_state=self.random_state)
for class_value in class_values
]
)
return balanced_df
class Interpolate:
"""Interpolate columns of the dataframe assuming that the data is at a fixed frequency.
Uses the `scipy.interpolate` function to interpolate the data."""
def __init__(
self,
groupby_column: Union[str, List[str]],
features_to_select: Union[str, List[str]],
original_fs: float,
target_fs: float,
kind: str = "cubic",
):
"""
Parameters
----------
groupby_column : Union[str, List[str]]
Name of the column(s) to be grouped to resample.
Normally grouped by user event
(otherwise calculates the difference using the entire dataframe, with samples from different events and users).
features_to_select : Union[str, List[str]]
Name of the column(s) to be resampled.
original_fs : float
Original sampling frequency.
target_fs : float
Desired sampling frequency.
kind : str, optional
Type of interpolation to be used, by default 'cubic'.
"""
self.groupby_column = groupby_column
self.features_to_select = (
[features_to_select]
if isinstance(features_to_select, str)
else features_to_select
)
self.original_fs = original_fs
self.target_fs = target_fs
self.kind = kind
def __call__(self, df: pd.DataFrame) -> pd.DataFrame:
"""Interpolate the columns of the dataframe.
Parameters
----------
df : pd.DataFrame
The dataframe to be interpolated.
Returns
-------
pd.DataFrame
The dataframe with the desired columns, interpolated.
"""
df = df.reset_index()
for _, grouped_df in tqdm.tqdm(
df.groupby(self.groupby_column, group_keys=True), desc="Interpoling"
):
for column in self.features_to_select:
signal = grouped_df[column].values
arr = np.array([np.nan] * len(grouped_df))
time = np.arange(0, len(signal), 1) / self.original_fs
interplator = interpolate.interp1d(
time,
signal,
kind=self.kind,
)
new_time = np.arange(0, time[-1], 1 / self.target_fs)
resampled = interplator(new_time)
arr[: len(resampled)] = resampled
df.loc[grouped_df.index, column] = arr
return df.dropna().reset_index(drop=True)
class AddGravityColumn:
"""Add a column with gravity in each axis."""
def __init__(self, axis_columns: List[str], gravity_columns: List[str]):
"""
Parameters
----------
axis_columns : List[str]
Name of the columns that contain the acceleration data.
gravity_columns : List[str]
Name of the column that contains the gravity data.
"""
self.axis_columns = axis_columns
self.gravity_columns = gravity_columns
def __call__(self, df: pd.DataFrame) -> pd.DataFrame:
"""Add a column with gravity in each axis.
Parameters
----------
df : pd.DataFrame
Dataframe to be used.
Returns
-------
pd.DataFrame
Dataframe with the gravity data added.
"""
for axis_col, gravity_col in zip(
self.axis_columns, self.gravity_columns
):
df[axis_col] = df[axis_col] + df[gravity_col]
return df
class Convert_G_to_Ms2:
"""Convert the acceleration from g to m/s²."""
def __init__(
self, axis_columns: List[str], g_constant: float = constants.g
):
"""
Parameters
----------
axis_columns : List[str]
Name of the columns that contain the acceleration data.
g_constant : float, optional
Value of gravity to be added, by default `scipy.constants.g`
"""
self.axis_columns = axis_columns
self.gravity_constant = g_constant
def __call__(self, df: pd.DataFrame) -> pd.DataFrame:
"""Apply the conversion from g to m/s².
Parameters
----------
df : pd.DataFrame
Dataframe to be used.
Returns
-------
pd.DataFrame
Dataframe with the converted acceleration data.
"""
for axis_col in self.axis_columns:
df[axis_col] = df[axis_col] * self.gravity_constant
return df
class ButterworthFilter:
"""Apply the Butterworth filter to remove gravity."""
def __init__(self, axis_columns: List[str], fs: float):
"""
Parameters
----------
axis_columns : List[str]
Name of the columns that contain the acceleration data.
fs : float
Original frequency of the dataset
"""
self.axis_columns = axis_columns
self.fs = fs
def __call__(self, df: pd.DataFrame) -> pd.DataFrame:
"""Apply the Butterworth filter to remove gravity.
Parameters
----------
df : pd.DataFrame
Dataframe to be used.
Returns
-------
pd.DataFrame
Dataframe with the filtered acceleration data (passed filter).
"""
h = signal.butter(3, 0.3, "hp", fs=self.fs, output="sos")
for axis_col in self.axis_columns:
df[axis_col] = signal.sosfiltfilt(h, df[axis_col].values)
return df
class CalcTimeDiffMean:
"""Calc the difference between the time intervals."""
def __init__(
self,
groupby_column: Union[str, List[str]],
column_to_diff: str,
new_column_name: str = "diff",
filter_predicate: Callable[[pd.DataFrame], pd.DataFrame] = None,
):
"""
Parameters
----------
groupby_column : Union[str, List[str]]
Name of the column(s) to be grouped to calculate the difference.
Normally grouped by user event
(otherwise calculates the difference using the entire dataframe, with samples from different events and users).
column_to_diff : str
Name of the column to be used to calculate the difference.
new_column_name : str, optional
Name of the column where the difference will be stored, by default "diff"
filter_predicate : Callable[[pd.DataFrame], pd.DataFrame], optional
Function that filters the dataframe, by default None
"""
self.groupby_column = groupby_column
self.column_to_diff = column_to_diff
self.new_column_name = new_column_name
self.filter_predicate = filter_predicate
def __call__(self, df: pd.DataFrame) -> pd.DataFrame:
"""Calc the difference between the time intervals.
Parameters
----------
df : pd.DataFrame
Dataframe to be used.
Returns
-------
pd.DataFrame
Dataframe with the column with the difference between the time intervals.
If `filter_predicate` is not None, the dataframe will be filtered.
"""
df[self.new_column_name] = df.groupby(self.groupby_column)[
self.column_to_diff
].diff()
df = df.dropna(subset=[self.new_column_name])
if self.filter_predicate:
df = df.groupby(self.groupby_column).filter(self.filter_predicate)
return df.reset_index(drop=True)
class Interp1D:
"""Resample columns of the dataframe assuming that the data is at a fixed frequency.
Uses interpolation to resample the data.
"""
def __init__(
self,
groupby_column: Union[str, List[str]],
features_to_select: Union[str, List[str]],
original_fs: float,
target_fs: float,
):
"""
Parameters
----------
groupby_column : Union[str, List[str]]
Name of the column(s) to be grouped to resample.
Normally grouped by user event
(otherwise calculates the difference using the entire dataframe, with samples from different events and users).
features_to_select : Union[str, List[str]]
Name of the column(s) to be resampled.
original_fs : float
Original sampling frequency.
target_fs : float
Desired sampling frequency.
"""
self.groupby_column = groupby_column
self.features_to_select = (
[features_to_select]
if isinstance(features_to_select, str)
else features_to_select
)
self.original_fs = original_fs
self.target_fs = target_fs
def __call__(self, df: pd.DataFrame) -> pd.DataFrame:
"""
Resample the columns of the dataframe.
Parameters
----------
df : pd.DataFrame
The dataframe to be resampled.
Returns
-------
pd.DataFrame
The dataframe with the desired columns, resampled.
"""
df = df.reset_index()
new_dfs = []
for _, grouped_df in tqdm.tqdm(
df.groupby(self.groupby_column, group_keys=True), desc="Resampling"
):
other_columns = set(df.columns) - set(self.features_to_select)
other_columns_df = grouped_df[list(other_columns)]
column_dfs = []
for column in self.features_to_select:
values = grouped_df[column].values
original_times = np.linspace(0, len(values) / self.original_fs, len(values))
target_times = np.linspace(0, len(values) / self.original_fs, int(len(values) * self.target_fs / self.original_fs))
interp_function = interp1d(original_times, values, kind='linear')
new_values = interp_function(target_times)
new_df = pd.DataFrame(new_values, columns=[column])
column_dfs.append(new_df)
# Concatenate the resampled columns
new_df = pd.concat(column_dfs, axis=1)
# Repeat the other columns to match the length of the resampled columns
repeated_other_columns_df = pd.concat(
[other_columns_df] * (len(new_df) // len(other_columns_df) + 1),
ignore_index=True,
).iloc[: len(new_df), :]
# Merge the resampled columns with the other columns (metadata)
merged_df = pd.concat([new_df, repeated_other_columns_df], axis=1)
new_dfs.append(merged_df)
df = pd.concat(new_dfs).dropna().reset_index(drop=True)
return df
class Resampler:
"""Resample columns of the dataframe assuming that the data is at a fixed frequency.
Uses the `scipy.signal.resample` function to resample the data.
"""
def __init__(
self,
groupby_column: Union[str, List[str]],
features_to_select: Union[str, List[str]],
original_fs: float,
target_fs: float,
):
"""
Parameters
----------
groupby_column : Union[str, List[str]]
Name of the column(s) to be grouped to resample.
Normally grouped by user event
(otherwise calculates the difference using the entire dataframe, with samples from different events and users).
features_to_select : Union[str, List[str]]
Name of the column(s) to be resampled.
original_fs : float
Original sampling frequency.
target_fs : float
Desired sampling frequency.
"""
self.groupby_column = groupby_column
self.features_to_select = (
[features_to_select]
if isinstance(features_to_select, str)
else features_to_select
)
self.original_fs = original_fs
self.target_fs = target_fs
def __call__(self, df: pd.DataFrame) -> pd.DataFrame:
"""
Resample the columns of the dataframe.
Parameters
----------
df : pd.DataFrame
The dataframe to be resampled.
Returns
-------
pd.DataFrame
The dataframe with the desired columns, resampled.
"""
df = df.reset_index()
new_dfs = []
for _, grouped_df in tqdm.tqdm(
df.groupby(self.groupby_column, group_keys=True), desc="Resampling"
):
other_columns = set(df.columns) - set(self.features_to_select)
other_columns_df = grouped_df[list(other_columns)]
column_dfs = []
for column in self.features_to_select:
values = grouped_df[column].values
time = len(values) // self.original_fs
target_time = int(time * self.target_fs)
values = signal.resample(values, target_time)
new_df = pd.DataFrame(values, columns=[column])
column_dfs.append(new_df)
# Concatenate the resampled columns
new_df = pd.concat(column_dfs, axis=1)
# Repeat the other columns to match the length of the resampled columns
repeated_other_columns_df = pd.concat(
[other_columns_df] * (len(new_df) // len(other_columns_df) + 1),
ignore_index=True,
).iloc[: len(new_df), :]
# Merge the resampled columns with the other columns (metadata)
merged_df = pd.concat([new_df, repeated_other_columns_df], axis=1)
new_dfs.append(merged_df)
df = pd.concat(new_dfs).dropna().reset_index(drop=True)
return df
class ResamplerPoly:
"""Resample columns of the dataframe assuming that the data is at a fixed frequency.
Uses the `scipy.signal.resample_poly` function to resample the data.
"""
def __init__(
self,
groupby_column: Union[str, List[str]],
features_to_select: Union[str, List[str]],
up: float,
down: float,
padtype: str = "mean",
):
"""
Parameters
----------
groupby_column : Union[str, List[str]]
Name of the column(s) to be grouped to resample.
Normally grouped by user event
(otherwise calculates the difference using the entire dataframe, with samples from different events and users).
features_to_select : Union[str, List[str]]
Name of the column(s) to be resampled.
up : float
Increase factor of the frequency.
down : float
Frequency reduction factor.
padtype : str, optional
Type of padding, by default 'mean'.
"""
self.groupby_column = groupby_column
self.features_to_select = (
[features_to_select]
if isinstance(features_to_select, str)
else features_to_select
)
self.up = up
self.down = down
self.padtype = padtype
def __call__(self, df: pd.DataFrame) -> pd.DataFrame:
"""Resample the columns of the dataframe.
Parameters
----------
df : pd.DataFrame
The dataframe to be resampled.
Returns
-------
pd.DataFrame
The dataframe with the desired columns, resampled.
"""
df = df.reset_index()
new_dfs = []
for _, grouped_df in tqdm.tqdm(
df.groupby(self.groupby_column, group_keys=True), desc="Resampling"
):
other_columns = set(df.columns) - set(self.features_to_select)
other_columns_df = grouped_df[list(other_columns)]
column_dfs = []
for column in self.features_to_select:
values = grouped_df[column].values
values = signal.resample_poly(
values, up=self.up, down=self.down, padtype=self.padtype
)
new_df = pd.DataFrame(values, columns=[column])
column_dfs.append(new_df)
# Concatenate the resampled columns
new_df = pd.concat(column_dfs, axis=1)
# Repeat the other columns to match the length of the resampled columns
repeated_other_columns_df = pd.concat(
[other_columns_df] * (len(new_df) // len(other_columns_df) + 1),
ignore_index=True,
).iloc[: len(new_df), :]
# Merge the resampled columns with the other columns (metadata)
merged_df = pd.concat([new_df, repeated_other_columns_df], axis=1)
new_dfs.append(merged_df)
df = pd.concat(new_dfs).dropna().reset_index(drop=True)
return df
# return df.dropna().reset_index(drop=True)
class Windowize:
"""Realize the windowing of the data in fixed size windows.
The windowing will be done with consecutive samples of the dataframe and the last window will be discarded.
The desired columns will be transposed (from row to column) in the desired window size.
For the remaining columns, the first element of the window will be kept.
Note: it is assumed here that the window has no overlap and that the sampling rate is constant.
"""
def __init__(
self,
features_to_select: List[str],
samples_per_window: int,
samples_per_overlap: int,
groupby_column: Union[str, List[str]],
divisible_by: int = None,
):
"""
Parameters
----------
features_to_select : List[str]
Features that will be used to perform the windowing
(will be transposed from rows to columns and a suffix of index will be added).
samples_per_window : int
Number of consecutive samples that will be used to perform the windowing.
samples_per_overlap : int
Number of samples that will be overlapped between consecutive windows.
groupby_column : Union[str, List[str]]
Name of the column(s) to be grouped to perform the windowing.
Normally grouped by user event
(otherwise calculates the difference using the entire dataframe, with samples from different events and users).
"""
self.features_to_select = (
features_to_select
if isinstance(features_to_select, list)
else [features_to_select]
)
self.samples_per_window = samples_per_window
self.samples_per_overlap = samples_per_overlap
self.groupby_column = groupby_column
self.divisible_by = divisible_by
def __call__(self, df: pd.DataFrame) -> pd.DataFrame:
"""Perform the windowing on the columns of the dataframe.
Parameters
----------
df : pd.DataFrame
The dataframe to be windowed.
Returns
-------
pd.DataFrame
The dataframe with fixed size windows.
"""
values = []
other_columns = set(df.columns) - set(self.features_to_select)
for key, grouped_df in tqdm.tqdm(
df.groupby(self.groupby_column), desc="Creating windows"
):
for i, start in enumerate(
range(
0,
len(grouped_df),
self.samples_per_window - self.samples_per_overlap,
)
):
window_df = grouped_df[
start : start + self.samples_per_window
].reset_index(drop=True)
if len(window_df) != self.samples_per_window:
continue
if window_df.isnull().values.any():
continue
features = window_df[self.features_to_select].unstack()
features.index = features.index.map(
lambda a: f"{a[0]}-{(a[1])%(self.samples_per_window)}"
)
for column in other_columns:
features[column] = window_df[column].iloc[0]
features["window"] = i
values.append(features)
return pd.concat(values, axis=1).T.reset_index(drop=True)
class AddStandardActivityCode:
"""Add the column "standard activity code" to the dataframe."""
def __init__(self, codes_map: dict):
"""
Parameters
----------
codes_map : dict
Dictionary with the activity code (from the original dataset)
as key and the standard activity code as value
"""
self.codes_map = codes_map
def __call__(self, df: pd.DataFrame) -> pd.DataFrame:
"""
Add the column "standard activity code" to the dataframe.
Parameters
----------
df : pd.DataFrame
The dataframe to be added to the column.
Returns
-------
pd.DataFrame
The dataframe with the column "standard activity code" added.
"""
df["standard activity code"] = df["activity code"].map(self.codes_map)
return df
class RenameColumns:
"""Rename dataframe columns."""
def __init__(self, columns_map: dict):
"""
Parameters
----------
columns_map : dict
Dictionary with the original column names as key and the new column name as value.
"""
self.columns_map = columns_map
def __call__(self, df: pd.DataFrame) -> pd.DataFrame:
"""Rename dataframe columns.
Parameters
----------
df : pd.DataFrame
The dataframe with the columns to be renamed.
Returns
-------
pd.DataFrame
The dataframe with the renamed columns.
"""
df.rename(columns=self.columns_map, inplace=True)
return df
class ConcatenateMultiSample:
"""Concatenate multiple samples in the same dataframe."""
def __init__(
self,
groupby_column: Union[str, List[str]],
features_to_select: Union[str, List[str]],
window_span: int = 5,
window_ovelap: int = 0,
):
self.groupby_column = groupby_column
self.features_to_select = (
features_to_select
if isinstance(features_to_select, list)
else [features_to_select]
)
self.window_span = window_span
self.window_ovelap = window_ovelap
def __call__(self, df: pd.DataFrame) -> pd.DataFrame:
"""Concatenate multiple samples in the same dataframe.
Parameters
----------
df : pd.DataFrame
The dataframe to be concatenated.
Returns
-------
pd.DataFrame
The dataframe with the concatenated samples.
"""
dfs = []
the_names = []
for key, grouped_df in tqdm.tqdm(
df.groupby(self.groupby_column), desc="Concatenating samples"
):
for i, start in enumerate(
range(
0,
len(grouped_df),
self.window_span - self.window_ovelap,
)
):
window_df = grouped_df.iloc[
start : start + self.window_span
].reset_index(drop=True)
if len(window_df) != self.window_span:
continue
values = []
column_names = []
for column in self.features_to_select: