Skip to content

Commit

Permalink
Use tqdm.auto for compatibility on Jupyter notebook env (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
tqtg authored May 6, 2020
1 parent 6528157 commit 7d753f5
Show file tree
Hide file tree
Showing 26 changed files with 3,008 additions and 3,174 deletions.
28 changes: 19 additions & 9 deletions cornac/eval_methods/base_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import time

import numpy as np
import tqdm
from scipy.sparse import csr_matrix
from tqdm.auto import tqdm

from ..data import TextModality
from ..data import ImageModality
Expand All @@ -31,7 +31,7 @@
from ..utils import get_rng


def rating_eval(model, metrics, test_set, user_based=False):
def rating_eval(model, metrics, test_set, user_based=False, verbose=False):
"""Evaluate model on provided rating metrics.
Parameters
Expand All @@ -48,6 +48,9 @@ def rating_eval(model, metrics, test_set, user_based=False):
user_based: bool, optional, default: False
Evaluation mode. Whether results are averaging based on number of users or number of ratings.
verbose: bool, optional, default: False
Output evaluation progress.
Returns
-------
res: (List, List)
Expand All @@ -65,12 +68,17 @@ def rating_eval(model, metrics, test_set, user_based=False):

(u_indices, i_indices, r_values) = test_set.uir_tuple
r_preds = np.fromiter(
(
model.rate(user_idx, item_idx).item()
for user_idx, item_idx in zip(u_indices, i_indices)
tqdm(
(
model.rate(user_idx, item_idx).item()
for user_idx, item_idx in zip(u_indices, i_indices)
),
desc="Rating",
disable=not verbose,
miniters=100,
total=len(u_indices),
),
dtype=np.float,
count=len(u_indices),
)

gt_mat = test_set.csr_matrix
Expand Down Expand Up @@ -161,7 +169,9 @@ def pos_items(csr_row):
if rating >= rating_threshold
]

for user_idx in tqdm.tqdm(test_set.user_indices, disable=not verbose, miniters=100):
for user_idx in tqdm(
test_set.user_indices, desc="Ranking", disable=not verbose, miniters=100
):
test_pos_items = pos_items(gt_mat.getrow(user_idx))
if len(test_pos_items) == 0:
continue
Expand Down Expand Up @@ -522,6 +532,7 @@ def _eval(self, model, test_set, val_set, user_based):
metrics=self.rating_metrics,
test_set=test_set,
user_based=user_based,
verbose=self.verbose,
)
for i, mt in enumerate(self.rating_metrics):
metric_avg_results[mt.name] = avg_results[i]
Expand Down Expand Up @@ -570,7 +581,7 @@ def evaluate(self, model, metrics, user_based, show_validation=True):
raise ValueError("train_set is required but None!")
if self.test_set is None:
raise ValueError("test_set is required but None!")

self._reset()
self._organize_metrics(metrics)

Expand Down Expand Up @@ -674,4 +685,3 @@ def from_splits(
return method.build(
train_data=train_data, test_data=test_data, val_data=val_data
)

14 changes: 7 additions & 7 deletions cornac/models/baseline_only/recom_bo.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cornac/models/baseline_only/recom_bo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ from libc.math cimport abs

import numpy as np
cimport numpy as np
from tqdm import trange
from tqdm.auto import trange

from ..recommender import Recommender
from ...utils.init_utils import zeros
Expand Down
14 changes: 7 additions & 7 deletions cornac/models/bpr/recom_wbpr.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cornac/models/bpr/recom_wbpr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ from cython cimport floating, integral

import numpy as np
cimport numpy as np
from tqdm import trange
from tqdm.auto import trange

from ..recommender import Recommender
from ...utils import get_rng
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/cdl/recom_cdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# ============================================================================

import numpy as np
from tqdm import trange
from tqdm.auto import trange

from ..recommender import Recommender
from ...exception import ScoreException
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/cdr/recom_cdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# ============================================================================

import numpy as np
from tqdm import trange
from tqdm.auto import trange

from ..recommender import Recommender
from ...exception import ScoreException
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/conv_mf/recom_convmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import math

import numpy as np
from tqdm import trange
from tqdm.auto import trange

from ..recommender import Recommender
from ...exception import ScoreException
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/ctr/recom_ctr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# ============================================================================

import numpy as np
from tqdm import trange
from tqdm.auto import trange

from ..recommender import Recommender
from ...exception import ScoreException
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/cvae/recom_cvae.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# ============================================================================

import numpy as np
from tqdm.auto import trange

from ..recommender import Recommender
from ...exception import ScoreException
Expand Down Expand Up @@ -177,7 +178,6 @@ def _fit_cvae(self):
# VAE initialization
from .cvae import Model
import tensorflow as tf
from tqdm import trange

model = Model(
n_users=self.train_set.num_users,
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/hft/recom_hft.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# ============================================================================

import numpy as np
from tqdm.auto import trange

from ..recommender import Recommender
from ...exception import ScoreException
Expand Down Expand Up @@ -167,7 +168,6 @@ def _build_data(csr_mat):

def _fit_hft(self):
from .hft import Model
from tqdm import trange

# document data
bow_mat = self.train_set.item_text.batch_bow(
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/ibpr/ibpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import numpy as np
import torch
from tqdm import tqdm
from tqdm.auto import tqdm


def ibpr(
Expand Down
1 change: 0 additions & 1 deletion cornac/models/knn/recom_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import numpy as np
from scipy.sparse import csr_matrix, coo_matrix
from tqdm.auto import trange

from ..recommender import Recommender
from ...exception import ScoreException
Expand Down
Loading

0 comments on commit 7d753f5

Please sign in to comment.