Skip to content

Commit

Permalink
Merge branch 'new_cluster_stats_api_GSOC24' of https://github.com/Car…
Browse files Browse the repository at this point in the history
…inaFo/mne-python into new_cluster_stats_api_GSOC24
  • Loading branch information
CarinaFo committed Jun 14, 2024
2 parents fa5b215 + 1a1511d commit 0ea220c
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions tutorials/stats-sensor-space/76_new_cluster_test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,15 @@ def old_api_cluster(n_permutations: int = 10000, seed: int = 1234):

return T_obs, clusters, cluster_p_values, H0


def create_random_evokeds_id_condition_list():
"""
Create a list of shuffled participant IDs, conditions, and evoked data.
# Keep the participant IDs and conditions paired but shuffle the order of the evoked data.
"""
import random

_ , evoked_data_a, evoked_data_b = prep_sample_data()
_, evoked_data_a, evoked_data_b = prep_sample_data()

# Example participant IDs
participant_ids = ["p1", "p2", "p3", "p4", "p5"] * 2
Expand Down Expand Up @@ -160,11 +161,14 @@ def create_random_paired_evokeds_list():
Create a list of shuffled evoked data where each pair of target and non-target evoked data is shuffled together.
"""
import random

_, evoked_data_a, evoked_data_b = prep_sample_data()

# Ensure evoked_data_a and evoked_data_b are of the same length
assert len(evoked_data_a) == len(evoked_data_b), "evoked_data_a and evoked_data_b must have the same length"

assert len(evoked_data_a) == len(
evoked_data_b
), "evoked_data_a and evoked_data_b must have the same length"

# Create a list of participant indices
participant_indices = list(range(len(evoked_data_a)))

Expand All @@ -188,7 +192,10 @@ def create_random_paired_evokeds_list():
original_evoked_data, shuffled_evoked_data = create_random_paired_evokeds_list()
# shouldn't change the results (p-value is different though?)

shuffled_participant_ids, shuffled_conditions, shuffled_evoked_data = create_random_evokeds_id_condition_list()
shuffled_participant_ids, shuffled_conditions, shuffled_evoked_data = (
create_random_evokeds_id_condition_list()
)


def prepare_dataframe_for_cluster_function(
evokeds: list = None,
Expand All @@ -215,27 +222,34 @@ def prepare_dataframe_for_cluster_function(
The prepared DataFrame for the cluster test function.
"""
# Initialize the DataFrame with evoked data
df = pd.DataFrame({
"evoked": evokeds,
"condition": condition if condition is not None else np.nan,
"subject_index": subject_index if subject_index is not None else np.nan
})
df = pd.DataFrame(
{
"evoked": evokeds,
"condition": condition if condition is not None else np.nan,
"subject_index": subject_index if subject_index is not None else np.nan,
}
)

return df


# run with original data
df = prepare_dataframe_for_cluster_function(evokeds=original_evoked_data,
condition=None,
subject_index=None)
df = prepare_dataframe_for_cluster_function(
evokeds=original_evoked_data, condition=None, subject_index=None
)

df = prepare_dataframe_for_cluster_function(
evokeds=shuffled_evoked_data, condition=None, subject_index=None
)

df = prepare_dataframe_for_cluster_function(evokeds=shuffled_evoked_data,
condition=None,
subject_index=None)
df = prepare_dataframe_for_cluster_function(
evokeds=shuffled_evoked_data,
condition=shuffled_conditions,
subject_index=shuffled_participant_ids,
)

df = prepare_dataframe_for_cluster_function(evokeds=shuffled_evoked_data,
condition=shuffled_conditions,
subject_index=shuffled_participant_ids)

cluster_test(df)

def cluster_test(
df: pd.DataFrame,
Expand Down Expand Up @@ -269,8 +283,8 @@ def cluster_test(
The permuted test statistics.
"""
# Check if conditions and subject_index are present and valid
conditions_present = pd.notna(df['condition']).all()
subject_index_present = pd.notna(df['subject_index']).all()
conditions_present = pd.notna(df["condition"]).all()
subject_index_present = pd.notna(df["subject_index"]).all()

if contrast == 1:
if conditions_present:
Expand Down

0 comments on commit 0ea220c

Please sign in to comment.