-
Notifications
You must be signed in to change notification settings - Fork 60
[PyMcaMath] fix numpy.dot usage in PCA Tools #1163
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
Conversation
Modified numpy.dot usage to select the first element of the result.
|
I am not satisfied with this fix. Everywhere in PyMca it is assumed the dot product returns a scalar. Probably the issue is on the shape of the input arrays. |
|
Now it is better. However, issue #1162 is found almost everywhere in PyMca. |
| tmpData.shape = 1, nChannels | ||
| if subtractAndNormalize: | ||
| tmpData = (tmpData[:, ::binning] - avgSpectrum) / standardDeviation | ||
| else: | ||
| tmpData = tmpData[:, ::binning] | ||
| tmpData = tmpData.reshape(nChannels) |
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.
For reference:
tmpData.shape = 1, nChannels
...
numpy.dot(tmpData, eigenvectors[j])is like
>>> numpy.zeros((1,1024)).dot(numpy.ones((1024,)))
array([0.])which is to be expected
(1×n)(n)→(1)
So the solution is to squeeze the first dimension before doing the matrix multiplication
>>> numpy.zeros((1024,)).dot(numpy.ones((1024,)))
np.float64(0.0)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.
Note that this could have been detected earlier if we run the tests with -W error
>>> import numpy
>>> arr = numpy.zeros((5,5))
>>> arr[0,0] = numpy.asarray([0])
<stdin>:1: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)This is why we always run with -W error. I'll make an issue for that.
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 prefer reshape or ravel rather than a math operation. I guess it will be faster or at least use less memory.
closes #1161