Exemplo n.º 1
0
def test_ALE_ma_map_reuse(testdata_cbma, tmp_path_factory, caplog):
    """Test that MA maps are re-used when appropriate."""
    from nimare.meta import kernel

    tmpdir = tmp_path_factory.mktemp("test_ALE_ma_map_reuse")
    testdata_cbma.update_path(tmpdir)

    # ALEKernel cannot extract sample_size from a Dataset,
    # so we need to set it for this kernel and for the later meta-analyses.
    kern = kernel.ALEKernel(sample_size=20)
    dset = kern.transform(testdata_cbma, return_type="dataset")

    # The associated column should be in the new Dataset's images DataFrame
    cols = dset.images.columns.tolist()
    assert any(["ALEKernel" in col for col in cols])

    # The Dataset without the images will generate them from scratch.
    # If drop_invalid is False, then there should be an Exception, since two studies in the test
    # dataset are missing coordinates.
    meta = ale.ALE(kernel__sample_size=20)
    with pytest.raises(Exception):
        meta.fit(testdata_cbma, drop_invalid=False)

    with caplog.at_level(logging.DEBUG, logger="nimare.meta.cbma.base"):
        meta.fit(testdata_cbma)
    assert "Loading pre-generated MA maps" not in caplog.text

    # The Dataset with the images will re-use them, as evidenced by the logger message.
    with caplog.at_level(logging.DEBUG, logger="nimare.meta.cbma.base"):
        meta.fit(dset)
    assert "Loading pre-generated MA maps" in caplog.text
Exemplo n.º 2
0
def test_ALEKernel_smoke(testdata_cbma):
    """Smoke test for nimare.meta.kernel.ALEKernel."""
    # Manually override dataset coordinates file sample sizes
    # This column would be extracted from metadata and added to coordinates
    # automatically by the Estimator
    coordinates = testdata_cbma.coordinates.copy()
    coordinates["sample_size"] = 20

    kern = kernel.ALEKernel()
    ma_maps = kern.transform(coordinates, testdata_cbma.masker, return_type="image")
    assert len(ma_maps) == len(testdata_cbma.ids) - 2
    ma_maps = kern.transform(coordinates, testdata_cbma.masker, return_type="array")
    assert ma_maps.shape[0] == len(testdata_cbma.ids) - 2
    # Test set_params
    kern.set_params(fwhm=10, sample_size=None)
    kern2 = kernel.ALEKernel(fwhm=10)
    ma_maps1 = kern.transform(coordinates, testdata_cbma.masker, return_type="array")
    ma_maps2 = kern2.transform(coordinates, testdata_cbma.masker, return_type="array")
    assert ma_maps1.shape[0] == ma_maps2.shape[0] == len(testdata_cbma.ids) - 2
    assert np.array_equal(ma_maps1, ma_maps2)
Exemplo n.º 3
0
def test_ALEKernel_fwhm(testdata_cbma):
    """Peaks of ALE kernel maps should match the foci fed in (assuming focus isn't masked out).

    Test with explicit FWHM.
    """
    coordinates = testdata_cbma.coordinates.copy()

    id_ = "pain_03.nidm-1"
    kern = kernel.ALEKernel(fwhm=10)
    ma_maps = kern.transform(coordinates, masker=testdata_cbma.masker, return_type="image")

    ijk = coordinates.loc[coordinates["id"] == id_, ["i", "j", "k"]]
    ijk = np.squeeze(ijk.values.astype(int))
    kern_data = ma_maps[0].get_fdata()
    max_idx = np.array(np.where(kern_data == np.max(kern_data))).T
    max_ijk = np.squeeze(max_idx)
    assert np.array_equal(ijk, max_ijk)
Exemplo n.º 4
0
def test_ALEKernel_sample_size(testdata_cbma):
    """Peaks of ALE kernel maps should match the foci fed in (assuming focus isn't masked out).

    Test with explicit sample size.
    """
    coordinates = testdata_cbma.coordinates.copy()

    id_ = "pain_03.nidm-1"
    kern = kernel.ALEKernel(sample_size=20)
    ma_maps = kern.transform(coordinates, masker=testdata_cbma.masker, return_type="image")

    xyz = coordinates.loc[coordinates["id"] == id_, ["x", "y", "z"]]
    ijk = mm2vox(xyz, testdata_cbma.masker.mask_img.affine)
    ijk = np.squeeze(ijk.astype(int))

    kern_data = ma_maps[0].get_fdata()
    max_idx = np.array(np.where(kern_data == np.max(kern_data))).T
    max_ijk = np.squeeze(max_idx)
    assert np.array_equal(ijk, max_ijk)
Exemplo n.º 5
0
def test_ALEKernel_1mm(testdata_cbma):
    """Peaks of ALE kernel maps should match the foci fed in (assuming focus isn't masked out).

    Test on 1mm template.
    """
    # Manually override dataset coordinates file sample sizes
    # This column would be extracted from metadata and added to coordinates
    # automatically by the Estimator
    coordinates = testdata_cbma.coordinates.copy()
    coordinates["sample_size"] = 20

    id_ = "pain_03.nidm-1"
    kern = kernel.ALEKernel()
    ma_maps = kern.transform(coordinates, testdata_cbma.masker, return_type="image")
    ijk = coordinates.loc[coordinates["id"] == id_, ["i", "j", "k"]]
    ijk = ijk.values.astype(int)
    kern_data = ma_maps[0].get_fdata()
    max_idx = np.where(kern_data == np.max(kern_data))
    max_ijk = np.array(max_idx).T
    assert np.array_equal(ijk, max_ijk)
Exemplo n.º 6
0
def test_ALEKernel_inputdataset_returndataset(testdata_cbma, tmp_path_factory):
    """Check that all return types produce equivalent results (minus the masking element)."""
    tmpdir = tmp_path_factory.mktemp("test_ALEKernel_inputdataset_returndataset")
    testdata_cbma.update_path(tmpdir)
    kern = kernel.ALEKernel(sample_size=20, memory_limit="1gb")
    ma_maps = kern.transform(testdata_cbma, return_type="image")
    ma_arr = kern.transform(testdata_cbma, return_type="array")
    dset = kern.transform(testdata_cbma, return_type="dataset")
    ma_maps_from_dset = kern.transform(dset, return_type="image")
    ma_arr_from_dset = kern.transform(dset, return_type="array")
    ma_maps_arr = testdata_cbma.masker.transform(ma_maps)
    ma_maps_from_dset_arr = dset.masker.transform(ma_maps_from_dset)
    dset_from_dset = kern.transform(dset, return_type="dataset")
    ids = dset.coordinates["id"].unique()
    ma_maps_dset = testdata_cbma.masker.transform(dset.get_images(ids=ids, imtype=kern.image_type))
    assert isinstance(dset_from_dset, Dataset)
    assert np.array_equal(ma_arr, ma_maps_arr)
    assert np.array_equal(ma_arr, ma_maps_dset)
    assert np.array_equal(ma_arr, ma_maps_from_dset_arr)
    assert np.array_equal(ma_arr, ma_arr_from_dset)
Exemplo n.º 7
0
def test_ALESubtraction_ma_map_reuse(testdata_cbma, tmp_path_factory, caplog):
    """Test that MA maps are re-used when appropriate."""
    from nimare.meta import kernel

    tmpdir = tmp_path_factory.mktemp("test_ALESubtraction_ma_map_reuse")
    testdata_cbma.update_path(tmpdir)

    # ALEKernel cannot extract sample_size from a Dataset,
    # so we need to set it for this kernel and for the later meta-analyses.
    kern = kernel.ALEKernel(sample_size=20)
    dset = kern.transform(testdata_cbma, return_type="dataset")

    # The Dataset without the images will generate them from scratch.
    sub_meta = ale.ALESubtraction(n_iters=10, kernel__sample_size=20)

    with caplog.at_level(logging.DEBUG, logger="nimare.meta.cbma.base"):
        sub_meta.fit(testdata_cbma, testdata_cbma)
    assert "Loading pre-generated MA maps" not in caplog.text

    # The Dataset with the images will re-use them,
    # as evidenced by the logger message.
    with caplog.at_level(logging.DEBUG, logger="nimare.meta.cbma.base"):
        sub_meta.fit(dset, dset)
    assert "Loading pre-generated MA maps" in caplog.text
Exemplo n.º 8
0
# There are three standard kernels that are currently available: {py:class}`~nimare.meta.kernel.MKDAKernel`, {py:class}`~nimare.meta.kernel.KDAKernel`, and {py:class}`~nimare.meta.kernel.ALEKernel`.
# Each class may be configured with certain parameters when a new object is initialized.
# For example, `MKDAKernel` accepts an `r` parameter, which determines the radius of the spheres that will be created around each peak coordinate.
# `ALEKernel` automatically uses the sample size associated with each experiment in the `Dataset` to determine the appropriate full-width-at-half-maximum of its Gaussian distribution, as described in {cite:t}`EICKHOFF20122349`; however, users may provide a constant `sample_size` or `fwhm` parameter when sample size information is not available within the `Dataset` metadata.
#
# Here we show how these three kernels can be applied to the same `Dataset`.

# In[2]:

from nimare.meta import kernel

mkda_kernel = kernel.MKDAKernel(r=10)
mkda_ma_maps = mkda_kernel.transform(sleuth_dset1)
kda_kernel = kernel.KDAKernel(r=10)
kda_ma_maps = kda_kernel.transform(sleuth_dset1)
ale_kernel = kernel.ALEKernel(sample_size=20)
ale_ma_maps = ale_kernel.transform(sleuth_dset1)

# In[3]:

# Here we delete the recent variables for the sake of reducing memory usage
del mkda_kernel, kda_kernel, ale_kernel

# In[4]:

# Generate figure
study_idx = 10  # a study with overlapping kernels
max_value = np.max(kda_ma_maps[study_idx].get_fdata()) + 1

ma_maps = {
    "MKDA Kernel": mkda_ma_maps[study_idx],