-
Notifications
You must be signed in to change notification settings - Fork 18
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
Modify Projection to Random Gaussian #45
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Mainly a few questions to briefly discuss and minor typos before merging.
@@ -214,6 +215,57 @@ def spectrum_to_vector(spectrum: MsmsSpectrum, min_mz: float, max_mz: float, | |||
return vector | |||
|
|||
|
|||
def spectrum_to_vector(spectrum: MsmsSpectrum, transformation: ss.csr_matrix, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: I assume that the sparse vectors need to be converted to dense vectors to be compatible with the Faiss index? Is there a benefit to using SparseRandomProjection
over GaussianRandomProjection
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is correct. Both use random projections, but each has its own advantages.
SparseRandomProjection is more efficient in calculation and requires less memory, so very ideal to very large vectors.
GaussianRandomProjection is not sparse and main advantage as known in the community is its ability to maintain pairwise distance between data points, after transformation. And I think that's what we want to aim for. Let's use a matrix using GaussianRandomProjection for transformation of spectra to low-dim vectors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Scikit-Learn documentation says this:
Sparse random matrices are an alternative to dense Gaussian random projection matrix that guarantees similar embedding quality while being much more memory efficient and allowing faster computation of the projected data.
Neither this statement nor that the Gaussian random projections should be better at conserving the pairwise distance is immediately obvious to me. Let's evaluate both for our specific context, then we can make an informed decision.
Returns | ||
------- | ||
np.ndarray | ||
The low-dimensional transformed spectrum vector with unit length. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: Unit length is only true if norm=True
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's what you had in the old version of the spectrum_to_vector docstring :).
It is obvious, from the docstring, the parameters of the function, and the code that you get a unit length vector if the norm parameter is True.
We can modify it with sthg else if you like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, let's just remove "with unit length" to make the documentation a bit more correct.
spectrum = spectrum.set_mz_range(min_mz, max_mz) | ||
# Convert a spectrum to a binned sparse vector | ||
data = np.array(spectrum.intensity, dtype=np.float32) | ||
indices = np.array( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: Nice way to avoid converting it to a dense vector.
indices = np.array( | ||
[math.floor((mz - min_mz) / bin_size) for mz in spectrum.mz], | ||
dtype=np.int32) | ||
indptr = np.array([0, len(spectrum.mz)], dtype=np.int32) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: I think you can use np.arange
instead.
(data, indices, indptr), (1, dim), np.float32, False) | ||
|
||
# Transform | ||
transformed_vector = (sparse_vector @ transformation).toarray() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment: This is pretty cool, I've probably never used this operator myself in code yet. 🙂 Is this matrix multiplication preferable over using transform()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are all similar, all vectorized alternatives.
We generate a random guassian matrix and transposed it, so we can use the @
operator, np.dot()
function, or pass the fitted model instead and use transform()
. I choose the first option :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I think that transform()
adds some safety checks, so maybe that's slightly preferable.
# Transform | ||
transformed_vector = (sparse_vector @ transformation).toarray() | ||
if norm: | ||
transformed_vector /= np.linalg.norm(transformed_vector) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment: Maybe there could be a small performance increase by computing the norm on the sparse vector and only afterwards converting to a dense vector?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll modify the transformation to Gaussian projection (given its advantage), and we'll need no further conversion to dense vector after the last dot product.
No description provided.