Source code for param_study.plot

import os
from pathlib import Path

import json
import logging
from typing import Type
import numpy as np
import pandas as pd

import plotly.express as px

[docs]class Plotter(): def __init__(self, parameter_study_name: str = 'default') -> None: self.config_key_strings = [ 'ukf/sigma_alpha', 'ukf/P/0', 'ukf/P/1', 'ukf/P/2', 'ukf/P/3', 'ukf/template_Q/0', 'ukf/template_Q/1', 'ukf/template_Q/2', 'ukf/template_Q/3', 'ukf/minimal_cone_covariance_area', 'local_mapping/data_association/chi2_quantile', 'local_mapping/fov_gate/min_distance', 'local_mapping/fov_gate/max_distance', 'local_mapping/fov_gate/angle', 'local_mapping/R_azimuth', 'local_mapping/R_distance_factor', 'local_mapping/R_distance_offset', 'novatel_position/R_scaling', 'novatel_heading/R_scaling', 'wheelspeed/R', ] self.informations_strings = [ 'timesteps_n', 'timesteps_taking_too_long_n', 'last_time_step', 'landmarks_n', 'landmarks_lap1_n', 'landmarks_lap2_n' ] self.parameter_study_name = parameter_study_name self.import_analysis()
[docs] def import_analysis(self): logging.info(f'Import analysis for parameter study "{self.parameter_study_name}"') with open(f'{os.path.abspath(os.path.dirname(__file__))}/../../param_study/{self.parameter_study_name}/analysis.json', 'r') as input: self.analysis = json.load(input) self.x = np.array([int(test_case_index) for test_case_index in self.analysis.keys()]) self.successful = [informations['successful'] for informations in self.analysis.values()]
[docs] def run(self): self.plot_config() self.plot_informations()
[docs] def get_config_values(self, key_string): return np.array([self.get_config_value(config=informations['config'], key_string=key_string) for informations in self.analysis.values()])
[docs] @staticmethod def get_config_value(config: dict, key_string: str): dct = config.copy() for key in key_string.split('/'): try: dct = dct[key] except TypeError: dct = dct[int(key)] except KeyError: return None return dct
[docs] def plot(self, df: pd.DataFrame, y_column: str) -> str: if y_column == 'ukf/sigma_alpha': fig = px.scatter(df, x='test_case_index', y=y_column, color='successful', marginal_y='violin', template='simple_white', title=y_column, log_y=True) else: fig = px.scatter(df, x='test_case_index', y=y_column, color='successful', marginal_y='violin', template='simple_white', title=y_column) return fig.to_html(full_html=False, include_plotlyjs='cdn')
[docs] def plot_config(self): dataframe_dict = {'test_case_index': self.x, 'successful': self.successful} for key_string in self.config_key_strings: dataframe_dict[key_string] = self.get_config_values(key_string=key_string) df = pd.DataFrame(dataframe_dict) base_path = f'{os.path.abspath(os.path.dirname(__file__))}/../../param_study/{self.parameter_study_name}/analysis' Path(base_path).mkdir(parents=True, exist_ok=True) with open(f'{base_path}/config_plots.html', 'w') as config_plots: for key_string in self.config_key_strings: config_plots.write(self.plot(df=df, y_column=key_string))
[docs] def plot_informations(self): dataframe_dict = {'test_case_index': self.x, 'successful': self.successful} for informations_string in self.informations_strings: dataframe_dict[informations_string] = np.array([informations.get(informations_string, 0) for informations in self.analysis.values()]) df = pd.DataFrame(dataframe_dict) base_path = f'{os.path.abspath(os.path.dirname(__file__))}/../../param_study/{self.parameter_study_name}/analysis' Path(base_path).mkdir(parents=True, exist_ok=True) with open(f'{base_path}/analysis_plots.html', 'w') as config_plots: for informations_string in self.informations_strings: config_plots.write(self.plot(df=df, y_column=informations_string))
if __name__ == '__main__': plotter = Plotter(parameter_study_name='test') plotter.run()