Exemple #1
0
def run(opts):
    adjacency = []
    # check for tmi file first
    if opts.appendtmi:
        if not os.path.exists(opts.appendtmi[0]):
            print "Cannot find tmi file: %s" % opts.appendtmi[0]
            quit()
    if opts.datatype[0] == 'voxel':
        if not opts.voxeladjacency:
            print "-va must be specified for voxel input data"
            quit()
        for i in range(len(opts.input)):
            mask_data = nib.load(opts.input[i]).get_data()
            data_index = mask_data == 1
            print "Computing adjacency for %d voxel with %d direction adjacency" % (
                len(data_index[data_index == True]), opts.voxeladjacency[0])
            adjacency.append(
                (create_adjac_voxel(data_index,
                                    dirtype=opts.voxeladjacency[0])))
    else:
        for i in range(len(opts.input)):
            if opts.datatype[0] == 'srf':
                v, f = convert_fs(str(opts.input[i]))
            if opts.datatype[0] == 'ply':
                v, f, _ = convert_ply(str(opts.input[i]))
            if opts.datatype[0] == 'mni_obj':
                v, f = convert_mni_object(str(opts.input[i]))
            if opts.datatype[0] == 'gii':
                v, f = convert_gifti(str(opts.input[i]))
            if opts.geodistance:
                min_dist = float(opts.geodistance[0])
                max_dist = float(opts.geodistance[1])
                step = float(opts.stepsize[0])
                max_dist += step
                step_range = np.arange(min_dist, max_dist, step=step)
                if np.divide(max_dist - min_dist, step).is_integer():
                    if opts.projectfraction:
                        projfrac = float(opts.projectfraction[0])
                        t = nib.freesurfer.read_morph_data(
                            opts.inputthickness[i])
                        temp_adjacency = compute_adjacency(min_dist,
                                                           max_dist,
                                                           step,
                                                           v,
                                                           f,
                                                           projfrac=projfrac,
                                                           t=t)
                    else:
                        temp_adjacency = compute_adjacency(
                            min_dist, max_dist, step, v, f)
                else:
                    print "The difference between max and min distance must be evenly divisible by the step size."
                    exit()
                if opts.setappendadj:
                    adjacency.append((temp_adjacency[int(
                        np.argwhere(
                            step_range == float(opts.setappendadj[0])))]))
                else:
                    if opts.appendtmi:
                        print "Warning: Multiple adjacency sets are appended for each surface."
                    count = 0
                    for j in step_range:
                        print "Appending adjacency set at geodesic distance of %1.2f" % j
                        adjacency.append((temp_adjacency[count]))
                        count += 1
            if opts.triangularmesh:
                adjacency.append((create_adjac_vertex(v, f)))

    # output adjacency sets
    if opts.outputnpy:
        for i in range(len(opts.input)):
            outname = os.path.basename(opts.input[i])
            basename = os.path.splitext(outname)[0]
            if opts.datatype[0] == 'voxel':
                outname = 'adjac_set_%d_dir%d_%s.npy' % (
                    i, opts.voxeladjacency[0], basename)
                np.save(outname, adjacency[i])
            elif opts.setappendadj:
                outname = 'adjac_set_%d_%1.2fmm_%s.npy' % (
                    i, float(opts.setappendadj[0]), basename)
                np.save(outname, adjacency[i])
            else:
                count = 0
                for j in step_range:
                    outname = 'adjac_set_%d_%1.2fmm_%s.npy' % (i, j, basename)
                    np.save(outname, adjacency[count])
                    count += 1

    if opts.appendtmi:
        outname = opts.appendtmi[0]
        _, image_array, masking_array, maskname, affine_array, vertex_array, face_array, surfname, adjacency_array, tmi_history, subjectids = read_tm_filetype(
            outname, verbose=True)
        for i in range(len(opts.input)):
            adjacency_array.append((adjacency[i]))
        write_tm_filetype(outname,
                          image_array=image_array[0],
                          masking_array=masking_array,
                          maskname=maskname,
                          affine_array=affine_array,
                          vertex_array=vertex_array,
                          face_array=face_array,
                          surfname=surfname,
                          checkname=False,
                          tmi_history=tmi_history,
                          adjacency_array=adjacency_array)
def run(opts):
    image_array = []
    affine_array = []
    masking_array = []
    maskname = []
    vertex_array = []
    face_array = []
    surfname = []
    adjacency_array = []
    tmi_history = []

    if opts.outputname:
        outname = opts.outputname[0]
        if not outname.endswith('tmi'):
            if opts.outputtype == 'binary':
                if not outname.endswith('tmi'):
                    outname += '.tmi'
            else:
                outname += '.ascii.tmi'
    if opts.append:
        outname = opts.append[0]
        _, image_array, masking_array, maskname, affine_array, vertex_array, face_array, surfname, adjacency_array, tmi_history, subjectids = read_tm_filetype(
            outname)

    if opts.inputimages:
        for i in range(len(opts.inputimages)):
            basename, file_ext = os.path.splitext(opts.inputimages[i])
            if file_ext == '.gz':

                os.system("zcat %s > %s" % (opts.inputimages[i], basename))
                img = nib.load('%s' % basename)
            else:
                img = nib.load(opts.inputimages[i])
            img_data = img.get_data()

            if opts.inputmasks:
                mask = nib.load(opts.inputmasks[i])
                mask_data = mask.get_data()
                if not np.array_equal(img_data.shape[:3], mask_data.shape[:3]):
                    print "Error mask data dimension do not fit image dimension"
                    exit()
                mask_data = mask_data == 1
                img_data = img_data[mask_data]
            else:
                img_data, mask_data = maskdata(img_data)
            masking_array.append(np.array(mask_data))
            image_array.append(np.array(img_data))
            affine_array.append(img.affine)
            maskname.append(np.array(os.path.basename(opts.inputimages[i])))
            if file_ext == '.gz':
                os.system("rm %s" % basename)

    if opts.concatenateimages:
        img = nib.load(opts.concatenateimages[0])
        img_data = img.get_data()
        numMerge = len(opts.concatenateimages)

        if opts.inputmasks:
            if not len(opts.inputmasks) == 1:
                print "Only one mask can be added using concatenate. See help (hint: rerun using append option for multiple modalities/surfaces)"
                exit()
            mask = nib.load(opts.inputmasks[0])
            mask_data = mask.get_data()
            if not np.array_equal(img_data.shape[:3], mask_data.shape[:3]):
                print "Error mask data dimension do not fit image dimension"
                exit()
            mask_data = mask_data == 1
            img_data = img_data[mask_data].astype(np.float32)
        else:
            print "Creating mask from first image."
            img_data, mask_data = maskdata(img_data)

        for i in xrange(numMerge):
            print "merging image %s" % opts.concatenateimages[i]
            if i > 0:
                tempdata = nib.load(opts.concatenateimages[i]).get_data()
                tempdata = tempdata[mask_data].astype(np.float32)
                if opts.scale:
                    tempdata = zscaler(tempdata.T).T
                img_data = np.column_stack((img_data, tempdata))
            else:
                if opts.scale:
                    img_data = zscaler(img_data.T).T

        masking_array.append(np.array(mask_data))
        image_array.append(np.array(img_data))
        affine_array.append(img.affine)
        if opts.concatenatename:
            maskname.append(np.array(os.path.basename(
                opts.concatenatename[0])))

    if opts.inputtext:
        for i in range(len(opts.inputtext)):
            #img_data = np.genfromtxt(opts.inputtext[i], delimiter=',') # slower, more ram usage
            img_data = []
            with open(opts.inputtext[i]) as data_file:
                for line in data_file:
                    img_data.append(line.strip().split(','))
            img_data = np.array(img_data).astype(np.float32)

            img_data, mask_data = maskdata(img_data)
            masking_array.append(np.array(mask_data))
            image_array.append(np.array(img_data))

    if opts.concatenatetext:

        firstimg_data = np.genfromtxt(opts.concatenatetext[0], delimiter=',')
        numMerge = len(opts.concatenatetext)

        for i in xrange(numMerge):
            print "merging text file %s" % opts.concatenatetext[i]
            if i > 0:
                tempdata = np.genfromtxt(opts.concatenatetext[i],
                                         delimiter=',')
            img_data = np.column_stack((img_data, tempdata))
        img_data, mask_data = maskdata(img_data)
        masking_array.append(np.array(mask_data))
        image_array.append(np.array(img_data))

    if opts.concatenatebinary:

        firstimg_data = np.fromfile(opts.concatenatebinary[0], dtype='f')
        numMerge = len(opts.concatenatebinary)

        for i in xrange(numMerge):
            print "merging simple float binary file %s" % opts.concatenatebinary[
                i]
            if i > 0:
                tempdata = np.fromfile(opts.concatenatebinary[i], dtype='f')
            img_data = np.column_stack((img_data, tempdata))
        img_data, mask_data = maskdata(img_data)
        masking_array.append(np.array(mask_data))
        image_array.append(np.array(img_data))

    if opts.inputfreesurfer:
        for i in range(len(opts.inputfreesurfer)):
            v, f = convert_fs(str(opts.inputfreesurfer[i]))
            vertex_array.append(v)
            face_array.append(f)
            surfname.append(np.array(os.path.basename(
                opts.inputfreesurfer[i])))
    if opts.inputgifti:
        for i in range(len(opts.inputgifti)):
            v, f = convert_gifti(str(opts.inputgifti[i]))
            vertex_array.append(v)
            face_array.append(f)
            surfname.append(np.array(os.path.basename(opts.inputgifti[i])))
    if opts.inputmniobj:
        for i in range(len(opts.inputmniobj)):
            v, f = convert_mni_object(str(opts.inputmniobj[i]))
            vertex_array.append(v)
            face_array.append(f)
            surfname.append(np.array(os.path.basename(opts.inputmniobj[i])))
    if opts.inputply:
        for i in range(len(opts.inputply)):
            v, f = convert_ply(str(opts.inputply[i]))
            vertex_array.append(v)
            face_array.append(f)
            surfname.append(np.array(os.path.basename(opts.inputply[i])))
    if opts.inputadjacencyobject:
        for i in range(len(opts.inputadjacencyobject)):
            adjacency_array.append(np.load(str(opts.inputadjacencyobject[i])))
        if not np.equal(len(adjacency_array), len(masking_array)):
            if not len(adjacency_array) % len(masking_array) == 0:
                print "Number of adjacency objects does not match number of images."
            else:
                print "Error number of adjacency objects is not divisable by the number of masking arrays."
                exit()

    # Write tmi file
    if not image_array == []:
        write_tm_filetype(outname,
                          output_binary=opts.outputtype == 'binary',
                          image_array=np.vstack(image_array),
                          masking_array=masking_array,
                          maskname=maskname,
                          affine_array=affine_array,
                          vertex_array=vertex_array,
                          face_array=face_array,
                          surfname=surfname,
                          adjacency_array=adjacency_array,
                          checkname=False,
                          tmi_history=tmi_history)
    else:
        write_tm_filetype(outname,
                          output_binary=opts.outputtype == 'binary',
                          masking_array=masking_array,
                          maskname=maskname,
                          affine_array=affine_array,
                          vertex_array=vertex_array,
                          face_array=face_array,
                          surfname=surfname,
                          adjacency_array=adjacency_array,
                          checkname=False,
                          tmi_history=tmi_history)
def run(opts):
    currentTime = int(time())
    if opts.multisurfacefwecorrection:
        #############################
        ###### FWER CORRECTION ######
        #############################
        _, image_array, masking_array, maskname, affine_array, vertex_array, face_array, surfname, adjacency_array, tmi_history, columnids = read_tm_filetype(
            '%s' % opts.tmifile[0], verbose=False)

        # check file dimensions
        if not image_array[0].shape[1] % 3 == 0:
            print(
                'Print file format is not understood. Please make sure %s is statistics file.'
                % opts.tmifile[0])
            quit()
        else:
            num_contrasts = int(image_array[0].shape[1] / 3)

        # get surface coordinates in data array
        position_array = create_position_array(masking_array)

        if num_contrasts == 1:
            # get lists for positive and negative contrasts
            pos_range = [1]
            neg_range = [2]
        else:
            # get lists for positive and negative contrasts
            pos_range = list(
                range(num_contrasts, num_contrasts + num_contrasts))
            neg_range = list(
                range(num_contrasts * 2, num_contrasts * 2 + num_contrasts))

        # check that randomisation has been run
        if not os.path.exists(
                "%s/output_%s/perm_maxTFCE_surf0_tcon1.csv" %
            (os.getcwd(), opts.tmifile[0])):  # make this safer
            print(
                'Permutation folder not found. Please run --randomise first.')
            quit()

        #check permutation file lengths
        num_surf = len(masking_array)
        surface_range = list(range(num_surf))
        num_perm = lowest_length(num_contrasts, surface_range, opts.tmifile[0])

        if opts.setsurfacerange:
            surface_range = list(
                range(opts.setsurfacerange[0], opts.setsurfacerange[1] + 1))
        elif opts.setsurface:
            surface_range = opts.setsurface
        if np.array(surface_range).max() > len(masking_array):
            print(
                "Error: range does note fit the surfaces contained in the tmi file. %s contains the following surfaces"
                % opts.tmifile[0])
            for i in range(len(surfname)):
                print(("Surface %d : %s, %s" % (i, surfname[i], maskname[i])))
            quit()
        print("Reading %d contrast(s) from %d of %d surface(s)" %
              ((num_contrasts), len(surface_range), num_surf))
        print("Reading %s permutations with an accuracy of p=0.05+/-%.4f" %
              (num_perm, (2 * (np.sqrt(0.05 * 0.95 / num_perm)))))

        # calculate the P(FWER) images from all surfaces
        positive_data, negative_data = apply_mfwer(image_array,
                                                   num_contrasts,
                                                   surface_range,
                                                   num_perm,
                                                   num_surf,
                                                   opts.tmifile[0],
                                                   position_array,
                                                   pos_range,
                                                   neg_range,
                                                   weight='logmasksize')

        # write out files
        if opts.concatestats:
            write_tm_filetype(opts.tmifile[0],
                              image_array=positive_data,
                              masking_array=masking_array,
                              maskname=maskname,
                              affine_array=affine_array,
                              vertex_array=vertex_array,
                              face_array=face_array,
                              surfname=surfname,
                              adjacency_array=adjacency_array,
                              checkname=False,
                              tmi_history=tmi_history)
            _, image_array, masking_array, maskname, affine_array, vertex_array, face_array, surfname, adjacency_array, tmi_history, columnids = read_tm_filetype(
                opts.tmifile[0], verbose=False)
            write_tm_filetype(opts.tmifile[0],
                              image_array=np.column_stack(
                                  (image_array[0], negative_data)),
                              masking_array=masking_array,
                              maskname=maskname,
                              affine_array=affine_array,
                              vertex_array=vertex_array,
                              face_array=face_array,
                              surfname=surfname,
                              adjacency_array=adjacency_array,
                              checkname=False,
                              tmi_history=tmi_history)
        else:
            for i in range(len(opts.outtype)):
                if opts.outtype[i] == 'tmi':
                    contrast_names = []

                    for j in range(num_contrasts):
                        contrast_names.append(("tstat_pFWER_con%d" % (j + 1)))
                    for k in range(num_contrasts):
                        contrast_names.append(
                            ("negtstat_pFWER_con%d" % (k + 1)))

                    outdata = np.column_stack((positive_data, negative_data))

                    if opts.neglog:
                        for j in range(num_contrasts):
                            contrast_names.append(
                                ("tstat_negLog_pFWER_con%d" % (j + 1)))
                        for k in range(num_contrasts):
                            contrast_names.append(
                                ("negtstat_negLog_pFWER_con%d" % (k + 1)))

                    outdata = np.column_stack(
                        (outdata, -np.log10(1 - positive_data)))
                    outdata = np.column_stack(
                        (outdata, -np.log10(1 - negative_data)))

                    write_tm_filetype("pFWER_%s" % opts.tmifile[0],
                                      image_array=outdata,
                                      masking_array=masking_array,
                                      maskname=maskname,
                                      affine_array=affine_array,
                                      vertex_array=vertex_array,
                                      face_array=face_array,
                                      surfname=surfname,
                                      checkname=False,
                                      columnids=np.array(contrast_names),
                                      tmi_history=tmi_history)

                else:
                    if opts.outtype[i] == 'mgh':
                        savefunc = savemgh_v2
                    if opts.outtype[i] == 'nii.gz':
                        savefunc = savenifti_v2
                    if opts.outtype[i] == 'auto':
                        savefunc = saveauto
                    for surf_count in surface_range:
                        start = position_array[surf_count]
                        end = position_array[surf_count + 1]
                        basename = strip_basename(maskname[surf_count])
                        if not os.path.exists("output_stats"):
                            os.mkdir("output_stats")
                        out_image = positive_data[start:end]
                        temp_image = negative_data[start:end]
                        for contrast in range(num_contrasts):
                            out_image[temp_image[:, contrast] != 0,
                                      contrast] = temp_image[
                                          temp_image[:, contrast] != 0,
                                          contrast] * -1
                        if affine_array == []:
                            savefunc(
                                out_image, masking_array[surf_count],
                                "output_stats/%d_%s_pFWER" %
                                (surf_count, basename))
                        else:
                            savefunc(
                                out_image, masking_array[surf_count],
                                "output_stats/%d_%s_pFWER" %
                                (surf_count, basename),
                                affine_array[surf_count])
                        if opts.neglog:
                            out_image = -np.log10(1 - positive_data[start:end,
                                                                    contrast])
                            temp_image = np.log10(1 - negative_data[start:end,
                                                                    contrast])
                            for contrast in range(num_contrasts):
                                out_image[temp_image[:, contrast] != 0,
                                          contrast] = temp_image[
                                              temp_image[:, contrast] != 0,
                                              contrast]
                            if affine_array == []:
                                savefunc(
                                    out_image, masking_array[surf_count],
                                    "output_stats/%d_%s_negLog_pFWER" %
                                    (surf_count, basename))
                            else:
                                savefunc(
                                    out_image, masking_array[surf_count],
                                    "output_stats/%d_%s_negLog_pFWER" %
                                    (surf_count, basename),
                                    affine_array[surf_count])
        if opts.outputply:
            colorbar = True
            if not os.path.exists("output_ply"):
                os.mkdir("output_ply")
            for contrast in range(num_contrasts):
                for surf_count in surface_range:
                    start = position_array[surf_count]
                    end = position_array[surf_count + 1]
                    basename = strip_basename(maskname[surf_count])
                    if masking_array[surf_count].shape[2] > 1:
                        img_data = np.zeros((masking_array[surf_count].shape))
                        combined_data = positive_data[start:end, contrast]
                        combined_data[combined_data <= 0] = negative_data[
                            start:end, contrast][combined_data <= 0] * -1
                        combined_data[np.abs(combined_data) < float(
                            opts.outputply[0])] = 0
                        img_data[masking_array[surf_count]] = combined_data
                        v, f, values = convert_voxel(
                            img_data,
                            affine=affine_array[surf_count],
                            absthreshold=float(opts.outputply[0]))
                        if not v == []:
                            out_color_array = paint_surface(
                                opts.outputply[0],
                                opts.outputply[1],
                                opts.outputply[2],
                                values,
                                save_colorbar=colorbar)
                            negvalues = values * -1
                            index = negvalues > float(opts.outputply[0])
                            out_color_array2 = paint_surface(
                                opts.outputply[0],
                                opts.outputply[1],
                                opts.outputply[3],
                                negvalues,
                                save_colorbar=colorbar)
                            out_color_array[index, :] = out_color_array2[
                                index, :]
                            save_ply(
                                v, f, "output_ply/%d_%s_pFWE_tcon%d.ply" %
                                (surf_count, basename, contrast + 1),
                                out_color_array)
                            colorbar = False
                        else:
                            print("No output for %d %s T-contrast %d" %
                                  (surf_count, basename, contrast + 1))
                    else:
                        img_data = np.zeros(
                            (masking_array[surf_count].shape[0]))
                        img_data[masking_array[surf_count][:, 0, 0] ==
                                 True] = positive_data[start:end, contrast]
                        out_color_array = paint_surface(opts.outputply[0],
                                                        opts.outputply[1],
                                                        opts.outputply[2],
                                                        img_data,
                                                        save_colorbar=colorbar)
                        img_data[masking_array[surf_count][:, 0, 0] ==
                                 True] = negative_data[start:end, contrast]
                        index = img_data > float(opts.outputply[0])
                        out_color_array2 = paint_surface(
                            opts.outputply[0],
                            opts.outputply[1],
                            opts.outputply[3],
                            img_data,
                            save_colorbar=colorbar)
                        out_color_array[index, :] = out_color_array2[index, :]
                        save_ply(
                            vertex_array[surf_count], face_array[surf_count],
                            "output_ply/%d_%s_pFWE_tcon%d.ply" %
                            (surf_count, basename, contrast + 1),
                            out_color_array)
                        colorbar = False

    elif opts.mediationmfwe:  # temporary solution -> maybe a general function instead of bulky code

        _, image_array, masking_array, maskname, affine_array, vertex_array, face_array, surfname, adjacency_array, tmi_history, columnids = read_tm_filetype(
            '%s' % opts.tmifile[0], verbose=False)

        # check file dimensions
        if not image_array[0].shape[1] % 2 == 0:
            print(
                'Print file format is not understood. Please make sure %s is statistics file.'
                % opts.tmifile[0])
            quit()

        # get surface coordinates in data array
        position_array = create_position_array(masking_array)

        # check that randomisation has been run
        if not os.path.exists("%s/output_%s/perm_maxTFCE_surf0_%s_zstat.csv" %
                              (os.getcwd(), opts.tmifile[0],
                               opts.mediationmfwe[0])):  # make this safer
            print(
                'Permutation folder not found. Please run --randomise first.')
            quit()

        #check permutation file lengths
        num_surf = len(masking_array)
        surface_range = list(range(num_surf))
        num_perm = lowest_length(1,
                                 surface_range,
                                 opts.tmifile[0],
                                 medtype=opts.mediationmfwe[0])

        if opts.setsurfacerange:
            surface_range = list(
                range(opts.setsurfacerange[0], opts.setsurfacerange[1] + 1))
        elif opts.setsurface:
            surface_range = opts.setsurface
        if np.array(surface_range).max() > len(masking_array):
            print(
                "Error: range does note fit the surfaces contained in the tmi file. %s contains the following surfaces"
                % opts.tmifile[0])
            for i in range(len(surfname)):
                print(("Surface %d : %s, %s" % (i, surfname[i], maskname[i])))
            quit()
        print("Reading %d contrast(s) from %d of %d surface(s)" %
              (1, len(surface_range), num_surf))
        print("Reading %s permutations with an accuracy of p=0.05+/-%.4f" %
              (num_perm, (2 * (np.sqrt(0.05 * 0.95 / num_perm)))))

        # calculate the P(FWER) images from all surfaces
        positive_data = apply_mfwer(image_array,
                                    1,
                                    surface_range,
                                    num_perm,
                                    num_surf,
                                    opts.tmifile[0],
                                    position_array, [1],
                                    weight='logmasksize',
                                    mediation=True,
                                    medtype=opts.mediationmfwe[0])
        if opts.outtype[0] == 'tmi':
            contrast_names = []

            contrast_names.append(("zstat_pFWER"))
            outdata = positive_data

            if opts.neglog:
                contrast_names.append(("zstat_negLog_pFWER"))
                outdata = np.column_stack(
                    (outdata, -np.log10(1 - positive_data)))

            write_tm_filetype("pFWER_%s" % (opts.tmifile[0]),
                              image_array=outdata,
                              masking_array=masking_array,
                              maskname=maskname,
                              affine_array=affine_array,
                              vertex_array=vertex_array,
                              face_array=face_array,
                              surfname=surfname,
                              checkname=False,
                              columnids=np.array(contrast_names),
                              tmi_history=tmi_history)

    else:
        ##################################
        ###### STATISTICAL ANALYSIS ######
        ##################################
        # read tmi file
        if opts.randomise:
            _, image_array, masking_array, _, _, _, _, _, adjacency_array, _, _ = read_tm_filetype(
                opts.tmifile[0])
            _ = None
        else:
            element, image_array, masking_array, maskname, affine_array, vertex_array, face_array, surfname, adjacency_array, tmi_history, _ = read_tm_filetype(
                opts.tmifile[0])
        # get surface coordinates in data array
        position_array = create_position_array(masking_array)

        if opts.setadjacencyobjs:
            if len(opts.setadjacencyobjs) == len(masking_array):
                adjacent_range = np.array(opts.setadjacencyobjs, dtype=np.int)
            else:
                print(
                    "Error: # of masking arrays (%d) must and list of matching adjacency (%d) must be equal."
                    % (len(masking_array), len(opts.setadjacencyobjs)))
                quit()
        else:
            adjacent_range = list(range(len(adjacency_array)))
        calcTFCE = []
        if opts.assigntfcesettings:
            if not len(opts.assigntfcesettings) == len(masking_array):
                print(
                    "Error: # of masking arrays (%d) must and list of matching tfce setting (%d) must be equal."
                    % (len(masking_array), len(opts.assigntfcesettings)))
                quit()
            if not len(opts.tfce) % 2 == 0:
                print("Error. The must be an even number of input for --tfce")
                quit()
            tfce_settings_mask = []
            for i in np.unique(opts.assigntfcesettings):
                tfce_settings_mask.append(
                    (np.array(opts.assigntfcesettings) == int(i)))
                pointer = int(i * 2)
                adjacency = merge_adjacency_array(
                    np.array(adjacent_range)[tfce_settings_mask[int(i)]],
                    np.array(adjacency_array)[tfce_settings_mask[int(i)]])
                calcTFCE.append((CreateAdjSet(float(opts.tfce[pointer]),
                                              float(opts.tfce[pointer + 1]),
                                              adjacency)))
                del adjacency
        else:
            adjacency = merge_adjacency_array(adjacent_range, adjacency_array)
            calcTFCE.append((CreateAdjSet(float(opts.tfce[0]),
                                          float(opts.tfce[1]), adjacency)))

        # make mega mask
        fullmask = create_full_mask(masking_array)

        if not opts.noweight:
            # correction for vertex density
            vdensity = []
            #np.ones_like(masking_array)
            for i in range(len(masking_array)):
                temp_vdensity = np.zeros(
                    (adjacency_array[adjacent_range[i]].shape[0]))
                for j in range(adjacency_array[adjacent_range[i]].shape[0]):
                    temp_vdensity[j] = len(
                        adjacency_array[adjacent_range[i]][j])
                if masking_array[i].shape[2] == 1:
                    temp_vdensity = temp_vdensity[masking_array[i][:, 0,
                                                                   0] == True]
                vdensity = np.hstack(
                    (vdensity,
                     np.array((1 - (temp_vdensity / temp_vdensity.max()) +
                               (temp_vdensity.mean() / temp_vdensity.max())),
                              dtype=np.float32)))
            del temp_vdensity
        else:
            vdensity = 1

        #load regressors
        if opts.input:
            for i, arg_pred in enumerate(opts.input):
                if i == 0:
                    pred_x = np.genfromtxt(arg_pred, delimiter=',')
                else:
                    pred_x = np.column_stack(
                        [pred_x,
                         np.genfromtxt(arg_pred, delimiter=',')])

            if opts.covariates:
                covars = np.genfromtxt(opts.covariates[0], delimiter=',')
                x_covars = np.column_stack([np.ones(len(covars)), covars])
            if opts.subset:
                masking_variable = np.isfinite(
                    np.genfromtxt(str(opts.subset[0]), delimiter=','))
                if opts.covariates:
                    merge_y = resid_covars(x_covars,
                                           image_array[0][:, masking_variable])
                else:
                    merge_y = image_array[0][:, masking_variable].T
                    print("Check dimensions")  # CHECK
                    print(merge_y.shape)
            else:
                if opts.covariates:
                    merge_y = resid_covars(x_covars, image_array[0])
                else:
                    merge_y = image_array[0].T
        if opts.inputmediation:
            medtype = opts.inputmediation[0]
            pred_x = np.genfromtxt(opts.inputmediation[1], delimiter=',')
            depend_y = np.genfromtxt(opts.inputmediation[2], delimiter=',')
            if opts.covariates:
                covars = np.genfromtxt(opts.covariates[0], delimiter=',')
                x_covars = np.column_stack([np.ones(len(covars)), covars])
                merge_y = resid_covars(x_covars, image_array[0])
            else:
                merge_y = image_array[0].T

        if opts.regressors:
            arg_predictor = opts.regressors[0]
            pred_x = np.genfromtxt(arg_predictor, delimiter=',')

            if opts.subset:
                masking_variable = np.isfinite(
                    np.genfromtxt(str(opts.subset[0]), delimiter=','))
                merge_y = image_array[0][:, masking_variable].T
            else:
                merge_y = image_array[0].T

        # cleanup
        image_array = None
        adjacency_array = None
        adjacency = None

        if opts.analysisname:
            outname = opts.analysisname[0]
        else:
            outname = opts.tmifile[0][:-4]

        # make output folder
        if not os.path.exists("output_%s" % (outname)):
            os.mkdir("output_%s" % (outname))
        os.chdir("output_%s" % (outname))
        if opts.randomise:
            randTime = int(time())
            mapped_y = merge_y.astype(np.float32,
                                      order="C")  # removed memory mapping
            merge_y = None
            if not outname.endswith('tmi'):
                outname += '.tmi'

            if opts.inputmediation:
                outname = 'med_stats_' + outname
            else:
                outname = 'stats_' + outname

            if not os.path.exists("output_%s" % (outname)):
                os.mkdir("output_%s" % (outname))
            os.chdir("output_%s" % (outname))
            for i in range(opts.randomise[0], (opts.randomise[1] + 1)):
                if opts.assigntfcesettings:
                    calc_mixed_tfce(opts.assigntfcesettings,
                                    mapped_y,
                                    masking_array,
                                    position_array,
                                    vdensity,
                                    pred_x,
                                    calcTFCE,
                                    perm_number=i,
                                    randomise=True)
                elif opts.inputmediation:
                    calculate_mediation_tfce(medtype,
                                             mapped_y,
                                             masking_array,
                                             pred_x,
                                             depend_y,
                                             calcTFCE[0],
                                             vdensity,
                                             position_array,
                                             fullmask,
                                             perm_number=i,
                                             randomise=True)
                else:
                    calculate_tfce(mapped_y,
                                   masking_array,
                                   pred_x,
                                   calcTFCE[0],
                                   vdensity,
                                   position_array,
                                   fullmask,
                                   perm_number=i,
                                   randomise=True)
            print(("Total time took %.1f seconds" % (time() - currentTime)))
            print(("Randomization took %.1f seconds" % (time() - randTime)))
        else:
            # Run TFCE
            if opts.assigntfcesettings:
                tvals, tfce_tvals, neg_tfce_tvals = calc_mixed_tfce(
                    opts.assigntfcesettings, merge_y, masking_array,
                    position_array, vdensity, pred_x, calcTFCE)
            elif opts.inputmediation:
                SobelZ, tfce_SobelZ = calculate_mediation_tfce(
                    medtype, merge_y, masking_array, pred_x, depend_y,
                    calcTFCE[0], vdensity, position_array, fullmask)
            else:
                tvals, tfce_tvals, neg_tfce_tvals = calculate_tfce(
                    merge_y, masking_array, pred_x, calcTFCE[0], vdensity,
                    position_array, fullmask)
            if opts.outtype[0] == 'tmi':
                if not outname.endswith('tmi'):
                    outname += '.tmi'
                if opts.inputmediation:
                    outname = 'med_stats_' + outname
                else:
                    outname = 'stats_' + outname

                if opts.inputmediation:
                    contrast_names = []
                    contrast_names.append(("SobelZ"))
                    contrast_names.append(("SobelZ_tfce"))
                    outdata = np.column_stack((SobelZ.T, tfce_SobelZ.T))
                else:
                    if tvals.ndim == 1:
                        num_contrasts = 1
                    else:
                        num_contrasts = tvals.shape[0]

                    contrast_names = []
                    for i in range(num_contrasts):
                        contrast_names.append(("tstat_con%d" % (i + 1)))
                    for j in range(num_contrasts):
                        contrast_names.append(("tstat_tfce_con%d" % (j + 1)))
                    for k in range(num_contrasts):
                        contrast_names.append(
                            ("negtstat_tfce_con%d" % (k + 1)))
                    outdata = np.column_stack((tvals.T, tfce_tvals.T))
                    outdata = np.column_stack((outdata, neg_tfce_tvals.T))

                # write tstat
                write_tm_filetype(outname,
                                  image_array=outdata,
                                  masking_array=masking_array,
                                  maskname=maskname,
                                  affine_array=affine_array,
                                  vertex_array=vertex_array,
                                  face_array=face_array,
                                  surfname=surfname,
                                  checkname=False,
                                  columnids=np.array(contrast_names),
                                  tmi_history=[])

            else:
                print("not implemented yet")
def run(opts):
	try:
		sopts = np.load("tmi_temp/opts.npy").tolist()
	except:
		print("Error: tmi_temp was not found. Run mmr-lr first, or change directory to where tmi_temp is located.") # this error should never happen.

	tfce_settings = []
	masking_array = np.load("tmi_temp/masking_array.npy")
	for surf_num in range(len(masking_array)):
		if sopts.assigntfcesettings:
			pointer = int(sopts.assigntfcesettings[surf_num] * 2)
			tfce_settings.append(([sopts.tfce[pointer],sopts.tfce[pointer+1]]))

	if opts.outputstats:

		# load npy objects for building the tmi file
		maskname = np.load("tmi_temp/maskname.npy")
		affine_array = np.load("tmi_temp/affine_array.npy")
		vertex_array = np.load("tmi_temp/vertex_array.npy")
		face_array = np.load("tmi_temp/face_array.npy")
		surfname = np.load("tmi_temp/surfname.npy")

		for surf_num in range(len(masking_array)):
			print("Calculating stats for:\t %s" % maskname[surf_num])
			adjacency = np.load("tmi_temp/%d_adjacency_temp.npy" % surf_num)
			mask = np.load("tmi_temp/%d_mask_temp.npy" % surf_num)
			data = np.load("tmi_temp/%d_data_temp.npy" % surf_num)
			vdensity = np.load("tmi_temp/%d_vdensity_temp.npy" % surf_num)
			if not sopts.assigntfcesettings:
				calcTFCE = CreateAdjSet(float(sopts.tfce[0]), float(sopts.tfce[1]), adjacency)

			if sopts.input:
				for i, arg_pred in enumerate(sopts.input):
					if i == 0:
						pred_x = np.genfromtxt(arg_pred, delimiter=',')
					else:
						pred_x = np.column_stack([pred_x, np.genfromtxt(arg_pred, delimiter=',')])
				if sopts.assigntfcesettings:
					calcTFCE = CreateAdjSet(float(tfce_settings[surf_num][0]), float(tfce_settings[surf_num][1]), adjacency)
				temp_tvals, temp_tfce_tvals, temp_neg_tfce_tvals = low_ram_calculate_tfce(data, mask, pred_x, calcTFCE, vdensity, set_surf_count = surf_num, randomise = False, no_intercept = True)

				if surf_num == 0:
					tvals = temp_tvals
					tfce_tvals = temp_tfce_tvals
					neg_tfce_tvals = temp_neg_tfce_tvals
					if pred_x.ndim == 1: # get number of contrasts
						num_contrasts = 1
					else:
						num_contrasts = pred_x.shape[1]
				else:
					tvals = np.concatenate((tvals, temp_tvals), 1)
					tfce_tvals = np.concatenate((tfce_tvals, temp_tfce_tvals), 1)
					neg_tfce_tvals = np.concatenate((neg_tfce_tvals, temp_neg_tfce_tvals), 1)

			if sopts.inputmediation:
				medtype = sopts.inputmediation[0]
				pred_x =  np.genfromtxt(sopts.inputmediation[1], delimiter=',')
				depend_y =  np.genfromtxt(sopts.inputmediation[2], delimiter=',')
				if sopts.assigntfcesettings:
					calcTFCE = CreateAdjSet(float(tfce_settings[surf_num][0]), float(tfce_settings[surf_num][1]), adjacency)
				temp_zvals, temp_tfce_zvals = low_ram_calculate_mediation_tfce(medtype, data, mask, pred_x, depend_y, calcTFCE, vdensity, set_surf_count = surf_num, randomise = False, no_intercept = True)

				if surf_num == 0:
					zvals = temp_zvals
					tfce_zvals = temp_tfce_zvals
					if pred_x.ndim == 1: # get number of contrasts
						num_contrasts = 1
					else:
						num_contrasts = pred_x.shape[1]
				else:
					zvals = np.concatenate((zvals, temp_zvals))
					tfce_zvals = np.concatenate((tfce_zvals, temp_tfce_zvals))

		if sopts.input:
			data = np.column_stack((tvals.T, tfce_tvals.T))
			data = np.column_stack((data, neg_tfce_tvals.T))
			contrast_names = []
			for i in range(num_contrasts):
				contrast_names.append(("tstat_con%d" % (i+1)))
			for j in range(num_contrasts):
				contrast_names.append(("tstat_tfce_con%d" % (j+1)))
			for k in range(num_contrasts):
				contrast_names.append(("negtstat_tfce_con%d" % (k+1)))
		if sopts.inputmediation:
			data = np.column_stack((zvals.T, tfce_zvals.T))
			contrast_names = []
			contrast_names.append(("SobelZ"))
			contrast_names.append(("SobelZ_tfce"))

		outname = os.path.basename(str(opts.path[0]))[7:]

		if not outname.endswith('tmi'):
			outname += '.tmi'

		write_tm_filetype("%s/%s" % (os.path.dirname(str(opts.path[0])), outname), 
			image_array = data, 
			masking_array = masking_array, 
			maskname = maskname, 
			affine_array = affine_array, 
			vertex_array = vertex_array, 
			face_array = face_array, 
			surfname = surfname, 
			checkname = True, 
			columnids = np.array(contrast_names),
			tmi_history=[])

	else:
		currentTime=int(time())
		surf_num = int(opts.surfacenumber[0])
		p_range = np.array(opts.permutationrange)
		adjacency = np.load("tmi_temp/%d_adjacency_temp.npy" % surf_num)
		mask = np.load("tmi_temp/%d_mask_temp.npy" % surf_num)
		data = np.load("tmi_temp/%d_data_temp.npy" % surf_num)
		vdensity = np.load("tmi_temp/%d_vdensity_temp.npy" % surf_num)


		if sopts.assigntfcesettings:
			calcTFCE = CreateAdjSet(float(tfce_settings[surf_num][0]), float(tfce_settings[surf_num][1]), adjacency)
		else:
			calcTFCE = CreateAdjSet(float(sopts.tfce[0]), float(sopts.tfce[1]), adjacency)
		if sopts.input:
			for i, arg_pred in enumerate(sopts.input):
				if i == 0:
					pred_x = np.genfromtxt(arg_pred, delimiter=',')
				else:
					pred_x = np.column_stack([pred_x, np.genfromtxt(arg_pred, delimiter=',')])
			for perm_number in range(p_range[0],int(p_range[1]+1)):
				low_ram_calculate_tfce(data, mask, pred_x, calcTFCE, vdensity,
					set_surf_count = surf_num,
					perm_number = perm_number,
					randomise = True,
					no_intercept = True,
					output_dir = str(opts.path[0]),
					perm_seed = int(opts.seed[0]))

		if sopts.inputmediation:
			medtype = sopts.inputmediation[0]
			pred_x =  np.genfromtxt(sopts.inputmediation[1], delimiter=',')
			depend_y =  np.genfromtxt(sopts.inputmediation[2], delimiter=',')
			for perm_number in range(p_range[0],int(p_range[1]+1)):
				low_ram_calculate_mediation_tfce(medtype, data, mask, pred_x, depend_y, calcTFCE, vdensity,
					set_surf_count = surf_num,
					perm_number = perm_number,
					randomise = True,
					no_intercept = True,
					output_dir = str(opts.path[0]),
					perm_seed = int(opts.seed[0]))
		print("Mask %d, Iteration %d -> %d took %i seconds." % (surf_num, p_range[0], p_range[1], (int(time()) - currentTime)))
Exemple #5
0
def run(opts):
    currentTime = int(strftime("%Y%m%d%H%M%S", gmtime()))
    # read tmi
    element, image_array, masking_array, maskname_array, affine_array, vertex_array, face_array, surfname, adjacency_array, tmi_history, subjectids = read_tm_filetype(
        opts.inputtmi[0])

    num_masks = 0
    num_affines = 0
    num_surfaces = 0
    num_adjac = 0
    append_history = True

    if opts.outputnewtmi:
        outname = opts.outputnewtmi[0]
    else:
        outname = opts.inputtmi[0]

    if opts.history:
        print "--- History ---"
        for i in range(len(tmi_history)):
            print "Time-point %d" % i
            line = tmi_history[i].split(' ')
            print("Date: %s-%s-%s %s:%s:%s" %
                  (line[2][6:8], line[2][4:6], line[2][0:4], line[2][8:10],
                   line[2][10:12], line[2][12:14]))
            if line[1] == 'mode_add':
                print "Elements added"
                num_masks += int(line[4])
                num_affines += int(line[5])
                num_surfaces += int(line[6])
                num_adjac += int(line[7])
            elif line[1] == 'mode_sub':
                print "Elements removed"
                num_masks -= int(line[4])
                num_affines -= int(line[5])
                num_surfaces -= int(line[6])
                num_adjac -= int(line[7])
            else:
                print "Error: mode is not understood"
            print "Number of masks: %s" % line[4]
            print "Number of affines: %s" % line[5]
            print "Number of surfaces: %s" % line[6]
            print "Number of adjacency sets: %s\n" % line[7]

        print "--- Total ---"
        print "Number of masks: %d ([0 -> %d])" % (num_masks, num_masks - 1)
        print "Number of affines: %d ([0 -> %d])" % (num_affines,
                                                     num_affines - 1)
        print "Number of surfaces: %d ([0 -> %d])" % (num_surfaces,
                                                      num_affines - 1)
        print "Number of adjacency sets: %d ([0 -> %d])\n" % (num_adjac,
                                                              num_affines - 1)
        quit()
    if opts.revert:
        for i in range(int(opts.revert[0]) + 1):
            #		for i in range(int(6)):
            line = tmi_history[i].split(' ')
            if line[1] == 'mode_add':
                num_masks += int(line[4])
                num_affines += int(line[5])
                num_surfaces += int(line[6])
                num_adjac += int(line[7])
            elif line[1] == 'mode_sub':
                num_masks -= int(line[4])
                num_affines -= int(line[5])
                num_surfaces -= int(line[6])
                num_adjac -= int(line[7])
        size_data = 0
        for i in range(num_masks):
            size_data += len(masking_array[i][masking_array[i] == True])
        image_array[0] = image_array[0][:size_data, :]
        masking_array = masking_array[:num_masks]
        maskname_array = maskname_array[:num_masks]
        affine_array = affine_array[:num_affines]
        vertex_array = vertex_array[:num_surfaces]
        face_array = face_array[:num_surfaces]
        surfname = surfname[:num_surfaces]
        adjacency_array = adjacency_array[:num_adjac]

        # edit history

        orig_num_masks = 0
        orig_num_affines = 0
        orig_num_surfaces = 0
        orig_num_adjac = 0
        for i in range(len(tmi_history)):
            line = tmi_history[i].split(' ')
            if line[1] == 'mode_add':
                orig_num_masks += int(line[4])
                orig_num_affines += int(line[5])
                orig_num_surfaces += int(line[6])
                orig_num_adjac += int(line[7])
            elif line[1] == 'mode_sub':
                orig_num_masks -= int(line[4])
                orig_num_affines -= int(line[5])
                orig_num_surfaces -= int(line[6])
                orig_num_adjac -= int(line[7])
        tmi_history.append("history mode_sub %d %d %d %d %d %d" %
                           (currentTime, 1, orig_num_masks - num_masks,
                            orig_num_affines - num_affines, orig_num_surfaces -
                            num_surfaces, orig_num_adjac - num_adjac))
        append_history = False
    # Write tmi file
    if not image_array == []:
        write_tm_filetype(outname,
                          output_binary=opts.outputtype == 'binary',
                          image_array=np.vstack(image_array),
                          masking_array=masking_array,
                          maskname=maskname,
                          affine_array=affine_array,
                          vertex_array=vertex_array,
                          face_array=face_array,
                          surfname=surfname,
                          adjacency_array=adjacency_array,
                          checkname=False,
                          tmi_history=tmi_history,
                          append_history=append_history)
    else:
        write_tm_filetype(outname,
                          output_binary=opts.outputtype == 'binary',
                          masking_array=masking_array,
                          maskname=maskname,
                          affine_array=affine_array,
                          vertex_array=vertex_array,
                          face_array=face_array,
                          surfname=surfname,
                          adjacency_array=adjacency_array,
                          checkname=False,
                          tmi_history=tmi_history,
                          append_history=append_history)
Exemple #6
0
def run(opts):

    currentTime = int(time())
    if opts.multisurfacefwecorrection:
        _, image_array, masking_array, maskname, affine_array, vertex_array, face_array, surfname, adjacency_array, tmi_history, subjectids = read_tm_filetype(
            '%s' % opts.tmifile[0], verbose=False)

        # check file dimensions
        if not image_array[0].shape[1] % 3 == 0:
            print 'Print file format is not understood. Please make sure %s is statistics file.' % opts.tmifile[
                0]
            quit()
        else:
            num_contrasts = int(image_array[0].shape[1] / 3)

        # get surface coordinates in data array
        pointer = 0
        position_array = [0]
        for i in range(len(masking_array)):
            pointer += len(masking_array[i][masking_array[i] == True])
            position_array.append(pointer)
        del pointer

        if num_contrasts == 1:
            # get lists for positive and negative contrasts
            pos_range = [1]
            neg_range = [2]
        else:
            # get lists for positive and negative contrasts
            pos_range = range(num_contrasts, num_contrasts + num_contrasts)
            neg_range = range(num_contrasts * 2,
                              num_contrasts * 2 + num_contrasts)

        # check that randomisation has been run
        if not os.path.exists(
                "%s/output_%s/perm_maxTFCE_surf0_tcon1.csv" %
            (os.getcwd(), opts.tmifile[0])):  # make this safer
            print 'Permutation folder not found. Please run --randomise first.'
            quit()

        #check permutation file lengths
        num_surf = len(masking_array)
        surface_range = range(num_surf)
        num_perm = lowest_length(num_contrasts, surface_range, opts.tmifile[0])

        if opts.setsurfacerange:
            surface_range = range(opts.setsurfacerange[0],
                                  opts.setsurfacerange[1] + 1)
        elif opts.setsurface:
            surface_range = opts.setsurface
        if np.array(surface_range).max() > len(masking_array):
            print "Error: range does note fit the surfaces contained in the tmi file. %s contains the following surfaces" % opts.tmifile[
                0]
            for i in range(len(surfname)):
                print("Surface %d : %s, %s" % (i, surfname[i], maskname[i]))
            quit()
        print "Reading %d contrast(s) from %d of %d surface(s)" % (
            (num_contrasts), len(surface_range), num_surf)
        print "Reading %s permutations with an accuracy of p=0.05+/-%.4f" % (
            num_perm, (2 * (np.sqrt(0.05 * 0.95 / num_perm))))

        # calculate the P(FWER) images from all surfaces
        positive_data, negative_data = apply_mfwer(image_array,
                                                   num_contrasts,
                                                   surface_range,
                                                   num_perm,
                                                   num_surf,
                                                   opts.tmifile[0],
                                                   position_array,
                                                   pos_range,
                                                   neg_range,
                                                   weight='logmasksize')

        # write out files
        if opts.concatestats:
            write_tm_filetype(opts.tmifile[0],
                              image_array=positive_data,
                              masking_array=masking_array,
                              maskname=maskname,
                              affine_array=affine_array,
                              vertex_array=vertex_array,
                              face_array=face_array,
                              surfname=surfname,
                              adjacency_array=adjacency_array,
                              checkname=False,
                              tmi_history=tmi_history)
            _, image_array, masking_array, maskname, affine_array, vertex_array, face_array, surfname, adjacency_array, tmi_history, subjectids = read_tm_filetype(
                opts.tmifile[0], verbose=False)
            write_tm_filetype(opts.tmifile[0],
                              image_array=np.column_stack(
                                  (image_array[0], negative_data)),
                              masking_array=masking_array,
                              maskname=maskname,
                              affine_array=affine_array,
                              vertex_array=vertex_array,
                              face_array=face_array,
                              surfname=surfname,
                              adjacency_array=adjacency_array,
                              checkname=False,
                              tmi_history=tmi_history)
        else:
            for i in range(len(opts.outtype)):
                if opts.outtype[i] == 'tmi':
                    write_tm_filetype("tstats_pFWER_%s" % opts.tmifile[0],
                                      image_array=positive_data,
                                      masking_array=masking_array,
                                      maskname=maskname,
                                      affine_array=affine_array,
                                      vertex_array=vertex_array,
                                      face_array=face_array,
                                      surfname=surfname,
                                      checkname=False,
                                      tmi_history=tmi_history)
                    write_tm_filetype("negtstats_pFWER_%s" % opts.tmifile[0],
                                      image_array=negative_data,
                                      masking_array=masking_array,
                                      maskname=maskname,
                                      affine_array=affine_array,
                                      vertex_array=vertex_array,
                                      face_array=face_array,
                                      surfname=surfname,
                                      checkname=False,
                                      tmi_history=tmi_history)
                    if opts.neglog:
                        write_tm_filetype(
                            "tstats_negLog_pFWER_%s" % opts.tmifile[0],
                            image_array=-np.log10(1 - positive_data),
                            masking_array=masking_array,
                            maskname=maskname,
                            affine_array=affine_array,
                            vertex_array=vertex_array,
                            face_array=face_array,
                            surfname=surfname,
                            checkname=False,
                            tmi_history=tmi_history)
                        write_tm_filetype(
                            "negtstats_negLog_pFWER_%s" % opts.tmifile[0],
                            image_array=-np.log10(1 - negative_data),
                            masking_array=masking_array,
                            maskname=maskname,
                            affine_array=affine_array,
                            vertex_array=vertex_array,
                            face_array=face_array,
                            surfname=surfname,
                            checkname=False,
                            tmi_history=tmi_history)
                else:
                    if opts.outtype[i] == 'mgh':
                        savefunc = savemgh_v2
                    if opts.outtype[i] == 'nii.gz':
                        savefunc = savenifti_v2
                    if opts.outtype[i] == 'auto':
                        savefunc = saveauto
                    for surf_count in surface_range:
                        start = position_array[surf_count]
                        end = position_array[surf_count + 1]
                        basename = strip_basename(maskname[surf_count])
                        if not os.path.exists("output_stats"):
                            os.mkdir("output_stats")
                        out_image = positive_data[start:end]
                        temp_image = negative_data[start:end]
                        for contrast in range(num_contrasts):
                            out_image[temp_image[:, contrast] != 0,
                                      contrast] = temp_image[
                                          temp_image[:, contrast] != 0,
                                          contrast] * -1
                        if affine_array == []:
                            savefunc(
                                out_image, masking_array[surf_count],
                                "output_stats/%d_%s_pFWER" %
                                (surf_count, basename))
                        else:
                            savefunc(
                                out_image, masking_array[surf_count],
                                "output_stats/%d_%s_pFWER" %
                                (surf_count, basename),
                                affine_array[surf_count])
                        if opts.neglog:
                            out_image = -np.log10(1 - positive_data[start:end,
                                                                    contrast])
                            temp_image = np.log10(1 - negative_data[start:end,
                                                                    contrast])
                            for contrast in range(num_contrasts):
                                out_image[temp_image[:, contrast] != 0,
                                          contrast] = temp_image[
                                              temp_image[:, contrast] != 0,
                                              contrast]
                            if affine_array == []:
                                savefunc(
                                    out_image, masking_array[surf_count],
                                    "output_stats/%d_%s_negLog_pFWER" %
                                    (surf_count, basename))
                            else:
                                savefunc(
                                    out_image, masking_array[surf_count],
                                    "output_stats/%d_%s_negLog_pFWER" %
                                    (surf_count, basename),
                                    affine_array[surf_count])
        if opts.outputply:
            if not os.path.exists("output_ply"):
                os.mkdir("output_ply")
            for contrast in range(num_contrasts):
                for surf_count in surface_range:
                    start = position_array[surf_count]
                    end = position_array[surf_count + 1]
                    basename = strip_basename(maskname[surf_count])
                    if masking_array[surf_count].shape[2] > 1:
                        img_data = np.zeros((masking_array[surf_count].shape))
                        combined_data = positive_data[start:end, contrast]
                        combined_data[combined_data <= 0] = negative_data[
                            start:end, contrast][combined_data <= 0] * -1
                        combined_data[np.abs(combined_data) < float(
                            opts.outputply[0])] = 0
                        img_data[masking_array[surf_count]] = combined_data
                        v, f, values = convert_voxel(
                            img_data,
                            affine=affine_array[surf_count],
                            absthreshold=float(opts.outputply[0]))
                        out_color_array = paint_surface(
                            opts.outputply[0], opts.outputply[1],
                            opts.outputply[2], values)
                        negvalues = values * -1
                        index = negvalues > float(opts.outputply[0])
                        out_color_array2 = paint_surface(
                            opts.outputply[0], opts.outputply[1],
                            opts.outputply[3], negvalues)
                        out_color_array[index, :] = out_color_array2[index, :]
                        save_ply(
                            v, f, "output_ply/%d_%s_pFWE_tcon%d.ply" %
                            (surf_count, basename, contrast + 1),
                            out_color_array)
                    else:
                        img_data = np.zeros(
                            (masking_array[surf_count].shape[0]))
                        img_data[masking_array[surf_count][:, 0, 0] ==
                                 True] = positive_data[start:end, contrast]
                        out_color_array = paint_surface(
                            opts.outputply[0], opts.outputply[1],
                            opts.outputply[2], img_data)
                        img_data[masking_array[surf_count][:, 0, 0] ==
                                 True] = negative_data[start:end, contrast]
                        index = img_data > float(opts.outputply[0])
                        out_color_array2 = paint_surface(
                            opts.outputply[0], opts.outputply[1],
                            opts.outputply[3], img_data)
                        out_color_array[index, :] = out_color_array2[index, :]
                        save_ply(
                            vertex_array[surf_count], face_array[surf_count],
                            "output_ply/%d_%s_pFWE_tcon%d.ply" %
                            (surf_count, basename, contrast + 1),
                            out_color_array)
    else:
        # read tmi file
        if opts.randomise:
            _, image_array, masking_array, _, _, _, _, _, adjacency_array, _, _ = read_tm_filetype(
                opts.tmifile[0])
            _ = None
        else:
            element, image_array, masking_array, maskname, affine_array, vertex_array, face_array, surfname, adjacency_array, tmi_history, _ = read_tm_filetype(
                opts.tmifile[0])
        # get surface coordinates in data array
        pointer = 0
        position_array = [0]
        for i in range(len(masking_array)):
            pointer += len(masking_array[i][masking_array[i] == True])
            position_array.append(pointer)
        del pointer

        if opts.setadjacencyobjs:
            if len(opts.setadjacencyobjs) == len(masking_array):
                adjacent_range = opts.setadjacencyobjs
            else:
                print "Error: # of masking arrays %d must and list of matching adjacency %d must be equal." % (
                    len(masking_array), len(opts.setadjacencyobjs))
                quit()
        else:
            adjacent_range = range(len(adjacency_array))
        calcTFCE = []
        if opts.setfcesettings:
            if len(opts.setfcesettings) == len(masking_array):
                print "Error: # of masking arrays %d must and list of matching tfce setting %d must be equal." % (
                    len(masking_array), len(opts.setfcesettings))
                quit()
            if len(opts.tfce) % 2 != 0:
                print "Error. The must be an even number of input for --tfce"
                quit()
            tfce_settings_mask = []
            for i in range(len(opts.tfce) / 2):
                tfce_settings_mask.append((opts.setfcesettings == int(i)))
                pointer = int(i * 2)
                adjacency = merge_adjacency_array(
                    adjacent_range[tfce_settings_mask[i]],
                    adjacency_array[tfce_settings_mask[i]])
                calcTFCE.append((CreateAdjSet(float(opts.tfce[pointer]),
                                              float(opts.tfce[pointer + 1]),
                                              adjacency)))
                del adjacency
        else:
            adjacency = merge_adjacency_array(adjacent_range, adjacency_array)
            calcTFCE.append((CreateAdjSet(float(opts.tfce[0]),
                                          float(opts.tfce[1]), adjacency)))

        # make mega mask
        fullmask = []
        for i in range(len(masking_array)):
            if masking_array[i].shape[
                    2] == 1:  # check if vertex or voxel image
                fullmask = np.hstack((fullmask, masking_array[i][:, 0, 0]))
            else:
                fullmask = np.hstack(
                    (fullmask, masking_array[i][masking_array[i] == True]))

        if not opts.noweight:
            # correction for vertex density
            vdensity = []
            #np.ones_like(masking_array)
            for i in range(len(masking_array)):
                temp_vdensity = np.zeros(
                    (adjacency_array[adjacent_range[i]].shape[0]))
                for j in xrange(adjacency_array[adjacent_range[i]].shape[0]):
                    temp_vdensity[j] = len(
                        adjacency_array[adjacent_range[i]][j])
                if masking_array[i].shape[2] == 1:
                    temp_vdensity = temp_vdensity[masking_array[i][:, 0,
                                                                   0] == True]
                vdensity = np.hstack(
                    (vdensity,
                     np.array((1 - (temp_vdensity / temp_vdensity.max()) +
                               (temp_vdensity.mean() / temp_vdensity.max())),
                              dtype=np.float32)))
            del temp_vdensity
        else:
            vdensity = 1

        #load regressors
        if opts.input:
            arg_predictor = opts.input[0]
            arg_covars = opts.input[1]
            pred_x = np.genfromtxt(arg_predictor, delimiter=',')
            covars = np.genfromtxt(arg_covars, delimiter=',')
            x_covars = np.column_stack([np.ones(len(covars)), covars])
            merge_y = resid_covars(x_covars, image_array[0])
        if opts.regressors:
            arg_predictor = opts.regressors[0]
            pred_x = np.genfromtxt(arg_predictor, delimiter=',')
            merge_y = image_array[0].T

        # cleanup
        image_array = None
        adjacency_array = None
        adjacency = None

        if opts.analysisname:
            outname = opts.analysisname[0]
        else:
            outname = opts.tmifile[0][:-4]

        # make output folder
        if not os.path.exists("output_%s" % (outname)):
            os.mkdir("output_%s" % (outname))
        os.chdir("output_%s" % (outname))
        if opts.randomise:
            randTime = int(time())
            mapped_y = merge_y.astype(np.float32,
                                      order="C")  # removed memory mapping
            merge_y = None
            if not outname.endswith('tmi'):
                outname += '.tmi'
            outname = 'stats_' + outname
            if not os.path.exists("output_%s" % (outname)):
                os.mkdir("output_%s" % (outname))
            os.chdir("output_%s" % (outname))
            for i in range(opts.randomise[0], (opts.randomise[1] + 1)):
                calculate_tfce(mapped_y,
                               masking_array,
                               pred_x,
                               calcTFCE[0],
                               vdensity,
                               position_array,
                               fullmask,
                               perm_number=i,
                               randomise=True)
            print("Total time took %.1f seconds" % (time() - currentTime))
            print("Randomization took %.1f seconds" % (time() - randTime))
        else:
            # Run TFCE
            tvals, tfce_tvals, neg_tfce_tvals = calculate_tfce(
                merge_y, masking_array, pred_x, calcTFCE[0], vdensity,
                position_array, fullmask)
            if opts.outtype[0] == 'tmi':
                if not outname.endswith('tmi'):
                    outname += '.tmi'
                outname = 'stats_' + outname
                # write tstat
                write_tm_filetype(outname,
                                  image_array=tvals.T,
                                  masking_array=masking_array,
                                  maskname=maskname,
                                  affine_array=affine_array,
                                  vertex_array=vertex_array,
                                  face_array=face_array,
                                  surfname=surfname,
                                  checkname=False,
                                  tmi_history=[])
                # read the tmi back in.
                _, image_array, masking_array, maskname, affine_array, vertex_array, face_array, surfname, _, tmi_history, subjectids = read_tm_filetype(
                    outname, verbose=False)
                write_tm_filetype(outname,
                                  image_array=np.column_stack(
                                      (image_array[0], tfce_tvals.T)),
                                  masking_array=masking_array,
                                  maskname=maskname,
                                  affine_array=affine_array,
                                  vertex_array=vertex_array,
                                  face_array=face_array,
                                  surfname=surfname,
                                  checkname=False,
                                  tmi_history=tmi_history)
                _, image_array, masking_array, maskname, affine_array, vertex_array, face_array, surfname, adjacency_array, tmi_history, subjectids = read_tm_filetype(
                    outname, verbose=False)
                write_tm_filetype(outname,
                                  image_array=np.column_stack(
                                      (image_array[0], neg_tfce_tvals.T)),
                                  masking_array=masking_array,
                                  maskname=maskname,
                                  affine_array=affine_array,
                                  vertex_array=vertex_array,
                                  face_array=face_array,
                                  surfname=surfname,
                                  checkname=False,
                                  tmi_history=tmi_history)
            else:
                print "not implemented yet"