-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_data.py
More file actions
135 lines (112 loc) · 4.36 KB
/
Copy pathload_data.py
File metadata and controls
135 lines (112 loc) · 4.36 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
# Clean names
clean_names = {
'accumbens-area': 'Accumbens area',
'amygdala': 'Amygdala',
'bankssts': 'Banks of the STS',
'caudalanteriorcingulate': 'Caudal anterior cingulate',
'caudalmiddlefrontal': 'Caudal middle frontal',
'caudate': 'Caudate',
'cerebellum-cortex': 'Cerebellum cortex',
'hippocampus': 'Hippocampus',
'cuneus': 'Cuneus',
'entorhinal': 'Entorhinal',
'frontalpole': 'Frontal pole',
'fusiform': 'Fusiform',
'inferiorparietal': 'Inferior parietal',
'inferiortemporal': 'Inferior temporal',
'insula': 'Insula',
'ICV': 'Brain volume',
'isthmuscingulate': 'Isthmus of cingulate',
'lateraloccipital': 'Lateral occipital',
'lateralorbitofrontal': 'Lateral orbitofrontal',
'lateral-ventricle': 'Lateral ventricle',
'lingual': 'Lingual',
'mean_thick': 'Mean Thickness',
'medialorbitofrontal': 'Medial orbitofrontal',
'middletemporal': 'Middle temporal',
'pallidum': 'Pallidum',
'paracentral': 'Paracentral',
'parahippocampal': 'Parahippocampal',
'parsopercularis': 'Pars opercularis',
'parsorbitalis': 'Pars orbitalis',
'parstriangularis': 'Pars triangularis',
'pericalcarine': 'Pericalcarine',
'postcentral': 'Postcentral',
'posteriorcingulate': 'Posterior cingulate',
'precentral': 'Precentral',
'precuneus': 'Precuneus',
'putamen': 'Putamen',
'rostralanteriorcingulate': 'Rostral anterior cingulate',
'rostralmiddlefrontal': 'Rostral middle frontal',
'superiorfrontal': 'Superior frontal',
'superiorparietal': 'Superior parietal',
'superiortemporal': 'Superior temporal',
'supramarginal': 'Supramarginal',
'temporalpole': 'Temporal pole',
'thalamus': 'Thalamus',
'transversetemporal': 'Tranverse temporal'
}
def load_data(
csv_file='raw_data.csv',
out_type='mean',
out_csv=None,
drop_var=None,
standardize=False,
):
# Load raw data
df = pd.read_csv(csv_file)
# Remove participants with no hamd_change_week8 values
df = df[np.logical_not(pd.isna(df['hamd_change_week8']))]
# Remove participants younger than 21
df = df[df.age > 21]
# Remove healthy controls
df = df[df['Person status'] != 'Healthy Control']
# Output data.frame
X = pd.DataFrame()
# Age
X['age'] = df['age']
# Binary encode sex and single_recurrent
X['sex'] = df['sex'].map({'Male' : 0, 'Female' : 1})
# Single/recurrent MDD
X['single_recurrent'] = df['single_recurrent'].map({'Single' : 0,
'Recurrent' : 1})
# baseline HAMD
X['hamd_base'] = df['hamd_base']
regions = df.columns[[col.startswith('lh') for col in df.columns]]
regions = [region.replace('lh.', '') for region in regions]
if out_type == 'mean':
for region in regions:
X['mean.' + region] = np.mean([df['lh.' + region], df['rh.' + region]], axis=0)
if out_type == 'lr':
for region in regions:
X['lh.' + region] = df['lh.' + region]
for region in regions:
X['rh.' + region] = df['rh.' + region]
if out_type == 'min.max':
for region in regions:
X['min.' + region] = np.min([df['lh.' + region], df['rh.' + region]], axis=0)
for region in regions:
X['max.' + region] = np.max([df['lh.' + region], df['rh.' + region]], axis=0)
# Add ICV
X['ICV'] = np.float64(df['ICV']) # somehow, casting is necessary to avoid NaN (???)
# Binary (threshold) encode the "change" column so
# that 1 equals a change less than 50% and 0 equals a change larger than 50%
threshold = -50
y = df['hamd_change_week8'].le(threshold) # .astype(int)
y = y.map({True : 0, False : 1})
# Save data for statistical analysis
if out_csv is not None:
df = pd.concat([X, y], axis=1)
df.columns = list(X.columns) + ['group']
df.to_csv(out_csv, index=False)
if drop_var is not None:
X = X.drop(drop_var, axis=1)
if standardize:
X = (X - np.mean(X, axis=0))/np.std(X, axis=0)
# Assign feature names
feature_names = np.array(X.columns)
return X.to_numpy(), y.to_numpy(), feature_names