Skip to content

Commit 64e355d

Browse files
committed
Merge pull request #94 from maciejkula/add_flake8
Add PEP8 checks to circle.
2 parents e166dd6 + 317d2c1 commit 64e355d

13 files changed

+46
-49
lines changed

.flake8rc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
[flake8]
2-
max-line-length = 100
2+
ignore = I100
3+
max-line-length = 100
4+
exclude = doc/*

circle.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ dependencies:
66
test:
77
pre:
88
- pip install pytest
9+
- pip install flake8
910
override:
11+
- flake8 --config .flake8rc
1012
- mv ~/lightfm/lightfm ~/lightfm/_lightfm && py.test -xv ~/lightfm/tests

lightfm/_lightfm_fast.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
try:
22
# Import OpenMP-enabled extension
3-
from ._lightfm_fast_openmp import *
4-
from ._lightfm_fast_openmp import __test_in_positives
3+
from ._lightfm_fast_openmp import * # NOQA
4+
from ._lightfm_fast_openmp import __test_in_positives # NOQA
55
except ImportError:
66
# Fall back on OpenMP-less extension
77
import warnings
88

99
warnings.warn('LightFM was compiled without OpenMP support. '
1010
'Only a single thread will be used.')
1111

12-
from ._lightfm_fast_no_openmp import *
13-
from ._lightfm_fast_no_openmp import __test_in_positives
12+
from ._lightfm_fast_no_openmp import * # NOQA
13+
from ._lightfm_fast_no_openmp import __test_in_positives # NOQA

lightfm/datasets/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from lightfm.datasets.movielens import fetch_movielens
2-
from lightfm.datasets.stackexchange import fetch_stackexchange
1+
from lightfm.datasets.movielens import fetch_movielens # NOQA
2+
from lightfm.datasets.stackexchange import fetch_stackexchange # NOQA

lightfm/datasets/movielens.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import array
21
import itertools
3-
import os
42
import zipfile
53

64
import numpy as np
75

8-
import requests
9-
106
import scipy.sparse as sp
117

128
from lightfm.datasets import _common
@@ -113,14 +109,16 @@ def fetch_movielens(data_home=None, indicator_features=True, genre_features=Fals
113109
"""
114110
Fetch the `Movielens 100k dataset <http://grouplens.org/datasets/movielens/100k/>`_.
115111
116-
The dataset contains 100,000 interactions from 1000 users on 1700 movies, and is exhaustively described
117-
in its `README <http://files.grouplens.org/datasets/movielens/ml-100k-README.txt>`_.
112+
The dataset contains 100,000 interactions from 1000 users on 1700 movies,
113+
and is exhaustively described in its
114+
`README <http://files.grouplens.org/datasets/movielens/ml-100k-README.txt>`_.
118115
119116
Parameters
120117
----------
121118
122119
data_home: path, optional
123-
Path to the directory in which the downloaded data should be placed. Defaults to ``~/lightfm_data/``.
120+
Path to the directory in which the downloaded data should be placed.
121+
Defaults to ``~/lightfm_data/``.
124122
indicator_features: bool, optional
125123
Use an [n_users, n_users] identity matrix for item features. When True with genre_features,
126124
indicator and genre features are concatenated into a single feature matrix of shape

lightfm/datasets/stackexchange.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ def fetch_stackexchange(dataset, test_set_fraction=0.2,
9797
shape=data['features_shape'].flatten())
9898
tag_labels = data['labels']
9999

100-
test_cutoff_timestamp = np.sort(interactions.data)[len(interactions.data)
101-
* (1.0 - test_set_fraction)]
100+
test_cutoff_timestamp = np.sort(interactions.data)[len(interactions.data) *
101+
(1.0 - test_set_fraction)]
102102
in_train = interactions.data < test_cutoff_timestamp
103103
in_test = np.logical_not(in_train)
104104

lightfm/evaluation.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def precision_at_k(model, test_interactions, train_interactions=None,
5151
-------
5252
5353
np.array of shape [n_users with interactions or n_users,]
54-
Numpy array containing precision@k scores for each user. If there are no interactions for a given
55-
user the returned precision will be 0.
54+
Numpy array containing precision@k scores for each user. If there are no interactions
55+
for a given user the returned precision will be 0.
5656
"""
5757

5858
ranks = model.predict_rank(test_interactions,
@@ -146,8 +146,8 @@ def reciprocal_rank(model, test_interactions, train_interactions=None,
146146
user_features=None, item_features=None,
147147
preserve_rows=False, num_threads=1):
148148
"""
149-
Measure the reciprocal rank metric for a model: 1 / the rank of the highest ranked positive example.
150-
A perfect score is 1.0.
149+
Measure the reciprocal rank metric for a model: 1 / the rank of the highest
150+
ranked positive example. A perfect score is 1.0.
151151
152152
Parameters
153153
----------
@@ -176,8 +176,8 @@ def reciprocal_rank(model, test_interactions, train_interactions=None,
176176
-------
177177
178178
np.array of shape [n_users with interactions or n_users,]
179-
Numpy array containing reciprocal rank scores for each user. If there are no interactions for a given
180-
user the returned value will be 0.0.
179+
Numpy array containing reciprocal rank scores for each user.
180+
If there are no interactions for a given user the returned value will be 0.0.
181181
"""
182182

183183
ranks = model.predict_rank(test_interactions,

lightfm/lightfm.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
import scipy.sparse as sp
66

77
from ._lightfm_fast import (CSRMatrix, FastLightFM,
8-
fit_logistic, predict_lightfm,
9-
predict_ranks,
10-
fit_warp, fit_bpr, fit_warp_kos)
8+
fit_bpr, fit_logistic, fit_warp,
9+
fit_warp_kos, predict_lightfm, predict_ranks)
1110

1211

1312
__all__ = ['LightFM']
@@ -181,17 +180,17 @@ def _initialize(self, no_components, no_item_features, no_user_features):
181180
"""
182181

183182
# Initialise item features.
184-
self.item_embeddings = ((self.rng.rand(no_item_features, no_components) - 0.5)
185-
/ no_components).astype(np.float32)
183+
self.item_embeddings = ((self.rng.rand(no_item_features, no_components) - 0.5) /
184+
no_components).astype(np.float32)
186185
self.item_embedding_gradients = np.zeros_like(self.item_embeddings)
187186
self.item_embedding_momentum = np.zeros_like(self.item_embeddings)
188187
self.item_biases = np.zeros(no_item_features, dtype=np.float32)
189188
self.item_bias_gradients = np.zeros_like(self.item_biases)
190189
self.item_bias_momentum = np.zeros_like(self.item_biases)
191190

192191
# Initialise user features.
193-
self.user_embeddings = ((self.rng.rand(no_user_features, no_components) - 0.5)
194-
/ no_components).astype(np.float32)
192+
self.user_embeddings = ((self.rng.rand(no_user_features, no_components) - 0.5) /
193+
no_components).astype(np.float32)
195194
self.user_embedding_gradients = np.zeros_like(self.user_embeddings)
196195
self.user_embedding_momentum = np.zeros_like(self.user_embeddings)
197196
self.user_biases = np.zeros(no_user_features, dtype=np.float32)
@@ -271,8 +270,7 @@ def _process_sample_weight(self, interactions, sample_weight):
271270
'matrices must be the same shape')
272271

273272
if not (np.array_equal(interactions.row,
274-
sample_weight.row)
275-
and
273+
sample_weight.row) and
276274
np.array_equal(interactions.col,
277275
sample_weight.col)):
278276
raise ValueError('Sample weight and interaction matrix '
@@ -608,17 +606,18 @@ def predict_rank(self, test_interactions, train_interactions=None,
608606
item_features=None, user_features=None, num_threads=1):
609607
"""
610608
Predict the rank of selected interactions. Computes recommendation rankings across all items
611-
for every user in interactions and calculates the rank of all non-zero entries in the recommendation
612-
ranking, with 0 meaning the top of the list (most recommended) and n_items - 1 being the end of the
613-
list (least recommended).
609+
for every user in interactions and calculates the rank of all non-zero entries
610+
in the recommendation ranking, with 0 meaning the top of the list (most recommended)
611+
and n_items - 1 being the end of the list (least recommended).
614612
615613
Arguments
616614
---------
617615
test_interactions: np.float32 csr_matrix of shape [n_users, n_items]
618616
Non-zero entries denote the user-item pairs whose rank will be computed.
619617
train_interactions: np.float32 csr_matrix of shape [n_users, n_items], optional
620-
Non-zero entries denote the user-item pairs which will be excluded from rank computation.
621-
Use to exclude training set interactions from being scored and ranked for evaluation.
618+
Non-zero entries denote the user-item pairs which will be excluded from
619+
rank computation. Use to exclude training set interactions from being scored
620+
and ranked for evaluation.
622621
item_ids: np.int32 array of shape [n_pairs,]
623622
an array containing the item ids for the user-item pairs for which
624623
a prediction is to be computed.
@@ -632,7 +631,7 @@ def predict_rank(self, test_interactions, train_interactions=None,
632631
633632
Returns
634633
-------
635-
634+
636635
np.float32 csr_matrix of shape [n_users, n_items]
637636
the [i, j]-th entry of the matrix will contain the rank of the j-th item
638637
in the sorted recommendations list for the i-th user. The degree

setup.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import platform
32
import subprocess
43
import sys
54
import textwrap
@@ -75,7 +74,6 @@ def generate_pyx(self):
7574

7675
def run(self):
7776

78-
import Cython
7977
from Cython.Build import cythonize
8078

8179
self.generate_pyx()
@@ -129,7 +127,6 @@ def run_tests(self):
129127
sys.exit(errno)
130128

131129

132-
133130
use_openmp = not (sys.platform.startswith('darwin') or sys.platform.startswith('win'))
134131

135132

tests/test_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ def test_not_enough_features_fails():
119119
no_items),
120120
dtype=np.int32)
121121

122-
user_features = sp.csr_matrix((no_users-1,
122+
user_features = sp.csr_matrix((no_users - 1,
123123
no_features),
124124
dtype=np.int32)
125-
item_features = sp.csr_matrix((no_items-1,
125+
item_features = sp.csr_matrix((no_items - 1,
126126
no_features),
127127
dtype=np.int32)
128128
model = LightFM()
@@ -261,8 +261,8 @@ def test_predict_ranks():
261261
# in train in that row
262262
ranks = model.predict_rank(rank_input,
263263
train_interactions=train).todense()
264-
assert np.all(np.squeeze(np.array(ranks.max(axis=1)))
265-
== no_items - 1 - np.squeeze(np.array(train.getnnz(axis=1))))
264+
assert np.all(np.squeeze(np.array(ranks.max(axis=1))) ==
265+
no_items - 1 - np.squeeze(np.array(train.getnnz(axis=1))))
266266

267267
# Make sure invariants hold when there are ties
268268
model.user_embeddings = np.zeros_like(model.user_embeddings)

0 commit comments

Comments
 (0)