iit-cs579 / main

CS579: Online Social Network Analysis at the Illinois Institute of Technology
147 stars 204 forks source link

Doctest Failure on Middle Line #499

Closed toopricey closed 4 years ago

toopricey commented 4 years ago

I'm having some weird behavior in a doctest I wrote, and I'm not finding an obvious solution online. I have the following doctest for the featurize method for the bonus assignment:

>>> movies = pd.DataFrame([[123, 'Horror|Romance'], [456, 'Sci-Fi']], columns=['movieId', 'genres'])
>>> movies = tokenize(movies)
>>> movies, vocab = featurize(movies)
>>> np.round(movies['features'][0].toarray(), 2).tolist()
[[3.32, 3.32, 0.0]]

Which is, for some reason, failing with the error

Failed example:
    movies, vocab = featurize(movies)
Expected nothing
Got:
    Index(['movieId', 'genres', 'tokens', 'features'], dtype='object')

It is passing the actual test case; if I modify the test line to say, [3.32, 3.32, 0.0] instead, I get:

Failed example:
    np.round(movies['features'][0].toarray(), 2).tolist()
Expected:
    [3.32, 3.32, 0.0]
Got:
    [[3.32, 3.32, 0.0]]

This isn't actually that important and I'm moving on, I'm just confused why this is happening. The docs for doctest clearly indicate only the last >>> line will be the test case, so I'm not sure why it's triggering on the wrong line. Switching it to being a single return value rather than a tuple does not work, nor does trying to extend the code lines with some parameter setting; it still fails on that particular line.

aronwc commented 4 years ago

Strange. Hard to tell without seeing the code, but it could be a weird spacing issue (e.g., tab vs spaces, or an extra space or newline somewhere in the doctest line).

On Tue, Nov 26, 2019 at 11:55 PM toopricey notifications@github.com wrote:

I'm having some weird behavior in a doctest I wrote, and I'm not finding an obvious solution online. I have the following doctest for the featurize method for the bonus assignment:

movies = pd.DataFrame([[123, 'Horror|Romance'], [456, 'Sci-Fi']], columns=['movieId', 'genres']) movies = tokenize(movies) movies, vocab = featurize(movies) np.round(movies['features'][0].toarray(), 2).tolist() [[3.32, 3.32, 0.0]]

Which is, for some reason, failing with the error

Failed example: movies, vocab = featurize(movies) Expected nothing Got: Index(['movieId', 'genres', 'tokens', 'features'], dtype='object')

It is passing the actual test case; if I modify the test line to say, [3.32, 3.32, 0.0] instead, I get:

Failed example: np.round(movies['features'][0].toarray(), 2).tolist() Expected: [3.32, 3.32, 0.0] Got: [[3.32, 3.32, 0.0]]

This isn't actually that important and I'm moving on, I'm just confused why this is happening. The docs for doctest clearly indicate only the last

line will be the test case, so I'm not sure why it's triggering on the wrong line. Switching it to being a single return value rather than a tuple does not work, nor does trying to extend the code lines with some parameter setting; it still fails on that particular line.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/iit-cs579/main/issues/499?email_source=notifications&email_token=AA575BPNC4A572LFFCXDWXLQVYDVZA5CNFSM4JSBZUIKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4H4J555A, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA575BOCFG3X7QGVGTDPUDLQVYDVZANCNFSM4JSBZUIA .

ppttzhu commented 4 years ago

I can't replicate the problem. This test case works fine in my local machine with the command: nosetests --with-doctest bonus.py:featurize You can add [0][0] at the end to avoid space or parentheses format issue. >>> np.round(movies['features'][0].toarray(), 2).tolist()[0][0] 3.32

toopricey commented 4 years ago

I figured it out. I had a print(something) line in the method that it was failing on, and that causes issues for doctest. That makes sense based on the documentation in hindsight, though I had assumed prints would only be checked for an expected value on lines prior to expected values.