text_html_output_1.png text_plain_output_1.png
from PIL import Image

import matplotlib.pyplot as plt

import numpy as np

import os

import pandas as pd

import seaborn as sns

data_path = '/kaggle/input/covidx9a/'
images_path = '/kaggle/input/covidx-cxr2/train'
data_file = 'train_COVIDx9A.txt'
train = pd.read_csv(os.path.join(data_path, data_file), header=None, sep=' ')
train.columns = ['patient id', 'filename', 'class', 'data source']

data_classes = train['class'].unique()
df_summary_count = pd.DataFrame()
for dataset in ['cohen', 'fig1', 'actmed', 'sirm', 'ricord', 'rsna', 'stonybrook', 'bimcv', 'rnsa']:
    num_negative = train.loc[(train['data source'] == dataset) & (train['class'] == data_classes[0]), 'filename'].count()
    if len(data_classes) == 2:
        num_positive = train.loc[(train['data source'] == dataset) & (train['class'] == data_classes[1]), 'filename'].count()
        df_new = pd.DataFrame({'Dataset': [dataset], 'Covid': [num_positive], 'Negative': [num_negative]})
    elif len(data_classes) == 3:
        num_pneumonia = train.loc[(train['data source'] == dataset) & (train['class'] == data_classes[1]), 'filename'].count()
        num_positive = train.loc[(train['data source'] == dataset) & (train['class'] == data_classes[2]), 'filename'].count()
        df_new = pd.DataFrame({'Dataset': [dataset], 'Covid': [num_positive], 'Pneumonia': [num_pneumonia], 'Negative': [num_negative]})
    df_summary_count = pd.concat([df_summary_count, df_new])

patient_distribution = train.groupby(['patient id', 'data source', 'class']).count().reset_index()
patient_distribution.rename(columns={'filename': 'num_patients'}, inplace=True)
num_patients_bydata = patient_distribution[['data source', 'num_patients']].groupby(['data source']).count()
num_patients_byclass = patient_distribution[['class', 'num_patients']].groupby(['class']).count()

print('Images are saved at:', images_path)
fig, axs = plt.subplots(3, 3, figsize=(18, 14))
for i in range(3):
    for j in range(3):
        if j == 0:
            file_name, class_label = train[train['class'] == data_classes[0]].iloc[i, [1, 2]]
        elif j == 1:
            file_name, class_label = train[train['class'] == data_classes[1]].iloc[i, [1, 2]]
        elif j == 2 and len(data_classes) == 3:
            file_name, class_label = train[train['class'] == data_classes[2]].iloc[i, [1, 2]]
        else:
            print('Out of bounds')
        image_file = os.path.join(images_path, file_name)
        img = Image.open(image_file)
        print('Original:', 3 * i + j, np.asarray(img).shape)
        img = img.convert('L')
        axs[i, j].set_title(f'Class: {class_label} - Image Size: {np.asarray(img).shape}')
        axs[i, j].axis('off')
        axs[i, j].imshow(img, cmap='gray')
plt.show()