-
Notifications
You must be signed in to change notification settings - Fork 576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Trouble running arithmetic in second_level_contrast of non_parametric_inference #4140
Comments
Thx for posting. Indeed this function only supports contrasts corresponding to design matrix columns and not arbitrary combinations. I think that it should call https://github.com/nilearn/nilearn/blob/main/nilearn/glm/contrasts.py#L20 |
isn't that a duplicate of issue #3807 ? |
Yes it's the same issue. I've looked into it but didn't seem as simple as calling the |
I think that this resonates with the conclusion I came to after a "shallow dive". I would prefer to keep only issue for this. Which one though? |
Feel free to close this! I should have found the previous one during search! Sorry. |
With over 250 issues open even the maintainers may be struggling to find potential duplicates, so honestly I would not expect contributors to easily find them. 😉 |
@andreifoldes |
Adapted code to reproduce and test import numpy as np
import nibabel as nib
from nilearn.glm.second_level import non_parametric_inference
def create_random_nii(filename, shape=(20, 20, 20), affine=np.eye(4)):
data = np.random.rand(*shape)
img = nib.Nifti1Image(data, affine)
nib.save(img, filename)
return filename
# Number of subjects and conditions
num_subjects = 10
num_conditions = 2
# Create four random .nii.gz files and add them to a list
nii_files = [create_random_nii(f'dataset{i}.nii.gz') for i in range(num_subjects * num_conditions)]
print(nii_files)
def generate_file_names(num_conditions, num_participants):
files = []
for i in range(1, num_participants + 1):
for j in range(1, num_conditions + 1):
files.append(f'sub-{i}_cond-{j}.nii.gz')
return files
files = generate_file_names(num_conditions, num_subjects)
# Initialize the design matrix
design_matrix = np.zeros((len(files), num_conditions + num_subjects))
# Iterate over the files
for i, file in enumerate(files):
# Split the file name to get the subject and condition
parts = file.split('_')
subject = int(parts[0].split('-')[1])
condition = int(parts[1].split('-')[1].split('.')[0])
# Assign a value of 1 for the corresponding condition
design_matrix[i, condition-1] = 1
# Assign a value of 1 for the corresponding subject
design_matrix[i, num_conditions + subject - 1] = 1
print("Design matrix:")
print(design_matrix)
# turn design matrix into a dataframe
import pandas as pd
# Convert design matrix to dataframe
design_matrix_df = pd.DataFrame(design_matrix)
# name first column H1 and second column H2 and rest of columns subject numbers
design_matrix_df.columns = ['H1', 'H2'] + [f'Subject {i}' for i in range(1, num_subjects + 1)]
out_dict = non_parametric_inference(
nii_files,
design_matrix=design_matrix_df,
second_level_contrast="H1-H2",
model_intercept=True,
n_perm=100, # 500 for the sake of time. Ideally, this should be 10,000.
two_sided_test=False,
smoothing_fwhm=None,
n_jobs=-1,
verbose=3,
threshold=0.01,
) |
Is there an existing issue for this?
Operating system
Operating system version
Rocky Linux release 8.8 (Green Obsidian)
Python version
nilearn version
0.10.2
Expected behavior
I would like to a run a paired t-test using
nilearn.glm.second_level.non_parametric_inference
. The documentation states thatdesign_matrix_df
H1 H2 Subject 1 Subject 2
0 1.0 0.0 1.0 0.0
1 0.0 1.0 1.0 0.0
2 1.0 0.0 0.0 1.0
3 0.0 1.0 0.0 1.0
Current behavior & error messages
I was able to run second_level_contrast="H1", second_level_contrast="H2", but not second_level_contrast="H1-H2"
Steps and code to reproduce bug
The text was updated successfully, but these errors were encountered: