featuremap.featuremap
Attributes
Classes
Feature-preserving Manifold Approximation and Projection |
Functions
|
Density estimation for data points specified by X with kernel density estimation. |
|
Local singular value decomposition (SVD) for each node in the data |
|
Perform iterative neighbor averaging as a simple graph convolution. |
|
Compute the origial gauge in high-dimensional data by local SVD |
|
Project the gauge (rotation matrix) to the KNN graph and compute the transition probability matrix |
|
Recover the gauge from the low-dimensional embedding |
|
Embedding the gauge (rotation matrix) to low dim space |
|
Compute the variation embedding of the data |
Perform a fuzzy simplicial set embedding, using a specified |
|
|
Standard clamping of a value into a fixed range (in this case -4.0 to |
|
Reduced Euclidean distance. |
Improve an embedding using stochastic gradient descent to minimize the |
Module Contents
- featuremap.featuremap_.kernel_density_estimate(data, X, bw=0.5, min_radius=5, output_onlylogp=False)[source]
Density estimation for data points specified by X with kernel density estimation.
- Parameters:
data (array of shape (n_samples, n_features)) – 2D array including data points. Input to density estimation.
X (array) – 2D array including multiple data points. Input to density estimation.
output_onlylogp (bool) – If true, returns logp, else returns p, g, h, msu.
- Returns:
p – 1D array. Unnormalized probability density. The probability density is not normalized due to numerical stability. Exact log probability
- Return type:
array
- featuremap.featuremap_.local_svd(data, knn_index, neighbor_weights, n_neighbors=15, n_neighbors_in_guage=30)[source]
Local singular value decomposition (SVD) for each node in the data
- Parameters:
data (array of shape (n_samples, n_features)) – The source data to be embedded by FeatureMAP.
knn_index (array of shape (n_samples, n_neighbors)) – The index of k-nearest neighbors for each node
weight (array of shape (n_samples, n_samples)) – The weight matrix of the graph
n_neighbors (int) – The number of nearest neighbors
n_neighbors_in_guage (int) – The number of nearest neighbors for local SVD
- Returns:
gauge_u (list of shape (n_neighbors_in_guage, n_neighbors_in_guage); store u)
singular_values (list of shape (n_neighbors_in_guage,); store single values for each frame)
gauge_vh (list of shape (n_neighbors_in_guage, d); store v)
- featuremap.featuremap_.graph_convolution(features, knn_index, num_iterations, verbose=False, memory_target_mb=256, backend='auto')[source]
Perform iterative neighbor averaging as a simple graph convolution.
- Parameters:
features (np.ndarray) – Input feature matrix of shape (num_nodes, num_features, num_channels).
knn_index (np.ndarray) – Nearest neighbor indices of shape (num_nodes, num_neighbors).
num_iterations (int, optional) – Number of smoothing iterations. Default is 20.
- Returns:
Smoothed feature matrix after num_iterations iterations. dict: Dictionary storing the first averaged result under the key “VH”.
- Return type:
np.ndarray
- featuremap.featuremap_.tangent_space_approximation(data, graph, featuremap_kwds)[source]
Compute the origial gauge in high-dimensional data by local SVD
- Parameters:
data (array of shape (n_samples, n_features)) – The source data to be embedded by FeatureMAP.
graph (sparse matrix) – The 1-skeleton of the high dimensional fuzzy simplicial set as represented by a graph for which we require a sparse matrix for the (weighted) adjacency matrix.
featuremap_kwds (dict) – Key word arguments to be used by the FeatureMAP optimization.
- Return type:
Local tangent space U, S, V
- featuremap.featuremap_.project_gauge_to_knn_graph(data, graph, featuremap_kwds, scale=10, use_negative_cosines=False)[source]
Project the gauge (rotation matrix) to the KNN graph and compute the transition probability matrix Modified from the scVelo: https://scvelo.readthedocs.io/en/stable/scvelo.utils.get_transition_matrix.html#scvelo.utils.get_transition_matrix
- Parameters:
data (array of shape (n_samples, n_features)) – The source data to be embedded by FeatureMAP.
graph (sparse matrix) – The 1-skeleton of the high dimensional fuzzy simplicial set as represented by a graph for which we require a sparse matrix for the (weighted) adjacency matrix.
featuremap_kwds (dict) – Key word arguments to be used by the FeatureMAP optimization.
scale (float) – The scale factor for the transition probability matrix
use_negative_cosines (bool) – Whether to use negative cosine similarity in the transition probability matrix
- Returns:
transition_matrix – The transition probability matrix
- Return type:
array of shape (n_samples, n_samples)
- featuremap.featuremap_.recover_gauge_from_embedding(data_embedding, featuremap_kwds)[source]
Recover the gauge from the low-dimensional embedding
- Parameters:
data_embedding (array of shape (n_samples, n_components)) – The low-dimensional embedding of the data
featuremap_kwds (dict)
- Returns:
gauge_v1_emb (array of shape (n_samples, n_components)) – The recovered gauge v1 in low dimesional space
gauge_v2_emb (array of shape (n_samples, n_components)) – The recovered gauge v2 in low dimensional space
- featuremap.featuremap_.tangent_space_embedding(featuremap_kwds)[source]
Embedding the gauge (rotation matrix) to low dim space
- Parameters:
featuremap_kwds (dict) – Key word arguments to be used by the FeatureMAP optimization.
- featuremap.featuremap_.variation_embedding(data, featuremap_kwds)[source]
Compute the variation embedding of the data
- Parameters:
data (array of shape (n_samples, n_features)) – The source data to be embedded by FeatureMAP.
featuremap_kwds (dict) – Key word arguments to be used by the FeatureMAP optimization.
- Returns:
emb_variation – The optimized of
graphof variation into ann_componentsdimensional euclidean space.- Return type:
array of shape (n_samples, n_components)
- featuremap.featuremap_.simplicial_set_embedding_with_tangent_space_embedding(data, graph, n_components, initial_alpha, a, b, gamma, negative_sample_rate, n_epochs, init, random_state, metric, metric_kwds, featuremap_kwds, output_feat, output_metric=dist.named_distances_with_gradients['euclidean'], output_metric_kwds={}, euclidean_output=True, parallel=False, verbose=False, tqdm_kwds=None, output_variation=False)[source]
Perform a fuzzy simplicial set embedding, using a specified initialisation method and then minimizing the fuzzy set cross entropy between the 1-skeletons of the high and low dimensional fuzzy simplicial sets.
- Parameters:
data (array of shape (n_samples, n_features)) – The source data to be embedded by FeatureMAP.
graph (sparse matrix) – The 1-skeleton of the high dimensional fuzzy simplicial set as represented by a graph for which we require a sparse matrix for the (weighted) adjacency matrix.
n_components (int) – The dimensionality of the euclidean space into which to embed the data.
initial_alpha (float) – Initial learning rate for the SGD.
a (float) – Parameter of differentiable approximation of right adjoint functor
b (float) – Parameter of differentiable approximation of right adjoint functor
gamma (float) – Weight to apply to negative samples.
negative_sample_rate (int (optional, default 5)) – The number of negative samples to select per positive sample in the optimization process. Increasing this value will result in greater repulsive force being applied, greater optimization cost, but slightly more accuracy.
n_epochs (int (optional, default 0)) – The number of training epochs to be used in optimizing the low dimensional embedding. Larger values result in more accurate embeddings. If 0 is specified a value will be selected based on the size of the input dataset (200 for large datasets, 500 for small).
init (string) –
- How to initialize the low dimensional embedding. Options are:
’spectral’: use a spectral embedding of the fuzzy 1-skeleton
’random’: assign initial embedding positions at random.
A numpy array of initial embedding positions.
random_state (numpy RandomState or equivalent) – A state capable being used as a numpy random state.
metric (string or callable) – The metric used to measure distance in high dimensional space; used if multiple connected components need to be layed out.
metric_kwds (dict) – Key word arguments to be passed to the metric function; used if multiple connected components need to be layed out.
featuremap_kwds (dict) – Key word arguments to be used by the FeatureMAP optimization.
- output_metric: function
Function returning the distance between two points in embedding space and the gradient of the distance wrt the first argument.
- output_metric_kwds: dict
Key word arguments to be passed to the output_metric function.
- euclidean_output: bool
Whether to use the faster code specialised for euclidean output metrics
- parallel: bool (optional, default False)
Whether to run the computation using numba parallel. Running in parallel is non-deterministic, and is not used if a random seed has been set, to ensure reproducibility.
- verbose: bool (optional, default False)
Whether to report information on the current progress of the algorithm.
- tqdm_kwds: dict
Key word arguments to be used by the tqdm progress bar.
- Returns:
embedding – The optimized of
graphinto ann_componentsdimensional euclidean space.- Return type:
array of shape (n_samples, n_components)
- class featuremap.featuremap_.FeatureMAP(n_neighbors=15, n_components=2, metric='euclidean', metric_kwds=None, output_metric='euclidean', output_metric_kwds=None, n_epochs=None, learning_rate=1.0, init='spectral', min_dist=0.5, spread=1.0, low_memory=True, n_jobs=-1, set_op_mix_ratio=1.0, local_connectivity=1.0, repulsion_strength=1.0, negative_sample_rate=5, a=None, b=None, random_state=None, angular_rp_forest=False, transform_seed=42, transform_mode='embedding', force_approximation_algorithm=False, verbose=False, tqdm_kwds=None, unique=False, feat_lambda=0.5, feat_frac=0.3, feat_gauge_coefficient=1.0, feat_var_shift=0.1, output_feat=False, disconnection_distance=None, output_variation=False, threshold=0.9, gcn_iterations=None)[source]
Bases:
sklearn.base.BaseEstimatorFeature-preserving Manifold Approximation and Projection
Approximate the high dimensional manifold of the input data by low dimensional embedding, with feature-preserving property. The algorithm is based on the paper: Feature-preserving manifold approximation and projection to visulize single-cell data, Yang et al. 2023.
The algorithm is based on the UMAP algorithm, and the main difference is that FeatureMAP uses the tangent space of the input data to compute the (embedding) gauge for low dimensional embedding, where the features are locally illustrated by the embedding tangent space (i.e., gauge) and data points are embedded to the low dimensional space with the gauge.
- Parameters:
n_neighbors (float (optional, default 15)) – The size of local neighborhood (in terms of number of neighboring sample points) used for manifold approximation. Larger values result in more global views of the manifold, while smaller values result in more local data being preserved. In general values should be in the range 2 to 100.
n_components (int (optional, default 2)) – The dimension of the space to embed into. This defaults to 2 to provide easy visualization, but can reasonably be set to any integer value in the range 2 to 100.
metric (string (currently, only 'euclidean' is supported)) – The metric to use to compute distances in high dimensional space.
n_epochs (int (optional, default None)) – The number of training epochs to be used in optimizing the low dimensional embedding. Larger values result in more accurate embeddings. If None is specified a value will be selected based on the size of the input dataset (200 for large datasets, 500 for small).
learning_rate (float (optional, default 1.0)) – The initial learning rate for the embedding optimization.
init (string (optional, default 'spectral')) –
- How to initialize the low dimensional embedding. Options are:
’spectral’: use a spectral embedding of the fuzzy 1-skeleton
’random’: assign initial embedding positions at random.
A numpy array of initial embedding positions.
min_dist (float (optional, default 0.5)) – The effective minimum distance between embedded points. Smaller values will result in a more clustered/clumped embedding where nearby points on the manifold are drawn closer together, while larger values will result on a more even dispersal of points. The value should be set relative to the
spreadvalue, which determines the scale at which embedded points will be spread out.spread (float (optional, default 1.0)) – The effective scale of embedded points. In combination with
min_distthis determines how clustered/clumped the embedded points are.low_memory (bool (optional, default True)) – For some datasets the nearest neighbor computation can consume a lot of memory. If you find that FeatureMAP is failing due to memory constraints consider setting this option to True. This approach is more computationally expensive, but avoids excessive memory use.
set_op_mix_ratio (float (optional, default 1.0)) – Interpolate between (fuzzy) union and intersection as the set operation used to combine local fuzzy simplicial sets to obtain a global fuzzy simplicial sets. Both fuzzy set operations use the product t-norm. The value of this parameter should be between 0.0 and 1.0; a value of 1.0 will use a pure fuzzy union, while 0.0 will use a pure fuzzy intersection.
local_connectivity (int (optional, default 1)) – The local connectivity required – i.e. the number of nearest neighbors that should be assumed to be connected at a local level. The higher this value the more connected the manifold becomes locally. In practice this should be not more than the local intrinsic dimension of the manifold.
repulsion_strength (float (optional, default 1.0)) – Weighting applied to negative samples in low dimensional embedding optimization. Values higher than one will result in greater weight being given to negative samples.
negative_sample_rate (int (optional, default 5)) – The number of negative samples to select per positive sample in the optimization process. Increasing this value will result in greater repulsive force being applied, greater optimization cost, but slightly more accuracy.
a (float (optional, default None)) – More specific parameters controlling the embedding. If None these values are set automatically as determined by
min_distandspread.b (float (optional, default None)) – More specific parameters controlling the embedding. If None these values are set automatically as determined by
min_distandspread.random_state (int, RandomState instance or None, optional (default: None)) – If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.
metric_kwds (dict (optional, default None)) – Arguments to pass on to the metric, such as the
pvalue for Minkowski distance. If None then no arguments are passed on.angular_rp_forest (bool (optional, default False)) – Whether to use an angular random projection forest to initialise the approximate nearest neighbor search. This can be faster, but is mostly on useful for metric that use an angular style distance such as cosine, correlation etc. In the case of those metrics angular forests will be chosen automatically.
transform_seed (int (optional, default 42)) – Random seed used for the stochastic aspects of the transform operation. This ensures consistency in transform operations.
verbose (bool (optional, default False)) – Controls verbosity of logging.
tqdm_kwds (dict (optional, defaul None)) – Key word arguments to be used by the tqdm progress bar.
unique (bool (optional, default False)) – Controls if the rows of your data should be uniqued before being embedded. If you have more duplicates than you have n_neighbour you can have the identical data points lying in different regions of your space. It also violates the definition of a metric. For to map from internal structures back to your data use the variable _unique_inverse_.
featuremap (bool (optional, default False)) – Specifies whether the featity-augmented objective of FeatureMAP should be used for optimization. Turning on this option generates an embedding where the local featities are encouraged to be correlated with those in the original space. Parameters below with the prefix ‘feat’ further control the behavior of this extension.
feat_lambda (float (optional, default 2.0)) – Controls the regularization weight of the anisotropic density correlation term in FeatureMAP. Higher values prioritize density preservation over the FeatureMAP objective, and vice versa for values closer to zero. Setting this parameter to zero is equivalent to running the original UMAP algorithm.
feat_frac (float (optional, default 0.3)) – Controls the fraction of epochs (between 0 and 1) where the feature-augmented objective is used in FeatureMAP. The first (1 - feat_frac) fraction of epochs optimize the original UMAP objective before introducing the anisotropic density correlation term.
feat_gauge_coefficient (float (default 1.0)) – A coefficient multiplication of n_neighbors as number of neighbors in gauge construction. Larger coefficient means long distance information
feat_var_shift (float (optional, default 0.1)) – A small constant added to the variance of local radii in the embedding when calculating the anisotropic density correlation objective to prevent numerical instability from dividing by a small number
output_feat (bool (optional, default False)) – Whether to compute the embedding radius. If set to True, the algorithm will compute the embedding radius and return the embedding radius.
disconnection_distance (float (optional, default np.inf or maximal value for bounded distances)) – Disconnect any vertices of distance greater than or equal to disconnection_distance when approximating the manifold via our k-nn graph. This is particularly useful in the case that you have a bounded metric. The UMAP assumption that we have a connected manifold can be problematic when you have points that are maximally different from all the rest of your data. The connected manifold assumption will make such points have perfect similarity to a random set of other points. Too many such points will artificially connect your space.
output_variation (bool (optional, default True)) – Whether to compute the variation embedding only. If set to True, the algorithm will compute the variation embedding only and return the embedding. If set to False, the algorithm will compute the Feature-augmented embedding.
threshold (float (optional, default 0.9)) – The threshold to compute the intrinsic dimensionality of the data. The intrinsic dimensionality is computed by the number of principal components that accumulates 90% of the variance of the data.
- fit(X)[source]
Fit X into an embedded space.
- Parameters:
X (array, shape (n_samples, n_features) or (n_samples, n_samples)) – If the metric is ‘precomputed’ X must be a square distance matrix. Otherwise it contains a sample per row. If the method is ‘exact’, X may be a sparse matrix of type ‘csr’, ‘csc’ or ‘coo’.
- fit_transform(X)[source]
Fit X into an embedded space and return that transformed output.
- Parameters:
X (array, shape (n_samples, n_features) or (n_samples, n_samples)) – If the metric is ‘precomputed’ X must be a square distance matrix. Otherwise it contains a sample per row.
- Returns:
X_new – Embedding of the training data in low-dimensional space.
- Return type:
array, shape (n_samples, n_components)
- featuremap.featuremap_.clip(val)[source]
Standard clamping of a value into a fixed range (in this case -4.0 to 4.0)
- Parameters:
val (float) – The value to be clamped.
- Return type:
The clamped value, now fixed to be in the range -4.0 to 4.0.
- featuremap.featuremap_.rdist(x, y)[source]
Reduced Euclidean distance.
- Parameters:
x (array of shape (embedding_dim,))
y (array of shape (embedding_dim,))
- Return type:
The squared euclidean distance between x and y
- featuremap.featuremap_.optimize_layout_euclidean_anisotropic_projection(head_embedding, tail_embedding, head, tail, n_epochs, n_vertices, epochs_per_sample, a, b, rng_state, gamma=1.0, initial_alpha=1.0, negative_sample_rate=5.0, parallel=False, verbose=False, featuremap_kwds=None, tqdm_kwds=None, move_other=False)[source]
Improve an embedding using stochastic gradient descent to minimize the fuzzy set cross entropy between the 1-skeletons of the high dimensional and low dimensional fuzzy simplicial sets. In practice this is done by sampling edges based on their membership strength (with the (1-p) terms coming from negative sampling similar to word2vec). :param head_embedding: The initial embedding to be improved by SGD. :type head_embedding: array of shape (n_samples, n_components) :param tail_embedding: The reference embedding of embedded points. If not embedding new
previously unseen points with respect to an existing embedding this is simply the head_embedding (again); otherwise it provides the existing embedding to embed with respect to.
- Parameters:
head (array of shape (n_1_simplices)) – The indices of the heads of 1-simplices with non-zero membership.
tail (array of shape (n_1_simplices)) – The indices of the tails of 1-simplices with non-zero membership.
n_epochs (int) – The number of training epochs to use in optimization.
n_vertices (int) – The number of vertices (0-simplices) in the dataset.
epochs_per_sample (array of shape (n_1_simplices)) – A float value of the number of epochs per 1-simplex. 1-simplices with weaker membership strength will have more epochs between being sampled.
a (float) – Parameter of differentiable approximation of right adjoint functor
b (float) – Parameter of differentiable approximation of right adjoint functor
rng_state (array of int64, shape (3,)) – The internal state of the rng
gamma (float (optional, default 1.0)) – Weight to apply to negative samples.
initial_alpha (float (optional, default 1.0)) – Initial learning rate for the SGD.
negative_sample_rate (int (optional, default 5)) – Number of negative samples to use per positive sample.
parallel (bool (optional, default False)) – Whether to run the computation using numba parallel. Running in parallel is non-deterministic, and is not used if a random seed has been set, to ensure reproducibility.
verbose (bool (optional, default False)) – Whether to report information on the current progress of the algorithm.
featuremap (bool (optional, default False)) – Whether to use the feature-augmented featuremap objective
featuremap_kwds (dict (optional, default None)) – Auxiliary data for featuremap
tqdm_kwds (dict (optional, default None)) – Keyword arguments for tqdm progress bar.
move_other (bool (optional, default False)) – Whether to adjust tail_embedding alongside head_embedding
- Returns:
embedding – The optimized embedding.
- Return type:
array of shape (n_samples, n_components)