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

Add warning when coordinates dataset contains both positive and negative z_stats #699

Merged
merged 4 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions nimare/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import logging
import os.path as op
import warnings

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -103,6 +104,22 @@ def __init__(self, source, target="mni152_2mm", mask=None):
self.texts = _dict_to_df(id_df, data, key="text")
self.basepath = None

if "z_stat" in self.coordinates.columns:
# "z_stat" column may contain Nones
if not self.coordinates["z_stat"].isna().any():
# Ensure z_stat is treated as float
self.coordinates["z_stat"] = self.coordinates["z_stat"].astype(float)

# Raise warning if coordinates dataset contains both positive and negative z_stats
if ((self.coordinates["z_stat"].values >= 0).any()) and (
(self.coordinates["z_stat"].values < 0).any()
):
warnings.warn(
"Coordinates dataset contains both positive and negative z_stats. "
"The algorithms currently implemented in NiMARE are designed for "
"one-sided tests. This might lead to unexpected results."
)

def __repr__(self):
"""Show basic Dataset representation.

Expand Down
45 changes: 45 additions & 0 deletions nimare/tests/test_dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Test nimare.dataset (Dataset IO/transformations)."""
import copy
import json
import os.path as op
import warnings

import nibabel as nib
import numpy as np
Expand Down Expand Up @@ -51,3 +54,45 @@ def test_empty_dset():
# dictionary with no information
minimal_dict = {"study-0": {"contrasts": {"1": {}}}}
dataset.Dataset(minimal_dict)


def test_posneg_warning():
"""Smoke test for nimare.dataset.Dataset initialization with positive and negative z_stat."""
db_file = op.join(get_test_data_path(), "neurosynth_dset.json")
with open(db_file, "r") as f_obj:
data = json.load(f_obj)

data_pos_zstats = copy.deepcopy(data)
data_neg_zstats = copy.deepcopy(data)
for pid in data.keys():
for expid in data[pid]["contrasts"].keys():
exp = data[pid]["contrasts"][expid]

if "coords" not in exp.keys():
continue

if "z_stat" not in exp["coords"].keys():
continue

n_zstats = len(exp["coords"]["z_stat"])
rand_arr = np.random.randn(n_zstats)
rand_pos_arr = np.abs(rand_arr)
rand_neg_arr = np.abs(rand_arr) * -1

data[pid]["contrasts"][expid]["coords"]["z_stat"] = rand_arr.tolist()
data_neg_zstats[pid]["contrasts"][expid]["coords"]["z_stat"] = rand_neg_arr.tolist()
data_pos_zstats[pid]["contrasts"][expid]["coords"]["z_stat"] = rand_pos_arr.tolist()

# Test Warning is raised if there are positive and negative z-stat
with pytest.warns(UserWarning, match=r"positive and negative z_stats"):
dset_posneg = dataset.Dataset(data)

# Test Warning is not raised if there are only positive or negative z-stat
with warnings.catch_warnings():
warnings.simplefilter("error")
dset_pos = dataset.Dataset(data_pos_zstats)
dset_neg = dataset.Dataset(data_neg_zstats)

assert isinstance(dset_posneg, nimare.dataset.Dataset)
assert isinstance(dset_pos, nimare.dataset.Dataset)
assert isinstance(dset_neg, nimare.dataset.Dataset)
13 changes: 12 additions & 1 deletion nimare/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import logging
import os.path as op
import warnings

import nibabel as nib
import numpy as np
Expand Down Expand Up @@ -456,10 +457,20 @@ def transform(self, dataset):
original_ids = set(old_coordinates_df["id"])
metadata.loc[metadata["id"].isin(original_ids), "coordinate_source"] = "original"

# ensure z_stat is treated as float
if "z_stat" in coordinates_df.columns:
# ensure z_stat is treated as float
coordinates_df["z_stat"] = coordinates_df["z_stat"].astype(float)

# Raise warning if coordinates dataset contains both positive and negative z_stats
if ((coordinates_df["z_stat"].values >= 0).any()) and (
(coordinates_df["z_stat"].values < 0).any()
):
warnings.warn(
"Coordinates dataset contains both positive and negative z_stats. "
"The algorithms currently implemented in NiMARE are designed for "
"one-sided tests. This might lead to unexpected results."
)

new_dataset = copy.deepcopy(dataset)
new_dataset.coordinates = coordinates_df
new_dataset.metadata = metadata
Expand Down