Chris Oswald
commited on
Commit
·
d004e7b
1
Parent(s):
5fa2de2
added parameter checking
Browse files
SPIDER.py
CHANGED
|
@@ -170,13 +170,15 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 170 |
def _generate_examples(
|
| 171 |
self,
|
| 172 |
paths_dict: Dict[str, str],
|
| 173 |
-
|
|
|
|
| 174 |
validate_share: float = 0.3,
|
| 175 |
test_share: float = 0.2,
|
| 176 |
raw_image: bool = True,
|
| 177 |
numeric_array: bool = True,
|
| 178 |
metadata: bool = True,
|
| 179 |
rad_gradings: bool = True,
|
|
|
|
| 180 |
) -> Tuple[str, Dict]:
|
| 181 |
"""
|
| 182 |
This method handles input defined in _split_generators to yield
|
|
@@ -185,6 +187,7 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 185 |
|
| 186 |
Args
|
| 187 |
paths_dict
|
|
|
|
| 188 |
split:
|
| 189 |
validate_share
|
| 190 |
test_share
|
|
@@ -194,24 +197,47 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 194 |
rad_gradings
|
| 195 |
|
| 196 |
Yields
|
| 197 |
-
|
| 198 |
"""
|
| 199 |
-
#
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
# Generate train/validate/test partitions of patient IDs
|
| 212 |
partition = np.random.choice(
|
| 213 |
['train', 'dev', 'test'],
|
| 214 |
-
p=[
|
| 215 |
size=N_PATIENTS,
|
| 216 |
)
|
| 217 |
patient_ids = (np.arange(N_PATIENTS) + 1)
|
|
@@ -270,12 +296,6 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 270 |
subset_ids = validate_ids
|
| 271 |
elif split == 'test':
|
| 272 |
subset_ids = test_ids
|
| 273 |
-
else:
|
| 274 |
-
subset_ids = None
|
| 275 |
-
raise ValueError( #TODO: move all parameter checking to beginning
|
| 276 |
-
f'Split argument "{split}" is not recognized. \
|
| 277 |
-
Please enter one of ["train", "validate", "test"]'
|
| 278 |
-
)
|
| 279 |
|
| 280 |
image_files = [
|
| 281 |
file for file in image_files
|
|
@@ -313,11 +333,14 @@ class SPIDER(datasets.GeneratorBasedBuilder):
|
|
| 313 |
# Extract overview data corresponding to image
|
| 314 |
image_overview = overview_dict[scan_id]
|
| 315 |
|
| 316 |
-
# Extract patient radiological gradings corresponding to
|
| 317 |
patient_grades_dict = {}
|
| 318 |
for item in grades_dict[patient_id]:
|
| 319 |
key = f'IVD{item["IVD label"]}'
|
| 320 |
-
value = {
|
|
|
|
|
|
|
|
|
|
| 321 |
patient_grades_dict[key] = value
|
| 322 |
|
| 323 |
# Prepare example return dict
|
|
|
|
| 170 |
def _generate_examples(
|
| 171 |
self,
|
| 172 |
paths_dict: Dict[str, str],
|
| 173 |
+
scan_types: List[str] = ['t1', 't2', 't2_SPACE'],
|
| 174 |
+
split: str = 'train',
|
| 175 |
validate_share: float = 0.3,
|
| 176 |
test_share: float = 0.2,
|
| 177 |
raw_image: bool = True,
|
| 178 |
numeric_array: bool = True,
|
| 179 |
metadata: bool = True,
|
| 180 |
rad_gradings: bool = True,
|
| 181 |
+
random_seed: int = 9999,
|
| 182 |
) -> Tuple[str, Dict]:
|
| 183 |
"""
|
| 184 |
This method handles input defined in _split_generators to yield
|
|
|
|
| 187 |
|
| 188 |
Args
|
| 189 |
paths_dict
|
| 190 |
+
scan_types:
|
| 191 |
split:
|
| 192 |
validate_share
|
| 193 |
test_share
|
|
|
|
| 197 |
rad_gradings
|
| 198 |
|
| 199 |
Yields
|
| 200 |
+
Tuple (unique patient-scan ID, dict of
|
| 201 |
"""
|
| 202 |
+
# Set constants
|
| 203 |
+
N_PATIENTS = 257
|
| 204 |
+
train_share = (1.0 - validate_share - test_share)
|
| 205 |
+
np.random.seed(int(random_seed))
|
| 206 |
+
|
| 207 |
+
# Validate params
|
| 208 |
+
for item in scan_types:
|
| 209 |
+
if item not in ['t1', 't2', 't2_SPACE']:
|
| 210 |
+
raise ValueError(
|
| 211 |
+
'Scan type "{item}" not recognized as valid scan type.\
|
| 212 |
+
Verify scan type argument.'
|
| 213 |
+
)
|
| 214 |
+
if split not in ['train', 'validate', 'test']:
|
| 215 |
+
raise ValueError(
|
| 216 |
+
f'Split argument "{split}" is not recognized. \
|
| 217 |
+
Please enter one of ["train", "validate", "test"]'
|
| 218 |
+
)
|
| 219 |
+
if train_share <= 0.0:
|
| 220 |
+
raise ValueError(
|
| 221 |
+
f'Training share is calculated as (1 - validate_share - test_share) \
|
| 222 |
+
and must be greater than 0. Current calculated value is \
|
| 223 |
+
{round(train_share, 3)}. Adjust validate_share and/or \
|
| 224 |
+
test_share parameters.'
|
| 225 |
+
)
|
| 226 |
+
if validate_share > 1.0 or validate_share < 0.0:
|
| 227 |
+
raise ValueError(
|
| 228 |
+
f'Validation share must be between (0, 1). Current value is \
|
| 229 |
+
{validate_share}.'
|
| 230 |
+
)
|
| 231 |
+
if test_share > 1.0 or test_share < 0.0:
|
| 232 |
+
raise ValueError(
|
| 233 |
+
f'Testing share must be between (0, 1). Current value is \
|
| 234 |
+
{test_share}.'
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
# Generate train/validate/test partitions of patient IDs
|
| 238 |
partition = np.random.choice(
|
| 239 |
['train', 'dev', 'test'],
|
| 240 |
+
p=[train_share, validate_share, test_share],
|
| 241 |
size=N_PATIENTS,
|
| 242 |
)
|
| 243 |
patient_ids = (np.arange(N_PATIENTS) + 1)
|
|
|
|
| 296 |
subset_ids = validate_ids
|
| 297 |
elif split == 'test':
|
| 298 |
subset_ids = test_ids
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
image_files = [
|
| 301 |
file for file in image_files
|
|
|
|
| 333 |
# Extract overview data corresponding to image
|
| 334 |
image_overview = overview_dict[scan_id]
|
| 335 |
|
| 336 |
+
# Extract patient radiological gradings corresponding to patient
|
| 337 |
patient_grades_dict = {}
|
| 338 |
for item in grades_dict[patient_id]:
|
| 339 |
key = f'IVD{item["IVD label"]}'
|
| 340 |
+
value = {
|
| 341 |
+
k:v for k,v in item.items()
|
| 342 |
+
if k not in ['Patient', 'IVD label']
|
| 343 |
+
}
|
| 344 |
patient_grades_dict[key] = value
|
| 345 |
|
| 346 |
# Prepare example return dict
|