示例#1
0
    mask_images = []
    for i in range(data.shape[0]):
        aux[list(xyz)] = data[i, :]
        data_images.append(Image(aux.copy(), np.eye(4)))
        aux[list(xyz)] = vardata[i, :]
        vardata_images.append(Image(aux.copy(), np.eye(4)))
        aux[list(xyz)] = 1
        mask_images.append(Image(aux.copy(), np.eye(4)))

    return data_images, vardata_images, mask_images


data_images, vardata_images, mask_images = remake_images()

# zimg, mask = sm.onesample_test(data_images, None, mask_images, 'student')
zimg, mask, nulls = sm.onesample_test(
    data_images, None, mask_images, "wilcoxon", permutations=1024, cluster_forming_th=0.01
)
clusters, info = sm.cluster_stats(zimg, mask, 0.01, nulls=nulls)

"""
# Save results
np.savez('group_result.npz', clusters=clusters, info=info)
"""
"""
# Load results 
f = np.load('group_result.npz')
clusters = f['clusters']
info = f['info']
"""
示例#2
0
def display_results_html(zmap_file_path, mask_file_path,
                         output_html_path, threshold=0.001,
                         method='fpr', cluster=0, null_zmax='bonferroni',
                         null_smax=None, null_s=None, nmaxima=4):
    """
    Parameters
    ----------
    zmap_file_path, string,
                    path of the input activation (z-transformed) image
    mask_file_path, string,
                    path of the a corresponding mask image
    output_html_path, string,
                      path where the output html should be written
    threshold, float, optional
               (p-variate) frequentist threshold of the activation image
    method, string, optional
            to be chosen as height_control in 
            nipy.neurospin.statistical_mapping
    cluster, scalar, optional,
             cluster size threshold
    null_zmax: optional,
               parameter for cluster leve statistics (?)
    null_s: optional,
             parameter for cluster leve statistics (?)
    nmaxima: optional,
             number of local maxima reported per supra-threshold cluster    
    """
    import nipy.neurospin.statistical_mapping as sm

    # Read data: z-map and mask     
    zmap = load(zmap_file_path)
    mask = load(mask_file_path)

    cluster_th = cluster
   
    # Compute cluster statistics
    #if null_smax != None:
    nulls={'zmax' : null_zmax, 'smax' : null_smax, 's' : null_s}
    clusters, info = sm.cluster_stats(zmap, mask, height_th=threshold,
                                      height_control=method.lower(),
                                      cluster_th=cluster, nulls=nulls)
    if clusters == None or info == None:
        print "No results were written for %s" % zmap_file_path
        return
    
    # Make HTML page 
    output = open(output_html_path, mode = "w")
    output.write("<html><head><title> Result Sheet for %s \
    </title></head><body><center>\n" % zmap_file_path)
    output.write("<h2> Results for %s</h2>\n" % zmap_file_path)
    output.write("<table border = 1>\n")
    output.write("<tr><th colspan=4> Voxel significance </th>\
    <th colspan=3> Coordinates in MNI referential</th>\
    <th>Cluster Size</th></tr>\n")
    output.write("<tr><th>p FWE corr<br>(Bonferroni)</th>\
    <th>p FDR corr</th><th>Z</th><th>p uncorr</th>")
    output.write("<th> x (mm) </th><th> y (mm) </th><th> z (mm) </th>\
    <th>(voxels)</th></tr>\n")

    for cluster in clusters:
        maxima = cluster['maxima']
        size=cluster['size']
        for j in range(min(len(maxima), nmaxima)):
            temp = ["%f" % cluster['fwer_pvalue'][j]]
            temp.append("%f" % cluster['fdr_pvalue'][j])
            temp.append("%f" % cluster['zscore'][j])
            temp.append("%f" % cluster['pvalue'][j])
            for it in range(3):
                temp.append("%f" % maxima[j][it])
            if j == 0:
                # Main local maximum
                temp.append('%i'%size)
                output.write('<tr><th align="center">' + '</th>\
                <th align="center">'.join(temp) + '</th></tr>')
            else:
                # Secondary local maxima 
                output.write('<tr><td align="center">' + '</td>\
                <td align="center">'.join(temp) + '</td><td></td></tr>\n')

                 
    nclust = len(clusters)
    nvox = sum([clusters[k]['size'] for k in range(nclust)])
    
    output.write("</table>\n")
    output.write("Number of voxels : %i<br>\n" % nvox)
    output.write("Number of clusters : %i<br>\n" % nclust)
    output.write("Threshold Z = %f (%s control at %f)<br>\n" \
                 % (info['threshold_z'], method, threshold))
    output.write("Cluster size threshold = %i voxels"%cluster_th)
    output.write("</center></body></html>\n")
    output.close()
示例#3
0
def ComputeResultsContents(zmap_file_path, mask_file_path,
                           output_html_path, threshold=0.001,
                           method='fpr', cluster=0, null_zmax='bonferroni',
                           null_smax=None, null_s=None, nmaxima=4):

    # Read data: z-map and mask 
    
    
    zmap = load(zmap_file_path)
    mask = load(mask_file_path)

    # Compute cluster statistics
    #if null_smax != None:
    nulls={'zmax' : null_zmax, 'smax' : null_smax, 's' : null_s}
    clusters, info = sm.cluster_stats(zmap, mask, height_th=threshold,
                                      height_control=method.lower(),
                                      cluster_th=cluster, nulls=nulls)
    if clusters == None or info == None:
        print "No results were writen for %s" % zmap_file_path
        return
    #else:
    #   clusters, info = sm.cluster_stats(zmap, mask, height_th=threshold,
    # height_control=method, cluster_th=cluster,
    #                     nulls={})
    
    # Make HTML page 
    output = open(output_html_path, mode = "w")
    output.write("<html><head><title> Result Sheet for %s \
    </title></head><body><center>\n" % zmap_file_path)
    output.write("<h2> Results for %s</h2>\n" % zmap_file_path)
    output.write("<table border = 1>\n")
    output.write("<tr><th colspan=4> Voxel significance </th>\
    <th colspan=3> Coordinates in MNI referential</th>\
    <th>Cluster Size</th></tr>\n")
    output.write("<tr><th>p FWE corr<br>(Bonferroni)</th>\
    <th>p FDR corr</th><th>Z</th><th>p uncorr</th>")
    output.write("<th> x (mm) </th><th> y (mm) </th><th> z (mm) </th>\
    <th>(voxels)</th></tr>\n")

    for cluster in clusters:
        maxima = cluster['maxima']
        for j in range(min(len(maxima), nmaxima)):
            temp = ["%f" % cluster['fwer_pvalue'][j]]
            temp.append("%f" % cluster['fdr_pvalue'][j])
            temp.append("%f" % cluster['zscore'][j])
            temp.append("%f" % cluster['pvalue'][j])
            for it in range(3):
                temp.append("%f" % maxima[j][it])
            if j == 0: ## Main local maximum 
                output.write('<tr><th align="center">' + '</th>\
                <th align="center">'.join(temp) + '</th></tr>\n')
            else: ## Secondary local maxima 
                output.write('<tr><td align="center">' + '</td>\
                <td align="center">'.join(temp) + '</td><td></td></tr>\n')

    output.write("</table>\n")
    output.write("Number of voxels : %i<br>\n" % len(mask.get_data() > 0))
    output.write("Threshold Z=%f (%s control at %f)<br>\n" \
                 % (info['threshold_z'], method, threshold))
    output.write("</center></body></html>\n")
    output.close()
def test_8():
    img = make_surrogate_data()
    clusters, info = cluster_stats(img, img, height_th=.001, height_control='fpr', cluster_th=0, nulls={})
    nstv = sum([c['size'] for c in clusters])
    assert nstv==36
def test_6():
    img = make_surrogate_data()
    clusters, info = cluster_stats(img, img, height_th=.05, height_control='fdr', cluster_th=0, nulls={})
    print len(clusters), sum([c['size'] for c in clusters])
    assert len(clusters)==4
def test_5():
    img = make_surrogate_data()
    clusters, info = cluster_stats(img, img, height_th=.05, height_control='bonferroni', cluster_th=0, nulls={})
    assert len(clusters)==4
def test_4():
    img = make_surrogate_data()
    clusters, info = cluster_stats(img, img, height_th=.001, height_control='fpr', cluster_th=0, nulls={})
    assert len(clusters)==4
def test3():
    img = make_surrogate_data()
    clusters, info = cluster_stats(img, img, height_th=3., height_control='None', cluster_th=10, nulls={})
    assert len(clusters)==0