Skip to content

Output Setting

Overview¤

A module for handling the output setting passed in the get_shots_data method. The output setting is used to handle the output of data from DisruptionPy as it is retrieved. This may include collecting all the data from a request and returning it as a list or streaming outputted data to a file as it is retrieved.

This module defines the abstract class OutputSetting that can have subclasses passed as the output_setting argument to the get_shots_data method. It also provides built-in classes and mappings to easily set the output type for common use cases.

Usage¤

Currently, these are the options that can be passed as the output_setting argument to get_shots_data:

  • An instance of a subclass of OutputSetting
  • A string identifier in the _output_setting_mappings dictionary:
    _output_setting_mappings: Dict[str, OutputSetting] = {
        "list": ListOutputSetting(),
        "dataframe": DataFrameOutputSetting(),
        "dict": DictOutputSetting(),
    }
    
  • A file path as a string with its suffix mapped to an OutputSetting type in the _file_suffix_to_output_setting dictionary:
    _file_suffix_to_output_setting: Dict[str, Type[OutputSetting]] = {
        ".h5": HDF5OutputSetting,
        ".hdf5": HDF5OutputSetting,
        ".csv": BatchedCSVOutputSetting,
    }
    
  • A dictionary mapping tokamak type strings to the desired OutputSetting for that tokamak. E.g. {'cmod': 'list'}.

  • A Python list of any other output type request option that can be passed as the OutputSetting argument to get_shots_data (all options listed previously). See ListOutputSetting for more details.

Built-in Implemenations¤

Handles output settings for retrieving and saving shot data.

This module provides classes and methods to manage various output settings for shot data, including saving to files, databases, lists, dictionaries, and dataframes.

disruption_py.settings.output_setting.BatchedCSVOutputSetting ¤

Bases: OutputSetting

Stream outputted data to a single CSV file in batches.

Source code in disruption_py/settings/output_setting.py
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
class BatchedCSVOutputSetting(OutputSetting):
    """
    Stream outputted data to a single CSV file in batches.
    """

    def __init__(self, filepath, batch_size=100, clear_file=True):
        """
        Initialize the BatchedCSVOutputSetting.

        Parameters
        ----------
        filepath : str
            The path to the CSV file where data will be written.
        batch_size : int, optional
            The number of records to write to the CSV file in one batch (default is 100).
        clear_file : bool, optional
            Whether to clear the file at the beginning (default is True).
        """
        self.filepath = filepath
        self.batch_size = batch_size
        self.clear_file = clear_file
        self.batch_data = []  # Initialize an empty list to hold batched data
        self.output_shot_count = 0

        # Clear the file at the beginning if required
        if self.clear_file and os.path.exists(filepath):
            os.remove(filepath)

        self.results: pd.DataFrame = pd.DataFrame()

    def _output_shot(self, params: OutputSettingParams):
        """
        Append the current result to the batch data list and write to CSV if
        batch size is reached.

        Parameters
        ----------
        params : OutputSettingParams
            The parameters containing the result to be outputted.
        """
        # Append the current result to the batch data list
        self.batch_data.append(params.result)

        # Check if the batch size has been reached
        if len(self.batch_data) >= self.batch_size:
            self._write_batch_to_csv()

        self.output_shot_count += 1
        self.results = safe_df_concat(self.results, [params.result])

    def _write_batch_to_csv(self):
        """
        Write the current batch of data to the CSV file.
        """
        file_exists = os.path.isfile(self.filepath)
        combined_df = safe_df_concat(pd.DataFrame(), self.batch_data)
        combined_df.to_csv(
            self.filepath, mode="a", index=False, header=(not file_exists)
        )
        self.batch_data.clear()

    def get_results(self, params: CompleteOutputSettingParams):
        """
        Write any remaining batched data to the CSV file before returning results.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for retrieving results.

        Returns
        -------
        pd.DataFrame
            The DataFrame containing the results.
        """
        # Write any remaining batched data to the CSV file before returning results
        if self.batch_data:
            self._write_batch_to_csv()
        return self.results

    def stream_output_cleanup(self, params: CompleteOutputSettingParams):
        """
        Clean up the output stream by resetting results.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for cleaning up the output stream.
        """
        self.results = pd.DataFrame()

__init__ ¤

__init__(filepath, batch_size=100, clear_file=True)
PARAMETER DESCRIPTION
filepath

The path to the CSV file where data will be written.

TYPE: str

batch_size

The number of records to write to the CSV file in one batch (default is 100).

TYPE: int DEFAULT: 100

clear_file

Whether to clear the file at the beginning (default is True).

TYPE: bool DEFAULT: True

Source code in disruption_py/settings/output_setting.py
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
def __init__(self, filepath, batch_size=100, clear_file=True):
    """
    Initialize the BatchedCSVOutputSetting.

    Parameters
    ----------
    filepath : str
        The path to the CSV file where data will be written.
    batch_size : int, optional
        The number of records to write to the CSV file in one batch (default is 100).
    clear_file : bool, optional
        Whether to clear the file at the beginning (default is True).
    """
    self.filepath = filepath
    self.batch_size = batch_size
    self.clear_file = clear_file
    self.batch_data = []  # Initialize an empty list to hold batched data
    self.output_shot_count = 0

    # Clear the file at the beginning if required
    if self.clear_file and os.path.exists(filepath):
        os.remove(filepath)

    self.results: pd.DataFrame = pd.DataFrame()

_output_shot ¤

_output_shot(params: OutputSettingParams)

Append the current result to the batch data list and write to CSV if batch size is reached.

PARAMETER DESCRIPTION
params

The parameters containing the result to be outputted.

TYPE: OutputSettingParams

Source code in disruption_py/settings/output_setting.py
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
def _output_shot(self, params: OutputSettingParams):
    """
    Append the current result to the batch data list and write to CSV if
    batch size is reached.

    Parameters
    ----------
    params : OutputSettingParams
        The parameters containing the result to be outputted.
    """
    # Append the current result to the batch data list
    self.batch_data.append(params.result)

    # Check if the batch size has been reached
    if len(self.batch_data) >= self.batch_size:
        self._write_batch_to_csv()

    self.output_shot_count += 1
    self.results = safe_df_concat(self.results, [params.result])

_write_batch_to_csv ¤

_write_batch_to_csv()

Write the current batch of data to the CSV file.

Source code in disruption_py/settings/output_setting.py
619
620
621
622
623
624
625
626
627
628
def _write_batch_to_csv(self):
    """
    Write the current batch of data to the CSV file.
    """
    file_exists = os.path.isfile(self.filepath)
    combined_df = safe_df_concat(pd.DataFrame(), self.batch_data)
    combined_df.to_csv(
        self.filepath, mode="a", index=False, header=(not file_exists)
    )
    self.batch_data.clear()

get_results ¤

get_results(params: CompleteOutputSettingParams)

Write any remaining batched data to the CSV file before returning results.

PARAMETER DESCRIPTION
params

The parameters for retrieving results.

TYPE: CompleteOutputSettingParams

RETURNS DESCRIPTION
DataFrame

The DataFrame containing the results.

Source code in disruption_py/settings/output_setting.py
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
def get_results(self, params: CompleteOutputSettingParams):
    """
    Write any remaining batched data to the CSV file before returning results.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for retrieving results.

    Returns
    -------
    pd.DataFrame
        The DataFrame containing the results.
    """
    # Write any remaining batched data to the CSV file before returning results
    if self.batch_data:
        self._write_batch_to_csv()
    return self.results

stream_output_cleanup ¤

stream_output_cleanup(params: CompleteOutputSettingParams)

Clean up the output stream by resetting results.

PARAMETER DESCRIPTION
params

The parameters for cleaning up the output stream.

TYPE: CompleteOutputSettingParams

Source code in disruption_py/settings/output_setting.py
649
650
651
652
653
654
655
656
657
658
def stream_output_cleanup(self, params: CompleteOutputSettingParams):
    """
    Clean up the output stream by resetting results.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for cleaning up the output stream.
    """
    self.results = pd.DataFrame()

disruption_py.settings.output_setting.CSVOutputSetting ¤

Bases: OutputSetting

Outputs shot data to a single CSV file. Not recommended when retrieving a large number of shots.

Source code in disruption_py/settings/output_setting.py
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
class CSVOutputSetting(OutputSetting):
    """
    Outputs shot data to a single CSV file.
    Not recommended when retrieving a large number of shots.
    """

    def __init__(
        self, filepath: str, flexible_columns: bool = True, clear_file: bool = True
    ):
        """
        Initialize CSVOutputSetting with a file path and options.

        Parameters
        ----------
        filepath : str
            The path to the CSV file.
        flexible_columns : bool, optional
            If True, allows for flexible columns in the CSV (default is True).
        clear_file : bool, optional
            If True, clears the file if it exists (default is True).
        """
        self.filepath = filepath
        self.flexible_columns = flexible_columns
        self.output_shot_count = 0
        if clear_file and os.path.exists(filepath):
            os.remove(filepath)
        self.results: pd.DataFrame = pd.DataFrame()

    def _output_shot(self, params: OutputSettingParams):
        """
        Output a single shot to the CSV file.

        Parameters
        ----------
        params : OutputSettingParams
            The parameters for outputting shot results.
        """
        file_exists = os.path.isfile(self.filepath)
        if self.flexible_columns:
            if file_exists:
                existing_df = pd.read_csv(self.filepath)
                combined_df = safe_df_concat(existing_df, [params.result])
            else:
                combined_df = params.result

            combined_df.to_csv(self.filepath, index=False)
        else:
            params.result.to_csv(
                self.filepath, mode="a", index=False, header=(not file_exists)
            )
        self.output_shot_count += 1
        self.results = safe_df_concat(self.results, [params.result])

    def get_results(self, params: CompleteOutputSettingParams):
        """
        Get the accumulated results.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for output cleanup and result fetching.

        Returns
        -------
        pd.DataFrame
            The combined DataFrame of results.
        """
        return self.results

    def stream_output_cleanup(self, params: CompleteOutputSettingParams):
        """
        Cleanup the results DataFrame.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for output cleanup and result fetching.
        """
        self.results = pd.DataFrame()

__init__ ¤

__init__(
    filepath: str,
    flexible_columns: bool = True,
    clear_file: bool = True,
)
PARAMETER DESCRIPTION
filepath

The path to the CSV file.

TYPE: str

flexible_columns

If True, allows for flexible columns in the CSV (default is True).

TYPE: bool DEFAULT: True

clear_file

If True, clears the file if it exists (default is True).

TYPE: bool DEFAULT: True

Source code in disruption_py/settings/output_setting.py
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
def __init__(
    self, filepath: str, flexible_columns: bool = True, clear_file: bool = True
):
    """
    Initialize CSVOutputSetting with a file path and options.

    Parameters
    ----------
    filepath : str
        The path to the CSV file.
    flexible_columns : bool, optional
        If True, allows for flexible columns in the CSV (default is True).
    clear_file : bool, optional
        If True, clears the file if it exists (default is True).
    """
    self.filepath = filepath
    self.flexible_columns = flexible_columns
    self.output_shot_count = 0
    if clear_file and os.path.exists(filepath):
        os.remove(filepath)
    self.results: pd.DataFrame = pd.DataFrame()

_output_shot ¤

_output_shot(params: OutputSettingParams)

Output a single shot to the CSV file.

PARAMETER DESCRIPTION
params

The parameters for outputting shot results.

TYPE: OutputSettingParams

Source code in disruption_py/settings/output_setting.py
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
def _output_shot(self, params: OutputSettingParams):
    """
    Output a single shot to the CSV file.

    Parameters
    ----------
    params : OutputSettingParams
        The parameters for outputting shot results.
    """
    file_exists = os.path.isfile(self.filepath)
    if self.flexible_columns:
        if file_exists:
            existing_df = pd.read_csv(self.filepath)
            combined_df = safe_df_concat(existing_df, [params.result])
        else:
            combined_df = params.result

        combined_df.to_csv(self.filepath, index=False)
    else:
        params.result.to_csv(
            self.filepath, mode="a", index=False, header=(not file_exists)
        )
    self.output_shot_count += 1
    self.results = safe_df_concat(self.results, [params.result])

get_results ¤

get_results(params: CompleteOutputSettingParams)

Get the accumulated results.

PARAMETER DESCRIPTION
params

The parameters for output cleanup and result fetching.

TYPE: CompleteOutputSettingParams

RETURNS DESCRIPTION
DataFrame

The combined DataFrame of results.

Source code in disruption_py/settings/output_setting.py
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
def get_results(self, params: CompleteOutputSettingParams):
    """
    Get the accumulated results.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for output cleanup and result fetching.

    Returns
    -------
    pd.DataFrame
        The combined DataFrame of results.
    """
    return self.results

stream_output_cleanup ¤

stream_output_cleanup(params: CompleteOutputSettingParams)

Cleanup the results DataFrame.

PARAMETER DESCRIPTION
params

The parameters for output cleanup and result fetching.

TYPE: CompleteOutputSettingParams

Source code in disruption_py/settings/output_setting.py
557
558
559
560
561
562
563
564
565
566
def stream_output_cleanup(self, params: CompleteOutputSettingParams):
    """
    Cleanup the results DataFrame.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for output cleanup and result fetching.
    """
    self.results = pd.DataFrame()

disruption_py.settings.output_setting.DataFrameOutputSetting ¤

Bases: OutputSetting

Outputs all shot data as a single DataFrame.

Source code in disruption_py/settings/output_setting.py
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
class DataFrameOutputSetting(OutputSetting):
    """
    Outputs all shot data as a single DataFrame.
    """

    def __init__(self):
        """Initialize DataFrameOutputSetting with an empty DataFrame."""
        self.results: pd.DataFrame = pd.DataFrame()

    def _output_shot(self, params: OutputSettingParams):
        """
        Output a single shot by concatenating the result to the DataFrame.

        Parameters
        ----------
        params : OutputSettingParams
            The parameters for outputting shot results.
        """
        self.results = safe_df_concat(self.results, [params.result])

    def get_results(self, params: CompleteOutputSettingParams):
        """
        Get the accumulated results.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for output cleanup and result fetching.

        Returns
        -------
        pd.DataFrame
            The combined DataFrame of results.
        """
        return self.results

    def stream_output_cleanup(self, params: CompleteOutputSettingParams):
        """
        Cleanup the results DataFrame.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for output cleanup and result fetching.
        """
        self.results = pd.DataFrame()

__init__ ¤

__init__()
Source code in disruption_py/settings/output_setting.py
368
369
370
def __init__(self):
    """Initialize DataFrameOutputSetting with an empty DataFrame."""
    self.results: pd.DataFrame = pd.DataFrame()

_output_shot ¤

_output_shot(params: OutputSettingParams)

Output a single shot by concatenating the result to the DataFrame.

PARAMETER DESCRIPTION
params

The parameters for outputting shot results.

TYPE: OutputSettingParams

Source code in disruption_py/settings/output_setting.py
372
373
374
375
376
377
378
379
380
381
def _output_shot(self, params: OutputSettingParams):
    """
    Output a single shot by concatenating the result to the DataFrame.

    Parameters
    ----------
    params : OutputSettingParams
        The parameters for outputting shot results.
    """
    self.results = safe_df_concat(self.results, [params.result])

get_results ¤

get_results(params: CompleteOutputSettingParams)

Get the accumulated results.

PARAMETER DESCRIPTION
params

The parameters for output cleanup and result fetching.

TYPE: CompleteOutputSettingParams

RETURNS DESCRIPTION
DataFrame

The combined DataFrame of results.

Source code in disruption_py/settings/output_setting.py
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
def get_results(self, params: CompleteOutputSettingParams):
    """
    Get the accumulated results.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for output cleanup and result fetching.

    Returns
    -------
    pd.DataFrame
        The combined DataFrame of results.
    """
    return self.results

stream_output_cleanup ¤

stream_output_cleanup(params: CompleteOutputSettingParams)

Cleanup the results DataFrame.

PARAMETER DESCRIPTION
params

The parameters for output cleanup and result fetching.

TYPE: CompleteOutputSettingParams

Source code in disruption_py/settings/output_setting.py
399
400
401
402
403
404
405
406
407
408
def stream_output_cleanup(self, params: CompleteOutputSettingParams):
    """
    Cleanup the results DataFrame.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for output cleanup and result fetching.
    """
    self.results = pd.DataFrame()

disruption_py.settings.output_setting.DictOutputSetting ¤

Bases: OutputSetting

Outputs shot data as a dict of DataFrames keyed by shot number.

Source code in disruption_py/settings/output_setting.py
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
class DictOutputSetting(OutputSetting):
    """
    Outputs shot data as a dict of DataFrames keyed by shot number.
    """

    def __init__(self):
        """Initialize DictOutputSetting with an empty results dictionary."""
        self.results = {}

    def _output_shot(self, params: OutputSettingParams):
        """
        Output a single shot by storing the result in the dictionary.

        Parameters
        ----------
        params : OutputSettingParams
            The parameters for outputting shot results.
        """
        self.results[params.shot_id] = params.result

    def get_results(self, params: CompleteOutputSettingParams):
        """
        Get the accumulated results.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for output cleanup and result fetching.

        Returns
        -------
        Dict[int, pd.DataFrame]
            The dictionary of results keyed by shot number.
        """
        return self.results

    def stream_output_cleanup(self, params: CompleteOutputSettingParams):
        """
        Cleanup the results dictionary.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for output cleanup and result fetching.
        """
        self.results = {}

__init__ ¤

__init__()
Source code in disruption_py/settings/output_setting.py
320
321
322
def __init__(self):
    """Initialize DictOutputSetting with an empty results dictionary."""
    self.results = {}

_output_shot ¤

_output_shot(params: OutputSettingParams)

Output a single shot by storing the result in the dictionary.

PARAMETER DESCRIPTION
params

The parameters for outputting shot results.

TYPE: OutputSettingParams

Source code in disruption_py/settings/output_setting.py
324
325
326
327
328
329
330
331
332
333
def _output_shot(self, params: OutputSettingParams):
    """
    Output a single shot by storing the result in the dictionary.

    Parameters
    ----------
    params : OutputSettingParams
        The parameters for outputting shot results.
    """
    self.results[params.shot_id] = params.result

get_results ¤

get_results(params: CompleteOutputSettingParams)

Get the accumulated results.

PARAMETER DESCRIPTION
params

The parameters for output cleanup and result fetching.

TYPE: CompleteOutputSettingParams

RETURNS DESCRIPTION
Dict[int, DataFrame]

The dictionary of results keyed by shot number.

Source code in disruption_py/settings/output_setting.py
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
def get_results(self, params: CompleteOutputSettingParams):
    """
    Get the accumulated results.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for output cleanup and result fetching.

    Returns
    -------
    Dict[int, pd.DataFrame]
        The dictionary of results keyed by shot number.
    """
    return self.results

stream_output_cleanup ¤

stream_output_cleanup(params: CompleteOutputSettingParams)

Cleanup the results dictionary.

PARAMETER DESCRIPTION
params

The parameters for output cleanup and result fetching.

TYPE: CompleteOutputSettingParams

Source code in disruption_py/settings/output_setting.py
351
352
353
354
355
356
357
358
359
360
def stream_output_cleanup(self, params: CompleteOutputSettingParams):
    """
    Cleanup the results dictionary.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for output cleanup and result fetching.
    """
    self.results = {}

disruption_py.settings.output_setting.HDF5OutputSetting ¤

Bases: OutputSetting

Stream outputted data to an HDF5 file. Data for each shot is stored in a table under the key df_SHOTID.

Source code in disruption_py/settings/output_setting.py
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
class HDF5OutputSetting(OutputSetting):
    """
    Stream outputted data to an HDF5 file. Data for each shot is stored in a table
    under the key `df_SHOTID`.
    """

    def __init__(self, filepath: str, only_output_numeric: bool = False):
        """
        Initialize HDF5OutputSetting with a file path and numeric output option.

        Parameters
        ----------
        filepath : str
            The path to the HDF5 file.
        only_output_numeric : bool, optional
            If True, only numeric data will be outputted (default is False) and
            non-numeric quantities like commit hash will be excluded.
        """
        self.filepath = filepath
        self.output_shot_count = 0
        self.only_output_numeric = only_output_numeric
        self.results: pd.DataFrame = pd.DataFrame()

    def _output_shot(self, params: OutputSettingParams):
        """
        Output a single shot to the HDF5 file.

        Parameters
        ----------
        params : OutputSettingParams
            The parameters for outputting shot results.
        """
        mode = "a" if self.output_shot_count > 0 else "w"

        if self.only_output_numeric:
            output_result = params.result.select_dtypes([np.number])
        else:
            output_result = params.result

        output_result.to_hdf(
            self.filepath,
            key=f"df_{params.shot_id}",
            format="table",
            complib="blosc",
            mode=mode,
        )
        self.output_shot_count += 1
        self.results = safe_df_concat(self.results, [params.result])

    def get_results(self, params: CompleteOutputSettingParams):
        """
        Get the accumulated results.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for output cleanup and result fetching.

        Returns
        -------
        pd.DataFrame
            The combined DataFrame of results.
        """
        return self.results

    def stream_output_cleanup(self, params: CompleteOutputSettingParams):
        """
        Cleanup the results DataFrame.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for output cleanup and result fetching.
        """
        self.results = pd.DataFrame()

__init__ ¤

__init__(filepath: str, only_output_numeric: bool = False)
PARAMETER DESCRIPTION
filepath

The path to the HDF5 file.

TYPE: str

only_output_numeric

If True, only numeric data will be outputted (default is False) and non-numeric quantities like commit hash will be excluded.

TYPE: bool DEFAULT: False

Source code in disruption_py/settings/output_setting.py
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
def __init__(self, filepath: str, only_output_numeric: bool = False):
    """
    Initialize HDF5OutputSetting with a file path and numeric output option.

    Parameters
    ----------
    filepath : str
        The path to the HDF5 file.
    only_output_numeric : bool, optional
        If True, only numeric data will be outputted (default is False) and
        non-numeric quantities like commit hash will be excluded.
    """
    self.filepath = filepath
    self.output_shot_count = 0
    self.only_output_numeric = only_output_numeric
    self.results: pd.DataFrame = pd.DataFrame()

_output_shot ¤

_output_shot(params: OutputSettingParams)

Output a single shot to the HDF5 file.

PARAMETER DESCRIPTION
params

The parameters for outputting shot results.

TYPE: OutputSettingParams

Source code in disruption_py/settings/output_setting.py
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
def _output_shot(self, params: OutputSettingParams):
    """
    Output a single shot to the HDF5 file.

    Parameters
    ----------
    params : OutputSettingParams
        The parameters for outputting shot results.
    """
    mode = "a" if self.output_shot_count > 0 else "w"

    if self.only_output_numeric:
        output_result = params.result.select_dtypes([np.number])
    else:
        output_result = params.result

    output_result.to_hdf(
        self.filepath,
        key=f"df_{params.shot_id}",
        format="table",
        complib="blosc",
        mode=mode,
    )
    self.output_shot_count += 1
    self.results = safe_df_concat(self.results, [params.result])

get_results ¤

get_results(params: CompleteOutputSettingParams)

Get the accumulated results.

PARAMETER DESCRIPTION
params

The parameters for output cleanup and result fetching.

TYPE: CompleteOutputSettingParams

RETURNS DESCRIPTION
DataFrame

The combined DataFrame of results.

Source code in disruption_py/settings/output_setting.py
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
def get_results(self, params: CompleteOutputSettingParams):
    """
    Get the accumulated results.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for output cleanup and result fetching.

    Returns
    -------
    pd.DataFrame
        The combined DataFrame of results.
    """
    return self.results

stream_output_cleanup ¤

stream_output_cleanup(params: CompleteOutputSettingParams)

Cleanup the results DataFrame.

PARAMETER DESCRIPTION
params

The parameters for output cleanup and result fetching.

TYPE: CompleteOutputSettingParams

Source code in disruption_py/settings/output_setting.py
476
477
478
479
480
481
482
483
484
485
def stream_output_cleanup(self, params: CompleteOutputSettingParams):
    """
    Cleanup the results DataFrame.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for output cleanup and result fetching.
    """
    self.results = pd.DataFrame()

disruption_py.settings.output_setting.ListOutputSetting ¤

Bases: OutputSetting

Outputs shot data as a list of DataFrames.

Source code in disruption_py/settings/output_setting.py
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
class ListOutputSetting(OutputSetting):
    """
    Outputs shot data as a list of DataFrames.
    """

    def __init__(self):
        """Initialize ListOutputSetting with an empty results list."""
        self.results = []

    def _output_shot(self, params: OutputSettingParams):
        """
        Output a single shot by appending the result to the list.

        Parameters
        ----------
        params : OutputSettingParams
            The parameters for outputting shot results.
        """
        self.results.append(params.result)

    def get_results(self, params: CompleteOutputSettingParams):
        """
        Get the accumulated results.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for output cleanup and result fetching.

        Returns
        -------
        List[pd.DataFrame]
            The list of results.
        """
        return self.results

    def stream_output_cleanup(self, params: CompleteOutputSettingParams):
        """
        Cleanup the results list.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for output cleanup and result fetching.
        """
        self.results = []

__init__ ¤

__init__()
Source code in disruption_py/settings/output_setting.py
272
273
274
def __init__(self):
    """Initialize ListOutputSetting with an empty results list."""
    self.results = []

_output_shot ¤

_output_shot(params: OutputSettingParams)

Output a single shot by appending the result to the list.

PARAMETER DESCRIPTION
params

The parameters for outputting shot results.

TYPE: OutputSettingParams

Source code in disruption_py/settings/output_setting.py
276
277
278
279
280
281
282
283
284
285
def _output_shot(self, params: OutputSettingParams):
    """
    Output a single shot by appending the result to the list.

    Parameters
    ----------
    params : OutputSettingParams
        The parameters for outputting shot results.
    """
    self.results.append(params.result)

get_results ¤

get_results(params: CompleteOutputSettingParams)

Get the accumulated results.

PARAMETER DESCRIPTION
params

The parameters for output cleanup and result fetching.

TYPE: CompleteOutputSettingParams

RETURNS DESCRIPTION
List[DataFrame]

The list of results.

Source code in disruption_py/settings/output_setting.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
def get_results(self, params: CompleteOutputSettingParams):
    """
    Get the accumulated results.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for output cleanup and result fetching.

    Returns
    -------
    List[pd.DataFrame]
        The list of results.
    """
    return self.results

stream_output_cleanup ¤

stream_output_cleanup(params: CompleteOutputSettingParams)

Cleanup the results list.

PARAMETER DESCRIPTION
params

The parameters for output cleanup and result fetching.

TYPE: CompleteOutputSettingParams

Source code in disruption_py/settings/output_setting.py
303
304
305
306
307
308
309
310
311
312
def stream_output_cleanup(self, params: CompleteOutputSettingParams):
    """
    Cleanup the results list.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for output cleanup and result fetching.
    """
    self.results = []

disruption_py.settings.output_setting.SQLOutputSetting ¤

Bases: OutputSetting

Stream outputted data to a SQL table. By default, stream to the test table: disruption_warning_test.

Source code in disruption_py/settings/output_setting.py
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
class SQLOutputSetting(OutputSetting):
    """
    Stream outputted data to a SQL table. By default, stream to the test table:
    disruption_warning_test.
    """

    def __init__(
        self,
        should_update=False,
        should_override_columns: List[str] = None,
        table_name="disruption_warning_test",
    ):
        """
        Initialize the SQLOutputSetting.

        Parameters
        ----------
        should_update : bool, optional
            Whether to update existing records (default is False).
        should_override_columns : List[str], optional
            List of columns to override in the SQL table (default is None).
        table_name : str, optional
            The name of the SQL table to stream data to (default is
            "disruption_warning_test").
        """
        self.should_update = should_update
        self.should_override_columns = should_override_columns
        self.table_name = table_name
        self.results: pd.DataFrame = pd.DataFrame()

    def _output_shot(self, params: OutputSettingParams):
        """
        Output the current shot data to the SQL table.

        Parameters
        ----------
        params : OutputSettingParams
            The parameters containing the result to be outputted.
        """
        if not params.result.empty and ("shot" in params.result.columns):
            shot_id = params.result["shot"].iloc[0]
            params.database.add_shot_data(
                shot_id=shot_id,
                shot_data=params.result,
                update=self.should_update,
                override_columns=self.should_override_columns,
            )
        else:
            params.logger.warning("No shot id found in result DataFrame")
        self.results = safe_df_concat(self.results, [params.result])

    def get_results(self, params: CompleteOutputSettingParams) -> Any:
        """
        Retrieve the results stored in the SQL output setting.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for retrieving results.

        Returns
        -------
        pd.DataFrame
            The DataFrame containing the results.
        """
        return self.results

    def stream_output_cleanup(self, params: CompleteOutputSettingParams):
        """
        Clean up the output stream by resetting results.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for cleaning up the output stream.
        """
        self.results = pd.DataFrame()

__init__ ¤

__init__(
    should_update=False,
    should_override_columns: List[str] = None,
    table_name="disruption_warning_test",
)
PARAMETER DESCRIPTION
should_update

Whether to update existing records (default is False).

TYPE: bool DEFAULT: False

should_override_columns

List of columns to override in the SQL table (default is None).

TYPE: List[str] DEFAULT: None

table_name

The name of the SQL table to stream data to (default is "disruption_warning_test").

TYPE: str DEFAULT: 'disruption_warning_test'

Source code in disruption_py/settings/output_setting.py
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
def __init__(
    self,
    should_update=False,
    should_override_columns: List[str] = None,
    table_name="disruption_warning_test",
):
    """
    Initialize the SQLOutputSetting.

    Parameters
    ----------
    should_update : bool, optional
        Whether to update existing records (default is False).
    should_override_columns : List[str], optional
        List of columns to override in the SQL table (default is None).
    table_name : str, optional
        The name of the SQL table to stream data to (default is
        "disruption_warning_test").
    """
    self.should_update = should_update
    self.should_override_columns = should_override_columns
    self.table_name = table_name
    self.results: pd.DataFrame = pd.DataFrame()

_output_shot ¤

_output_shot(params: OutputSettingParams)

Output the current shot data to the SQL table.

PARAMETER DESCRIPTION
params

The parameters containing the result to be outputted.

TYPE: OutputSettingParams

Source code in disruption_py/settings/output_setting.py
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
def _output_shot(self, params: OutputSettingParams):
    """
    Output the current shot data to the SQL table.

    Parameters
    ----------
    params : OutputSettingParams
        The parameters containing the result to be outputted.
    """
    if not params.result.empty and ("shot" in params.result.columns):
        shot_id = params.result["shot"].iloc[0]
        params.database.add_shot_data(
            shot_id=shot_id,
            shot_data=params.result,
            update=self.should_update,
            override_columns=self.should_override_columns,
        )
    else:
        params.logger.warning("No shot id found in result DataFrame")
    self.results = safe_df_concat(self.results, [params.result])

get_results ¤

get_results(params: CompleteOutputSettingParams) -> Any

Retrieve the results stored in the SQL output setting.

PARAMETER DESCRIPTION
params

The parameters for retrieving results.

TYPE: CompleteOutputSettingParams

RETURNS DESCRIPTION
DataFrame

The DataFrame containing the results.

Source code in disruption_py/settings/output_setting.py
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
def get_results(self, params: CompleteOutputSettingParams) -> Any:
    """
    Retrieve the results stored in the SQL output setting.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for retrieving results.

    Returns
    -------
    pd.DataFrame
        The DataFrame containing the results.
    """
    return self.results

stream_output_cleanup ¤

stream_output_cleanup(params: CompleteOutputSettingParams)

Clean up the output stream by resetting results.

PARAMETER DESCRIPTION
params

The parameters for cleaning up the output stream.

TYPE: CompleteOutputSettingParams

Source code in disruption_py/settings/output_setting.py
728
729
730
731
732
733
734
735
736
737
def stream_output_cleanup(self, params: CompleteOutputSettingParams):
    """
    Clean up the output stream by resetting results.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for cleaning up the output stream.
    """
    self.results = pd.DataFrame()

disruption_py.settings.output_setting.resolve_output_setting ¤

resolve_output_setting(
    output_setting: OutputSettingType,
) -> OutputSetting

Resolve the output setting to an OutputSetting instance.

PARAMETER DESCRIPTION
output_setting

The output setting to resolve, which can be an instance of OutputSetting, a string, a dictionary, or a list.

TYPE: OutputSettingType

RETURNS DESCRIPTION
OutputSetting

The resolved OutputSetting instance.

Source code in disruption_py/settings/output_setting.py
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
def resolve_output_setting(
    output_setting: OutputSettingType,
) -> OutputSetting:
    """
    Resolve the output setting to an OutputSetting instance.

    Parameters
    ----------
    output_setting : OutputSettingType
        The output setting to resolve, which can be an instance of OutputSetting,
        a string, a dictionary, or a list.

    Returns
    -------
    OutputSetting
        The resolved OutputSetting instance.
    """
    if isinstance(output_setting, OutputSetting):
        return output_setting

    if isinstance(output_setting, str):
        output_setting_object = _output_setting_mappings.get(output_setting, None)
        if output_setting_object is not None:
            return output_setting_object

    if isinstance(output_setting, str):
        # assume that it is a file path
        for (
            suffix,
            output_setting_type,
        ) in _file_suffix_to_output_setting.items():
            if output_setting.endswith(suffix):
                return output_setting_type(output_setting)

    if isinstance(output_setting, dict):
        return OutputSettingDict(output_setting)

    if isinstance(output_setting, list):
        return OutputSettingList(output_setting)

    raise ValueError(f"Invalid output processor {output_setting}")

Custom Implementations¤

Custom implementations of output type requests must inherit from the OutputTypeRequest abstract class, implementing the abstract methods.

Handles output settings for retrieving and saving shot data.

This module provides classes and methods to manage various output settings for shot data, including saving to files, databases, lists, dictionaries, and dataframes.

disruption_py.settings.output_setting.OutputSetting ¤

Bases: ABC

OutputSetting abstract class that should be inherited by all output setting classes.

Source code in disruption_py/settings/output_setting.py
 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
class OutputSetting(ABC):
    """
    OutputSetting abstract class that should be inherited by all output setting classes.
    """

    def output_shot(self, params: OutputSettingParams):
        """
        Output a single shot based on the provided parameters.

        Parameters
        ----------
        params : OutputSettingParams
            The parameters for outputting shot results.
        """
        if hasattr(self, "tokamak_overrides"):
            if params.tokamak in self.tokamak_overrides:
                return self.tokamak_overrides[params.tokamak](params)
        return self._output_shot(params)

    @abstractmethod
    def _output_shot(self, params: OutputSettingParams):
        """
        Abstract method implemented by subclasses to handle data output for a
        single shot.

        Parameters
        ----------
        params : OutputSettingParams
            The parameters for outputting shot results.
        """

    def stream_output_cleanup(self, params: CompleteOutputSettingParams):
        """
        Empty method optionally overridden by subclasses to handle cleanup after
        all shots have been output. This may include closing files or other cleanup.
        Subclasses should implement this method so multiple output types can be
        used for the same data without appending to the other's outputted dataframe.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for output cleanup and result fetching.
        """

    @abstractmethod
    def get_results(self, params: CompleteOutputSettingParams) -> Any:
        """
        Return final output after all shots are processed.

        Parameters
        ----------
        params : CompleteOutputSettingParams
            The parameters for output cleanup and result fetching.

        Returns
        -------
        Any
            The final output results.
        """

_output_shot abstractmethod ¤

_output_shot(params: OutputSettingParams)

Abstract method implemented by subclasses to handle data output for a single shot.

PARAMETER DESCRIPTION
params

The parameters for outputting shot results.

TYPE: OutputSettingParams

Source code in disruption_py/settings/output_setting.py
 96
 97
 98
 99
100
101
102
103
104
105
106
@abstractmethod
def _output_shot(self, params: OutputSettingParams):
    """
    Abstract method implemented by subclasses to handle data output for a
    single shot.

    Parameters
    ----------
    params : OutputSettingParams
        The parameters for outputting shot results.
    """

get_results abstractmethod ¤

get_results(params: CompleteOutputSettingParams) -> Any

Return final output after all shots are processed.

PARAMETER DESCRIPTION
params

The parameters for output cleanup and result fetching.

TYPE: CompleteOutputSettingParams

RETURNS DESCRIPTION
Any

The final output results.

Source code in disruption_py/settings/output_setting.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
@abstractmethod
def get_results(self, params: CompleteOutputSettingParams) -> Any:
    """
    Return final output after all shots are processed.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for output cleanup and result fetching.

    Returns
    -------
    Any
        The final output results.
    """

output_shot ¤

output_shot(params: OutputSettingParams)

Output a single shot based on the provided parameters.

PARAMETER DESCRIPTION
params

The parameters for outputting shot results.

TYPE: OutputSettingParams

Source code in disruption_py/settings/output_setting.py
82
83
84
85
86
87
88
89
90
91
92
93
94
def output_shot(self, params: OutputSettingParams):
    """
    Output a single shot based on the provided parameters.

    Parameters
    ----------
    params : OutputSettingParams
        The parameters for outputting shot results.
    """
    if hasattr(self, "tokamak_overrides"):
        if params.tokamak in self.tokamak_overrides:
            return self.tokamak_overrides[params.tokamak](params)
    return self._output_shot(params)

stream_output_cleanup ¤

stream_output_cleanup(params: CompleteOutputSettingParams)

Empty method optionally overridden by subclasses to handle cleanup after all shots have been output. This may include closing files or other cleanup. Subclasses should implement this method so multiple output types can be used for the same data without appending to the other's outputted dataframe.

PARAMETER DESCRIPTION
params

The parameters for output cleanup and result fetching.

TYPE: CompleteOutputSettingParams

Source code in disruption_py/settings/output_setting.py
108
109
110
111
112
113
114
115
116
117
118
119
def stream_output_cleanup(self, params: CompleteOutputSettingParams):
    """
    Empty method optionally overridden by subclasses to handle cleanup after
    all shots have been output. This may include closing files or other cleanup.
    Subclasses should implement this method so multiple output types can be
    used for the same data without appending to the other's outputted dataframe.

    Parameters
    ----------
    params : CompleteOutputSettingParams
        The parameters for output cleanup and result fetching.
    """

disruption_py.settings.output_setting.OutputSettingParams dataclass ¤

Parameters for outputting shot results.

ATTRIBUTE DESCRIPTION
shot_id

Shot ID.

TYPE: int

result

DataFrame of shot results.

TYPE: DataFrame

database

Database connection for retrieving cache data.

TYPE: ShotDatabase

tokamak

The tokamak for which results are being outputted.

TYPE: Tokamak

logger

Logger instance.

TYPE: Logger

Source code in disruption_py/settings/output_setting.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@dataclass
class OutputSettingParams:
    """
    Parameters for outputting shot results.

    Attributes
    ----------
    shot_id : int
        Shot ID.
    result : pd.DataFrame
        DataFrame of shot results.
    database : ShotDatabase
        Database connection for retrieving cache data.
    tokamak : Tokamak
        The tokamak for which results are being outputted.
    logger : Logger
        Logger instance.
    """

    shot_id: int
    result: pd.DataFrame
    database: ShotDatabase
    tokamak: Tokamak
    logger: Logger

disruption_py.settings.output_setting.CompleteOutputSettingParams dataclass ¤

Parameters for output cleanup and result fetching.

ATTRIBUTE DESCRIPTION
tokamak

The tokamak for which results are being outputted.

TYPE: Tokamak

logger

Logger instance.

TYPE: Logger

Source code in disruption_py/settings/output_setting.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@dataclass
class CompleteOutputSettingParams:
    """
    Parameters for output cleanup and result fetching.

    Attributes
    ----------
    tokamak : Tokamak
        The tokamak for which results are being outputted.
    logger : Logger
        Logger instance.
    """

    tokamak: Tokamak
    logger: Logger