Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "[ENH] t-SNE: Load openTSNE lazily" #3894

Merged
merged 1 commit into from
Jun 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions Orange/projection/manifold.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from scipy.sparse.linalg import eigsh as arpack_eigh
import sklearn.manifold as skl_manifold

import openTSNE
import openTSNE.affinity
import openTSNE.initialization

import Orange
from Orange.data import Table, Domain, ContinuousVariable
from Orange.distance import Distance, DistanceModel, Euclidean
Expand All @@ -17,6 +21,10 @@
__all__ = ["MDS", "Isomap", "LocallyLinearEmbedding", "SpectralEmbedding",
"TSNE"]

# Disable t-SNE user warnings
openTSNE.tsne.log.setLevel(logging.ERROR)
openTSNE.affinity.log.setLevel(logging.ERROR)


def torgerson(distances, n_components=2, eigen_solver="auto"):
"""
Expand Down Expand Up @@ -182,23 +190,6 @@ def __init__(self, n_components=2, affinity='nearest_neighbors', gamma=None,
self.params = vars()


class lazy_openTSNE:
"""openTSNE uses numba, which is slow to load, so load it lazily."""
def __getattr__(self, item):
import sys
import openTSNE
import openTSNE.affinity
import openTSNE.initialization
if "openTSNE" in sys.modules:
# Disable t-SNE user warnings
openTSNE.tsne.log.setLevel(logging.ERROR)
openTSNE.affinity.log.setLevel(logging.ERROR)
return getattr(openTSNE, item)


openTSNE = lazy_openTSNE()


class TSNEModel(Projection):
"""A t-SNE embedding object. Supports further optimization as well as
adding new data into the existing embedding.
Expand All @@ -213,8 +204,8 @@ class TSNEModel(Projection):
pre_domain : Domain
Original data domain
"""
def __init__(self, embedding, table, pre_domain):
# type: (openTSNE.TSNEEmbedding, Table, Domain) -> None
def __init__(self, embedding: openTSNE.TSNEEmbedding, table: Table,
pre_domain: Domain):
transformer = TransformDomain(self)

def proj_variable(i):
Expand All @@ -230,8 +221,7 @@ def proj_variable(i):
class_vars=table.domain.class_vars,
metas=table.domain.metas)

def transform(self, X, learning_rate=1, **kwargs):
# type: (np.ndarray, int, ...) -> openTSNE.PartialTSNEEmbedding
def transform(self, X: np.ndarray, learning_rate=1, **kwargs) -> openTSNE.PartialTSNEEmbedding:
if sp.issparse(X):
raise TypeError(
"A sparse matrix was passed, but dense data is required. Use "
Expand Down Expand Up @@ -491,8 +481,7 @@ def prepare_embedding(self, affinities, initialization):
callbacks_every_iters=self.callbacks_every_iters,
)

def fit(self, X, Y=None):
# type: (np.ndarray, Optional[np.ndarray]) -> openTSNE.TSNEEmbedding
def fit(self, X: np.ndarray, Y: np.ndarray = None) -> openTSNE.TSNEEmbedding:
# Compute affinities and initial positions and prepare the embedding object
affinities = self.compute_affinities(X)
initialization = self.compute_initialization(X)
Expand Down