modflowpy / flopy

A Python package to create, run, and post-process MODFLOW-based models.
https://flopy.readthedocs.io
Other
517 stars 313 forks source link

New datatype checking (or np.ndarray requirements) in flopy/modpath/mp7particledata.py? #2072

Closed wzell-usgs closed 9 months ago

wzell-usgs commented 9 months ago

I'm not sure if the following is a bug or, instead, a consequence of something implicitly understood about use of np.ndarrays. I believe that the following behavior is new to 3.5.0 as I'm experiencing this error in a script that worked on a previous machine (and earlier version of flopy):

>>> flopy.__version__
'3.5.0.dev0'
>>> np.__version__
'1.26.0'

For partlocs as a 1D np.ndarray (e.g., partlocs.shape = (38890, )

npd = flopy.modpath.ParticleData(partlocs=partlocs, drape=1)

results in the following error:

    npd = flopy.modpath.ParticleData(partlocs=partlocs, drape=1)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wzell\projects\wzell\flopy\flopy\modpath\mp7particledata.py", line 174, in __init__
    partlocs = unstructured_to_structured(partlocs, dtype=dtype)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wzell\miniforge-pypy3\envs\ngwm_wq1.0\Lib\site-packages\numpy\lib\recfunctions.py", line 1153, in unstructured_to_structured
    raise ValueError('The length of the last dimension of arr must '
ValueError: The length of the last dimension of arr must be equal to the number of fields in dtype

Recasting partlocs to (i) an explicit 1D np.ndarray or (ii) a list work as before:

partlocs = partlocs.reshape(len(partlocs), 1)
npd = flopy.modpath.ParticleData(partlocs=partlocs, drape=1)
partlocs = list(partlocs)
npd = flopy.modpath.ParticleData(partlocs=partlocs, drape=1)
wpbonelli commented 9 months ago

@wzell-usgs thanks for reporting, this regression was introduced with flopy 3.4 in a patch meant to fix this issue. It is indeed a bug