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

fix(particledata): support 1D numpy array for partlocs #2074

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions autotest/test_particledata.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading