Source code for dpemu.utils

# MIT License
#
# Copyright (c) 2019 Tuomas Halvari, Juha Harviainen, Juha Mylläri, Antti Röyskö, Juuso Silvennoinen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from datetime import datetime
from os.path import join
from pathlib import Path


[docs]def generate_unique_path(folder_name, extension, prefix=None): """Generates a unique path name with desired folder name, extension and prefix. Args: folder_name (str): The name of the folder which the file path should contain. extension (str): The file extension of the file. prefix (str, optional): The optional prefix of the filename. Defaults to None. Returns: str: The path generated by the function. """ root_folder = get_project_root() timestamp = datetime.now().strftime("%Y%m%d-%H%M%S-%f") if prefix: return join(root_folder, "{}/{}_{}.{}".format(folder_name, prefix, timestamp, extension)) return join(root_folder, "{}/{}.{}".format(folder_name, timestamp, extension))
[docs]def get_project_root(): """Returns a path to the root of the project. Returns: pathlib.PosixPath: The path to the root of the project. """ return Path(__file__).resolve().parents[1]
[docs]def get_data_dir(): """Returns a path to the directory where datasets are saved. Returns: pathlib.PosixPath: The path to the directory where datasets are saved. """ root_dir = get_project_root() return root_dir / "data"
[docs]def split_df_by_model(df): """Splits a dataframe such that each model gets its own dataframe. Args: df (pandas.DataFrame): A dataframe containing all the data returned by the runner. Returns: dfs: A list of dataframes. """ dfs = [] for model_name, df_ in df.groupby("model_name"): df_ = df_.dropna(axis=1, how="all") df_ = df_.drop("model_name", axis=1) df_ = df_.reset_index(drop=True) df_.name = model_name dfs.append(df_) return dfs
[docs]def filter_optimized_results(df, err_param_name, score_name, is_higher_score_better): """Removes suboptimal rows from the dataframe, returning only the best ones. Args: df (pandas.DataFrame): A dataframe containing all the data returned by the runner. err_param_name (str): The name of the error parameter by which the data will be grouped. score_name (str): The name of the score type we want to optimize. is_higher_score_better (bool): If true, then only the highest results are returned. Otherwise the lowest results are returned. Returns: pandas.DataFrame: A dataframe containing the optimized results. """ if is_higher_score_better: df_ = df.loc[df.groupby(err_param_name, sort=False)[score_name].idxmax()].reset_index(drop=True) else: df_ = df.loc[df.groupby(err_param_name, sort=False)[score_name].idxmin()].reset_index(drop=True) df_.name = df.name return df_