Skip to content

Commit a962d1a

Browse files
added 'save' param to 'phase_synchrony_metrics'
1 parent d803527 commit a962d1a

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

pyleida/_data_loader.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ def overlap_withyeo(self,parcellation=None,n_areas=None,k=2,state=None,darkstyle
601601

602602
return overlap
603603

604-
def phase_synchrony_metrics(self,k=2):
604+
def phase_synchrony_metrics(self,k=2,save=True):
605605
"""
606606
For each subject, compute the Kuramoto order
607607
parameter for each community at each time t.
@@ -622,6 +622,10 @@ def phase_synchrony_metrics(self,k=2):
622622
k : int.
623623
Select the K partition of interest.
624624
625+
save : bool. Default = True.
626+
Whether to save the results in
627+
.csv files in 'dynamics_metrics/'.
628+
625629
Returns:
626630
--------
627631
kuramoto : pd.DataFrame.
@@ -648,6 +652,7 @@ def phase_synchrony_metrics(self,k=2):
648652

649653
kura_lst = []
650654

655+
print('-Computing the Kuramoto order parameter.')
651656
for sub_id in subject_ids: #for each subject
652657
#get current subject signals
653658
tseries = time_series[sub_id]
@@ -676,6 +681,7 @@ def phase_synchrony_metrics(self,k=2):
676681

677682
# Compute synchronization of each
678683
# community as their time-average
684+
print('\n-Computing synchronization')
679685
synchronization = kuramoto.groupby(['subject_id','condition']).mean().reset_index()
680686

681687
# Compute global synchronization
@@ -688,6 +694,7 @@ def phase_synchrony_metrics(self,k=2):
688694

689695
# Compute metastability of each community
690696
# as their std across time.
697+
print('-Computing metastability.')
691698
metastability = kuramoto.groupby(['subject_id','condition']).std().reset_index()
692699

693700
# Compute global metastability
@@ -698,6 +705,16 @@ def phase_synchrony_metrics(self,k=2):
698705

699706
metastability['global'] = global_meta
700707

708+
if save:
709+
try:
710+
res_pth = f'{self._results_path_}/dynamics_metrics/k_{k}'
711+
print(f'Saving results to {res_pth}')
712+
kuramoto.to_csv(f'{res_pth}/kuramoto.csv',index=False)
713+
synchronization.to_csv(f'{res_pth}/synchronization.csv',index=False)
714+
metastability.to_csv(f'{res_pth}/metastability.csv',index=False)
715+
except:
716+
print(f'An error occured when saving the results to {res_pth}')
717+
701718
return kuramoto,synchronization,metastability
702719

703720
def phase_locking_metastability(self,k):

pyleida/_leida.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ def _pool_stats(self,metric="occupancies"):
10421042
data = pd.concat((data),ignore_index=True)
10431043
return data
10441044

1045-
def phase_synchrony_metrics(self,k=2):
1045+
def phase_synchrony_metrics(self,k=2,save=True):
10461046
"""
10471047
For each subject, compute the Kuramoto order
10481048
parameter for each community at each time t.
@@ -1063,6 +1063,10 @@ def phase_synchrony_metrics(self,k=2):
10631063
k : int.
10641064
Select the K partition of interest.
10651065
1066+
save : bool. Default = True.
1067+
Whether to save the results in
1068+
.csv files in 'dynamics_metrics/'.
1069+
10661070
Returns:
10671071
--------
10681072
kuramoto : pd.DataFrame.
@@ -1088,6 +1092,7 @@ def phase_synchrony_metrics(self,k=2):
10881092

10891093
kura_lst = []
10901094

1095+
print('-Computing the Kuramoto order parameter.')
10911096
for sub_id in subject_ids: #for each subject
10921097
#get current subject signals
10931098
tseries = self.time_series[sub_id]
@@ -1116,6 +1121,7 @@ def phase_synchrony_metrics(self,k=2):
11161121

11171122
# Compute synchronization of each
11181123
# community as their time-average
1124+
print('\n-Computing synchronization')
11191125
synchronization = kuramoto.groupby(['subject_id','condition']).mean().reset_index()
11201126

11211127
# Compute global synchronization
@@ -1128,6 +1134,7 @@ def phase_synchrony_metrics(self,k=2):
11281134

11291135
# Compute metastability of each community
11301136
# as their std across time.
1137+
print('-Computing metastability.')
11311138
metastability = kuramoto.groupby(['subject_id','condition']).std().reset_index()
11321139

11331140
# Compute global metastability
@@ -1138,6 +1145,16 @@ def phase_synchrony_metrics(self,k=2):
11381145

11391146
metastability['global'] = global_meta
11401147

1148+
if save:
1149+
try:
1150+
res_pth = f'{self._results_path_}/dynamics_metrics/k_{k}'
1151+
print(f'Saving results to {res_pth}')
1152+
kuramoto.to_csv(f'{res_pth}/kuramoto.csv',index=False)
1153+
synchronization.to_csv(f'{res_pth}/synchronization.csv',index=False)
1154+
metastability.to_csv(f'{res_pth}/metastability.csv',index=False)
1155+
except:
1156+
print(f'An error occured when saving the results to {res_pth}')
1157+
11411158
return kuramoto,synchronization,metastability
11421159

11431160
def phase_locking_metastability(self,k):

0 commit comments

Comments
 (0)