minhanhto09 commited on
Commit
f64c774
1 Parent(s): cf3a022

Update BuilderConfig

Browse files
Files changed (1) hide show
  1. NuCLS_dataset.py +35 -7
NuCLS_dataset.py CHANGED
@@ -23,7 +23,7 @@ Created on Tue Mar 12 16:13:56 2024
23
  import pandas as pd
24
  from PIL import Image as PilImage # Import PIL Image with an alias
25
  import datasets
26
- from datasets import DatasetBuilder, GeneratorBasedBuilder, DownloadManager, DatasetInfo, Features, Image, ClassLabel, Value, Sequence, load_dataset, SplitGenerator
27
  import os
28
  import io
29
  from typing import Tuple, Dict, List
@@ -51,10 +51,29 @@ _LICENSE = "CC0 1.0 license"
51
 
52
  _URL = "https://www.dropbox.com/scl/fi/zsm9l3bkwx808wfryv5zm/NuCLS_dataset.zip?rlkey=x3358slgrxt00zpn7zpkpjr2h&dl=1"
53
 
54
- class NuCLSDataset(GeneratorBasedBuilder):
55
- """The NuCLS dataset."""
56
 
57
- VERSION = datasets.Version("1.1.0")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  def _info(self):
60
  """Returns the dataset info."""
@@ -118,7 +137,12 @@ class NuCLSDataset(GeneratorBasedBuilder):
118
  unique_filenames = [os.path.splitext(f)[0] for f in os.listdir(rgb_dir)]
119
 
120
  # Process train/test split files to get slide names for each split and fold
121
- split_slide_names = self._process_train_test_split_files(split_dir)
 
 
 
 
 
122
 
123
  # Create the split generators for each fold
124
  split_generators = []
@@ -149,13 +173,17 @@ class NuCLSDataset(GeneratorBasedBuilder):
149
 
150
  return split_generators
151
 
152
- def _process_train_test_split_files(self, split_dir):
153
  """Reads the train/test split CSV files and returns a dictionary with fold numbers as keys and tuple of train/test slide names as values."""
 
154
  split_slide_names = {}
155
  for split_file in os.listdir(split_dir):
 
 
 
 
156
  file_path = os.path.join(split_dir, split_file)
157
  fold_number = split_file.split('_')[1] # Assumes file naming format "fold_X_[train/test].csv"
158
-
159
  with open(file_path, 'r') as f:
160
  csv_reader = csv.reader(f)
161
  next(csv_reader) # Skip header
 
23
  import pandas as pd
24
  from PIL import Image as PilImage # Import PIL Image with an alias
25
  import datasets
26
+ from datasets import DatasetBuilder, GeneratorBasedBuilder, DownloadManager, DatasetInfo, Features, Image, ClassLabel, Value, Sequence, load_dataset, SplitGenerator, BuilderConfig
27
  import os
28
  import io
29
  from typing import Tuple, Dict, List
 
51
 
52
  _URL = "https://www.dropbox.com/scl/fi/zsm9l3bkwx808wfryv5zm/NuCLS_dataset.zip?rlkey=x3358slgrxt00zpn7zpkpjr2h&dl=1"
53
 
 
 
54
 
55
+ class NuCLSDatasetConfig(BuilderConfig):
56
+ def __init__(self, use_fold_999=False, **kwargs):
57
+ super(NuCLSDatasetConfig, self).__init__(**kwargs)
58
+ self.use_fold_999 = use_fold_999
59
+
60
+ class NuCLSDataset(GeneratorBasedBuilder):
61
+ # Define multiple configurations for your dataset
62
+ BUILDER_CONFIGS = [
63
+ NuCLSDatasetConfig(
64
+ name="default",
65
+ version=datasets.Version("1.1.0"),
66
+ description="Default configuration with the full dataset",
67
+ ),
68
+ NuCLSDatasetConfig(
69
+ name="debug",
70
+ version=datasets.Version("1.1.0"),
71
+ description="Debug configuration which uses fold 999 for quick tests",
72
+ use_fold_999=True
73
+ ),
74
+ ]
75
+
76
+ DEFAULT_CONFIG_NAME = "default"
77
 
78
  def _info(self):
79
  """Returns the dataset info."""
 
137
  unique_filenames = [os.path.splitext(f)[0] for f in os.listdir(rgb_dir)]
138
 
139
  # Process train/test split files to get slide names for each split and fold
140
+ if self.config.use_fold_999:
141
+ # Generate the split generators for fold 999
142
+ split_slide_names = self._process_train_test_split_files(split_dir, specific_fold = '999')
143
+ else:
144
+ # Generate the split generators for all folds
145
+ split_slide_names = self._process_train_test_split_files(split_dir)
146
 
147
  # Create the split generators for each fold
148
  split_generators = []
 
173
 
174
  return split_generators
175
 
176
+ def _process_train_test_split_files(self, split_dir, specific_fold=None):
177
  """Reads the train/test split CSV files and returns a dictionary with fold numbers as keys and tuple of train/test slide names as values."""
178
+
179
  split_slide_names = {}
180
  for split_file in os.listdir(split_dir):
181
+ fold_number = split_file.split('_')[1] # Assumes file naming format "fold_X_[train/test].csv"
182
+ # If specific_fold is set, skip all other folds
183
+ if specific_fold is not None and fold_number != specific_fold:
184
+ continue
185
  file_path = os.path.join(split_dir, split_file)
186
  fold_number = split_file.split('_')[1] # Assumes file naming format "fold_X_[train/test].csv"
 
187
  with open(file_path, 'r') as f:
188
  csv_reader = csv.reader(f)
189
  next(csv_reader) # Skip header