Skip to content

Conversation

@vasole
Copy link
Member

@vasole vasole commented Dec 21, 2025

closes #1161

Modified numpy.dot usage to select the first element of the result.
@vasole
Copy link
Member Author

vasole commented Dec 21, 2025

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.

@vasole
Copy link
Member Author

vasole commented Dec 21, 2025

Now it is better. However, issue #1162 is found almost everywhere in PyMca.

Comment on lines +690 to +695
tmpData.shape = 1, nChannels
if subtractAndNormalize:
tmpData = (tmpData[:, ::binning] - avgSpectrum) / standardDeviation
else:
tmpData = tmpData[:, ::binning]
tmpData = tmpData.reshape(nChannels)
Copy link
Collaborator

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)

Copy link
Collaborator

@woutdenolf woutdenolf Dec 22, 2025

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.

Copy link
Member Author

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.

@vasole vasole merged commit 3777452 into master Dec 22, 2025
6 checks passed
@vasole vasole deleted the vasole-patch-1 branch December 22, 2025 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tests do not pass with numpy 2.4.0

3 participants