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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import torch
import pickle
import numpy as np
from torch.utils.data import Dataset
from typing import Optional, Callable, Tuple
import random
# from dataload_utils import (default_loader, default_loader_test)
from dataLoder.dataload_utils import (default_loader, default_loader_test)

np.random.seed(1337)
random.seed(1337)

def get_fold_data(fold_number: int, mode: str, pickle_path: str) -> Tuple[list, list]:
    """
    Load data for a specific fold and mode from a PKL file.

    Parameters:
    - mode (str): Either 'train' or 'test'.
    - fold_number (int): An integer between 0 and 4 indicating the fold index.
    - pickle_path (str): The file path to the PKL file.

    Returns:
    - Tuple containing:
        - paths (list): List of file paths for the specified fold.
        - class_labels (list): List of class labels corresponding to the file paths.

    Raises:
    - ValueError: If mode is not 'train' or 'test', or if fold_number is out of range.
    - FileNotFoundError: If the PKL file does not exist.
    - ValueError: If the PKL file cannot be unpickled.
    - KeyError: If the expected data structure is not found in the PKL file.
    """
    # Validate mode
    if mode not in ['train', 'test']:
        raise ValueError("mode must be 'train' or 'test'")

    # Validate fold_number
    if not isinstance(fold_number, int) or not (0 <= fold_number <= 4):
        raise ValueError("fold_number must be an integer between 0 and 4")

    # Attempt to load data from the PKL file
    try:
        with open(pickle_path, 'rb') as file:
            data = pickle.load(file)
    except FileNotFoundError:
        raise FileNotFoundError(f"PKL file not found: {pickle_path}")
    except pickle.UnpicklingError:
        raise ValueError(f"Unable to load PKL file: {pickle_path}")

    # Construct the fold key
    fold_key = f'fold{fold_number}'

    # Retrieve data for the specified mode and fold
    try:
        fold_data = data[mode][fold_key]
        return fold_data['path'], fold_data['cls_label']
    except KeyError:
        raise KeyError(f"Missing {mode} data for {fold_key} in the data structure")

def split_path_by_label(file_paths, file_labels):
    file_paths_0 = []
    file_paths_1 = []
    for i in range(len(file_labels)):
        if file_labels[i]==0:
            file_paths_0.append(file_paths[i])
        elif file_labels[i]==1:
            file_paths_1.append(file_paths[i])
    return file_paths_0, file_paths_1

class TrainDataLoader_balance(Dataset):
    def __init__(
        self,
        transform: Optional[Callable] = None,
        mode: str = 'train',
        fold: int = 1,
        random_flag: bool = True,
        pickle_path: str = ''
    ):
        """
        Initialize the training data loader.

        Parameters:
        - transform (Callable, optional): Transformation to apply to the images and masks.
        - mode (str): Mode of data loading, default is 'train'.
        - fold (int): Fold number to load data from.
        - random_flag (bool): If True, randomly select tensors; otherwise, use all.
        - pickle_path (str): Path to the PKL file containing data.
        """
        self.random_flag = random_flag
        self.mode = mode
        self.transform = transform

        # Load file paths and labels for the specified fold and mode
        file_paths, file_labels = get_fold_data(fold, mode, pickle_path)
        self.file_paths_0, self.file_paths_1 = split_path_by_label(file_paths, file_labels)

    def __len__(self) -> int:
        """Return the total number of samples."""
        return max(len(self.file_paths_0), len(self.file_paths_1))

    def get_match_tensor(
        self,
        images: list,
        masks: list
    ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
        """
        Generate matched tensors from images and masks.

        Parameters:
        - images (list): List of image tensors.
        - masks (list): List of mask tensors.

        Returns:
        - Tuple containing:
            - IT1 (Tensor)
            - IT2 (Tensor)
            - IT3 (Tensor)
            - INT1 (Tensor)
            - INT2 (Tensor)
        """
        # Apply transformations if provided
        if self.transform is not None:
            images = [self.transform(img) for img in images]
            masks = [self.transform(mask) for mask in masks]

        # Unpack masks
        mask_original, mask_positive, mask_negative = masks

        # Apply original mask to images and concatenate along a new dimension
        images_tensor = torch.cat([
            (images[0] * mask_original).unsqueeze(0),
            (images[1] * mask_original).unsqueeze(0)
        ], dim=0)

        # Create various tensor combinations
        IT1 = torch.cat([images_tensor, mask_original.unsqueeze(0)], dim=0)
        IT2 = torch.cat([images_tensor, mask_positive.unsqueeze(0)], dim=0)
        IT3 = torch.cat([images_tensor * mask_positive.unsqueeze(0), mask_original.unsqueeze(0)], dim=0)

        INT1 = torch.cat([images_tensor * mask_negative.unsqueeze(0), mask_original.unsqueeze(0)], dim=0)
        INT2 = torch.cat([images_tensor, mask_negative.unsqueeze(0)], dim=0)

        return IT1, IT2, IT3, INT1, INT2

    def get_match_out(self, IT1, IT2, IT3, INT1, INT2, class_label):
        if self.random_flag:
            # Randomly select one IT tensor
            imgs_IT = random.choice([IT1, IT2, IT3]).unsqueeze(0)
            # Randomly select one INT tensor
            imgs_INT = random.choice([INT1, INT2]).unsqueeze(0)
            # Convert class label to tensor
            class_label_tensor = torch.tensor(class_label, dtype=torch.long).unsqueeze(0)
        else:
            # Concatenate all IT and INT tensors
            imgs_IT = torch.cat([IT1, IT2, IT3], dim=0)
            imgs_INT = torch.cat([INT1, INT2], dim=0)
            # Repeat class labels for each tensor
            class_label_tensor = torch.tensor([class_label, class_label, class_label], dtype=torch.long)

        # Determine the number of IT and INT samples
        num_IT = imgs_IT.size(0)
        num_INT = imgs_INT.size(0)

        # Create NT labels: IT samples as 1, INT samples as 0
        label_NT_IT = torch.ones(num_IT, dtype=torch.long)    # IT class label
        label_NT_INT = torch.zeros(num_INT, dtype=torch.long)  # INT class label

        # Concatenate NT labels
        label_NT = torch.cat((label_NT_IT, label_NT_INT), dim=0)
        # Concatenate image tensors
        images_combined = torch.cat([imgs_IT, imgs_INT], dim=0)
        # Concatenate class labels with INT labels
        labels_combined = torch.cat([class_label_tensor, label_NT_INT], dim=0)
        return images_combined.float(), label_NT, labels_combined

    def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
        """
        Retrieve a sample from the dataset.

        Parameters:
        - index (int): Index of the sample to retrieve.

        Returns:
        - Tuple containing:
            - images (Tensor): Image tensors.
            - label_NT (Tensor): NT labels.
            - labels (Tensor): Combined labels.
        """

        # Safely get index 0
        if len(self.file_paths_0) == 0:
            path_0 = self.file_paths_1[random.randint(0, len(self.file_paths_1) - 1)]
        else:
            path_0 = self.file_paths_0[index % len(self.file_paths_0)]

        # Safely get index 1
        if len(self.file_paths_1) == 0:
            path_1 = self.file_paths_0[random.randint(0, len(self.file_paths_0) - 1)]
        else:
            path_1 = self.file_paths_1[(index + random.randint(0, len(self.file_paths_1) - 1)) % len(self.file_paths_1)]

        # Load images and masks using a default loader
        images_0, masks_0 = default_loader(path_0)
        images_1, masks_1 = default_loader(path_1)

        # Generate matched tensors
        IT1_0, IT2_0, IT3_0, INT1_0, INT2_0 = self.get_match_tensor(images_0, masks_0)
        IT1_1, IT2_1, IT3_1, INT1_1, INT2_1 = self.get_match_tensor(images_1, masks_1)

        # Adjust class label (shift by 1 for classification)
        class_label = 0
        images_combined_0, label_NT_0, labels_combined_0 = self.get_match_out(IT1_0, IT2_0, IT3_0,
                                                                              INT1_0, INT2_0, class_label + 1)
        # Adjust class label (shift by 1 for classification)
        class_label = 1
        images_combined_1, label_NT_1, labels_combined_1 = self.get_match_out(IT1_1, IT2_1, IT3_1,
                                                                              INT1_1, INT2_1, class_label + 1)

        images_combined = torch.cat([images_combined_0, images_combined_1], dim=0)
        label_NT = torch.cat([label_NT_0, label_NT_1], dim=0)
        labels_combined = torch.cat([labels_combined_0, labels_combined_1], dim=0)
        return images_combined, label_NT, labels_combined


class TrainDataLoader(Dataset):
    def __init__(
        self,
        transform: Optional[Callable] = None,
        mode: str = 'train',
        fold: int = 1,
        random_flag: bool = True,
        pickle_path: str = ''
    ):
        """
        Initialize the training data loader.

        Parameters:
        - transform (Callable, optional): Transformation to apply to the images and masks.
        - mode (str): Mode of data loading, default is 'train'.
        - fold (int): Fold number to load data from.
        - random_flag (bool): If True, randomly select tensors; otherwise, use all.
        - pickle_path (str): Path to the PKL file containing data.
        """
        self.random_flag = random_flag
        self.mode = mode
        self.transform = transform

        # Load file paths and labels for the specified fold and mode
        self.file_paths, self.file_labels = get_fold_data(fold, mode, pickle_path)

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

    def get_match_tensor(
        self,
        images: list,
        masks: list
    ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
        """
        Generate matched tensors from images and masks.

        Parameters:
        - images (list): List of image tensors.
        - masks (list): List of mask tensors.

        Returns:
        - Tuple containing:
            - IT1 (Tensor)
            - IT2 (Tensor)
            - IT3 (Tensor)
            - INT1 (Tensor)
            - INT2 (Tensor)
        """
        # Apply transformations if provided
        if self.transform is not None:
            images = [self.transform(img) for img in images]
            masks = [self.transform(mask) for mask in masks]

        # Unpack masks
        mask_original, mask_positive, mask_negative = masks

        # Apply original mask to images and concatenate along a new dimension
        images_tensor = torch.cat([
            (images[0] * mask_original).unsqueeze(0),
            (images[1] * mask_original).unsqueeze(0)
        ], dim=0)

        # Create various tensor combinations
        IT1 = torch.cat([images_tensor, mask_original.unsqueeze(0)], dim=0)
        IT2 = torch.cat([images_tensor, mask_positive.unsqueeze(0)], dim=0)
        IT3 = torch.cat([images_tensor * mask_positive.unsqueeze(0), mask_original.unsqueeze(0)], dim=0)

        INT1 = torch.cat([images_tensor * mask_negative.unsqueeze(0), mask_original.unsqueeze(0)], dim=0)
        INT2 = torch.cat([images_tensor, mask_negative.unsqueeze(0)], dim=0)

        return IT1, IT2, IT3, INT1, INT2

    def get_match_out(self, IT1, IT2, IT3, INT1, INT2, class_label):
        if self.random_flag:
            # Randomly select one IT tensor
            imgs_IT = random.choice([IT1, IT2, IT3]).unsqueeze(0)
            # Randomly select one INT tensor
            imgs_INT = random.choice([INT1, INT2]).unsqueeze(0)
            # Convert class label to tensor
            class_label_tensor = torch.tensor(class_label, dtype=torch.long).unsqueeze(0)
        else:
            # Concatenate all IT and INT tensors
            imgs_IT = torch.cat([IT1, IT2, IT3], dim=0)
            imgs_INT = torch.cat([INT1, INT2], dim=0)
            # Repeat class labels for each tensor
            class_label_tensor = torch.tensor([class_label, class_label, class_label], dtype=torch.long)

        # Determine the number of IT and INT samples
        num_IT = imgs_IT.size(0)
        num_INT = imgs_INT.size(0)

        # Create NT labels: IT samples as 1, INT samples as 0
        label_NT_IT = torch.ones(num_IT, dtype=torch.long)    # IT class label
        label_NT_INT = torch.zeros(num_INT, dtype=torch.long)  # INT class label

        # Concatenate NT labels
        label_NT = torch.cat((label_NT_IT, label_NT_INT), dim=0)
        # Concatenate image tensors
        images_combined = torch.cat([imgs_IT, imgs_INT], dim=0)
        # Concatenate class labels with INT labels
        labels_combined = torch.cat([class_label_tensor, label_NT_INT], dim=0)
        return images_combined.float(), label_NT, labels_combined

    def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
        """
        Retrieve a sample from the dataset.

        Parameters:
        - index (int): Index of the sample to retrieve.

        Returns:
        - Tuple containing:
            - images (Tensor): Image tensors.
            - label_NT (Tensor): NT labels.
            - labels (Tensor): Combined labels.
        """
        # Load images and masks using a default loader
        images, masks = default_loader(self.file_paths[index])
        class_label = self.file_labels[index]

        # Generate matched tensors
        IT1, IT2, IT3, INT1, INT2 = self.get_match_tensor(images, masks)

        # Adjust class label (shift by 1 for classification)
        images_combined, label_NT, labels_combined = self.get_match_out(IT1, IT2, IT3, INT1, INT2, class_label + 1)
        return images_combined, label_NT, labels_combined

class TestDataLoader(Dataset):
    def __init__(
        self,
        transform: Optional[Callable] = None,
        mode: str = 'test',
        fold: int = 1,
        random_flag: bool = False,
        pickle_path: str = ''
    ):
        """
        Initialize the testing data loader.

        Parameters:
        - transform (Callable, optional): Transformation to apply to the images and masks.
        - mode (str): Mode of data loading, default is 'test'.
        - fold (int): Fold number to load data from.
        - random_flag (bool): If True, randomly select tensors; otherwise, use all.
        - pickle_path (str): Path to the PKL file containing data.
        """
        self.random_flag = random_flag
        self.mode = mode
        self.transform = transform

        # Load file paths and labels for the specified fold and mode
        self.file_paths, self.file_labels = get_fold_data(fold, mode, pickle_path)

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

    def get_match_tensor(
        self,
        images: list,
        masks: list
    ) -> torch.Tensor:
        """
        Generate matched tensor from images and masks for testing.

        Parameters:
        - images (list): List of image tensors.
        - masks (list): List of mask tensors.

        Returns:
        - IT1 (Tensor)
        """
        # Apply transformations if provided
        if self.transform is not None:
            images = [self.transform(img) for img in images]
            masks = [self.transform(mask) for mask in masks]

        # Use only the original mask for testing
        mask_original = masks[0]

        # Apply original mask to images and concatenate along a new dimension
        images_tensor = torch.cat([
            (images[0] * mask_original).unsqueeze(0),
            (images[1] * mask_original).unsqueeze(0)
        ], dim=0)

        # Create IT1 tensor
        IT1 = torch.cat([images_tensor, mask_original.unsqueeze(0)], dim=0)

        return IT1

    def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor]:
        """
        Retrieve a sample from the test dataset.

        Parameters:
        - index (int): Index of the sample to retrieve.

        Returns:
        - Tuple containing:
            - images_IT (Tensor): Image IT tensors.
            - class_label (Tensor): Class label.
        """
        # Load images and masks using a default test loader
        images, masks = default_loader_test(self.file_paths[index])
        class_label = self.file_labels[index]

        # Generate matched tensor
        images_IT = self.get_match_tensor(images, masks)

        # Convert class label to tensor
        class_label_tensor = torch.tensor(class_label, dtype=torch.long)

        return images_IT.float(), class_label_tensor.long()


if __name__ == '__main__':
    import monai
    from torch.utils.data import DataLoader

    pkl_path = './CMTLNet_Path/1p19q.pkl'
    pkl_path = './CMTLNet_Path/LHG.pkl'
    pkl_path = './CMTLNet_Path/IDH.pkl'

    # Define the transformation for the validation stage
    validation_transforms = monai.transforms.Compose([
        monai.transforms.ToTensor(),
    ])

    # Initialize the training dataset with transformation and data reading type
    training_dataset = TrainDataLoader_balance(read_type='train', fold=0, transform=validation_transforms,
                                       random_flag=False, pkl_path=pkl_path)

    # Create the training data loader
    training_loader = DataLoader(training_dataset,
                                 batch_size=1,  # One sample per batch
                                 shuffle=True,  # Shuffle the data order
                                 drop_last=True,  # Drop the last incomplete batch
                                 num_workers=1,  # Number of worker threads for data loading
                                 pin_memory=False)  # Whether to pin memory for CUDA

    # Output the size of the training dataset
    print("Number of training samples: {}".format(len(training_dataset)))

    # Loop through each batch in the data loader
    for batch_data in training_loader:
        # Unpack the data for each batch
        imgs_IT, imgs_INT, cls_label = batch_data