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71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
"""Data loading and preprocessing utilities for classification and segmentation."""
import os
import random
from typing import List, Tuple, Optional

import numpy as np
import cv2
import yaml
import torch
from torch.utils.data import Dataset, DataLoader, Subset
from sklearn.model_selection import train_test_split
import albumentations as A
from albumentations.pytorch import ToTensorV2


def load_config(config_path: str = "config.yaml") -> dict:
    """Load configuration from a YAML file.

    Args:
        config_path: Path to the YAML configuration file.

    Returns:
        Dictionary containing configuration parameters.
    """
    with open(config_path, "r", encoding="utf-8") as f:
        return yaml.safe_load(f)


def set_seed(seed: int) -> None:
    """Set random seeds for reproducibility across libraries.

    Args:
        seed: Integer seed value.
        
    """
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False


class ClassificationDataset(Dataset):
    """PyTorch Dataset for brain tumor classification.

    Expects a directory structure where each class has its own subfolder
    containing image files (JPG/PNG).
    """

    def __init__(
        self,
        root_dir: str,
        class_names: List[str],
        img_size: int = 224,
        transform: Optional[A.Compose] = None,
        phase: str = "train",
    ):
        """Initialize the classification dataset.

        Args:
            root_dir: Root directory containing class subfolders.
            class_names: Ordered list of class names.
            img_size: Target image size (square).
            transform: Albumentations composition to apply.
            phase: Dataset phase identifier (train/val/test).
        """
        self.root_dir = root_dir
        self.class_names = class_names
        self.img_size = img_size
        self.transform = transform
        self.phase = phase
        self.samples: List[Tuple[str, int]] = []
        self._build_samples()

    def _build_samples(self) -> None:
        """Populate the samples list by scanning class directories."""
        for idx, class_name in enumerate(self.class_names):
            class_dir = os.path.join(self.root_dir, class_name)
            if not os.path.isdir(class_dir):
                continue
            for fname in sorted(os.listdir(class_dir)):
                if fname.lower().endswith((".png", ".jpg", ".jpeg")):
                    self.samples.append((os.path.join(class_dir, fname), idx))

    def __len__(self) -> int:
        """Return the number of samples in the dataset."""
        return len(self.samples)

    def __getitem__(self, idx: int) -> Tuple[torch.Tensor, int]:
        """Retrieve a single image-label pair.

        Args:
            idx: Sample index.

        Returns:
            Tuple of transformed image tensor and integer label.
        """
        img_path, label = self.samples[idx]
        image = cv2.imread(img_path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = cv2.resize(image, (self.img_size, self.img_size))

        if self.transform is not None:
            augmented = self.transform(image=image)
            image = augmented["image"]

        return image, label


class SegmentationDataset(Dataset):
    """PyTorch Dataset for brain tumor segmentation.

    Recursively scans a directory for images and pairs them with masks
    identified by a configurable suffix (e.g., image_mask.png).
    """

    def __init__(
        self,
        image_dir: str,
        mask_suffix: str = "_mask",
        img_size: int = 128,
        transform: Optional[A.Compose] = None,
    ):
        """Initialize the segmentation dataset.

        Args:
            image_dir: Root directory containing images and masks.
            mask_suffix: Suffix identifying mask files.
            img_size: Target image size (square).
            transform: Albumentations composition to apply.
        """
        self.image_dir = image_dir
        self.mask_suffix = mask_suffix
        self.img_size = img_size
        self.transform = transform
        self.pairs: List[Tuple[str, str]] = []
        self._build_pairs()

    def _build_pairs(self) -> None:
        """Populate image-mask pairs by scanning the directory tree."""
        for root, _, files in os.walk(self.image_dir):
            for fname in sorted(files):
                if not fname.lower().endswith((".png", ".jpg", ".jpeg", ".tif", ".tiff")):
                    continue
                if self.mask_suffix in fname:
                    continue
                base, ext = os.path.splitext(fname)
                mask_name = f"{base}{self.mask_suffix}{ext}"
                mask_path = os.path.join(root, mask_name)
                if os.path.exists(mask_path):
                    self.pairs.append((os.path.join(root, fname), mask_path))

    def __len__(self) -> int:
        """Return the number of image-mask pairs."""
        return len(self.pairs)

    def __getitem__(self, idx: int) -> Tuple[torch.Tensor, torch.Tensor]:
        """Retrieve a single image-mask pair.

        Args:
            idx: Sample index.

        Returns:
            Tuple of transformed image tensor and binary mask tensor.
        """
        img_path, mask_path = self.pairs[idx]
        image = cv2.imread(img_path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = cv2.resize(image, (self.img_size, self.img_size))

        mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
        mask = cv2.resize(mask, (self.img_size, self.img_size))
        mask = (mask > 0).astype(np.float32)

        if self.transform is not None:
            augmented = self.transform(image=image, mask=mask)
            image = augmented["image"]
            mask = augmented["mask"].unsqueeze(0)
        else:
            image = torch.from_numpy(image.transpose(2, 0, 1)).float() / 255.0
            mask = torch.from_numpy(mask).unsqueeze(0).float()

        return image, mask


def get_classification_transforms(
    img_size: int, augmentation: dict
) -> Tuple[A.Compose, A.Compose]:
    """Create Albumentations transforms for classification.

    Args:
        img_size: Target square image size.
        augmentation: Augmentation parameters from config.

    Returns:
        Tuple of (train_transform, val_transform).
    """
    extra = []
    if augmentation.get("elastic_transform", 0) > 0:
        extra.append(A.ElasticTransform(
            alpha=1, sigma=50, p=0.5
        ))
    if augmentation.get("grid_distortion", 0) > 0:
        extra.append(A.GridDistortion(distort_limit=augmentation["grid_distortion"], p=0.5))
    if augmentation.get("optical_distortion", 0) > 0:
        extra.append(A.OpticalDistortion(
            distort_limit=augmentation["optical_distortion"], p=0.5
        ))
    if augmentation.get("gaussian_noise", 0) > 0:
        extra.append(A.GaussNoise(std_range=(0.04, 0.20), p=0.5))
    if augmentation.get("cutout", 0) > 0:
        extra.append(A.CoarseDropout(
            num_holes_range=(1, 8), hole_height_range=(0.0, 0.1), hole_width_range=(0.0, 0.1),
            p=0.5
        ))

    train_transform = A.Compose(
        [
            A.Resize(img_size, img_size),
            A.HorizontalFlip(p=0.5)
            if augmentation.get("random_flip") == "horizontal"
            else A.NoOp(),
            A.Rotate(
                limit=int(augmentation["random_rotation"] * 180), p=0.5
            ),
            A.RandomScale(
                scale_limit=augmentation["random_zoom"], p=0.5
            ),
            A.RandomBrightnessContrast(
                brightness_limit=0, contrast_limit=augmentation["random_contrast"], p=0.5
            ),
        ]
        + extra
        + [
            A.Resize(img_size, img_size),
            A.Normalize(
                mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)
            ),
            ToTensorV2(),
        ]
    )

    val_transform = A.Compose(
        [
            A.Resize(img_size, img_size),
            A.Normalize(
                mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)
            ),
            ToTensorV2(),
        ]
    )
    return train_transform, val_transform


def get_segmentation_transforms(img_size: int) -> Tuple[A.Compose, A.Compose]:
    """Create Albumentations transforms for segmentation.

    Args:
        img_size: Target square image size.

    Returns:
        Tuple of (train_transform, val_transform).
    """
    train_transform = A.Compose(
        [
            A.Resize(img_size, img_size),
            A.HorizontalFlip(p=0.5),
            A.Rotate(limit=20, p=0.5),
            A.RandomScale(scale_limit=0.1, p=0.5),
            A.RandomBrightnessContrast(
                brightness_limit=0, contrast_limit=0.1, p=0.5
            ),
            A.Resize(img_size, img_size),
            A.Normalize(
                mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)
            ),
            ToTensorV2(),
        ]
    )

    val_transform = A.Compose(
        [
            A.Resize(img_size, img_size),
            A.Normalize(
                mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)
            ),
            ToTensorV2(),
        ]
    )
    return train_transform, val_transform


def get_classification_loaders(
    config: dict, num_workers: int = 4
) -> Tuple[DataLoader, DataLoader, DataLoader, List[str]]:
    """Create train/validation/test data loaders for classification.

    Args:
        config: Loaded configuration dictionary.
        num_workers: Number of workers for data loading.

    Returns:
        Tuple of (train_loader, val_loader, test_loader, class_names).
    """
    set_seed(config["seed"])

    data_dir = config["paths"]["data_classification"]
    class_names = config["classification"]["class_names"]
    img_size = config["classification"]["img_size"]
    batch_size = config["classification"]["batch_size"]
    aug = config["classification"]["augmentation"]
    val_split = config["classification"].get("val_split", 0.1)

    train_transform, val_transform = get_classification_transforms(
        img_size, aug
    )

    full_train = ClassificationDataset(
        os.path.join(data_dir, "Training"),
        class_names,
        img_size,
        train_transform,
        "train",
    )
    test_dataset = ClassificationDataset(
        os.path.join(data_dir, "Testing"),
        class_names,
        img_size,
        val_transform,
        "test",
    )

    indices = list(range(len(full_train)))
    labels = [label for _, label in full_train.samples]
    train_indices, val_indices = train_test_split(
        indices,
        test_size=val_split,
        random_state=config["seed"],
        stratify=labels,
    )

    train_dataset = Subset(full_train, train_indices)
    val_dataset = ClassificationDataset(
        os.path.join(data_dir, "Training"),
        class_names,
        img_size,
        val_transform,
        "val",
    )
    val_dataset.samples = [full_train.samples[i] for i in val_indices]

    train_loader = DataLoader(
        train_dataset,
        batch_size=batch_size,
        shuffle=True,
        num_workers=num_workers,
        pin_memory=True,
    )
    val_loader = DataLoader(
        val_dataset,
        batch_size=batch_size,
        shuffle=False,
        num_workers=num_workers,
        pin_memory=True,
    )
    test_loader = DataLoader(
        test_dataset,
        batch_size=batch_size,
        shuffle=False,
        num_workers=num_workers,
        pin_memory=True,
    )

    return train_loader, val_loader, test_loader, class_names


def get_segmentation_loaders(
    config: dict, num_workers: int = 4
) -> Tuple[DataLoader, DataLoader]:
    """Create train/validation data loaders for segmentation.

    Args:
        config: Loaded configuration dictionary.
        num_workers: Number of workers for data loading.

    Returns:
        Tuple of (train_loader, val_loader).
    """
    set_seed(config["seed"])

    data_dir = config["paths"]["data_segmentation"]
    img_size = config["segmentation"]["img_size"]
    batch_size = config["segmentation"]["batch_size"]
    mask_suffix = config["segmentation"].get("mask_suffix", "_mask")
    val_split = config["segmentation"].get("val_split", 0.2)

    train_transform, val_transform = get_segmentation_transforms(img_size)
    full_dataset = SegmentationDataset(
        data_dir, mask_suffix=mask_suffix, img_size=img_size, transform=train_transform
    )

    indices = list(range(len(full_dataset)))
    train_indices, val_indices = train_test_split(
        indices, test_size=val_split, random_state=config["seed"]
    )

    train_dataset = Subset(full_dataset, train_indices)
    val_dataset = SegmentationDataset(
        data_dir,
        mask_suffix=mask_suffix,
        img_size=img_size,
        transform=val_transform,
    )
    val_dataset.pairs = [full_dataset.pairs[i] for i in val_indices]

    train_loader = DataLoader(
        train_dataset,
        batch_size=batch_size,
        shuffle=True,
        num_workers=num_workers,
        pin_memory=True,
    )
    val_loader = DataLoader(
        val_dataset,
        batch_size=batch_size,
        shuffle=False,
        num_workers=num_workers,
        pin_memory=True,
    )

    return train_loader, val_loader