Skip to content

Sensitivity plots

sensitivity_plots

Classes

SensitivityBarPlot

SensitivityBarPlot(filename='sensitivity_bar_plot')

Bases: SensitivityPlot

Source code in src/vimseo/tools/post_tools/sensitivity_plots.py
50
51
52
53
54
55
def __init__(self, filename="sensitivity_bar_plot"):
    super().__init__()
    self.options = {
        "filename": filename,
        "n_variables": 10,
    }
Functions
plot
plot(
    results,
    save=False,
    show=True,
    output_names=(),
    indices=(),
    directory_path: str | Path = "",
    **options
)

Parameters:

  • results

    A SensitivityTool result.

  • title

    The title of the figure to be plotted.

  • **options

    Specific options for the plot

Returns:

  • A figure object.

Source code in src/vimseo/tools/post_tools/sensitivity_plots.py
 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
def plot(
    self,
    results,
    save=False,
    show=True,
    output_names=(),
    indices=(),
    directory_path: str | Path = "",
    **options,
):
    self.options.update(options)

    indices = list(results.indices.keys()) if len(indices) == 0 else indices
    output_names = (
        results.metadata.settings["output_names"]
        if len(output_names) == 0
        else output_names
    )

    data = deepcopy(results.indices)

    self.figures = defaultdict(dict)

    for output_name in output_names:
        for index in indices:
            fig = make_subplots(
                rows=1,
                cols=1,
                subplot_titles=f"Index: {index}",
            )

            input_variables = [
                k
                for k, v in sorted(
                    data[index][output_name][0].items(),
                    key=operator.itemgetter(1),
                )
            ]

            color_by_variable_name = names_to_ints(input_variables)

            x = (
                pd.DataFrame
                .from_dict(data[index][output_name])
                .T.sort_values(0, ascending=True)
                .to_dict()[0]
            )
            input_variables = list(x.keys())

            if self.options["n_variables"] is not None:
                input_variables = input_variables[0 : self.options["n_variables"]]
                color_by_variable_name = color_by_variable_name[
                    0 : self.options["n_variables"]
                ]
                x = [x[k][0] for k in input_variables]

            fig.add_trace(
                go.Bar(
                    x=x,
                    y=input_variables,
                    marker={"color": array(color_by_variable_name)},
                    orientation="h",
                ),
                row=1,
                col=1,
            )

            fig.update_layout(
                title_text=f"Sensitivity of {output_name} for index {index}"
            )
            fig.layout.title.x = 0.5
            fig.update_layout(font_size=14)
            # adjust subplot title font size.
            fig.update_annotations(font_size=12)

            if show:
                fig.show()
            if save:
                directory_path = (
                    Path.cwd() if directory_path == "" else Path(directory_path)
                )
                fig.write_html(
                    directory_path
                    / f"{self.options['filename']}_{output_name}_{index}.html"
                )
            self.figures[output_name][index] = fig

    return self.figures

SensitivityPlot

SensitivityPlot()

Bases: ABC

Source code in src/vimseo/tools/post_tools/sensitivity_plots.py
32
33
def __init__(self):
    self.figures = None
Functions
plot abstractmethod
plot(
    results, save, show, variable, indices=None, **options
)

Parameters:

  • results

    A SensitivityTool result.

  • title

    The title of the figure to be plotted.

  • **options

    Specific options for the plot

Returns:

  • A figure object.

Source code in src/vimseo/tools/post_tools/sensitivity_plots.py
35
36
37
38
39
40
41
42
43
44
45
46
@abstractmethod
def plot(self, results, save, show, variable, indices=None, **options):
    """

    Args:
        results: A SensitivityTool result.
        title: The title of the figure to be plotted.
        **options: Specific options for the plot

    Returns:
        A figure object.
    """