def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--yaml-config',
                        type=str,
                        default='simg_bmi_regression_0_cam.yaml')
    args = parser.parse_args()

    SRC_ROOT = os.path.dirname(os.path.realpath(__file__)) + '/../..'
    yaml_config = os.path.join(SRC_ROOT, f'src/yaml/{args.yaml_config}')
    logger.info(f'Read yaml file {yaml_config}')
    f = open(yaml_config, 'r').read()
    config = yaml.safe_load(f)

    num_fold = config['fold_num']
    exp_dir = config['exp_dir']

    out_dir = osp.join(exp_dir, 'cam_analysis/atlas_space')
    mkdir_p(out_dir)

    file_list = read_file_contents_list(file_list_txt)
    # folder_obj = DataFolder(ori_img_dir, file_list)
    folder_obj = DataFolder(ori_atlas_dir, file_list)

    ave_obj = AverageValidRegion(folder_obj, 10)
    ave_obj.run_get_average()
    ave_obj.output_result_folder(out_dir, ambient_val=-1000)
def main():
    file_list = read_file_contents_list(in_file_list_file)
    label_reader_obj = ClinicalDataReaderSPORE.create_spore_data_reader_csv(
        in_label_file)
    bmi_list, valid_file_list = label_reader_obj.get_gt_value_BMI(file_list)
    # print(bmi_list[:10])
    train_idx_fold_array, valid_idx_fold_array, test_idx_fold_array = get_idx_list_array_bmi_session_level_split(
        bmi_list, num_fold)

    test_bmi_list_array = []
    for idx_fold in range(num_fold):
        test_bmi_list = [
            bmi_list[data_idx] for data_idx in test_idx_fold_array[idx_fold]
        ]
        test_bmi_list_array.append(test_bmi_list)

    out_png = os.path.join(out_folder, 'fold_distribution.png')
    fig, ax = plt.subplots(figsize=(10, 7))
    ax.hist(test_bmi_list_array,
            bins=5,
            label=[f'fold-{idx}' for idx in range(5)],
            alpha=0.7,
            rwidth=0.8,
            density=False)
    ax.legend(loc='best')
    plt.grid(axis='y', alpha=0.8)
    plt.xlabel('BMI')
    # plt.ylabel('Density (Count / Total)')
    plt.ylabel('Count')

    print(f'Save image to {out_png}')
    plt.savefig(out_png, bbox_inches='tight', pad_inches=0.1)
    plt.close()
def get_statics_subgroup(file_list_txt, in_csv):
    # out_csv = os.path.join(exp_dir, 'pred_total.csv')
    print(f'Load csv {in_csv}')

    data_df = pd.read_csv(in_csv, index_col='file_name')

    study_file_list = read_file_contents_list(file_list_txt)
    num_study_case = len(study_file_list)
    print(f'# Study files: {num_study_case}')
    study_data_df = data_df.loc[study_file_list]

    study_label_list = study_data_df['label'].to_numpy()
    study_pred1_list = study_data_df['pred1'].to_numpy()
    study_pred2_list = study_data_df['pred2'].to_numpy()

    diff1 = study_pred1_list - study_label_list
    diff2 = study_pred2_list - study_label_list

    diff1_rmse = np.sqrt(np.mean(np.power(np.abs(diff1), 2)))
    diff1_mean = np.mean(diff1)
    diff2_rmse = np.sqrt(np.mean(np.power(np.abs(diff2), 2)))
    diff2_mean = np.mean(diff2)

    print(f'Diff1 RMSE: {diff1_rmse:.3f}')
    print(f'Diff1 Mean: {diff1_mean:.3f}')
    print(f'Diff2 RMSE: {diff2_rmse:.3f}')
    print(f'Diff2 Mean: {diff2_mean:.3f}')
def axial_clip_plot_native():
    file_name_list = read_file_contents_list(file_list_txt)
    for file_name in file_name_list:
        in_img_path = os.path.join(in_folder, file_name)
        cliper_obj = ClipPlotSeriesWithBack(in_img_path, None, None, 10, 35,
                                            15, 1, -255, 255, None, None, None)
        cliper_obj.clip_plot_img_only(out_png_folder)
def plot_round_normal_distribution():
    file_list_round = read_file_contents_list(out_file_list_round_fov)
    file_list_normal = read_file_contents_list(out_file_list_normal_fov)

    label_df = pd.read_csv(in_csv_file, index_col='id')

    file_list_round_no_ext = [
        file_name.replace('.nii.gz', '') for file_name in file_list_round
    ]
    file_list_normal_no_ext = [
        file_name.replace('.nii.gz', '') for file_name in file_list_normal
    ]

    bmi_round = label_df.loc[file_list_round_no_ext]['bmi'].to_numpy()
    bmi_normal = label_df.loc[file_list_normal_no_ext]['bmi'].to_numpy()

    data_array_sequence = []
    data_array_sequence.append(bmi_round)
    data_array_sequence.append(bmi_normal)

    f, ax = plt.subplots(figsize=(8, 6))
    ax.hist(data_array_sequence,
            bins=10,
            color=['r', 'b'],
            label=[
                f'Round FOV ({len(bmi_round)})',
                f'Normal FOV ({len(bmi_normal)})'
            ],
            alpha=0.8,
            rwidth=0.9,
            density=True)

    ax.legend(loc='best')
    plt.grid(axis='y', alpha=0.8)
    plt.xlabel('BMI')
    plt.ylabel('Density (Count / Total)')

    print(f'Save image to {out_fov_type_bmi_hist_plot}')
    plt.savefig(out_fov_type_bmi_hist_plot,
                bbox_inches='tight',
                pad_inches=0.1)
    plt.close()
def plot_round_fov_output(yaml_config):
    """
    Get the output plot on round fov cases. Run regression_agreement_analysis before this.
    :param yaml_config:
    :return:
    """
    SRC_ROOT = os.path.dirname(os.path.realpath(__file__)) + '/../..'
    yaml_config = os.path.join(SRC_ROOT, f'src/yaml/{yaml_config}')
    logger.info(f'Read yaml file {yaml_config}')
    f = open(yaml_config, 'r').read()
    config = yaml.safe_load(f)

    # num_fold = config['fold_num']
    exp_dir = config['exp_dir']

    in_csv = os.path.join(exp_dir, 'pred_total.csv')
    data_df = pd.read_csv(in_csv, index_col='file_name')

    round_fov_file_list = read_file_contents_list(out_file_list_round_fov)
    _get_rmse_mean_shift_group(round_fov_file_list, data_df)

    normal_fov_file_list = read_file_contents_list(out_file_list_normal_fov)
    _get_rmse_mean_shift_group(normal_fov_file_list, data_df)

    all_file_list = read_file_contents_list(file_list_all)
    _get_rmse_mean_shift_group(all_file_list, data_df)

    round_fov_df = data_df.loc[round_fov_file_list]

    round_fov_pred_list = round_fov_df['pred'].to_numpy()
    round_fov_label_list = round_fov_df['label'].to_numpy()

    out_mean_diff_png = os.path.join(exp_dir, 'round_fov_mean_diff.png')
    out_scatter_png = os.path.join(exp_dir, 'round_fov_scatter.png')

    mean_diff_plot(round_fov_pred_list, round_fov_label_list, np.array([0]),
                   out_mean_diff_png)
    scatter_plot(round_fov_pred_list, round_fov_label_list, np.array([0]),
                 out_scatter_png)

    out_pred_csv = os.path.join(exp_dir, 'pred_total_round_fov.csv')
    round_fov_df.to_csv(out_pred_csv)
Example #7
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--yaml-config',
                        type=str,
                        default='simg_bmi_regression_0_cam.yaml')
    args = parser.parse_args()

    file_list = read_file_contents_list(file_list_txt)
    # folder_obj = DataFolder(ori_img_dir, file_list)
    folder_obj = DataFolder(affine_img_dir, file_list)

    ave_obj = AverageValidRegion(folder_obj, 10)
    ave_obj.run_get_average()
    ave_obj.output_result_folder(out_folder, ambient_val=-1000)
def get_list_round_and_normal_fov():
    file_name_list = read_file_contents_list(file_list_txt)
    round_list = []
    normal_list = []
    for file_name in file_name_list:
        img_obj = ScanWrapper(os.path.join(in_native_folder, file_name))
        img_data = img_obj.get_data()
        img_thres_data = (img_data < threshold_val).astype(int)
        if np.sum(img_thres_data) > thres_num_voxel:
            print(f'Round FOV: {file_name}')
            round_list.append(file_name)
        else:
            print(f'Normal FOV: {file_name}')
            normal_list.append(file_name)

    print(f'# Total round FOV: {len(round_list)}')
    print(f'# Total normal FOV: {len(normal_list)}')
    save_file_contents_list(out_file_list_round_fov, round_list)
    save_file_contents_list(out_file_list_normal_fov, normal_list)
Example #9
0
def get_missing_roi_ratio():
    file_name_list = read_file_contents_list(file_list_txt)

    data_dict = []
    num_total = len(file_name_list)
    counter = 0
    for file_name in file_name_list:
        logger.info(f'Analyze {file_name} ({counter} / {num_total})')
        missing_ratio = _get_sess_missing_ratio(file_name)
        logger.info(f'missing ratio - {missing_ratio:.3f}')
        counter += 1

        data_dict.append({
            'file_name': file_name,
            'missing_ratio': missing_ratio
        })

    missing_ratio_df = pd.DataFrame(data_dict)
    missing_ratio_df.to_csv(out_missing_ratio_csv)