Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEA: Give logger a color #761

Merged
merged 19 commits into from
Mar 12, 2021
11 changes: 6 additions & 5 deletions recbole/config/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from recbole.evaluator import group_metrics, individual_metrics
from recbole.utils import get_model, Enum, EvaluatorType, ModelType, InputType, \
general_arguments, training_arguments, evaluation_arguments, dataset_arguments
from recbole.utils.utils import set_color


class Config(object):
Expand Down Expand Up @@ -337,18 +338,18 @@ def __contains__(self, key):
return key in self.final_config_dict

def __str__(self):
args_info = ''
args_info = '\n'
for category in self.parameters:
args_info += category + ' Hyper Parameters: \n'
args_info += set_color(category + ' Hyper Parameters:\n', 'pink')
args_info += '\n'.join([
"{}={}".format(arg, value) for arg, value in self.final_config_dict.items()
(set_color("{}", 'cyan') + " =" + set_color(" {}", 'yellow')).format(arg, value) for arg, value in self.final_config_dict.items()
if arg in self.parameters[category]
])
args_info += '\n\n'

args_info += 'Other Hyper Parameters: \n'
args_info += set_color('Other Hyper Parameters: \n', 'pink')
args_info += '\n'.join([
"{}={}".format(arg, value) for arg, value in self.final_config_dict.items()
(set_color("{}", 'cyan') + " = " + set_color("{}", 'yellow')).format(arg, value) for arg, value in self.final_config_dict.items()
if arg not in {_ for args in self.parameters.values() for _ in args}.union({'model', 'dataset', 'config_files'})
])
args_info += '\n\n'
Expand Down
20 changes: 11 additions & 9 deletions recbole/config/eval_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
################################
"""

from recbole.utils.utils import set_color


class EvalSetting(object):
"""Class containing settings about model evaluation.
Expand Down Expand Up @@ -90,27 +92,27 @@ def __init__(self, config):
setattr(self, args, config[args])

def __str__(self):
info = ['Evaluation Setting:']
info = [set_color('Evaluation Setting:', 'pink')]

if self.group_field:
info.append('Group by {}'.format(self.group_field))
info.append(set_color('Group by', 'blue') + f' {self.group_field}')
else:
info.append('No Grouping')
info.append(set_color('No Grouping', 'yellow'))

if self.ordering_args is not None and self.ordering_args['strategy'] != 'none':
info.append('Ordering: {}'.format(self.ordering_args))
info.append(set_color('Ordering', 'blue') + f': {self.ordering_args}')
else:
info.append('No Ordering')
info.append(set_color('No Ordering', 'yellow'))

if self.split_args is not None and self.split_args['strategy'] != 'none':
info.append('Splitting: {}'.format(self.split_args))
info.append(set_color('Splitting', 'blue') + f': {self.split_args}')
else:
info.append('No Splitting')
info.append(set_color('No Splitting', 'yellow'))

if self.neg_sample_args is not None and self.neg_sample_args['strategy'] != 'none':
info.append('Negative Sampling: {}'.format(self.neg_sample_args))
info.append(set_color('Negative Sampling', 'blue') + f': {self.neg_sample_args}')
else:
info.append('No Negative Sampling')
info.append(set_color('No Negative Sampling', 'yellow'))

return '\n\t'.join(info)

Expand Down
41 changes: 21 additions & 20 deletions recbole/data/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from recbole.data.interaction import Interaction
from recbole.data.utils import dlapi
from recbole.utils import FeatureSource, FeatureType, get_local_time
from recbole.utils.utils import set_color


class Dataset(object):
Expand Down Expand Up @@ -101,7 +102,7 @@ def _from_scratch(self):
"""Load dataset from scratch.
Initialize attributes firstly, then load data from atomic files, pre-process the dataset lastly.
"""
self.logger.debug(f'Loading {self.__class__} from scratch.')
self.logger.debug(set_color(f'Loading {self.__class__} from scratch.', 'green'))

self._get_preset()
self._get_field_from_config()
Expand Down Expand Up @@ -134,8 +135,8 @@ def _get_field_from_config(self):
'USER_ID_FIELD and ITEM_ID_FIELD need to be set at the same time or not set at the same time.'
)

self.logger.debug(f'uid_field: {self.uid_field}')
self.logger.debug(f'iid_field: {self.iid_field}')
self.logger.debug(set_color('uid_field', 'blue') + f': {self.uid_field}')
self.logger.debug(set_color('iid_field', 'blue') + f': {self.iid_field}')

def _data_processing(self):
"""Data preprocessing, including:
Expand Down Expand Up @@ -334,9 +335,9 @@ def _get_load_and_unload_col(self, source):
if load_col and unload_col:
raise ValueError(f'load_col [{load_col}] and unload_col [{unload_col}] can not be set the same time.')

self.logger.debug(f'[{source}]: ')
self.logger.debug(f'\t load_col: [{load_col}]')
self.logger.debug(f'\t unload_col: [{unload_col}]')
self.logger.debug(set_color(f'[{source}]: ', 'pink'))
self.logger.debug(set_color('\t load_col', 'blue') + f': [{load_col}]')
self.logger.debug(set_color('\t unload_col', 'blue') + f': [{unload_col}]')
return load_col, unload_col

def _load_feat(self, filepath, source):
Expand All @@ -356,7 +357,7 @@ def _load_feat(self, filepath, source):
Their length is limited only after calling :meth:`~_dict_to_interaction` or
:meth:`~_dataframe_to_interaction`
"""
self.logger.debug(f'Loading feature from [{filepath}] (source: [{source}]).')
self.logger.debug(set_color(f'Loading feature from [{filepath}] (source: [{source}]).', 'green'))

load_col, unload_col = self._get_load_and_unload_col(source)
if load_col == set():
Expand Down Expand Up @@ -414,11 +415,11 @@ def _user_item_feat_preparation(self):
if self.user_feat is not None:
new_user_df = pd.DataFrame({self.uid_field: np.arange(self.user_num)})
self.user_feat = pd.merge(new_user_df, self.user_feat, on=self.uid_field, how='left')
self.logger.debug('ordering user features by user id.')
self.logger.debug(set_color('ordering user features by user id.', 'green'))
if self.item_feat is not None:
new_item_df = pd.DataFrame({self.iid_field: np.arange(self.item_num)})
self.item_feat = pd.merge(new_item_df, self.item_feat, on=self.iid_field, how='left')
self.logger.debug('ordering item features by user id.')
self.logger.debug(set_color('ordering item features by user id.', 'green'))

def _preload_weight_matrix(self):
"""Transfer preload weight features into :class:`numpy.ndarray` with shape ``[id_token_length]``
Expand Down Expand Up @@ -488,7 +489,7 @@ def _fill_nan(self):
For fields with type :obj:`~recbole.utils.enum_type.FeatureType.FLOAT`, missing value will be filled by
the average of original data.
"""
self.logger.debug('Filling nan')
self.logger.debug(set_color('Filling nan', 'green'))

for feat_name in self.feat_name_list:
feat = getattr(self, feat_name)
Expand Down Expand Up @@ -528,7 +529,7 @@ def _normalize(self):
else:
return

self.logger.debug(f'Normalized fields: {fields}')
self.logger.debug(set_color('Normalized fields', 'blue') + f': {fields}')

for feat_name in self.feat_name_list:
feat = getattr(self, feat_name)
Expand Down Expand Up @@ -683,7 +684,7 @@ def _get_illegal_ids_by_inter_num(self, field, feat, inter_num, max_num=None, mi
Returns:
set: illegal ids, whose inter num out of [min_num, max_num]
"""
self.logger.debug(f'get_illegal_ids_by_inter_num: field=[{field}], max_num=[{max_num}], min_num=[{min_num}]')
self.logger.debug(set_color('get_illegal_ids_by_inter_num', 'blue') + f': field=[{field}], max_num=[{max_num}], min_num=[{min_num}]')

max_num = max_num or np.inf
min_num = min_num or -1
Expand Down Expand Up @@ -728,7 +729,7 @@ def _drop_by_value(self, val, cmp):
if val is None:
return []

self.logger.debug(f'drop_by_value: val={val}')
self.logger.debug(set_color('drop_by_value', 'blue') + f': val={val}')
filter_field = []
for field in val:
if field not in self.field2type:
Expand Down Expand Up @@ -876,7 +877,7 @@ def _remap_ID_all(self):
"""Get ``config['fields_in_same_space']`` firstly, and remap each.
"""
fields_in_same_space = self._get_fields_in_same_space()
self.logger.debug(f'fields_in_same_space: {fields_in_same_space}')
self.logger.debug(set_color('fields_in_same_space', 'blue') + f': {fields_in_same_space}')
for field_set in fields_in_same_space:
remap_list = self._get_remap_list(field_set)
self._remap(remap_list)
Expand Down Expand Up @@ -1174,19 +1175,19 @@ def __repr__(self):
return self.__str__()

def __str__(self):
info = [self.dataset_name]
info = [set_color(self.dataset_name, 'pink')]
if self.uid_field:
info.extend([
f'The number of users: {self.user_num}', f'Average actions of users: {self.avg_actions_of_users}'
set_color('The number of users', 'blue') + f': {self.user_num}', set_color('Average actions of users', 'blue') + f': {self.avg_actions_of_users}'
])
if self.iid_field:
info.extend([
f'The number of items: {self.item_num}', f'Average actions of items: {self.avg_actions_of_items}'
set_color('The number of items', 'blue') + f': {self.item_num}', set_color('Average actions of items', 'blue') + f': {self.avg_actions_of_items}'
])
info.append(f'The number of inters: {self.inter_num}')
info.append(set_color('The number of inters', 'blue') + f': {self.inter_num}')
if self.uid_field and self.iid_field:
info.append(f'The sparsity of the dataset: {self.sparsity * 100}%')
info.append(f'Remain Fields: {list(self.field2type)}')
info.append(set_color('The sparsity of the dataset', 'blue') + f': {self.sparsity * 100}%')
info.append(set_color('Remain Fields', 'blue') + f': {list(self.field2type)}')
return '\n'.join(info)

def copy(self, new_inter_feat):
Expand Down
11 changes: 6 additions & 5 deletions recbole/data/dataset/kg_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from recbole.data.dataset import Dataset
from recbole.data.utils import dlapi
from recbole.utils import FeatureSource, FeatureType
from recbole.utils.utils import set_color


class KnowledgeBasedDataset(Dataset):
Expand Down Expand Up @@ -80,8 +81,8 @@ def _get_field_from_config(self):
self._check_field('head_entity_field', 'tail_entity_field', 'relation_field', 'entity_field')
self.set_field_property(self.entity_field, FeatureType.TOKEN, FeatureSource.KG, 1)

self.logger.debug(f'relation_field: {self.relation_field}')
self.logger.debug(f'entity_field: {self.entity_field}')
self.logger.debug(set_color('relation_field', 'blue') + f': {self.relation_field}')
self.logger.debug(set_color('entity_field', 'blue') + f': {self.entity_field}')

def _data_processing(self):
self._set_field2ent_level()
Expand Down Expand Up @@ -138,7 +139,7 @@ def save(self, filepath):
raise NotImplementedError()

def _load_kg(self, token, dataset_path):
self.logger.debug(f'Loading kg from [{dataset_path}].')
self.logger.debug(set_color(f'Loading kg from [{dataset_path}].', 'green'))
kg_path = os.path.join(dataset_path, f'{token}.kg')
if not os.path.isfile(kg_path):
raise ValueError(f'[{token}.kg] not found in [{dataset_path}].')
Expand All @@ -153,7 +154,7 @@ def _check_kg(self, kg):
assert self.relation_field in kg, kg_warn_message.format(self.relation_field)

def _load_link(self, token, dataset_path):
self.logger.debug(f'Loading link from [{dataset_path}].')
self.logger.debug(set_color(f'Loading link from [{dataset_path}].', 'green'))
link_path = os.path.join(dataset_path, f'{token}.link')
if not os.path.isfile(link_path):
raise ValueError(f'[{token}.link] not found in [{dataset_path}].')
Expand Down Expand Up @@ -207,7 +208,7 @@ def _get_ent_fields_in_same_space(self):
if self._contain_ent_field(field_set):
field_set = self._remove_ent_field(field_set)
ent_fields.update(field_set)
self.logger.debug(f'ent_fields: {fields_in_same_space}')
self.logger.debug(set_color('ent_fields', 'blue') + f': {fields_in_same_space}')
return ent_fields

def _remove_ent_field(self, field_set):
Expand Down
4 changes: 2 additions & 2 deletions recbole/data/dataset/social_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def _get_field_from_config(self):
self.target_field = self.config['TARGET_ID_FIELD']
self._check_field('source_field', 'target_field')

self.logger.debug(f'source_id_field: {self.source_field}')
self.logger.debug(f'target_id_field: {self.target_field}')
self.logger.debug(set_color('source_id_field', 'blue') + f': {self.source_field}')
self.logger.debug(set_color('target_id_field', 'blue') + f': {self.target_field}')

def _load_data(self, token, dataset_path):
"""Load ``.net`` additionally.
Expand Down
Loading