Skip to content

Commit

Permalink
Merge pull request #3894 from biolab/revert-3883-lazy-load-tsne
Browse files Browse the repository at this point in the history
Revert "[ENH] t-SNE: Load openTSNE lazily"
  • Loading branch information
lanzagar authored Jun 19, 2019
2 parents 019a41d + 18ba60f commit 3c3484a
Showing 1 changed file with 12 additions and 23 deletions.
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

0 comments on commit 3c3484a

Please sign in to comment.