Skip to content

Commit

Permalink
Merge pull request #1150 from wright-group/issue-1118
Browse files Browse the repository at this point in the history
safeguard join "method" kwarg
  • Loading branch information
kameyer226 authored Jan 29, 2024
2 parents 880e054 + 8a290eb commit a7c0461
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 29 deletions.
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

0 comments on commit a7c0461

Please sign in to comment.