-
Notifications
You must be signed in to change notification settings - Fork 1
/
augment.py
188 lines (149 loc) · 5.03 KB
/
augment.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
from argparse import ArgumentParser
from functools import partial
import glob
import os
import re
from typing import List, Text, Tuple
import pandas as pd
from ocddetection import data
from ocddetection.data import augmentation
def __arg_parser() -> ArgumentParser:
parser = ArgumentParser()
parser.add_argument('path', type=str)
parser.add_argument('output', type=str)
parser.add_argument('--num-repetitions', type=int, default=3)
parser.add_argument('--include-original', dest='include_original', action='store_const', const=True, default=True)
parser.add_argument('--exclude-original', dest='include_original', action='store_const', const=False, default=True)
return parser
def __get_files(pattern, root: Text) -> Tuple[List[Text], List[pd.DataFrame]]:
ids = []
files = []
for path in glob.iglob(root):
matched = pattern.match(os.path.basename(path))
if matched:
ids.append(int(matched.group(2)))
files.append(augmentation.read_dat(path))
return ids, files
def __write_augmented(df: pd.DataFrame, output: Text, subject: int, run: int) -> None:
df.drop(data.MID_LEVEL_COLUMN, axis=1).to_csv(
os.path.join(output, f'S{subject}-ADL{run}-AUGMENTED.csv'),
index=False,
header=False
)
def __write_meta(df: pd.DataFrame, output: Text, subject: int, run: int) -> None:
df.to_csv(
os.path.join(output, f'S{subject}-ADL{run}-META.csv'),
index=True,
columns=[data.MID_LEVEL_COLUMN, 'ocd'],
header=['activity', 'ocd']
)
def main() -> None:
args = __arg_parser().parse_args()
# Outer
outer_state = augmentation.Stateful(
'outer',
'Outer Action',
None,
None
)
# Clean Table
clean_table_state_machine = augmentation.one_state_action_state_machine_fn(
'clean_table',
'Clean Table',
outer_state
)
# Drawers
drawer_one_state_machine = augmentation.two_state_action_state_machine_fn(
'drawer_one',
'Open Drawer 1',
'Close Drawer 1',
outer_state
)
drawer_two_state_machine = augmentation.two_state_action_state_machine_fn(
'drawer_two',
'Open Drawer 2',
'Close Drawer 2',
outer_state
)
drawer_three_state_machine = augmentation.two_state_action_state_machine_fn(
'drawer_three',
'Open Drawer 3',
'Close Drawer 3',
outer_state
)
# Dishwasher
dishwasher_state_machine = augmentation.two_state_action_state_machine_fn(
'dishwasher',
'Open Dishwasher',
'Close Dishwasher',
outer_state
)
# Fridge
fridge_state_machine = augmentation.two_state_action_state_machine_fn(
'fridge',
'Open Fridge',
'Close Fridge',
outer_state
)
# Subject 1
subject_one_state_machine = partial(
augmentation.action_state_machine,
state_machine_fn={
'drawer_one': drawer_one_state_machine,
'drawer_two': drawer_two_state_machine,
'drawer_three': drawer_three_state_machine
},
outer_state=outer_state
)
subject_one_collect_fn = partial(
augmentation.collect_actions,
state_machine_fn=subject_one_state_machine,
outer_state=outer_state
)
# Subject 2
subject_two_state_machine = partial(
augmentation.action_state_machine,
state_machine_fn={
'clean_table': clean_table_state_machine,
'fridge': fridge_state_machine
},
outer_state=outer_state
)
subject_two_collect_fn = partial(
augmentation.collect_actions,
state_machine_fn=subject_two_state_machine,
outer_state=outer_state
)
# Subject 3
subject_three_state_machine = partial(
augmentation.action_state_machine,
state_machine_fn={
'dishwasher': dishwasher_state_machine,
'fridge': fridge_state_machine
},
outer_state=outer_state
)
subject_three_collect_fn = partial(
augmentation.collect_actions,
state_machine_fn=subject_three_state_machine,
outer_state=outer_state
)
collect_fns = [
subject_one_collect_fn,
subject_two_collect_fn,
subject_three_collect_fn,
None
]
file_regex = re.compile(f'S(\d)-ADL(\d).dat')
for subject, collect_fn in enumerate(collect_fns, start=1):
ids, adls = __get_files(file_regex, os.path.join(args.path, f'S{subject}-ADL?.dat'))
if collect_fn:
drill = augmentation.read_dat(os.path.join(args.path, f'S{subject}-Drill.dat'))
augmented = augmentation.augment(adls, drill, collect_fn, args.num_repetitions, args.include_original)
else:
augmented = [adl.assign(ocd=0) for adl in adls]
for run, df in zip(ids, augmented):
__write_augmented(df, args.output, subject, run)
__write_meta(df, args.output, subject, run)
if __name__ == "__main__":
main()