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

paCMAP generate an error , #13

Open
Marwansha opened this issue Dec 7, 2023 · 1 comment
Open

paCMAP generate an error , #13

Marwansha opened this issue Dec 7, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@Marwansha
Copy link

using same code with MAP projecion works fine

projections=["MAP"]

`tg = tp.TopOGraph(n_eigs=119, n_jobs=-1, verbosity=0)

tg.run_models(adata.X, kernels=['bw_adaptive'],
eigenmap_methods=['msDM'],
projections=["PaCMAP"])


ValueError Traceback (most recent call last)
Cell In[65], line 3
1 tg = tp.TopOGraph(n_eigs=119, n_jobs=-1, verbosity=0)
----> 3 tg.run_models(adata.X, kernels=['bw_adaptive'],
4 eigenmap_methods=['msDM'],
5 projections=["PaCMAP"])

File ~/venvs/topometry/lib/python3.11/site-packages/topo/topograph.py:1013, in TopOGraph.run_models(self, X, kernels, eigenmap_methods, projections)
1011 gc.collect()
1012 for projection in projections:
-> 1013 self.project(projection_method=projection)
1014 gc.collect()

File ~/venvs/topometry/lib/python3.11/site-packages/topo/topograph.py:925, in TopOGraph.project(self, n_components, init, projection_method, landmarks, landmark_method, n_neighbors, num_iters, **kwargs)
923 projection_key = projection_method + ' of ' + key
924 start = time.time()
--> 925 Y = Projector(n_components=n_components,
926 projection_method=projection_method,
927 metric=metric,
928 n_neighbors=self.graph_knn,
929 n_jobs=self.n_jobs,
930 landmarks=landmarks,
931 landmark_method=landmark_method,
932 num_iters=num_iters,
933 init=init_Y,
934 nbrs_backend=self.backend,
935 keep_estimator=False,
936 random_state=self.random_state,
937 verbose=self.layout_verbose).fit_transform(input, **kwargs)
938 end = time.time()
939 gc.collect()

File ~/venvs/topometry/lib/python3.11/site-packages/sklearn/utils/_set_output.py:157, in _wrap_method_output..wrapped(self, X, *args, **kwargs)
155 @wraps(f)
156 def wrapped(self, X, *args, **kwargs):
--> 157 data_to_wrap = f(self, X, *args, **kwargs)
158 if isinstance(data_to_wrap, tuple):
159 # only wrap the first output for cross decomposition
160 return_tuple = (
161 _wrap_data_with_container(method, data_to_wrap[0], X, self),
162 *data_to_wrap[1:],
163 )

File ~/venvs/topometry/lib/python3.11/site-packages/topo/layouts/projector.py:416, in Projector.fit_transform(self, X, **kwargs)
405 def fit_transform(self, X, **kwargs):
406 """
407 Calls the fit_transform method of the desired method.
408 If the desired method does not have a fit_transform method, calls the results from the fit method.
(...)
413 Projection results
414 """
--> 416 self.fit(X, **kwargs)
417 return self.Y_

File ~/venvs/topometry/lib/python3.11/site-packages/topo/layouts/projector.py:318, in Projector.fit(self, X, **kwargs)
315 metric = self.metric
316 self.estimator_ = pacmap.PaCMAP(n_components=self.n_components, n_neighbors=self.n_neighbors,
317 apply_pca=False, distance=metric, num_iters=self.num_iters, verbose=self.verbose, **kwargs)
--> 318 self.Y_ = self.estimator_.fit_transform(X=X, init=self.init_Y_)
320 elif self.projection_method == 'TriMAP':
321 try:

File ~/venvs/topometry/lib/python3.11/site-packages/pacmap/pacmap.py:943, in PaCMAP.fit_transform(self, X, init, save_pairs)
925 def fit_transform(self, X, init=None, save_pairs=True):
926 '''Projects a high dimensional dataset into a low-dimensional embedding and return the embedding.
927
928 Parameters
(...)
940 Whether to save the pairs that are sampled from the dataset. Useful for reproducing results.
941 '''
--> 943 self.fit(X, init, save_pairs)
944 if self.intermediate:
945 return self.intermediate_states

File ~/venvs/topometry/lib/python3.11/site-packages/pacmap/pacmap.py:906, in PaCMAP.fit(self, X, init, save_pairs)
904 self.num_dimensions = X.shape[1]
905 # Initialize and Optimize the embedding
--> 906 self.embedding_, self.intermediate_states, self.pair_neighbors, self.pair_MN, self.pair_FP = pacmap(
907 X,
908 self.n_components,
909 self.pair_neighbors,
910 self.pair_MN,
911 self.pair_FP,
912 self.lr,
913 self.num_iters,
914 init,
915 self.verbose,
916 self.intermediate,
917 self.intermediate_snapshots,
918 pca_solution,
919 self.tsvd_transformer
920 )
921 if not save_pairs:
922 self.del_pairs()

File ~/venvs/topometry/lib/python3.11/site-packages/pacmap/pacmap.py:539, in pacmap(X, n_dims, pair_neighbors, pair_MN, pair_FP, lr, num_iters, Yinit, verbose, intermediate, inter_snapshots, pca_solution, tsvd)
536 intermediate_states = None
538 # Initialize the embedding
--> 539 if (Yinit is None or Yinit == "pca"):
540 if pca_solution:
541 Y = 0.01 * X[:, :n_dims]

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()`

@davisidarta
Copy link
Owner

This seems to be an error with PaCMAP. Probably they updated the library and now it takes a different format for initialization. TopOMetry uses spectral initialization for all projections, while PaCMAP defaults to PCA initialization. In most systems, this shouldn't cause really big differences.

I'll update the Projector class to handle whatever updates they might have done and should push a fixed version later today.

For now, you could try running PaCMAP on the latent eigenspace you learned with topometry:

# I suppose this is the eigenspace you're using, from your input:

latent_eigenspace = tg.EigenbasisDict['msDM with bw_adaptive'].results() 
  
embedding = pacmap.PaCMAP(n_components=2, n_neighbors=None, MN_ratio=0.5, FP_ratio=2.0) 

X_transformed = embedding.fit_transform(latent_eigenspace, init="pca") 

Alternatively, use the spectral initialization:

spectral_init = tg.spectral_layout()

X_transformed = embedding.fit_transform(latent_eigenspace, init=spectral_init) 

Let me know if this workaround works for now :)

@davisidarta davisidarta self-assigned this Dec 7, 2023
@davisidarta davisidarta added the bug Something isn't working label Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants