From d3206838893113e90a39586f85cb14da99d38840 Mon Sep 17 00:00:00 2001 From: Tim Rheinfels <tim.rheinfels@fau.de> Date: Wed, 24 May 2023 11:32:02 +0200 Subject: [PATCH] evaluation: add utilities for exporting result artifacts --- src/evaluation/artifacts.py | 137 ++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/evaluation/artifacts.py diff --git a/src/evaluation/artifacts.py b/src/evaluation/artifacts.py new file mode 100644 index 0000000..f96ff4a --- /dev/null +++ b/src/evaluation/artifacts.py @@ -0,0 +1,137 @@ +## 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 evaluation/artifacts.py +### +### @brief Provides functionality for handling the evaluation artifacts +### +### @author Tim Rheinfels <tim.rheinfels@fau.de> +### + +import numpy as np +import os +import tabulate + +from state_abstraction import StateAbstraction + +### +### @brief Stores a textual representation of the @p state_abstraction under @p path +### +### @param state_abstraction A @ref state_abstraction.StateAbstraction for which to store the data +### @param path Path to the file which should contain the abstraction details after the call +### +def save_state_abstraction(state_abstraction, path): + assert(isinstance(state_abstraction, StateAbstraction)) + assert(isinstance(path, str)) + + headers = ( + 'Abstraction', + + 'alpha', + 'v_max', + + 'Param. Time', + 'Cond.', + + 'rho', + 'gamma', + 'beta', + 'delta', + 'v_star', + 'v_inf', + ) + + rows = [ + (state_abstraction.name, state_abstraction.alpha, state_abstraction.v_max, state_abstraction.parametrization_time, state_abstraction.condition_number, '--', '--', '--', '--', '--', '--') + ] + + for i, mode in enumerate(state_abstraction.modes): + rows.append( + ('Mode %d: %s' % (i, mode.name), '--', '--', '--', '--', mode.rho, mode.gamma, mode.beta, mode.delta, mode.v_star, mode.v_inf) + ) + + with open(path, 'w') as f: + f.write('Abstraction Data:\n') + f.write('\n') + f.write(tabulate.tabulate(rows, headers)) + f.write('\n') + f.write('\n') + + f.write('Analysis Ellipsoid\' shape matrix P^{-1}:\n') + f.write('\n') + f.write(str(state_abstraction.E_P.Pi)) + f.write('\n') + f.write('Eigenvalues: %s' % list(sorted(np.linalg.eigvalsh(state_abstraction.E_P.Pi)))) + f.write('\n') + f.write('\n') + + f.write('Observer Gains:\n') + f.write('\n') + for i, mode in enumerate(state_abstraction.modes): + f.write('Mode %d: %s\n' % (i, mode.name)) + f.write(str(mode.L)) + f.write('\n') + f.write('\n') + +### +### @brief Stores a textual representation of the evaluation @p statistics under @p path +### +### @param statistics The result of a call to @ref statistics.compute_statistics to be stored +### @param path Path to the directory which should contain the statistics after the call +### +def save_statistics(statistics, path): + assert(isinstance(statistics, dict)) + assert(set(statistics.keys()) == {'sigma', 'norm_x_P', 'v_sys', 'v', 'v_over_v_sys'}) + assert(isinstance(path, str)) + + def format_table(statistic): + headers = ('Switching \ Disturbance', 'No', 'Yes', 'Combined') + rows = [ + ('No', '%.4f' % statistic[0, 0], '%.4f' % statistic[0, 1], '%.4f' % statistic[0, 2]), + ('Yes', '%.4f' % statistic[1, 0], '%.4f' % statistic[1, 1], '%.4f' % statistic[1, 2]), + ('Combined', '%.4f' % statistic[2, 0], '%.4f' % statistic[2, 1], '%.4f' % statistic[2, 2]), + ] + + return tabulate.tabulate(rows, headers) + + # Save switching statistics + os.makedirs(os.path.join(path, 'sigma'), exist_ok=True) + for statistic_name in ('counts', 'ratios'): + with open(os.path.join(path, 'sigma', '%s.txt' % statistic_name), 'w') as f: + statistic = statistics['sigma'][statistic_name] + for sigma in range(statistic.shape[2]): + f.writelines([ + 'Mode: %d\n' % sigma, + '\n', + format_table(statistic[:, :, sigma]), + '\n', + '\n', + '\n', + ]) + + # Save state statistics + for statistic_name in ('norm_x_P', 'v_sys', 'v', 'v_over_v_sys'): + statistic = statistics[statistic_name] + with open(os.path.join(path, '%s.txt' % statistic_name), 'w') as f: + statistic = statistics[statistic_name] + for statistic_type in ('mean', 'std', 'max', 'violations'): + f.writelines([ + 'Type: %s\n' % statistic_type, + '\n', + format_table(statistic[statistic_type]), + '\n', + '\n', + '\n', + ]) -- GitLab