Skip to content

Commit

Permalink
[FIX] Specify path to SPM when using Matlab and SPM12 (aramis-lab#1261)
Browse files Browse the repository at this point in the history
* specify path to SPM if using Matlab and SPM12

* update unit tests
  • Loading branch information
NicolasGensollen authored and AliceJoubert committed Aug 22, 2024
1 parent 9b21b8a commit c92eb53
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
6 changes: 5 additions & 1 deletion clinica/utils/spm.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def use_spm_standalone_if_available() -> bool:
"""
from clinica.utils.stream import cprint

from .check_dependency import get_mcr_home, get_spm_standalone_home
from .check_dependency import get_mcr_home, get_spm_home, get_spm_standalone_home
from .exceptions import ClinicaMissingDependencyError

try:
Expand All @@ -87,6 +87,10 @@ def use_spm_standalone_if_available() -> bool:
"to set the following environment variables: "
"$SPMSTANDALONE_HOME, and $MCR_HOME"
)
import nipype.interfaces.matlab as mlab

cprint(f"Setting SPM path to {get_spm_home()}", lvl="info")
mlab.MatlabCommand.set_default_paths(f"{get_spm_home()}")
return False


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import math
import os
from functools import partial
from pathlib import Path
from typing import List
from unittest import mock

import numpy as np
import pytest
Expand Down Expand Up @@ -304,12 +306,16 @@ def test_run_m_script_no_output_file_error(tmp_path, mocker):
"clinica.pipelines.statistics_volume.statistics_volume_utils._run_matlab_script_with_matlab",
return_value=None,
)
(tmp_path / "spm_home_folder").mkdir()

with pytest.raises(
RuntimeError,
match="Output matrix",
):
run_m_script(m_file)
with mock.patch.dict(
os.environ, {"SPM_HOME": str(tmp_path / "spm_home_folder")}
):
run_m_script(m_file)


def test_run_m_script(tmp_path, mocker):
Expand All @@ -327,8 +333,10 @@ def test_run_m_script(tmp_path, mocker):
"clinica.pipelines.statistics_volume.statistics_volume_utils._run_matlab_script_with_matlab",
return_value=None,
)
(tmp_path / "spm_home_folder").mkdir()

assert run_m_script(m_file) == str(expected_output_file)
with mock.patch.dict(os.environ, {"SPM_HOME": str(tmp_path / "spm_home_folder")}):
assert run_m_script(m_file) == str(expected_output_file)


@pytest.mark.parametrize(
Expand Down
20 changes: 18 additions & 2 deletions test/unittests/utils/test_spm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@

import pytest

from clinica.utils.exceptions import ClinicaMissingDependencyError

def test_spm_standalone_is_available_no_env_variable():

def test_spm_standalone_is_available_no_env_variable_error():
from clinica.utils.spm import use_spm_standalone_if_available

with pytest.raises(
ClinicaMissingDependencyError,
match="Clinica could not find spm software: the SPM_HOME variable is not set.",
):
use_spm_standalone_if_available()


def test_spm_standalone_is_available_warning(tmp_path):
from clinica.utils.spm import use_spm_standalone_if_available

(tmp_path / "spm_home_folder").mkdir()
with pytest.warns(
UserWarning,
match=re.escape(
Expand All @@ -17,7 +30,10 @@ def test_spm_standalone_is_available_no_env_variable():
"$SPMSTANDALONE_HOME, and $MCR_HOME"
),
):
assert not use_spm_standalone_if_available()
with mock.patch.dict(
os.environ, {"SPM_HOME": str(tmp_path / "spm_home_folder")}
):
assert not use_spm_standalone_if_available()


def test_spm_standalone_is_available(tmp_path, mocker):
Expand Down

0 comments on commit c92eb53

Please sign in to comment.