danielfrg / tsne

A python wrapper for Barnes-Hut tsne
Apache License 2.0
404 stars 132 forks source link

Running into relative import issues while trying to import bh_sne #20

Closed jrajagopal closed 4 years ago

jrajagopal commented 7 years ago

Please help me with this issue. I am currently running Python 3.4 on Ubuntu 14.04 with 60 GB RAM. I installed tsne from source with command: sudo -H pip3 install git+https://github.com/danielfrg/tsne.git

1) While that was successful, when testing the import, I got a relative import error:

>>> import tsne
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/tsne/__init__.py", line 6, in <module>
    from bh_sne import BH_SNE
  File "tsne/bh_sne.pyx", line 2, in init tsne.bh_sne (tsne/bh_sne.cpp:4867)
SystemError: Parent module '' not loaded, cannot perform relative import

>>> from tsne import bh_sne
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/tsne/__init__.py", line 6, in <module>
    from bh_sne import BH_SNE
  File "tsne/bh_sne.pyx", line 2, in init tsne.bh_sne (tsne/bh_sne.cpp:4867)
SystemError: Parent module '' not loaded, cannot perform relative import
>>> 

2) Also, I noticed something strange however. I tried to import BH_SNE from tsne.bh_sne. If I do it right off the bat in python, it fails, with the same error:

>>> from tsne.bh_sne import BH_SNE
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/tsne/__init__.py", line 6, in <module>
    from bh_sne import BH_SNE
  File "tsne/bh_sne.pyx", line 2, in init tsne.bh_sne (tsne/bh_sne.cpp:4867)
SystemError: Parent module '' not loaded, cannot perform relative import

3) However, if I first try importing tsne or bh_sne, or even repeat the above command, it succeeds:

root@ip-172-31-27-17:/# python3
Python 3.4.3 (default, Sep 14 2016, 12:36:27) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from tsne import bh_sne
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/tsne/__init__.py", line 6, in <module>
    from bh_sne import BH_SNE
  File "tsne/bh_sne.pyx", line 2, in init tsne.bh_sne (tsne/bh_sne.cpp:4867)
SystemError: Parent module '' not loaded, cannot perform relative import
>>> from tsne.bh_sne import BH_SNE
>>> BH_SNE()
<tsne.bh_sne.BH_SNE object at 0x7ffa28a9c498>
>>> 
jrajagopal commented 7 years ago

Also, FYI, regarding your comment about using the t-sne function in sklearn. If you use a large dataset, it fails with a Segmentation Error/Core Dump. This is a known documented issue in sklearn.

areshytko commented 7 years ago

try: ...: from tsne.bh_sne import BH_SNE ...: except SystemError: ...: pass ...: from tsne.bh_sne import BH_SNE

this is how you can import it, but it raises an error in run method:

     73     tsne = BH_SNE()
---> 74     Y = tsne.run(X, N, X.shape[1], d, perplexity, theta, seed)
     75     return Y
     76 

tsne/bh_sne.pyx in tsne.bh_sne.BH_SNE.run (tsne/bh_sne.cpp:1590)()

NameError: name 'np' is not defined
hobofan commented 7 years ago

@areshytko I had the same error, but it was resolved when I restarted my Jupyter notebook kernel.

https://github.com/alexisbcook/tsne is a fork that works out of the box for me.

jrajagopal commented 7 years ago

Thanks guys!