aspartate
commited on
Commit
·
953dbaf
1
Parent(s):
c9117af
exclude PNGs from download
Browse files- Patho-Bench.py +48 -18
Patho-Bench.py
CHANGED
|
@@ -30,12 +30,19 @@ class PathoBenchConfig(datasets.BuilderConfig):
|
|
| 30 |
|
| 31 |
|
| 32 |
class PathoBenchDataset(datasets.GeneratorBasedBuilder):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
BUILDER_CONFIGS = [
|
| 34 |
PathoBenchConfig(name="custom_config", version="1.0.0", description="PathoBench config")
|
| 35 |
]
|
| 36 |
BUILDER_CONFIG_CLASS = PathoBenchConfig
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
def _info(self):
|
| 40 |
return datasets.DatasetInfo(
|
| 41 |
description="PathoBench: collection of canonical computational pathology tasks",
|
|
@@ -56,36 +63,60 @@ class PathoBenchDataset(datasets.GeneratorBasedBuilder):
|
|
| 56 |
# Ensure the base local directory exists
|
| 57 |
os.makedirs(local_dir, exist_ok=True)
|
| 58 |
|
| 59 |
-
#
|
| 60 |
snapshot_download(
|
| 61 |
repo_id=repo_id,
|
| 62 |
-
allow_patterns=["available_splits.yaml"],
|
| 63 |
repo_type="dataset",
|
| 64 |
local_dir=local_dir,
|
| 65 |
force_download=force_download,
|
| 66 |
)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
with open(os.path.join(local_dir, "available_splits.yaml"), 'r') as file:
|
| 70 |
available_splits = yaml.safe_load(file)
|
| 71 |
|
| 72 |
-
#
|
| 73 |
if dataset_to_download != "*":
|
| 74 |
-
assert dataset_to_download in available_splits,
|
|
|
|
|
|
|
|
|
|
| 75 |
if task_in_dataset != "*":
|
| 76 |
-
assert task_in_dataset in available_splits[dataset_to_download],
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
#
|
| 82 |
if dataset_to_download == "*":
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
snapshot_download(
|
| 90 |
repo_id=repo_id,
|
| 91 |
allow_patterns=allow_patterns,
|
|
@@ -94,7 +125,7 @@ class PathoBenchDataset(datasets.GeneratorBasedBuilder):
|
|
| 94 |
force_download=force_download,
|
| 95 |
)
|
| 96 |
|
| 97 |
-
# Locate all .tsv files
|
| 98 |
search_pattern = os.path.join(local_dir, '**', '*.tsv')
|
| 99 |
all_tsv_splits = glob.glob(search_pattern, recursive=True)
|
| 100 |
|
|
@@ -105,7 +136,6 @@ class PathoBenchDataset(datasets.GeneratorBasedBuilder):
|
|
| 105 |
)
|
| 106 |
]
|
| 107 |
|
| 108 |
-
|
| 109 |
def _generate_examples(self, filepath):
|
| 110 |
idx = 0
|
| 111 |
for file in filepath:
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
class PathoBenchDataset(datasets.GeneratorBasedBuilder):
|
| 33 |
+
"""
|
| 34 |
+
Downloads only the .tsv and .yaml files needed to construct the dataset.
|
| 35 |
+
Excludes .png images so they don't break the builder.
|
| 36 |
+
"""
|
| 37 |
BUILDER_CONFIGS = [
|
| 38 |
PathoBenchConfig(name="custom_config", version="1.0.0", description="PathoBench config")
|
| 39 |
]
|
| 40 |
BUILDER_CONFIG_CLASS = PathoBenchConfig
|
| 41 |
|
| 42 |
+
@property
|
| 43 |
+
def repo_id(self):
|
| 44 |
+
return "MahmoodLab/patho-bench"
|
| 45 |
+
|
| 46 |
def _info(self):
|
| 47 |
return datasets.DatasetInfo(
|
| 48 |
description="PathoBench: collection of canonical computational pathology tasks",
|
|
|
|
| 63 |
# Ensure the base local directory exists
|
| 64 |
os.makedirs(local_dir, exist_ok=True)
|
| 65 |
|
| 66 |
+
# 1) Download the top-level available_splits.yaml
|
| 67 |
snapshot_download(
|
| 68 |
repo_id=repo_id,
|
| 69 |
+
allow_patterns=["available_splits.yaml"], # only this file
|
| 70 |
repo_type="dataset",
|
| 71 |
local_dir=local_dir,
|
| 72 |
force_download=force_download,
|
| 73 |
)
|
| 74 |
|
| 75 |
+
# Read available splits
|
| 76 |
with open(os.path.join(local_dir, "available_splits.yaml"), 'r') as file:
|
| 77 |
available_splits = yaml.safe_load(file)
|
| 78 |
|
| 79 |
+
# Basic validation
|
| 80 |
if dataset_to_download != "*":
|
| 81 |
+
assert dataset_to_download in available_splits, (
|
| 82 |
+
f"{dataset_to_download} was not found. "
|
| 83 |
+
f"Available splits: {list(available_splits.keys())}"
|
| 84 |
+
)
|
| 85 |
if task_in_dataset != "*":
|
| 86 |
+
assert task_in_dataset in available_splits[dataset_to_download], (
|
| 87 |
+
f"{task_in_dataset} was not found in {dataset_to_download}. "
|
| 88 |
+
f"Available tasks: {available_splits[dataset_to_download]}"
|
| 89 |
+
)
|
| 90 |
|
| 91 |
+
# 2) Decide what to allow based on dataset/task
|
| 92 |
+
#
|
| 93 |
+
# We only want .tsv and the relevant .yaml files (like about.yaml, config.yaml).
|
| 94 |
+
# That way, we skip .png images which can cause issues or be large in LFS.
|
| 95 |
if dataset_to_download == "*":
|
| 96 |
+
# Download every dataset subfolder's .tsv and about.yaml/config.yaml
|
| 97 |
+
allow_patterns = [
|
| 98 |
+
"**/*.tsv", # All tsv splits
|
| 99 |
+
"**/about.yaml", # The about files
|
| 100 |
+
"**/config.yaml", # The config files
|
| 101 |
+
"available_splits.yaml" # Already downloaded, but no harm
|
| 102 |
+
]
|
| 103 |
else:
|
| 104 |
+
if task_in_dataset == "*":
|
| 105 |
+
allow_patterns = [
|
| 106 |
+
f"{dataset_to_download}/**/*.tsv",
|
| 107 |
+
f"{dataset_to_download}/**/about.yaml",
|
| 108 |
+
f"{dataset_to_download}/**/config.yaml",
|
| 109 |
+
"available_splits.yaml"
|
| 110 |
+
]
|
| 111 |
+
else:
|
| 112 |
+
allow_patterns = [
|
| 113 |
+
f"{dataset_to_download}/{task_in_dataset}/*.tsv",
|
| 114 |
+
f"{dataset_to_download}/{task_in_dataset}/config.yaml",
|
| 115 |
+
f"{dataset_to_download}/about.yaml",
|
| 116 |
+
"available_splits.yaml"
|
| 117 |
+
]
|
| 118 |
+
|
| 119 |
+
# 3) Download the requested patterns
|
| 120 |
snapshot_download(
|
| 121 |
repo_id=repo_id,
|
| 122 |
allow_patterns=allow_patterns,
|
|
|
|
| 125 |
force_download=force_download,
|
| 126 |
)
|
| 127 |
|
| 128 |
+
# 4) Locate all .tsv files to pass to _generate_examples
|
| 129 |
search_pattern = os.path.join(local_dir, '**', '*.tsv')
|
| 130 |
all_tsv_splits = glob.glob(search_pattern, recursive=True)
|
| 131 |
|
|
|
|
| 136 |
)
|
| 137 |
]
|
| 138 |
|
|
|
|
| 139 |
def _generate_examples(self, filepath):
|
| 140 |
idx = 0
|
| 141 |
for file in filepath:
|