import matplotlib.pylab as mp
mp.figure()
mp.subplot(1,2,1)
mp.boxplot(kap)
mp.title('voxel-level reproducibility')
mp.xticks(range(1,1+len(thresholds)),thresholds)
mp.xlabel('threshold')
mp.subplot(1,2,2)
mp.boxplot(clt)
mp.title('cluster-level reproducibility')
mp.xticks(range(1,1+len(thresholds)),thresholds)
mp.xlabel('threshold')


mp.figure()
q = 1 
for threshold in thresholds:
    mp.subplot(3, len(thresholds)/3, q)
    rmap = map_reproducibility(func, var, xyz, ngroups,
                           method, verbose, threshold=threshold,
                           csize=csize)
    rmap = np.reshape(rmap, (dimx, dimy))
    mp.imshow(rmap, interpolation=None, vmin=0, vmax=ngroups)
    mp.title('threshold: %f' % threshold)
    q +=1
mp.suptitle('Map reproducibility for different thresholds') 
mp.colorbar()
mp.show()

    kappa = []
    cls = []
    sent = []
    peaks = []
    for i in range(niter):
        k = voxel_reproducibility(func, var, mask, ngroups,
                                  method, swap, verbose, **kwargs)
        kappa.append(k)
        cld = cluster_reproducibility(func, var, mask, ngroups, sigma,
                                      method, swap, verbose, **kwargs)
        cls.append(cld)
        peak = peak_reproducibility(func, var, mask, ngroups, sigma,
                                      method, swap, verbose, **kwargs)
        peaks.append(peak)
        seni = map_reproducibility(func, var, mask, ngroups,
                           method, True, verbose, threshold=threshold,
                           csize=csize).mean()/ngroups
        sent.append(seni)
    sens.append(np.array(sent))
    kap.append(np.array(kappa))
    clt.append(np.array(cls))
    pk.append(np.array(peaks))
    
################################################################################
# Visualize the results
import scipy.stats as st
aux = st.norm.sf(thresholds)

import matplotlib.pylab as mp
a = mp.figure()
mp.subplot(1, 3, 1)
mp.boxplot(kap)
mp.title("voxel-level reproducibility")
mp.xticks(range(1, 1 + len(thresholds)), thresholds)
mp.xlabel("threshold")
mp.subplot(1, 2, 2)
mp.boxplot(clt)
mp.title("cluster-level reproducibility")
mp.xticks(range(1, 1 + len(thresholds)), thresholds)
mp.xlabel("threshold")


################################################################################
# create an image

th = 4.0
swap = True
kwargs = {"threshold": th, "csize": csize}
rmap = map_reproducibility(Functional, VarFunctional, xyz, ngroups, method, swap, verbose, **kwargs)
wmap = mask.astype(np.int)
wmap[mask] = rmap
wim = Nifti1Image(wmap, affine)
wim.get_header()["descrip"] = (
    "reproducibility map at threshold %f, \
                             cluster size %d"
    % (th, csize)
)
wname = op.join(swd, "repro.nii")
save(wim, wname)

print ("Wrote a reproducibility image in %s" % wname)