PaNOSC-ViNYL / SimEx

Start-to-end photon experiment simulation platform
https://simex.readthedocs.io/
GNU General Public License v3.0
26 stars 25 forks source link

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

Open godot11 opened 2 years ago

godot11 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

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