def get_field_names(self): """Get fields names Args: Returns: Field names in the dataset Raises: None """ Precondition.is_true(isinstance(self.data_array, np.ndarray), 'Invalid data_array') # find the index of the field_name in data_array field_names = self.data_array[0, :].tolist() return field_names
def __init__(self, config_file_path=DEFAULT_CONFIG_FILE_NAME): """Init Args: config_file_path: absolute path to the config file Returns: None Raises: None """ Precondition.is_string(config_file_path, 'Invalid config_file_path') self.config_file_path = config_file_path self.config = self.load(self.config_file_path)
def get_field(self, field_name, field_dtype=np.str_): """Get a field from the data_array and convert to specified data_type Args: field_name: name of the field to be returned field_dtype: datatype to be used Returns: An NP array of field values for the given field name Raises: None """ Precondition.is_true(isinstance(self.data_array, np.ndarray), 'Invalid data_array') # find the index of the field_name in data_array field_list = self.get_field_names() field_index = field_list.index(field_name) field_data = self.data_array[1:, field_index] return field_data.astype(field_dtype)
def __init__(self, log_file_path, logger_impl, logger_name=None): """Init Args: log_file_path: absolute path to the log file logger_impl: logger implementation object logger_name: name of the log file Returns: None Raises: None """ Precondition.is_string(log_file_path, 'Invalid log_file_path') Precondition.is_true(logger_impl, 'Invalid logger_impl') self.log_file_path = log_file_path self.log_file_name = logger_name self.logger_impl = logger_impl
def __init__(self, dataset_csv_file_path, logger=None, should_load=True): """Init Args: dataset_csv_file_path: absolute path to the csv file logger: shared logger (could be null) should_load: should the dataset (default = True) Returns: None Raises: None """ Precondition.is_string(dataset_csv_file_path, 'Invalid dataset_csv_file_path') self.dataset_csv_file_path = dataset_csv_file_path self.logger = logger self.data_array = [] if should_load: self.load(self.dataset_csv_file_path)
def __init__(self, analysis_name, log_file_path, output_folder_path): """Init Args: analysis_name: name of the analysis log_file_path: path to the log file output_folder_path: output folder path Returns: None Raises: None """ if sys.version_info[0] < 3: raise UnsupportedPythongException('Python Version must be >= 3.0') Precondition.is_string(analysis_name, 'Invalid analysis_name') Precondition.is_string(log_file_path, 'Invalid log_file_path') Precondition.is_string(output_folder_path, 'Invalid output_folder_path') self.analysis_name = analysis_name self.logger = Logger.create(os.path.abspath(log_file_path), self.analysis_name) self.output_folder_path = os.path.abspath(output_folder_path) # create output folder if it doesn't exist if not os.path.exists(output_folder_path): os.makedirs(output_folder_path)
def create(log_file_path, logger_name=None, level=None): """Creates the log file Args: log_file_path: absolute path to the log file logger_name: name of the logger level: logging level Returns: None Raises: None """ Precondition.is_string(log_file_path, 'Invalid log_file_path') if level is None: level = logging.DEBUG # configure log file log_file_folder_path = os.path.dirname(os.path.realpath(log_file_path)) if not os.path.exists(log_file_folder_path): os.makedirs(log_file_folder_path) logger_impl = logging.getLogger(logger_name) Assertion.is_true(logger_impl, 'Invalid logger_impl') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') handler = logging.FileHandler(log_file_path) handler.setFormatter(formatter) logger_impl.addHandler(handler) logger_impl.setLevel(level) return Logger(log_file_path, logger_impl, logger_name)
def test_is_methods_exceptions(self): """Test is_* methods exceptions Args: self: TestPrecondition Returns: None Raises: None """ # is_true with self.assertRaises(PreconditionException) as context: Precondition.is_true(False, 'Test for boolean failed') self.assertTrue('Test for boolean failed' in str(context.exception)) # is_string with self.assertRaises(PreconditionException) as context: Precondition.is_string(1, 'Test for string failed') self.assertTrue('Test for string failed' in str(context.exception)) # is_integer with self.assertRaises(PreconditionException) as context: Precondition.is_integer('not integer', 'Test for integer failed') self.assertTrue('Test for integer failed' in str(context.exception)) # is_positive_integer with self.assertRaises(PreconditionException) as context: Precondition.is_positive_integer( 0, 'Test for positive integer failed') self.assertTrue( 'Test for positive integer failed' in str(context.exception)) # is_non_negative_integer with self.assertRaises(PreconditionException) as context: Precondition.is_non_negative_integer( -1, 'Test for non-negative integer failed') self.assertTrue( 'Test for non-negative integer failed' in str(context.exception)) # is_array with self.assertRaises(PreconditionException) as context: Precondition.is_array(0, 'Test for array failed') self.assertTrue('Test for array failed' in str(context.exception)) # is_dict with self.assertRaises(PreconditionException) as context: Precondition.is_dict([1, 2], 'Test for dict failed') self.assertTrue('Test for dict failed' in str(context.exception))
def test_is_methods_go_right(self): """Test is_* methods Args: self: TestPrecondition Returns: None Raises: None """ Precondition.is_true(True, 'Test for boolean failed') Precondition.is_string('Test', 'Test for string failed') Precondition.is_integer(1, 'Test for integer failed') Precondition.is_positive_integer(1, 'Test for positive integer failed') Precondition.is_non_negative_integer( 0, 'Test for positive integer failed') Precondition.is_array([1, 3], 'Test for array failed') Precondition.is_dict({'a': 'test'}, 'Test for message failed')