Skip to content
Snippets Groups Projects
Commit ec569ad5 authored by Charly LAMOTHE's avatar Charly LAMOTHE
Browse files

- Remove useless error handling files;

- Save and load json files from __dict__.
parent 44e43b58
No related branches found
No related tags found
1 merge request!3clean scripts
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)
#####################################################################################
# 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)
#####################################################################################
# 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)
#####################################################################################
# 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
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)
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment