Skip to main content

Neighbor embedding spectrum

Project description

PyPI - Version License: MIT

Neighbor embedding spectrum

This repository implements the computation of neighbor embedding spectra as described in Visualizing single-cell data with the neighbor embedding spectrum (bioarxiv).

It can use openTSNE or cne as backends.

Installation

pip install ne-spectrum

If you want to use the GPU support for the cne backend, please make sure that you have pytorch installed with CUDA support before installing ne-spectrum. Similarly, if you want to save animations as .mp4 rather than .gif files, you need to install ffmpeg.

Usage

Load MNIST as example data

from ne_spectrum import TSNESpectrum, CNESpectrum
import torchvision
from sklearn.decomposition import PCA
import os
import numpy as np

fig_path = "./"

# load MNIST as example dataset
mnist_train = torchvision.datasets.MNIST(train=True,
                                         download=True,
                                         transform=None,
                                         root=fig_path
                                         )
x_train, y_train = mnist_train.data.float().numpy(), mnist_train.targets

mnist_test = torchvision.datasets.MNIST(train=False,
                                        download=True,
                                        transform=None,
                                        root=fig_path
                                        )
x_test, y_test = mnist_test.data.float().numpy(), mnist_test.targets

x_train = x_train.reshape(x_train.shape[0], -1)
x_test = x_test.reshape(x_test.shape[0], -1)

x = np.concatenate([x_train, x_test], axis=0)
y = np.concatenate([y_train, y_test], axis=0)

# transform data with PCA to save some time when computing the kNN graphs
x_pca = PCA(n_components=50).fit_transform(x)

Compute the neighbor embedding spectrum with the openTSNE backend

# compute spectrum with openTSNE backend
tsnespectrum = TSNESpectrum()
tsnespectrum.fit(x_pca)

# save individual slides, all embeddings, and a gif animation
tsnespectrum.save_slides(save_path=os.path.join(fig_path, "mnist_tsne"),
                         cmap="tab10",
                         color=y)
tsnespectrum.save_embeddings(os.path.join(fig_path, "mnist_tsne", "embeddings.npy"))
tsnespectrum.save_video(save_path=os.path.join(fig_path, "mnist_tsne"),
                        cmap="tab10",
                        color=y)

Neighbor embedding spectrum on MNIST animated

Similarly, we can compute the neighbor embedding spectrum with the cne backend

# compute spectrum with CNE backend
cnespectrum = CNESpectrum()
cnespectrum.fit(x_pca)

# save individual slides, all embeddings, and a gif animation
cnespectrum.save_slides(save_path=os.path.join(fig_path, "mnist_cne"),
                        cmap="tab10",
                        color=y)
cnespectrum.save_embeddings(os.path.join(fig_path, "mnist_cne", "embeddings.npy"))
cnespectrum.save_video(save_path=os.path.join(fig_path, "mnist_cne"),
                       cmap="tab10",
                       color=y)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ne_spectrum-0.1.1.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ne_spectrum-0.1.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file ne_spectrum-0.1.1.tar.gz.

File metadata

  • Download URL: ne_spectrum-0.1.1.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.0

File hashes

Hashes for ne_spectrum-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4e4fe4193119ac49e79e3d3638e7d7440b1ea8da81d3a71e29bf14fab24bc576
MD5 f6f2b68669a62f9cf9fcd507f53aced9
BLAKE2b-256 b5e50d65bc83bae120c16ed74937fbeb7b0c7ad58fa9f042d6175d2b35f67bbf

See more details on using hashes here.

File details

Details for the file ne_spectrum-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ne_spectrum-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.0

File hashes

Hashes for ne_spectrum-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef1af57de976aef246e49ce4abee1e18f3d2d19e7a84d23188e85cafe55d2a7b
MD5 c8ec77f80eeace05b67482fe5137e44d
BLAKE2b-256 5d8c65f433ed5547afaa65afcd048c04d53616ec4a4acb5f75c1ad0de7420729

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page