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

safeguard join "method" kwarg #1150

Merged
merged 6 commits into from
Jan 29, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).

### Fixed
- `interact2D`: fixed bug where use_imshow broke the sliders
- `data.join` ensures valid `method` is selected

### Changed
- `data.join` no longer supports `sum` method

## [3.5.0]

Expand Down
11 changes: 8 additions & 3 deletions WrightTools/data/_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def join(
The name for the data object which is created. Default is 'join'.
parent : WrightTools.Collection (optional)
The location to place the joined data object. Default is new temp file at root.
method : {'first', 'last', 'min', 'max', 'sum', 'mean'}
method : {'first', 'last', 'min', 'max', 'mean'}
Mode to use for merged points in the joined space.
Default is 'first'.
verbose : bool (optional)
Expand All @@ -76,6 +76,11 @@ def join(
warnings.warn("join", category=wt_exceptions.EntireDatasetInMemoryWarning)
if isinstance(datas, Collection):
datas = datas.values()
valid_methods = ["first", "last", "min", "max", "mean"]
if method not in valid_methods:
if method == "sum":
raise ValueError(f"method 'sum' is deprecated; consider 'mean' instead.")
raise ValueError(f"invalid method {method!r}: expected {valid_methods}")
datas = list(datas)
if not isinstance(atol, collections.abc.Iterable):
atol = [atol] * len(datas[0].axes)
Expand Down Expand Up @@ -177,7 +182,7 @@ def get_shape(out, datas, item_name):
out.create_channel(
shape=get_shape(out, datas, channel_name),
**datas[0][channel_name].attrs,
dtype=datas[0][channel_name].dtype
dtype=datas[0][channel_name].dtype,
)
count[channel_name] = np.zeros_like(out[channel_name], dtype=int)
for variable_name in variable_names:
Expand All @@ -186,7 +191,7 @@ def get_shape(out, datas, item_name):
out.create_variable(
shape=get_shape(out, datas, variable_name),
**datas[0][variable_name].attrs,
dtype=datas[0][variable_name].dtype
dtype=datas[0][variable_name].dtype,
)
count[variable_name] = np.zeros_like(out[variable_name], dtype=int)

Expand Down
3 changes: 1 addition & 2 deletions examples/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
last = wt.data.join([a, b], method="last", name="last")
min = wt.data.join([a, b], method="min", name="min")
max = wt.data.join([a, b], method="max", name="max")
sum = wt.data.join([a, b], method="sum", name="sum")
mean = wt.data.join([a, b], method="mean", name="mean")

# Plot the splits in columns
fig, gs = wt.artists.create_figure(nrows=4, cols=[1, 1])
for i, da in enumerate([a, b, first, last, min, max, sum, mean]):
for i, da in enumerate([a, b, first, last, min, max, mean]):
ax = plt.subplot(gs[i])
ax.pcolor(da, vmin=0, vmax=6)
wt.artists.corner_text(da.natural_name, ax=ax)
Expand Down
24 changes: 0 additions & 24 deletions tests/data/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,30 +515,6 @@ def test_overlap_last():
joined.close()


def test_overlap_sum():
a = wt.Data()
b = wt.Data()

a.create_variable("x", np.linspace(0, 10, 11))
b.create_variable("x", np.linspace(5, 15, 11))
a.transform("x")
b.transform("x")
a.create_channel("y", np.ones_like(a.x[:]))
b.create_channel("y", np.ones_like(b.x[:]) * 2)

joined = wt.data.join([a, b], method="sum")

assert joined.shape == (16,)
assert np.allclose(joined.x.points, np.linspace(0, 15, 16))
assert np.isclose(joined.y[0], 1.0)
assert np.isclose(joined.y[10], 3.0)
assert np.isclose(joined.y[-1], 2.0)

a.close()
b.close()
joined.close()


def test_overlap_max():
a = wt.Data()
b = wt.Data()
Expand Down
Loading