From ec569ad56dcaff837ff8f99959c31e74662ec3c1 Mon Sep 17 00:00:00 2001 From: Charly LAMOTHE <lamothe.c@intlocal.univ-amu.fr> Date: Tue, 5 Nov 2019 12:14:49 +0100 Subject: [PATCH] - Remove useless error handling files; - Save and load json files from __dict__. --- code/bolsonaro/data/dataset_parameters.py | 28 ++----- code/bolsonaro/error_handling/color_print.py | 61 -------------- .../error_handling/console_logger.py | 81 ------------------- .../error_handling/exception_decorators.py | 55 ------------- code/bolsonaro/models/model_parameters.py | 24 ++---- code/bolsonaro/utils.py | 17 ++++ 6 files changed, 29 insertions(+), 237 deletions(-) delete mode 100644 code/bolsonaro/error_handling/color_print.py delete mode 100644 code/bolsonaro/error_handling/console_logger.py delete mode 100644 code/bolsonaro/error_handling/exception_decorators.py diff --git a/code/bolsonaro/data/dataset_parameters.py b/code/bolsonaro/data/dataset_parameters.py index 9e7e7de..e251c75 100644 --- a/code/bolsonaro/data/dataset_parameters.py +++ b/code/bolsonaro/data/dataset_parameters.py @@ -1,4 +1,5 @@ -import json +from bolsonaro.utils import save_obj_to_json, load_obj_from_json + import os @@ -37,27 +38,10 @@ class DatasetParameters(object): return self._dataset_normalizer def save(self, directory_path, experiment_id): - with open(directory_path + os.sep + 'dataset_parameters_{}.json'.format(experiment_id), 'w') as output_file: - json.dump({ - 'name': self._name, - 'test_size': self._test_size, - 'dev_size': self._dev_size, - 'random_state': self._random_state, - 'dataset_normalizer': self._dataset_normalizer, - 'train_on_subset': self._train_on_subset - }, - output_file, - indent=4) + save_obj_to_json(directory_path + os.sep + 'dataset_parameters_{}.json'.format(experiment_id), + self.__dict__) @staticmethod def load(directory_path, experiment_id): - with open(directory_path + os.sep + 'dataset_parameters_{}.json'.format(experiment_id), 'r') as input_file: - parameters = json.load(input_file) - return DatasetParameters( - name=parameters['name'], - test_size=parameters['test_size'], - dev_size=parameters['dev_size'], - random_state=parameters['random_state'], - dataset_normalizer=parameters['dataset_normalizer'], - train_on_subset=parameters['train_on_subset'] - ) + return load_obj_from_json(directory_path + os.sep + 'dataset_parameters_{}.json'.format(experiment_id), + DatasetParameters) diff --git a/code/bolsonaro/error_handling/color_print.py b/code/bolsonaro/error_handling/color_print.py deleted file mode 100644 index b577e5a..0000000 --- a/code/bolsonaro/error_handling/color_print.py +++ /dev/null @@ -1,61 +0,0 @@ - ##################################################################################### - # MIT License # - # # - # Copyright (C) 2019 Charly Lamothe # - # # - # This file is part of VQ-VAE-Speech. # - # # - # 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. # - ##################################################################################### - -import sys - - -class ColorPrint(object): - """ Colored printing functions for strings that use universal ANSI escape sequences. - - fail: bold red, pass: bold green, warn: bold yellow, - info: bold blue, bold: bold white - - :source: https://stackoverflow.com/a/47622205 - """ - - @staticmethod - def print_fail(message, end='\n'): - sys.stderr.write('\x1b[1;31m' + message.strip() + '\x1b[0m' + end) - - @staticmethod - def print_pass(message, end='\n'): - sys.stdout.write('\x1b[1;32m' + message.strip() + '\x1b[0m' + end) - - @staticmethod - def print_warn(message, end='\n'): - sys.stderr.write('\x1b[1;33m' + message.strip() + '\x1b[0m' + end) - - @staticmethod - def print_info(message, end='\n'): - sys.stdout.write('\x1b[1;34m' + message.strip() + '\x1b[0m' + end) - - @staticmethod - def print_major_fail(message, end='\n'): - sys.stdout.write('\x1b[1;35m' + message.strip() + '\x1b[0m' + end) - - @staticmethod - def print_bold(message, end='\n'): - sys.stdout.write('\x1b[1;37m' + message.strip() + '\x1b[0m' + end) diff --git a/code/bolsonaro/error_handling/console_logger.py b/code/bolsonaro/error_handling/console_logger.py deleted file mode 100644 index 93d34d6..0000000 --- a/code/bolsonaro/error_handling/console_logger.py +++ /dev/null @@ -1,81 +0,0 @@ - ##################################################################################### - # MIT License # - # # - # Copyright (C) 2019 Charly Lamothe # - # # - # This file is part of VQ-VAE-Speech. # - # # - # 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 bolsonaro.error_handling.color_print import ColorPrint - -import sys -import traceback -import os - - -class ConsoleLogger(object): - - @staticmethod - def status(message): - if os.name == 'nt': - print('[~] {message}'.format(message=message)) - else: - ColorPrint.print_info('[~] {message}'.format(message=message)) - - @staticmethod - def success(message): - if os.name == 'nt': - print('[+] {message}'.format(message=message)) - else: - ColorPrint.print_pass('[+] {message}'.format(message=message)) - - @staticmethod - def error(message): - if sys.exc_info()[2]: - line = traceback.extract_tb(sys.exc_info()[2])[-1].lineno - error_message = '[-] {message} with cause: {cause} (line {line})'.format( \ - message=message, cause=str(sys.exc_info()[1]), line=line) - else: - error_message = '[-] {message}'.format(message=message) - if os.name == 'nt': - print(error_message) - else: - ColorPrint.print_fail(error_message) - - @staticmethod - def warn(message): - if os.name == 'nt': - print('[-] {message}'.format(message=message)) - else: - ColorPrint.print_warn('[-] {message}'.format(message=message)) - - @staticmethod - def critical(message): - if sys.exc_info()[2]: - line = traceback.extract_tb(sys.exc_info()[2])[-1].lineno - error_message = '[!] {message} with cause: {cause} (line {line})'.format( \ - message=message, cause=str(sys.exc_info()[1]), line=line) - else: - error_message = '[!] {message}'.format(message=message) - if os.name == 'nt': - print(error_message) - else: - ColorPrint.print_major_fail(error_message) diff --git a/code/bolsonaro/error_handling/exception_decorators.py b/code/bolsonaro/error_handling/exception_decorators.py deleted file mode 100644 index 428c618..0000000 --- a/code/bolsonaro/error_handling/exception_decorators.py +++ /dev/null @@ -1,55 +0,0 @@ - ##################################################################################### - # MIT License # - # # - # Copyright (C) 2019 Charly Lamothe # - # # - # This file is part of VQ-VAE-Speech. # - # # - # 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 functools import wraps - - -class InvalidRaiseException(Exception): - pass - - -def only_throws(E): - """ - :source: https://stackoverflow.com/a/18289516 - """ - - def decorator(f): - @wraps(f) - def wrapped(*args, **kwargs): - try: - return f(*args, **kwargs) - except E: - raise - except InvalidRaiseException: - raise - except Exception as e: - raise InvalidRaiseException( - 'got %s, expected %s, from %s' % (e.__class__.__name__, E.__name__, f.__name__) - ) - - return wrapped - - return decorator diff --git a/code/bolsonaro/models/model_parameters.py b/code/bolsonaro/models/model_parameters.py index 93bb177..7198dce 100644 --- a/code/bolsonaro/models/model_parameters.py +++ b/code/bolsonaro/models/model_parameters.py @@ -1,4 +1,5 @@ -import json +from bolsonaro.utils import save_obj_to_json, load_obj_from_json + import os @@ -27,23 +28,10 @@ class ModelParameters(object): return self._normalize_D def save(self, directory_path, experiment_id): - with open(directory_path + os.sep + 'model_parameters_{}.json'.format(experiment_id), 'w') as output_file: - json.dump({ - 'forest_size': self._forest_size, - 'extracted_forest_size': self._extracted_forest_size, - 'seed': self._seed, - 'normalize_D': self._normalize_D - }, - output_file, - indent=4) + save_obj_to_json(directory_path + os.sep + 'model_parameters_{}.json'.format(experiment_id), + self.__dict__) @staticmethod def load(directory_path, experiment_id): - with open(directory_path + os.sep + 'model_parameters_{}.json'.format(experiment_id), 'r') as input_file: - parameters = json.load(input_file) - return ModelParameters( - forest_size=parameters['forest_size'], - extracted_forest_size=parameters['extracted_forest_size'], - seed=parameters['seed'], - normalize_D=parameters['normalize_D'] - ) + return load_obj_from_json(directory_path + os.sep + 'model_parameters_{}.json'.format(experiment_id), + ModelParameters) diff --git a/code/bolsonaro/utils.py b/code/bolsonaro/utils.py index 7e39c13..4186eef 100644 --- a/code/bolsonaro/utils.py +++ b/code/bolsonaro/utils.py @@ -1,4 +1,5 @@ import os +import json def resolve_experiment_id(models_dir): @@ -16,3 +17,19 @@ def resolve_experiment_id(models_dir): ids.sort(key=int) return int(max([int(i) for i in ids])) + 1 return 1 + +def save_obj_to_json(file_path, attributes_dict): + attributes = dict() + for key, value in attributes_dict.items(): + attributes[key[1:]] = value + with open(file_path, 'w') as output_file: + json.dump( + attributes, + output_file, + indent=4 + ) + +def load_obj_from_json(file_path, constructor): + with open(file_path, 'r') as input_file: + parameters = json.load(input_file) + return constructor(**parameters) -- GitLab