@@ -65,42 +65,25 @@ def scalp_coupling_index(
6565
6666 sci = np .zeros (picks .shape )
6767
68- if n_wavelengths == 2 :
69- # Use pairwise correlation for 2 wavelengths (backward compatibility)
70- for ii in range (0 , len (picks ), 2 ):
68+ # Calculate all pairwise correlations within each group and use the minimum as SCI
69+ pair_indices = np .triu_indices (n_wavelengths , k = 1 )
70+ for gg in range (0 , len (picks ), n_wavelengths ):
71+ group_data = filtered_data [gg : gg + n_wavelengths ]
72+
73+ # Calculate pairwise correlations within the group
74+ correlations = np .zeros (pair_indices [0 ].shape [0 ])
75+
76+ for n , (ii , jj ) in enumerate (zip (* pair_indices )):
7177 with np .errstate (invalid = "ignore" ):
72- c = np .corrcoef (filtered_data [ii ], filtered_data [ii + 1 ])[0 ][1 ]
73- if not np .isfinite (c ): # someone had std=0
74- c = 0
75- sci [ii ] = c
76- sci [ii + 1 ] = c
77- else :
78- # For multiple wavelengths: calculate all pairwise correlations within each group
79- # and use the minimum as the quality metric
80-
81- # Group picks by number of wavelengths
82- # Drops last incomplete group, but we're assuming valid data
83- pick_iter = iter (picks )
84- pick_groups = zip (* [pick_iter ] * n_wavelengths )
85-
86- for group_picks in pick_groups :
87- group_data = filtered_data [group_picks ]
88-
89- # Calculate pairwise correlations within the group
90- pair_indices = np .triu_indices (len (group_picks ), k = 1 )
91- correlations = np .zeros (pair_indices [0 ].shape [0 ])
92-
93- for n , (ii , jj ) in enumerate (zip (* pair_indices )):
94- with np .errstate (invalid = "ignore" ):
95- c = np .corrcoef (group_data [ii ], group_data [jj ])[0 ][1 ]
96- if np .isfinite (c ):
97- correlations [n ] = c
98-
99- # Use minimum correlation as the quality metric
100- group_sci = correlations .min ()
101-
102- # Assign the same SCI value to all channels in the group
103- sci [group_picks ] = group_sci
78+ c = np .corrcoef (group_data [ii ], group_data [jj ])[0 ][1 ]
79+ if np .isfinite (c ):
80+ correlations [n ] = c
81+
82+ # Use minimum correlation as SCI
83+ group_sci = correlations .min ()
84+
85+ # Assign the same SCI value to all channels in the group
86+ sci [gg : gg + n_wavelengths ] = group_sci
10487
10588 sci [zero_mask ] = 0
10689 sci = sci [np .argsort (picks )] # restore original order
0 commit comments