if not path.isfile(path.join(folder, "%03.0d_Flair_ws.npy" % id)):
            continue
        if not path.isfile(path.join(folder, "%03.0d_T1_bc_ws.npy" % id)):
            continue
        if not path.isfile(path.join(folder, "%03.0d_T1c_bc_ws.npy" % id)):
            continue
        if not path.isfile(path.join(folder, "%03.0d_T2_bc_ws.npy" % id)):
            continue
        if not path.isfile(path.join(folder, "%03.0d_segmentation.npy" % id)):
            continue
        t1_img = np.load(path.join(folder, "%03.0d_T1_bc_ws.npy" % id))
        t1c_img = np.load(path.join(folder, "%03.0d_T1c_bc_ws.npy" % id))
        t2_img = np.load(path.join(folder, "%03.0d_T2_bc_ws.npy" % id))
        t2_img_no_bc = np.load(path.join(folder, "%03.0d_T2_ws.npy" % id))
        flair_img = np.load(path.join(folder, "%03.0d_Flair_ws.npy" % id))
        seg_img = np.load(path.join(folder, "%03.0d_segmentation.npy" % id))
        seg_img += 1
        seg_img[t1_img == t1_img[0, 0, 0]] = 0
        t1_img = extract_brain_region(t1_img, seg_img, 0)
        t1c_img = extract_brain_region(t1c_img, seg_img, 0)
        t2_img = extract_brain_region(t2_img, seg_img, 0)
        t2_img_no_bc = extract_brain_region(t2_img_no_bc, seg_img, 0)
        flair_img = extract_brain_region(flair_img, seg_img, 0)
        seg_img = extract_brain_region(seg_img, seg_img, 0)
        np.save(path.join(folder_out, "%03.0d_Flair_ws.npy" % id), flair_img)
        np.save(path.join(folder_out, "%03.0d_segmentation.npy" % id), seg_img)
        np.save(path.join(folder_out, "%03.0d_T2_bc_ws.npy" % id), t2_img)
        np.save(path.join(folder_out, "%03.0d_T2_ws.npy" % id), t2_img_no_bc)
        np.save(path.join(folder_out, "%03.0d_T1c_bc_ws.npy" % id), t1c_img)
        np.save(path.join(folder_out, "%03.0d_T1_bc_ws.npy" % id), t1_img)
 if not path.isfile(path.join(folder, "%03.0d_T1_bc_ws.npy" % id)):
     continue
 if not path.isfile(path.join(folder, "%03.0d_T1c_bc_ws.npy" % id)):
     continue
 if not path.isfile(path.join(folder, "%03.0d_T2_bc_ws.npy" % id)):
     continue
 if not path.isfile(path.join(folder, "%03.0d_segmentation.npy" % id)):
     continue
 t1_img = np.load(path.join(folder, "%03.0d_T1_bc_ws.npy" % id))
 t1c_img = np.load(path.join(folder, "%03.0d_T1c_bc_ws.npy" % id))
 t2_img = np.load(path.join(folder, "%03.0d_T2_bc_ws.npy" % id))
 flair_img = np.load(path.join(folder, "%03.0d_Flair_bc_ws.npy" % id))
 seg_img = np.load(path.join(folder, "%03.0d_segmentation.npy" % id))
 seg_img += 1
 seg_img[t1_img == t1_img[0, 0, 0]] = 0
 t1_img = extract_brain_region(t1_img, seg_img, 0)
 t1c_img = extract_brain_region(t1c_img, seg_img, 0)
 t2_img = extract_brain_region(t2_img, seg_img, 0)
 flair_img = extract_brain_region(flair_img, seg_img, 0)
 seg_img = extract_brain_region(seg_img, seg_img, 0)
 np.save(path.join(folder_out, "%03.0d_Flair_bc_ws.npy" % id), flair_img)
 np.save(path.join(folder_out, "%03.0d_segmentation.npy" % id), seg_img)
 np.save(path.join(folder_out, "%03.0d_T2_bc_ws.npy" % id), t2_img)
 np.save(path.join(folder_out, "%03.0d_T1c_bc_ws.npy" % id), t1c_img)
 np.save(path.join(folder_out, "%03.0d_T1_bc_ws.npy" % id), t1_img)
 all_shapes_HGG.append(seg_img.shape)
 max_values_HGG.append(
     [np.max(t1_img),
      np.max(t1c_img),
      np.max(t2_img),
      np.max(flair_img)])
            continue
        if not path.isfile(path.join(folder, "%03.0d_T2_bc_ws.npy"%id)):
            continue
        has_seg = True
        if not path.isfile(path.join(folder, "%03.0d_segmentation.npy"%id)):
            has_seg = False
        t1_img = np.load(path.join(folder, "%03.0d_T1_bc_ws.npy"%id))
        t1c_img = np.load(path.join(folder, "%03.0d_T1c_bc_ws.npy"%id))
        t2_img = np.load(path.join(folder, "%03.0d_T2_bc_ws.npy"%id))
        t2_img_no_bc = np.load(path.join(folder, "%03.0d_T2_ws.npy"%id))
        flair_img = np.load(path.join(folder, "%03.0d_Flair_ws.npy"%id))
        if has_seg:
            seg_img = np.load(path.join(folder, "%03.0d_segmentation.npy"%id))
        else:
            seg_img = np.ones(t1_img.shape, dtype=np.int32)
        seg_img += 1
        seg_img[t1_img == t1_img[0,0,0]] = 0
        t1_img = extract_brain_region(t1_img, seg_img, 0)
        t1c_img = extract_brain_region(t1c_img, seg_img, 0)
        t2_img = extract_brain_region(t2_img, seg_img, 0)
        t2_img_no_bc = extract_brain_region(t2_img_no_bc, seg_img, 0)
        flair_img = extract_brain_region(flair_img, seg_img, 0)
        seg_img = extract_brain_region(seg_img, seg_img, 0)
        np.save(path.join(folder_out, "%03.0d_Flair_ws.npy"%id), flair_img)
        np.save(path.join(folder_out, "%03.0d_segmentation.npy"%id), seg_img)
        np.save(path.join(folder_out, "%03.0d_T2_bc_ws.npy"%id), t2_img)
        np.save(path.join(folder_out, "%03.0d_T2_ws.npy"%id), t2_img_no_bc)
        np.save(path.join(folder_out, "%03.0d_T1c_bc_ws.npy"%id), t1c_img)
        np.save(path.join(folder_out, "%03.0d_T1_bc_ws.npy"%id), t1_img)

if not os.path.isdir(results_out_folder):
    os.mkdir(results_out_folder)

all_official_metrics = np.zeros((len(test_patients), 13))
ctr=0

patients_test = load_patients_David_for_3D_UNet(test_patients)


for patient_id in test_patients:
    print patient_id
    output_folder = os.path.join(results_out_folder, "%03.0d" % patient_id)
    if not os.path.isdir(output_folder):
        os.mkdir(output_folder)
    patient_data = patients_test[patient_id]
    patient_data["t1"] = extract_brain_region(patient_data["t1"], patient_data["seg"], 0)
    patient_data["t1km"] = extract_brain_region(patient_data["t1km"], patient_data["seg"], 0)
    patient_data["flair"] = extract_brain_region(patient_data["flair"], patient_data["seg"], 0)
    patient_data["adc"] = extract_brain_region(patient_data["adc"], patient_data["seg"], 0)
    patient_data["cbv"] = extract_brain_region(patient_data["cbv"], patient_data["seg"], 0)
    patient_data["seg"] = extract_brain_region(patient_data["seg"], patient_data["seg"], 0)
    curr_shape = patient_data["t1"].shape
    new_shape = np.array(curr_shape) + 92
    for dim in range(len(new_shape)):
        new_shape[dim] = new_shape[dim] + 8 - new_shape[dim]%8
    patient_data["t1"] = resize_image_by_padding(patient_data["t1"], new_shape)
    patient_data["t1km"] = resize_image_by_padding(patient_data["t1km"], new_shape)
    patient_data["flair"] = resize_image_by_padding(patient_data["flair"], new_shape)
    patient_data["adc"] = resize_image_by_padding(patient_data["adc"], new_shape)
    patient_data["cbv"] = resize_image_by_padding(patient_data["cbv"], new_shape)
    patient_data["seg"] = resize_image_by_padding(patient_data["seg"], new_shape, 0)