# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.9.7 64-bit (''tfGpuEnv'': conda)'
#     name: python3
# ---

# # The COVIDNetX challenge

# <img src="https://www.psycharchives.org/retrieve/096175aa-f7f2-4970-989d-d934c30b5551" alt="drawing" width="400"/>
#
# The following is a classification challenge using the [COVID-X dataset](https://github.com/lindawangg/COVID-Net/blob/master/docs/COVIDx.md).
# The goal is to predict whether a person has COVID-19 or not based on chest X-RAY images.
#
# There are two different categories: `positive` and `negative`. `positive` means a person has COVID-19, `negative` means a person
# has not COVID-19.
#
# The metric we use is F1 (https://en.wikipedia.org/wiki/F1_score). The goal is to maximize F1.
#
# The data contains images with their associated labels.

# ## EDA

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
import seaborn as sns

# ### Load Data

# +
data_dir = 'data/'
# data_dir = 'data_subset/'

df = pd.read_csv(data_dir+'train.csv')
test_df = pd.read_csv(data_dir+'submission_valid.csv')
# -

(df.label.value_counts())/len(df)
sns.countplot(data=df, x='label');

# ### Add "img_shape" Column

df['img_shape'] = df.image.apply(lambda img: Image.open(data_dir+'train/'+img).size)
df.sample(10)

# ### Image Shapes

fig = plt.figure(figsize=(15,4), dpi=150)
sns.countplot(data=df.sample(frac=0.1, random_state=42), x='img_shape');
plt.xticks(rotation=90);

df.img_shape.value_counts()/len(df)

# **NOTE**: 87% images have shape: 1024x1024

# #### Covid "positive" Image Shapes

pos = df[df.label=='positive']
fig = plt.figure(figsize=(15,4), dpi=150)
sns.countplot(data=pos.sample(frac=0.1, random_state=42), x='img_shape');
plt.xticks(rotation=90);

pos.img_shape.value_counts()/len(pos)

pos_non_1024x1024 = pos[pos.img_shape!=(1024,1024)]
print(f"% of covid positive images which are not of 1024x1024 shape: {len(pos_non_1024x1024)/len(pos)}")

# **NOTE**: 99% X-ray images of covid positive cases are _not_ of 1024x1024 shape

StarcoderdataJupyterScriptsDedupFiltered