Skip to content

Commit

Permalink
[MAINT] replace tmpdir with tmp_path in tests (#3929)
Browse files Browse the repository at this point in the history
* replace tmpdir with tmp_path

* rm extra import
  • Loading branch information
Remi-Gau committed Aug 29, 2023
1 parent 23da827 commit 4e2a954
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 55 deletions.
44 changes: 22 additions & 22 deletions nilearn/_utils/tests/test_data_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ def _bids_path_template(
[(["main"], [1]), (["main"], [2]), (["main", "localizer"], [2, 1])],
)
def test_fake_bids_raw_with_session_and_runs(
tmpdir, n_sub, n_ses, tasks, n_runs
tmp_path, n_sub, n_ses, tasks, n_runs
):
"""Check number of each file 'type' created in raw."""
bids_path = create_fake_bids_dataset(
base_dir=tmpdir, n_sub=n_sub, n_ses=n_ses, tasks=tasks, n_runs=n_runs
base_dir=tmp_path, n_sub=n_sub, n_ses=n_ses, tasks=tasks, n_runs=n_runs
)

# raw
Expand Down Expand Up @@ -193,11 +193,11 @@ def _check_nb_files_derivatives_for_task(
[(["main"], [1]), (["main"], [2]), (["main", "localizer"], [2, 1])],
)
def test_fake_bids_derivatives_with_session_and_runs(
tmpdir, n_sub, n_ses, tasks, n_runs
tmp_path, n_sub, n_ses, tasks, n_runs
):
"""Check number of each file 'type' created in derivatives."""
bids_path = create_fake_bids_dataset(
base_dir=tmpdir, n_sub=n_sub, n_ses=n_ses, tasks=tasks, n_runs=n_runs
base_dir=tmp_path, n_sub=n_sub, n_ses=n_ses, tasks=tasks, n_runs=n_runs
)

# derivatives
Expand All @@ -216,10 +216,10 @@ def test_fake_bids_derivatives_with_session_and_runs(
assert len(all_files) == n_derivatives_files_expected


def test_bids_dataset_no_run_entity(tmpdir):
def test_bids_dataset_no_run_entity(tmp_path):
"""n_runs = 0 produces files without the run entity."""
bids_path = create_fake_bids_dataset(
base_dir=tmpdir,
base_dir=tmp_path,
n_sub=1,
n_ses=1,
tasks=["main"],
Expand All @@ -240,10 +240,10 @@ def test_bids_dataset_no_run_entity(tmpdir):
assert len(files) == 1


def test_bids_dataset_no_session(tmpdir):
def test_bids_dataset_no_session(tmp_path):
"""n_ses = 0 prevent creation of a session folder."""
bids_path = create_fake_bids_dataset(
base_dir=tmpdir,
base_dir=tmp_path,
n_sub=1,
n_ses=0,
tasks=["main"],
Expand All @@ -264,10 +264,10 @@ def test_bids_dataset_no_session(tmpdir):
assert len(files) == 1


def test_create_fake_bids_dataset_no_derivatives(tmpdir):
def test_create_fake_bids_dataset_no_derivatives(tmp_path):
"""Check no file is created in derivatives."""
bids_path = create_fake_bids_dataset(
base_dir=tmpdir,
base_dir=tmp_path,
n_sub=1,
n_ses=1,
tasks=["main"],
Expand All @@ -282,11 +282,11 @@ def test_create_fake_bids_dataset_no_derivatives(tmpdir):
"confounds_tag,with_confounds", [(None, True), ("_timeseries", False)]
)
def test_create_fake_bids_dataset_no_confounds(
tmpdir, confounds_tag, with_confounds
tmp_path, confounds_tag, with_confounds
):
"""Check that files are created in the derivatives but no confounds."""
bids_path = create_fake_bids_dataset(
base_dir=tmpdir,
base_dir=tmp_path,
n_sub=1,
n_ses=1,
tasks=["main"],
Expand All @@ -299,15 +299,15 @@ def test_create_fake_bids_dataset_no_confounds(
assert not files


def test_fake_bids_errors(tmpdir):
def test_fake_bids_errors(tmp_path):
with pytest.raises(ValueError, match="labels.*alphanumeric"):
create_fake_bids_dataset(
base_dir=tmpdir, n_sub=1, n_ses=1, tasks=["foo_bar"], n_runs=[1]
base_dir=tmp_path, n_sub=1, n_ses=1, tasks=["foo_bar"], n_runs=[1]
)

with pytest.raises(ValueError, match="labels.*alphanumeric"):
create_fake_bids_dataset(
base_dir=tmpdir,
base_dir=tmp_path,
n_sub=1,
n_ses=1,
tasks=["main"],
Expand All @@ -317,23 +317,23 @@ def test_fake_bids_errors(tmpdir):

with pytest.raises(ValueError, match="number.*tasks.*runs.*same"):
create_fake_bids_dataset(
base_dir=tmpdir,
base_dir=tmp_path,
n_sub=1,
n_ses=1,
tasks=["main"],
n_runs=[1, 2],
)


def test_fake_bids_extra_raw_entity(tmpdir):
def test_fake_bids_extra_raw_entity(tmp_path):
"""Check files with extra entity are created appropriately."""
n_sub = 2
n_ses = 2
tasks = ["main"]
n_runs = [2]
entities = {"acq": ["foo", "bar"]}
bids_path = create_fake_bids_dataset(
base_dir=tmpdir,
base_dir=tmp_path,
n_sub=n_sub,
n_ses=n_ses,
tasks=tasks,
Expand Down Expand Up @@ -383,15 +383,15 @@ def test_fake_bids_extra_raw_entity(tmpdir):
assert len(all_files) == n_derivatives_files_expected


def test_fake_bids_extra_derivative_entity(tmpdir):
def test_fake_bids_extra_derivative_entity(tmp_path):
"""Check files with extra entity are created appropriately."""
n_sub = 2
n_ses = 2
tasks = ["main"]
n_runs = [2]
entities = {"res": ["foo", "bar"]}
bids_path = create_fake_bids_dataset(
base_dir=tmpdir,
base_dir=tmp_path,
n_sub=n_sub,
n_ses=n_ses,
tasks=tasks,
Expand Down Expand Up @@ -426,11 +426,11 @@ def test_fake_bids_extra_derivative_entity(tmpdir):
assert len(all_files) == n_derivatives_files_expected


def test_fake_bids_extra_entity_not_bids_entity(tmpdir):
def test_fake_bids_extra_entity_not_bids_entity(tmp_path):
"""Check files with extra entity are created appropriately."""
with pytest.raises(ValueError, match="Invalid entity"):
create_fake_bids_dataset(
base_dir=tmpdir,
base_dir=tmp_path,
entities={"egg": ["spam"]},
)

Expand Down
4 changes: 2 additions & 2 deletions nilearn/interfaces/fmriprep/tests/test_load_confounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,15 +597,15 @@ def test_inputs(tmp_path, image_type):
assert len(conf) == 2


def test_load_confounds_for_gifti(tmpdir):
def test_load_confounds_for_gifti(tmp_path):
"""Ensure that confounds are found for gifti files.
Regression test for
https://github.com/nilearn/nilearn/issues/3817
Wrong order of space and hemi entity in filename pattern
lead to confounds not being found.
"""
bids_path = create_fake_bids_dataset(base_dir=tmpdir, n_sub=1, n_ses=1)
bids_path = create_fake_bids_dataset(base_dir=tmp_path, n_sub=1, n_ses=1)
selection = get_bids_files(
bids_path / "derivatives",
sub_label="01",
Expand Down
12 changes: 6 additions & 6 deletions nilearn/plotting/tests/test_img_plotting/test_img_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ def test_mni152template_is_reordered():


@pytest.mark.parametrize("plot_func", PLOTTING_FUNCS_3D)
def test_plot_functions_3d_default_params(plot_func, img_3d_mni, tmpdir):
def test_plot_functions_3d_default_params(plot_func, img_3d_mni, tmp_path):
"""Smoke tests for 3D plotting functions with default parameters."""
filename = str(tmpdir.join("temp.png"))
filename = tmp_path / "temp.png"
plot_func(img_3d_mni, output_file=filename)
plt.close()


@pytest.mark.parametrize("plot_func", PLOTTING_FUNCS_3D)
@pytest.mark.parametrize("cbar_tick_format", ["%f", "%i"])
def test_cbar_tick_format(plot_func, img_3d_mni, cbar_tick_format, tmpdir):
def test_cbar_tick_format(plot_func, img_3d_mni, cbar_tick_format, tmp_path):
"""Test different colorbar tick format with 3D plotting functions."""
filename = str(tmpdir.join("temp.png"))
filename = tmp_path / "temp.png"
plot_func(
img_3d_mni,
output_file=filename,
Expand All @@ -80,10 +80,10 @@ def test_cbar_tick_format(plot_func, img_3d_mni, cbar_tick_format, tmpdir):

@pytest.mark.parametrize("plot_func", PLOTTING_FUNCS_4D)
def test_plot_functions_4d_default_params(
plot_func, img_3d_mni, testdata_4d_for_plotting, tmpdir
plot_func, img_3d_mni, testdata_4d_for_plotting, tmp_path
):
"""Smoke-test for 4D plotting functions with default arguments."""
filename = str(tmpdir.join("temp.png"))
filename = tmp_path / "temp.png"
kwargs = {"output_file": filename}
if plot_func == plot_carpet:
kwargs["mask_img"] = testdata_4d_for_plotting["img_mask"]
Expand Down
14 changes: 8 additions & 6 deletions nilearn/plotting/tests/test_img_plotting/test_plot_anat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,35 @@

@pytest.mark.parametrize("anat_img", [False, MNI152TEMPLATE])
@pytest.mark.parametrize("display_mode", ["z", "ortho"])
def test_plot_anat_MNI(anat_img, display_mode, tmpdir):
def test_plot_anat_MNI(anat_img, display_mode, tmp_path):
"""Tests for plot_anat with MNI template."""
slicer = plot_anat(anat_img=anat_img, display_mode=display_mode)
filename = str(tmpdir.join("test.png"))
filename = tmp_path / "test.png"
slicer.savefig(filename)
plt.close()


@pytest.mark.parametrize("anat_img", [False, MNI152TEMPLATE])
@pytest.mark.parametrize("display_mode", ["z", "ortho"])
@pytest.mark.parametrize("cbar_tick_format", ["%.2g", "%i"])
def test_plot_anat_colorbar(anat_img, display_mode, cbar_tick_format, tmpdir):
def test_plot_anat_colorbar(
anat_img, display_mode, cbar_tick_format, tmp_path
):
"""Tests for plot_anat with MNI template and colorbar."""
slicer = plot_anat(
anat_img=anat_img,
display_mode=display_mode,
colorbar=True,
cbar_tick_format=cbar_tick_format,
)
filename = str(tmpdir.join("test.png"))
filename = tmp_path / "test.png"
slicer.savefig(filename)
plt.close()


def test_plot_anat_3d_img(img_3d_mni, tmpdir):
def test_plot_anat_3d_img(img_3d_mni, tmp_path):
"""Smoke test for plot_anat."""
filename = str(tmpdir.join("test.png"))
filename = tmp_path / "test.png"
slicer = plot_anat(img_3d_mni, dim="auto")
slicer.savefig(filename)
plt.close()
Expand Down
10 changes: 6 additions & 4 deletions nilearn/plotting/tests/test_img_plotting/test_plot_connectome.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def node_coords():
)
@pytest.mark.parametrize("display_mode", ["ortho", "lzry"])
def test_plot_connectome_node_colors(
node_color, display_mode, node_coords, adjacency, base_params, tmpdir
node_color, display_mode, node_coords, adjacency, base_params
):
"""Smoke test for plot_connectome with different values for node_color."""
plot_connectome(
Expand Down Expand Up @@ -124,7 +124,7 @@ def test_plot_connectome_with_nans(adjacency, node_coords, base_params):


def test_plot_connectome_tuple_node_coords(
adjacency, node_coords, base_params, tmpdir
adjacency, node_coords, base_params
):
"""Smoke test for plot_connectome where node_coords is not provided
as an array but as a list of tuples.
Expand Down Expand Up @@ -154,10 +154,12 @@ def test_plot_connectome_alpha(alpha, adjacency, node_coords):
plt.close()


def test_plot_connectome_to_file(adjacency, node_coords, base_params, tmpdir):
def test_plot_connectome_to_file(
adjacency, node_coords, base_params, tmp_path
):
"""Smoke test for plot_connectome and saving to file."""
base_params["display_mode"] = "x"
filename = str(tmpdir.join("temp.png"))
filename = tmp_path / "temp.png"
display = plot_connectome(
adjacency, node_coords, output_file=filename, **base_params
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def test_plot_glass_brain(img_3d_mni):
)


def test_plot_glass_brain_file_output(img_3d_mni, tmpdir):
def test_plot_glass_brain_file_output(img_3d_mni, tmp_path):
"""Smoke-test for hemispheric glass brain with file output."""
filename = str(tmpdir.join("test.png"))
filename = tmp_path / "test.png"
plot_glass_brain(
img_3d_mni,
output_file=filename,
Expand Down
4 changes: 2 additions & 2 deletions nilearn/plotting/tests/test_img_plotting/test_plot_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ def test_plot_markers_tuple_node_coords(coords):
plt.close()


def test_plot_markers_saving_to_file(coords, tmpdir):
def test_plot_markers_saving_to_file(coords, tmp_path):
"""Smoke test for plot_markers and file saving."""
filename = str(tmpdir.join("test.png"))
filename = tmp_path / "test.png"
display = plot_markers(
[1, 2, 3, 4], coords, output_file=filename, display_mode="x"
)
Expand Down
4 changes: 2 additions & 2 deletions nilearn/plotting/tests/test_img_plotting/test_plot_roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def test_plot_roi_view_type_error():
demo_plot_roi(view_type="flled")


def test_demo_plot_roi_output_file(tmpdir):
def test_demo_plot_roi_output_file(tmp_path):
"""Tests plot_roi file saving capabilities."""
filename = str(tmpdir.join("test.png"))
filename = tmp_path / "test.png"
with open(filename, "wb") as fp:
out = demo_plot_roi(output_file=fp)
assert out is None
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from nilearn.plotting.find_cuts import find_cut_slices


def test_plot_stat_map_bad_input(img_3d_mni, tmpdir):
def test_plot_stat_map_bad_input(img_3d_mni, tmp_path):
"""Test for bad input arguments (cf. #510)."""
filename = str(tmpdir.join("temp.png"))
filename = tmp_path / "temp.png"
ax = plt.subplot(111, rasterized=True)
plot_stat_map(
img_3d_mni,
Expand All @@ -29,9 +29,9 @@ def test_plot_stat_map_bad_input(img_3d_mni, tmpdir):
@pytest.mark.parametrize(
"params", [{}, {"display_mode": "x", "cut_coords": 3}]
)
def test_save_plot_stat_map(params, img_3d_mni, tmpdir):
def test_save_plot_stat_map(params, img_3d_mni, tmp_path):
"""Test saving figure to file in different ways."""
filename = str(tmpdir.join("test.png"))
filename = tmp_path / "test.png"
display = plot_stat_map(img_3d_mni, output_file=filename, **params)
assert display is None
display = plot_stat_map(img_3d_mni, **params)
Expand Down
9 changes: 4 additions & 5 deletions nilearn/plotting/tests/test_surf_plotting.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Tests for functions in surf_plotting.py
import os
import re
import tempfile
import unittest.mock as mock
Expand Down Expand Up @@ -310,12 +309,12 @@ def test_plotly_show(renderer):
@pytest.mark.skipif(not PLOTLY_INSTALLED or not KALEIDO_INSTALLED,
reason=("Plotly and/or kaleido not installed; "
"required for this test."))
def test_plotly_savefig(tmpdir):
ps = PlotlySurfaceFigure(go.Figure(), output_file=str(tmpdir / "foo.png"))
assert ps.output_file == str(tmpdir / "foo.png")
def test_plotly_savefig(tmp_path):
ps = PlotlySurfaceFigure(go.Figure(), output_file=tmp_path / "foo.png")
assert ps.output_file == tmp_path / "foo.png"
assert ps.figure is not None
ps.savefig()
assert os.path.exists(str(tmpdir / "foo.png"))
assert (tmp_path / "foo.png").exists()


@pytest.mark.skipif(not PLOTLY_INSTALLED,
Expand Down

0 comments on commit 4e2a954

Please sign in to comment.