Skip to content

Commit

Permalink
fix(particledata): support 1D numpy array for partlocs (#2074)
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Jan 25, 2024
1 parent 4051244 commit ea73e0d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
70 changes: 70 additions & 0 deletions autotest/test_particledata.py
Expand Up @@ -59,6 +59,16 @@ def flatten(a):
("drape", "<i4"),
]
)
unstructured_dtype = np.dtype(
[
("node", "<i4"),
("localx", "<f4"),
("localy", "<f4"),
("localz", "<f4"),
("timeoffset", "<f4"),
("drape", "<i4"),
]
)


def test_particledata_structured_ctor_with_partlocs_as_list_of_tuples():
Expand Down Expand Up @@ -99,6 +109,66 @@ def test_particledata_structured_ctor_with_partlocs_as_ndarray():
)


def test_particledata_unstructured_ctor_with_partlocs_as_ndarray():
locs = np.array([0, 1, 2])
data = ParticleData(partlocs=locs, structured=False)

assert data.particlecount == 3
assert data.dtype == unstructured_dtype
assert isinstance(data.particledata, pd.DataFrame)
assert np.array_equal(
data.particledata.to_records(index=False),
np.core.records.fromrecords(
[
(0, 0.5, 0.5, 0.5, 0.0, 0),
(1, 0.5, 0.5, 0.5, 0.0, 0),
(2, 0.5, 0.5, 0.5, 0.0, 0),
],
dtype=unstructured_dtype,
),
)


def test_particledata_unstructured_ctor_with_partlocs_as_list():
locs = [0, 1, 2]
data = ParticleData(partlocs=locs, structured=False)

assert data.particlecount == 3
assert data.dtype == unstructured_dtype
assert isinstance(data.particledata, pd.DataFrame)
assert np.array_equal(
data.particledata.to_records(index=False),
np.core.records.fromrecords(
[
(0, 0.5, 0.5, 0.5, 0.0, 0),
(1, 0.5, 0.5, 0.5, 0.0, 0),
(2, 0.5, 0.5, 0.5, 0.0, 0),
],
dtype=unstructured_dtype,
),
)


def test_particledata_unstructured_ctor_with_partlocs_as_ndarray():
locs = np.array([0, 1, 2])
data = ParticleData(partlocs=locs, structured=False)

assert data.particlecount == 3
assert data.dtype == unstructured_dtype
assert isinstance(data.particledata, pd.DataFrame)
assert np.array_equal(
data.particledata.to_records(index=False),
np.core.records.fromrecords(
[
(0, 0.5, 0.5, 0.5, 0.0, 0),
(1, 0.5, 0.5, 0.5, 0.0, 0),
(2, 0.5, 0.5, 0.5, 0.0, 0),
],
dtype=unstructured_dtype,
),
)


def test_particledata_structured_ctor_with_partlocs_as_list_of_lists():
locs = [list(p) for p in [(0, 1, 1), (0, 1, 2)]]
data = ParticleData(partlocs=locs, structured=True)
Expand Down
9 changes: 7 additions & 2 deletions flopy/modpath/mp7particledata.py
Expand Up @@ -166,12 +166,17 @@ def __init__(
"one entry".format(self.name)
)

# convert partlocs composed of a lists/tuples of lists/tuples
# to a numpy array
# convert partlocs to numpy array with proper dtype
partlocs = np.array(partlocs)
if len(partlocs.shape) == 1:
partlocs = partlocs.reshape(len(partlocs), 1)
partlocs = unstructured_to_structured(
np.array(partlocs), dtype=dtype
)
elif isinstance(partlocs, np.ndarray):
# reshape and convert dtype if needed
if len(partlocs.shape) == 1:
partlocs = partlocs.reshape(len(partlocs), 1)
dtypein = partlocs.dtype
if dtypein != dtype:
partlocs = unstructured_to_structured(partlocs, dtype=dtype)
Expand Down

0 comments on commit ea73e0d

Please sign in to comment.