text_plain_output_1.png
import os

import pandas as pd

import shutil

MAIN_DIR = '/kaggle/input/bach-breast-cancer-histology-images/ICIAR2018_BACH_Challenge/ICIAR2018_BACH_Challenge/Photos'

BASE_DIR = '/kaggle/working/bach-train'
TRAIN_DIR = os.path.join(BASE_DIR, 'training')
VAL_DIR = os.path.join(BASE_DIR, 'validation')
TEST_DIR = os.path.join(BASE_DIR, 'test')
if os.path.exists(BASE_DIR):
    shutil.rmtree(BASE_DIR)
os.makedirs(BASE_DIR)
os.makedirs(TRAIN_DIR)
os.makedirs(VAL_DIR)
os.makedirs(TEST_DIR)

bc_types = [file for file in os.listdir(MAIN_DIR) if os.path.isdir(os.path.join(MAIN_DIR, file))]
bc_dict = {'InSitu': 2, 'Benign': 1, 'Normal': 0, 'Invasive': 3}
dict_bc = {0: 'Normal', 1: 'Benign', 2: 'InSitu', 3: 'Invasive'}

for bc in bc_types:
    train_type_folder = os.path.join(TRAIN_DIR, bc)
    val_type_folder = os.path.join(VAL_DIR, bc)
    test_type_folder = os.path.join(TEST_DIR, bc)
    os.makedirs(train_type_folder)
    os.makedirs(val_type_folder)
    os.makedirs(test_type_folder)

df = pd.read_csv('/kaggle/input/init-bach-eda/bach_data.csv', index_col=0)

df['type'] = df['type'].map(bc_dict)

from sklearn.model_selection import train_test_split
type_dict = dict({0: list(), 1: list(), 2: list(), 3: list()})
for i in range(len(df)):
    path = df['path'][i]
    type_ = df['type'][i]
    if os.path.getsize(path):
        type_dict[type_].append(path)

(len(type_dict[0]), len(type_dict[1]), len(type_dict[2]), len(type_dict[3]))