-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation_tools.py
445 lines (328 loc) · 13.3 KB
/
simulation_tools.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
import statsmodels.api as sm
import pandas as pd
import numpy as np
import random
import math
from statsmodels.stats.multitest import fdrcorrection
from sklearn.preprocessing import LabelEncoder
def random_sample(df, N):
"""
Perform random sampling on a pandas DataFrame.
Parameters
----------
df : pandas DataFrame
The input DataFrame from which random sampling will be performed.
N : int
The number of random rows to select from the DataFrame.
Returns
-------
sampled_df : pandas DataFrame
A new DataFrame containing N randomly selected rows from the input DataFrame.
"""
# Select N random rows from the DataFrame
random_indices = random.sample(range(len(df)), N)
sampled_df = df.iloc[random_indices]
return sampled_df
def split_sampled_df(sampled_df):
"""
Split a DataFrame into two equal-sized groups after shuffling its rows.
Parameters
----------
sampled_df : pandas DataFrame
The input DataFrame to be split.
Returns
-------
group1 : pandas DataFrame
The first half of the shuffled input DataFrame.
group2 : pandas DataFrame
The second half of the shuffled input DataFrame.
"""
# Use the sample method with frac=1 to shuffle all rows of the df
sampled_df_shuffled = sampled_df.sample(frac=1).reset_index(drop=True)
# Calculate the number of rows to split it in half
half_rows = len(sampled_df_shuffled) // 2
# Split the DataFrame into two halves
group1 = sampled_df_shuffled.iloc[:half_rows]
group2 = sampled_df_shuffled.iloc[half_rows:]
return group1, group2
def fisher_transform(df):
"""
Apply Fisher's transformation to a given DataFrame.
Parameters
----------
df : pandas DataFrame or numpy array
The input data to which Fisher's transformation will be applied.
Returns
-------
df_transformed : numpy array
The data transformed using Fisher's transformation.
"""
df_transformed = np.arctanh(df)
return df_transformed
def extract_data(sampled_df, group1, group2):
"""
Extract site and connectome data from two groups.
Parameters
----------
sampled_df : pandas DataFrame
The original DataFrame containing subject data.
group1 : pandas DataFrame
The first group of subjects after splitting and shuffling.
group2 : pandas DataFrame
The second group of subjects after splitting and shuffling.
Returns
-------
group1_site : pandas Series
Encoded "Site" values for group 1.
group2_site : pandas Series
Encoded "Site" values for group 2.
group1_conn : pandas DataFrame
Connectome data for group 1 after removing "Subject" and "Site" columns.
group2_conn : pandas DataFrame
"""
# Extract the "site" columns
group1_site = group1["Site"]
group2_site = group2["Site"]
# Convert site values to numeric using label encoding
le = LabelEncoder()
le.fit(sampled_df["Site"])
group1_site = le.transform(group1_site)
group2_site = le.transform(group2_site)
# Convert the transformed site values to Pandas Series (for later concatenation)
group1_site = pd.Series(group1_site)
group2_site = pd.Series(group2_site)
# Extract connectome values (excluding "Subject" and "site")
group1_conn = group1.drop(columns=["Subject", "Site"])
group2_conn = group2.drop(columns=["Subject", "Site"])
return group1_site, group2_site, group1_conn, group2_conn
def apply_modification(value, d, std_value):
"""
Apply a modification to a given value (connection) using an effect size.
Parameters
----------
value : float or numeric
The original value to which the modification will be applied.
d : float or numeric
The effect size.
std_value : float or numeric
The standard deviation of the combined data.
Returns
-------
modified_value : float or numeric
The result of applying the modification to the original value.
"""
return value + d * std_value
def modify_group2(group1_conn, group2_conn, pi, d):
"""
Modify a subset of connections in the second group's connectome data.
Parameters
----------
group1_conn : pandas DataFrame
Connectome data of the first group of subjects.
group2_conn : pandas DataFrame
Connectome data of the second group of subjects.
pi : float
The proportion of connections in group2_conn to modify.
d : float
The effect size applied to the selected connections.
Returns
-------
connections_to_modify : pandas DataFrame
A subset of connections randomly selected from group2_conn.
group2_modified : pandas DataFrame
The connectome data of the second group after modification.
"""
# Calculate the total number of connections in the DataFrame
total_conn = group2_conn.shape[1]
# Calculate the number of connections to modify based on pi%
num_to_modify = int(total_conn * pi)
# Randomly select the connections (columns) to modify
connections_to_modify = group2_conn.sample(n=num_to_modify, axis=1)
# Stack both groups vertically for std claculation
combined_data = pd.concat([group1_conn, group2_conn], axis=0)
# Modify the selected columns in group2
group2_modified = group2_conn.copy()
for col in connections_to_modify.columns:
std = combined_data[col].std()
group2_modified.loc[:, col] = group2_modified.loc[:, col] + d * std
return connections_to_modify, group2_modified
def standardize_data(group1_conn, group2_modified):
"""
Standardize (z-score) the connectivity data of both groups based on the values of group1 (control group).
Parameters
----------
group1_conn : pandas DataFrame
Connectome data of the first group of subjects (control group).
group2_modified : pandas DataFrame
Modified connectome data of the second group.
Returns
-------
group1_conn_stand : pandas DataFrame
Standardized connectome data of the first group.
group2_modified_stand : pandas DataFrame
Connectome data of the second group standardized according to the control group.
"""
# Standardize the control group's data
mean_control = group1_conn.mean()
std_control = group1_conn.std()
group1_conn_stand = (group1_conn - mean_control) / std_control
# Standardize the "case" group's data
group2_modified_stand = (group2_modified - mean_control) / std_control
return group1_conn_stand, group2_modified_stand
def run_cwas(group1_conn_stand, group2_modified_stand, group1_site, group2_site):
"""
Run a Connectome-Wide Association Study (CWAS) to identify significant connections.
Parameters
----------
group1_conn_stand : pandas DataFrame
Standradised connectome data of the first group of subjects.
group2_modified_stand : pandas DataFrame
Modified connectome data of the second group of subjects, standardised based on the variance of the control group.
group1_site : pandas Series
Site values for the first group.
group2_site : pandas Series
Site values for the second group.
Returns
-------
pval_list : list of floats
List of p-values representing the significance of each connection.
"""
connection_count = group1_conn_stand.shape[1]
pval_list = []
for connection_i in range(connection_count):
# Extract the connectivity data for this connection
connectivity_i_group1 = group1_conn_stand.iloc[:, connection_i]
connectivity_i_group2 = group2_modified_stand.iloc[:, connection_i]
# Stack the connectivity data
connectivity_data = pd.concat(
[connectivity_i_group1, connectivity_i_group2], axis=0
)
connectivity_data = connectivity_data.astype(float)
# Create a design matrix with the group (0 or 1) and site information
design_matrix = pd.DataFrame(
{
"Group": np.concatenate(
([0] * len(connectivity_i_group1), [1] * len(connectivity_i_group2))
),
"Site": pd.concat([group1_site, group2_site], axis=0),
"Constant": 1,
}
)
# Reset index so they match
connectivity_data.index = design_matrix.index
# Perform linear regression
model = sm.OLS(connectivity_data, design_matrix)
results = model.fit()
# Save the p values for each connection
pval = results.pvalues["Group"]
pval_list.append(pval)
return pval_list
def run_simulation(conn_df, N, pi, d):
"""
Simulate a Connectome-Wide Association Study (CWAS) experiment.
Parameters
----------
conn_df : pandas DataFrame
The original connectome data for a group of subjects.
N : int
The number of subjects to randomly select and simulate the experiment.
pi : float
The proportion of connections in group2 to modify.
d : float
The effect size used to modify the selected connections.
Returns
-------
group1_conn : pandas DataFrame
Connectome data of the first group of subjects.
connections_to_modify : pandas DataFrame
A subset of connections randomly selected for modification.
pval_list : list of floats
List of p-values representing the significance of each connection.
"""
# Step 1: Randomly select N subjects
sampled_df = random_sample(conn_df, N)
# Step 2: Randomly split N selected subjects into 2 groups
group1, group2 = split_sampled_df(sampled_df)
group1_site, group2_site, group1_conn, group2_conn = extract_data(
sampled_df, group1, group2
)
# Step 3: Pick pi% of connections at random and modify for group 2
connections_to_modify, group2_modified = modify_group2(
group1_conn, group2_conn, pi, d
)
# Step 4: Run CWAS
group1_conn_stand, group2_modified_stand = standardize_data(
group1_conn, group2_modified
)
pval_list = run_cwas(
group1_conn_stand, group2_modified_stand, group1_site, group2_site
)
return group1_conn, connections_to_modify, pval_list
def calculate_sens_spef(group1_conn, connections_to_modify, rejected, q):
"""
Calculate sensitivity and specificity based on the results of the CWAS.
Parameters
----------
group1_conn : pandas DataFrame
Connectome data of the first group of subjects.
connections_to_modify : pandas DataFrame
A subset of connections that were modified during the simulation.
rejected : list of bool
A list of boolean values indicating whether the null hypothesis was rejected
for each connection during CWAS.
q : float
The significance level or threshold for rejecting the null hypothesis.
Returns
-------
sensitivity : float
Sensitivity, a measure of the ability to correctly identify modified connections.
specificity : float
Specificity, a measure of the ability to correctly identify unmodified connections.
"""
connection_count = group1_conn.shape[1]
# Get a list of the modified connections
modified_conn_list = connections_to_modify.columns.tolist()
modified_conn_list = [int(conn) for conn in modified_conn_list]
# Calculate the number of modified connections (condition positive), and non-modified connections (condition negative)
condition_positive = len(modified_conn_list)
condition_negative = connection_count - condition_positive
true_positive_count = 0
true_negative_count = 0
for connection in range(connection_count):
# Connection has been modified, the null hypothesis should be rejected
if connection in modified_conn_list and rejected[connection]:
true_positive_count += 1
# Connection has not been modified, the null hypothesis should not be rejected
elif connection not in modified_conn_list and not (rejected[connection]):
true_negative_count += 1
# Calculate sensitivity and specificity
sensitivity = true_positive_count / condition_positive
specificity = true_negative_count / condition_negative
return sensitivity, specificity
def run_multiple_simulation(conn_df, N, pi, d, q, num_sample):
sensitivity_list = []
specificity_list = []
correct_rejected_count = 0
for sample in range(num_sample):
# Perform steps 1-4 of simulation
(
group1_conn,
connections_to_modify,
pval_list,
) = run_simulation(conn_df, N, pi, d)
# Step 5: Apply FDR correction
rejected, corrected_pval_list = fdrcorrection(pval_list, alpha=q)
# Calculate sensitivity and specificity
sensitivity, specificity = calculate_sens_spef(
group1_conn, connections_to_modify, rejected, q
)
sensitivity_list.append(sensitivity)
specificity_list.append(specificity)
# If null hypothesis rejected, plus 1
if np.any(corrected_pval_list < q):
correct_rejected_count += 1
result_summary = (
f"Estimated mean sensitivity to detect d={d}, with pi={pi}%, q={q} and N={N}: "
f"{round(np.mean(sensitivity_list), 2)}, with mean specificity of {round(np.mean(specificity_list), 2)}."
)
return result_summary