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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
"""

Ablation Study Framework for Brain Tumor Detection Models

"""

import numpy as np
import tensorflow as tf
import json
import os
import pandas as pd
from datetime import datetime
from pathlib import Path
import matplotlib.pyplot as plt
import seaborn as sns


class AblationStudy:
    """

    Framework for conducting ablation studies on brain tumor detection models

    """
    
    def __init__(self, base_config, results_dir='./ablation_results'):
        """

        Initialize ablation study

        

        Args:

            base_config: Base configuration dictionary

            results_dir: Directory to save results

        """
        self.base_config = base_config
        self.results_dir = results_dir
        self.results = {}
        self.study_metadata = {
            'start_time': datetime.now().isoformat(),
            'base_config': base_config,
            'experiments': []
        }
        
        os.makedirs(results_dir, exist_ok=True)
    
    def add_experiment(self, name, config_modification, description=""):
        """

        Add an experiment configuration

        

        Args:

            name: Name of the experiment

            config_modification: Dictionary of config modifications

            description: Description of what this experiment tests

        """
        config = self.base_config.copy()
        config.update(config_modification)
        
        self.study_metadata['experiments'].append({
            'name': name,
            'config': config,
            'description': description,
            'modifications': config_modification
        })
    
    def run_experiment(self, experiment_idx, model_builder, data, metrics_calculator):
        """

        Run a single ablation experiment

        

        Args:

            experiment_idx: Index of experiment in study_metadata['experiments']

            model_builder: Function to build model with given config

            data: Tuple of (X_train, y_train, X_val, y_val)

            metrics_calculator: Function to calculate metrics

            

        Returns:

            Results dictionary

        """
        experiment = self.study_metadata['experiments'][experiment_idx]
        config = experiment['config']
        name = experiment['name']
        
        print(f"\n{'='*60}")
        print(f"Running Ablation Experiment: {name}")
        print(f"{'='*60}")
        print(f"Description: {experiment['description']}")
        print(f"Modifications: {experiment['modifications']}")
        
        # Build model
        model = model_builder(config)
        
        # Train model
        history = self._train_model(model, data, config)
        
        # Evaluate
        metrics = metrics_calculator(model, data)
        
        # Store results
        self.results[name] = {
            'metrics': metrics,
            'history': history.history if hasattr(history, 'history') else history,
            'config': config
        }
        
        print(f"Experiment {name} completed. Metrics: {metrics}")
        
        return self.results[name]
    
    def _train_model(self, model, data, config):
        """

        Train model with given configuration

        

        Args:

            model: Model to train

            data: Training data tuple

            config: Training configuration

            

        Returns:

            Training history

        """
        X_train, y_train, X_val, y_val = data
        
        # Compile model
        model.compile(
            optimizer=tf.keras.optimizers.Adam(learning_rate=config.get('learning_rate', 1e-4)),
            loss=config.get('loss_fn', 'binary_crossentropy'),
            metrics=config.get('metrics', ['accuracy'])
        )
        
        # Callbacks
        callbacks = [
            tf.keras.callbacks.EarlyStopping(
                monitor='val_loss',
                patience=config.get('patience', 10),
                restore_best_weights=True
            ),
            tf.keras.callbacks.ReduceLROnPlateau(
                monitor='val_loss',
                factor=0.5,
                patience=5,
                min_lr=1e-7
            )
        ]
        
        # Train
        history = model.fit(
            X_train,
            y_train,
            validation_data=(X_val, y_val),
            epochs=config.get('epochs', 50),
            batch_size=config.get('batch_size', 32),
            callbacks=callbacks,
            verbose=1
        )
        
        return history
    
    def run_all_experiments(self, model_builder, data, metrics_calculator):
        """

        Run all experiments in the study

        

        Args:

            model_builder: Function to build model with given config

            data: Training data tuple

            metrics_calculator: Function to calculate metrics

        """
        for i in range(len(self.study_metadata['experiments'])):
            self.run_experiment(i, model_builder, data, metrics_calculator)
        
        # Save results
        self.save_results()
        
        return self.results
    
    def save_results(self):
        """Save ablation study results"""
        # Save summary
        summary_path = os.path.join(self.results_dir, 'ablation_summary.json')
        with open(summary_path, 'w') as f:
            json.dump({
                'study_metadata': self.study_metadata,
                'results': self.results
            }, f, indent=2)
        
        # Save detailed results as CSV
        results_data = []
        for name, result in self.results.items():
            row = {'experiment': name}
            row.update(result['metrics'])
            results_data.append(row)
        
        results_df = pd.DataFrame(results_data)
        results_df.to_csv(os.path.join(self.results_dir, 'ablation_results.csv'), index=False)
        
        # Save plots
        self.plot_results()
        
        print(f"Results saved to {self.results_dir}")
    
    def plot_results(self):
        """Plot ablation study results"""
        if not self.results:
            return
        
        # Extract metrics
        experiments = list(self.results.keys())
        metrics_names = list(list(self.results.values())[0]['metrics'].keys())
        
        # Create subplots for each metric
        fig, axes = plt.subplots(1, len(metrics_names), figsize=(6*len(metrics_names), 5))
        if len(metrics_names) == 1:
            axes = [axes]
        
        for ax, metric_name in zip(axes, metrics_names):
            values = [self.results[exp]['metrics'][metric_name] for exp in experiments]
            
            # Create bar plot
            bars = ax.bar(experiments, values, color=plt.cm.Set3(np.linspace(0, 1, len(experiments))))
            ax.set_title(metric_name.replace('_', ' ').title())
            ax.set_ylabel(metric_name)
            ax.tick_params(axis='x', rotation=45)
            
            # Add value labels on bars
            for bar, value in zip(bars, values):
                ax.text(bar.get_x() + bar.get_width()/2., bar.get_height() + 0.001,
                       f'{value:.4f}', ha='center', va='bottom', fontsize=9)
        
        plt.tight_layout()
        plt.savefig(os.path.join(self.results_dir, 'ablation_results.png'), dpi=300, bbox_inches='tight')
        plt.close()
        
        # Plot training histories
        if len(experiments) > 0 and 'history' in self.results[experiments[0]]:
            self._plot_training_histories(experiments)
    
    def _plot_training_histories(self, experiments):
        """Plot training histories for all experiments"""
        fig, axes = plt.subplots(1, 2, figsize=(15, 5))
        
        for exp in experiments:
            history = self.results[exp]['history']
            if 'loss' in history:
                axes[0].plot(history['loss'], label=f'{exp} - train')
                if 'val_loss' in history:
                    axes[0].plot(history['val_loss'], label=f'{exp} - val', linestyle='--')
            
            if 'accuracy' in history:
                axes[1].plot(history['accuracy'], label=f'{exp} - train')
                if 'val_accuracy' in history:
                    axes[1].plot(history['val_accuracy'], label=f'{exp} - val', linestyle='--')
        
        axes[0].set_title('Loss')
        axes[0].set_xlabel('Epoch')
        axes[0].set_ylabel('Loss')
        axes[0].legend(bbox_to_anchor=(1.05, 1), loc='upper left')
        
        axes[1].set_title('Accuracy')
        axes[1].set_xlabel('Epoch')
        axes[1].set_ylabel('Accuracy')
        axes[1].legend(bbox_to_anchor=(1.05, 1), loc='upper left')
        
        plt.tight_layout()
        plt.savefig(os.path.join(self.results_dir, 'training_histories.png'), dpi=300, bbox_inches='tight')
        plt.close()
    
    def get_comparison_table(self):
        """

        Get comparison table of all experiments

        

        Returns:

            Pandas DataFrame with comparison results

        """
        if not self.results:
            raise ValueError("No results available. Run experiments first.")
        
        rows = []
        for name, result in self.results.items():
            row = {'Experiment': name}
            row.update(result['metrics'])
            row['Description'] = next(
                (exp['description'] for exp in self.study_metadata['experiments'] 
                 if exp['name'] == name), ''
            )
            rows.append(row)
        
        return pd.DataFrame(rows)


class SegmentationAblationStudy(AblationStudy):
    """

    Ablation study framework specifically for segmentation models

    """
    
    def __init__(self, base_config, results_dir='./segmentation_ablation_results'):
        super().__init__(base_config, results_dir)
    
    def add_segmentation_experiment(self, name, model_config, training_config, description=""):
        """

        Add a segmentation experiment

        

        Args:

            name: Experiment name

            model_config: Model configuration modifications

            training_config: Training configuration modifications

            description: Description of the experiment

        """
        config = {
            **self.base_config,
            **model_config,
            **training_config
        }
        
        self.study_metadata['experiments'].append({
            'name': name,
            'config': config,
            'description': description,
            'model_modifications': model_config,
            'training_modifications': training_config
        })
    
    def run_segmentation_experiment(self, experiment_idx, model_builder, data, metrics_calculator):
        """

        Run a segmentation experiment

        

        Args:

            experiment_idx: Index of experiment

            model_builder: Function to build model

            data: Tuple of (X_train, y_train, X_val, y_val) where y are masks

            metrics_calculator: Function to calculate segmentation metrics

            

        Returns:

            Results dictionary

        """
        experiment = self.study_metadata['experiments'][experiment_idx]
        config = experiment['config']
        name = experiment['name']
        
        print(f"\n{'='*60}")
        print(f"Running Segmentation Ablation: {name}")
        print(f"{'='*60}")
        
        # Build model
        model = model_builder(config)
        
        # Compile
        model.compile(
            optimizer=tf.keras.optimizers.Adam(learning_rate=config.get('learning_rate', 1e-4)),
            loss=config.get('loss_fn', 'binary_crossentropy'),
            metrics=[
                tf.keras.metrics.MeanIoU(num_classes=2),
                'accuracy'
            ]
        )
        
        # Train
        X_train, y_train, X_val, y_val = data
        
        callbacks = [
            tf.keras.callbacks.EarlyStopping(
                monitor='val_loss',
                patience=config.get('patience', 10),
                restore_best_weights=True
            )
        ]
        
        history = model.fit(
            X_train,
            y_train,
            validation_data=(X_val, y_val),
            epochs=config.get('epochs', 100),
            batch_size=config.get('batch_size', 16),
            callbacks=callbacks
        )
        
        # Evaluate with custom metrics
        metrics = metrics_calculator(model, (X_val, y_val))
        
        # Store results
        self.results[name] = {
            'metrics': metrics,
            'history': history.history,
            'config': config
        }
        
        print(f"Segmentation ablation {name} completed. Metrics: {metrics}")
        
        return self.results[name]


def create_attention_ablation_study(base_config, results_dir='./attention_ablation'):
    """

    Create an ablation study for attention mechanisms

    

    Args:

        base_config: Base configuration

        results_dir: Directory to save results

        

    Returns:

        AblationStudy instance with experiments added

    """
    study = AblationStudy(base_config, results_dir)
    
    # Baseline without attention
    study.add_experiment(
        name='baseline_no_attention',
        config_modification={'use_attention': False},
        description='Baseline model without any attention mechanisms'
    )
    
    # With attention gates in skip connections
    study.add_experiment(
        name='attention_skip_connections',
        config_modification={'use_attention': True},
        description='Model with attention gates in U-Net skip connections'
    )
    
    # With channel attention
    study.add_experiment(
        name='channel_attention',
        config_modification={'attention_type': 'channel'},
        description='Model with channel-wise attention mechanism'
    )
    
    # With spatial attention
    study.add_experiment(
        name='spatial_attention',
        config_modification={'attention_type': 'spatial'},
        description='Model with spatial attention mechanism'
    )
    
    # With both channel and spatial attention
    study.add_experiment(
        name='cbam_attention',
        config_modification={'attention_type': 'cbam'},
        description='Model with combined channel and spatial attention (CBAM)'
    )
    
    return study


def create_architecture_ablation_study(base_config, results_dir='./architecture_ablation'):
    """

    Create an ablation study for architecture variations

    

    Args:

        base_config: Base configuration

        results_dir: Directory to save results

        

    Returns:

        AblationStudy instance with experiments added

    """
    study = AblationStudy(base_config, results_dir)
    
    # Baseline
    study.add_experiment(
        name='baseline',
        config_modification={},
        description='Baseline architecture'
    )
    
    # Different depths
    study.add_experiment(
        name='shallow_network',
        config_modification={'num_layers': 3},
        description='Shallower network with fewer layers'
    )
    
    study.add_experiment(
        name='deep_network',
        config_modification={'num_layers': 6},
        description='Deeper network with more layers'
    )
    
    # Different filter sizes
    study.add_experiment(
        name='smaller_filters',
        config_modification={'base_filters': 32},
        description='Network with smaller base number of filters'
    )
    
    study.add_experiment(
        name='larger_filters',
        config_modification={'base_filters': 128},
        description='Network with larger base number of filters'
    )
    
    # With residual connections
    study.add_experiment(
        name='residual_connections',
        config_modification={'use_residual': True},
        description='Network with residual connections'
    )
    
    # With dense connections
    study.add_experiment(
        name='dense_connections',
        config_modification={'use_dense': True},
        description='Network with dense connections'
    )
    
    return study


def create_loss_ablation_study(base_config, results_dir='./loss_ablation'):
    """

    Create an ablation study for different loss functions

    

    Args:

        base_config: Base configuration

        results_dir: Directory to save results

        

    Returns:

        AblationStudy instance with experiments added

    """
    study = AblationStudy(base_config, results_dir)
    
    # Baseline cross-entropy
    study.add_experiment(
        name='cross_entropy',
        config_modification={'loss_fn': 'binary_crossentropy'},
        description='Standard binary cross-entropy loss'
    )
    
    # Dice loss
    study.add_experiment(
        name='dice_loss',
        config_modification={'loss_fn': 'dice_loss'},
        description='Dice loss for better handling of class imbalance'
    )
    
    # Combined loss
    study.add_experiment(
        name='combined_dice_bce',
        config_modification={'loss_fn': 'combined_dice_bce', 'loss_weights': [0.5, 0.5]},
        description='Combined Dice and BCE loss'
    )
    
    # Focal loss
    study.add_experiment(
        name='focal_loss',
        config_modification={'loss_fn': 'focal_loss', 'focal_gamma': 2.0},
        description='Focal loss for hard example mining'
    )
    
    # Tversky loss
    study.add_experiment(
        name='tversky_loss',
        config_modification={'loss_fn': 'tversky_loss', 'alpha': 0.5, 'beta': 0.5},
        description='Tversky loss for imbalanced segmentation'
    )
    
    return study


def create_data_augmentation_ablation_study(base_config, results_dir='./augmentation_ablation'):
    """

    Create an ablation study for data augmentation strategies

    

    Args:

        base_config: Base configuration

        results_dir: Directory to save results

        

    Returns:

        AblationStudy instance with experiments added

    """
    study = AblationStudy(base_config, results_dir)
    
    # No augmentation
    study.add_experiment(
        name='no_augmentation',
        config_modification={'use_augmentation': False},
        description='No data augmentation'
    )
    
    # Basic augmentation
    study.add_experiment(
        name='basic_augmentation',
        config_modification={
            'use_augmentation': True,
            'augmentation': ['flip', 'rotation']
        },
        description='Basic augmentation: flips and rotations'
    )
    
    # Advanced augmentation
    study.add_experiment(
        name='advanced_augmentation',
        config_modification={
            'use_augmentation': True,
            'augmentation': ['flip', 'rotation', 'zoom', 'contrast', 'brightness']
        },
        description='Advanced augmentation with multiple transformations'
    )
    
    # With MixUp
    study.add_experiment(
        name='mixup_augmentation',
        config_modification={
            'use_augmentation': True,
            'augmentation': ['flip', 'rotation', 'mixup'],
            'mixup_alpha': 0.2
        },
        description='Augmentation with MixUp strategy'
    )
    
    # With CutMix
    study.add_experiment(
        name='cutmix_augmentation',
        config_modification={
            'use_augmentation': True,
            'augmentation': ['flip', 'rotation', 'cutmix'],
            'cutmix_alpha': 1.0
        },
        description='Augmentation with CutMix strategy'
    )
    
    return study


def calculate_segmentation_metrics(model, data, thresholds=None):
    """

    Calculate comprehensive segmentation metrics

    

    Args:

        model: Trained segmentation model

        data: Tuple of (X_val, y_val)

        thresholds: List of thresholds for binary classification

        

    Returns:

        Dictionary of metrics

    """
    from sklearn.metrics import jaccard_score, confusion_matrix

    def _dice_score(y_true, y_pred, smooth=1e-6):
        y_true = np.asarray(y_true).ravel().astype(np.float32)
        y_pred = np.asarray(y_pred).ravel().astype(np.float32)
        intersection = float(np.sum(y_true * y_pred))
        return (2.0 * intersection + smooth) / (float(np.sum(y_true) + np.sum(y_pred)) + smooth)

    X_val, y_val = data

    # Predict
    y_pred = model.predict(X_val)

    # Use threshold of 0.5 by default
    if thresholds is None:
        thresholds = [0.5]

    metrics = {}

    for threshold in thresholds:
        y_pred_binary = (y_pred >= threshold).astype(int)

        # Flatten for metric calculation
        y_val_flat = y_val.flatten()
        y_pred_flat = y_pred_binary.flatten()

        # Dice coefficient (local implementation; sklearn has no dice_score)
        dice = _dice_score(y_val_flat, y_pred_flat)
        
        # IoU (Jaccard index)
        iou = jaccard_score(y_val_flat, y_pred_flat)
        
        # Precision, Recall, F1
        tn, fp, fn, tp = confusion_matrix(y_val_flat, y_pred_flat).ravel()
        precision = tp / (tp + fp) if (tp + fp) > 0 else 0
        recall = tp / (tp + fn) if (tp + fn) > 0 else 0
        f1 = 2 * precision * recall / (precision + recall) if (precision + recall) > 0 else 0
        
        # Specificity
        specificity = tn / (tn + fp) if (tn + fp) > 0 else 0
        
        metrics[f'dice_t{threshold}'] = float(dice)
        metrics[f'iou_t{threshold}'] = float(iou)
        metrics[f'precision_t{threshold}'] = float(precision)
        metrics[f'recall_t{threshold}'] = float(recall)
        metrics[f'f1_t{threshold}'] = float(f1)
        metrics[f'specificity_t{threshold}'] = float(specificity)
    
    return metrics