-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocess_non_wear_methods.py
552 lines (420 loc) · 22.8 KB
/
process_non_wear_methods.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
9
8000
0
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
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
# -*- coding: utf-8 -*-
"""
IMPORT PACKAGES
"""
import os
import pandas as pd
import numpy as np
from multiprocessing import cpu_count
from joblib import Parallel
from joblib import delayed
"""
IMPORTED FUNCTIONS
"""
from functions.helper_functions import set_start, set_end, calculate_vector_magnitude, get_subjects_with_invalid_data, load_pickle
from functions.hdf5_functions import get_all_subjects_hdf5, read_metadata_from_group, read_dataset_from_group, save_data_to_group_hdf5, get_datasets_from_group
from functions.datasets_functions import get_actigraph_acc_data, get_actiwave_acc_data, get_actiwave_hr_data, get_actigraph_epoch_data, get_actigraph_epoch_60_data
from functions.plot_functions import plot_non_wear_algorithms
from algorithms.non_wear_time.hecht_2009 import hecht_2009_triaxial_calculate_non_wear_time
from algorithms.non_wear_time.troiano_2007 import troiano_2007_calculate_non_wear_time
from algorithms.non_wear_time.choi_2011 import choi_2011_calculate_non_wear_time
from algorithms.non_wear_time.hees_2013 import hees_2013_calculate_non_wear_time
"""
GLOBAL VARIABLES
"""
ACTIGRAPH_HDF5_FILE = os.path.join(os.sep, 'Volumes', 'LaCie', 'ACTIGRAPH_TU7.hdf5')
# ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE = os.path.join(os.sep, 'Volumes', 'LaCie', 'ACTIWAVE_ACTIGRAPH_MAPPING.hdf5')
ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE = os.path.join(os.sep, 'users', 'shaheensyed', 'hdf5', 'ACTIWAVE_ACTIGRAPH_MAPPING.hdf5')
def batch_process_non_wear_algorithm(algorithm, limit = None, skip_n = 0, use_parallel = True, num_jobs = cpu_count(), save_hdf5 = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE):
"""
Batch process finding non-wear time based on the following algorithms:
- hecht_2009_triaxial_calculate_non_wear_time
- troiano_2007_calculate_non_wear_time
- choi_2011_calculate_non_wear_time
- hees_2013_calculate_non_wear_time
Parameters
-----------
algorithm : python function name
name of the function that processes the non-wear time method
limit : int (optional)
limit the number of subjects to be processed
skip_n : int (optional)
skip first N subjects
use_parallel : Boolean (optional)
Set to true of subjects need to be processed in parallel, this will execute much faster
num_jobs : int (optional)
if parallel is set to true, then this indicates have many jobs at the same time need to be executed. Default set to the number of CPU cores
save_hdf5 : os.path
location of HDF5 file to save data to
"""
# get all the subjects from the hdf5 file and remove subjects with invalid data
subjects = [s for s in get_all_subjects_hdf5(hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE) if s not in get_subjects_with_invalid_data()][0 + skip_n:limit]
logging.info('Start batch processing estimating non-wear time based on {}'.format(algorithm.__name__))
# loop over the subjects
if use_parallel:
# verbose
logging.info('Processing in parallel (parallelization on)')
# use parallel processing to speed up processing time
executor = Parallel(n_jobs = num_jobs, backend = 'multiprocessing')
# create tasks so we can execute them in parallel
tasks = (delayed(algorithm)(subject = subject, idx = i, total = len(subjects), save_hdf5 = save_hdf5) for i, subject in enumerate(subjects))
# execute task
executor(tasks)
else:
# verbose
logging.info('Processing one-by-one (parallelization off)')
# loop over the subjects
for i, subject in enumerate(subjects):
algorithm(subject = subject, idx = i, total = len(subjects), save_hdf5 = save_hdf5)
def process_hecht_2009_triaxial(subject, save_hdf5, idx = 1, total = 1, epoch_dataset = 'epoch10'):
"""
Calculate the non-wear time from a data array that contains the vector magnitude (VMU) according to Hecht 2009 algorithm
Paper:
COPD. 2009 Apr;6(2):121-9. doi: 10.1080/15412550902755044.
Methodology for using long-term accelerometry monitoring to describe daily activity patterns in COPD.
Hecht A1, Ma S, Porszasz J, Casaburi R; COPD Clinical Research Network.
Parameters
---------
subject : string
subject ID
save_hdf5 : os.path
location of HDF5 file to save non wear data to
idx : int (optional)
index of counter, only useful when processing large batches and you want to monitor the status
total: int (optional)
total number of subjects to process, only useful when processing large batches and you want to monitor the status
epoch_dataset : string (optional)
name of dataset within an HDF5 group that contains the 10sec epoch data
"""
logging.info('{style} Processing subject: {} {}/{} {style}'.format(subject, idx, total, style = '='*10))
"""
ACTIGRAPH DATA
"""
# read actigraph acceleration time
_, _, actigraph_time = get_actigraph_acc_data(subject, hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE)
# get start and stop time
start_time, stop_time = actigraph_time[0], actigraph_time[-1]
"""
EPOCH DATA
"""
# check if epoch dataset is part of HDF5 group
if epoch_dataset in get_datasets_from_group(group_name = subject, hdf5_file = ACTIGRAPH_HDF5_FILE):
# get actigraph 10s epoch data
epoch_data, _ , epoch_time_data = get_actigraph_epoch_data(subject, epoch_dataset = epoch_dataset, hdf5_file = ACTIGRAPH_HDF5_FILE)
# convert to 60s epoch data
epoch_60_data, epoch_60_time_data = get_actigraph_epoch_60_data(epoch_data, epoch_time_data)
# calculate epoch 60 VMU
epoch_60_vmu_data = calculate_vector_magnitude(epoch_60_data[:,:3], minus_one = False, round_negative_to_zero = False)
"""
GET NON WEAR VECTOR
"""
# create dataframe of actigraph acceleration
df_epoch_60_vmu = pd.DataFrame(epoch_60_vmu_data, index = epoch_60_time_data, columns = ['VMU']).loc[start_time:stop_time]
# retrieve non-wear vector
epoch_60_vmu_non_wear_vector = hecht_2009_triaxial_calculate_non_wear_time(data = df_epoch_60_vmu.values)
# get the croped time array as int64 (cropped because we selected the previous dataframe to be between start and stop slice)
epoch_60_time_data_cropped = np.array(df_epoch_60_vmu.index).astype('int64')
# reshape
epoch_60_time_data_cropped = epoch_60_time_data_cropped.reshape(len(epoch_60_time_data_cropped), 1)
# add two arrays
combined_data = np.hstack((epoch_60_time_data_cropped, epoch_60_vmu_non_wear_vector))
"""
SAVE TO HDF5 FILE
"""
save_data_to_group_hdf5(group = subject, data = combined_data, data_name = 'hecht_2009_3_axes_non_wear_data', overwrite = True, create_group_if_not_exists = True, hdf5_file = save_hdf5)
else:
logging.warning('Subject {} has no corresponding epoch data, skipping...'.format(subject))
def process_troiano_2007(subject, save_hdf5, idx = 1, total = 1, epoch_dataset = 'epoch10'):
"""
Calculate non wear time by using Troiano 2007 algorithm
Troiano 2007 non-wear algorithm
detects non wear time from 60s epoch counts
Nonwear was defined by an interval of at least 60 consecutive minutes of zero activity intensity counts, with allowance for 1–2 min of counts between 0 and 100
Paper:
Physical Activity in the United States Measured by Accelerometer
DOI:
10.1249/mss.0b013e31815a51b3
Parameters
---------
subject : string
subject ID
save_hdf5 : os.path
location of HDF5 file to save non wear data to
idx : int (optional)
index of counter, only useful when processing large batches and you want to monitor the status
total: int (optional)
total number of subjects to process, only useful when processing large batches and you want to monitor the status
epoch_dataset : string (optional)
name of dataset within an HDF5 group that contains the 10sec epoch data
"""
logging.info('{style} Processing subject: {} {}/{} {style}'.format(subject, idx, total, style = '='*10))
"""
ACTIGRAPH DATA
"""
# read actigraph acceleration time
_, _, actigraph_time = get_actigraph_acc_data(subject, hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE)
# get start and stop time
start_time, stop_time = actigraph_time[0], actigraph_time[-1]
"""
EPOCH DATA
"""
if epoch_dataset in get_datasets_from_group(group_name = subject, hdf5_file = ACTIGRAPH_HDF5_FILE):
# get actigraph 10s epoch data
epoch_data, _ , epoch_time_data = get_actigraph_epoch_data(subject, epoch_dataset = epoch_dataset, hdf5_file = ACTIGRAPH_HDF5_FILE)
# convert to 60s epoch data
epoch_60_data, epoch_60_time_data = get_actigraph_epoch_60_data(epoch_data, epoch_time_data)
# obtain counts values
epoch_60_count_data = epoch_60_data[:,:3]
"""
GET NON WEAR VECTOR
"""
# create dataframe of actigraph acceleration
df_epoch_60_count = pd.DataFrame(epoch_60_count_data, index = epoch_60_time_data, columns = ['X - COUNT', 'Y - COUNT', 'Z - COUNT']).loc[start_time:stop_time]
# retrieve non-wear vector
epoch_60_count_non_wear_vector = troiano_2007_calculate_non_wear_time(data = df_epoch_60_count.values, time = df_epoch_60_count.index.values)
# get the croped time array as int64 (cropped because we selected the previous dataframe to be between start and stop slice)
epoch_60_time_data_cropped = np.array(df_epoch_60_count.index).astype('int64')
# reshape
epoch_60_time_data_cropped = epoch_60_time_data_cropped.reshape(len(epoch_60_time_data_cropped), 1)
# add two arrays
combined_data = np.hstack((epoch_60_time_data_cropped, epoch_60_count_non_wear_vector))
"""
SAVE TO HDF5 FILE
"""
save_data_to_group_hdf5(group = subject, data = combined_data, data_name = 'troiano_2007_non_wear_data', overwrite = True, create_group_if_not_exists = True, hdf5_file = save_hdf5)
else:
logging.warning('Subject {} has no corresponding epoch data, skipping...'.format(subject))
def process_choi_2011(subject, save_hdf5, idx = 1, total = 1, epoch_dataset = 'epoch10'):
"""
Estimate non-wear time based on Choi 2011 paper:
Med Sci Sports Exerc. 2011 Feb;43(2):357-64. doi: 10.1249/MSS.0b013e3181ed61a3.
Validation of accelerometer wear and nonwear time classification algorithm.
Choi L1, Liu Z, Matthews CE, Buchowski MS.
Parameters
---------
subject : string
subject ID
save_hdf5 : os.path
location of HDF5 file to save non wear data to
idx : int (optional)
index of counter, only useful when processing large batches and you want to monitor the status
total: int (optional)
total number of subjects to process, only useful when processing large batches and you want to monitor the status
epoch_dataset : string (optional)
name of dataset within an HDF5 group that contains the 10sec epoch data
"""
logging.info('{style} Processing subject: {} {}/{} {style}'.format(subject, idx, total, style = '='*10))
"""
ACTIGRAPH DATA
"""
# read actigraph acceleration time
_, _, actigraph_time = get_actigraph_acc_data(subject, hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE)
# get start and stop time
start_time, stop_time = actigraph_time[0], actigraph_time[-1]
"""
EPOCH DATA
"""
if epoch_dataset in get_datasets_from_group(group_name = subject, hdf5_file = ACTIGRAPH_HDF5_FILE):
# get actigraph 10s epoch data
epoch_data, _ , epoch_time_data = get_actigraph_epoch_data(subject, epoch_dataset = epoch_dataset, hdf5_file = ACTIGRAPH_HDF5_FILE)
# convert to 60s epoch data
epoch_60_data, epoch_60_time_data = get_actigraph_epoch_60_data(epoch_data, epoch_time_data)
# obtain counts values
epoch_60_count_data = epoch_60_data[:,:3]
"""
GET NON WEAR VECTOR
"""
# create dataframe of actigraph acceleration
df_epoch_60_count = pd.DataFrame(epoch_60_count_data, index = epoch_60_time_data, columns = ['X - COUNT', 'Y - COUNT', 'Z - COUNT']).loc[start_time:stop_time]
# retrieve non-wear vector
epoch_60_count_non_wear_vector = choi_2011_calculate_non_wear_time(data = df_epoch_60_count.values, time = df_epoch_60_count.index.values)
# get the croped time array as int64 (cropped because we selected the previous dataframe to be between start and stop slice)
epoch_60_time_data_cropped = np.array(df_epoch_60_count.index).astype('int64')
# reshape
epoch_60_time_data_cropped = epoch_60_time_data_cropped.reshape(len(epoch_60_time_data_cropped), 1)
# add two arrays
combined_data = np.hstack((epoch_60_time_data_cropped, epoch_60_count_non_wear_vector))
"""
SAVE TO HDF5 FILE
"""
save_data_to_group_hdf5(group = subject, data = combined_data, data_name = 'choi_2011_non_wear_data', overwrite = True, create_group_if_not_exists = False, hdf5_file = save_hdf5)
else:
logging.warning('Subject {} has no corresponding epoch data, skipping...'.format(subject))
def process_hees_2013(subject, save_hdf5, idx = 1, total = 1):
"""
Estimation of non-wear time periods based on Hees 2013 paper
Estimation of Daily Energy Expenditure in Pregnant and Non-Pregnant Women Using a Wrist-Worn Tri-Axial Accelerometer
Vincent T. van Hees, Frida Renström , Antony Wright, Anna Gradmark, Michael Catt, Kong Y. Chen, Marie Löf, Les Bluck, Jeremy Pomeroy, Nicholas J. Wareham, Ulf Ekelund, Søren Brage, Paul W. Franks
Published: July 29, 2011https://doi.org/10.1371/journal.pone.0022922
Accelerometer non-wear time was estimated on the basis of the standard deviation and the value range of each accelerometer axis, calculated for consecutive blocks of 30 minutes.
A block was classified as non-wear time if the standard deviation was less than 3.0 mg (1 mg = 0.00981 m·s−2) for at least two out of the three axes or if the value range, for
at least two out of three axes, was less than 50 mg.
Parameters
---------
subject : string
subject ID
save_hdf5 : os.path
location of HDF5 file to save non wear data to
idx : int (optional)
index of counter, only useful when processing large batches and you want to monitor the status
total: int (optional)
total number of subjects to process, only useful when processing large batches and you want to monitor the status
"""
logging.info('{style} Processing subject: {} {}/{} {style}'.format(subject, idx, total, style = '='*10))
# read actigraph acceleration data
actigraph_acc, *_ = get_actigraph_acc_data(subject, hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE)
# calculate non non wear time based on Hees 2013 algorithm
non_wear_vector = hees_2013_calculate_non_wear_time(actigraph_acc)
# save non-wear vector to HDF5
save_data_to_group_hdf5(group = subject, data = non_wear_vector, data_name = 'hees_2013_non_wear_data', overwrite = True, create_group_if_not_exists = False, hdf5_file = save_hdf5)
def batch_process_plot_non_wear_algorithms(limit = None, skip_n = 0, plot_folder = os.path.join('plots', 'non-wear-time', 'algorithms')):
"""
Batch process finding non-wear time based on Hecht 2009 algorithm
Parameters
-----------
limit : int (optional)
limit the number of subjects to be processed
skip_n : int (optional)
skip first N subjects
"""
# get all the subjects from the hdf5 file (subjects are individuals who participated in the Tromso Study #7
subjects = [s for s in get_all_subjects_hdf5(hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE) if s not in get_subjects_with_invalid_data()][0 + skip_n:limit]
# loop over the subjects
for i, subject in enumerate(subjects):
# verbose
logging.info('Processing subject: {} {}/{}'.format(subject, i, len(subjects)))
# call plot function
process_plot_non_wear_algorithms(subject, plot_folder)
def process_plot_non_wear_algorithms(subject, plot_folder, epoch_dataset = 'epoch10', use_optimized_parameters = True, optimized_data_folder = os.path.join('files', 'grid-search', 'final')):
"""
Plot acceleration data from actigraph and actiwave including the 4 non wear methods and the true non wear time
- plot actigraph XYZ
- plot actiwave YXZ
- plot actiwave heart rate
- plot hecht, choi, troiano, v. Hees
- plot true non wear time
Parameters
---------
subject : string
subject ID
plot_folder : os.path
folder location to save plots to
epoch_dataset : string (optional)
name of hdf5 dataset that contains 10s epoch data
"""
optimized_parameters = {}
classification = 'f1'
for nw_method in ['hecht', 'troiano', 'choi', 'hees']:
# load data
data = load_pickle('grid-search-results-{}'.format(nw_method), optimized_data_folder)
# obtain top parameters
top_results = sorted(data.items(), key = lambda item: item[1][classification], reverse = True)[0]
optimized_parameters[nw_method] = top_results[0]
"""
GET ACTIGRAPH DATA
"""
actigraph_acc, _ , actigraph_time = get_actigraph_acc_data(subject, hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE)
# get start and stop time
start_time, stop_time = actigraph_time[0], actigraph_time[-1]
"""
GET ACTIWAVE DATA
"""
actiwave_acc, _ , actiwave_time = get_actiwave_acc_data(subject, hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE)
actiwave_hr, actiwave_hr_time = get_actiwave_hr_data(subject, hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE)
"""
EPOCH DATA
"""
if epoch_dataset in get_datasets_from_group(group_name = subject, hdf5_file = ACTIGRAPH_HDF5_FILE):
# get 10s epoch data from HDF5 file
epoch_data, _ , epoch_time_data = get_actigraph_epoch_data(subject, epoch_dataset = epoch_dataset, hdf5_file = ACTIGRAPH_HDF5_FILE)
# convert to 60s epoch data
epoch_60_data, epoch_60_time_data = get_actigraph_epoch_60_data(epoch_data, epoch_time_data)
# calculate epoch 60 VMU
epoch_60_vmu_data = calculate_vector_magnitude(epoch_60_data[:,:3], minus_one = False, round_negative_to_zero = False)
"""
GET NON WEAR TIME
"""
# true non wear time
true_non_wear_time = read_dataset_from_group(group_name = subject, dataset = 'actigraph_true_non_wear', hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE)
# hecht 3-axes non wear time
hecht_3_non_wear_time = read_dataset_from_group(group_name = subject, dataset = 'hecht_2009_3_axes_non_wear_data', hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE)
# troiano non wear time
troiano_non_wear_time = read_dataset_from_group(group_name = subject, dataset = 'troiano_2007_non_wear_data', hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE)
# choi non wear time
choi_non_wear_time = read_dataset_from_group(group_name = subject, dataset = 'choi_2011_non_wear_data', hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE)
# hees non wear time
hees_non_wear_time = read_dataset_from_group(group_name = subject, dataset = 'hees_2013_non_wear_data', hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE)
# if set to True, then update the matrix column with non-wear data
if use_optimized_parameters:
"""
READ 60S EPOCH DATA
"""
subject_epoch_data = read_dataset_from_group(group_name = subject, dataset = 'epoch60', hdf5_file = ACTIWAVE_ACTIGRAPH_MAPPING_HDF5_FILE).astype('float16')
subject_epoch_data_vmu = calculate_vector_magnitude(subject_epoch_data, minus_one = False, round_negative_to_zero = False)
"""
HECHT
"""
# unpack variables
t, i, m = optimized_parameters['hecht'].split('-')
# use optimized parameters
hecht_3_non_wear_time[:,1] = hecht_2009_triaxial_calculate_non_wear_time(data = subject_epoch_data_vmu, epoch_sec = 60, threshold = int(t), time_interval_mins = int(i), min_count = int(m))[:,0]
"""
TROIANO
"""
# unpack variables
at, mpl, st, ss, vm = optimized_parameters['troiano'].split('-')
# use optimized variables to calculate non wear vector
troiano_non_wear_time[:,1] = troiano_2007_calculate_non_wear_time(subject_epoch_data, None, activity_threshold = int(at), min_period_len = int(mpl), spike_tolerance = int(st), spike_stoplevel = int(ss), use_vector_magnitude = eval(vm), print_output = False)[:,0]
"""
CHOI
"""
at, mpl, st, mwl, wst, vm = optimized_parameters['choi'].split('-')
choi_non_wear_time[:,1] = choi_2011_calculate_non_wear_time(subject_epoch_data, None, activity_threshold = int(at), min_period_len = int(mpl), spike_tolerance = int(st), min_window_len = int(mwl), window_spike_tolerance = int(wst), use_vector_magnitude = eval(vm), print_output = False)[:,0]
"""
HEES
"""
mw, wo, st, sa, vt, va = optimized_parameters['hees'].split('-')
hees_non_wear_time = hees_2013_calculate_non_wear_time(actigraph_acc, hz = 100, min_non_wear_time_window = int(mw), window_overlap = int(wo), std_mg_threshold = float(st), std_min_num_axes = int(sa) , value_range_mg_threshold = float(vt), value_range_min_num_axes = int(va))
"""
CREATING THE DATAFRAMES
"""
# convert actigraph data to pandas dataframe
df_actigraph_acc = pd.DataFrame(actigraph_acc, index = actigraph_time, columns = ['ACTIGRAPH Y', 'ACTIGRAPH X', 'ACTIGRAPH Z'])
# convert actiwave data to pandas dataframe
df_actiwave_acc = pd.DataFrame(actiwave_acc, index = actiwave_time, columns = ['ACTIWAVE Y', 'ACTIWAVE X', 'ACTIWAVE Z'])
# convert actiwave hr to pandas dataframe
df_actiwave_hr = pd.DataFrame(actiwave_hr, index = actiwave_hr_time, columns = ['ESTIMATED HR'])
# convert 60s epoch vmu to dataframe
df_epoch_60_vmu = pd.DataFrame(epoch_60_vmu_data, index = epoch_60_time_data, columns = ['EPOCH 60s VMU'])
# slice based on start and stop time
df_epoch_60_vmu = df_epoch_60_vmu.loc[start_time:stop_time]
# create dataframe of true non wear time
df_true_non_wear_time = pd.DataFrame(true_non_wear_time, index = actigraph_time, columns = ['TRUE NON WEAR TIME'])
# create dataframe of hecht 3-axes non wear time
df_hecht_3_non_wear_time = pd.DataFrame(hecht_3_non_wear_time[:,1], index = np.asarray(hecht_3_non_wear_time[:,0], dtype ='datetime64[ns]'), columns = ['HECHT-3 NON WEAR TIME'])
# create dataframe of troiano non wear time
df_troiano_non_wear_time = pd.DataFrame(troiano_non_wear_time[:,1], index = np.asarray(troiano_non_wear_time[:,0], dtype = 'datetime64[ns]'), columns = ['TROIANO NON WEAR TIME'])
# create dataframe of choi non wear time
df_choi_non_wear_time = pd.DataFrame(choi_non_wear_time[:,1], index = np.asarray(choi_non_wear_time[:,0], dtype = 'datetime64[ns]'), columns = ['CHOI NON WEAR TIME'])
# create dataframe of hees non wear time
df_hees_non_wear_time = pd.DataFrame(hees_non_wear_time, index = actigraph_time, columns = ['HEES NON WEAR TIME'])
# merge all dataframes
df_joined = df_actigraph_acc.join(df_true_non_wear_time, how='outer').join(df_actiwave_acc, how='outer').join(df_hecht_3_non_wear_time, how='outer') \
.join(df_troiano_non_wear_time, how='outer').join(df_choi_non_wear_time, how='outer').join(df_hees_non_wear_time, how='outer').join(df_epoch_60_vmu, how='outer').join(df_actiwave_hr, how='outer')
# call plot function
plot_non_wear_algorithms(data = df_joined, subject = subject, plot_folder = plot_folder)
if __name__ == "__main__":
# start timer and memory counter
tic, process, logging = set_start()
# 1) batch process Hecht 2009 non-wear method
batch_process_non_wear_algorithm(algorithm = process_hecht_2009_triaxial)
# # 2) batch process Troiano 2007 non-wear method
# batch_process_non_wear_algorithm(algorithm = process_troiano_2007)
# # 3) batch process Choi 2011 non-wear method
# batch_process_non_wear_algorithm(algorithm = process_choi_2011)
# # 4) batch process Hees 2013 non-wear method
# batch_process_non_wear_algorithm(algorithm = process_hees_2013)
# 5) batch process the plotting of the non-wear algorithms including true non-wear time
# batch_process_plot_non_wear_algorithms()
# print time and memory
set_end(tic, process)