"""

.. _metas_ibma:

====================================
Image-based meta-analysis algorithms
====================================

A tour of IBMA algorithms in NiMARE.

This tutorial is intended to provide a brief description and example of each of
the IBMA algorithms implemented in NiMARE.
For a more detailed introduction to the elements of an image-based
meta-analysis, see other stuff.
"""

from nilearn.plotting import plot_stat_map

###############################################################################
# Download data
# -----------------------------------------------------------------------------
# .. note::
#   The data used in this example come from a collection of NIDM-Results packs
#   downloaded from Neurovault collection 1425, uploaded by Dr. Camille Maumet.
from nimare.extract import download_nidm_pain

dset_dir = download_nidm_pain()

###############################################################################
# Load Studyset
# -----------------------------------------------------------------------------
import os
from pprint import pprint

from nimare.nimads import Studyset
from nimare.transforms import ImageTransformer
from nimare.utils import get_resource_path

studyset_file = os.path.join(get_resource_path(), "nidm_pain_studyset.json")
studyset = Studyset(studyset_file, target="mni152_2mm")
studyset.update_path(dset_dir)

# Calculate missing images
xformer = ImageTransformer(target=["varcope", "z"])
studyset = xformer.transform(studyset)

###############################################################################
# Stouffer's
# -----------------------------------------------------------------------------
from nimare.meta.ibma import Stouffers

meta = Stouffers(use_sample_size=False)
results = meta.fit(studyset)

plot_stat_map(
    results.get_map("z"),
    cut_coords=[0, 0, -8],
    draw_cross=False,
    cmap="RdBu_r",
    symmetric_cbar=True,
)

print("Description:")
pprint(results.description_)
print("References:")
pprint(results.bibtex_)

###############################################################################
# Stouffer's with weighting by sample size
# -----------------------------------------------------------------------------
meta = Stouffers(use_sample_size=True)
results = meta.fit(studyset)

plot_stat_map(
    results.get_map("z"),
    cut_coords=[0, 0, -8],
    draw_cross=False,
    cmap="RdBu_r",
    symmetric_cbar=True,
)

print("Description:")
pprint(results.description_)
print("References:")
pprint(results.bibtex_)

###############################################################################
# Fisher's
# -----------------------------------------------------------------------------
from nimare.meta.ibma import Fishers

meta = Fishers()
results = meta.fit(studyset)

plot_stat_map(
    results.get_map("z"),
    cut_coords=[0, 0, -8],
    draw_cross=False,
    cmap="RdBu_r",
    symmetric_cbar=True,
)

print("Description:")
pprint(results.description_)
print("References:")
pprint(results.bibtex_)

###############################################################################
# Permuted OLS
# -----------------------------------------------------------------------------
from nimare.correct import FWECorrector
from nimare.meta.ibma import PermutedOLS

meta = PermutedOLS(two_sided=True)
results = meta.fit(studyset)

plot_stat_map(
    results.get_map("z"),
    cut_coords=[0, 0, -8],
    draw_cross=False,
    cmap="RdBu_r",
    symmetric_cbar=True,
)

corrector = FWECorrector(method="montecarlo", n_iters=100, n_cores=1)
cresult = corrector.transform(results)

plot_stat_map(
    cresult.get_map("z_level-voxel_corr-FWE_method-montecarlo"),
    cut_coords=[0, 0, -8],
    draw_cross=False,
    cmap="RdBu_r",
    symmetric_cbar=True,
)

print("Description:")
pprint(cresult.description_)
print("References:")
pprint(cresult.bibtex_)

###############################################################################
# Weighted Least Squares
# -----------------------------------------------------------------------------
from nimare.meta.ibma import WeightedLeastSquares

meta = WeightedLeastSquares(tau2=0)
results = meta.fit(studyset)

plot_stat_map(
    results.get_map("z"),
    cut_coords=[0, 0, -8],
    draw_cross=False,
    cmap="RdBu_r",
    symmetric_cbar=True,
)

print("Description:")
pprint(results.description_)
print("References:")
pprint(results.bibtex_)

###############################################################################
# DerSimonian-Laird
# -----------------------------------------------------------------------------
from nimare.meta.ibma import DerSimonianLaird

meta = DerSimonianLaird()
results = meta.fit(studyset)

plot_stat_map(
    results.get_map("z"),
    cut_coords=[0, 0, -8],
    draw_cross=False,
    cmap="RdBu_r",
    symmetric_cbar=True,
)

print("Description:")
pprint(results.description_)
print("References:")
pprint(results.bibtex_)

###############################################################################
# Hedges
# -----------------------------------------------------------------------------
from nimare.meta.ibma import Hedges

meta = Hedges()
results = meta.fit(studyset)

plot_stat_map(
    results.get_map("z"),
    cut_coords=[0, 0, -8],
    draw_cross=False,
    cmap="RdBu_r",
    symmetric_cbar=True,
)

print("Description:")
pprint(results.description_)
print("References:")
pprint(results.bibtex_)


###############################################################################
# Fixed Effects Meta-Analysis with Hedges’ g
# -----------------------------------------------------------------------------
from nimare.meta.ibma import FixedEffectsHedges

meta = FixedEffectsHedges(tau2=0)
results = meta.fit(studyset)

plot_stat_map(
    results.get_map("z"),
    cut_coords=[0, 0, -8],
    draw_cross=False,
    cmap="RdBu_r",
    symmetric_cbar=True,
)

print("Description:")
pprint(results.description_)
print("References:")
pprint(results.bibtex_)
