Skip to content
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

refactor tests to use context manager #3796

Closed
Remi-Gau opened this issue Jun 30, 2023 · 3 comments · Fixed by #3854
Closed

refactor tests to use context manager #3796

Remi-Gau opened this issue Jun 30, 2023 · 3 comments · Fixed by #3854
Assignees
Labels
Effort: low The issue is likely to require a small amount of work (less than a few hours). Good first issue Good for newcomers. Equivalent to "very low" effort. Hackathon Testing The issue is related to testing.

Comments

@Remi-Gau
Copy link
Collaborator

Remi-Gau commented Jun 30, 2023

Tests that check for errors or warnings and should use context manager.

Originally posted by @ymzayek in #3795 (comment)

Example:

This:

# Check that if we have not fit the masker we get a intelligible
# error
pytest.raises(
ValueError,
masker.transform,
[[img]],
)

Should become:

    # Check that if we have not fit the masker we get a intelligible
    # error
    with pytest.raises(ValueError):
        masker.transform([[img]])

video intros to:

See pytest documentation for more information:

list of files to to modify:

$ find nilearn -name '*.py' -exec grep -H "pytest.raises" {} \; | grep -v with

nilearn/datasets/tests/test_atlas.py:    pytest.raises(
nilearn/datasets/tests/test_atlas.py:    pytest.raises(ValueError, atlas.fetch_atlas_talairach, "bad_level")
nilearn/datasets/tests/test_atlas.py:    pytest.raises(ValueError, atlas.fetch_atlas_schaefer_2018, n_rois=44)
nilearn/datasets/tests/test_atlas.py:    pytest.raises(ValueError, atlas.fetch_atlas_schaefer_2018, yeo_networks=10)
nilearn/datasets/tests/test_atlas.py:    pytest.raises(ValueError, atlas.fetch_atlas_schaefer_2018, resolution_mm=3)
nilearn/datasets/tests/test_neurovault.py:    pytest.raises(requests.RequestException, neurovault._get_batch, "http://")
nilearn/datasets/tests/test_neurovault.py:        pytest.raises(
nilearn/datasets/tests/test_neurovault.py:    pytest.raises(ValueError, neurovault._get_batch, no_results_url)
nilearn/datasets/tests/test_neurovault.py:        pytest.raises(
nilearn/datasets/tests/test_neurovault.py:        pytest.raises(
nilearn/datasets/tests/test_neurovault.py:    pytest.raises(ValueError, neurovault.fetch_neurovault_ids, mode="bad")
nilearn/tests/test_signal.py:    pytest.raises(TypeError, nisignal.clean, x, low_pass=False)
nilearn/tests/test_signal.py:    pytest.raises(TypeError, nisignal.clean, x, high_pass=False)
nilearn/tests/test_signal.py:    pytest.raises(ValueError, clean, sx, low_pass=0.4, high_pass=0.5, t_r=2.5)
nilearn/tests/test_signal.py:    pytest.raises(TypeError, nisignal.clean, signals, confounds=1)
nilearn/tests/test_signal.py:    pytest.raises(ValueError, nisignal.clean, signals, confounds=np.zeros(2))
nilearn/tests/test_signal.py:    pytest.raises(ValueError, nisignal.clean, signals,
nilearn/tests/test_signal.py:    pytest.raises(ValueError, nisignal.clean, signals,
nilearn/tests/test_signal.py:    pytest.raises(ValueError, nisignal.clean, signals[:-1, :],
nilearn/tests/test_signal.py:    pytest.raises(TypeError, nisignal.clean, signals,
nilearn/tests/test_signal.py:    error_msg = pytest.raises(ValueError, nisignal.clean, signals,
nilearn/tests/test_signal.py:    pytest.raises(ValueError, nisignal.clean, signals, t_r=None,
nilearn/tests/test_signal.py:    pytest.raises(ValueError, nisignal.clean, signals,
nilearn/tests/test_signal.py:    pytest.raises(ValueError, nisignal.clean, signals,
nilearn/tests/test_signal.py:    pytest.raises(AssertionError, np.testing.assert_almost_equal,
nilearn/tests/test_masking.py:    pytest.raises(ValueError, compute_epi_mask, mean_image)
nilearn/tests/test_masking.py:    pytest.raises(ValueError, compute_background_mask, mean_image)
nilearn/tests/test_masking.py:    pytest.raises(ValueError, masking.apply_mask,
nilearn/tests/test_masking.py:    pytest.raises(ValueError, masking.apply_mask,
nilearn/tests/test_masking.py:    pytest.raises(ValueError, masking.apply_mask,
nilearn/tests/test_masking.py:    pytest.raises(ValueError, masking.apply_mask,
nilearn/tests/test_masking.py:    pytest.raises(TypeError, unmask, vec_1D, mask_img)
nilearn/tests/test_masking.py:    pytest.raises(TypeError, unmask, [vec_1D], mask_img)
nilearn/tests/test_masking.py:    pytest.raises(TypeError, unmask, vec_2D, mask_img)
nilearn/tests/test_masking.py:    pytest.raises(TypeError, unmask, [vec_2D], mask_img)
nilearn/tests/test_masking.py:    pytest.raises(TypeError, compute_multi_epi_mask, [])
nilearn/tests/test_masking.py:        pytest.raises(ValueError, compute_multi_epi_mask,
nilearn/tests/test_masking.py:    pytest.raises(TypeError, compute_multi_brain_mask, [])
nilearn/tests/test_masking.py:    pytest.raises(ValueError, compute_multi_brain_mask, imgs)
nilearn/tests/test_masking.py:    pytest.raises(TypeError, unmask, X, mask_img)
nilearn/tests/test_masking.py:    pytest.raises(TypeError, unmask, X, mask_img)
nilearn/surface/tests/test_surface.py:    pytest.raises(ValueError, surface._sample_locations,
nilearn/surface/tests/test_surface.py:    pytest.raises(ValueError, surface._projection_matrix,
nilearn/maskers/tests/test_multi_nifti_maps_masker.py:            pytest.raises(ValueError, masker11.transform, fmri12_img)
nilearn/maskers/tests/test_multi_nifti_maps_masker.py:            pytest.raises(ValueError, masker11.transform, fmri21_img)
nilearn/maskers/tests/test_multi_nifti_maps_masker.py:            pytest.raises(ValueError, masker11.fit)
nilearn/maskers/tests/test_multi_nifti_maps_masker.py:    pytest.raises(ValueError, masker11.fit)
nilearn/maskers/tests/test_multi_nifti_maps_masker.py:    pytest.raises(
nilearn/maskers/tests/test_multi_nifti_maps_masker.py:    pytest.raises(
nilearn/maskers/tests/test_multi_nifti_masker.py:    pytest.raises(
nilearn/maskers/tests/test_multi_nifti_masker.py:    pytest.raises(ValueError, masker.fit, img)
nilearn/maskers/tests/test_nifti_masker.py:        pytest.raises(TypeError, masker.fit)
nilearn/maskers/tests/test_nifti_masker.py:    pytest.raises(ValueError, masker.fit_transform, data_img)
nilearn/maskers/tests/test_multi_nifti_labels_masker.py:    pytest.raises(ValueError, masker11.transform, fmri12_img)
nilearn/maskers/tests/test_multi_nifti_labels_masker.py:    pytest.raises(ValueError, masker11.transform, fmri21_img)
nilearn/maskers/tests/test_multi_nifti_labels_masker.py:    pytest.raises(ValueError, masker11.fit)
nilearn/maskers/tests/test_multi_nifti_labels_masker.py:    pytest.raises(ValueError, masker11.fit)
nilearn/maskers/tests/test_multi_nifti_labels_masker.py:    pytest.raises(
nilearn/maskers/tests/test_multi_nifti_labels_masker.py:    pytest.raises(
nilearn/maskers/tests/test_nifti_maps_masker.py:            pytest.raises(ValueError, masker11.transform, fmri12_img)
nilearn/maskers/tests/test_nifti_maps_masker.py:            pytest.raises(ValueError, masker11.transform, fmri21_img)
nilearn/maskers/tests/test_nifti_maps_masker.py:            pytest.raises(ValueError, masker11.fit)
nilearn/maskers/tests/test_nifti_maps_masker.py:    pytest.raises(ValueError, masker11.fit)
nilearn/maskers/tests/test_nifti_maps_masker.py:    pytest.raises(ValueError, NiftiMapsMasker, maps33_img,
nilearn/maskers/tests/test_nifti_maps_masker.py:    pytest.raises(ValueError, NiftiMapsMasker, maps33_img,
nilearn/maskers/tests/test_nifti_labels_masker.py:    pytest.raises(ValueError, masker11.transform, fmri12_img)
nilearn/maskers/tests/test_nifti_labels_masker.py:    pytest.raises(ValueError, masker11.transform, fmri21_img)
nilearn/maskers/tests/test_nifti_labels_masker.py:    pytest.raises(ValueError, masker11.fit)
nilearn/maskers/tests/test_nifti_labels_masker.py:    pytest.raises(ValueError, masker11.fit)
nilearn/maskers/tests/test_nifti_labels_masker.py:    pytest.raises(ValueError, NiftiLabelsMasker, labels33_img,
nilearn/maskers/tests/test_nifti_labels_masker.py:    pytest.raises(ValueError, NiftiLabelsMasker, labels33_img,
nilearn/_utils/tests/test_param_validation.py:                pytest.raises(
nilearn/_utils/tests/test_numpy_conversions.py:    pytest.raises(ValueError, as_ndarray, "test string")
nilearn/_utils/tests/test_numpy_conversions.py:    pytest.raises(ValueError, as_ndarray, [], order="invalid")
nilearn/_utils/tests/test_numpy_conversions.py:        pytest.raises(TypeError, csv_to_array, filename, delimiters="?!")
nilearn/_utils/tests/test_ndimage.py:    pytest.raises(ValueError, largest_connected_component, a)
nilearn/_utils/tests/test_ndimage.py:    pytest.raises(ValueError, largest_connected_component, img)
nilearn/_utils/tests/test_ndimage.py:    pytest.raises(ValueError, largest_connected_component, "Test String")
nilearn/plotting/tests/test_img_plotting/test_plot_stat_map.py:    pytest.raises(ValueError, plot_stat_map, testdata_3d_for_plotting['img'],
nilearn/plotting/tests/test_img_plotting/test_plot_anat.py:    pytest.raises(Exception, plot_anat, display_mode="zzz")
nilearn/regions/tests/test_rena_clustering.py:    pytest.raises(ValueError, rena.fit, X)
nilearn/regions/tests/test_rena_clustering.py:        pytest.raises(ValueError, rena.fit, X)
$ find nilearn -name '*.py' -exec grep -H "pytest.warns" {} \; | grep -v with

nilearn/tests/test_signal.py:    pytest.warns(UserWarning, nisignal.clean, signals,
nilearn/plotting/tests/test_html_document.py:    pytest.warns(UserWarning, _open_views)
nilearn/plotting/tests/test_html_document.py:    pytest.warns(UserWarning, _open_views)
@Remi-Gau Remi-Gau added Good first issue Good for newcomers. Equivalent to "very low" effort. Testing The issue is related to testing. Effort: low The issue is likely to require a small amount of work (less than a few hours). Hackathon labels Jul 4, 2023
@Remi-Gau Remi-Gau changed the title refactor tests in nilear/maskers to use context manager refactor tests to use context manager Jul 5, 2023
@FrancoisPgm
Copy link
Contributor

Hi, I'm happy to take care of this one :)

@Remi-Gau
Copy link
Collaborator Author

Hey.
Are you on site or remote?

@htwangtw
Copy link
Member

François is here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Effort: low The issue is likely to require a small amount of work (less than a few hours). Good first issue Good for newcomers. Equivalent to "very low" effort. Hackathon Testing The issue is related to testing.
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

3 participants