def test_KDAKernel_smoke(testdata_cbma): """Smoke test for nimare.meta.kernel.KDAKernel.""" kern = kernel.KDAKernel() ma_maps = kern.transform(testdata_cbma.coordinates, testdata_cbma.masker, return_type="image") assert len(ma_maps) == len(testdata_cbma.ids) - 2 ma_maps = kern.transform(testdata_cbma.coordinates, testdata_cbma.masker, return_type="array") assert ma_maps.shape[0] == len(testdata_cbma.ids) - 2
def test_kdakernel_inputdataset_returndataset(testdata_cbma, tmp_path_factory): """ Check that the different return types produce equivalent results (minus the masking element). """ tmpdir = tmp_path_factory.mktemp("test_kdakernel_inputdataset_returndataset") testdata_cbma.update_path(tmpdir) kern = kernel.KDAKernel(r=4, value=1) # MA map generation from transformer 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") # Load generated MA maps ma_maps_from_dset = kern.transform(dset, return_type="image") ma_arr_from_dset = kern.transform(dset, return_type="array") dset_from_dset = kern.transform(dset, return_type="dataset") ma_maps_arr = testdata_cbma.masker.transform(ma_maps) ma_maps_from_dset_arr = dset.masker.transform(ma_maps_from_dset) ma_maps_dset = testdata_cbma.masker.transform( dset.get_images(ids=dset.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)
def test_KDAKernel_transform_attributes(testdata_cbma): """Check that attributes are added at transform.""" kern = kernel.KDAKernel(r=4, value=1) assert not hasattr(kern, "filename_pattern") assert not hasattr(kern, "image_type") _ = kern.transform(testdata_cbma, return_type="image") assert hasattr(kern, "filename_pattern") assert hasattr(kern, "image_type")
def test_KDAKernel_inputdataset_returnimages(testdata_cbma): """Centers of mass of KDA kernel maps should match the foci fed in. This assumes focus isn't masked out and spheres don't overlap. Test on Dataset object. """ id_ = "pain_03.nidm-1" kern = kernel.KDAKernel(r=4, value=1) ma_maps = kern.transform(testdata_cbma, return_type="image") ijk = testdata_cbma.coordinates.loc[testdata_cbma.coordinates["id"] == id_, ["i", "j", "k"]] ijk = np.squeeze(ijk.values.astype(int)) kern_data = ma_maps[0].get_fdata() com = np.array(center_of_mass(kern_data)).astype(int).T com = np.squeeze(com) assert np.array_equal(ijk, com)
def test_kdakernel_2mm(testdata_cbma): """ COMs of KDA kernel maps should match the foci fed in (assuming focus isn't masked out and spheres don't overlap). Test on 2mm template. """ id_ = "pain_01.nidm-1" kern = kernel.KDAKernel(r=4, value=1) ma_maps = kern.transform(testdata_cbma.coordinates, testdata_cbma.masker, return_type="image") ijk = testdata_cbma.coordinates.loc[testdata_cbma.coordinates["id"] == id_, ["i", "j", "k"]] ijk = np.squeeze(ijk.values.astype(int)) kern_data = ma_maps[0].get_fdata() com = np.array(center_of_mass(kern_data)).astype(int).T com = np.squeeze(com) assert np.array_equal(ijk, com)
# # CBMA kernels are available as {py:class}`~nimare.meta.kernel.KernelTransformer`s in the {py:mod}`nimare.meta.kernel` module. # 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