From f69f3960ab4831fe3c04efb704f722271a16f61a Mon Sep 17 00:00:00 2001 From: Taylor Salo Date: Thu, 1 Jul 2021 10:49:53 -0400 Subject: [PATCH] [TST, REF] Address pandas `SettingWithCopyWarning`s (#534) * Address warning in ImagesToCoordinates. * Address in validate_images_df. * Raise exception on warning in tests. --- nimare/tests/conftest.py | 3 +++ nimare/transforms.py | 2 +- nimare/utils.py | 4 +++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/nimare/tests/conftest.py b/nimare/tests/conftest.py index fa588188d..a55a59dcf 100644 --- a/nimare/tests/conftest.py +++ b/nimare/tests/conftest.py @@ -4,6 +4,7 @@ import nibabel as nib import numpy as np +import pandas as pd import pytest from nilearn.image import resample_img @@ -12,6 +13,8 @@ from ..utils import get_resource_path +pd.options.mode.chained_assignment = "raise" + @pytest.fixture(scope="session") def testdata_ibma(tmp_path_factory): diff --git a/nimare/transforms.py b/nimare/transforms.py index 12f57ce0d..4e4abe436 100644 --- a/nimare/transforms.py +++ b/nimare/transforms.py @@ -456,7 +456,7 @@ def transform(self, dataset): # specify original coordinates original_ids = set(old_coordinates_df["id"]) - metadata[metadata["id"].isin(original_ids)]["coordinate_source"] = "original" + metadata.loc[metadata["id"].isin(original_ids), "coordinate_source"] = "original" # ensure z_stat is treated as float if "z_stat" in coordinates_df.columns: diff --git a/nimare/utils.py b/nimare/utils.py index 6d4f4a6f1..c22006609 100755 --- a/nimare/utils.py +++ b/nimare/utils.py @@ -227,10 +227,12 @@ def validate_images_df(image_df): if not shared_path.endswith(op.sep): shared_path = op.dirname(shared_path) + op.sep LGR.info("Shared path detected: '{0}'".format(shared_path)) + image_df_out = image_df.copy() # To avoid SettingWithCopyWarning for abs_col in abs_cols: - image_df[abs_col + "__relative"] = image_df[abs_col].apply( + image_df_out[abs_col + "__relative"] = image_df[abs_col].apply( lambda x: x.split(shared_path)[1] if isinstance(x, str) else x ) + image_df = image_df_out return image_df