Exemple #1
0
def main():
    parser = argparse.ArgumentParser('')
    parser.add_argument('--file-list-total', type=str, default=default_list_total)
    parser.add_argument('--file-list-exclude', type=str, default=default_list_exclude)
    parser.add_argument('--file-list-out', type=str, default=default_list_out)
    args = parser.parse_args()

    file_list_total = read_file_contents_list(args.file_list_total)
    file_list_exclude = read_file_contents_list(args.file_list_exclude)

    file_list_reduced = [file_name for file_name in file_list_total if file_name not in file_list_exclude]

    save_file_contents_list(args.file_list_out, file_list_reduced)
def main():
    # file_name = 'sub-S03174_ses-E06315_run-7_bp-chest_ct.nii'
    # file_name = 'sub-S03591_ses-E07300_run-3_bp-chest_ct.nii'
    # file_name = '00000001time20131205.nii.gz'

    file_name_list_txt = '/nfs/masi/COVID19_public/BIMCV-COVID19/ct_cxr_pair/data/centaur_pilot.24.file_list'
    file_name_list = read_file_contents_list(file_name_list_txt)

    in_nii_folder = '/nfs/masi/COVID19_public/BIMCV-COVID19/ct_cxr_pair/data/ct'
    out_mask_nii_folder = '/nfs/masi/COVID19_public/BIMCV-COVID19/ct_cxr_pair/data/centaur_pilot.24'
    out_png_folder = '/nfs/masi/COVID19_public/BIMCV-COVID19/ct_cxr_pair/data/centaur_pilot.24.clip'

    mkdir_p(out_png_folder)

    for file_name in file_name_list:
        in_nii = os.path.join(in_nii_folder, file_name + '.gz')
        out_nii = os.path.join(out_mask_nii_folder, file_name)
        # out_png = os.path.join(out_png_folder, file_name.replace('.nii', '.png'))

        # get_pad_mask2(in_nii, out_nii)
        # out_axial_png = os.path.join(out_png_folder, file_name.replace('.nii', '_axial.png'))
        # multiple_clip_overlay_with_mask(in_nii, out_nii, out_axial_png, view_flag='axial')
        #
        # out_sagittal_png = os.path.join(out_png_folder, file_name.replace('.nii', '_sagittal.png'))
        # multiple_clip_overlay_with_mask(in_nii, out_nii, out_sagittal_png, view_flag='sagittal')
        #
        # out_coronal_png = os.path.join(out_png_folder, file_name.replace('.nii', '_coronal.png'))
        # multiple_clip_overlay_with_mask(in_nii, out_nii, out_coronal_png, view_flag='coronal')

        out_folder = os.path.join(
            '/nfs/masi/COVID19_public/BIMCV-COVID19/ct_cxr_pair/data/centaur_pilot.24.clip',
            file_name)
        clip_connected_component_bb_center_with_mask(in_nii, out_nii,
                                                     out_folder)
Exemple #3
0
def main():
    parser = argparse.ArgumentParser('Plot box and scatter data.')
    parser.add_argument('--file-list-total', type=str)
    parser.add_argument('--file-list-out', type=str)
    parser.add_argument('--num-file-select', type=int)

    args = parser.parse_args()

    file_list_total = read_file_contents_list(args.file_list_total)

    file_list_out = random.choices(file_list_total, k=args.num_file_select)

    save_file_contents_list(args.file_list_out, file_list_out)
Exemple #4
0
def main():
    # file_name = 'sub-S03174_ses-E06315_run-7_bp-chest_ct.nii'
    # file_name = 'sub-S03591_ses-E07300_run-3_bp-chest_ct.nii'
    # file_name = '00000001time20131205.nii.gz'

    # file_name_list_txt = '/nfs/masi/COVID19_public/BIMCV-COVID19/ct_cxr_pair/data/centaur_pilot.24.file_list'
    file_name_list_txt = '/nfs/masi/COVID19_public/TCIA_COVID_19_CT/COVID-19-20_miccai/COVID-19-20_v2/ct_list.txt'
    file_name_list = read_file_contents_list(file_name_list_txt)

    # in_nii_folder = '/nfs/masi/COVID19_public/BIMCV-COVID19/ct_cxr_pair/data/ct'
    # out_mask_nii_folder = '/nfs/masi/COVID19_public/BIMCV-COVID19/ct_cxr_pair/data/centaur_pilot.24'
    # out_png_folder = '/nfs/masi/COVID19_public/BIMCV-COVID19/ct_cxr_pair/data/centaur_pilot.24.clip'

    in_nii_folder = '/nfs/masi/COVID19_public/TCIA_COVID_19_CT/COVID-19-20_miccai/COVID-19-20_v2/Train'
    out_mask_nii_folder = '/nfs/masi/COVID19_public/TCIA_COVID_19_CT/COVID-19-20_miccai/COVID-19-20_v2/Train'
    out_png_folder = '/nfs/masi/COVID19_public/TCIA_COVID_19_CT/COVID-19-20_miccai/COVID-19-20_v2/clip_png'

    mkdir_p(out_png_folder)

    first_n = 20

    counter = 0
    for file_name in file_name_list:
        if counter > first_n:
            break

        in_nii = os.path.join(in_nii_folder, file_name)
        mask_nii = os.path.join(out_mask_nii_folder,
                                file_name.replace('_ct.', '_seg.'))
        # out_png = os.path.join(out_png_folder, file_name.replace('.nii', '.png'))

        # get_pad_mask2(in_nii, out_nii)
        # out_axial_png = os.path.join(out_png_folder, file_name.replace('.nii', '_axial.png'))
        # multiple_clip_overlay_with_mask(in_nii, out_nii, out_axial_png, view_flag='axial')
        #
        # out_sagittal_png = os.path.join(out_png_folder, file_name.replace('.nii', '_sagittal.png'))
        # multiple_clip_overlay_with_mask(in_nii, out_nii, out_sagittal_png, view_flag='sagittal')
        #
        # out_coronal_png = os.path.join(out_png_folder, file_name.replace('.nii', '_coronal.png'))
        # multiple_clip_overlay_with_mask(in_nii, out_nii, out_coronal_png, view_flag='coronal')

        out_folder = os.path.join(out_png_folder, file_name)
        clip_connected_component_bb_center_with_mask(in_nii, mask_nii,
                                                     out_folder)

        counter += 1
Exemple #5
0
def main():
    parser = argparse.ArgumentParser('Plot box and scatter data.')
    parser.add_argument('--file-list-folder', type=str)
    parser.add_argument('--out-file-list', type=str)
    args = parser.parse_args()

    file_list_of_file_list_txt = os.listdir(args.file_list_folder)

    total_list = []
    for file_list_file in file_list_of_file_list_txt:
        file_path = os.path.join(args.file_list_folder, file_list_file)
        file_list = read_file_contents_list(file_path)
        total_list = total_list + file_list
    unique_list = unique(total_list)
    logger.info(f'Number of unique files {len(unique_list)}')

    with open(args.out_file_list, 'w') as file:
        for item in unique_list:
            file.write(item + '\n')
Exemple #6
0
def main():
    parser = argparse.ArgumentParser('Box plot for dice statistics.')
    parser.add_argument('--dice-data-root', type=str)
    parser.add_argument('--item-list', type=str)
    parser.add_argument('--out-csv-folder', type=str)
    args = parser.parse_args()

    item_list = read_file_contents_list(args.item_list)
    logger.info('Item list')

    metric_list = get_metric_list(args.dice_data_root)

    mean_table = []
    std_table = []
    for method_name in item_list:
        metric_mean = {}
        metric_std = {}
        for metric in metric_list:
            metric_dict = metric_list[metric]
            csv_path = metric_dict['csv_root'] + '/' + method_name + '/' + metric_dict['csv_name']
            mean, std = get_statics_csv(csv_path, metric_dict['column_name'])
            metric_mean[metric] = mean
            metric_std[metric] = std
        metric_mean['Method'] = method_name
        metric_std['Method'] = method_name

        mean_table.append(metric_mean)
        std_table.append(metric_std)

    out_csv_mean = os.path.join(args.out_csv_folder, 'mean.csv')
    out_csv_std = os.path.join(args.out_csv_folder, 'std.csv')

    df_mean = pd.DataFrame(mean_table)
    df_std = pd.DataFrame(std_table)

    # headers = ["Method", "Dice_lung", "Dice_body", "MSD_lung", "MSD_body"]
    pd.set_option('precision', 5)
    headers = ["Method", "Dice_lung", "Dice_body"]
    logger.info(f'Saving mean table to {out_csv_mean}')
    df_mean.to_csv(out_csv_mean, index=False, columns=headers)
    logger.info(f'Saving std table to {out_csv_std}')
    df_std.to_csv(out_csv_std, index=False, columns=headers)
def main():
    parser = argparse.ArgumentParser('Plot box and scatter data.')
    parser.add_argument('--sub-file-list', type=str)
    parser.add_argument('--in-csv', type=str)
    parser.add_argument('--out-csv', type=str)
    args = parser.parse_args()

    data_df = pd.read_csv(args.in_csv)
    scan_list_total = data_df['Scan'].tolist()
    data_dict_total = data_df.to_dict('records')

    sub_file_list = read_file_contents_list(args.sub_file_list)
    idx_list = [
        scan_list_total.index(file_name) for file_name in sub_file_list
    ]
    sub_dict = [data_dict_total[idx] for idx in idx_list]

    sub_data_df = pd.DataFrame(sub_dict)

    logger.info(f'Save to {args.out_csv}')
    sub_data_df.to_csv(args.out_csv, index=False)
def main():
    parser = argparse.ArgumentParser('Box plot for dice statistics.')
    parser.add_argument('--dice-data-root', type=str)
    parser.add_argument('--item-list', type=str)
    parser.add_argument('--out-fig-folder', type=str)
    parser.add_argument('--num-scans', type=int, default=50)
    args = parser.parse_args()

    item_list = read_file_contents_list(args.item_list)
    logger.info('Item list')
    # print(item_list)

    mask_flag_list = ['lung_mask', 'body_mask']

    for mask_flag in mask_flag_list:
        data_table = get_dice_data_table(args.dice_data_root, item_list,
                                         args.num_scans, mask_flag)
        mask_df = pd.DataFrame(data_table, columns=item_list)
        mask_df.plot.box(grid='True')

        out_fig_path = os.path.join(args.out_fig_folder, mask_flag + '.png')
        logger.info(f'Save plot to {out_fig_path}')
        plt.savefig(out_fig_path)
Exemple #9
0
 def _get_file_list(file_list_txt):
     return read_file_contents_list(file_list_txt)
def main():
    parser = argparse.ArgumentParser('Plot box and scatter data.')
    parser.add_argument('--in-csv', type=str)
    parser.add_argument('--thres-val', type=float)
    parser.add_argument('--out-fig', type=str)
    args = parser.parse_args()

    test_collection_df = pd.read_csv(args.in_csv)
    test_dict = test_collection_df.set_index('TestName').to_dict('index')

    num_test = len(test_dict)
    num_data = 50

    y_table = np.full((num_data, num_test), 1).astype(float)
    for test_idx, test_name in enumerate(test_dict):
        test_item = test_dict[test_name]
        test_df = pd.read_csv(test_item['CSV'])
        y_all = test_df[test_item['COLUMN']].to_numpy()
        y_table[:, test_idx] = y_all[:]

    fig, ax = plt.subplots(figsize=(num_test * 2 + 2, 8))
    plt.boxplot(y_table)

    # Add outliers as scatter points
    num_outlier = np.zeros(num_test)
    mean_val = np.zeros(num_test)
    # kp_val = np.zeros(num_test)
    outlier_data = []
    for test_idx, test_name in enumerate(test_dict):
        test_item = test_dict[test_name]
        test_df = pd.read_csv(test_item['CSV'])
        data_dict = test_df.set_index('Scan').to_dict('index')
        outlier_list = read_file_contents_list(test_item['OUTLIER'])
        num_outlier[test_idx] = len(outlier_list)
        scan_list = test_df['Scan'].to_list()
        column_flag = test_item['COLUMN']
        x_out_all, y_out_all = get_x_y_outlier_list(data_dict, column_flag,
                                                    scan_list, test_idx + 1)
        # kp_val[test_idx] = data_dict[outlier_list[0]][column_flag]
        mean_val[test_idx] = np.mean(y_out_all)
        plt.scatter(x_out_all, y_out_all, color='r', alpha=0.5)

        x_out, y_out = get_x_y_outlier_list(data_dict, column_flag,
                                            outlier_list, test_idx + 1)
        outlier_data.append(y_out)

    # plot outlier as connected dots
    # for outlier_idx in range(int(num_outlier[0])):
    #     y_val = [outlier_data[test_idx][outlier_idx] for test_idx in range(num_test)]
    #     plt.plot(range(1, len(y_val) + 1), y_val, linestyle='--', marker='o', color='b')

    labels = [item.get_text() for item in ax.get_xticklabels()]
    for test_idx, test_name in enumerate(test_dict):
        mean = float("{:.5f}".format(mean_val[test_idx]))
        # kp = float("{:.5f}".format(kp_val[test_idx]))
        # labels[test_idx] = f'{test_name} \noutlier {int(num_outlier[test_idx])}/{num_data}\nmean {mean}'
        labels[test_idx] = f'{test_name} \nmean {mean}'
        # labels[test_idx] = f'{test_name}\n KP1 Lung DSC {kp}'
    ax.set_xticklabels(labels)

    # Threshold.
    print(f'Thres: {args.thres_val}')
    plt.axhline(y=args.thres_val, color='r', linestyle='--')

    logger.info(f'Save plot to {args.out_fig}')
    plt.grid()
    plt.savefig(args.out_fig)
Exemple #11
0
def main():
    parser = argparse.ArgumentParser('Plot box and scatter data.')
    parser.add_argument('--csv-data', type=str)
    parser.add_argument('--column', type=str)
    parser.add_argument('--out-fig', type=str)
    parser.add_argument('--outlier-list-lung-dice', type=str)
    parser.add_argument('--outlier-list-body-dice', type=str)
    parser.add_argument('--outlier-list-nmi', type=str)
    parser.add_argument('--outlier-list-manual', type=str)
    parser.add_argument('--thres-val', type=float)
    args = parser.parse_args()

    logger.info(f'Read csv: {args.csv_data}')
    data_df = pd.read_csv(args.csv_data)
    data_dict = data_df.set_index('Scan').to_dict()[args.column]
    # print(data_dict)
    outlier_list_lung_dice = read_file_contents_list(
        args.outlier_list_lung_dice)
    outlier_list_body_dice = read_file_contents_list(
        args.outlier_list_body_dice)
    outlier_list_nmi = read_file_contents_list(args.outlier_list_nmi)
    outlier_list_manual = read_file_contents_list(args.outlier_list_manual)

    outlier_items = [{
        'outlier_list': outlier_list_lung_dice,
        'idx': 2,
        'color': 'red'
    }, {
        'outlier_list': outlier_list_body_dice,
        'idx': 3,
        'color': 'blue'
    }, {
        'outlier_list': outlier_list_nmi,
        'idx': 4,
        'color': 'green'
    }, {
        'outlier_list': outlier_list_manual,
        'idx': 5,
        'color': 'orange'
    }]

    num_metric = 4
    y_all = data_df[args.column].to_numpy()
    y_all_table = np.zeros((len(y_all), num_metric + 1))
    for i in range(num_metric + 1):
        y_all_table[:, i] = y_all[:]
    x_all = np.random.normal(1, 0.01, len(y_all))

    fig, ax = plt.subplots()

    plt.boxplot(y_all_table)
    plt.scatter(x_all, y_all, c='grey', alpha=1)

    for outlier_item in outlier_items:
        x_outlier, y_outlier = get_x_y_outlier_list(
            data_dict, outlier_item['outlier_list'], outlier_item['idx'])
        plt.scatter(x_outlier, y_outlier, c=outlier_item['color'], alpha=0.5)

    labels = [item.get_text() for item in ax.get_xticklabels()]
    labels[0] = f'All ({len(y_all)})'
    labels[1] = f'Outliers (Lung, {len(outlier_list_lung_dice)}/{len(y_all)})'
    labels[2] = f'Outliers (Body, {len(outlier_list_body_dice)}/{len(y_all)})'
    labels[3] = f'Outliers (NMI, {len(outlier_list_nmi)}/{len(y_all)})'
    labels[
        4] = f'Outliers (Manual QA, {len(outlier_list_manual)}/{len(y_all)})'
    ax.set_xticklabels(labels)

    # Threshold.
    print(f'Thres: {args.thres_val}')
    plt.axhline(y=args.thres_val, color='r', linestyle='--')

    logger.info(f'Save plot to {args.out_fig}')
    fig.set_size_inches(14, 7.5)
    plt.savefig(args.out_fig)
def main():
    parser = argparse.ArgumentParser('Plot box and scatter data.')
    parser.add_argument('--csv-data-baseline', type=str)
    parser.add_argument('--csv-data-1', type=str)
    parser.add_argument('--csv-data-2', type=str)
    parser.add_argument('--csv-data-3', type=str)
    parser.add_argument('--csv-data-4', type=str)
    parser.add_argument('--csv-data-5', type=str)
    parser.add_argument('--outlier-list-baseline', type=str)
    parser.add_argument('--outlier-list-1', type=str)
    parser.add_argument('--outlier-list-2', type=str)
    parser.add_argument('--outlier-list-3', type=str)
    parser.add_argument('--outlier-list-4', type=str)
    parser.add_argument('--outlier-list-5', type=str)
    parser.add_argument('--column', type=str)
    parser.add_argument('--thres-val', type=float)
    parser.add_argument('--out-fig', type=str)
    parser.add_argument('--num-complete-test', type=int)
    args = parser.parse_args()

    test_items = [
        {
            'in_csv': args.csv_data_baseline,
            'outlier_list': args.outlier_list_baseline,
            'name': 'Baseline',
            'idx': 1
        }, {
            'in_csv': args.csv_data_1,
            'outlier_list': args.outlier_list_1,
            'name': 'Cap. Range 80mm',
            'idx': 2
        }
        # },
        # {
        #     'in_csv': args.csv_data_2,
        #     'outlier_list': args.outlier_list_2,
        #     'name': 'Template ref',
        #     'idx': 3
        # },
        # {
        #     'in_csv': args.csv_data_3,
        #     'outlier_list': args.outlier_list_3,
        #     'name': '6 Step',
        #     'idx': 4
        # },
        # {
        #     'in_csv': args.csv_data_4,
        #     'outlier_list': args.outlier_list_4,
        #     'name': '4 Step',
        #     'idx': 5
        # },
        # {
        #     'in_csv': args.csv_data_5,
        #     'outlier_list': args.outlier_list_5,
        #     'name': 'Inst. Opt. (MH)',
        #     'idx': 6
        # }
    ]

    num_test = len(test_items)
    num_test_complete = args.num_complete_test
    num_data = 50

    y_table = np.full((num_data, num_test), 1).astype(float)
    for test_idx in range(num_test_complete):
        test_item = test_items[test_idx]
        test_df = pd.read_csv(test_item['in_csv'])
        y_all = test_df[args.column].to_numpy()
        y_table[:, test_idx] = y_all[:]

    fig, ax = plt.subplots()

    plt.boxplot(y_table)

    # Add outliers as scatter points
    num_outlier = np.zeros(num_test)
    mean_val = np.zeros(num_test)
    for test_idx in range(num_test_complete):
        test_item = test_items[test_idx]
        test_df = pd.read_csv(test_item['in_csv'])
        data_dict = test_df.set_index('Scan').to_dict()[args.column]
        outlier_list = read_file_contents_list(test_item['outlier_list'])
        num_outlier[test_idx] = len(outlier_list)
        scan_list = test_df['Scan'].to_list()
        x_out, y_out = get_x_y_outlier_list(data_dict, scan_list,
                                            test_item['idx'])
        mean_val[test_idx] = np.mean(y_out)
        plt.scatter(x_out, y_out, color='r', alpha=0.5)

    labels = [item.get_text() for item in ax.get_xticklabels()]
    for test_idx in range(num_test):
        test_item = test_items[test_idx]
        test_name = test_item['name']
        mean = float("{:.5f}".format(mean_val[test_idx]))
        labels[
            test_idx] = f'{test_name} \noutlier {int(num_outlier[test_idx])}/{num_data}\nmean {mean}'
    ax.set_xticklabels(labels)

    # Threshold.
    print(f'Thres: {args.thres_val}')
    plt.axhline(y=args.thres_val, color='r', linestyle='--')

    logger.info(f'Save plot to {args.out_fig}')
    plt.grid()
    fig.set_size_inches(14, 7.5)
    plt.savefig(args.out_fig)
Exemple #13
0
def main():
    parser = argparse.ArgumentParser('Plot box and scatter data.')
    parser.add_argument('--csv-data', type=str)
    parser.add_argument('--outlier-list-1', type=str)
    parser.add_argument('--outlier-list-2', type=str)
    parser.add_argument('--thres-val', type=float)
    parser.add_argument('--out-fig', type=str)
    args = parser.parse_args()

    test_items = [{
        'outlier_list': args.outlier_list_1,
        'name': 'Diap Dist (mm)',
        'idx': 1,
        'title': 'Diaphragm Dist (mm)'
    }, {
        'outlier_list': args.outlier_list_2,
        'name': 'Body Dist (mm)',
        'idx': 2,
        'title': 'Body Dist (mm)'
    }]

    num_data = 50
    num_metric = 2
    y_table = np.full((num_data, num_metric), 1).astype(float)
    data_df = pd.read_csv(args.csv_data)
    y_table[:, 0] = data_df['Diap Dist (mm)'].to_numpy()
    y_table[:, 1] = data_df['Body Dist (mm)'].to_numpy()

    fig, ax = plt.subplots()

    plt.boxplot(y_table)

    # Add outliers as scatter points
    num_outlier = np.zeros(num_metric)
    data_dict = data_df.set_index('Scan').to_dict()
    for test_idx in range(len(test_items)):
        test_item = test_items[test_idx]
        data_dict_metric = data_dict[test_item['name']]
        outlier_list = read_file_contents_list(test_item['outlier_list'])
        num_outlier[test_idx] = len(outlier_list)
        x_outlier, y_outlier = get_x_y_outlier_list(data_dict_metric,
                                                    outlier_list,
                                                    test_item['idx'])
        scan_list_all = data_df['Scan'].to_list()
        x_all, y_all = get_x_y_outlier_list(data_dict_metric, scan_list_all,
                                            test_item['idx'])
        plt.scatter(x_outlier, y_outlier, color='r', alpha=0.5)
        plt.scatter(x_all, y_all, color='gray', alpha=0.3)

    labels = [item.get_text() for item in ax.get_xticklabels()]
    for test_idx in range(len(test_items)):
        test_item = test_items[test_idx]
        test_title = test_item['title']
        labels[
            test_idx] = f'{test_title} (Failed case {num_outlier[test_idx]}/{num_data})'
    ax.set_xticklabels(labels)

    # Threshold.
    print(f'Thres: {args.thres_val}')
    plt.axhline(y=args.thres_val, color='r', linestyle='--')

    logger.info(f'Save plot to {args.out_fig}')
    fig.set_size_inches(14, 7.5)
    plt.savefig(args.out_fig)