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

Deprecated usage of pylab.find results in AttributeError in XMDYNPhotonMatterAnalysis #247

Open
godot11 opened this issue Sep 27, 2022 · 6 comments

Comments

@godot11
Copy link

godot11 commented Sep 27, 2022

In Analysis.XMDYNPhotonMatterAnalysis.XMDYNPhotonMatterAnalysis.load_snapshot(), there is the following line:

xsnp['q'] = numpy.array([xsnp['ff'][pylab.find(xsnp['T']==x), 0] for x in xsnp['xyz']]).reshape(N,)

pylab.find seems no longer availble in the pylab namespace and this line results in an

AttributeError: module 'pylab' has no attribute 'find'

I couldn't find a reference to see when it was removed, but my environment corresponds to environment.yml.

@godot11
Copy link
Author

godot11 commented Sep 27, 2022

FWIW, in general the usage of pylab is discouraged as it's merely a convenience alias for numpy, scipy and matplotlib modules; it's better to directly reference the corresponding libraries. In XMDYNPhotonMatterAnalysis there's oy two location where it's used and both use pylab.find: the line above, and in XMDYNPhotonMatterAnalysis.load_sample:

sample['selZ'][sel_Z] = pylab.find(sel_Z == sample['Z'])

so it should be easy to remove it.

@godot11
Copy link
Author

godot11 commented Sep 27, 2022

The following seems to work, but I'm not sure it is conceptually equivalent to pylab.find:

 xsnp['q'] = numpy.array([xsnp['ff'][(numpy.array(xsnp['T'])==numpy.array(x)), 0] for x in xsnp['xyz']]).reshape(N,)

@JunCEEE
Copy link
Collaborator

JunCEEE commented Sep 27, 2022

The following seems to work, but I'm not sure it is conceptually equivalent to pylab.find:

 xsnp['q'] = numpy.array([xsnp['ff'][(numpy.array(xsnp['T'])==numpy.array(x)), 0] for x in xsnp['xyz']]).reshape(N,)

This solution looks fine to me. It may be simplified as:

xsnp['q'] = numpy.array([xsnp['ff'][xsnp['T']==x][0] for x in xsnp['xyz']]).reshape(N,)

Could you @godot11 test if it works?

@CFGrote What do you think?

@godot11
Copy link
Author

godot11 commented Sep 28, 2022

This solution looks fine to me. It may be simplified as:

xsnp['q'] = numpy.array([xsnp['ff'][xsnp['T']==x][0] for x in xsnp['xyz']]).reshape(N,)

Tested but doesn't work as the arrays in the test are not Numpy arrays.

@godot11
Copy link
Author

godot11 commented Sep 29, 2022

@JunCEEE Seems I was wrong. The above fix made the code working by all means, but the results for some charges don't make too much sense.

It seems pylab.find may not have been what I thought it was, a "numpyizer" wrapper around np.array(x) == np.array(y). Probably it was matplotlib.mlab.find which is simply

import numpy as np
def find(condition):
    res, = np.nonzero(np.ravel(condition))
    return res

(source), and was removed in matplotlib versions >= 3.1.
Where could I put the above snippet though?

@JunCEEE
Copy link
Collaborator

JunCEEE commented Sep 30, 2022

I think to just put it here is enough: https://github.com/PaNOSC-ViNYL/SimEx/blob/master/Sources/python/SimEx/Analysis/XMDYNPhotonMatterAnalysis.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants