Skip to content

Commit 9c5215a

Browse files
authored
Fix gensim build (docs & pyemd issues) (#2318)
* disable-pyemd * revert pyemd to setup.py (it still works on linux) * extend 'catch' on import * correct test skipping * fix flake8 * fix docs building * correct skipping if pyemd not available * fix typo * upd * pin sphinx * revert sphinx pin * disable -W for sphinx (REVERT ME), issue not reproduced locally, only here * more verbosity * MOAR verbosity * try to use different path * build binaries before docs * pin previous version of programoutput (avoid bug from 0.13) * revert Makefile * fix * disable programoutput sphinx plugin * revert pinning * one more attempt * cleanup * cleanup[2] * fix
1 parent ce403d3 commit 9c5215a

9 files changed

Lines changed: 49 additions & 35 deletions

File tree

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
name: Build documentation
3131
command: |
3232
source venv/bin/activate
33-
tox -e docs -vv
33+
tox -e compile,docs -vv
3434
3535
- store_artifacts:
3636
path: docs/src/_build

docs/src/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# If extensions (or modules to document with autodoc) are in another directory,
1818
# add these directories to sys.path here. If the directory is relative to the
1919
# documentation root, use os.path.abspath to make it absolute, like shown here.
20-
sys.path.append(os.path.abspath('.'))
20+
sys.path.insert(0, os.path.abspath('../..'))
2121

2222
# -- General configuration -----------------------------------------------------
2323

gensim/models/deprecated/keyedvectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
try:
8787
from pyemd import emd
8888
PYEMD_EXT = True
89-
except ImportError:
89+
except (ImportError, ValueError):
9090
PYEMD_EXT = False
9191

9292
from numpy import dot, zeros, dtype, float32 as REAL,\

gensim/models/keyedvectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
try:
173173
from pyemd import emd
174174
PYEMD_EXT = True
175-
except ImportError:
175+
except (ImportError, ValueError):
176176
PYEMD_EXT = False
177177

178178
from numpy import dot, float32 as REAL, empty, memmap as np_memmap, \

gensim/test/test_fasttext.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
from gensim.models.keyedvectors import Word2VecKeyedVectors
1919
from gensim.test.utils import datapath, get_tmpfile, temporary_file, common_texts as sentences
2020

21+
22+
try:
23+
from pyemd import emd # noqa:F401
24+
PYEMD_EXT = True
25+
except (ImportError, ValueError):
26+
PYEMD_EXT = False
27+
2128
logger = logging.getLogger(__name__)
2229

2330
IS_WIN32 = (os.name == "nt") and (struct.calcsize('P') * 8 == 32)
@@ -357,6 +364,7 @@ def test_contains(self):
357364
self.assertFalse('nights' in self.test_model.wv.vocab)
358365
self.assertTrue('nights' in self.test_model.wv)
359366

367+
@unittest.skipIf(PYEMD_EXT is False, "pyemd not installed or have some issues")
360368
def test_wm_distance(self):
361369
doc = ['night', 'payment']
362370
oov_doc = ['nights', 'forests', 'payments']

gensim/test/test_fasttext_wrapper.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
from gensim.models import keyedvectors
1919
from gensim.test.utils import datapath, get_tmpfile
2020

21+
22+
try:
23+
from pyemd import emd # noqa:F401
24+
PYEMD_EXT = True
25+
except (ImportError, ValueError):
26+
PYEMD_EXT = False
27+
28+
2129
logger = logging.getLogger(__name__)
2230

2331

@@ -311,6 +319,7 @@ def testContains(self):
311319
self.assertFalse('a!@' in self.test_model.wv.vocab)
312320
self.assertFalse('a!@' in self.test_model)
313321

322+
@unittest.skipIf(PYEMD_EXT is False, "pyemd not installed or have some issues")
314323
def testWmdistance(self):
315324
"""Tests wmdistance for docs with in-vocab and out-of-vocab words"""
316325
doc = ['night', 'payment']

gensim/test/test_similarities.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
try:
3030
from pyemd import emd # noqa:F401
3131
PYEMD_EXT = True
32-
except ImportError:
32+
except (ImportError, ValueError):
3333
PYEMD_EXT = False
3434

3535
sentences = [doc2vec.TaggedDocument(words, [i]) for i, words in enumerate(texts)]
@@ -78,9 +78,8 @@ def testFull(self, num_best=None, shardsize=100):
7878
index.destroy()
7979

8080
def testNumBest(self):
81-
8281
if self.cls == similarities.WmdSimilarity and not PYEMD_EXT:
83-
return
82+
self.skipTest("pyemd not installed or have some issues")
8483

8584
for num_best in [None, 0, 1, 9, 1000]:
8685
self.testFull(num_best=num_best)
@@ -110,6 +109,9 @@ def test_scipy2scipy_clipped(self):
110109

111110
def testEmptyQuery(self):
112111
index = self.factoryMethod()
112+
if isinstance(index, similarities.WmdSimilarity) and not PYEMD_EXT:
113+
self.skipTest("pyemd not installed or have some issues")
114+
113115
query = []
114116
try:
115117
sims = index[query]
@@ -166,7 +168,7 @@ def testIter(self):
166168

167169
def testPersistency(self):
168170
if self.cls == similarities.WmdSimilarity and not PYEMD_EXT:
169-
return
171+
self.skipTest("pyemd not installed or have some issues")
170172

171173
fname = get_tmpfile('gensim_similarities.tst.pkl')
172174
index = self.factoryMethod()
@@ -186,7 +188,7 @@ def testPersistency(self):
186188

187189
def testPersistencyCompressed(self):
188190
if self.cls == similarities.WmdSimilarity and not PYEMD_EXT:
189-
return
191+
self.skipTest("pyemd not installed or have some issues")
190192

191193
fname = get_tmpfile('gensim_similarities.tst.pkl.gz')
192194
index = self.factoryMethod()
@@ -206,7 +208,7 @@ def testPersistencyCompressed(self):
206208

207209
def testLarge(self):
208210
if self.cls == similarities.WmdSimilarity and not PYEMD_EXT:
209-
return
211+
self.skipTest("pyemd not installed or have some issues")
210212

211213
fname = get_tmpfile('gensim_similarities.tst.pkl')
212214
index = self.factoryMethod()
@@ -228,7 +230,7 @@ def testLarge(self):
228230

229231
def testLargeCompressed(self):
230232
if self.cls == similarities.WmdSimilarity and not PYEMD_EXT:
231-
return
233+
self.skipTest("pyemd not installed or have some issues")
232234

233235
fname = get_tmpfile('gensim_similarities.tst.pkl.gz')
234236
index = self.factoryMethod()
@@ -250,7 +252,7 @@ def testLargeCompressed(self):
250252

251253
def testMmap(self):
252254
if self.cls == similarities.WmdSimilarity and not PYEMD_EXT:
253-
return
255+
self.skipTest("pyemd not installed or have some issues")
254256

255257
fname = get_tmpfile('gensim_similarities.tst.pkl')
256258
index = self.factoryMethod()
@@ -273,7 +275,7 @@ def testMmap(self):
273275

274276
def testMmapCompressed(self):
275277
if self.cls == similarities.WmdSimilarity and not PYEMD_EXT:
276-
return
278+
self.skipTest("pyemd not installed or have some issues")
277279

278280
fname = get_tmpfile('gensim_similarities.tst.pkl.gz')
279281
index = self.factoryMethod()
@@ -298,12 +300,10 @@ def factoryMethod(self):
298300
# Override factoryMethod.
299301
return self.cls(texts, self.w2v_model)
300302

303+
@unittest.skipIf(PYEMD_EXT is False, "pyemd not installed or have some issues")
301304
def testFull(self, num_best=None):
302305
# Override testFull.
303306

304-
if not PYEMD_EXT:
305-
return
306-
307307
index = self.cls(texts, self.w2v_model)
308308
index.num_best = num_best
309309
query = texts[0]
@@ -319,15 +319,13 @@ def testFull(self, num_best=None):
319319
self.assertTrue(numpy.alltrue(sims[1:] > 0.0))
320320
self.assertTrue(numpy.alltrue(sims[1:] < 1.0))
321321

322+
@unittest.skipIf(PYEMD_EXT is False, "pyemd not installed or have some issues")
322323
def testNonIncreasing(self):
323324
''' Check that similarities are non-increasing when `num_best` is not
324325
`None`.'''
325326
# NOTE: this could be implemented for other similarities as well (i.e.
326327
# in _TestSimilarityABC).
327328

328-
if not PYEMD_EXT:
329-
return
330-
331329
index = self.cls(texts, self.w2v_model, num_best=3)
332330
query = texts[0]
333331
sims = index[query]
@@ -337,12 +335,10 @@ def testNonIncreasing(self):
337335
cond = sum(numpy.diff(sims2) < 0) == len(sims2) - 1
338336
self.assertTrue(cond)
339337

338+
@unittest.skipIf(PYEMD_EXT is False, "pyemd not installed or have some issues")
340339
def testChunking(self):
341340
# Override testChunking.
342341

343-
if not PYEMD_EXT:
344-
return
345-
346342
index = self.cls(texts, self.w2v_model)
347343
query = texts[:3]
348344
sims = index[query]
@@ -358,12 +354,10 @@ def testChunking(self):
358354
self.assertTrue(numpy.alltrue(sim > 0.0))
359355
self.assertTrue(numpy.alltrue(sim <= 1.0))
360356

357+
@unittest.skipIf(PYEMD_EXT is False, "pyemd not installed or have some issues")
361358
def testIter(self):
362359
# Override testIter.
363360

364-
if not PYEMD_EXT:
365-
return
366-
367361
index = self.cls(texts, self.w2v_model)
368362
for sims in index:
369363
self.assertTrue(numpy.alltrue(sims >= 0.0))

gensim/test/test_word2vec.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
try:
2727
from pyemd import emd # noqa:F401
2828
PYEMD_EXT = True
29-
except ImportError:
29+
except (ImportError, ValueError):
3030
PYEMD_EXT = False
3131

3232

@@ -1023,12 +1023,11 @@ def test_compute_training_loss(self):
10231023
# endclass TestWord2VecModel
10241024

10251025
class TestWMD(unittest.TestCase):
1026+
1027+
@unittest.skipIf(PYEMD_EXT is False, "pyemd not installed or have some issues")
10261028
def testNonzero(self):
10271029
'''Test basic functionality with a test sentence.'''
10281030

1029-
if not PYEMD_EXT:
1030-
return
1031-
10321031
model = word2vec.Word2Vec(sentences, min_count=2, seed=42, workers=1)
10331032
sentence1 = ['human', 'interface', 'computer']
10341033
sentence2 = ['survey', 'user', 'computer', 'system', 'response', 'time']
@@ -1037,25 +1036,21 @@ def testNonzero(self):
10371036
# Check that distance is non-zero.
10381037
self.assertFalse(distance == 0.0)
10391038

1039+
@unittest.skipIf(PYEMD_EXT is False, "pyemd not installed or have some issues")
10401040
def testSymmetry(self):
10411041
'''Check that distance is symmetric.'''
10421042

1043-
if not PYEMD_EXT:
1044-
return
1045-
10461043
model = word2vec.Word2Vec(sentences, min_count=2, seed=42, workers=1)
10471044
sentence1 = ['human', 'interface', 'computer']
10481045
sentence2 = ['survey', 'user', 'computer', 'system', 'response', 'time']
10491046
distance1 = model.wv.wmdistance(sentence1, sentence2)
10501047
distance2 = model.wv.wmdistance(sentence2, sentence1)
10511048
self.assertTrue(np.allclose(distance1, distance2))
10521049

1050+
@unittest.skipIf(PYEMD_EXT is False, "pyemd not installed or have some issues")
10531051
def testIdenticalSentences(self):
10541052
'''Check that the distance from a sentence to itself is zero.'''
10551053

1056-
if not PYEMD_EXT:
1057-
return
1058-
10591054
model = word2vec.Word2Vec(sentences, min_count=1)
10601055
sentence = ['survey', 'user', 'computer', 'system', 'response', 'time']
10611056
distance = model.wv.wmdistance(sentence, sentence)

tox.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ deps = flake8-rst == 0.4.3
6969
commands = flake8-rst gensim/ docs/ {posargs}
7070

7171

72+
[testenv:compile]
73+
basepython = python2
74+
recreate = True
75+
76+
deps = numpy == 1.11.3
77+
commands = python setup.py build_ext --inplace
78+
79+
7280
[testenv:docs]
7381
basepython = python2
7482
recreate = True

0 commit comments

Comments
 (0)