Skip to content

Commit 2e5dcb5

Browse files
authored
fix: Numpy 2 compatibility (#905)
* fix(doctest): Set legacy printoptions * fix: Numpy stopped accepting b-strings as savetxt fmt args * fix(np2): Replace np.NaN with np.nan
1 parent 5d9e666 commit 2e5dcb5

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

niworkflows/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ def find_resource_or_skip(resource):
4848
return pathlike
4949

5050

51+
@pytest.fixture(scope="session", autouse=True)
52+
def legacy_printoptions():
53+
from packaging.version import Version
54+
55+
if Version(np.__version__) >= Version("1.22"):
56+
np.set_printoptions(legacy="1.21")
57+
58+
5159
@pytest.fixture(autouse=True)
5260
def add_np(doctest_namespace):
5361
from .utils.bids import collect_data

niworkflows/interfaces/images.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,6 @@ def _run_interface(self, runtime):
749749

750750
output = np.vstack((self.inputs.class_labels, series.astype(str)))
751751
self._results["out_file"] = os.path.join(runtime.cwd, self.inputs.out_file)
752-
np.savetxt(self._results["out_file"], output, fmt=b"%s", delimiter="\t")
752+
np.savetxt(self._results["out_file"], output, fmt="%s", delimiter="\t")
753753

754754
return runtime

niworkflows/tests/test_confounds.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ def test_expansion_derivatives_and_powers():
9898
{
9999
"a": [-1, -2, -3, -4, -5],
100100
"a_power2": [1, 4, 9, 16, 25],
101-
"a_derivative1": [np.NaN, -1, -1, -1, -1],
102-
"a_derivative1_power2": [np.NaN, 1, 1, 1, 1],
103-
"b_derivative1": [np.NaN, 0, 0, 0, 0],
104-
"b_derivative1_power2": [np.NaN, 0, 0, 0, 0],
105-
"c_power2_derivative1": [np.NaN, 1, -1, 1, -1],
106-
"c_power2_derivative2": [np.NaN, np.NaN, -2, 2, -2],
101+
"a_derivative1": [np.nan, -1, -1, -1, -1],
102+
"a_derivative1_power2": [np.nan, 1, 1, 1, 1],
103+
"b_derivative1": [np.nan, 0, 0, 0, 0],
104+
"b_derivative1_power2": [np.nan, 0, 0, 0, 0],
105+
"c_power2_derivative1": [np.nan, 1, -1, 1, -1],
106+
"c_power2_derivative2": [np.nan, np.nan, -2, 2, -2],
107107
"d": [9, 7, 5, 3, 1],
108108
"e": [0, 0, 0, 0, 0],
109-
"f": [np.NaN, 6, 4, 2, 0],
109+
"f": [np.nan, 6, 4, 2, 0],
110110
}
111111
)
112112
exp_data = _expand_test(model_formula)
@@ -122,10 +122,10 @@ def test_expansion_na_robustness():
122122
model_formula = "(dd1(f))^^2"
123123
expected_data = pd.DataFrame(
124124
{
125-
"f": [np.NaN, 6, 4, 2, 0],
126-
"f_power2": [np.NaN, 36, 16, 4, 0],
127-
"f_derivative1": [np.NaN, np.NaN, -2, -2, -2],
128-
"f_derivative1_power2": [np.NaN, np.NaN, 4, 4, 4],
125+
"f": [np.nan, 6, 4, 2, 0],
126+
"f_power2": [np.nan, 36, 16, 4, 0],
127+
"f_derivative1": [np.nan, np.nan, -2, -2, -2],
128+
"f_derivative1_power2": [np.nan, np.nan, 4, 4, 4],
129129
}
130130
)
131131
exp_data = _expand_test(model_formula)

0 commit comments

Comments
 (0)