Skip to content

Command Line Interface (CLI)

The AutoMIL Command Line Interface (CLI) provides a user-friendly way to interact with the framework directly from the terminal. It allows users to perform various tasks such as data preprocessing, model training, evaluation, and prediction without needing to write any code.

run_pipeline

run_pipeline(
    slide_dir: str | Path,
    annotation_file: str | Path,
    project_dir: str | Path,
    patient_column: str,
    label_column: str,
    slide_column: str | None,
    resolutions: str,
    model: str,
    k: int,
    split_file: str | None,
    transform_labels: bool,
    is_pretiled: bool,
    verbose: bool,
)

Execute the complete AutoMIL pipeline for whole slide image analysis.

This command runs the full AutoMIL workflow, including project setup, dataset preparation, model training with k-fold cross-validation, evaluation, and result visualization.

Pipeline stages:

  1. Project setup and configuration
  2. Dataset preparation and tile extraction
  3. Model training with k-fold cross-validation
  4. Model evaluation and ensemble creation
  5. Result visualization

Parameters:

Name Type Description Default
slide_dir str | Path

Directory containing whole-slide images or pre-extracted tiles.

required
annotation_file str | Path

CSV file containing slide- or patient-level annotations and labels.

required
project_dir str | Path

Output directory where trained models and intermediate files will be written.

required
patient_column str

Name of the column containing patient identifiers.

required
label_column str

Name of the column containing class labels.

required
slide_column str | None

Name of the column containing slide identifiers.

required
resolutions str

Comma-separated list of resolution presets to train on.

required
model str

Model architecture to train.

required
k int

Number of folds used for k-fold cross-validation.

required
is_pretiled bool

Indicates that the input slides are already tiled.

required
transform_labels bool

If enabled, transforms labels to floating-point values.

required
verbose bool

Enables verbose logging output.

required
Examples

Basic usage with default settings:

automil run-pipeline /data/slides /data/annotations.csv ./results

Multi-resolution training with verbose output:

automil run-pipeline -r "Low,High" -v /data/slides /data/annotations.csv ./results

Custom model and k-fold settings:

automil run-pipeline -m TransMIL -k 5 /data/slides /data/annotations.csv ./results

Skip tiling if tiles are pre-extracted:

automil run-pipeline -p /data/slides /data/annotations.csv ./results

Custom column names in the annotation file:

automil run-pipeline -pc "patient_name" -lc "diagnosis" -sc "slide_name" /data/slides /data/annotations.csv ./results

Provide a predefined train-test split:

automil run-pipeline --split-file /data/split.json /data/slides /data/annotations.csv ./results
Annotation file requirements

The annotation file must be a CSV file containing at least the following columns:

  • Patient identifiers (default column name: patient)
  • Slide identifiers (default column name: slide; optional)
  • Class labels (default column name: label)

By default, AutoMIL looks for columns named patient, slide, and label. These defaults can be overridden using the --patient_column, --slide_column, and --label_column options.

Minimal annotation file example
patient,slide,label
001,001_1,0
001,001_2,0
002,002,1
003,003,1
Expected slide directory structure

SLIDE_DIR should contain whole slide images in supported formats such as .svs, .tiff, or .png. Example structure:

/data/slides/
|-- slide1.svs
|-- slide2.tiff
|-- slide3.tiff
PNG Slide Handling

If slides are in PNG, AutoMIL will first convert them to TIFF for easier processing.

Using pretiled data

If tiles have already been extracted from the slides, use the --is_pretiled flag. In the case of pretiled data, AutoMIL expects the following directory structure for SLIDE_DIR:

/data/slides/
|-- slide1/
|    |-- tile_0_0.png
|    |-- tile_0_1.png
|    |-- ...
|-- slide2/
|    |-- tile_0_0.png
|    |-- tile_0_1.png
|    |-- ...
Slide name matching

Tile names are arbitrary but slide subdirectories must match the slide names in ANNOTATION_FILE.

Providing a train-test split

Use the --split-file option to provide a JSON file defining train-test splits. The JSON file will have the following structure:

    {
    "train": ["slide1", "slide2", ...],
    "test":  ["slide3", "slide4", ...]
    }

or:

    {
    "train": ["slide1", "slide2", ...],
    "validation":  ["slide3", "slide4", ...]
    }
Output structure
project_dir/
├── bags/           # Extracted tile features
├── models/         # Trained model checkpoints  
├── ensemble/       # Ensemble predictions
├── annotations.csv # Processed annotations
└── results.json    # Performance metrics
Source code in automil/cli.py
 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
@AutoMIL.command(
    name="run-pipeline", 
    context_settings=CONTEXT_SETTINGS,
    no_args_is_help=True,
    help=RUN_PIPELINE_HELP
)
@click.argument("slide_dir",        type=click.Path(exists=True, file_okay=False))
@click.argument("annotation_file",  type=click.Path(exists=True, file_okay=True))
@click.argument("project_dir",      type=click.Path(file_okay=False))
@click.option(
    "-pc", "--patient_column", type=str, default="patient",
    help="Name of the column containing patient IDs"
)
@click.option(
    "-lc", "--label_column", type=str, default="label",
    help="Name of the column containing labels"
)
@click.option(
    "-sc", "--slide_column", type=str, default=None,
    help="Name of the column containing slide names"
)
@click.option(
    "-r", "--resolutions",
    type=str,
    default="Low",
    help=f"Comma-separated list of resolution presets to train on. "
         f"Available: {', '.join([choice for choice in RESOLUTION_CHOICES])} "
         f"(e.g., 'Low,High')"
)
@click.option(
    "-m", "--model",
    type=(model_choice := click.Choice([choice for choice in MODEL_CHOICES])),
    default=model_choice.choices[0],
    help=f"Model type to train and evaluate"
)
@click.option(
    "-k", type=int, default=3,
    help="number of folds to train per resolution level"
)
@click.option(
    "--split-file", type=click.Path(file_okay=True), default="split.json",
    help="Path to a .json file defining train-test splits"
)
@click.option("-t", "--transform_labels", is_flag=True, help="Transforms labels to float values (0.0, 1.0, ...)")
@click.option("-p", "--is-pretiled",      is_flag=True, help="Indicated that the input format is pretiled slides")
@click.option("-v", "--verbose",          is_flag=True, help="Enables additional logging messages")
def run_pipeline(
    slide_dir:       str | Path,
    annotation_file: str | Path,
    project_dir:     str | Path,
    patient_column:  str,
    label_column:    str,
    slide_column:    str | None,
    resolutions:     str,
    model:           str,
    k:               int,
    split_file:      str | None,
    transform_labels: bool,
    is_pretiled:      bool,
    verbose:          bool
    ):
    """
    Execute the complete AutoMIL pipeline for whole slide image analysis.

    This command runs the full AutoMIL workflow, including project setup,
    dataset preparation, model training with k-fold cross-validation,
    evaluation, and result visualization.

    Pipeline stages:

    1. Project setup and configuration
    2. Dataset preparation and tile extraction
    3. Model training with k-fold cross-validation
    4. Model evaluation and ensemble creation
    5. Result visualization

    Args:
        slide_dir (str | Path):
            Directory containing whole-slide images or pre-extracted tiles.

        annotation_file (str | Path):
            CSV file containing slide- or patient-level annotations and labels.

        project_dir (str | Path):
            Output directory where trained models and intermediate files
            will be written.

        patient_column (str):
            Name of the column containing patient identifiers.

        label_column (str):
            Name of the column containing class labels.

        slide_column (str | None):
            Name of the column containing slide identifiers.

        resolutions (str):
            Comma-separated list of resolution presets to train on.

        model (str):
            Model architecture to train.

        k (int):
            Number of folds used for k-fold cross-validation.

        is_pretiled (bool):
            Indicates that the input slides are already tiled.

        transform_labels (bool):
            If enabled, transforms labels to floating-point values.

        verbose (bool):
            Enables verbose logging output.

    ### Examples

      Basic usage with default settings:

        automil run-pipeline /data/slides /data/annotations.csv ./results

      Multi-resolution training with verbose output:

        automil run-pipeline -r "Low,High" -v /data/slides /data/annotations.csv ./results

      Custom model and k-fold settings:

        automil run-pipeline -m TransMIL -k 5 /data/slides /data/annotations.csv ./results

      Skip tiling if tiles are pre-extracted:

        automil run-pipeline -p /data/slides /data/annotations.csv ./results

      Custom column names in the annotation file:

        automil run-pipeline -pc "patient_name" -lc "diagnosis" -sc "slide_name" /data/slides /data/annotations.csv ./results

      Provide a predefined train-test split:

        automil run-pipeline --split-file /data/split.json /data/slides /data/annotations.csv ./results

    ### Annotation file requirements

    The annotation file must be a CSV file containing at least the following columns:

    - Patient identifiers (default column name: `patient`)
    - Slide identifiers (default column name: `slide`; optional)
    - Class labels (default column name: `label`)

    By default, AutoMIL looks for columns named `patient`, `slide`, and `label`.
    These defaults can be overridden using the `--patient_column`,
    `--slide_column`, and `--label_column` options.

    ### Minimal annotation file example
        patient,slide,label
        001,001_1,0
        001,001_2,0
        002,002,1
        003,003,1

    ### Expected slide directory structure
      `SLIDE_DIR` should contain whole slide images in supported formats
      such as .svs, .tiff, or .png.
      Example structure:

        /data/slides/
        |-- slide1.svs
        |-- slide2.tiff
        |-- slide3.tiff

    ??? Note "PNG Slide Handling"
        If slides are in PNG, AutoMIL will first convert them to TIFF for easier processing.

    ### Using pretiled data
      If tiles have already been extracted from the slides, use the `--is_pretiled` flag.
      In the case of pretiled data, AutoMIL expects the following directory structure for `SLIDE_DIR`:

        /data/slides/
        |-- slide1/
        |    |-- tile_0_0.png
        |    |-- tile_0_1.png
        |    |-- ...
        |-- slide2/
        |    |-- tile_0_0.png
        |    |-- tile_0_1.png
        |    |-- ...

    ??? Note "Slide name matching"
        Tile names are arbitrary but slide subdirectories must match the slide names in ANNOTATION_FILE.

    ### Providing a train-test split
      Use the `--split-file` option to provide a JSON file defining train-test splits.
      The JSON file will have the following structure:

            {
            "train": ["slide1", "slide2", ...],
            "test":  ["slide3", "slide4", ...]
            }

      or:

            {
            "train": ["slide1", "slide2", ...],
            "validation":  ["slide3", "slide4", ...]
            }

    ### Output structure

        project_dir/
        ├── bags/           # Extracted tile features
        ├── models/         # Trained model checkpoints  
        ├── ensemble/       # Ensemble predictions
        ├── annotations.csv # Processed annotations
        └── results.json    # Performance metrics

    """
    import slideflow as sf

    from .dataset import Dataset
    from .evaluation import Evaluator
    from .project import Project
    from .trainer import Trainer
    from .util import (INFO_CLR, RESOLUTION_PRESETS, LogLevel, ModelType,
                       get_vlog)
    from .util.backend import configure_image_backend, has_png_slides
    from .util.pretiled import is_input_pretiled

    # Getting a verbose logger
    vlog = get_vlog(verbose)
    sf.setLoggingLevel(20) # INFO: 20, DEBUG: 10

    # Logging the executed command
    command = " ".join(sys.argv)
    vlog(f"Executing command: [{INFO_CLR}]{command}[/]")

    # Define some paths
    bags_dir = Path(project_dir) / "bags"
    models_dir = Path(project_dir) / "models"
    ensemble_dir = Path(project_dir) / "ensemble"

    # Some type coercion
    slide_dir = Path(slide_dir)
    annotation_file = Path(annotation_file)
    project_dir = Path(project_dir)

    try:

        # === 1. Parsing === #
        # Parse given string resolutions into list of RESOLUTION_PRESETS
        resolution_presets: list[RESOLUTION_PRESETS] = []
        for res in [r.strip() for r in resolutions.split(',')]: resolution_presets.append(RESOLUTION_PRESETS[res])
        vlog(f"Using resolution presets: [{INFO_CLR}]{[preset.name for preset in resolution_presets]}[/]")

        # Parse the model type
        model_type = ModelType[model]
        vlog(f"Using model type: [{INFO_CLR}]{model_type.name}[/]")

        # === 2. Image Backend Configuration === #
        png_slides_present = has_png_slides(slide_dir)

        tiff_conversion = configure_image_backend(
            slide_dir=slide_dir,
            needs_png_conversion=png_slides_present,
            verbose=verbose,
        )

        # === 3. Project Creation And Setup === #
        project_setup = Project(
            Path(project_dir),
            Path(annotation_file),
            Path(slide_dir),
            patient_column,
            label_column,
            slide_column,
            transform_labels,
            verbose,
        )

        # Prepare slideflow project object
        project = project_setup.prepare_project()
        # We'll need the label map and slide ids for the dataset setup
        label_map = project_setup.label_map
        slide_ids = project_setup.slide_ids

        project_setup.summary()

        # === 4. Setup Dataset Sources ===
        # Determine if the slide_dir has pretiled slides
        if not is_pretiled: # is_pretiled == False means the flag was not set
            is_pretiled = is_input_pretiled(
                slide_dir,
                slide_ids
            )

        datasets: dict[str, sf.Dataset] = {}
        for preset in resolution_presets:
            vlog(f"Setting up dataset for resolution preset: [{INFO_CLR}]{preset.name}[/]")

            dataset = Dataset(
                project,
                preset,
                label_map,
                slide_dir=Path(slide_dir),
                bags_dir=Path(project_dir) / "bags",
                is_pretiled=is_pretiled,
                tiff_conversion=tiff_conversion,
                verbose=verbose
            )
            dataset.summary()
            datasets[preset.name] = dataset.prepare_dataset_source()
            vlog(f"Dataset setup complete for resolution preset: [{INFO_CLR}]{preset.name}[/]")

        # === 5. Prepare (or Load) Train/Test Split === #
        dataset = datasets[resolution_presets[0].name]
        train, test = dataset.split(
            labels="label",
            val_fraction=0.2,
            splits=split_file
        )

        # === 6. Model Training === #
        for resolution in resolution_presets:
            vlog(f"Train/Test split for resolution preset [{INFO_CLR}]{resolution.name}[/]: "
                 f"[{INFO_CLR}]{len(train.slides())}[/] train slides"
            )

            train, val = train.split(
                labels="label",
                val_fraction=0.2
            )

            trainer = Trainer(
                bags_dir,
                project,
                train,
                val,
                model=model_type,
                k=k,
                epochs=300
            )
            trainer.train_k_fold()
            trainer.summary()

        # === 7. Model Evaluation === #
        evaluator = Evaluator(
            test,
            models_dir,
            ensemble_dir,
            bags_dir,
            verbose=verbose
        )

        evaluator.evaluate_models(generate_attention_heatmaps=True)
        evaluator.create_ensemble_predictions(
            output_path=Path(project.root) / "ensemble_predictions.csv"
        )

        evaluator.compare_models()
        evaluator.generate_plots(
            save_path=Path(project.root) / "figures",
            model_paths=None
        )

    except Exception as e:
        tb = traceback.format_exc()
        vlog(tb, LogLevel.ERROR)
        vlog(f"Error: {e}", LogLevel.ERROR)
        return

train

train(
    slide_dir: str | Path,
    annotation_file: str | Path,
    project_dir: str | Path,
    patient_column: str,
    label_column: str,
    slide_column: str | None,
    resolutions: str,
    model: str,
    k: int,
    is_pretiled: bool,
    transform_labels: bool,
    verbose: bool,
)

Train one or more MIL models on a given dataset.

This command initializes an AutoMIL project, prepares the dataset, and trains MIL models using k-fold cross-validation. Training can be performed at one or multiple resolution presets.

Pipeline stages:

  1. Project setup and configuration
  2. Dataset preparation and tile extraction
  3. Model training with k-fold cross-validation

Parameters:

Name Type Description Default
slide_dir str | Path

Directory containing whole-slide images or pre-extracted tiles.

required
annotation_file str | Path

CSV file containing slide- or patient-level annotations and labels.

required
project_dir str | Path

Output directory where trained models and intermediate files will be written.

required
patient_column str

Name of the column containing patient identifiers.

required
label_column str

Name of the column containing class labels.

required
slide_column str | None

Name of the column containing slide identifiers.

required
resolutions str

Comma-separated list of resolution presets to train on.

required
model str

Model architecture to train.

required
k int

Number of folds used for k-fold cross-validation.

required
is_pretiled bool

Indicates that the input slides are already tiled.

required
transform_labels bool

If enabled, transforms labels to floating-point values.

required
verbose bool

Enables verbose logging output.

required
Examples

Basic usage with default settings:

automil train /data/slides /data/annotations.csv ./results

Multi-resolution training with verbose output::

automil train -r "Low,High" -v /data/slides /data/annotations.csv ./results

Custom model and 5-fold configuration:

automil train -m TransMIL -k 5 /data/slides /data/annotations.csv ./results

Using pre-tiled slides::

automil train -p /data/slides /data/annotations.csv ./results
Annotation file requirements

The annotation file must be a CSV file containing at least the following columns:

  • Patient identifiers (default column name: patient)
  • Slide identifiers (default column name: slide; optional)
  • Class labels (default column name: label)

By default, AutoMIL looks for columns named patient, slide, and label. These defaults can be overridden using the --patient_column, --slide_column, and --label_column options.

Minimal annotation file example
patient,slide,label
001,001_1,0
001,001_2,0
002,002,1
003,003,1
Expected slide directory structure

SLIDE_DIR should contain whole slide images in supported formats such as .svs, .tiff, or .png. Example structure:

/data/slides/
|-- slide1.svs
|-- slide2.tiff
|-- slide3.tiff
PNG Slide Handling

If slides are in PNG, AutoMIL will first convert them to TIFF for easier processing.

Using pretiled data

If tiles have already been extracted from the slides, use the --is_pretiled flag. In the case of pretiled data, AutoMIL expects the following directory structure for SLIDE_DIR:

/data/slides/
|-- slide1/
|    |-- tile_0_0.png
|    |-- tile_0_1.png
|    |-- ...
|-- slide2/
|    |-- tile_0_0.png
|    |-- tile_0_1.png
|    |-- ...
Slide name matching

Tile names are arbitrary but slide subdirectories must match the slide names in ANNOTATION_FILE.

Providing a train-test split

Use the --split-file option to provide a JSON file defining train-test splits. The JSON file will have the following structure:

    {
    "train": ["slide1", "slide2", ...],
    "test":  ["slide3", "slide4", ...]
    }

or:

    {
    "train": ["slide1", "slide2", ...],
    "validation":  ["slide3", "slide4", ...]
    }
Output structure
project_dir/
├── bags/           # Extracted tile features
├── models/         # Trained model checkpoints  
├── ensemble/       # Ensemble predictions
├── annotations.csv # Processed annotations
└── results.json    # Performance metrics
Source code in automil/cli.py
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
@AutoMIL.command(
    name="train",
    context_settings=CONTEXT_SETTINGS,
    no_args_is_help=True,
    help=TRAIN_HELP
)
@click.argument("slide_dir",        type=click.Path(exists=True, file_okay=False))
@click.argument("annotation_file",  type=click.Path(exists=True, file_okay=True))
@click.argument("project_dir",      type=click.Path(file_okay=False))
@click.option(
    "-pc", "--patient_column", type=str, default="patient",
    help="Name of the column containing patient IDs"
)
@click.option(
    "-lc", "--label_column", type=str, default="label",
    help="Name of the column containing labels"
)
@click.option(
    "-sc", "--slide_column", type=str, default=None,
    help="Name of the column containing slide names"
)
@click.option(
    "-r", "--resolutions",
    type=str,
    default="Low",
    help=f"Comma-separated list of resolution presets to train on. "
         f"Available: {', '.join([choice for choice in RESOLUTION_CHOICES])} "
         f"(e.g., 'Low,High')"
)
@click.option(
    "-m", "--model",
    type=(model_choice := click.Choice([choice for choice in MODEL_CHOICES])),
    default=model_choice.choices[0],
    help=f"Model type to train and evaluate"
)
@click.option(
    "-k", type=int, default=3,
    help="number of folds to train per resolution level"
)
@click.option("-p", "--is-pretiled",      is_flag=True, help="Indicated that the input format is pretiled slides")
@click.option("-t", "--transform_labels", is_flag=True, help="Transforms labels to float values (0.0, 1.0, ...)")
@click.option("-v", "--verbose",          is_flag=True, help="Enables additional logging messages")
def train(
    slide_dir:       str | Path,
    annotation_file: str | Path,
    project_dir:     str | Path,
    patient_column:  str,
    label_column:    str,
    slide_column:    str | None,
    resolutions:     str,
    model:           str,
    k:               int,
    is_pretiled:      bool,
    transform_labels: bool,
    verbose:          bool
):
    """
    Train one or more MIL models on a given dataset.

    This command initializes an AutoMIL project, prepares the dataset,
    and trains MIL models using k-fold cross-validation. Training can be
    performed at one or multiple resolution presets.

    Pipeline stages:

    1. Project setup and configuration
    2. Dataset preparation and tile extraction
    3. Model training with k-fold cross-validation

    Args:
        slide_dir (str | Path):
            Directory containing whole-slide images or pre-extracted tiles.

        annotation_file (str | Path):
            CSV file containing slide- or patient-level annotations and labels.

        project_dir (str | Path):
            Output directory where trained models and intermediate files
            will be written.

        patient_column (str):
            Name of the column containing patient identifiers.

        label_column (str):
            Name of the column containing class labels.

        slide_column (str | None):
            Name of the column containing slide identifiers.

        resolutions (str):
            Comma-separated list of resolution presets to train on.

        model (str):
            Model architecture to train.

        k (int):
            Number of folds used for k-fold cross-validation.

        is_pretiled (bool):
            Indicates that the input slides are already tiled.

        transform_labels (bool):
            If enabled, transforms labels to floating-point values.

        verbose (bool):
            Enables verbose logging output.

    ### Examples
      Basic usage with default settings:

        automil train /data/slides /data/annotations.csv ./results

      Multi-resolution training with verbose output::

        automil train -r "Low,High" -v /data/slides /data/annotations.csv ./results

      Custom model and 5-fold configuration:

        automil train -m TransMIL -k 5 /data/slides /data/annotations.csv ./results

      Using pre-tiled slides::

        automil train -p /data/slides /data/annotations.csv ./results

    ### Annotation file requirements

    The annotation file must be a CSV file containing at least the following columns:

    - Patient identifiers (default column name: `patient`)
    - Slide identifiers (default column name: `slide`; optional)
    - Class labels (default column name: `label`)

    By default, AutoMIL looks for columns named `patient`, `slide`, and `label`.
    These defaults can be overridden using the `--patient_column`,
    `--slide_column`, and `--label_column` options.

    ### Minimal annotation file example
        patient,slide,label
        001,001_1,0
        001,001_2,0
        002,002,1
        003,003,1

    ### Expected slide directory structure
      `SLIDE_DIR` should contain whole slide images in supported formats
      such as .svs, .tiff, or .png.
      Example structure:

        /data/slides/
        |-- slide1.svs
        |-- slide2.tiff
        |-- slide3.tiff

    ??? Note "PNG Slide Handling"
        If slides are in PNG, AutoMIL will first convert them to TIFF for easier processing.

    ### Using pretiled data
      If tiles have already been extracted from the slides, use the `--is_pretiled` flag.
      In the case of pretiled data, AutoMIL expects the following directory structure for `SLIDE_DIR`:

        /data/slides/
        |-- slide1/
        |    |-- tile_0_0.png
        |    |-- tile_0_1.png
        |    |-- ...
        |-- slide2/
        |    |-- tile_0_0.png
        |    |-- tile_0_1.png
        |    |-- ...

    ??? Note "Slide name matching"
        Tile names are arbitrary but slide subdirectories must match the slide names in ANNOTATION_FILE.

    ### Providing a train-test split
      Use the `--split-file` option to provide a JSON file defining train-test splits.
      The JSON file will have the following structure:

            {
            "train": ["slide1", "slide2", ...],
            "test":  ["slide3", "slide4", ...]
            }

      or:

            {
            "train": ["slide1", "slide2", ...],
            "validation":  ["slide3", "slide4", ...]
            }

    ### Output structure

        project_dir/
        ├── bags/           # Extracted tile features
        ├── models/         # Trained model checkpoints  
        ├── ensemble/       # Ensemble predictions
        ├── annotations.csv # Processed annotations
        └── results.json    # Performance metrics

    """

    import slideflow as sf

    from .dataset import Dataset
    from .project import Project
    from .trainer import Trainer
    from .util import (INFO_CLR, RESOLUTION_PRESETS, LogLevel, ModelType,
                       get_vlog)
    from .util.backend import configure_image_backend, has_png_slides
    from .util.pretiled import is_input_pretiled

    # Getting a verbose logger
    vlog = get_vlog(verbose)
    sf.setLoggingLevel(20) # INFO: 20, DEBUG: 10

    # Logging the executed command
    command = " ".join(sys.argv)
    vlog(f"Executing command: [{INFO_CLR}]{command}[/]")

    # Define some paths
    bags_dir = Path(project_dir) / "bags"

    # Some type coercion
    slide_dir = Path(slide_dir)
    annotation_file = Path(annotation_file)
    project_dir = Path(project_dir)

    try:

        # === 1. Parsing === #
        # Parse given string resolutions into list of RESOLUTION_PRESETS
        resolution_presets: list[RESOLUTION_PRESETS] = []
        for res in [r.strip() for r in resolutions.split(',')]: resolution_presets.append(RESOLUTION_PRESETS[res])
        vlog(f"Using resolution presets: [{INFO_CLR}]{[preset.name for preset in resolution_presets]}[/]")

        # Parse the model type
        model_type = ModelType[model]
        vlog(f"Using model type: [{INFO_CLR}]{model_type.name}[/]")

        # === 2. Image Backend Configuration === #
        png_slides_present = has_png_slides(slide_dir)

        tiff_conversion = configure_image_backend(
            slide_dir=slide_dir,
            needs_png_conversion=png_slides_present,
            verbose=verbose,
        )

        # === 3. Project Creation And Setup === #
        project_setup = Project(
            Path(project_dir),
            Path(annotation_file),
            Path(slide_dir),
            patient_column,
            label_column,
            slide_column,
            transform_labels=transform_labels,
            verbose=verbose,
        )
        # Prepare slideflow project object
        project = project_setup.prepare_project()
        # We'll need the label map and slide ids for the dataset setup
        label_map = project_setup.label_map
        slide_ids = project_setup.slide_ids

        project_setup.summary()

        # === 4. Setup Dataset Sources ===
        # Determine if the slide_dir has pretiled slides
        if not is_pretiled: # is_pretiled == False means the flag was not set
            is_pretiled = is_input_pretiled(
                slide_dir,
                slide_ids
            )

        datasets: dict[str, sf.Dataset] = {}
        for resolution in resolution_presets:
            vlog(f"Setting up dataset for resolution preset: [{INFO_CLR}]{resolution.name}[/]")

            dataset = Dataset(
                project,
                resolution,
                label_map,
                slide_dir=Path(slide_dir),
                bags_dir=Path(project_dir) / "bags",
                is_pretiled=is_pretiled,
                tiff_conversion=tiff_conversion,
                verbose=verbose
            )
            dataset.summary()
            datasets[resolution.name] = dataset.prepare_dataset_source()
            vlog(f"Dataset setup complete for resolution preset: [{INFO_CLR}]{resolution.name}[/]")

        # === 5. Model Training === #
        for resolution in resolution_presets:
            dataset = datasets[resolution.name]
            vlog(f"Train/Test split for resolution preset '[{INFO_CLR}]{resolution.name}[/]': "
                 f"[{INFO_CLR}]{len(dataset.slides())}[/] train slides"
            )

            train, val = dataset.split(
                labels="label",
                val_fraction=0.2
            )

            trainer = Trainer(
                bags_dir,
                project,
                train,
                val,
                model=model_type,
                k=k,
                epochs=300
            )
            trainer.train_k_fold()
            trainer.summary()

    except Exception as e:
        tb = traceback.format_exc()
        vlog(tb, LogLevel.ERROR)
        vlog(f"Error: {e}", LogLevel.ERROR)
        return

predict

predict(
    slide_dir: str | Path,
    annotation_file: str | Path,
    bags_dir: str | Path,
    model_dir: str | Path,
    output_dir: str | Path,
    patient_column: str,
    label_column: str,
    slide_column: str | None,
    verbose: bool,
)

Generate predictions using one or more trained MIL models.

This command loads trained model checkpoints and generates predictions for the slides in SLIDE_DIR using precomputed tile feature bags from BAGS_DIR. Predictions are written to the specified output directory.

Parameters:

Name Type Description Default
slide_dir str | Path

Directory containing whole-slide images.

required
annotation_file str | Path

CSV file containing slide- or patient-level annotations and labels.

required
bags_dir str | Path

Directory containing extracted tile feature bags.

required
model_dir str | Path

Directory containing trained model checkpoints.

required
output_dir str | Path

Directory to which prediction files will be written.

required
patient_column str

Name of the column containing patient identifiers.

required
label_column str

Name of the column containing class labels.

required
slide_column str | None

Name of the column containing slide identifiers.

required
verbose bool

Enables verbose logging output.

required
Examples

Basic usage with multiple models:

automil predict /data/slides /data/annotations.csv /data/bags /data/models -o ./predictions

Generate predictions with a single model:

automil predict /data/slides /data/annotations.csv /data/bags /data/models/model_1 -v

Override annotation column names:

automil predict -pc "patient_id" -lc "outcome" -sc "slide_id"             /data/slides /data/annotations.csv /data/bags /data/models/model_1             -o ./predictions
Expected model directory structure

MODEL_DIR may either point to a single model directory or to a parent directory containing multiple model subdirectories.

Single model example:

/data/models/model_1/
|-- best_valid.pth
|-- ...

Multiple models example:

/data/models/
|-- model_1/
|    |-- best_valid.pth
|-- model_2/
|    |-- best_valid.pth
|    |-- ...
Multiple models

When multiple models are provided, AutoMIL generates a separate prediction file for each model.

Annotation file requirements

The annotation file must be a CSV file containing at least the following columns:

  • Patient identifiers (default column name: patient)
  • Slide identifiers (default column name: slide; optional)
  • Class labels (default column name: label)

By default, AutoMIL looks for columns named patient, slide, and label. These defaults can be overridden using the --patient_column, --slide_column, and --label_column options.

Minimal annotation file example
patient,slide,label
001,001_1,0
001,001_2,0
002,002,1
003,003,1
Output directory format

OUTPUT_DIR must be a directory path. Prediction results are saved as separate .csv or .parquet files inside this directory.

When multiple models are used, output files include a suffix indicating the corresponding model.

Source code in automil/cli.py
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
@AutoMIL.command(
    name="predict",
    context_settings=CONTEXT_SETTINGS,
    no_args_is_help=True,
    help=PREDICT_HELP
)
@click.argument("slide_dir",    type=click.Path(exists=True, file_okay=False))
@click.argument("annotation_file",  type=click.Path(exists=True, file_okay=True))
@click.argument("bags_dir",     type=click.Path(exists=True, file_okay=False))
@click.argument("model_dir",    type=click.Path(exists=True, file_okay=False))
@click.option(
    "-o", "--output-dir", 
    type=click.Path(file_okay=True), default="predictions",
    help="Directory to which to save predictions (should either be .csv or .parquet)"
)
@click.option(
    "-pc", "--patient_column", type=str, default="patient",
    help="Name of the column containing patient IDs"
)
@click.option(
    "-lc", "--label_column", type=str, default="label",
    help="Name of the column containing labels"
)
@click.option(
    "-sc", "--slide_column", type=str, default=None,
    help="Name of the column containing slide names"
)
@click.option("-v", "--verbose", is_flag=True, help="Enables additional logging messages")
def predict(
    slide_dir:   str | Path,
    annotation_file: str | Path,
    bags_dir:    str | Path,
    model_dir:   str | Path,
    output_dir: str | Path,
    patient_column:  str,
    label_column:    str,
    slide_column:    str | None,
    verbose:     bool
):
    """
    Generate predictions using one or more trained MIL models.

    This command loads trained model checkpoints and generates predictions
    for the slides in `SLIDE_DIR` using precomputed tile feature bags from
    `BAGS_DIR`. Predictions are written to the specified output directory.

    Args:
        slide_dir (str | Path):
            Directory containing whole-slide images.

        annotation_file (str | Path):
            CSV file containing slide- or patient-level annotations and labels.

        bags_dir (str | Path):
            Directory containing extracted tile feature bags.

        model_dir (str | Path):
            Directory containing trained model checkpoints.

        output_dir (str | Path):
            Directory to which prediction files will be written.

        patient_column (str):
            Name of the column containing patient identifiers.

        label_column (str):
            Name of the column containing class labels.

        slide_column (str | None):
            Name of the column containing slide identifiers.

        verbose (bool):
            Enables verbose logging output.

    ### Examples

    Basic usage with multiple models:

        automil predict /data/slides /data/annotations.csv /data/bags /data/models -o ./predictions

    Generate predictions with a single model:

        automil predict /data/slides /data/annotations.csv /data/bags /data/models/model_1 -v

    Override annotation column names:

        automil predict -pc "patient_id" -lc "outcome" -sc "slide_id" \
            /data/slides /data/annotations.csv /data/bags /data/models/model_1 \
            -o ./predictions

    ### Expected model directory structure

    `MODEL_DIR` may either point to a single model directory or to a parent
    directory containing multiple model subdirectories.

    Single model example:

        /data/models/model_1/
        |-- best_valid.pth
        |-- ...

    Multiple models example:

        /data/models/
        |-- model_1/
        |    |-- best_valid.pth
        |-- model_2/
        |    |-- best_valid.pth
        |    |-- ...

    ??? Note "Multiple models"
        When multiple models are provided, AutoMIL generates a separate
        prediction file for each model.

    ### Annotation file requirements

    The annotation file must be a CSV file containing at least the following columns:

    - Patient identifiers (default column name: `patient`)
    - Slide identifiers (default column name: `slide`; optional)
    - Class labels (default column name: `label`)

    By default, AutoMIL looks for columns named `patient`, `slide`, and `label`.
    These defaults can be overridden using the `--patient_column`,
    `--slide_column`, and `--label_column` options.

    ### Minimal annotation file example

        patient,slide,label
        001,001_1,0
        001,001_2,0
        002,002,1
        003,003,1

    ### Output directory format

    `OUTPUT_DIR` must be a directory path. Prediction results are saved as
    separate `.csv` or `.parquet` files inside this directory.

    When multiple models are used, output files include a suffix indicating
    the corresponding model.
    """

    import slideflow as sf

    from .evaluation import Evaluator
    from .project import Project
    from .util import INFO_CLR, LogLevel, get_vlog

    # Getting a verbose logger
    vlog = get_vlog(verbose)
    sf.setLoggingLevel(20) # INFO: 20, DEBUG: 10

    # Logging the executed command
    command = " ".join(sys.argv)
    vlog(f"Executing command: [{INFO_CLR}]{command}[/]")

    # Some type coercion
    slide_dir = Path(slide_dir)
    bags_dir =  Path(bags_dir)
    model_dir = Path(model_dir)
    output_dir = Path(output_dir)

    # Setup output folder as project (modifies annotation file)
    project = Project(
        Path(output_dir),
        Path(annotation_file),
        Path(slide_dir),
        patient_column,
        label_column,
        slide_column,
        transform_labels=False,
        verbose=verbose,
    )
    project.setup_project_scaffold()
    annotation_file = project.modified_annotations_file

    # Create a minimal dataset (needed for prediction)
    dataset = sf.Dataset(
        slides=str(slide_dir),
        annotations=str(annotation_file)
    )

    # Generate predictions
    try:
        evaluator = Evaluator(
            dataset,
            model_dir,
            output_dir,
            bags_dir,
            verbose=verbose
        )
        evaluator.generate_predictions()

    except Exception as e:
        tb = traceback.format_exc()
        vlog(tb, LogLevel.ERROR)
        vlog(f"Error: {e}", LogLevel.ERROR)
        return

evaluate

evaluate(
    slide_dir: str | Path,
    annotation_file: str | Path,
    bags_dir: str | Path,
    model_dir: str | Path,
    output_dir: str | Path,
    patient_column: str,
    label_column: str,
    slide_column: str | None,
    verbose: bool,
)

Evaluate one or more trained MIL models on a labeled dataset.

This command generates predictions for the slides in SLIDE_DIR using trained models from MODEL_DIR and corresponding tile feature bags from BAGS_DIR. The predictions are evaluated against the provided annotations, and summary metrics and plots are generated.

Parameters:

Name Type Description Default
slide_dir str | Path

Directory containing whole-slide images.

required
annotation_file str | Path

CSV file containing slide- or patient-level annotations and labels.

required
bags_dir str | Path

Directory containing extracted tile feature bags.

required
model_dir str | Path

Directory containing trained model checkpoints.

required
output_dir str | Path

Directory to which evaluation results will be written.

required
patient_column str

Name of the column containing patient identifiers.

required
label_column str

Name of the column containing class labels.

required
slide_column str | None

Name of the column containing slide identifiers.

required
verbose bool

Enables verbose logging output.

required
Examples

Evaluate a single model:

automil evaluate /data/slides /data/annotations.csv /data/bags /data/models/model_1 -o ./results

Evaluate multiple models:

automil evaluate /data/slides /data/annotations.csv /data/bags /data/models -v

Override annotation column names:

automil evaluate -pc "patient_id" -lc "outcome" -sc "slide_id"             /data/slides /data/annotations.csv /data/bags /data/models/model_1             -o ./results
Expected model directory structure

MODEL_DIR may refer either to a single model directory or to a parent directory containing multiple model subdirectories.

Single model example:

/data/models/model_1/
|-- best_valid.pth
|-- ...

Multiple models example:

/data/models/
|-- model_1/
|    |-- best_valid.pth
|-- model_2/
|    |-- best_valid.pth
|    |-- ...
Multiple models

When multiple models are evaluated, AutoMIL generates separate evaluation results for each model and compares their performance.

Annotation file requirements

The annotation file must be a CSV file containing at least the following columns:

  • Patient identifiers (default column name: patient)
  • Slide identifiers (default column name: slide; optional)
  • Class labels (default column name: label)

By default, AutoMIL looks for columns named patient, slide, and label. These defaults can be overridden using the --patient_column, --slide_column, and --label_column options.

Minimal annotation file example
patient,slide,label
001,001_1,0
001,001_2,0
002,002,1
003,003,1
Output directory format

OUTPUT_DIR must be a directory path. Evaluation results, metrics, and plots are written to this directory.

When multiple models are evaluated, output files include a suffix indicating the corresponding model.

Source code in automil/cli.py
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
@AutoMIL.command(
    name="evaluate",
    context_settings=CONTEXT_SETTINGS,
    no_args_is_help=True,
    help=EVALUATE_HELP
)
@click.argument("slide_dir",    type=click.Path(exists=True, file_okay=False))
@click.argument("annotation_file",  type=click.Path(exists=True, file_okay=True))
@click.argument("bags_dir",     type=click.Path(exists=True, file_okay=False))
@click.argument("model_dir",    type=click.Path(exists=True, file_okay=False))
@click.option(
    "-o", "--output-dir", 
    type=click.Path(file_okay=True), default="evaluation",
    help="Directory to which to save evaluation results"
)
@click.option(
    "-pc", "--patient_column", type=str, default="patient",
    help="Name of the column containing patient IDs"
)
@click.option(
    "-lc", "--label_column", type=str, default="label",
    help="Name of the column containing labels"
)
@click.option(
    "-sc", "--slide_column", type=str, default=None,
    help="Name of the column containing slide names"
)
@click.option("-v", "--verbose", is_flag=True, help="Enables additional logging messages")
def evaluate(
    slide_dir:   str | Path,
    annotation_file: str | Path,
    bags_dir:    str | Path,
    model_dir:   str | Path,
    output_dir: str | Path,
    patient_column:  str,
    label_column:    str,
    slide_column:    str | None,
    verbose:     bool
):
    """
    Evaluate one or more trained MIL models on a labeled dataset.

    This command generates predictions for the slides in `SLIDE_DIR` using
    trained models from `MODEL_DIR` and corresponding tile feature bags from
    `BAGS_DIR`. The predictions are evaluated against the provided annotations,
    and summary metrics and plots are generated.

    Args:
        slide_dir (str | Path):
            Directory containing whole-slide images.

        annotation_file (str | Path):
            CSV file containing slide- or patient-level annotations and labels.

        bags_dir (str | Path):
            Directory containing extracted tile feature bags.

        model_dir (str | Path):
            Directory containing trained model checkpoints.

        output_dir (str | Path):
            Directory to which evaluation results will be written.

        patient_column (str):
            Name of the column containing patient identifiers.

        label_column (str):
            Name of the column containing class labels.

        slide_column (str | None):
            Name of the column containing slide identifiers.

        verbose (bool):
            Enables verbose logging output.

    ### Examples

    Evaluate a single model:

        automil evaluate /data/slides /data/annotations.csv /data/bags /data/models/model_1 -o ./results

    Evaluate multiple models:

        automil evaluate /data/slides /data/annotations.csv /data/bags /data/models -v

    Override annotation column names:

        automil evaluate -pc "patient_id" -lc "outcome" -sc "slide_id" \
            /data/slides /data/annotations.csv /data/bags /data/models/model_1 \
            -o ./results

    ### Expected model directory structure

    `MODEL_DIR` may refer either to a single model directory or to a parent
    directory containing multiple model subdirectories.

    Single model example:

        /data/models/model_1/
        |-- best_valid.pth
        |-- ...

    Multiple models example:

        /data/models/
        |-- model_1/
        |    |-- best_valid.pth
        |-- model_2/
        |    |-- best_valid.pth
        |    |-- ...

    ??? Note "Multiple models"
        When multiple models are evaluated, AutoMIL generates separate
        evaluation results for each model and compares their performance.

    ### Annotation file requirements

    The annotation file must be a CSV file containing at least the following columns:

    - Patient identifiers (default column name: `patient`)
    - Slide identifiers (default column name: `slide`; optional)
    - Class labels (default column name: `label`)

    By default, AutoMIL looks for columns named `patient`, `slide`, and `label`.
    These defaults can be overridden using the `--patient_column`,
    `--slide_column`, and `--label_column` options.

    ### Minimal annotation file example

        patient,slide,label
        001,001_1,0
        001,001_2,0
        002,002,1
        003,003,1

    ### Output directory format

    `OUTPUT_DIR` must be a directory path. Evaluation results, metrics, and plots
    are written to this directory.

    When multiple models are evaluated, output files include a suffix indicating
    the corresponding model.
    """
    import slideflow as sf

    from .evaluation import Evaluator
    from .project import Project
    from .util import INFO_CLR, LogLevel, get_vlog

    # Getting a verbose logger
    vlog = get_vlog(verbose)
    sf.setLoggingLevel(20) # INFO: 20, DEBUG: 10

    # Logging the executed command
    command = " ".join(sys.argv)
    vlog(f"Executing command: [{INFO_CLR}]{command}[/]")

    # Some type coercion
    slide_dir =  Path(slide_dir)
    bags_dir =   Path(bags_dir)
    model_dir =  Path(model_dir)
    output_dir = Path(output_dir)

    vlog(f"Evaluating models in: [{INFO_CLR}]{model_dir}[/]")

    # Setup output folder as project (modifies annotation file)
    project = Project(
        Path(output_dir),
        Path(annotation_file),
        Path(slide_dir),
        patient_column,
        label_column,
        slide_column,
        transform_labels=False,
        verbose=verbose,
    )
    project.setup_project_scaffold()
    annotation_file = project.modified_annotations_file

    # Create a minimal dataset (needed for prediction)
    dataset = sf.Dataset(
        slides=str(slide_dir),
        annotations=str(annotation_file)
    )

    # Evaluate models
    try:
        evaluator = Evaluator(
            dataset,
            model_dir,
            output_dir,
            bags_dir,
            verbose=verbose
        )
        evaluator.evaluate_models(generate_attention_heatmaps=True)
        evaluator.compare_models()
        evaluator.generate_plots()

    except Exception as e:
        tb = traceback.format_exc()
        vlog(tb, LogLevel.ERROR)
        vlog(f"Error: {e}", LogLevel.ERROR)
        return

create_split

create_split(
    slide_dir: str | Path,
    annotation_file: str | Path,
    output_file: str | Path,
    test_fraction: float,
    read_only: bool,
    verbose: bool,
)

Create a train–test split file from dataset annotations.

This command reads the provided annotation file and generates a train–test split, which is saved as a JSON file. The resulting split can be reused for reproducible training and evaluation.

Parameters:

Name Type Description Default
slide_dir str | Path

Directory containing whole-slide images.

required
annotation_file str | Path

CSV file containing slide- or patient-level annotations and labels.

required
output_file str | Path

Path to which the split JSON file will be written.

required
test_fraction float

Fraction of samples to assign to the test set.

required
read_only bool

If enabled, an existing split file will not be overwritten.

required
verbose bool

Enables verbose logging output.

required
Examples

Create a split with default settings:

automil create-split /data/slides /data/annotations.csv -o split.json

Create a split without overwriting an existing file:

automil create-split /data/slides /data/annotations.csv -o split.json --read-only
Output file format

The output JSON file contains slide identifiers grouped by split name.

Example structure:

{
"train": ["slide1", "slide2", ...],
"test":  ["slide3", "slide4", ...]
}

Depending on the configuration, a validation split may be generated instead of or in addition to a test split.

Source code in automil/cli.py
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
@AutoMIL.command(
    "create-split",
    context_settings=CONTEXT_SETTINGS,
    no_args_is_help=True,
    help=CREATE_SPLIT_HELP
)
@click.argument("slide_dir",        type=click.Path(exists=True, file_okay=False))
@click.argument("annotation_file",  type=click.Path(exists=True, file_okay=True))
@click.option(
    "-o", "--output-file", type=click.Path(file_okay=True), default="split.json",
    help="Path to which to save the split .json file"
)
@click.option("-f", "--test-fraction", type=float, default=0.2, help="Fraction of slides to include in the test set")
@click.option("-r", "--read-only", is_flag=True, help="If set, existing split file will not be overwritten")
@click.option("-v", "--verbose", is_flag=True, help="Enables additional logging messages")
def create_split(
    slide_dir:       str | Path,
    annotation_file: str | Path,
    output_file:     str | Path,
    test_fraction:   float,
    read_only:       bool,
    verbose:         bool
):
    """
    Create a train–test split file from dataset annotations.

    This command reads the provided annotation file and generates a train–test
    split, which is saved as a JSON file. The resulting split can be reused
    for reproducible training and evaluation.

    Args:
        slide_dir (str | Path):
            Directory containing whole-slide images.

        annotation_file (str | Path):
            CSV file containing slide- or patient-level annotations and labels.

        output_file (str | Path):
            Path to which the split JSON file will be written.

        test_fraction (float):
            Fraction of samples to assign to the test set.

        read_only (bool):
            If enabled, an existing split file will not be overwritten.

        verbose (bool):
            Enables verbose logging output.

    ### Examples

    Create a split with default settings:

        automil create-split /data/slides /data/annotations.csv -o split.json

    Create a split without overwriting an existing file:

        automil create-split /data/slides /data/annotations.csv -o split.json --read-only

    ### Output file format

    The output JSON file contains slide identifiers grouped by split name.

    Example structure:

        {
        "train": ["slide1", "slide2", ...],
        "test":  ["slide3", "slide4", ...]
        }

    Depending on the configuration, a `validation` split may be generated
    instead of or in addition to a `test` split.
    """

    import slideflow as sf

    from .util import INFO_CLR, LogLevel, get_vlog

    # Getting a verbose logger
    vlog = get_vlog(verbose)
    sf.setLoggingLevel(20) # INFO: 20, DEBUG: 10

    # Logging the executed command
    command = " ".join(sys.argv)
    vlog(f"Executing command: [{INFO_CLR}]{command}[/]")

    # Some type coercion
    slide_dir = Path(slide_dir)
    annotation_file = Path(annotation_file)
    output_file = Path(output_file)

    try:
        # Minimal dataset for splitting
        dataset = sf.Dataset(
            slides=str(slide_dir),
            annotations=str(annotation_file)
        )
        # Create the split and save it
        _, _ = dataset.split(
            labels="label",
            val_fraction=test_fraction,
            splits=str(output_file),
            read_only=read_only
        )

    except Exception as e:
        tb = traceback.format_exc()
        vlog(tb, LogLevel.ERROR)
        vlog(f"Error: {e}", LogLevel.ERROR)
        return