Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
cond7_idx = event_labels0 == "imitate, follower"
cond67_idx = cond6_idx+cond7_idx
micro_data[cond67_idx] = np.concatenate([micro_data2[cond67_idx],
micro_data1[cond67_idx]],axis=1)
# Transform data to correct shape
arr_shape = micro_data.shape # get shape
micro_data = micro_data.swapaxes(1,2) # swap ch and time axis
micro_data = micro_data.reshape(arr_shape[0]*arr_shape[2],arr_shape[1]) # reshape by combining epochs and times
# Filter the data in alpha
# Notice it is done only after combining epochs and time, as filter length
# would be too long for 1s epochs. The filter function wants time on the last axis
micro_data = micro_data.transpose()
micro_data_alpha = mne.filter.filter_data(micro_data, sfreq, freq_range[0], freq_range[1])
# Reverse the shape
micro_data_alpha = micro_data_alpha.transpose()
return micro_data_alpha
def pseudo_pair_dualmicro_backfitting(micro_data, prototype_map, events, n_maps, sfreq):
""" Backfit microstate labels based on prototype map previously determined """
assert prototype_map.shape[0] == n_maps, "Template map size is not equal to n_maps"
# Load micro data
micro_data0 = micro_data
n_ch = micro_data.shape[1]
# Estimate global explained variance (GEV) by the averaged template maps
# on the global field potential peaks in our data
# Find GFPs
gfp = np.std(micro_data0, axis=1)
gfp_peaks = locmax(gfp)
gfp_values = gfp[gfp_peaks]
gfp2 = np.sum(gfp_values**2) # normalizing constant in GEV
# Calculate spatial correlation
# Using absolute value of the topographies to obtain polarity invariance
# Since we are working on two-person microstates, we also need to test
# the different configurations. There are 4 in total, but we only need to try 2
tmp_maps = np.split(prototype_map, [n_ch//2,n_ch], axis=1)
all_polarity_combinations = [np.concatenate([tmp_maps[0],tmp_maps[1]],axis=1),
np.concatenate([tmp_maps[0],-tmp_maps[1]],axis=1)]
C_arr = np.zeros((micro_data0.shape[0],n_maps,len(all_polarity_combinations)))
for p in range(len(all_polarity_combinations)):
C_arr[:,:,p] = np.dot(micro_data0, all_polarity_combinations[p].T)
# rescale cov
C_arr[:,:,p] /= (n_ch*np.outer(gfp, np.std(prototype_map, axis=1)))
# Get C as the highest correlation independent of polarity
# Take max for all polarity configurations and then argmax to find label
C = np.sqrt(np.max(C_arr**2,axis=2)) # notice sign is lost here, but it is only used as C^2 later so it is fine
L = np.argmax(C**2, axis=1)
L_gfp = L[gfp_peaks]
C_gfp = C[gfp_peaks]
gev = np.zeros(n_maps)
for k in range(n_maps):
r = L_gfp==k
gev[k] = np.sum(gfp_values[r]**2 * C_gfp[r,k]**2)/gfp2
# Reshape labels back to epoch, time
L_reshaped = L.reshape(len(events[0]),sfreq)
return L_reshaped, gev
def dualmicro_fit_all_pseudo_pair_feature_computation(i, n_maps, backfit_results, sfreq,
event_id, collapsed_event_id):
"""
Overview of common microstate features:
1. Average duration a given microstate remains stable (Dur)
2. Frequency occurrence, independent of individual duration (Occ)
Average number of times a microstate becomes dominant per second
3. Ratio of total Time Covered (TCo)
4. Transition probabilities (TMx)
5. Ratio of shannon entropy relative to theoretical max chaos (Ent)
Parameters
----------
i : int
The index.
n_maps : int
The number of maps (clusters) used.
backfit_results : list
The estimated back-fitted microstates.
sfreq : int
The sampling frequency.
event_id : dict
The mappings between event id and condition.
collapsed_brain_event_id : dict
The renamed mappings between event id and collapsed conditions
Returns
-------
m_labels : list of two np.array
The microstate sequence (labels) time series in the format (epoch, time).
events : list of two pd.DataFrame
The events corresponding to each epoch.
MFeatures : list
List of arrays of each microstate feature.
"""
m_labels = backfit_results[1][i]
events = backfit_results[3][i]
# Since they are synchronized already, just take the first and fix
# epoch idx column with the new idx for the synchronized labels
events = events[0]
# Update in v7
# # Collapse event_id 6 and 7 to 4 and 5
# events.loc[events["Event_id"] == 6,["Event_id","Event_label"]] = [4, "observe, actor"]
# events.loc[events["Event_id"] == 7,["Event_id","Event_label"]] = [5, "imitate, leader"]
# The microstate clustering was performed on flipped (collapsed) events,
# but I will compute the features on the 8 trials to avoid the flipping
# effect and then collapse by averaging afterwards
# Reshape m_labels to (epoch, time)
m_labels = m_labels.reshape(len(events),sfreq)
# Remove pre-trial epochs
pre_trial_epochs = events["Trial_start_time"] < 0
m_labels = m_labels[np.invert(pre_trial_epochs)]
events = events.loc[np.invert(pre_trial_epochs)].reset_index(drop=True)
events["Epoch_idx"] = events.index
# Pre-allocate memory
Dur_arr = np.zeros((len(event_id),n_maps)); Dur_arr.fill(np.nan)
Occ_arr = np.zeros((len(event_id),n_maps)); Occ_arr.fill(np.nan)
TCo_arr = np.zeros((len(event_id),n_maps)); TCo_arr.fill(np.nan)
TMx_arr = np.zeros((len(event_id),n_maps,n_maps))
Ent_arr = np.zeros(len(event_id))
for e in range(len(event_id)):
ev_idx = list(event_id.values())[e]
ep_idx = events["Epoch_idx"][events["Event_id"] == ev_idx]
trial_numbers0 = np.unique(events["Trial_number"][ep_idx])
if len(trial_numbers0) > 8:
# Compute the first 8 trials and the last 8 separately, before
# taking the average to be consistent with asymmetric trials
Dur_arr0 = np.zeros((2,n_maps))
Occ_arr0 = np.zeros((2,n_maps))
TCo_arr0 = np.zeros((2,n_maps))
TMx_arr0 = np.zeros((2,n_maps,n_maps))
Ent_arr0 = np.zeros(2)
trial_numbers_split = np.array_split(trial_numbers0, 2)
# Array_split is used in cases where a trial might have been
# dropped, hence the split is only 8/7
for s in range(len(trial_numbers_split)):
ep_idx0 = events.loc[(events["Event_id"] == ev_idx)&
(events["Trial_number"].isin(trial_numbers_split[s])),
"Epoch_idx"]
m_labels0 = m_labels[ep_idx0,:]
m_labels0_flat = m_labels0.reshape(m_labels0.shape[0]*m_labels0.shape[1])
# Compute duration, occurrence and time covered
l_, d_ = microstate_run_length_encoding(m_labels0_flat)
# For each microstate
for ii in range(n_maps):
if np.isnan(np.nanmean(d_[l_==ii])):
# The specific microstate did not occur at all for this event
Dur_arr0[s,ii] = 0
Occ_arr0[s,ii] = 0
TCo_arr0[s,ii] = 0
else:
Dur_arr0[s,ii] = np.mean(d_[l_==ii]) * 1000/sfreq # convert to ms
Occ_arr0[s,ii] = len(d_[l_==ii])/len(d_) * sfreq
TCo_arr0[s,ii] = np.sum(d_[l_==ii])/np.sum(d_)
# Compute transition matrix
TMx_arr0[s] = T_empirical(m_labels0_flat, n_maps)
# Compute Shannon Entropy relative to max
Ent_arr0[s] = H_1(m_labels0_flat, n_maps)/np.log(float(n_maps))
# Average over the splits
Dur_arr[e] = np.mean(Dur_arr0, axis=0)
Occ_arr[e] = np.mean(Occ_arr0, axis=0)
TCo_arr[e] = np.mean(TCo_arr0, axis=0)
TMx_arr[e] = np.mean(TMx_arr0, axis=0)
Ent_arr[e] = np.mean(Ent_arr0, axis=0)
else:
m_labels0 = m_labels[ep_idx,:]
m_labels0_flat = m_labels0.reshape(m_labels0.shape[0]*m_labels0.shape[1])
# Compute duration, occurrence and time covered
l_, d_ = microstate_run_length_encoding(m_labels0_flat)
# For each microstate
for ii in range(n_maps):
if np.isnan(np.nanmean(d_[l_==ii])):
# The specific microstate did not occur at all for this event
Dur_arr[e,ii] = 0
Occ_arr[e,ii] = 0
TCo_arr[e,ii] = 0
else:
Dur_arr[e,ii] = np.mean(d_[l_==ii]) * 1000/sfreq # convert to ms
Occ_arr[e,ii] = len(d_[l_==ii])/len(d_) * sfreq
TCo_arr[e,ii] = np.sum(d_[l_==ii])/np.sum(d_)
# Compute transition matrix
TMx_arr[e] = T_empirical(m_labels0_flat, n_maps)
# Compute Shannon Entropy relative to max
Ent_arr[e] = H_1(m_labels0_flat, n_maps)/np.log(float(n_maps))
# Combine all features in a list
MFeatures = [Dur_arr,Occ_arr,TCo_arr,TMx_arr,Ent_arr]
# Update in v7 - Collapse after computations in single events
Dur_arr2 = np.zeros((len(collapsed_event_id),n_maps))
Occ_arr2 = np.zeros((len(collapsed_event_id),n_maps))
TCo_arr2 = np.zeros((len(collapsed_event_id),n_maps))
TMx_arr2 = np.zeros((len(collapsed_event_id),n_maps,n_maps))
Ent_arr2 = np.zeros(len(collapsed_event_id))
MFeatures2 = [Dur_arr2,Occ_arr2,TCo_arr2,TMx_arr2,Ent_arr2]
for f in range(len(MFeatures2)):
tmp_feat = MFeatures[f]
tmp_feat2 = MFeatures2[f]
for e in range(len(collapsed_event_id)):
ee = list(collapsed_event_id.keys())[e]
if (ee == 'observer_actor'):
old_ev_idx1 = list(event_id.keys()).index("observe, actor")
old_ev_idx2 = list(event_id.keys()).index("observe, observer")
tmp_feat2[e] = np.mean([tmp_feat[old_ev_idx1],tmp_feat[old_ev_idx2]], axis=0)
elif ee == 'follower_leader':
old_ev_idx1 = list(event_id.keys()).index("imitate, leader")
old_ev_idx2 = list(event_id.keys()).index("imitate, follower")
tmp_feat2[e] = np.mean([tmp_feat[old_ev_idx1],tmp_feat[old_ev_idx2]], axis=0)
else:
new_ev_idx = list(collapsed_event_id.values())[e]
old_ev_idx = list(event_id.values()).index(new_ev_idx)
tmp_feat2[e] = tmp_feat[old_ev_idx]
return m_labels, events, MFeatures2
def label_to_walk_direction(labels):
directions = np.zeros(len(labels))
directions[pd.Series(labels).isin([0,1,2,3])] = 1 # A/B/C/D assigned to 1
directions[pd.Series(labels).isin([4,5,6,7])] = -1 # E/F/G/H assigned to -1
assert np.sum(directions==0)==0
return directions
def compute_dualmicro_DFA(i, microstate_results, trialinfo_list, sfreq, window_sizes, event_id, collapsed_event_id, overlap=True):
"""
See Hardstone et al, 2012 for more info
Perform DFA
1 Compute cumulative sum of time series to create signal profile
2 Define set of window sizes (see below)
3 Remove the linear trend using least-squares for each window
4 Calculate standard deviation for each window and take the mean
5 Plot fluctuation function (Standard deviation) as function
for all window sizes, on double logarithmic scale
6 The DFA exponent alpha correspond to Hurst exponent
f(L) = sd = L^alpha (with alpha as linear coefficient in log plot)
Parameters
----------
i : int
The index.
microstate_results : list
The estimated microstates.
trialinfo_list : list
List with trial informations.
sfreq : int
The sampling frequency.
window_sizes : np.array
Window sizes to compute DFA over.
event_id : dict
The mappings between event id and condition.
collapsed_brain_event_id : dict
The renamed mappings between event id and collapsed conditions
overlap : bool, optional
Boolean to determine whether to use overlapping windows. The default is True.
Returns
-------
dfa_array : np.array
The Hurst Exponents.
fluctuations : np.array
The fluctuations estimated at different window sizes.
"""
# Load all microstate results
pair_idx = microstate_results[5]
pair_indices = pair_idx[i], pair_idx[i+1]
m_labels = microstate_results[1][pair_indices[0]:pair_indices[1]]
events = trialinfo_list[2][i]
# Since they are synchronized already, just take the first and fix
# epoch idx column with the new idx for the synchronized labels
events = events[0]
# Reshape m_labels to (epoch, time)
m_labels = m_labels.reshape(len(events),sfreq)
# Remove pre-trial epochs
pre_trial_epochs = events["Trial_start_time"] < 0
m_labels = m_labels[np.invert(pre_trial_epochs)]
events = events.loc[np.invert(pre_trial_epochs)].reset_index(drop=True)
events["Epoch_idx"] = events.index
# Pre-allocate memory
dfa_array = np.zeros((len(event_id)))
dfa_array[:] = np.nan
fluctuations = np.zeros((len(event_id),len(window_sizes)))
fluctuations[:] = np.nan
# As I am working with linear regression on log-values, I can just take
# the avg of the x, y coordinates and the coefficients will correspond
# to the avg for each separate linear regression
for e in range(len(event_id)):
ev_idx = list(event_id.values())[e]
ep_idx = events["Epoch_idx"][events["Event_id"] == ev_idx]
trial_numbers0 = np.unique(events["Trial_number"][ep_idx])
if len(trial_numbers0) > 8:
# Compute the first 8 trials and the last 8 separately, before
# taking the average to be consistent with asymmetric trials
dfa0 = []
fluct0 = np.zeros((2,len(window_sizes)))
trial_numbers_split = np.array_split(trial_numbers0, 2)
# Array_split is used in cases where a trial might have been
# dropped, hence the split is only 8/7
for s in range(len(trial_numbers_split)):
ep_idx0 = events.loc[(events["Event_id"] == ev_idx)&
(events["Trial_number"].isin(trial_numbers_split[s])),
"Epoch_idx"]
m_labels0 = m_labels[ep_idx0,:]
m_labels0_flat = m_labels0.reshape(m_labels0.shape[0]*m_labels0.shape[1])
# Convert the labels to the directons for computation of cumulative
# sum in order to get the signal profile
data = label_to_walk_direction(m_labels0_flat)
# plt.plot(np.arange(1,len(data)+1,1),np.cumsum(data)) # plot signal profile
# Estimate and save DFA
dfa, fluctuations_2d = nolds.dfa(data,nvals=window_sizes,overlap=overlap,debug_data=True)
dfa0.append(dfa)
fluct0[s] = fluctuations_2d[1]
# Average the two DFA estimations
dfa_array[e] = np.mean(dfa0)
fluctuations[e,:] = np.mean(fluct0, axis=0)
else:
m_labels0 = m_labels[ep_idx,:]
m_labels0_flat = m_labels0.reshape(m_labels0.shape[0]*m_labels0.shape[1])
# Convert the labels to the directons for computation of cumulative
# sum in order to get the signal profile
data = label_to_walk_direction(m_labels0_flat)
# Estimate and save DFA
dfa_array[e],fluctuations_2d = nolds.dfa(data,nvals=window_sizes,overlap=overlap,debug_data=True)
fluctuations[e,:] = fluctuations_2d[1]
# Update in DFA v2 - Collapse after computation of DFA
dfa_array2 = np.zeros((len(collapsed_event_id)))
dfa_array2[:] = np.nan
fluctuations2 = np.zeros((len(collapsed_event_id),len(window_sizes)))
fluctuations2[:] = np.nan
for e in range(len(collapsed_event_id)):
ee = list(collapsed_event_id.keys())[e]
if ee == 'observer_actor':
old_ev_idx1 = list(event_id.keys()).index("observe, actor")
old_ev_idx2 = list(event_id.keys()).index("observe, observer")
dfa_array2[e] = np.mean([dfa_array[old_ev_idx1],dfa_array[old_ev_idx2]])
fluctuations2[e] = np.mean([fluctuations[old_ev_idx1],fluctuations[old_ev_idx2]], axis=0)
elif ee == 'follower_leader':
old_ev_idx1 = list(event_id.keys()).index("imitate, leader")
old_ev_idx2 = list(event_id.keys()).index("imitate, follower")
dfa_array2[e] = np.mean([dfa_array[old_ev_idx1],dfa_array[old_ev_idx2]])
fluctuations2[e] = np.mean([fluctuations[old_ev_idx1],fluctuations[old_ev_idx2]], axis=0)
else:
new_ev_idx = list(collapsed_event_id.values())[e]
old_ev_idx = list(event_id.values()).index(new_ev_idx)
dfa_array2[e] = dfa_array[old_ev_idx]
fluctuations2[e] = fluctuations[old_ev_idx]
return dfa_array2, fluctuations2
def compute_dualmicro_DFA_pseudo(i, backfit_results, sfreq, window_sizes, event_id, collapsed_event_id, overlap=True):
"""
See Hardstone et al, 2012 for more info
Perform DFA
1 Compute cumulative sum of time series to create signal profile
2 Define set of window sizes (see below)
3 Remove the linear trend using least-squares for each window
4 Calculate standard deviation for each window and take the mean
5 Plot fluctuation function (Standard deviation) as function
for all window sizes, on double logarithmic scale
6 The DFA exponent alpha correspond to Hurst exponent
f(L) = sd = L^alpha (with alpha as linear coefficient in log plot)
Parameters
----------
i : int
The index.
backfit_results : list
The estimated back-fitted microstates.
sfreq : int
The sampling frequency.
window_sizes : np.array
Window sizes to compute DFA over.
event_id : dict
The mappings between event id and condition.
collapsed_brain_event_id : dict
The renamed mappings between event id and collapsed conditions
overlap : bool, optional
Boolean to determine whether to use overlapping windows. The default is True.
Returns
-------
dfa_array : np.array
The Hurst Exponents.
fluctuations : np.array
The fluctuations estimated at different window sizes.
"""
m_labels = backfit_results[1][i]
events = backfit_results[3][i]
# Since they are synchronized already, just take the first and fix
# epoch idx column with the new idx for the synchronized labels
events = events[0]
# Update in DFA v2
# # Collapse event_id 6 and 7 to 4 and 5
# events.loc[events["Event_id"] == 6,["Event_id","Event_label"]] = [4, "observe, actor"]
# events.loc[events["Event_id"] == 7,["Event_id","Event_label"]] = [5, "imitate, leader"]
# The microstate clustering was performed on flipped (collapsed) events,
# but I will compute the DFA on the 8 trials to avoid the flipping
# effect on the linear detrending, and then collapse by averaging
# after DFA computation
# Reshape m_labels to (epoch, time)
m_labels = m_labels.reshape(len(events),sfreq)
# Remove pre-trial epochs
pre_trial_epochs = events["Trial_start_time"] < 0
m_labels = m_labels[np.invert(pre_trial_epochs)]
events = events.loc[np.invert(pre_trial_epochs)].reset_index(drop=True)
events["Epoch_idx"] = events.index
# Pre-allocate memory
dfa_array = np.zeros((len(event_id)))
dfa_array[:] = np.nan
fluctuations = np.zeros((len(event_id),len(window_sizes)))
fluctuations[:] = np.nan
# As I am working with linear regression on log-values, I can just take
# the avg of the x, y coordinates and the coefficients will correspond
# to the avg for each separate linear regression
for e in range(len(event_id)):
ev_idx = list(event_id.values())[e]
ep_idx = events["Epoch_idx"][events["Event_id"] == ev_idx]
trial_numbers0 = np.unique(events["Trial_number"][ep_idx])
if len(trial_numbers0) > 8:
# Compute the first 8 trials and the last 8 separately, before
# taking the average to be consistent with asymmetric trials
dfa0 = []
fluct0 = np.zeros((2,len(window_sizes)))
trial_numbers_split = np.array_split(trial_numbers0, 2)
# Array_split is used in cases where a trial might have been
# dropped, hence the split is only 8/7
for s in range(len(trial_numbers_split)):
ep_idx0 = events.loc[(events["Event_id"] == ev_idx)&
(events["Trial_number"].isin(trial_numbers_split[s])),
"Epoch_idx"]
m_labels0 = m_labels[ep_idx0,:]
m_labels0_flat = m_labels0.reshape(m_labels0.shape[0]*m_labels0.shape[1])
# Convert the labels to the directons for computation of cumulative
# sum in order to get the signal profile
data = label_to_walk_direction(m_labels0_flat)
# plt.plot(np.arange(1,len(data)+1,1),np.cumsum(data)) # plot signal profile
# Estimate and save DFA
dfa, fluctuations_2d = nolds.dfa(data,nvals=window_sizes,overlap=overlap,debug_data=True)
dfa0.append(dfa)
fluct0[s] = fluctuations_2d[1]
# Average the two DFA estimations
dfa_array[e] = np.mean(dfa0)
fluctuations[e,:] = np.mean(fluct0, axis=0)
else:
m_labels0 = m_labels[ep_idx,:]
m_labels0_flat = m_labels0.reshape(m_labels0.shape[0]*m_labels0.shape[1])
# Convert the labels to the directons for computation of cumulative
# sum in order to get the signal profile
data = label_to_walk_direction(m_labels0_flat)
# Estimate and save DFA
dfa_array[e],fluctuations_2d = nolds.dfa(data,nvals=window_sizes,overlap=overlap,debug_data=True)
fluctuations[e,:] = fluctuations_2d[1]
# Update in DFA v2 - Collapse after computation of DFA
dfa_array2 = np.zeros((len(collapsed_event_id)))
dfa_array2[:] = np.nan
fluctuations2 = np.zeros((len(collapsed_event_id),len(window_sizes)))
fluctuations2[:] = np.nan
for e in range(len(collapsed_event_id)):
ee = list(collapsed_event_id.keys())[e]
if ee == 'observer_actor':
old_ev_idx1 = list(event_id.keys()).index("observe, actor")
old_ev_idx2 = list(event_id.keys()).index("observe, observer")
dfa_array2[e] = np.mean([dfa_array[old_ev_idx1],dfa_array[old_ev_idx2]])
fluctuations2[e] = np.mean([fluctuations[old_ev_idx1],fluctuations[old_ev_idx2]], axis=0)
elif ee == 'follower_leader':
old_ev_idx1 = list(event_id.keys()).index("imitate, leader")
old_ev_idx2 = list(event_id.keys()).index("imitate, follower")
dfa_array2[e] = np.mean([dfa_array[old_ev_idx1],dfa_array[old_ev_idx2]])
fluctuations2[e] = np.mean([fluctuations[old_ev_idx1],fluctuations[old_ev_idx2]], axis=0)
else:
new_ev_idx = list(collapsed_event_id.values())[e]
old_ev_idx = list(event_id.values()).index(new_ev_idx)
dfa_array2[e] = dfa_array[old_ev_idx]
fluctuations2[e] = fluctuations[old_ev_idx]
return dfa_array2, fluctuations2
def similar_interbrain_microstates(x, y):
assert len(x) == len(y)
ratio_in_similar_state = sum(x == y)/len(x)
return ratio_in_similar_state
def shift_label_time_series(x, y, shift):
assert len(x) == len(y)
x = pd.Series(x)
y = pd.Series(y)
# Shift the data
y = y.shift(shift)
# Drop the NaN
drop_idx = np.where(y.isna())[0]
y = y.drop(drop_idx)
x = x.drop(drop_idx)
assert len(y) == len(x)
assert sum(y.isna()) == 0
return x.to_numpy().astype(int), y.to_numpy().astype(int)
def shifted_interbrain_microstate_feature_computation(i, n_maps, microstate_results,
trialinfo_list, sfreq, event_id, collapsed_event_id, lag_search_range, lag_interval):
"""
Compute inter-brain features, but for time-lagged microstate label time series.
Similar to cross-correlation, but maximizing the amount of synchronized
(similar) microstates at a given lag
Parameters
----------
i : int
The index.
n_maps : int
The number of maps (clusters) used.
microstate_results : list
The estimated microstates.
trialinfo_list : list
List with trial informations.
sfreq : int
The sampling frequency.
event_id : dict
The mappings between event id and condition.
collapsed_brain_event_id : dict
The renamed mappings between event id and collapsed conditions
lag_search_range : float
The lag in both directions the data is being shifted
lag_interval : np.array
The sample points corresponding to the lags the data is being shifted
Returns
-------
m_labels : list of two np.array
The microstate sequence (labels) time series in the format (epoch, time).
events : list of two pd.DataFrame
The events corresponding to each epoch.
MFeatures : list
List of arrays of each microstate feature.
shift_info : list
List containing the information regarding the shift, e.g. the cross-
similarity at different time lags and the optimal time lag
"""
# Here i refers to the pair in range(n_subjects//2)
sub_idx = microstate_results[5]
# Get the microstate labels and events for participant 1
subject_indices1 = sub_idx[2*i], sub_idx[2*i+1]
m_labels1 = microstate_results[1][subject_indices1[0]:subject_indices1[1]]
# Get the trialinfo with conditions
Subject1, trialinfo1 = trialinfo_list[2*i]
# Convert to dataframe
event_id_inv = {v: k for k, v in event_id.items()} # Inverse the event id
events1 = pd.DataFrame(trialinfo1,columns=["Event_id","Trial_number","Trial_start_time"])
events1["Event_label"] = events1["Event_id"].replace(event_id_inv)
events1 = events1.reset_index().rename(columns={"index":"Epoch_idx"})
# Reshape m_labels to (epoch, time)
m_labels1 = m_labels1.reshape(len(trialinfo1),sfreq)
# Get the microstate labels and events for participant 2
subject_indices2 = sub_idx[2*i+1], sub_idx[2*i+2]
m_labels2 = microstate_results[1][subject_indices2[0]:subject_indices2[1]]
# Get the trialinfo with conditions
Subject2, trialinfo2 = trialinfo_list[2*i+1]
# Convert to dataframe
events2 = pd.DataFrame(trialinfo2,columns=["Event_id","Trial_number","Trial_start_time"])
events2["Event_label"] = events2["Event_id"].replace(event_id_inv)
events2 = events2.reset_index().rename(columns={"index":"Epoch_idx"})
# Reshape m_labels to (epoch, time)
m_labels2 = m_labels2.reshape(len(trialinfo2),sfreq)
# check that the participants from a pair was loaded
assert Subject2-1 == Subject1
# Check that the same amount of event types are present
assert trialinfo1[-1,1] == trialinfo2[-1,1]
# Synchronize the events from the pair based on timing info
# By trimming the epochs to only include the epochs that are present
# in both participants of the pair
# Initialize with an array filled with a unique number not in use
sync_m_labels1 = np.zeros_like(m_labels1); sync_m_labels1.fill(9999)
sync_m_labels2 = np.zeros_like(m_labels2); sync_m_labels2.fill(9999)
for t in np.unique(trialinfo1[:,1]):
t_idx1 = np.where(trialinfo1[:,1] == t)[0]
t_idx2 = np.where(trialinfo2[:,1] == t)[0]
# Get the timings for the epochs for the specific trial t
t_timings1 = trialinfo1[t_idx1,2]
t_timings2 = trialinfo2[t_idx2,2]
timings_intersect = np.intersect1d(t_timings1,t_timings2)
# Get the indices where the timings matches
t_idx_match1 = t_idx1[pd.Series(t_timings1).isin(timings_intersect)]
t_idx_match2 = t_idx2[pd.Series(t_timings2).isin(timings_intersect)]
# Get the actual values from the synchronized epochs
sync_m_labels1[t_idx_match1] = m_labels1[t_idx_match1]
sync_m_labels2[t_idx_match2] = m_labels2[t_idx_match2]
# Find the epochs that were asynchronous, which have to be trimmed
asynch_epochs1 = np.unique(np.where(sync_m_labels1==9999)[0])
asynch_epochs2 = np.unique(np.where(sync_m_labels2==9999)[0])
# Trim/delete the asynchronous epochs
sync_m_labels1 = np.delete(sync_m_labels1,asynch_epochs1,axis=0)
sync_m_labels2 = np.delete(sync_m_labels2,asynch_epochs2,axis=0)
assert len(sync_m_labels1) == len(sync_m_labels2) # check the amount of synchronized epochs are equal
# Fix events
sync_events1 = events1.drop(asynch_epochs1,axis=0).reset_index(drop=True)
sync_events2 = events2.drop(asynch_epochs2,axis=0).reset_index(drop=True)
# Since they are synchronized already, just take the first and fix
# epoch idx column with the new idx for the synchronized labels
events = sync_events1
events["Epoch_idx"] = events.index
# Notice that for the intrabrain fit all alpha v6 I only corrected
# ppn2. So by taking ppn1 I get the original event labels.
# Remove pre-trial epochs
pre_trial_epochs = events["Trial_start_time"] < 0
sync_m_labels1 = sync_m_labels1[np.invert(pre_trial_epochs)]
sync_m_labels2 = sync_m_labels2[np.invert(pre_trial_epochs)]
events = events.loc[np.invert(pre_trial_epochs)].reset_index(drop=True)
events["Epoch_idx"] = events.index
# Determine the optimal lag for highest interbrain state synchrony
# For each condition - using the collapsed dictionary
# To get one value of optimal lag for e.g. follower-leader and observer-actor
# Collapse event_id 6 and 7 to 4 and 5
events_collapsed = events.copy()
events_collapsed.loc[events_collapsed["Event_id"] == 6,["Event_id","Event_label"]] = [4, "observe, actor"]
events_collapsed.loc[events_collapsed["Event_id"] == 7,["Event_id","Event_label"]] = [5, "imitate, leader"]
normal_events_to_collapsed_map = {"1":1, "2":2, "3":3, "4":4, "5":5, "6":4, "7":5, "8":8}
shift_info = [0]*len(collapsed_event_id)
for e in range(len(collapsed_event_id)):
ev_idx = list(collapsed_event_id.values())[e]
ep_idx = events_collapsed["Epoch_idx"][events_collapsed["Event_id"] == ev_idx]
trial_numbers0 = np.unique(events_collapsed["Trial_number"][ep_idx])
lts1 = sync_m_labels1[ep_idx,:]
lts2 = sync_m_labels2[ep_idx,:]
# Flatten the labels
lts1_flat = pd.Series(lts1.ravel())
lts2_flat = pd.Series(lts2.ravel())
# Compute cross similarity
cross_similarity = [lts1_flat.corr(lts2_flat.shift(lag), method=similar_interbrain_microstates) for lag in lag_interval]
# Original ratio of time in similar states (time = 0)
t_zero_ratio_in_similar_microstate = cross_similarity[lag_search_range]
# The optimal lag
opt_lag = lag_interval[np.argmax(cross_similarity)]
opt_ratio_in_similar_microstate = cross_similarity[np.argmax(cross_similarity)]
# Shift the label time series with the optimal lag prior to computation of interbrain features
# Save the features
shift_info[e] = [t_zero_ratio_in_similar_microstate, opt_lag, opt_ratio_in_similar_microstate, cross_similarity]
# Pre-allocate memory
Dur_arr = np.zeros((len(event_id),n_maps+1)); Dur_arr.fill(np.nan)
Occ_arr = np.zeros((len(event_id),n_maps+1)); Occ_arr.fill(np.nan)
TCo_arr = np.zeros((len(event_id),n_maps+1)); TCo_arr.fill(np.nan)
TMx_arr = np.zeros((len(event_id),n_maps+1,n_maps+1))
Ent_arr = np.zeros(len(event_id))
for e in range(len(event_id)):
ev_idx = list(event_id.values())[e]
ep_idx = events["Epoch_idx"][events["Event_id"] == ev_idx]
trial_numbers0 = np.unique(events["Trial_number"][ep_idx])
# Get the opt_lag from collapsed events
ev_collapsed_idx = normal_events_to_collapsed_map[str(ev_idx)]
shift_info_idx = list(collapsed_event_id.values()).index(ev_collapsed_idx)
opt_lag = shift_info[shift_info_idx][1]
if len(trial_numbers0) > 8:
# Compute the first 8 trials and the last 8 separately, before
# taking the average to be consistent with asymmetric trials
Dur_arr0 = np.zeros((2,n_maps+1))
Occ_arr0 = np.zeros((2,n_maps+1))
TCo_arr0 = np.zeros((2,n_maps+1))
TMx_arr0 = np.zeros((2,n_maps+1,n_maps+1))
Ent_arr0 = np.zeros(2)
trial_numbers_split = np.array_split(trial_numbers0, 2)
# Array_split is used in cases where a trial might have been
# dropped, hence the split is only 8/7
for s in range(len(trial_numbers_split)):
ep_idx0 = events.loc[(events["Event_id"] == ev_idx)&
(events["Trial_number"].isin(trial_numbers_split[s])),
"Epoch_idx"]
# Get the microstate labels
m_labels10 = sync_m_labels1[ep_idx0,:]
m_labels20 = sync_m_labels2[ep_idx0,:]
# Flatten the labels
m_labels10_flat = m_labels10.reshape(m_labels10.shape[0]*m_labels10.shape[1])
m_labels20_flat = m_labels20.reshape(m_labels20.shape[0]*m_labels20.shape[1])
# Shift the label time-series to maximize interbrain synchrony
m_labels10_shifted, m_labels20_shifted = shift_label_time_series(m_labels10_flat, m_labels20_flat, opt_lag)
# Compute average duration of common microstate
# Output: label and duration of common microstate. Label -1 is used
# for not common microstate
l_, d_ = interbrain_microstate_run_length_encoding(m_labels10_shifted,m_labels20_shifted)
# For each microstate
for ii in range(n_maps+1):
if np.isnan(np.nanmean(d_[l_==ii-1])):
# The specific microstate did not occur at all for this event
Dur_arr0[s,ii] = 0
Occ_arr0[s,ii] = 0
TCo_arr0[s,ii] = 0
else:
Dur_arr0[s,ii] = np.mean(d_[l_==ii-1]) * 1000/sfreq # convert to ms
Occ_arr0[s,ii] = len(d_[l_==ii-1])/len(d_) * sfreq
TCo_arr0[s,ii] = np.sum(d_[l_==ii-1])/np.sum(d_)
# Compute transition matrix
TMx_arr0[s] = interbrain_T_matrix(m_labels10_shifted,m_labels20_shifted,n_maps)
# Compute Joint Shannon Entropy relative to max
# Max is the sum of max individual entropies
Ent_arr0[s] = H_2(m_labels10_shifted,m_labels20_shifted,n_maps)/(2*np.log(float(n_maps)))
# Average over the splits
Dur_arr[e] = np.mean(Dur_arr0, axis=0)
Occ_arr[e] = np.mean(Occ_arr0, axis=0)
TCo_arr[e] = np.mean(TCo_arr0, axis=0)
TMx_arr[e] = np.mean(TMx_arr0, axis=0)
Ent_arr[e] = np.mean(Ent_arr0, axis=0)
else:
# Get the microstate labels
m_labels10 = sync_m_labels1[ep_idx,:]
m_labels20 = sync_m_labels2[ep_idx,:]
# Flatten the labels
m_labels10_flat = m_labels10.reshape(m_labels10.shape[0]*m_labels10.shape[1])
m_labels20_flat = m_labels20.reshape(m_labels20.shape[0]*m_labels20.shape[1])
# Shift the label time-series to maximize interbrain synchrony
m_labels10_shifted, m_labels20_shifted = shift_label_time_series(m_labels10_flat, m_labels20_flat, opt_lag)
# Compute average duration of common microstate
# Output: label and duration of common microstate. Label -1 is used
# for not common microstate
l_, d_ = interbrain_microstate_run_length_encoding(m_labels10_shifted,m_labels20_shifted)
# For each microstate
for ii in range(n_maps+1):
if np.isnan(np.nanmean(d_[l_==ii-1])):
# The specific microstate did not occur at all for this event
Dur_arr[e,ii] = 0
Occ_arr[e,ii] = 0
TCo_arr[e,ii] = 0
else:
Dur_arr[e,ii] = np.mean(d_[l_==ii-1]) * 1000/sfreq # convert to ms
Occ_arr[e,ii] = len(d_[l_==ii-1])/len(d_) * sfreq
TCo_arr[e,ii] = np.sum(d_[l_==ii-1])/np.sum(d_)
# Compute transition matrix
TMx_arr[e] = interbrain_T_matrix(m_labels10_shifted,m_labels20_shifted,n_maps)
# Compute Joint Shannon Entropy relative to max
# Max is the sum of max individual entropies
Ent_arr[e] = H_2(m_labels10_shifted,m_labels20_shifted,n_maps)/(2*np.log(float(n_maps)))
# Combine all features in a list
MFeatures = [Dur_arr,Occ_arr,TCo_arr,TMx_arr,Ent_arr]
# Update in v7 - Collapse after computations in single events
Dur_arr2 = np.zeros((len(collapsed_event_id),n_maps+1))
Occ_arr2 = np.zeros((len(collapsed_event_id),n_maps+1))
TCo_arr2 = np.zeros((len(collapsed_event_id),n_maps+1))
TMx_arr2 = np.zeros((len(collapsed_event_id),n_maps+1,n_maps+1))
Ent_arr2 = np.zeros(len(collapsed_event_id))
MFeatures2 = [Dur_arr2,Occ_arr2,TCo_arr2,TMx_arr2,Ent_arr2]
for f in range(len(MFeatures2)):
tmp_feat = MFeatures[f]
tmp_feat2 = MFeatures2[f]
for e in range(len(collapsed_event_id)):
ee = list(collapsed_event_id.keys())[e]
if (ee == 'observer_actor'):
old_ev_idx1 = list(event_id.keys()).index("observe, actor")
old_ev_idx2 = list(event_id.keys()).index("observe, observer")
tmp_feat2[e] = np.mean([tmp_feat[old_ev_idx1],tmp_feat[old_ev_idx2]], axis=0)
elif ee == 'follower_leader':
old_ev_idx1 = list(event_id.keys()).index("imitate, leader")
old_ev_idx2 = list(event_id.keys()).index("imitate, follower")
tmp_feat2[e] = np.mean([tmp_feat[old_ev_idx1],tmp_feat[old_ev_idx2]], axis=0)
else:
new_ev_idx = list(collapsed_event_id.values())[e]
old_ev_idx = list(event_id.values()).index(new_ev_idx)
tmp_feat2[e] = tmp_feat[old_ev_idx]
return [sync_m_labels1, sync_m_labels2], [sync_events1, sync_events2], MFeatures2, shift_info