-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_analysis.py
More file actions
188 lines (167 loc) · 6.86 KB
/
Copy pathdata_analysis.py
File metadata and controls
188 lines (167 loc) · 6.86 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
import os
import pandas as pd
import random
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
from core.data import preprocess_train_test_dataframes
from core.util import data_mean_med_std
SEED = random.randint(0,32768)
DIR = "figures"
def get_time_index():
'''
Creates an index of times for every minute starting from 12:00 PM to 11:59 AM, then 12:00 PM again.
Used as an X axis for graphs.
'''
time_index = [f'{12 + i//60:02d}' for i in range(0,720,120)]
time_index += [f'{i//60:02d}' for i in range(0,720,120)]
time_index += ['12']
return time_index
def data_mean_trend_plot(data_control:pd.DataFrame = None, data_condition:pd.DataFrame = None, stat:str = None, title=None, ax=None, ylabel:str=None):
fig=None
if ax is None:
fig, ax = plt.subplots()
time_index = get_time_index()
if data_control is not None:
data_control_trend = data_control.apply(data_mean_med_std, axis=0).transpose()
_, caps, bars = ax.errorbar(
x=data_control_trend.index,
y=data_control_trend[stat],
yerr=data_control_trend['std'],
label='non-depressed', color='blue',
errorevery=7, capsize=0
)
[bar.set_alpha(0.15) for bar in bars]
[cap.set_alpha(0.15) for cap in caps]
if data_condition is not None:
data_condition_trend = data_condition.apply(data_mean_med_std, axis=0).transpose()
_, caps, bars = ax.errorbar(
x=data_condition_trend.index,
y=data_condition_trend[stat],
yerr=data_condition_trend['std'],
label='depressed', color='red',
errorevery=7, capsize=0
)
[bar.set_alpha(0.15) for bar in bars]
[cap.set_alpha(0.15) for cap in caps]
ax.set_xticks(ticks=range(0,1441,120), labels=time_index)
ax.set_xlabel('Time(Hour of Day)')
ax.set_ylabel(ylabel)
ax.set_title(title)
ax.legend()
return (fig, ax)
def plot_random_samples(data_control:pd.DataFrame, data_condition:pd.DataFrame, n_random:int, ax=None, ylabel:str=None):
random.seed(SEED)
control_index = random.sample(range(len(data_control.index)), n_random)
condition_index = random.sample(range(len(data_condition.index)), n_random)
control_selected = data_control.iloc[control_index].copy()
condition_selected = data_condition.iloc[condition_index].copy()
fig=None
if ax is None:
fig, ax = plt.subplots()
for i in range(n_random):
ax.scatter(range(0,1440), control_selected.iloc[i], label='non-depressed', color='blue', alpha=0.2, s=1.5)
for i in range(n_random):
ax.scatter(range(0,1440), condition_selected.iloc[i], label='depressed', color='red', alpha=0.2, s=1.5)
time_index = get_time_index()
ax.set_xticks(ticks=range(0,1441,120), labels=time_index)
ax.set_xlabel('Time(Hour of Day)')
ax.set_ylabel(ylabel)
ax.set_title(f' Activity Plot of {n_random*2} Samples')
red_patch = mpatches.Patch(color='red', label='non-depressed')
blue_patch = mpatches.Patch(color='blue', label='depressed')
ax.legend(handles=[red_patch, blue_patch])
return (fig, ax)
data = pd.read_csv("data/processed_dataframes/data_raw.csv", index_col=0)
data_control = data[data['label'] == 0].copy().drop(labels=['label'], axis=1)
data_condition = data[data['label'] == 1].copy().drop(labels=['label'], axis=1)
NUM_RANDOM = 5
fig, ((ax1, ax2), (ax3, ax4), (ax5, ax6), (ax7, ax8)) = plt.subplots(4, 2, figsize=(12,12))
fig.tight_layout(pad=3)
data_mean_trend_plot(data_control=data_control,
data_condition=data_condition,
stat='mean',
title='No Preprocessing',
ax=ax1,
ylabel='# of Recorded Movements')
plot_random_samples(data_control,
data_condition,
n_random=NUM_RANDOM,
ax=ax2,
ylabel='# of Recorded Movements')
preprocessing_settings = {
'resample' : False,
'log_base' : None,
'scale_range' : (0,1),
'use_standard' : None,
}
(processed_data, _) = preprocess_train_test_dataframes(
X_train=data.drop(labels=['label'], axis=1),
settings=preprocessing_settings
)
processed_data['label'] = data['label']
data_control = processed_data[processed_data['label'] == 0].copy().drop(labels=['label'], axis=1)
data_condition = processed_data[processed_data['label'] == 1].copy().drop(labels=['label'], axis=1)
data_mean_trend_plot(data_control=data_control,
data_condition=data_condition,
stat='mean',
title='Scaled Data',
ax=ax3,
ylabel='Value')
plot_random_samples(data_control,
data_condition,
n_random=NUM_RANDOM,
ax=ax4,
ylabel='Value')
preprocessing_settings = {
'resample' : False,
'log_base' : None,
'scale_range' : (0,1),
'use_standard' : None,
'use_gaussian' : 30
}
(processed_data, _) = preprocess_train_test_dataframes(
X_train=data.drop(labels=['label'], axis=1),
settings=preprocessing_settings
)
processed_data['label'] = data['label']
data_control = processed_data[processed_data['label'] == 0].copy().drop(labels=['label'], axis=1)
data_condition = processed_data[processed_data['label'] == 1].copy().drop(labels=['label'], axis=1)
data_mean_trend_plot(data_control=data_control,
data_condition=data_condition,
stat='mean',
title='De-noised Data',
ax=ax5,
ylabel='Value')
plot_random_samples(data_control,
data_condition,
n_random=NUM_RANDOM,
ax=ax6,
ylabel='Value')
preprocessing_settings = {
'resample' : False,
'log_base' : None,
'scale_range' : (0,1),
'use_standard' : True,
'use_gaussian' : 30,
'adjust_seasonality' : True
}
(processed_data, _) = preprocess_train_test_dataframes(
X_train=data.drop(labels=['label'], axis=1),
settings=preprocessing_settings
)
processed_data['label'] = data['label']
data_control = processed_data[processed_data['label'] == 0].copy().drop(labels=['label'], axis=1)
data_condition = processed_data[processed_data['label'] == 1].copy().drop(labels=['label'], axis=1)
data_mean_trend_plot(data_control=data_control,
data_condition=data_condition,
stat='mean',
title='Seasonally Adjusted Data',
ax=ax7,
ylabel='Value')
plot_random_samples(data_control,
data_condition,
n_random=NUM_RANDOM,
ax=ax8,
ylabel='Value')
fig.savefig(os.path.join(DIR, "preprocessing.png"))
plt.show()