Skip to content

Commit

Permalink
exception safety
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cortes committed May 30, 2022
1 parent f87572f commit c418a29
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
29 changes: 15 additions & 14 deletions hpfrec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from . import cython_loops_float, cython_loops_double, _check_openmp
import ctypes, types, inspect
from scipy.sparse import coo_matrix, csr_matrix
### TODO: don't do this, use iloc/loc and make copies instead
pd.options.mode.chained_assignment = None

class HPF:
Expand Down Expand Up @@ -692,7 +693,7 @@ def _process_data_single(self, counts_df):
if self.produce_dicts:
try:
counts_df['ItemId'] = counts_df.ItemId.map(lambda x: self.item_dict_[x])
except:
except Exception:
raise ValueError("Can only make calculations for items that were in the training set.")
else:
counts_df['ItemId'] = pd.Categorical(counts_df.ItemId.values, self.item_mapping_).codes
Expand Down Expand Up @@ -787,7 +788,7 @@ def partial_fit(self, counts_df, batch_type='users', step_size=None,
msg = "When using 'partial_fit', the list of items seen by each user is not updated "
msg += "with the data passed here."
warnings.warn(msg)
except:
except Exception:
msg = "When fitting the model through 'partial_fit' without calling 'fit' beforehand, "
msg += "'keep_data' will be forced to False."
warnings.warn(msg)
Expand All @@ -802,37 +803,37 @@ def partial_fit(self, counts_df, batch_type='users', step_size=None,
if nusers is None:
try:
nusers = self.nusers
except:
except Exception:
raise ValueError("Must specify total number of users when calling 'partial_fit' for the first time.")
if nitems is None:
try:
nitems = self.nitems
except:
except Exception:
raise ValueError("Must specify total number of items when calling 'partial_fit' for the first time.")

try:
if self.nusers is None:
self.nusers = nusers
except:
except Exception:
self.nusers = nusers
try:
if self.nitems is None:
self.nitems = nitems
except:
except Exception:
self.nitems = nitems

if step_size is None:
try:
self.step_size(0)
try:
step_size = self.step_size(self.niter)
except:
except Exception:
self.niter = 0
step_size = 1.0
except:
except Exception:
try:
step_size = 1 / np.sqrt(self.niter + 2)
except:
except Exception:
self.niter = 0
step_size = 1.0
assert step_size >= 0
Expand Down Expand Up @@ -1224,7 +1225,7 @@ def predict(self, user, item):
if self.user_dict_ is not None:
try:
user = self.user_dict_[user]
except:
except Exception:
user = -1
else:
user = pd.Categorical(user, self.user_mapping_).codes[0]
Expand All @@ -1233,7 +1234,7 @@ def predict(self, user, item):
if self.user_dict_ is not None:
try:
user = self.user_dict_[user]
except:
except Exception:
user = -1
else:
user = pd.Categorical(np.array([user]), self.user_mapping_).codes[0]
Expand All @@ -1250,7 +1251,7 @@ def predict(self, user, item):
if self.item_dict_ is not None:
try:
item = self.item_dict_[item]
except:
except Exception:
item = -1
else:
item = pd.Categorical(item, self.item_mapping_).codes[0]
Expand All @@ -1259,7 +1260,7 @@ def predict(self, user, item):
if self.item_dict_ is not None:
try:
item = self.item_dict_[item]
except:
except Exception:
item = -1
else:
item = pd.Categorical(np.array([item]), self.item_mapping_).codes[0]
Expand Down Expand Up @@ -1321,7 +1322,7 @@ def topN(self, user, n=10, exclude_seen=True, items_pool=None):
if self.produce_dicts:
try:
user = self.user_dict_[user]
except:
except Exception:
raise ValueError("Can only predict for users who were in the training set.")
else:
user = pd.Categorical(np.array([user]), self.user_mapping_).codes[0]
Expand Down
12 changes: 6 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import setuptools
from setuptools import setup
from setuptools import Extension
except:
except ImportError:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_supports_compile_arg(self, comm, with_omp=False):
cmd = list(self.compiler.compiler)
else:
cmd = self.compiler.compiler
except:
except Exception:
cmd = self.compiler.compiler
val_good = subprocess.call(cmd + [fname])
if with_omp:
Expand All @@ -127,13 +127,13 @@ def test_supports_compile_arg(self, comm, with_omp=False):
try:
val = subprocess.call(cmd + comm + [fname])
is_supported = (val == val_good)
except:
except Exception:
is_supported = False
except:
except Exception:
pass
try:
os.remove(fname)
except:
except Exception:
pass
return is_supported

Expand All @@ -147,7 +147,7 @@ def test_supports_compile_arg(self, comm, with_omp=False):
'scipy',
'cython'
],
version = '0.2.5-5',
version = '0.2.5-6',
description = 'Hierarchical Poisson matrix factorization for recommender systems',
author = 'David Cortes',
author_email = 'david.cortes.rivera@gmail.com',
Expand Down

0 comments on commit c418a29

Please sign in to comment.