Skip to content
Snippets Groups Projects
Main.py 93.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • glia's avatar
    glia committed
    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 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
        plot_dualmicro(n_maps, maps, gev, epoch.info)
        
        # Make dictionary with n_maps and new order
        # All 4 top row consecutively, followed by 4 bot row
        manual_reordering_template = {"8_alpha":[5,2,7,0,1,4,6,3],
                                      "8_beta":[4,1,3,6,7,5,0,2],
                                      "8_broadband":[6,3,4,0,2,1,5,7]} 
        
        new_order = manual_reordering_template[f"{n_maps}_{ff}"]
        
        maps, gev, m_labels = reorder_microstate_results(new_order, maps, gev, m_labels)
        
        # Plot again to check it worked
        plot_dualmicro(n_maps, maps, gev, epoch.info)
        
        # Since neuronal activity is often oscillating, this causes polarity inversions
        # Microstates ignores the sign, and hence the polarity in the map is arbitrary
        # It is only the relative difference within the plot that is interesting
        # depending on initiation. We can thus freely change the sign for visualization
        # For two-person microstates, each person's map is sign-changed separately
        manual_sign_correction = {"8_alpha":[[-1,-1,-1,1,-1,1,1,1],[1,1,1,-1,-1,1,1,1]],
                                  "8_beta":[[-1,-1,-1,-1,-1,1,1,1],[-1,-1,1,-1,-1,1,1,-1]],
                                  "8_broadband":[[1,1,-1,-1,1,-1,-1,-1],[-1,1,-1,-1,1,-1,-1,-1]]}
        sign_swap = manual_sign_correction[f"{n_maps}_{ff}"]
    
        maps = sign_swap_microstates(sign_swap, maps, n_maps, n_channels)
        
        # Plot a final time for last confirmation
        plot_dualmicro(n_maps, maps, gev, epoch.info)
        
        # Close all figures and repeat by changing n_maps
        plt.close("all")
        
        ### Save reordered results
        n_maps = 8
        ii = n_clusters.index(n_maps)
        
        with open(f"{microstate_save_path}Dualmicro_fit_all_{ff}_data_maps{n_maps}.pkl", "rb") as file:
            microstate_results = pickle.load(file)
        maps, m_labels, gfp_peaks, gev, cv_min, pair_idx = microstate_results
        # Re-order
        new_order = manual_reordering_template[str(n_maps)]
        maps, gev, m_labels = reorder_microstate_results(new_order, maps, gev, m_labels)
        # Sign alignment
        maps = sign_swap_microstates(sign_swap, maps, n_maps, n_channels)
        # Overwrite variable
        microstate_results = maps, m_labels, gfp_peaks, gev, cv_min, pair_idx
        # Save to new file
        with open(f"{microstate_save_path}Reordered/Dualmicro_fit_all_{ff}_data_maps{n_maps}.pkl", "wb") as filehandle:
            pickle.dump(microstate_results, filehandle) # [maps, L, gfp_peaks, gev, cv_min, pair_idx]
        
        # Save topomaps for the microstates
        save_path = f"{fig_save_path}Microstates/Fit_all_{ff}/"
        with open(f"{microstate_save_path}Reordered/Dualmicro_fit_all_{ff}_data_maps{n_maps}.pkl", "rb") as file:
            microstate_results = pickle.load(file)
        maps, m_labels, gfp_peaks, gev, cv_min, pair_idx = microstate_results
        fig = plot_dualmicro(n_maps, maps, gev, epoch.info)
        fig.savefig(save_path+f"Dualmicro_fit_all_{ff}_maps{n_maps}"+".png")
        
        # Save svg for Paper
        fig.savefig(save_path+f"Dualmicro_fit_all_{ff}_maps{n_maps}"+".svg")
        
        ### Save svg with fixed color scales across all microstates
        vlims = (np.min(maps), np.max(maps))
        
        fig = plot_dualmicro(n_maps, maps, gev, vlims, epoch.info, vlims)
        
        fig.savefig(save_path+f"Dualmicro_fit_all_{ff}_fixed_colorscale_maps{n_maps}"+".png")
        fig.savefig(save_path+f"Dualmicro_fit_all_{ff}_fixed_colorscale_maps{n_maps}"+".svg")
        
        # =========================================================================
        # # Estimate two-person microstate metrics/features
        # # There might be a small error introduced due to gaps in the time series from
        # # dropped segments, e.g. when calculating the transition probability as
        # # the time series is discontinuous due to the gaps. But with the high sampling rate
        # # only a very small fraction of the samples have discontinuous neighbors
        # =========================================================================
        """
        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)
        """
        # Hard-coded the optimal number of microstates based on CV criterion and GEV
        n_maps = 8
        # Load all microstate results
        with open(f"{microstate_save_path}Reordered/Dualmicro_fit_all_{ff}_data_maps{n_maps}.pkl", "rb") as file:
            microstate_results = pickle.load(file)
        # Load all trialinfos
        with open(f"{microstate_save_path}Dualmicro_fit_all_{ff}_trial_events_infos.pkl", "rb") as file:
            trialinfo_list = pickle.load(file)
        
        Microstate_names = [chr(ele) for ele in range(65,65+n_maps)]
        
        m_labels = [0]*n_pairs
        events = [0]*n_pairs
        m_feats = [0]*n_pairs
        
        for i in range(n_pairs):
            m_labels[i], events[i], m_feats[i] = dualmicro_fit_all_feature_computation(i)
            print(f"Finished computing microstate features for pair {Pair_id[i]}")
        
        # Save the raw microstate features
        with open(f"{microstate_save_path}/raw_dualmicro_fit_all_{ff}_features_maps{n_maps}.pkl", "wb") as filehandle:
            pickle.dump(m_feats, filehandle) # [Subject][Dur_arr,Occ_arr,TCo_arr,TMx_arr,Ent_arr] [Event, map*]
            # * the feature is calculated for each map, where applicable.
            # Transition matrix is calculated for each map -> map transition probability
        
        # with open(f"{microstate_save_path}/raw_computed_dualmicro_fit_all_{ff}_features.pkl", "rb") as file:
        #     m_feats = pickle.load(file) # [Subject][Dur_arr,Occ_arr,TCo_arr,TMx_arr,Ent_arr] [Event, map*]
        
        ### Convert all features to dataframes for further processing
        col_names = ["Pair_ID", "Event_ID", "Microstate", "Value"]
        col_values = [Pair_id,list(collapsed_event_id.keys()),Microstate_names]
        dtypes = [int,str,str,"float64"]
        # Mean duration
        Dur_arr = np.stack([ele[0] for ele in m_feats]) # [Subject, event, n_map]
        Dur_df = numpy_arr_to_pandas_df(Dur_arr, col_names, col_values, dtypes)
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["Duration"]*len(Dur_df)
        Dur_df.insert(2, "Measurement", measurement_id)
        # Save df
        Dur_df.to_pickle(os.path.join(microstate_save_path,"Dualmicro_fit_all_{ff}_duration_df.pkl"))
        
        # Frequency of occurrence per sec
        Occ_arr = np.stack([ele[1] for ele in m_feats]) # [Subject, event, n_map]
        Occ_df = numpy_arr_to_pandas_df(Occ_arr, col_names, col_values, dtypes)
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["Occurrence"]*len(Occ_df)
        Occ_df.insert(2, "Measurement", measurement_id)
        # Save df
        Occ_df.to_pickle(os.path.join(microstate_save_path,"Dualmicro_fit_all_{ff}_occurrence_df.pkl"))
        
        # Ratio total Time Covered
        TCo_arr = np.stack([ele[2] for ele in m_feats]) # [Subject, event, n_map]
        TCo_df = numpy_arr_to_pandas_df(TCo_arr, col_names, col_values, dtypes)
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["Time_covered"]*len(TCo_df)
        TCo_df.insert(2, "Measurement", measurement_id)
        # Save df
        TCo_df.to_pickle(os.path.join(microstate_save_path,"Dualmicro_fit_all_{ff}_ratio_time_covered_df.pkl"))
        
        # Transition matrix should be read as probability of row to column
        xi, xj = np.meshgrid(Microstate_names,Microstate_names)
        _, arrow = np.meshgrid(Microstate_names,["->"]*n_maps)
        
        transition_info = np.char.add(np.char.add(xj,arrow),xi)
        
        TMx_arr = np.stack([ele[3] for ele in m_feats]) # [Subject, event, n_map, n_map]
        TMx_arr = TMx_arr.reshape((n_pairs,len(collapsed_event_id),n_maps*n_maps)) # Flatten the maps to 1D
        
        col_names = ["Pair_ID", "Event_ID", "Transition", "Value"]
        col_values = [Pair_id,list(collapsed_event_id.keys()),transition_info.flatten()]
        TMx_df = numpy_arr_to_pandas_df(TMx_arr, col_names, col_values, dtypes)
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["Probability"]*len(TMx_df)
        TMx_df.insert(2, "Measurement", measurement_id)
        # Save df
        TMx_df.to_pickle(os.path.join(microstate_save_path,"Dualmicro_fit_all_{ff}_transition_df.pkl"))
        
        # Entropy
        Ent_arr = np.stack([ele[4] for ele in m_feats]) # [Subject, event]
        col_names = ["Pair_ID", "Event_ID", "Value"]
        col_values = [Pair_id,list(collapsed_event_id.keys())]
        dtypes = [int, str, "float64"]
        Ent_df = numpy_arr_to_pandas_df(Ent_arr, col_names, col_values, dtypes)
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["Entropy"]*len(Ent_df)
        Ent_df.insert(2, "Measurement", measurement_id)
        # Save df
        Ent_df.to_pickle(os.path.join(microstate_save_path,"Dualmicro_fit_all_{ff}_ratio_entropy_df.pkl"))
        
    # %% Backfit two-person microstates to pseudo-pairs
    # The pseudo-pairs are created for all participants except the real pair.
    # This is fine for symmetrical tasks, e.g. rest and coupled.
    # But not for assymmetrical tasks like observation and leader.
    # We might have a leader - leader pseudo-pair.
    # Hence we only look at ppn1 with ppn2 from different pairs and exclude
    # ppn1 with ppn1 or ppn2 with ppn2
    
    for f in len(all_freq_ranges):
        ff = freq_names[f]
        freq_range0 = all_freq_ranges[f]
        # =========================================================================
        # It might be an advantage to run the backfitting of microstates on a HPC
        # =========================================================================
        # To save time and prevent reloading the same EEG over and over, I divided
        # the prepare array function into a load and combine function
        # By loading all into memory, I can skip loading for every combination
        # but this requires a very high memory, which is fortunately not a problem on the hpc
        
        # I am limiting the pseudo-pairs to be where ppn1 ends with 1 and ppn2 with 2
        # Which means we have 21 * 20 options
        n_pseudo_pairs = n_pairs*(n_pairs-1)
        
        # To not load data 420 times for two participants, we preload all EEG data to ram
        c_time1 = time_now(); print("Starting load",c_time1)
        all_micro_data = [0]*n_subjects
        all_trial_data = [0]*n_subjects
        for i in range(n_subjects):
            all_micro_data[i], all_trial_data[i] = load_microstate_arrays(i)
        print("Load finished", time_now())
        
        # Get the prototypical alpha maps
        n_maps = 8
        with open(f"{microstate_save_path}Reordered/Dualmicro_fit_all_{ff}_data_maps{n_maps}.pkl", "rb") as file:
            microstate_results = pickle.load(file)
        
        prototype_map = microstate_results[0]
        
        # Start the backfitting
        m_labels = [0]*n_pseudo_pairs
        events = [0]*n_pseudo_pairs
        GEVs = [0]*n_pseudo_pairs
        counter = 0
        pseudo_pair_id = []
        for i in range(n_subjects):
            for j in range(n_subjects):
                # Skip if the subject is the same
                if np.abs(Subject_id[i]-Subject_id[j]) == 0:
                    continue
                # Skip if the subject are from the same pair
                if np.abs(Subject_id[i]-Subject_id[j]) == 1:
                    continue
                # Skip if ppn1 is not ending on 1, and ppn2 not ending on 2
                if not (str(Subject_id[i])[-1] == "1") & (str(Subject_id[j])[-1] == "2"):
                    continue
                # A valid pseudo pair
                else:
                    # Get the synchronized events
                    event0 = get_synch_events_from_pseudo_pairs(all_trial_data[i],all_trial_data[j])
                    # Get the preloaded micro data
                    micro_data1 = all_micro_data[i]
                    micro_data2 = all_micro_data[j]
                    # Get the synchronized and concatenated micro data in alpha
                    micro_data0 = combine_two_person_microstate_arrays(micro_data1, micro_data2, event0, sfreq, freq_range=freq_range0)
                    # Backfit and get the labels
                    L, GEV = pseudo_pair_dualmicro_backfitting(micro_data0, prototype_map, event0, n_maps, sfreq)
                    # Save the results
                    m_labels[counter], GEVs[counter], events[counter] = L, GEV, event0
                    pseudo_pair_id.append(f"{Subject_id[i]}-{Subject_id[j]}")
                    # Move counter
                    counter += 1
                    print(f"Finished backfitting for pseudo pair {pseudo_pair_id[-1]}")
                    print("Started", c_time1, "\nCurrent",time_now())
        
        backfit_results = [pseudo_pair_id, m_labels, GEVs, events]
        # Save the results from all pseudo pairs
        with open(f"{microstate_save_path}Reordered/Backfitting/Dualmicro_fit_all_{ff}_data_maps{n_maps}.pkl", "wb") as filehandle:
            pickle.dump(backfit_results, filehandle) # [pseudo_pair_id, L, GEV, events]
        
        # =========================================================================
        # Estimate two-person microstate metrics/features
        # There might be a small error introduced due to gaps in the time series from
        # dropped segments, e.g. when calculating the transition probability as
        # the time series is discontinuous due to the gaps. But with the high sampling rate
        # only a very small fraction of the samples have discontinuous neighbors
        # =========================================================================
        
        """
        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)
        """
        n_maps = 8
        # Load all the backfit pseudo-pair results
        with open(f"{microstate_save_path}Reordered/Backfitting/Dualmicro_fit_all_{ff}_data_maps{n_maps}.pkl", "rb") as file:
            backfit_results = pickle.load(file) # [pseudo_pair_id, L, GEV, events]
        
        # Hard-coded the optimal number of microstates based on CV criterion and GEV
        n_maps = 8
        Microstate_names = [chr(ele) for ele in range(65,65+n_maps)]
        
        pseudo_pair_id = backfit_results[0]
        n_pseudo_pairs = len(pseudo_pair_id)
        
        m_labels = [0]*n_pseudo_pairs
        events = [0]*n_pseudo_pairs
        m_feats = [0]*n_pseudo_pairs
        
        for i in range(n_pseudo_pairs):
            m_labels[i], events[i], m_feats[i] = dualmicro_fit_all_pseudo_pair_feature_computation(i,\
               n_maps, backfit_results, sfreq, event_id, collapsed_event_id)
            print(f"Finished computing microstate features for psuedo pair {pseudo_pair_id[i]}")
        
        # Save the raw microstate features
        with open(f"{microstate_save_path}/raw_dualmicro_fit_all_{ff}_pseudo_pairs_features_maps{n_maps}.pkl", "wb") as filehandle:
            pickle.dump(m_feats, filehandle) # [Subject][Dur_arr,Occ_arr,TCo_arr,TMx_arr,Ent_arr] [Event, map*]
            # * the feature is calculated for each map, where applicable.
            # Transition matrix is calculated for each map -> map transition probability
        
        # with open(f"{microstate_save_path}/raw_computed_dualmicro_fit_all_{ff}_features.pkl", "rb") as file:
        #     m_feats = pickle.load(file) # [Subject][Dur_arr,Occ_arr,TCo_arr,TMx_arr,Ent_arr] [Event, map*]
        
        ### Convert all features to dataframes for further processing
        col_names = ["Pseudo_Pair_ID", "Event_ID", "Microstate", "Value"]
        col_values = [pseudo_pair_id,list(collapsed_event_id.keys()),Microstate_names]
        dtypes = [str,str,str,"float64"]
        # Mean duration
        Dur_arr = np.stack([ele[0] for ele in m_feats]) # [Subject, event, n_map]
        Dur_df = numpy_arr_to_pandas_df(Dur_arr, col_names, col_values, dtypes)
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["Duration"]*len(Dur_df)
        Dur_df.insert(2, "Measurement", measurement_id)
        # Save df
        Dur_df.to_pickle(os.path.join(microstate_save_path,"Dualmicro_fit_all_{ff}_pseudo_pairs_duration_df.pkl"))
        
        # Frequency of occurrence per sec
        Occ_arr = np.stack([ele[1] for ele in m_feats]) # [Subject, event, n_map]
        Occ_df = numpy_arr_to_pandas_df(Occ_arr, col_names, col_values, dtypes)
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["Occurrence"]*len(Occ_df)
        Occ_df.insert(2, "Measurement", measurement_id)
        # Save df
        Occ_df.to_pickle(os.path.join(microstate_save_path,"Dualmicro_fit_all_{ff}_pseudo_pairs_occurrence_df.pkl"))
        
        # Ratio total Time Covered
        TCo_arr = np.stack([ele[2] for ele in m_feats]) # [Subject, event, n_map]
        TCo_df = numpy_arr_to_pandas_df(TCo_arr, col_names, col_values, dtypes)
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["Time_covered"]*len(TCo_df)
        TCo_df.insert(2, "Measurement", measurement_id)
        # Save df
        TCo_df.to_pickle(os.path.join(microstate_save_path,"Dualmicro_fit_all_{ff}_pseudo_pairs_ratio_time_covered_df.pkl"))
        
        # Transition matrix should be read as probability of row to column
        xi, xj = np.meshgrid(Microstate_names,Microstate_names)
        _, arrow = np.meshgrid(Microstate_names,["->"]*n_maps)
        
        transition_info = np.char.add(np.char.add(xj,arrow),xi)
        
        TMx_arr = np.stack([ele[3] for ele in m_feats]) # [Subject, event, n_map, n_map]
        TMx_arr = TMx_arr.reshape((n_pseudo_pairs,len(collapsed_event_id),n_maps*n_maps)) # Flatten the maps to 1D
        
        col_names = ["Pseudo_Pair_ID", "Event_ID", "Transition", "Value"]
        col_values = [pseudo_pair_id,list(collapsed_event_id.keys()),transition_info.flatten()]
        TMx_df = numpy_arr_to_pandas_df(TMx_arr, col_names, col_values, dtypes)
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["Probability"]*len(TMx_df)
        TMx_df.insert(2, "Measurement", measurement_id)
        # Save df
        TMx_df.to_pickle(os.path.join(microstate_save_path,"Dualmicro_fit_all_{ff}_pseudo_pairs_transition_df.pkl"))
        
        # Entropy
        Ent_arr = np.stack([ele[4] for ele in m_feats]) # [Subject, event]
        col_names = ["Pseudo_Pair_ID", "Event_ID", "Value"]
        col_values = [pseudo_pair_id,list(collapsed_event_id.keys())]
        dtypes = [str,str,"float64"]
        Ent_df = numpy_arr_to_pandas_df(Ent_arr, col_names, col_values, dtypes)
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["Entropy"]*len(Ent_df)
        Ent_df.insert(2, "Measurement", measurement_id)
        # Save df
        Ent_df.to_pickle(os.path.join(microstate_save_path,"Dualmicro_fit_all_{ff}_pseudo_pairs_ratio_entropy_df.pkl"))
    
    # %% eLORETA on Intrabrain microstates
    ### Make forward solutions
    # Computed using the fsaverage template MRI
    
    # # First time setup will need to download fsaverage templates
    # mne.datasets.fetch_fsaverage()
    
    fs_dir = "C:/Users/glia/mne_data/MNE-fsaverage-data/fsaverage"
    
    subjects_dir = os.path.dirname(fs_dir)
    trans = "fsaverage"
    src = os.path.join(fs_dir, "bem", "fsaverage-ico-5-src.fif")
    bem = os.path.join(fs_dir, "bem", "fsaverage-5120-5120-5120-bem-sol.fif")
    
    # Read the template sourcespace
    sourcespace = mne.read_source_spaces(src)
    
    # Since I use a template, I only need to make the forward operator once
    # As we assume the channel positions are fixed approximately the same
    # for all subjects using the same caps
    subject_eeg = epoch.copy()
    
    subject_eeg.set_eeg_reference(projection=True) # needed for inverse modelling
    
    # Make forward solution
    fwd = mne.make_forward_solution(subject_eeg.info, trans=trans, src=src,
                                bem=bem, eeg=True, mindist=5.0, n_jobs=1)
    
    # # Save forward operator
    # fname_fwd = "./Source_fwd/fsaverage_{}-fwd.fif".format(study_order[i])
    # mne.write_forward_solution(fname_fwd, fwd, overwrite=True)
    
    # # Check the alignment looks correct between EEG sensors and the template
    # mne.viz.plot_alignment(
    #     subject_eeg.info, trans, src=src, fwd=fwd, dig=True,
    #     meg=["helmet", "sensors"], subjects_dir=subjects_dir, surfaces="auto")
    
    ### Load Parcellation
    # Desikan-Killiany atlas (34 ROI from both hemispheres = 68 ROIs)
    # Named aparc.annot in MNE python fsaverage folder
    labels = mne.read_labels_from_annot("fsaverage", parc="aparc",
                                        subjects_dir=subjects_dir)
    labels = labels[:-1] # remove unknowns
    label_names = [label.name for label in labels]
    n_roi = len(labels)
    
    # Prepare brain lobe information
    Frontal_rois = ['superiorfrontal-lh','superiorfrontal-rh',
                    'rostralmiddlefrontal-lh','rostralmiddlefrontal-rh',
                    'caudalmiddlefrontal-lh','caudalmiddlefrontal-rh',
                    'parsopercularis-lh','parsopercularis-rh',
                    'parstriangularis-lh','parstriangularis-rh',
                    'parsorbitalis-lh','parsorbitalis-rh',
                    'lateralorbitofrontal-lh','lateralorbitofrontal-rh',
                    'medialorbitofrontal-lh','medialorbitofrontal-rh',
                    'precentral-lh','precentral-rh',
                    'paracentral-lh','paracentral-rh',
                    'frontalpole-lh','frontalpole-rh']
    Parietal_rois = ['superiorparietal-lh','superiorparietal-rh',
                     'inferiorparietal-lh','inferiorparietal-rh',
                     'supramarginal-lh','supramarginal-rh',
                     'postcentral-lh','postcentral-rh',
                     'precuneus-lh','precuneus-rh']
    Temporal_rois = ['superiortemporal-lh','superiortemporal-rh',
                     'middletemporal-lh','middletemporal-rh',
                     'inferiortemporal-lh','inferiortemporal-rh',
                     'bankssts-lh','bankssts-rh',
                     'fusiform-lh','fusiform-rh',
                     'transversetemporal-lh','transversetemporal-rh',
                     'entorhinal-lh','entorhinal-rh',
                     'temporalpole-lh','temporalpole-rh',
                     'parahippocampal-lh','parahippocampal-rh']
    Occipital_rois = ['lateraloccipital-lh','lateraloccipital-rh',
                      'lingual-lh','lingual-rh',
                      'cuneus-lh','cuneus-rh',
                      'pericalcarine-lh','pericalcarine-rh']
    Cingulate_rois = ['rostralanteriorcingulate-lh','rostralanteriorcingulate-rh',
                      'caudalanteriorcingulate-lh','caudalanteriorcingulate-rh',
                      'posteriorcingulate-lh','posteriorcingulate-rh',
                      'isthmuscingulate-lh','isthmuscingulate-rh']
    Insular_rois = ['insula-lh','insula-rh']
    
    Lobes = [Frontal_rois,Parietal_rois,Temporal_rois,Occipital_rois,Cingulate_rois,Insular_rois]
    
    Brain_region_labels = ["Frontal","Parietal","Temporal","Occipital","Cingulate","Insular"]
    Brain_region_hemi_labels = np.repeat(Brain_region_labels,2).astype("<U12")
    Brain_region_hemi_labels[::2] = [ele+"-lh" for ele in Brain_region_labels]
    Brain_region_hemi_labels[1::2] = [ele+"-rh" for ele in Brain_region_labels]
    
    Brain_region = np.array(label_names, dtype = "<U32")
    for l in range(len(Lobes)):
        Brain_region[np.array([i in Lobes[l] for i in Brain_region])] = Brain_region_labels[l]
    
    ### Concatenate the microstates into one Raw Object to apply inverse on it
    n_maps = 8
    Microstate_names = [chr(ele) for ele in range(65,65+n_maps)]
    
    for f in len(all_freq_ranges):
        ff = freq_names[f]
    
        with open(f"{microstate_save_path}Reordered/Intrabrain_microstate_fit_all_{ff}{n_maps}.pkl", "rb") as file:
            microstate_results = pickle.load(file)
        
        # Get the microstates and reshape to have channels in the first dim
        maps = microstate_results[0]
        maps = maps.transpose()
        
        raw_maps = mne.io.RawArray(maps,subject_eeg.info)
        raw_maps._filenames = [""] # Fix error with NoneType for "filename" for raw created with RawArray
        raw_maps.set_eeg_reference(projection=True) # needed for inverse modelling
        
        # Using assumption about equal variance and no correlations I make a diagonal matrix as cov
        noise_cov = mne.make_ad_hoc_cov(subject_eeg.info, None)
        
        # Make inverse operator
        # Using default depth parameter = 0.8 and free orientation (loose = 1)
        inverse_operator = mne.minimum_norm.make_inverse_operator(subject_eeg.info,
                                                                  fwd, noise_cov,
                                                                  loose = 1, depth = 0.8,
                                                                  verbose = 0)
        src_inv = inverse_operator["src"]
        # Compute inverse solution and retrieve the source localized microstate activities for each label
        # Define regularization
        snr = 3 # Default setting
        
        # Use eLORETA and only keep the activity normal to the cortical surface
        stc = mne.minimum_norm.apply_inverse_raw(raw_maps,inverse_operator,
                                                    lambda2 = 1/(snr**2),
                                                    pick_ori = "normal",
                                                    method = "eLORETA", verbose = 2)
        
        # Get the source activity in the ROIs
        label_activity = mne.extract_label_time_course(stc, labels, src_inv, mode="mean_flip",
                                         return_generator=False, verbose=0)
        
        # Visualize the microstates in source space
        # This way of plotting makes the color scale fixed across microstates
        brain = stc.plot(
            hemi="lh",
            subjects_dir=subjects_dir,
            smoothing_steps=1,
        )
        
        ### Convert Label Activity to Pandas DataFrame
        # With ROI names and then add Brain Region label
        col_names = ["ROI", "Microstate", "Value"]
        col_names = ["Microstate", "ROI", "Value"]
        col_val = [Microstate_names, label_names]
        
        # Create the source microstate activity dataframe
        sMicro_df = numpy_arr_to_pandas_df(label_activity.T, col_names = col_names, col_values = col_val)
        
        assert sMicro_df.loc[(sMicro_df["ROI"]==label_names[4])&
                                 (sMicro_df["Microstate"]==Microstate_names[3]),
                                 "Value"].iloc[0] == label_activity[4,3]
        
        # Add brain region information
        sMicro_df.insert(2, "Brain_region", np.tile(Brain_region,int(sMicro_df.shape[0]/n_roi)))
        sMicro_df["Brain_region"] = sMicro_df["Brain_region"].astype("category").\
                    cat.reorder_categories(Brain_region_labels, ordered=True)
        
        # Add hemisphere information
        sMicro_df.insert(3, "Hemisphere", [ele[-2:] for ele in sMicro_df["ROI"]])
        
        # Add a colum that combines brain region and hemisphere for plotting
        sMicro_df.insert(4, "Brain_region_hemi", [b+"-"+h for b, h in zip(sMicro_df["Brain_region"],sMicro_df["Hemisphere"])])
        sMicro_df["Brain_region_hemi"] = sMicro_df["Brain_region_hemi"].astype("category").\
                    cat.reorder_categories(Brain_region_hemi_labels, ordered=True)
        
        # Save the dataframe
        sMicro_df.to_pickle(os.path.join(microstate_save_path,f"Single_micro_{ff}_source_activity_df.pkl"))
    
    # %% eLORETA on two-brain microstates
    # Continued based on fwd operator and template loaded for intrabrain
    n_maps = 8
    Microstate_names = [chr(ele) for ele in range(65,65+n_maps)]
    
    for f in len(all_freq_ranges):
        ff = freq_names[f]
    
        with open(f"{microstate_save_path}Reordered/Dualmicro_fit_all_{ff}_data_maps{n_maps}.pkl", "rb") as file:
            microstate_results = pickle.load(file)
        
        # Get the microstates
        maps = microstate_results[0]
        maps = maps.reshape(2*n_maps,n_channels)
        
        # # Check the maps were split properly
        # plot_microstates(n_maps, maps[:8], microstate_results[3])
        # plot_microstates(n_maps, maps[8:], microstate_results[3])
        # Maps are ordered as: ppn1 A, ppn2 A, ppn1 B, ppn2 B etc
        
        # Transpose to have channels in the first dim
        maps = maps.transpose()
        
        raw_maps = mne.io.RawArray(maps,subject_eeg.info)
        raw_maps._filenames = [""] # Fix error with NoneType for "filename" for raw created with RawArray
        raw_maps.set_eeg_reference(projection=True) # needed for inverse modelling
        
        # Using assumption about equal variance and no correlations I make a diagonal matrix as cov
        noise_cov = mne.make_ad_hoc_cov(subject_eeg.info, None)
        
        # Make inverse operator
        # Using default depth parameter = 0.8 and free orientation (loose = 1)
        inverse_operator = mne.minimum_norm.make_inverse_operator(subject_eeg.info,
                                                                  fwd, noise_cov,
                                                                  loose = 1, depth = 0.8,
                                                                  verbose = 0)
        src_inv = inverse_operator["src"]
        # Compute inverse solution and retrieve the source localized microstate activities for each label
        # Define regularization
        snr = 3 # Default setting
        
        # Use eLORETA and only keep the activity normal to the cortical surface
        stc = mne.minimum_norm.apply_inverse_raw(raw_maps,inverse_operator,
                                                    lambda2 = 1/(snr**2),
                                                    pick_ori = "normal",
                                                    method = "eLORETA", verbose = 2)
        
        # Get the source activity in the ROIs
        label_activity = mne.extract_label_time_course(stc, labels, src_inv, mode="mean_flip",
                                         return_generator=False, verbose=0)
        
        # Visualize the microstates in source space
        # This way of plotting makes the color scale fixed across microstates
        brain = stc.plot(
            hemi="lh",
            subjects_dir=subjects_dir,
            smoothing_steps=1,
        )
        
        # Visualize with different color scales for each microstate
        Microstate_names2 = np.repeat(Microstate_names,2).astype("<U2")
        Microstate_names2[::2] = [ele+"1" for ele in Microstate_names]
        Microstate_names2[1::2] = [ele+"2" for ele in Microstate_names]
        
        # Save source activations for each microstate
        # Lateral and medial for each hemisphere + dorsal + flatmaps
        save_path = f"{fig_save_path}Microstates/SourceDualmicroPrototypes/"
        
        hemis = ["lh","rh"]
        views = ["lateral","medial"]
        
        for i in range(len(Microstate_names2)):
            times0 = np.linspace(0,1,sfreq+1)[:2*n_maps+1]
            stc0 = stc.copy().crop(times0[i],times0[i+1],include_tmax=False)
            # Color bar limits defined as max saturation of top 1% (yellow or teal)
            # middle at 5%, which means they will have alpha = 1 and progressively be
            # closer to yellow or teal
            # Lower boundary at 10%, which means they will be red/blue but with decreased
            # transparency
            clim_max = -(np.sort(-np.abs(stc0.data),axis=0)[stc0.shape[0]//100])[0]
            clim_mid = -(np.sort(-np.abs(stc0.data),axis=0)[stc0.shape[0]//20])[0]
            clim_min = -(np.sort(-np.abs(stc0.data),axis=0)[stc0.shape[0]//10])[0]
            clim0 = {"kind":"value","pos_lims":[clim_min,clim_mid,clim_max]}
            
            # Lateral and medial
            for h in range(len(hemis)):
                hh = hemis[h]
                brain = stc0.plot(
                    hemi=hh,
                    subjects_dir=subjects_dir,
                    smoothing_steps=10, # spatial smoothing
                    colorbar=False,
                    background="white",
                    cortex="classic",
                    size=800,
                    transparent=True,
                    views=views[0],
                    clim=clim0,
                )
                brain.save_image(os.path.join(save_path, f"Dualmicro_source_{Microstate_names2[i]}_{hh}_{views[0]}"+".png"))
                brain.show_view(views[1])
                brain.save_image(os.path.join(save_path, f"Dualmicro_source_{Microstate_names2[i]}_{hh}_{views[1]}"+".png"))
            
            # Dorsal map
            brain = stc0.plot(
                hemi="both",
                subjects_dir=subjects_dir,
                smoothing_steps=10, # spatial smoothing
                colorbar=True,
                background="white",
                cortex="classic",
                size=1500,
                transparent=True,
                views="dorsal",
                clim=clim0,
            )
            brain.save_image(os.path.join(save_path, f"Dualmicro_source_{Microstate_names2[i]}_dorsal"+".png"))
            
            # Flat map
            brain = stc0.plot(
                hemi="both",
                surface="flat",
                subjects_dir=subjects_dir,
                smoothing_steps=10, # spatial smoothing
                colorbar=False,
                background="white",
                cortex="classic",
                size=1500,
                transparent=True,
                views="flat",
                clim=clim0,
            )
            brain.save_image(os.path.join(save_path, f"Dualmicro_source_{Microstate_names2[i]}_flat"+".png"))
            # Close all figures
            mne.viz.close_all_3d_figures()
        
        # Mean
        brain = stc.mean().plot(
            hemi="lh",
            subjects_dir=subjects_dir,
            smoothing_steps=10,
        )
        
        ### Convert Label Activity to Pandas DataFrame
        # With ROI names and then add Brain Region label
        col_names = ["ROI", "Microstate", "Value"]
        col_names = ["Microstate", "ROI", "Value"]
        col_val = [Microstate_names2, label_names]
        dtypes = [str, str, "float64"]
        
        # Create the source microstate activity dataframe
        sMicro_df = numpy_arr_to_pandas_df(label_activity.T, col_names, col_val, dtypes)
        
        assert sMicro_df.loc[(sMicro_df["ROI"]==label_names[4])&
                                 (sMicro_df["Microstate"]==Microstate_names2[3]),
                                 "Value"].iloc[0] == label_activity[4,3]
        
        # Add brain region information
        sMicro_df.insert(2, "Brain_region", np.tile(Brain_region,int(sMicro_df.shape[0]/n_roi)))
        sMicro_df["Brain_region"] = sMicro_df["Brain_region"].astype("category").\
                    cat.reorder_categories(Brain_region_labels, ordered=True)
        
        # Add hemisphere information
        sMicro_df.insert(3, "Hemisphere", [ele[-2:] for ele in sMicro_df["ROI"]])
        
        # Add a colum that combines brain region and hemisphere for plotting
        sMicro_df.insert(4, "Brain_region_hemi", [b+"-"+h for b, h in zip(sMicro_df["Brain_region"],sMicro_df["Hemisphere"])])
        sMicro_df["Brain_region_hemi"] = sMicro_df["Brain_region_hemi"].astype("category").\
                    cat.reorder_categories(Brain_region_hemi_labels, ordered=True)
        
        # Save the dataframe
        sMicro_df.to_pickle(os.path.join(microstate_save_path,"Dualmicro_{ff}_source_activity_df.pkl"))
    
    # %% LRTC with DFA on Two-person microstate label time series
    # Using Detrended Fluctuation Analysis (DFA)
    # Adapted from Python Implementation by Arthur-Ervin Avramiea <a.e.avramiea@vu.nl>
    # From NBT2 toolbox
    """
    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)
    
    If 0 < alpha < 0.5: The process exhibits anti-correlations
    If 0.5 < alpha < 1: The process exhibits positive correlations
    If alpha = 0.5: The process is indistinguishable from a random process
    If 1.0 < alpha < 2.0: The process is non-stationary. H = alpha - 1
    
    Window sizes should be equally spaced on a logarithmic scale
    Sizes should be at least 4 samples and up to 10% of total signal length
    
    ### Specific for our microstate DFA analysis
    We have 8 microstates, but to compute the random walk we will partition
    the microstate sequence into two classes (see reference on microstate Hurst
    https://pubmed.ncbi.nlm.nih.gov/20921381/)
    
    A/B/C/D will be assigned the positive direction, while E/F/G/H will be
    assigned the negative direction, corresponding to whether ppn1 or ppn2
    are in one of the canonical microstates, while the other have a non-specific
    (average) topography.
    
    Each 25s trial is too short to estimate LRTC on, so I will concatenate all
    the trials corresponding to each condition.
    
    This should yield up to 25s * 16 trials = 400s of data for each condition,
    except rest which is up to 120s * 2 trials = 240s
    
    DFA is computed from 8 trials and then averaged, to avoid the
    problem of flipping in the asymmetric trials. We change windows size to 5-20s
    To ensure consistency the same procedure is applied to the symmetric trials
    
    """
    
    # Window sizes
    compute_interval = [5,20] # the window sizes should be between 5s and 30s
    # Compute DFA window sizes for the given Interval
    window_sizes = np.floor(np.logspace(-1,3,40) * sfreq).astype(int) # %logspace from 0.1 seccond (10^-1) to 1000 (10^3) seconds
    window_sizes = window_sizes[(window_sizes >= compute_interval[0]*sfreq) & \
        (window_sizes <= compute_interval[1]*sfreq)]
    
    for f in len(all_freq_ranges):
        ff = freq_names[f]
        # Nolds are already using all cores so multiprocessing with concurrent makes it slower
        n_maps = 8
        
        with open(f"{microstate_save_path}Reordered/Dualmicro_fit_all_{ff}_data_maps{n_maps}.pkl", "rb") as file:
            microstate_results = pickle.load(file)
        # Load all trialinfos
        with open(f"{microstate_save_path}Dualmicro_fit_all_{ff}_trial_events_infos.pkl", "rb") as file:
            trialinfo_list = pickle.load(file)
        
        # Pre-allocate memory
        DFA_arr = np.zeros((n_pairs,len(collapsed_event_id)))
        Fluctuation_arr = np.zeros((n_pairs,len(collapsed_event_id),len(window_sizes)))
        
        # Get start time
        c_time1 = time.localtime()
        c_time1 = time.strftime("%a %d %b %Y %H:%M:%S", c_time1)
        print("Started {}".format(c_time1))
        # Nolds are already using all cores so concurrent futures with make it slower
        for i in range(n_pairs):
            # Compute DFA
            dfa_temp, fluc_temp = compute_dualmicro_DFA(i, microstate_results, 
               trialinfo_list, sfreq, window_sizes, event_id, collapsed_event_id, True)
            # Save to array
            DFA_arr[i] = dfa_temp
            Fluctuation_arr[i] = fluc_temp
            print("Finished {} out of {} pairs".format(i+1,n_pairs))
        
        # Get ending time
        c_time2 = time.localtime()
        c_time2 = time.strftime("%a %d %b %Y %H:%M:%S", c_time2)
        print(("Started {} \nEnded Time {}".format(c_time1,c_time2)))
        
        # Save the raw DFA analysis data 
        np.save(microstate_save_path+"DFA_arr.npy", DFA_arr)
        np.save(microstate_save_path+"Fluctuation_arr.npy", Fluctuation_arr)
        
        # Convert to Pandas dataframe (DFA exponent)
        col_names = ["Pair_ID", "Event_ID", "Value"]
        col_values = [Pair_id,list(collapsed_event_id.keys())]
        dtypes = ["int64",str,"float64"]
        
        DFA_df = numpy_arr_to_pandas_df(DFA_arr, col_names, col_values, dtypes)
        
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["DFA"]*len(DFA_df)
        DFA_df.insert(2, "Measurement", measurement_id)
        # Save df
        DFA_df.to_pickle(os.path.join(microstate_save_path,f"Dualmicro_{ff}_DFA_exponent_df.pkl"))
    
    # %% DFA in pseudo-pairs
    for f in len(all_freq_ranges):
        ff = freq_names[f]
        # Nolds are already using all cores so multiprocessing with concurrent makes it slower
        n_maps = 8
        
        # Load all the backfit pseudo-pair results
        with open(f"{microstate_save_path}Reordered/Backfitting/Dualmicro_fit_all_{ff}_data_maps{n_maps}.pkl", "rb") as file:
            backfit_results = pickle.load(file) # [pseudo_pair_id, L, GEV, events]
            
        # Pre-allocate memory
        DFA_arr = np.zeros((n_pairs,len(collapsed_event_id)))
        Fluctuation_arr = np.zeros((n_pairs,len(collapsed_event_id),len(window_sizes)))
        
        # Get start time
        c_time1 = time.localtime()
        c_time1 = time.strftime("%a %d %b %Y %H:%M:%S", c_time1)
        print("Started {}".format(c_time1))
        # Nolds are already using all cores so concurrent futures with make it slower
        for i in range(n_pairs):
            # Compute DFA
            dfa_temp, fluc_temp = compute_dualmicro_DFA_pseudo(i, backfit_results,
               sfreq, window_sizes, event_id, collapsed_event_id, True)
            # Save to array
            DFA_arr[i] = dfa_temp
            Fluctuation_arr[i] = fluc_temp
            print("Finished {} out of {} pairs".format(i+1,n_pairs))
        
        # Get ending time
        c_time2 = time.localtime()
        c_time2 = time.strftime("%a %d %b %Y %H:%M:%S", c_time2)
        print(("Started {} \nEnded Time {}".format(c_time1,c_time2)))
        
        # Save the raw DFA analysis data 
        np.save(microstate_save_path+"DFA_arr.npy", DFA_arr)
        np.save(microstate_save_path+"Fluctuation_arr.npy", Fluctuation_arr)
        
        # Convert to Pandas dataframe (DFA exponent)
        col_names = ["Pair_ID", "Event_ID", "Value"]
        col_values = [Pair_id,list(collapsed_event_id.keys())]
        dtypes = ["int64",str,"float64"]
        
        DFA_df = numpy_arr_to_pandas_df(DFA_arr, col_names, col_values, dtypes)
        
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["DFA"]*len(DFA_df)
        DFA_df.insert(2, "Measurement", measurement_id)
        # Save df
        DFA_df.to_pickle(os.path.join(microstate_save_path,f"Dualmicro_{ff}_DFA_exponent_df.pkl"))
    
    # %% Time-lagged inter-brain microstate synchrony
    # Hard-coded the optimal number of microstates based on CV criterion and GEV
    n_maps = 5
    
    # The lag (number of samples) we will iterate over to find greatest time-lagged interbrain microstate synchrony
    lag_search_range = sfreq # 1 second in both directions
    lag_interval = np.linspace(-lag_search_range,lag_search_range,lag_search_range*2+1).astype(int)
    
    Microstate_names = [chr(ele) for ele in range(65,65+n_maps)]
    # Insert Z as the symbol for non common microstate
    Microstate_names.insert(0,"Z")
    
    # Loop over frequencies
    for f in len(all_freq_ranges):
        ff = freq_names[f]
    
        # Load all microstate results
        with open(f"{microstate_save_path}Reordered/Intrabrain_microstate_fit_all_{ff}{n_maps}.pkl", "rb") as file:
            microstate_results = pickle.load(file)
        # Load all trialinfos
        with open(f"{microstate_save_path}Intrabrain_microstate_fit_all_{ff}_trialinfos.pkl", "rb") as file:
            trialinfo_list = pickle.load(file)
        
        m_labels = [0]*(n_subjects//2)
        events = [0]*(n_subjects//2)
        m_feats = [0]*(n_subjects//2)
        shift_info = [0]*(n_subjects//2)
        Pair_id = [0]*(n_subjects//2)
        
        for i in tqdm(range(n_subjects//2)):
            m_labels[i], events[i], m_feats[i], shift_info[i] = shifted_interbrain_microstate_feature_computation(i,
                   n_maps, microstate_results, trialinfo_list, sfreq,
                   event_id, collapsed_event_id, lag_search_range, lag_interval)
            Pair_id[i] = int(str(Subject_id[2*i])[1:-1])
            print(f"Finished computing interbrain microstate features for pair {Pair_id[i]}")
        
        Pair_id = [ele+100 for ele in Pair_id]
        
        # Save the raw microstate features
        with open(f"{microstate_save_path}/raw_shifted_interbrain_single_micro_fit_all_{ff}_maps{n_maps}.pkl", "wb") as filehandle:
            pickle.dump([Pair_id, m_feats, shift_info], filehandle) # [Subject][Dur_arr,Occ_arr,TCo_arr,TMx_arr,Ent_arr][Event, map*]
            # * the feature is calculated for each map, where applicable.
            # Transition matrix is calculated for each map -> map transition probability
            # The first row and column correspond to the non common microstate, i.e.
            # there is a different microstate in the pair
        
        # with open(f"{microstate_save_path}/raw_shifted_interbrain_single_micro_fit_all_{ff}_maps{n_maps}.pkl", "rb") as file:
        #     Pair_id, m_feats, shift_info = pickle.load(file) # [Subject][Dur_arr,Occ_arr,TCo_arr,TMx_arr,Ent_arr] [Event, map*]
        
        n_pairs = len(Pair_id)
        
        ### Convert all features to dataframes for further processing
        col_names = ["Pair_ID", "Event_ID", "Microstate", "Value"]
        col_values = [Pair_id,list(collapsed_event_id.keys()),Microstate_names]
        dtypes = [int,str,str,"float64"]
        
        # Ratio total Time Covered
        TCo_arr = np.stack([ele[2] for ele in m_feats]) # [Subject, event, n_map]
        TCo_df = numpy_arr_to_pandas_df(TCo_arr, col_names, col_values, dtypes)
        # Add dummy variable to enabling combining of dataframes
        measurement_id = ["Time_covered"]*len(TCo_df)
        TCo_df.insert(2, "Measurement", measurement_id)
        # Save df
        TCo_df.to_pickle(os.path.join(microstate_save_path,f"Shifted_IB_Single_micro_fit_all_{ff}_maps{n_maps}_ratio_time_covered_df.pkl"))