Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bach script #47

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 102 additions & 22 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ requires-python = ">=3.10"
dependencies = [
"pytorch-lightning>=2.1.3",
"loguru>=0.7.2",
"pandas>=2.2.0",
]

[project.optional-dependencies]
Expand Down
81 changes: 81 additions & 0 deletions scripts/metadata/bach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""script to create metadata for the BACH dataset
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest a bit different folder structure:

scripts
  | 
  datasets
    |
    BACH
      |- README.md
      |- generate_metadata.py

Now in README.md we can put some info abou the dataset and then instructions on how to use it (step 1: download the dataset from to this 2. Then execute the script python scripts/datasets/BACH/generate_metadata.py ... etc)

Basically something similar to this

What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, created an issue for this: #70


This script creates a metadata file with paths, labels and splits of the
BACH train patch dataset. These are 400 patches in
"ICIAR2018_BACH_Challenge/Photos" that belong to one of the four classes:
"Normal", "Benign", "InSitu", "Invasive".

The metadata is a dataframe, that will be stored as csv with the columns:
- path: path to the raw images
- label: label of the image
- split: split the image belongs to ('train', 'val', 'test')*

* the splits are created as follows: ordered by filename, stratfied by label,
train: 70%, val: 15%, test: 15%

to run:
(1) download 'ICIAR2018_BACH_Challenge.zip' from https://zenodo.org/records/3632035
and unzip the data (only the 'Photos' folder is needed)
(2) specify the following parameters:
- DOWNLOADED_DATA_PATH: the directory where the raw patches are stored, e.g.
"<...>/3632035/ICIAR2018_BACH_Challenge/Photos"
- TARGET_METADATA_FILE: the directory where the metadata dataframe will be
stored (e.g. "./metadata").
- TARGET_METADATA_FILE: the metadata file name (e.g. "bach_metadata.csv")
(3) run script with `python scripts/metadata_creation/bach.py`
"""

import glob
import os

import pandas as pd
from loguru import logger

# Specify relevant directories:
DOWNLOADED_DATA_PATH = "<...>/3632035/ICIAR2018_BACH_Challenge/Photos"
TARGET_METADATA_DIR = "./metadata"
TARGET_METADATA_FILE = "bach_metadata.csv"

# labels mapping and train/val/test split fractions (do not modify):
_file_dir_to_label = {
"Normal": 0,
"Benign": 1,
"InSitu": 2,
"Invasive": 3,
}
_train_fraction = 0.7
_val_fraction = 0.15


def main():
# create dataframe with paths and labels:
all_patches = glob.glob(f"{DOWNLOADED_DATA_PATH}/**/*.tif")
logger.info(f"Loaded paths to {len(all_patches)} images.")

df_metadata = pd.DataFrame(all_patches, columns=["path"])
df_metadata["label"] = df_metadata["path"].apply(lambda x: _file_dir_to_label[x.split("/")[-2]])

# create splits:
df_metadata["split"] = ""
dfs_label = []
for label in df_metadata["label"].unique():
df = (
df_metadata[df_metadata["label"] == label].sort_values(by="path").reset_index(drop=True)
)
n_train = round(df.shape[0] * _train_fraction)
n_val = round(df.shape[0] * _val_fraction)
df.loc[:n_train, "split"] = "train"
df.loc[n_train : n_train + n_val, "split"] = "val"
df.loc[n_train + n_val :, "split"] = "test"
dfs_label.append(df)
df_metadata = pd.concat(dfs_label).sort_values(by=["split", "label"]).reset_index(drop=True)

# save metadata:
if not os.path.exists(TARGET_METADATA_DIR):
os.mkdir(TARGET_METADATA_DIR)
df_metadata.to_csv(os.path.join(TARGET_METADATA_DIR, TARGET_METADATA_FILE), index=False)
logger.info(f"Metadata saved to {os.path.join(TARGET_METADATA_DIR, TARGET_METADATA_FILE)}.")


if __name__ == "__main__":
main()