示例#1
0
def image_standardize(img, mask, img_std):
    print("\n standardization ...")
    c3 = C3d()
    c3.inputs.in_file = img
    c3.inputs.args = "%s -nlw 25x25x25 %s -times -replace nan 0" % (mask, mask)
    c3.inputs.out_file = img_std
    c3.run()
示例#2
0
def trim_like(in_img_file, ref_img_file, out_img_file, interp=0):
    print("\n cropping ...")
    c3 = C3d()
    c3.inputs.in_file = ref_img_file
    c3.inputs.args = "-int %s %s -reslice-identity" % (interp, in_img_file)
    c3.inputs.out_file = out_img_file
    c3.run()
示例#3
0
def image_mask(img, mask, img_masked):
    print("\n skull stripping ...")
    c3 = C3d()
    c3.inputs.in_file = img
    c3.inputs.args = "%s -multiply" % mask
    c3.inputs.out_file = img_masked
    c3.run()
示例#4
0
def copy_orient(in_img_file, ref_img_file, out_img_file):
    print("\n copy orientation ...")
    c3 = C3d()
    c3.inputs.in_file = ref_img_file
    c3.inputs.args = "%s -copy-transform -type uchar" % in_img_file
    c3.inputs.out_file = out_img_file
    c3.run()
示例#5
0
def resample(img, x, y, z, out, interp=0):
    print("\n resmapling ...")
    c3 = C3d()
    c3.inputs.in_file = img
    c3.inputs.args = "-int %s -resample %sx%sx%s" % (interp, x, y, z)
    c3.inputs.out_file = out
    c3.run()
示例#6
0
def trim(in_img_file, out_img_file, voxels=1):
    print("\n cropping ...")
    c3 = C3d()
    c3.inputs.in_file = in_img_file
    c3.inputs.args = "-trim %svox" % voxels
    c3.inputs.out_file = out_img_file
    c3.run()
示例#7
0
def trim(img, out, voxels=1):
    c3 = C3d()
    c3.inputs.in_file = img
    c3.inputs.args = "-trim %svox" % voxels
    c3.inputs.out_file = out
    if not os.path.exists(out):
        print("\n cropping")
        c3.run()
示例#8
0
def trim_like(img, ref, out, interp=0):
    c3 = C3d()
    c3.inputs.in_file = ref
    c3.inputs.args = "-int %s %s -reslice-identity" % (interp, img)
    c3.inputs.out_file = out
    if not os.path.exists(out):
        print("\n cropping like")
        c3.run()
示例#9
0
def orient_img(in_img_file, orient_tag, out_img_file):
    c3 = C3d()
    c3.inputs.in_file = in_img_file
    c3.inputs.args = "-orient %s" % orient_tag
    c3.inputs.out_file = out_img_file
    if os.path.exists(out_img_file):
        print("\n %s already exists" % out_img_file)
    else:
        c3.run()
示例#10
0
def trim_img_to_size(in_img, trimmed_img):
    """
    Trim image to specific size (112x112x64mm)
    :param in_img: input image
    :param trimmed_img: trimmed image
    """
    c3 = C3d()
    c3.inputs.in_file = in_img
    c3.inputs.args = "-trim-to-size 112x112x64mm"
    c3.inputs.out_file = trimmed_img

    if not os.path.exists(trimmed_img):
        print("\n extracting hippocampus region")
        c3.run()
示例#11
0
文件: dasher.py 项目: bigerHe/DASH3R
def standard_img(in_file, std_file):
    """
    Orient image in standard orientation
    :param in_file: input image
    :param std_file: output oriented image
    """
    c3 = C3d()
    c3.inputs.in_file = in_file
    file_shape = nib.load(in_file).shape
    nx = int(file_shape[0] / 2.2)
    ny = int(file_shape[1] / 2.2)
    nz = int(file_shape[2] / 2.2)
    c3.inputs.args = "-binarize -as m %s -push m -nlw %sx%sx%s -push m -times -replace nan 0" % (in_file, nx, ny, nz)
    c3.inputs.out_file = std_file

    if not os.path.exists(std_file):
        c3.run()
示例#12
0
def trim_img_to_size(in_img, trimmed_img):
    """
    Trim image to specific size (112x112x64mm)
    :param in_img: input image
    :param trimmed_img: trimmed image
    """
    # trim(in_img, trimmed_img, voxels=20)
    # file_shape = nib.load(trimmed_img).shape
    # print(file_shape)

    c3 = C3d()
    c3.inputs.in_file = in_img
    c3.inputs.args = "-trim-to-size 112x112x64vox"
    # c3.inputs.args = "-trim-to-size %sx%sx%svox" % (file_shape[0], file_shape[0], int(file_shape[0]/2))
    c3.inputs.out_file = trimmed_img

    # if not os.path.exists(trimmed_img):
    #     print("\n extracting hippocampus region")
    c3.run()
示例#13
0
def reslice_like(in_img, ref_img, trimmed_img):
    c3 = C3d()
    c3.inputs.in_file = ref_img
    c3.inputs.args = "%s -reslice-identity -interpolation Cubic" % in_img
    c3.inputs.out_file = trimmed_img
    c3.run()
示例#14
0
def orient_img(in_img_file, orient_tag, out_img_file):
    c3 = C3d()
    c3.inputs.in_file = in_img_file
    c3.inputs.args = "-orient %s" % orient_tag
    c3.inputs.out_file = out_img_file
    c3.run()
示例#15
0
def main(args):
    parser = parsefn()
    subj_dir, img, seg, gap, tile, alpha, ax, roi, flip, min_sl, out = parse_inputs(
        parser, args)

    # pred preprocess dir
    pred_dir = '%s/pred_process' % os.path.abspath(subj_dir)
    if not os.path.exists(pred_dir):
        os.mkdir(pred_dir)

    # trim seg to focus
    c3 = C3d()

    mosaic_slicer = CreateTiledMosaic()

    if seg:
        c3.inputs.in_file = seg
        c3.inputs.args = "-trim %sx%sx%svox" % (roi, roi, roi)
        seg_trim_file = "%s/%s_trim_mosaic.nii.gz" % (
            pred_dir, os.path.basename(seg).split('.')[0])
        # seg_trim_file = "seg_trim.nii.gz"
        c3.inputs.out_file = seg_trim_file
        c3.run()

        # trim struct like seg
        c3.inputs.in_file = seg_trim_file
        c3.inputs.args = "%s -reslice-identity" % img
        struct_trim_file = "%s/%s_trim_mosaic.nii.gz" % (
            pred_dir, os.path.basename(img).split('.')[0])
        # struct_trim_file = "struct_trim.nii.gz"
        c3.inputs.out_file = struct_trim_file
        c3.run()

        # create rgb image from seg
        converter = ConvertScalarImageToRGB()

        converter.inputs.dimension = 3
        converter.inputs.input_image = seg_trim_file
        converter.inputs.colormap = 'jet'
        converter.inputs.minimum_input = 0
        converter.inputs.maximum_input = 10
        out_rgb = "%s/%s_trim_rgb.nii.gz" % (
            pred_dir, os.path.basename(seg).split('.')[0])
        converter.inputs.output_image = out_rgb
        converter.run()

        mosaic_slicer.inputs.rgb_image = out_rgb
        mosaic_slicer.inputs.mask_image = seg_trim_file
        mosaic_slicer.inputs.alpha_value = alpha

    else:
        struct_trim_file = img

        mosaic_slicer.inputs.rgb_image = struct_trim_file

    # stretch and clip intensities
    c3.inputs.in_file = struct_trim_file
    c3.inputs.args = "-stretch 2% 98% 0 255 -clip 0 255"
    c3.inputs.out_file = struct_trim_file
    c3.run()

    # slices to show
    if gap == 1:
        max_sl = 100
    elif gap == 2:
        max_sl = 220
    elif gap == 5:
        max_sl = 275
    else:
        max_sl = 300

    slices = '[%s,%s,%s]' % (gap, min_sl, max_sl)

    mosaic_slicer.inputs.input_image = struct_trim_file

    mosaic_slicer.inputs.output_image = out
    mosaic_slicer.inputs.direction = ax
    # mosaic_slicer.inputs.pad_or_crop = '[ -15x -50 , -15x -30 ,0]'
    mosaic_slicer.inputs.tile_geometry = tile
    mosaic_slicer.inputs.slices = slices
    mosaic_slicer.inputs.flip_slice = flip
    mosaic_slicer.terminal_output = "none"
    mosaic_slicer.run()
示例#16
0
def main(args):
    parser = parsefn()
    subj_dir, subj, t1, fl, t2, woc, out, bias, num_mc, thresh, ign_ort, force = parse_inputs(
        parser, args)
    cp_orient = False

    if out is None:
        prediction = '%s/%s_T1acq_nu_HfB_pred.nii.gz' % (subj_dir, subj) \
            # if bias is True else '%s/%s_T1acq_HfB_pred.nii.gz' % (subj_dir, subj)

        prediction_std_orient = '%s/%s_T1acq_nu_HfB_pred_std_orient.nii.gz' % (
            subj_dir, subj)
    else:
        prediction = out
        prediction_std_orient = "%s/%s_std_orient.nii.gz" % (
            subj_dir, os.path.basename(out).split('.')[0])

    if os.path.exists(prediction) and force is False:
        print("\n %s already exists" % prediction)
    else:
        start_time = datetime.now()

        hfb = os.path.realpath(__file__)
        hyper_dir = Path(hfb).parents[2]

        if fl is None and t2 is None:
            test_seqs = [t1]
            training_mods = ["t1"]
            model_name = 'hfb_t1only_mcdp'
            model_name_woc = 'hfb_t1'
            print("\n found only t1-w, using the %s model" % model_name)

        elif t2 is None and fl:
            test_seqs = [t1, fl]
            training_mods = ["t1", "flair"]
            model_name = 'hfb_t1fl_mcdp'
            model_name_woc = 'hfb_t1fl'
            print("\n found t1 and fl sequences, using the %s model" %
                  model_name)

        elif fl is None and t2:
            test_seqs = [t1, t2]
            training_mods = ["t1", "t2"]
            model_name = 'hfb_t1t2_mcdp'
            model_name_woc = 'hfb_t1t2'
            print("\n found t1 and t2 sequences, using the %s model" %
                  model_name)

        else:
            test_seqs = [t1, fl, t2]
            training_mods = ["t1", "flair", "t2"]
            model_name = 'hfb_multi_mcdp'
            model_name_woc = 'hfb'
            print("\n found all 3 sequences, using the full %s model" %
                  model_name)

        model_json = '%s/models/%s_model.json' % (hyper_dir, model_name)
        model_weights = '%s/models/%s_model_weights.h5' % (hyper_dir,
                                                           model_name)

        assert os.path.exists(
            model_json
        ), "%s does not exist ... please download and rerun script" % model_json
        assert os.path.exists(
            model_weights
        ), "%s does not exist ... please download and rerun script" % model_weights

        # pred preprocess dir
        print(
            colored("\n pre-processing %s..." % os.path.abspath(subj_dir),
                    'green'))
        pred_dir = "%s/pred_process" % os.path.abspath(subj_dir)
        if not os.path.exists(pred_dir):
            os.mkdir(pred_dir)

        #############
        # bias correction if requested
        if bias is True:
            t1_bias = os.path.join(subj_dir, "%s_T1_nu.nii.gz" % subj)
            biascorr.main(["-i", "%s" % t1, "-o", "%s" % t1_bias])
            in_ort = t1_bias
        else:
            in_ort = t1

        # std orientations
        r_orient = 'RPI'
        l_orient = 'LPI'
        # check orientation
        t1_ort = "%s/%s_std_orient.nii.gz" % (
            subj_dir, os.path.basename(t1).split('.')[0])
        if ign_ort is False:
            cp_orient = check_orient(in_ort, r_orient, l_orient, t1_ort)

        # loading t1
        in_t1 = t1_ort if os.path.exists(t1_ort) else in_ort
        t1_img = nib.load(in_t1)

        ###########
        c3 = C3d()
        pred_shape = [128, 128, 128]
        test_data = np.zeros((1, len(training_mods), pred_shape[0],
                              pred_shape[1], pred_shape[2]),
                             dtype=t1_img.get_data_dtype())

        for s, seq in enumerate(test_seqs):
            print(
                colored(
                    "\n pre-processing %s" %
                    os.path.basename(seq).split('.')[0], 'green'))

            seq_ort = "%s/%s_std_orient.nii.gz" % (
                subj_dir, os.path.basename(seq).split('.')[0])
            if training_mods[s] != 't1':
                if training_mods[s] == 'flair':
                    seq_bias = os.path.join(subj_dir,
                                            "%s_T1acq_nu_FL.nii.gz" % subj)
                else:
                    seq_bias = os.path.join(subj_dir,
                                            "%s_T1acq_nu_T2.nii.gz" % subj)

                # bias correction if requested
                if bias is True:
                    biascorr.main(["-i", "%s" % seq, "-o", "%s" % seq_bias])
                seq = seq_bias if os.path.exists(seq_bias) else seq

                # check orientation
                if ign_ort is False:
                    cp_orient_seq = check_orient(seq, r_orient, l_orient,
                                                 seq_ort)

            in_seq = seq_ort if os.path.exists(seq_ort) else seq

            image = nib.load(in_seq)
            img = image.get_data()

            # standardize intensity for data
            print("\n standardizing ...")
            std_img = (img - img.mean()) / img.std()
            std_file = '%s/%s_standardized.nii.gz' % (
                pred_dir, os.path.basename(seq).split('.')[0])
            nib.save(nib.Nifti1Image(std_img, t1_img.affine), std_file)

            # cropping
            if training_mods[s] == 't1':
                crop_file = '%s/%s_standardized_cropped.nii.gz' % (
                    pred_dir, os.path.basename(seq).split('.')[0])
                trim(std_file, crop_file, voxels=1)
            else:
                crop_file = '%s/%s_standardized_cropped.nii.gz' % (
                    pred_dir, os.path.basename(seq).split('.')[0])
                ref_file = '%s/%s_standardized_cropped.nii.gz' % (
                    pred_dir, os.path.basename(t1).split('.')[0])
                trim_like(std_file, ref_file, crop_file, interp=1)

            #resampling
            # img = nib.load(crop_file)
            # res = resample(img, [pred_shape[0], pred_shape[1], pred_shape[2]])
            # res_file = '%s/%s_resampled.nii.gz' % (pred_dir, os.path.basename(seq).split('.')[0])
            # nib.save(res, res_file)

            res_file = '%s/%s_resampled.nii.gz' % (
                pred_dir, os.path.basename(seq).split('.')[0])
            resample(crop_file,
                     pred_shape[0],
                     pred_shape[1],
                     pred_shape[2],
                     res_file,
                     interp=1)

            if not os.path.exists(res_file):
                print("\n pre-processing %s" % training_mods[s])
                c3.run()
            res_data = nib.load(res_file)
            test_data[0, s, :, :, :] = res_data.get_data()

        print(
            colored(
                "\n predicting hfb segmentation using MC Dropout with %s samples"
                % num_mc, 'green'))

        res_t1_file = '%s/%s_resampled.nii.gz' % (
            pred_dir, os.path.basename(t1).split('.')[0])
        res = nib.load(res_t1_file)

        pred_s = np.zeros(
            (num_mc, pred_shape[0], pred_shape[1], pred_shape[2]),
            dtype=res.get_data_dtype())

        for sample_id in range(num_mc):
            print("MC sample # %s" % sample_id)
            pred = run_test_case(test_data=test_data,
                                 model_json=model_json,
                                 model_weights=model_weights,
                                 affine=res.affine,
                                 output_label_map=True,
                                 labels=1)
            pred_s[sample_id, :, :, :] = pred.get_data()

        # computing mean
        pred_mean = pred_s.mean(axis=0)
        pred = nib.Nifti1Image(pred_mean, res.affine)

        pred_prob = os.path.join(pred_dir, "hfb_prob.nii.gz")
        nib.save(pred, pred_prob)

        pred_th_name = os.path.join(pred_dir, "hfb_pred.nii.gz")
        pred_th = math_img('img > %s' % thresh, img=pred)
        nib.save(pred_th, pred_th_name)

        # resample back
        pred_res = resample_to_img(pred_prob, t1_img)
        pred_prob_name = os.path.join(
            pred_dir, "%s_%s_pred_prob.nii.gz" % (subj, model_name))
        nib.save(pred_res, pred_prob_name)

        # sm
        pred_sm = smooth_img(pred_res, fwhm=3)
        pred_res_th = math_img('img > %s' % thresh, img=pred_sm)
        # conn comp
        pred_comp = largest_connected_component_img(pred_res_th)
        pred_name = os.path.join(pred_dir,
                                 "%s_%s_pred.nii.gz" % (subj, model_name))
        nib.save(pred_comp, pred_name)

        # copy original orientation to final prediction
        if ign_ort is False and cp_orient:
            nib.save(pred_comp, prediction_std_orient)
            fill_holes(prediction_std_orient, prediction_std_orient)

            copy_orient(pred_name, in_ort, prediction)
            fill_holes(prediction, prediction)

        else:
            nib.save(pred_comp, prediction)
            fill_holes(prediction, prediction)

        # mask
        t1_masked_name = '%s/%s_T1_nu_masked.nii.gz' % (subj_dir, subj) \
            if bias is True else '%s/%s_masked.nii.gz' % (subj_dir, os.path.basename(t1).split('.')[0])
        masked_t1 = math_img("img1 * img2",
                             img1=nib.load(in_ort),
                             img2=nib.load(prediction))
        nib.save(masked_t1, t1_masked_name)

        if ign_ort is False and cp_orient:
            t1_masked_name_std = '%s/%s_T1_nu_masked_std_orient.nii.gz' % (subj_dir, subj) \
                if bias is True else '%s/%s_masked_std_orient.nii.gz' % (subj_dir, os.path.basename(t1).split('.')[0])
            masked_t1_std = math_img("img1 * img2",
                                     img1=t1_img,
                                     img2=nib.load(prediction_std_orient))
            nib.save(masked_t1_std, t1_masked_name_std)

        # predict cerebellum
        if woc == 1 and model_name_woc == 'hfb':
            print("\n predicting approximate cerebellar mask")

            cereb_prediction = '%s/%s_T1acq_nu_cerebellum_pred.nii.gz' % (subj_dir, subj) \
                if bias is True else '%s/%s_T1acq_cerebellum_pred.nii.gz' % (subj_dir, subj)
            model_json_woc = '%s/models/%s_model.json' % (hyper_dir,
                                                          model_name_woc)
            cereb_weights = '%s/models/cereb_model_weights.h5' % hyper_dir

            cereb_pred = run_test_case(test_data=test_data,
                                       model_json=model_json_woc,
                                       model_weights=cereb_weights,
                                       affine=res.affine,
                                       output_label_map=True,
                                       labels=1)

            # resample back
            cereb_pred_res = resample_to_img(cereb_pred, t1_img)
            cereb_pred_name = os.path.join("%s/%s_hfb_cereb_pred_prob.nii.gz" %
                                           (pred_dir, subj))
            nib.save(cereb_pred_res, cereb_pred_name)
            cereb_sm = smooth_img(cereb_pred_res, fwhm=2)
            cereb_th = math_img('img > 0.25', img=cereb_sm)
            nib.save(cereb_th, cereb_prediction)

            # remove cerebellum
            woc_img = pred_comp.get_data() - cereb_th.get_data()
            woc_nii = nib.Nifti1Image(woc_img, t1_img.affine)
            # conn comp
            woc_th = math_img('img > 0', img=woc_nii)
            woc_comp = largest_connected_component_img(woc_th)
            woc_name = os.path.join(
                pred_dir, "%s_%s_woc_pred.nii.gz" % (subj, model_name))
            nib.save(woc_comp, woc_name)

            woc_pred = '%s/%s_T1acq_nu_HfB_woc_pred.nii.gz' % (subj_dir, subj) \
                if bias is True else '%s/%s_T1acq_HfB_woc_pred.nii.gz' % (subj_dir, subj)
            woc_pred_std_orient = '%s/%s_T1acq_nu_HfB_woc_pred_std_orient.nii.gz' % (
                subj_dir, subj)

            if ign_ort is False and cp_orient:
                nib.save(woc_comp, woc_pred_std_orient)
                fill_holes(woc_pred_std_orient, woc_pred_std_orient)

                copy_orient(woc_name, in_ort, woc_pred)
                fill_holes(woc_pred, woc_pred)

            else:
                nib.save(woc_comp, woc_pred)
                fill_holes(woc_pred, woc_pred)

            # mask
            t1_woc_name = '%s/%s_T1_nu_masked_woc.nii.gz' % (subj_dir, subj) \
                if bias is True else '%s/%s_masked_woc.nii.gz' % (subj_dir, os.path.basename(t1).split('.')[0])
            woc_t1 = math_img("img1 * img2",
                              img1=nib.load(in_ort),
                              img2=nib.load(woc_pred))
            nib.save(woc_t1, t1_woc_name)

            if ign_ort is False and cp_orient:
                t1_woc_name = '%s/%s_T1_nu_masked_woc_std_orient.nii.gz' % (subj_dir, subj) \
                    if bias is True else '%s/%s_masked_woc_std_orient.nii.gz' % (subj_dir, os.path.basename(t1).split('.')[0])
                woc_t1 = math_img("img1 * img2",
                                  img1=t1_img,
                                  img2=nib.load(woc_pred_std_orient))
                nib.save(woc_t1, t1_woc_name)
        else:
            print(
                'Removing cerebellum is available only when all 3 sequence t1, flair, and t2 have existed.'
            )

        print("\n generating mosaic image for qc")

        seg_qc.main(
            ["-i",
             "%s" % t1, "-s",
             "%s" % prediction, "-g", "5", "-m", "75"])

        endstatement.main('Brain extraction and mosaic generation',
                          '%s' % (datetime.now() - start_time))
示例#17
0
def fill_holes(in_img_file, out_img_file):
    c3 = C3d()
    c3.inputs.in_file = in_img_file
    c3.inputs.args = "-holefill 1 0 -type uchar"
    c3.inputs.out_file = out_img_file
    c3.run()
示例#18
0
def main(args):
    parser = parsefn()
    subj_dir, subj, t1, fl, t2, mask, out, force = parse_inputs(parser, args)
    cp_orient = False

    if out is None:
        prediction = '%s/%s_T1acq_nu_ventricles_pred.nii.gz' % (subj_dir, subj)
        prediction_std_orient = '%s/%s_T1acq_nu_ventricles_pred_std_orient.nii.gz' % (
            subj_dir, subj)
    else:
        prediction = out
        prediction_std_orient = "%s/%s_std_orient.nii.gz" % (
            subj_dir, os.path.basename(out).split('.')[0])

    if os.path.exists(prediction) and force is False:
        print("\n %s already exists" % prediction)
    else:
        start_time = datetime.now()

        file_path = os.path.realpath(__file__)
        hyper_dir = Path(file_path).parents[2]

        # if fl is None or t2 is None:
        if fl is None and t2 is None:
            test_seqs = [t1]
            training_mods = ["t1"]
            model_name = 'vent_t1only'
            print("\n found only t1-w, using the %s model" % model_name)
        elif t2 is None and fl:
            test_seqs = [t1, fl]
            training_mods = ["t1", "flair"]
            model_name = 'vent_t1fl'
            print("\n found the t1-w and FLAIR, using the t1-flair model")
        else:
            test_seqs = [t1, fl, t2]
            training_mods = ["t1", "flair", "t2"]
            model_name = 'vent_multi'
            print(
                "\n found all 3 sequences, using the model with all 3 sequences"
            )

        model_json = '%s/models/%s_model.json' % (hyper_dir, model_name)
        model_weights = '%s/models/%s_model_weights.h5' % (hyper_dir,
                                                           model_name)

        assert os.path.exists(
            model_json
        ), "%s does not exist ... please download and rerun script" % model_json
        assert os.path.exists(model_weights), \
            "%s model does not exist ... please download and rerun script" % model_weights

        # pred preprocess dir
        print(colored("\n pre-processing ...", 'green'))
        pred_dir = '%s/pred_process' % os.path.abspath(subj_dir)
        if not os.path.exists(pred_dir):
            os.mkdir(pred_dir)

        # standardize intensity and mask data
        c3 = C3d()

        pred_shape = [128, 128, 128]
        # std orientations
        r_orient = 'RPI'
        l_orient = 'LPI'

        # check orientation t1 and mask
        t1_ort = "%s/%s_std_orient.nii.gz" % (
            subj_dir, os.path.basename(t1).split('.')[0])
        cp_orient = check_orient(t1, r_orient, l_orient, t1_ort)
        mask_ort = "%s/%s_std_orient.nii.gz" % (
            subj_dir, os.path.basename(mask).split('.')[0])
        cp_orient_m = check_orient(mask, r_orient, l_orient, mask_ort)
        in_mask = mask_ort if os.path.exists(mask_ort) else mask

        # loading t1
        in_t1 = t1_ort if os.path.exists(t1_ort) else t1
        t1_img = nib.load(in_t1)

        test_data = np.zeros((1, len(training_mods), pred_shape[0],
                              pred_shape[1], pred_shape[2]),
                             dtype=t1_img.get_data_dtype())

        for s, seq in enumerate(test_seqs):
            print(
                colored(
                    "\n pre-processing %s" %
                    os.path.basename(seq).split('.')[0], 'green'))

            seq_ort = "%s/%s_std_orient.nii.gz" % (
                subj_dir, os.path.basename(seq).split('.')[0])
            if training_mods[s] != 't1':
                # check orientation
                cp_orient_seq = check_orient(seq, r_orient, l_orient, seq_ort)
            in_seq = seq_ort if os.path.exists(seq_ort) else seq

            # masked
            seq_masked = "%s/%s_masked.nii.gz" % (
                pred_dir, os.path.basename(seq).split('.')[0])
            image_mask(in_seq, in_mask, seq_masked)

            # standardized
            seq_std = "%s/%s_masked_standardized.nii.gz" % (
                pred_dir, os.path.basename(seq).split('.')[0])
            image_standardize(seq_masked, in_mask, seq_std)

            # cropping
            if training_mods[s] == 't1':
                seq_crop = '%s/%s_masked_standardized_cropped.nii.gz' % (
                    pred_dir, os.path.basename(seq).split('.')[0])
                trim(seq_std, seq_crop, voxels=1)
            else:
                seq_crop = '%s/%s_masked_standardized_cropped.nii.gz' % (
                    pred_dir, os.path.basename(seq).split('.')[0])
                ref_file = '%s/%s_masked_standardized_cropped.nii.gz' % (
                    pred_dir, os.path.basename(t1).split('.')[0])
                trim_like(seq_std, ref_file, seq_crop, interp=1)

            # resampling
            img = nib.load(seq_crop)
            res = resample(img, [pred_shape[0], pred_shape[1], pred_shape[2]])
            seq_res = '%s/%s_resampled.nii.gz' % (
                pred_dir, os.path.basename(seq).split('.')[0])
            nib.save(res, seq_res)

            if not os.path.exists(seq_res):
                print("\n pre-processing %s" % training_mods[s])
                c3.run()
            res_data = nib.load(seq_res)
            test_data[0, s, :, :, :] = res_data.get_data()

        print(colored("\n generating ventricle segmentation", 'green'))

        res_t1_file = '%s/%s_resampled.nii.gz' % (
            pred_dir, os.path.basename(t1).split('.')[0])
        res = nib.load(res_t1_file)

        pred = run_test_case(test_data=test_data,
                             model_json=model_json,
                             model_weights=model_weights,
                             affine=res.affine,
                             output_label_map=True,
                             labels=1)

        # resample back
        pred_res = resample_to_img(pred, t1_img)
        pred_prob_name = os.path.join(
            pred_dir, "%s_%s_pred_prob.nii.gz" % (subj, model_name))
        nib.save(pred_res, pred_prob_name)

        pred_res_th = math_img('img > 0.5', img=pred_res)
        pred_name = os.path.join(pred_dir,
                                 "%s_%s_pred.nii.gz" % (subj, model_name))
        nib.save(pred_res_th, pred_name)

        # copy original orientation to final prediction
        if cp_orient:
            nib.save(pred_res_th, prediction_std_orient)
            copy_orient(pred_name, t1, prediction)
        else:
            nib.save(pred_res_th, prediction)

        print("\n generating mosaic image for qc")

        seg_qc.main(
            ['-i',
             '%s' % t1, '-s',
             '%s' % prediction, '-g', '2', '-m', '40'])

        endstatement.main('Ventricles prediction and mosaic generation',
                          '%s' % (datetime.now() - start_time))