def test_MKDAChi2_fdr(testdata_cbma): """Smoke test for MKDAChi2.""" meta = MKDAChi2() res = meta.fit(testdata_cbma, testdata_cbma) corr = FDRCorrector(method="bh", alpha=0.001) cres = corr.transform(res) assert isinstance(res, nimare.results.MetaResult) assert isinstance(cres, nimare.results.MetaResult)
def test_mkda_chi2_fdr(): """ Smoke test for MKDAChi2 """ meta = mkda.MKDAChi2() res = meta.fit(pytest.cbma_testdata1, pytest.cbma_testdata1) corr = FDRCorrector(method='fdr_bh', alpha=0.001) cres = corr.transform(res) assert isinstance(res, nimare.base.MetaResult) assert isinstance(cres, nimare.base.MetaResult)
def test_Fishers(testdata_ibma): """ Smoke test for Fisher's. """ meta = ibma.Fishers() res = meta.fit(testdata_ibma) corr = FDRCorrector(method="indep", alpha=0.001) cres = corr.transform(res) assert isinstance(meta.results, nimare.results.MetaResult) assert isinstance(res, nimare.results.MetaResult) assert isinstance(cres, nimare.results.MetaResult)
def test_fishers(): """ Smoke test for Fisher's. """ meta = ibma.Fishers() res = meta.fit(pytest.dset_z) corr = FDRCorrector(method='indep', alpha=0.001) cres = corr.transform(res) assert isinstance(meta.results, nimare.base.MetaResult) assert isinstance(res, nimare.base.MetaResult) assert isinstance(cres, nimare.base.MetaResult)
def analyze_and_plot(dset, ground_truth_foci=None, correct=True, return_cres=False): meta = ALE(kernel__fwhm=10) results = meta.fit(dset) if correct: corr = FDRCorrector() cres = corr.transform(results) else: cres = results # get the z coordinates if ground_truth_foci: stat_map_kwargs = {"cut_coords": [c[2] for c in ground_truth_foci]} else: stat_map_kwargs = {} fig, ax = plt.subplots() display = plot_stat_map( cres.get_map("z"), display_mode="z", draw_cross=False, cmap="Purples", threshold=2.3, symmetric_cbar=False, figure=fig, axes=ax, **stat_map_kwargs, ) if ground_truth_foci: # place red dots indicating the ground truth foci display.add_markers(ground_truth_foci) if return_cres: return fig, cres return fig
def test_ALE_approximate_null_unit(testdata_cbma, tmp_path_factory): """Unit test for ALE with approximate null_method.""" tmpdir = tmp_path_factory.mktemp("test_ALE_approximate_null_unit") out_file = os.path.join(tmpdir, "file.pkl.gz") meta = ale.ALE(null_method="approximate") res = meta.fit(testdata_cbma) assert "stat" in res.maps.keys() assert "p" in res.maps.keys() assert "z" in res.maps.keys() assert isinstance(res, nimare.results.MetaResult) assert isinstance(res.get_map("z", return_type="image"), nib.Nifti1Image) assert isinstance(res.get_map("z", return_type="array"), np.ndarray) res2 = res.copy() assert res2 != res assert isinstance(res, nimare.results.MetaResult) # Test saving/loading meta.save(out_file, compress=True) assert os.path.isfile(out_file) meta2 = ale.ALE.load(out_file, compressed=True) assert isinstance(meta2, ale.ALE) with pytest.raises(pickle.UnpicklingError): ale.ALE.load(out_file, compressed=False) meta.save(out_file, compress=False) assert os.path.isfile(out_file) meta2 = ale.ALE.load(out_file, compressed=False) assert isinstance(meta2, ale.ALE) with pytest.raises(OSError): ale.ALE.load(out_file, compressed=True) # Test MCC methods # Monte Carlo FWE corr = FWECorrector(method="montecarlo", voxel_thresh=0.001, n_iters=5, n_cores=-1) cres = corr.transform(meta.results) assert isinstance(cres, nimare.results.MetaResult) assert "z_level-cluster_corr-FWE_method-montecarlo" in cres.maps.keys() assert "z_level-voxel_corr-FWE_method-montecarlo" in cres.maps.keys() assert "logp_level-cluster_corr-FWE_method-montecarlo" in cres.maps.keys() assert "logp_level-voxel_corr-FWE_method-montecarlo" in cres.maps.keys() assert isinstance( cres.get_map("z_level-cluster_corr-FWE_method-montecarlo", return_type="image"), nib.Nifti1Image, ) assert isinstance( cres.get_map("z_level-cluster_corr-FWE_method-montecarlo", return_type="array"), np.ndarray ) # Bonferroni FWE corr = FWECorrector(method="bonferroni") cres = corr.transform(res) assert isinstance(cres, nimare.results.MetaResult) assert isinstance( cres.get_map("z_corr-FWE_method-bonferroni", return_type="image"), nib.Nifti1Image ) assert isinstance( cres.get_map("z_corr-FWE_method-bonferroni", return_type="array"), np.ndarray ) # FDR corr = FDRCorrector(method="indep", alpha=0.05) cres = corr.transform(meta.results) assert isinstance(cres, nimare.results.MetaResult) assert isinstance( cres.get_map("z_corr-FDR_method-indep", return_type="image"), nib.Nifti1Image ) assert isinstance(cres.get_map("z_corr-FDR_method-indep", return_type="array"), np.ndarray)
meta = Stouffers(resample=True) results = meta.fit(dset) print(f"FWECorrector options: {FWECorrector.inspect(results)}") print(f"FDRCorrector options: {FDRCorrector.inspect(results)}") ############################################################################### # Note that the FWECorrector does not support a "montecarlo" method for the # Stouffers Estimator. # This is because NiMARE does not have a Monte Carlo-based method implemented # for most IBMA algorithms. ############################################################################### # Apply the Corrector to the MetaResult # ============================================================================= corr = FDRCorrector(method="indep", alpha=0.05) cres = corr.transform(results) ############################################################################### # Show corrected results # ============================================================================= fig, axes = plt.subplots(figsize=(8, 6), nrows=2) plot_stat_map( cres.get_map("z"), draw_cross=False, cmap="RdBu_r", threshold=0.5, cut_coords=[0, 0, -8], figure=fig, axes=axes[0], )
def test_ALE_montecarlo_null_unit(testdata_cbma, tmp_path_factory): """Unit test for ALE with an montecarlo null_method. This test is run with low-memory kernel transformation as well. """ tmpdir = tmp_path_factory.mktemp("test_ALE_montecarlo_null_unit") out_file = os.path.join(tmpdir, "file.pkl.gz") meta = ale.ALE(null_method="montecarlo", n_iters=10, kernel__memory_limit="1gb") res = meta.fit(testdata_cbma) assert "stat" in res.maps.keys() assert "p" in res.maps.keys() assert "z" in res.maps.keys() assert isinstance(res, nimare.results.MetaResult) assert isinstance(res.get_map("z", return_type="image"), nib.Nifti1Image) assert isinstance(res.get_map("z", return_type="array"), np.ndarray) res2 = res.copy() assert res2 != res assert isinstance(res, nimare.results.MetaResult) # Test saving/loading meta.save(out_file, compress=True) assert os.path.isfile(out_file) meta2 = ale.ALE.load(out_file, compressed=True) assert isinstance(meta2, ale.ALE) with pytest.raises(pickle.UnpicklingError): ale.ALE.load(out_file, compressed=False) meta.save(out_file, compress=False) assert os.path.isfile(out_file) meta2 = ale.ALE.load(out_file, compressed=False) assert isinstance(meta2, ale.ALE) with pytest.raises(OSError): ale.ALE.load(out_file, compressed=True) # Test MCC methods # Monte Carlo FWE corr = FWECorrector(method="montecarlo", voxel_thresh=0.001, n_iters=5, n_cores=-1) cres = corr.transform(res) assert isinstance(cres, nimare.results.MetaResult) assert "z_desc-size_level-cluster_corr-FWE_method-montecarlo" in cres.maps.keys( ) assert "z_desc-mass_level-cluster_corr-FWE_method-montecarlo" in cres.maps.keys( ) assert "z_level-voxel_corr-FWE_method-montecarlo" in cres.maps.keys() assert "logp_desc-size_level-cluster_corr-FWE_method-montecarlo" in cres.maps.keys( ) assert "logp_desc-mass_level-cluster_corr-FWE_method-montecarlo" in cres.maps.keys( ) assert "logp_level-voxel_corr-FWE_method-montecarlo" in cres.maps.keys() assert isinstance( cres.get_map("z_desc-size_level-cluster_corr-FWE_method-montecarlo", return_type="image"), nib.Nifti1Image, ) assert isinstance( cres.get_map("z_desc-size_level-cluster_corr-FWE_method-montecarlo", return_type="array"), np.ndarray, ) assert isinstance( cres.get_map("z_desc-mass_level-cluster_corr-FWE_method-montecarlo", return_type="image"), nib.Nifti1Image, ) assert isinstance( cres.get_map("z_desc-mass_level-cluster_corr-FWE_method-montecarlo", return_type="array"), np.ndarray, ) # Check that the updated null distribution is in the corrected MetaResult's Estimator. assert ("values_desc-mass_level-cluster_corr-fwe_method-montecarlo" in cres.estimator.null_distributions_.keys()) # The updated null distribution should *not* be in the original Estimator, nor in the # uncorrected MetaResult's Estimator. assert ("values_desc-mass_level-cluster_corr-fwe_method-montecarlo" not in meta.null_distributions_.keys()) assert ("values_desc-mass_level-cluster_corr-fwe_method-montecarlo" not in res.estimator.null_distributions_.keys()) # Bonferroni FWE corr = FWECorrector(method="bonferroni") cres = corr.transform(res) assert isinstance(cres, nimare.results.MetaResult) assert isinstance( cres.get_map("z_corr-FWE_method-bonferroni", return_type="image"), nib.Nifti1Image) assert isinstance( cres.get_map("z_corr-FWE_method-bonferroni", return_type="array"), np.ndarray) # FDR corr = FDRCorrector(method="indep", alpha=0.05) cres = corr.transform(res) assert isinstance(cres, nimare.results.MetaResult) assert isinstance( cres.get_map("z_corr-FDR_method-indep", return_type="image"), nib.Nifti1Image) assert isinstance( cres.get_map("z_corr-FDR_method-indep", return_type="array"), np.ndarray)
########################################## # multiple comparison correctors (testing) ########################################## @pytest.fixture( scope="session", params=[ pytest.param(FWECorrector(method="bonferroni"), id="fwe_bonferroni"), pytest.param( FWECorrector(method="montecarlo", voxel_thresh=ALPHA, n_iters=100, n_cores=N_CORES), id="fwe_montecarlo", ), pytest.param(FDRCorrector(method="indep", alpha=ALPHA), id="fdr_indep"), pytest.param(FDRCorrector(method="negcorr", alpha=ALPHA), id="fdr_negcorr"), ], ) def corr(request): """Define multiple comparisons correctors for tests.""" return request.param ########################################## # multiple comparison correctors (smoke) ########################################## @pytest.fixture( scope="session",
figure=fig, axes=axes[i_ax], ) axes[i_ax].set_title(title) from nimare.correct import FDRCorrector ############################################################################### # Multiple comparisons correction in image-based meta-analyses # ----------------------------------------------------------------------------- from nimare.meta.ibma import Stouffers meta = Stouffers(resample=True) results = meta.fit(dset) print(FWECorrector.inspect(results)) print(FDRCorrector.inspect(results)) ############################################################################### # Note that the FWECorrector does not support a "montecarlo" method for the # Stouffers Estimator. # This is because NiMARE does not have a Monte Carlo-based method implemented # for most IBMA algorithms. ############################################################################### # Apply the Corrector to the MetaResult # ============================================================================= corr = FDRCorrector(method="indep", alpha=0.05) cres = corr.transform(results) ############################################################################### # Show corrected results