def check_finished(ind, subj, sess, protocol, cycle):
    log.info('cycle:{}'.format(cycle))
    printer = []
    sleep(90)
    out = check_output(["ps", "-aux"])
    # log.info(out)
    fin = 0
    for i in ind:
        printer.append(i.poll())
        if i.poll() == None:
            fin = fin + 1
    log.info(printer)
    log_gm_job_status(
        "in progress; on second set of registrations: {0}/{1} finished".format(
            len(ind) - fin, len(ind)), subj, sess, protocol)
    log.info('{}/{} processes finished'.format(len(ind) - fin, len(ind)))
    if fin > 0:
        return check_finished(ind, subj, sess, protocol, cycle + 1)
    return True
Exemplo n.º 2
0
 def check_finished(self, ind, cycle):
     # import pdb
     # pdb.set_trace()
     log.info('cycle:{}'.format(cycle))
     printer = []
     sleep(90)
     out = check_output(["ps", "-aux"])
     log.info(out)
     fin = 0
     # import pdb
     # pdb.set_trace()
     count = 0
     for i in ind:
         out = []
         if i.stdout:
             for n in np.arange(20):
                 if n % 2 == 0:
                     sleep(1)
                 out.append(i.stdout.readline())
         # pdb.set_trace()
             f = open('/flywheel/v0/output/{}_error.txt'.format(count), 'w')
             # print(out)
             for line in out:
                 # print(line)
                 f.write(line.decode('utf-8'))
             f.close()
         printer.append(i.poll())
         if i.poll() == None:
             fin = fin + 1
         count += 1
     log.info(printer)
     log_gm_job_status(
         "in progress; first cycle {}/{} finished".format(
             len(ind) - fin, len(ind)), self.subj, self.sess, self.protocol)
     log.info('{}/{} processes finished'.format(len(ind) - fin, len(ind)))
     # pdb.set_trace()
     if fin > 0:
         return self.check_finished(ind, cycle + 1)
     return True
Exemplo n.º 3
0
def run_this(static,
             outputs_path,
             pth,
             pth2,
             cycle_size,
             subj,
             sess,
             protocol,
             prefix=0):
    file_handl = open(outputs_path + 'prints.txt', 'w')
    errors = []
    pass_on = []
    if not (prefix):
        if 'retest' in static:
            mse_static = c.get_mse(static) + 'PSIR_retest' + scanner(static)
        else:
            mse_static = c.get_mse(static) + 'PSIR_' + scanner(static)
    else:
        mse_static = prefix
    try:
        os.remove(
            os.path.join(outputs_path,
                         'registrations1/warped/synslice_avggmsegs.nii.gz'))
    except:
        pass
    output_path = os.path.join(outputs_path, 'registrations1/')
    dim = 2
    static_path = static
    log.info("about to start first round of registrations!")
    GRP.SimpleRegister(pth,
                       output_path,
                       static_path,
                       file_handl,
                       cycle_size,
                       subj,
                       sess,
                       protocol,
                       pth2=pth2).Syn(gilroy=True)
    file_handl.write(str(static) + '\n')

    if True:
        log_gm_job_status("quality assurance check", subj, sess, protocol)
        #gray matter transformed images#
        template_grays = glob(os.path.join(output_path, 'warped/*ms*'))
        #cord transforms#
        templates = glob(os.path.join(output_path, 'warped1/*ms*'))

        fgs, distributions, a, adat, adat_raw = create_prob_seg_iteration3(
            template_grays, templates, static, file_handl)
        file_handl.close()
        avgim(os.path.join(output_path, 'warped/'))

        aff = nb.load(static)

        adat_raw = nb.load(static).get_data()
        adat_z = z_score(adat)
        #print(adat.dtype)
        #mask=nb.load(glob('/data/henry4/jjuwono/data/henry4/jjuwono/'+prefix+'/'+mse_static+'/warped/syn*')[0]).get_data()

        #mask=np.where(mask>.6,mask,0)
        #bar=np.mean(adat_z[np.where(mask>0)])
        sh = adat.shape
        slope = np.zeros(sh)
        intercept = np.zeros(sh)
        confidences = np.zeros(sh)
        confidences1 = np.zeros(sh)
        confidences2 = np.zeros(sh)
        t_map = np.zeros(sh)
        mean_templates = np.zeros(sh)
        new_image = np.zeros(sh)
        new_image_logi = np.zeros(sh)
        original_line_fit = np.zeros(sh)
        color_im = np.zeros(sh)
        print('length fg:{}'.format(len(fgs)))
        print('length dist:{}'.format(len(distributions)))
        distributions = np.asarray(distributions)
        fgs = np.asarray(fgs)
        for i in range(sh[0]):
            for j in range(sh[1]):

                if adat_raw[i, j] == 0:
                    continue
        ##insert A block here for polynomial degree fitting ###
        ##average method##
                a = np.array(distributions[:, i, j])[np.newaxis]
                if np.sum(distributions[:, i, j]) == 0:
                    params = [0, np.mean(fgs[:i, j])]
                else:
                    params = np.polyfit(distributions[:, i, j], fgs[:, i, j],
                                        1)
                original_line_fit[i,
                                  j] = (adat_z[i, j] * params[0]) + params[1]
                #if len(np.where(fgs[:,i,j]==0)[0])<40 or len(np.where(fgs[:,i,j]==1)[0])<40:

                #    assign=(adat_z[i,j]*params[0])+params[1]
                #else:
                #print('logi')
                #    group0=np.mean(distributions[:,i,j][np.where(fgs[:,i,j]==0)])
                #    group1=np.mean(distributions[:,i,j][np.where(fgs[:,i,j]==1)])
                #    grouped=np.where(fgs[:,i,j]!=0,distributions[:,i,j],group0)
                #    grouped=np.where(fgs[:,i,j]!=1,grouped,group1)
                #    params=np.polyfit(grouped,fgs[:,i,j],1)
                #    logi_slope=params[0]
                #    logi_inter=params[1]
                #    assign=(adat_z[i,j]*params[0])+params[1]
                #    if 0<=assign<=1:
                #        assign=assign
                #    elif assign<0:
                #        assign=0
                #    else:
                #        assign=1
                #if params[0]!=0:
                #plt.plot(grouped,fgs[:,i,j],'o',label=str((i,j)))
                #plt.plot([group0,group1],[group0*params[0]-params[1],group1*params[0]-params[1]],'r')
                #plt.legend()
                #plt.show()
                #input([group0,group1])
                #new_image_logi[i,j]=assign

                ##reverse method##
                #if len(np.where(fgs[:,i,j]==0)[0])<25 or len(np.where(fgs[:,i,j]==1)[0])<25:
                #    params=np.polyfit(distributions[:,i,j],fgs[:,i,j],1)
                #    new_image[i,j]=(adat_z[i,j]*params[0])+params[1]
                #    if len(np.where(fgs[:,i,j]==0)[0])<60:
                #        color_im[i,j]=1
                #else:
                #plt.plot(distributions[:,i,j],fgs[:,i,j],'o',label=str((i,j)))
                #plt.legend()
                #plt.show()
                #input()

                #    params,covs=np.polyfit(fgs[:,i,j],distributions[:,i,j],1,cov=True)
                #input(covs)

                #   slope[i,j]=params[0]
                #   intercept[i,j]=params[1]
                #   if abs(params[0])<2*(covs[0,0]**0.5):
                #       new_image[i,j]=stats.mode(fgs[:,i,j],axis=None)[0][0]
                #       color_im[i,j]=2
                #   else:
                #       new_image[i,j]=(adat_z[i,j]-params[1])/params[0]
                #       if np.isnan(covs[0,1]):
                #input('whyyyyyyyyyyyyuyuyyyyyyyy')
                #           covs[0,1]=0
                #       confidence=(abs((covs[1,1]/((adat_z[i,j]-params[1])**2))+(covs[0,0]/((params[0])**2))-(2*((abs(covs[0,1]))**0.5)/((adat_z[i,j]-params[1])*params[0]))))**0.5

                #      confidences[i,j]=confidence/new_image[i,j]
                #      if confidence>=2:
                #          new_image[i,j]=-1000
                #      color_im[i,j]=3
                #      if i>136:

                #plt.plot(fgs[:,i,j],distributions[:,i,j],'o',label=str((i,j)))
                #plt.plot(sorted(fgs[:,i,j]),[x*params[0]+params[1] for x in sorted(fgs[:,i,j])],'r')
                #plt.legend()
                #plt.show()
                #plt.close()
                #input((params[0],params[1]))
                #plt.plot(distributions[:,i,j],fgs[:,i,j],'o',label=str((i,j)))

                #plt.plot([adat_z[i,j]],[(adat_z[i,j]-params[1])/params[0]],'+',
                #        label='{:.3g},{:.3g}'.format(adat_z[i,j],(adat_z[i,j]-params[1])/params[0]))

                #plt.plot(sorted(distributions[:,i,j]),[(x-params[1])/params[0] for x in sorted(distributions[:,i,j])],'r')
                #plt.ylim([-0.01,1.01])
                #plt.legend()
                #plt.show()
                #plt.close()
                #input((params[1],params[0]+params[1]))
                #         pass
                # print(params[0],params[1])
                mean_template = np.mean(distributions[:, i, j])
                std_template = np.std(distributions[:, i, j])
                mean_templates[i, j] = mean_template
                t_map[i, j] = (adat_z[i, j] - mean_template) / std_template

                #input()
        #nb.save(nb.Nifti1Image(mean_templates,aff.affine),'/data/henry4/jjuwono/mean_templates.nii.gz')
        #nb.save(nb.Nifti1Image(slope,a.affine),'/data/henry4/jjuwono/slopes.nii.gz')
        #nb.save(nb.Nifti1Image(intercept,a.affine),'/data/henry4/jjuwono/intercepts.nii.gz')
        try:
            os.mkdir(os.path.join(outputs_path, 'quality_assurance'))
        except:
            pass

        nb.save(
            nb.Nifti1Image(confidences, aff.affine),
            os.path.join(outputs_path, 'quality_assurance/confidence.nii.gz'))
        nb.save(
            nb.Nifti1Image(new_image, aff.affine),
            os.path.join(outputs_path, 'quality_assurance/new_image.nii.gz'))
        nb.save(
            nb.Nifti1Image(new_image_logi, aff.affine),
            os.path.join(outputs_path,
                         'quality_assurance/new_image_logi.nii.gz'))
        nb.save(nb.Nifti1Image(t_map, aff.affine),
                os.path.join(outputs_path, 'quality_assurance/t_map.nii.gz'))
        nb.save(
            nb.Nifti1Image(color_im, aff.affine),
            os.path.join(outputs_path, 'quality_assurance/color_im.nii.gz'))
        nb.save(
            nb.Nifti1Image(original_line_fit, aff.affine),
            os.path.join(outputs_path,
                         'quality_assurance/original_line_fit.nii.gz'))
        return 1
    else:
        return 0
Exemplo n.º 4
0
def run_this(static, outputs_path, subj, sess, protocol, prefix=0):
    file_handl = open(os.path.join(outputs_path, 'papers.txt'), 'a')
    apply_warps = False
    if not (prefix):
        if 'retest' in static:
            subject = c.get_mse(static) + 'retest' + scanner(static)
        else:
            subject = c.get_mse(static) + scanner(static)
    else:
        subject = prefix
    log.info('#########{}######{}'.format(subject, subject))
    mse = subject

    try:
        os.remove(
            os.path.join(outputs_path,
                         'registrations2/warped/synslice_avggmsegs.nii.gz'))
    except:
        pass

    output_path = os.path.join(outputs_path, 'registrations1/')
    log_gm_job_status("final set of warps", subj, sess, protocol)

    if apply_warps == True:
        dim = 2
        static_path = static
        files = sorted(glob('/flywheel/v0/input/pth/*'))
        files2 = sorted(glob('/flywheel/v0/input/pth2/*'))
        if not os.path.exists(os.path.join(output_path, 'warped1')):
            os.makedirs(os.path.join(self.output_path, 'warped1'))
        for i in range(len(files)):
            if 'syn' in files[i]:
                continue
            fls = os.path.basename(files[i])
            fls2 = os.path.basename(files2[i])
            log.info((fls, fls2))
            cmd1 = [
                '/opt/ants-2.3.1/WarpImageMultiTransform',
                str(dim), files2[i],
                os.path.join(os.path.join(output_path, 'warped/'),
                             fls2.split('.')[0] + '.nii.gz'),
                os.path.join(output_path,
                             'warp' + fls.split('.')[0] + '1Warp.nii.gz'),
                os.path.join(output_path, 'warp' + fls.split('.')[0] +
                             '0GenericAffine.mat'), '-R', static_path, '\n'
            ]
            cmd2 = [
                '/opt/ants-2.3.1/WarpImageMultiTransform',
                str(dim), files[i],
                os.path.join(os.path.join(output_path, 'warped1/'),
                             fls.split('.')[0] + '.nii.gz'),
                os.path.join(output_path,
                             'warp' + fls.split('.')[0] + '1Warp.nii.gz'),
                os.path.join(output_path, 'warp' + fls.split('.')[0] +
                             '0GenericAffine.mat'), '-R', static_path, '\n'
            ]
            proc = Popen(cmd1, stdout=PIPE)
            proc.wait()

            proc = Popen(cmd2, stdout=PIPE)
            proc.wait()
###run process to grab distributions##

    template_grays = glob(
        os.path.join(outputs_path, 'registrations2/warped/ms*.nii.gz'))
    templates = glob(
        os.path.join(outputs_path, 'registrations1/warped1/ms*.nii.gz'))
    fgs, distributions, a, adat, adat_raw = create_prob_seg_iteration3(
        template_grays, templates, static, file_handl)
    avgim(os.path.join(outputs_path, 'registrations2/warped/'))
    ###run process to fit lines###

    aff = nb.load(static)
    adat_raw = nb.load(static).get_data()
    first_tmap = nb.load(
        glob(os.path.join(outputs_path,
                          'quality_assurance/t_map.nii.gz'))[0]).get_data()
    adat_z = z_score(adat, mapt=first_tmap)
    mask = nb.load(
        glob(os.path.join(outputs_path,
                          'registrations2/warped/syn*'))[0]).get_data()

    mask = np.where(mask > .6, mask, 0)
    bar = np.mean(adat_z[np.where(mask > 0)])
    sh = adat.shape
    slope = np.zeros(sh)
    screwed = np.zeros(sh)
    intercept = np.zeros(sh)
    confidences = np.zeros(sh)
    confidences1 = np.zeros(sh)
    confidences2 = np.zeros(sh)
    t_map = np.zeros(sh)
    mean_templates = np.zeros(sh)
    new_image = np.zeros(sh)
    new_image_logi = np.zeros(sh)
    original_line_fit = np.zeros(sh)
    color_im = np.zeros(sh)
    file_handl.write(str(len(fgs)) + '\n')
    file_handl.write(str(len(distributions)) + '\n')
    distributions = np.asarray(distributions)
    fgs = np.asarray(fgs)
    file_handl.close()
    for i in range(sh[0]):
        for j in range(sh[1]):

            if adat_raw[i, j] == 0:
                continue
    ##insert A block here for polynomial degree fitting ###
    ##average method##
            a = np.array(distributions[:, i, j])[np.newaxis]
            try:
                params = np.polyfit(distributions[:, i, j], fgs[:, i, j], 1)
            except:
                screwed[i, j] = 1
                #plt.plot(distributions[:,i,j],fgs[:,i,j],'o',label=str((i,j)))
                #plt.legend()
                #plt.show()
                #input()
                continue

            original_line_fit[i, j] = (adat_z[i, j] * params[0]) + params[1]
            if len(np.where(fgs[:, i, j] == 0)[0]) < 40 or len(
                    np.where(fgs[:, i, j] == 1)[0]) < 40:

                assign = (adat_z[i, j] * params[0]) + params[1]
            else:
                #print('logi')
                group0 = np.mean(distributions[:, i, j][np.where(fgs[:, i,
                                                                     j] == 0)])
                group1 = np.mean(distributions[:, i, j][np.where(fgs[:, i,
                                                                     j] == 1)])
                grouped = np.where(fgs[:, i, j] != 0, distributions[:, i, j],
                                   group0)
                grouped = np.where(fgs[:, i, j] != 1, grouped, group1)
                params = np.polyfit(grouped, fgs[:, i, j], 1)
                logi_slope = params[0]
                logi_inter = params[1]
                assign = (adat_z[i, j] * params[0]) + params[1]
                if 0 <= assign <= 1:
                    assign = assign
                elif assign < 0:
                    assign = 0
                else:
                    assign = 1
                #if params[0]!=0:
                #plt.plot(grouped,fgs[:,i,j],'o',label=str((i,j)))
                #plt.plot([group0,group1],[group0*params[0]-params[1],group1*params[0]-params[1]],'r')
                #plt.legend()
                #plt.show()
                #input([group0,group1])
            new_image_logi[i, j] = assign

            ##reverse method##
            if len(np.where(fgs[:, i, j] == 0)[0]) < 25 or len(
                    np.where(fgs[:, i, j] == 1)[0]) < 25:
                params = np.polyfit(distributions[:, i, j], fgs[:, i, j], 1)
                new_image[i, j] = (adat_z[i, j] * params[0]) + params[1]
                if len(np.where(fgs[:, i, j] == 0)[0]) < 60:
                    color_im[i, j] = 1
            else:
                #plt.plot(distributions[:,i,j],fgs[:,i,j],'o',label=str((i,j)))
                #plt.legend()
                #plt.show()
                #input()

                params, covs = np.polyfit(fgs[:, i, j],
                                          distributions[:, i, j],
                                          1,
                                          cov=True)
                #input(covs)

                slope[i, j] = params[0]
                intercept[i, j] = params[1]
                if abs(params[0]) < 2 * (covs[0, 0]**0.5):
                    new_image[i, j] = statss.mode(fgs[:, i, j],
                                                  axis=None)[0][0]
                    color_im[i, j] = 2
                else:
                    new_image[i, j] = (adat_z[i, j] - params[1]) / params[0]
                    if np.isnan(covs[0, 1]):
                        #input('whyyyyyyyyyyyyuyuyyyyyyyy')
                        covs[0, 1] = 0
                    confidence = (
                        abs((covs[1, 1] / ((adat_z[i, j] - params[1])**2)) +
                            (covs[0, 0] / ((params[0])**2)) -
                            (2 * ((abs(covs[0, 1]))**0.5) /
                             ((adat_z[i, j] - params[1]) * params[0]))))**0.5

                    confidences[i, j] = confidence / new_image[i, j]
                    if confidence >= 2:
                        new_image[i, j] = -1000
                    color_im[i, j] = 3
                    if i > 136:

                        #plt.plot(fgs[:,i,j],distributions[:,i,j],'o',label=str((i,j)))
                        #plt.plot(sorted(fgs[:,i,j]),[x*params[0]+params[1] for x in sorted(fgs[:,i,j])],'r')
                        #plt.legend()
                        #plt.show()
                        #plt.close()
                        #input((params[0],params[1]))
                        #plt.plot(distributions[:,i,j],fgs[:,i,j],'o',label=str((i,j)))

                        #plt.plot([adat_z[i,j]],[(adat_z[i,j]-params[1])/params[0]],'+',
                        #        label='{:.3g},{:.3g}'.format(adat_z[i,j],(adat_z[i,j]-params[1])/params[0]))

                        #plt.plot(sorted(distributions[:,i,j]),[(x-params[1])/params[0] for x in sorted(distributions[:,i,j])],'r')
                        #plt.ylim([-0.01,1.01])
                        #plt.legend()
                        #plt.show()
                        #plt.close()
                        #input((params[1],params[0]+params[1]))
                        pass
            #print(params[0],params[1])
            mean_template = np.mean(distributions[:, i, j])
            std_template = np.std(distributions[:, i, j])
            mean_templates[i, j] = mean_template
            t_map[i, j] = (adat_z[i, j] - mean_template) / std_template

            #input()
    #nb.save(nb.Nifti1Image(mean_templates,aff.affine),'/data/henry4/jjuwono/mean_templates.nii.gz')
    #nb.save(nb.Nifti1Image(slope,a.affine),'/data/henry4/jjuwono/slopes.nii.gz')
    #nb.save(nb.Nifti1Image(intercept,a.affine),'/data/henry4/jjuwono/intercepts.nii.gz')
    try:
        os.mkdir(os.path.join(outputs_path, 'final_output'))
    except:
        pass
    #nb.save(nb.Nifti1Image(confidences,aff.affine),'/data/henry4/jjuwono/new_GM_method/'+mse+'/confidence.nii.gz')
    #nb.save(nb.Nifti1Image(new_image,aff.affine),'/data/henry4/jjuwono/new_GM_method/'+mse+'/new_image.nii.gz')
    #nb.save(nb.Nifti1Image(new_image_logi,aff.affine),'/data/henry4/jjuwono/new_GM_method/'+mse+'/new_image_logi.nii.gz')
    nb.save(nb.Nifti1Image(t_map, aff.affine),
            os.path.join(outputs_path, 'final_output/rereg_t_map.nii.gz'))
    #nb.save(nb.Nifti1Image(color_im,aff.affine),'/data/henry4/jjuwono/new_GM_method/'+mse+'/color_im.nii.gz')
    nb.save(
        nb.Nifti1Image(original_line_fit, aff.affine),
        os.path.join(outputs_path,
                     'final_output/rereg_original_line_fit.nii.gz'))
    return 1
def run_this(static, outputs_path, subj, sess, protocol, prefix=0):
    file_handl = open(os.path.join(outputs_path, 'papers.txt'), 'a')
    apply_warps = False
    if not (prefix):
        if 'retest' in static:
            subject = c.get_mse(static) + 'retest' + scanner(static)
        else:
            subject = c.get_mse(static) + scanner(static)
    else:
        subject = prefix

    mse = subject

    try:
        os.remove(
            os.path.join(outputs_path,
                         'registrations2/warped/synslice_avggmsegs.nii.gz'))
    except:
        pass
    log_gm_job_status("final set of warps", subj, sess, protocol)

    ###run process to fit lines###

    aff = nb.load(static)
    adat_raw = nb.load(static).get_data()
    print(outputs_path)

    ###run process to grab distributions##
    #quick control helper
    controls = glob(
        os.path.join(outputs_path, 'registrations2/warped/[0-9]*.nii.gz'))
    controls1 = glob(
        os.path.join(outputs_path, 'registrations1/warped1/[0-9]*.nii.gz'))
    control_flag = False
    control1_flag = False
    for i in controls:
        if control_flag == False:
            control_par = os.path.dirname(i)
            control_flag = True
        il = control_par + '/ms' + os.path.basename(i)
        os.rename(i, il)
    for i in controls1:
        if control1_flag == False:
            control1_par = os.path.dirname(i)
            control1_flag = True
        il = control1_par + '/ms' + os.path.basename(i)
        os.rename(i, il)
    #control helper done

    template_grays = glob(
        os.path.join(outputs_path, 'registrations2/warped/ms*.nii.gz'))
    templates = glob(
        os.path.join(outputs_path, 'registrations1/warped1/ms*.nii.gz'))
    fgs, distributions, a, adat, adat_raw = create_prob_seg_iteration3(
        template_grays, templates, static, file_handl)
    avgim(os.path.join(outputs_path, 'registrations2/warped/'))

    #initialize images to write
    sh = adat.shape
    slope = np.zeros(sh)
    screwed = np.zeros(sh)
    intercept = np.zeros(sh)
    confidences = np.zeros(sh)
    confidences1 = np.zeros(sh)
    confidences2 = np.zeros(sh)
    t_map = np.zeros(sh)
    mean_templates = np.zeros(sh)
    new_image = np.zeros(sh)
    new_image_logi = np.zeros(sh)
    original_line_fit = np.zeros(sh)
    color_im = np.zeros(sh)

    file_handl.write(str(len(fgs)) + '\n')
    file_handl.write(str(len(distributions)) + '\n')
    distributions = np.asarray(distributions)
    print('distributions:{}'.format(distributions.shape))
    fgs = np.asarray(fgs)
    file_handl.close()
    adat_list = [z_score_a_dat(adat)]
    count = 0
    for adat_z in adat_list:
        count += 1
        for i in range(sh[0]):
            for j in range(sh[1]):

                if adat_raw[i, j] == 0:
                    continue
                ##insert A block here for polynomial degree fitting ###
                ##average method##
                a = np.array(distributions[:, i, j])[np.newaxis]
                try:
                    params = np.polyfit(distributions[:, i, j], fgs[:, i, j],
                                        1)
                except:
                    screwed[i, j] = 1
                    continue

                original_line_fit[i,
                                  j] = (adat_z[i, j] * params[0]) + params[1]
                if len(np.where(fgs[:, i, j] == 0)[0]) < 40 or len(
                        np.where(fgs[:, i, j] == 1)[0]) < 40:

                    assign = (adat_z[i, j] * params[0]) + params[1]
                else:
                    #print('logi')
                    group0 = np.mean(distributions[:, i,
                                                   j][np.where(fgs[:, i,
                                                                   j] == 0)])
                    group1 = np.mean(distributions[:, i,
                                                   j][np.where(fgs[:, i,
                                                                   j] == 1)])
                    grouped = np.where(fgs[:, i, j] != 0,
                                       distributions[:, i, j], group0)
                    grouped = np.where(fgs[:, i, j] != 1, grouped, group1)
                    params = np.polyfit(grouped, fgs[:, i, j], 1)
                    logi_slope = params[0]
                    logi_inter = params[1]
                    assign = (adat_z[i, j] * params[0]) + params[1]
                    if 0 <= assign <= 1:
                        assign = assign
                    elif assign < 0:
                        assign = 0
                    else:
                        assign = 1
                new_image_logi[i, j] = assign

                ##reverse method##
                if len(np.where(fgs[:, i, j] == 0)[0]) < 25 or len(
                        np.where(fgs[:, i, j] == 1)[0]) < 25:
                    try:
                        params = np.polyfit(distributions[:, i, j], fgs[:, i,
                                                                        j], 1)
                    except:
                        continue
                    new_image[i, j] = (adat_z[i, j] * params[0]) + params[1]
                    if len(np.where(fgs[:, i, j] == 0)[0]) < 60:
                        color_im[i, j] = 1
                else:
                    try:
                        params, covs = np.polyfit(fgs[:, i, j],
                                                  distributions[:, i, j],
                                                  1,
                                                  cov=True)
                    except:
                        continue

                    slope[i, j] = params[0]
                    intercept[i, j] = params[1]
                    if abs(params[0]) < 2 * (covs[0, 0]**0.5):
                        new_image[i, j] = statss.mode(fgs[:, i, j],
                                                      axis=None)[0][0]
                        color_im[i, j] = 2
                    else:
                        new_image[i,
                                  j] = (adat_z[i, j] - params[1]) / params[0]
                        if np.isnan(covs[0, 1]):
                            #input('whyyyyyyyyyyyyuyuyyyyyyyy')
                            covs[0, 1] = 0
                        confidence = (abs(
                            (covs[1, 1] / ((adat_z[i, j] - params[1])**2)) +
                            (covs[0, 0] / ((params[0])**2)) -
                            (2 * ((abs(covs[0, 1]))**0.5) /
                             ((adat_z[i, j] - params[1]) * params[0]))))**0.5

                        confidences[i, j] = confidence / new_image[i, j]
                        if confidence >= 2:
                            new_image[i, j] = -1000
                        color_im[i, j] = 3

                mean_template = np.mean(distributions[:, i, j])
                std_template = np.std(distributions[:, i, j])
                mean_templates[i, j] = mean_template
                t_map[i, j] = (adat_z[i, j] - mean_template) / std_template

        try:
            os.mkdir(os.path.join(outputs_path, 'final_output'))
        except:
            pass
        #nb.save(nb.Nifti1Image(confidences,aff.affine),'/data/henry4/jjuwono/new_GM_method/'+mse+'/confidence.nii.gz')
        #nb.save(nb.Nifti1Image(new_image,aff.affine),'/data/henry4/jjuwono/new_GM_method/'+mse+'/new_image.nii.gz')
        #nb.save(nb.Nifti1Image(new_image_logi,aff.affine),'/data/henry4/jjuwono/new_GM_method/'+mse+'/new_image_logi.nii.gz')
        nb.save(nb.Nifti1Image(t_map, aff.affine),
                os.path.join(outputs_path, 'final_output/rereg_t_map.nii.gz'))
        #nb.save(nb.Nifti1Image(color_im,aff.affine),'/data/henry4/jjuwono/new_GM_method/'+mse+'/color_im.nii.gz')
        nb.save(
            nb.Nifti1Image(original_line_fit, aff.affine),
            os.path.join(outputs_path,
                         'final_output/rereg_original_line_fit.nii.gz'))
    return 1