modflowpy / flopy

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

refactor: np.where(cond) -> np.asarray(cond).nonzero() #2238

Closed wpbonelli closed 2 months ago

wpbonelli commented 2 months ago

The docs for np.where() (https://numpy.org/doc/stable/reference/generated/numpy.where.html) suggest to prefer nonzero() over where() without x and y arguments. In the spirit of defensive programming I included np.asarray(cond) even where cond is already an array.

This PR also fixes a bug I introduced in the model splitter in https://github.com/modflowpy/flopy/pull/2124: while E711 (https://www.flake8rules.com/rules/E711.html) dictates comparisons to None should use identity rather than equality, this rule should not be applied to NumPy array selection conditions as it will change the semantics:

>>> a = np.array([None, None])
>>> a[a != None]
array([], dtype=object)
>>> a[a is not None]
array([[None, None]], dtype=object)

Unrelatedly, mark test_mt3d.py::test_mfnwt_keat_uzf() slow, it should not be included in smoke tests (this was causing the optional dependency CI tests to fail due to timeout). And clean up some unused imports in conftest.py.