Skip to content

Commit

Permalink
artists.quicknD bug fix (#1160)
Browse files Browse the repository at this point in the history
* working for my issue

* apply same to quick2d

* ignore irrelevant constants

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update _quick.py

* Update _quick.py

* is pytest version the issue?

pytest 8.0.0 works locally for me...

* Update setup.py

* check os

* Update setup.py

* make pytest happy

for some reason it hates my use of * notation...

* restore setup.py

* Update __citation__.py

Path.__enter__ will be deprecated in python 3.13

* Update _interact.py

trim whitespace

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update from_PyCMDS.py

try to make test robust to intermittent FileNotFound errors

* add tests

* Update CHANGELOG.md

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ddkohler and pre-commit-ci[bot] authored Apr 1, 2024
1 parent 08509de commit 8750f31
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
## [Unreleased]

### Fixed
- fixed Quick2D/Quick1D issues where collapsing unused dims did not work
- wt5 explore : fixed bug where data will not load interactively if directory is not cwd
- constants in chopped data will inherit the units of the original data

Expand Down
4 changes: 2 additions & 2 deletions WrightTools/__citation__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

here = pathlib.Path(__file__).parent

with here / "CITATION" as f:
with open(str(here / "CITATION")) as f:
# remove extra whitespace
__citation__ = " ".join(f.read_text().split())
__citation__ = " ".join(f.read().split())
4 changes: 3 additions & 1 deletion WrightTools/artists/_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ def interact2D(
if nsliders < 0:
raise DimensionalityError(">= 2", data.ndim)
# TODO: implement aspect; doesn't work currently because of our incorporation of colorbar
fig, gs = create_figure(width="single", nrows=7 + nsliders, cols=[1, 1, 1, 1, 1, "cbar"])
fig, gs = create_figure(
width="single", margin=[0.2, 1, 0.2, 1], nrows=7 + nsliders, cols=[1, 1, 1, 1, 1, "cbar"]
)
plt.get_current_fig_manager().set_window_title(f"interact2D: {data.natural_name}")
# create axes
ax0 = plt.subplot(gs[1:6, 0:5])
Expand Down
60 changes: 28 additions & 32 deletions WrightTools/artists/_quick.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def quick1D(
autosave=False,
save_directory=None,
fname=None,
verbose=True
verbose=True,
):
"""Quickly plot 1D slice(s) of data.
Expand Down Expand Up @@ -65,14 +65,15 @@ def quick1D(
list of strings
List of saved image files (if any).
"""
# channel index
channel_index = wt_kit.get_index(data.channel_names, channel)
shape = data.channels[channel_index].shape
collapse = [i for i in range(len(shape)) if shape[i] == 1]
at = at.copy()
at.update({c: 0 for c in collapse})
# remove dimensions that do not involve the channel
channel_slice = [0 if size == 1 else slice(None, None) for size in shape]
sliced_constants = [
data.axis_expressions[i] for i in range(len(shape)) if not channel_slice[i]
]
# prepare data
with closing(data.chop(axis, at=at, verbose=False)) as chopped:
with closing(data._from_slice(channel_slice).chop(axis, at=at, verbose=False)) as chopped:
# prepare figure
fig = None
if len(chopped) > 10:
Expand Down Expand Up @@ -132,18 +133,19 @@ def quick1D(
plt.xticks(rotation=45)
plt.axvline(0, lw=2, c="k")
plt.xlim(xi.min(), xi.max())
# add constants to title
# constants: variable marker lines, title
ls = []
for constant in d.constants:
if constant.expression in sliced_constants:
# ignore these constants; no relation to the data
continue
ls.append(constant.label)
title = ", ".join(ls)
_title(fig, data.natural_name, subtitle=title)
# variable marker lines
for constant in d.constants:
if constant.units is not None:
if axis.units_kind == constant.units_kind:
constant.convert(axis.units)
plt.axvline(constant.value, color="k", linewidth=4, alpha=0.25)
title = ", ".join(ls)
_title(fig, data.natural_name, subtitle=title)
# save --------------------------------------------------------------------------------
if autosave:
if fname:
Expand Down Expand Up @@ -175,7 +177,7 @@ def quick2D(
autosave=False,
save_directory=None,
fname=None,
verbose=True
verbose=True,
):
"""Quickly plot 2D slice(s) of data.
Expand Down Expand Up @@ -223,21 +225,14 @@ def quick2D(
# channel index
channel_index = wt_kit.get_index(data.channel_names, channel)
shape = data.channels[channel_index].shape
collapse = [i for i in range(len(shape)) if shape[i] == 1]
at = at.copy()
at.update({c: 0 for c in collapse})
# prepare data
with closing(data.chop(xaxis, yaxis, at=at, verbose=False)) as chopped:
# colormap
# get colormap
if cmap is None:
if data.channels[channel_index].signed:
cmap = "signed"
else:
cmap = "default"
cmap = colormaps[cmap]
cmap.set_bad([0.75] * 3, 1.0)
cmap.set_under([0.75] * 3, 1.0)
# remove axes that are independent of channel
channel_slice = [0 if size == 1 else slice(None, None) for size in shape]
sliced_constants = [
data.axis_expressions[i] for i in range(len(shape)) if not channel_slice[i]
]
with closing(
data._from_slice(channel_slice).chop(xaxis, yaxis, at=at, verbose=False)
) as chopped:
# fname
if fname is None:
fname = data.natural_name
Expand Down Expand Up @@ -344,14 +339,13 @@ def quick2D(
# add zero lines
plt.axvline(0, lw=2, c="k")
plt.axhline(0, lw=2, c="k")
# add constants to title
# constants: variable marker lines, title
ls = []
for constant in d.constants:
if constant.expression in sliced_constants:
# ignore these constants; no relation to the data
continue
ls.append(constant.label)
title = ", ".join(ls)
_title(fig, data.natural_name, subtitle=title)
# variable marker lines
for constant in d.constants:
if constant.units is not None:
# x axis
if xaxis.units_kind == constant.units_kind:
Expand All @@ -361,6 +355,8 @@ def quick2D(
if yaxis.units_kind == constant.units_kind:
constant.convert(yaxis.units)
plt.axhline(constant.value, color="k", linewidth=4, alpha=0.25)
title = ", ".join(ls)
_title(fig, data.natural_name, subtitle=title)
# colorbar
cax = plt.subplot(gs[1])
cbar_ticks = np.linspace(levels.min(), levels.max(), 11)
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ add-ignore = D105
description_file = README.rst
license_file = LICENSE.txt


[tool:pytest]
addopts = --durations=10 --cov=WrightTools --verbose --cov-report term:skip-covered
python_files = tests/*.py
16 changes: 16 additions & 0 deletions tests/artists/test_quick1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,26 @@ def test_2D():
wt.artists.quick1D(data)


def test_moment_channel():
w1 = np.linspace(-2, 2, 51)
w2 = np.linspace(-1, 1, 3)
w3 = np.linspace(1, 3, 5)
signal = np.cos(w1[:, None, None] + w2[None, :, None] + w3[None, None, :])
data = wt.data.Data(name="data")
data.create_channel("signal", values=signal, signed=True)
data.create_variable("w1", values=w1[:, None, None], units="wn", label="1")
data.create_variable("w2", values=w2[None, :, None], units="wn", label="2")
data.create_variable("w3", values=w3[None, None, :], units="wn", label="3")
data.transform("w1", "w2", "w3")
data.moment(2, 0, 0)
wt.artists.quick1D(data, channel=-1)


if __name__ == "__main__":
import matplotlib.pyplot as plt

plt.close("all")
test_perovskite()
test_2D()
test_moment_channel()
plt.show()
16 changes: 16 additions & 0 deletions tests/artists/test_quick2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,26 @@ def test_4D():
wt.artists.quick2D(data, xaxis=0, yaxis=1)


def test_moment_channel():
w1 = np.linspace(-2, 2, 51)
w2 = np.linspace(-1, 1, 11)
w3 = np.linspace(1, 3, 5)
signal = np.cos(w1[:, None, None] + w2[None, :, None] + w3[None, None, :])
data = wt.data.Data(name="data")
data.create_channel("signal", values=signal, signed=True)
data.create_variable("w1", values=w1[:, None, None], units="wn", label="1")
data.create_variable("w2", values=w2[None, :, None], units="wn", label="2")
data.create_variable("w3", values=w3[None, None, :], units="wn", label="3")
data.transform("w1", "w2", "w3")
data.moment(2, 0, 0)
wt.artists.quick2D(data, channel=-1)


if __name__ == "__main__":
import matplotlib.pyplot as plt

plt.close("all")
test_perovskite()
test_4D()
test_moment_channel()
plt.show()
11 changes: 10 additions & 1 deletion tests/data/from_PyCMDS.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,16 @@ def test_centers_tolerance():


def test_remote():
data = wt.data.from_PyCMDS("https://osf.io/rdn7v/download")
i = 0
while True:
# sometimes doesn't work, so try a few times
try:
data = wt.data.from_PyCMDS("https://osf.io/rdn7v/download")
break
except FileNotFoundError as e:
if i == 5:
raise e
i += 1
assert data.shape == (21, 81)
assert data.axis_expressions == ("wm", "w2=w1")
data.close()
Expand Down

0 comments on commit 8750f31

Please sign in to comment.