diff --git a/src/visualization/measurements.py b/src/visualization/measurements.py new file mode 100644 index 0000000000000000000000000000000000000000..f9a46fcb416e169c29cc955484798c84a2855ca1 --- /dev/null +++ b/src/visualization/measurements.py @@ -0,0 +1,111 @@ +## This file is part of the simulative evaluation for the qronos observer abstractions. +## Copyright (C) 2022-2023 Tim Rheinfels <tim.rheinfels@fau.de> +## See https://gitlab.cs.fau.de/qronos-state-abstractions/simulation +## +## Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +## +## 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +## +## 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +## +## 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +### +### @file visualization/measurements.py +### +### @brief Provides the visualization of @cite ECRTS23-Rheinfels figure 3 +### +### @author Tim Rheinfels <tim.rheinfels@fau.de> +### + +import itertools +import matplotlib.pyplot as plot +import numpy as np + +from simulation.closed_loop import ClosedLoopSimulation +from visualization.color import scale_lightness + +### +### @brief Visualizes the results from multiple @ref closed_loop.ClosedLoopSimulation instances with respect to their individual state abstractions +### +### @param ax The matplotlib.Axes object to render to +### @param experiments List of @ref closed_loop.ClosedLoopSimulation instances +### @param names Names associated with the @p experiments +### @param which_run Index of the run to visualize (defaults to 0) +### +def visualize_measurements(ax, experiments, names, which_run=0): + assert(len(experiments) == len(names)) + assert(len(experiments) > 0) + + colors = ( + scale_lightness('C0', 0.5), + scale_lightness('C0', 0.75), + scale_lightness('C0', 1.0), + ) + linestyles=( + 'dashdot', + 'dashed', + 'solid', + ) + + timesteps = None + for i in range(len(experiments)): + experiment = experiments[i] + name = names[i] + + assert(isinstance(experiment, ClosedLoopSimulation)) + + run = experiment.get_run(which_run) + + # Get data from experiment + state_abstraction = experiment.state_abstraction_simulation.abstraction + assert(np.isclose(state_abstraction.v_max, 1.0)) + + # Extract signals, match lengths, and convert to numpy arrays + t = np.array(run['sigma']['time_data']) + if timesteps is not None: + assert(t.size == timesteps) + timesteps = t.size + + v = np.array(run['v']['signal_data'][:timesteps]) + v_sys = np.concatenate(run['v_sys']['signal_data'][:timesteps]) + + # === Plot common data === + if i == 0: + + # Plot specification + ax.plot((0, timesteps-1), (1, 1), color='C1', linewidth=2, label=r'Safety Specification: $v_{max}$') + + # Plot area where dropouts occur + k_start = max(experiment.switching_sequence_simulation.T_start, 0) + k_end = min(experiment.switching_sequence_simulation.T_end, timesteps-1) + ax.axvspan(k_start, k_end, color='darkgray', alpha=0.4, label=r'Scenario: 50% Dropout in $\sigma_k$') + + # === Plot abstraction data === + # Plot abstraction + ax.plot(t, v, color=colors[i], linestyle=linestyles[i], linewidth=2, label=name) + + if i == len(experiments) - 1: + # Plot system safety output scaled to be comparable with the abstraction + ax.plot(t, state_abstraction.v_max * v_sys, color='k', linestyle=':', linewidth=2, label=r'Specification Output: $v_{max} \cdot ||s_k||_S$') + + + # Some styling + ax.set_xlabel('Time Step $k$') + ax.set_ylabel('Abstraction Domain') + ax.set_ylim([-0.05, 1.05]) + ax.grid() + + legend = ax.legend(loc='center right', ncol=1, bbox_to_anchor=(0.95, 0.5)) + + for text in legend.get_texts(): + if not text.get_text().startswith(r'\Large '): + continue + + text.set( + text = text.get_text()[7:], + size = 'medium', + weight = 32, + )