示例#1
0
def compute_warped_image_label(img_label_txt_pth,phi_pth,phi_type, saving_pth):
    img_label_pth_list = read_txt_into_list(img_label_txt_pth)
    phi_pth_list = glob(os.path.join(phi_pth,phi_type))
    f = lambda pth: sitk.GetArrayFromImage(sitk.ReadImage(pth))
    img_list = [f(pth[0]) for pth in img_label_pth_list]
    label_list = [f(pth[1]) for pth in img_label_pth_list]
    num_img = len(img_list)
    for i in range(num_img):
        fname = get_file_name(img_label_pth_list[i][0])
        img = torch.Tensor(img_list[i][None][None])
        label = torch.Tensor(label_list[i][None][None])
        f_phi = lambda x: get_file_name(x).find(fname)==0
        phi_sub_list = list(filter(f_phi, phi_pth_list))
        num_aug = len(phi_sub_list)
        phi_list = [f(pth) for pth in phi_sub_list]
        img = img.repeat(num_aug,1,1,1,1)
        label = label.repeat(num_aug,1,1,1,1)
        phi = np.stack(phi_list,0)
        phi = np.transpose(phi,(0,4,3,2,1))
        phi = torch.Tensor(phi)
        sz = np.array(img.shape[2:])
        spacing = 1./(sz-1)
        phi, _ = resample_image(phi,spacing,[1,3]+list(img.shape[2:]))
        warped_img = compute_warped_image_multiNC(img,phi,spacing,spline_order=1,zero_boundary=True)
        warped_label = compute_warped_image_multiNC(label,phi,spacing,spline_order=0,zero_boundary=True)
        save_image_with_given_reference(warped_img,[img_label_pth_list[i][0]]*num_aug,saving_pth,[get_file_name(pth).replace("_phi","")+'_warped' for pth in phi_sub_list])
        save_image_with_given_reference(warped_label,[img_label_pth_list[i][0]]*num_aug,saving_pth,[get_file_name(pth).replace("_phi","")+'_label' for pth in phi_sub_list])
示例#2
0
def get_test_file_for_brainstorm_color(test_path,transfer_path,output_txt):
    #atlas_image_9023193_image_test_iter_0_warped.nii.gz
    file_label_list = read_txt_into_list(test_path)
    file_list, label_list = [file[0] for file in file_label_list],[file[1] for file in file_label_list]
    f = lambda x: "atlas_image_"+x+"_test_iter_0_warped.nii.gz"
    new_file_list = [os.path.join(transfer_path,f(get_file_name(file))) for file in file_list]
    new_file_label_list = [[new_file_list[i],label_list[i]] for i in range(len(file_label_list))]
    write_list_into_txt(output_txt,new_file_label_list)
示例#3
0
def init_test_env(setting_path, output_path, file_list, fname_list):
    """
    create test environment, the file list would be saved into output_path/reg/test/file_path_list.txt,
     a corresponding auto-parsed filename list would also be saved in output/path/reg/test/file_name_list.txt

    :param setting_path: the path to load 'cur_task_setting.json' and 'cur_data_setting.json' (optional if the related settings are in cur_task_setting)
    :param output_path: the output path of the task
    :param image_path_list: the image list, each item refers to the abstract path of the image
    :param l_path_list:optional, the label of image list, each item refers to the abstract path of the image
    :return: tuple of ParameterDict,  datapro (optional) and tsk_set
    """
    dm_json_path = os.path.join(setting_path, 'cur_data_setting.json')
    tsm_json_path = os.path.join(setting_path, 'cur_task_setting.json')
    assert os.path.isfile(tsm_json_path), "task setting not exists"
    dm = DataTask('task_reg',
                  dm_json_path) if os.path.isfile(dm_json_path) else None
    tsm = ModelTask('task_reg', tsm_json_path)
    file_num = len(file_list)
    os.makedirs(os.path.join(output_path, 'seg/test'), exist_ok=True)
    os.makedirs(os.path.join(output_path, 'seg/res'), exist_ok=True)
    file_txt_path = os.path.join(output_path, 'seg/test/file_path_list.txt')
    fn_txt_path = os.path.join(output_path, 'seg/test/file_name_list.txt')
    has_label = len(file_list[0]) == 2
    if fname_list is None:
        if has_label:
            fname_list = [
                get_file_name(file_list[i][0]) for i in range(file_num)
            ]
        else:
            fname_list = [get_file_name(file_list[i]) for i in range(file_num)]
    write_list_into_txt(file_txt_path, file_list)
    write_list_into_txt(fn_txt_path, fname_list)
    data_task_name = 'seg'
    cur_task_name = 'res'
    if dm is not None:
        dm.data_par['datapro']['dataset']['output_path'] = output_path
        dm.data_par['datapro']['dataset']['task_name'] = data_task_name
    tsm.task_par['tsk_set']['task_name'] = cur_task_name
    tsm.task_par['tsk_set']['output_root_path'] = os.path.join(
        output_path, data_task_name)
    return dm, tsm
示例#4
0
def get_pair_txt_for_oai_reg_net(train_txt_path,warped_folder,warped_type, num_train,output_txt):
    train_pair_list = read_txt_into_list(train_txt_path)
    warped_file_list = glob(os.path.join(warped_folder,warped_type))
    name_set = [get_file_name(pair[0]).split("_")[0] for pair in train_pair_list]
    name_set = set(name_set)
    name_file_dict = {name:[] for name in name_set}
    extra_weight = 2
    for pair in train_pair_list:
        fname = get_file_name(pair[0]).split("_")[0]
        for i in range(extra_weight):
            name_file_dict[fname].append(pair[0])
            name_file_dict[fname].append(pair[1])
    for file in warped_file_list:
        fname = get_file_name(file).split("_")[0]
        name_file_dict[fname].append(file)
    num_per_patient = int(num_train/len(name_set))
    train_list = []
    for name,values in name_file_dict.items():
        num_sample = 0
        while num_sample < num_per_patient:
            pair = random.sample(name_file_dict[name],2)
            if get_file_name(pair[0])==get_file_name(pair[1]) or get_file_name(pair[0]).split("_")[1]==get_file_name(pair[1]).split("_")[1]:
                continue
            else:
                train_list.append(pair)
                num_sample += 1
    write_list_into_txt(output_txt, train_list)
示例#5
0
        deformation_np, mask_np, spacing = read_deformation_and_mask(deformation_path_list[i], mask_path_list[i],itk_format)
        extra_res = compute_jacobi_map(deformation_np,spacing,[fname_list[i]],mask_np,crop_boundary=True,save_jacobi_map=True,saving_folder=saving_folder)
        jacobi_val_res += extra_res[0] * batch_size
        jacobi_num_res += extra_res[1] * batch_size
        average_jacobi_masked_res += extra_res[2]*batch_size
        records_jacobi_val_np[i] = extra_res[0]
        records_jacobi_num_np[i] = extra_res[1]
        average_jacobi_masked_np[i] = extra_res[2]
        print("id {} and current pair name is : {}".format(i,os.path.split(f_path)[-1]))
        print('the running averge jocobi val sum is {}'.format(jacobi_val_res/(i+1)/batch_size))
        print('the running averge jocobi num sum is {}'.format(jacobi_num_res/(i+1)/batch_size))
        print('the running averge jocobi masked average is {}'.format(average_jacobi_masked_res/(i+1)/batch_size))
    jacobi_val_res = jacobi_val_res/num_samples
    jacobi_num_res = jacobi_num_res/num_samples
    average_jacobi_masked_res = average_jacobi_masked_res/num_samples
    print("the average {}_ jacobi val sum: {}  :".format('test', jacobi_val_res))
    print("the average {}_ jacobi num sum: {}  :".format('test', jacobi_num_res))
    print("the average {}_ average_jacobi_masked average: {}  :".format('test', average_jacobi_masked_res))
    np.save(os.path.join(saving_folder,'records_jacobi'),records_jacobi_val_np)
    np.save(os.path.join(saving_folder,'records_jacobi_num'),records_jacobi_num_np)
    np.save(os.path.join(saving_folder,'records_average_jacobi_masked'),average_jacobi_masked_np)




deformation_mask_folder_path = "/playpen-raid1/zyshen/data/reg_new_lung/testing_lddmm/reg/res/records/original_sz"
deformation_path_list =glob.glob(os.path.join(deformation_mask_folder_path,'*img_inv_phi.nii.gz'), recursive=True)
f_mask_list =[f.replace('img_inv_phi','img_moving_l') for f in deformation_path_list]
fname_list = [get_file_name(f).replace("_img_inv_phi","") for f in deformation_path_list]
saving_path = "/playpen-raid1/zyshen/data/reg_new_lung/testing_lddmm/jacobi_analysis"
compute_jacobi(deformation_path_list,fname_list, saving_path, f_mask_list,itk_format=False)