From 7f00717142bc5fc819a888b7cf193e8028f57f11 Mon Sep 17 00:00:00 2001 From: Daniel Kohler <11864045+ddkohler@users.noreply.github.com> Date: Thu, 7 Apr 2022 23:30:54 -0500 Subject: [PATCH 1/3] initial attempt --- attune/_arrangement.py | 7 +++++++ attune/_instrument.py | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/attune/_arrangement.py b/attune/_arrangement.py index 25d38da..85801e0 100644 --- a/attune/_arrangement.py +++ b/attune/_arrangement.py @@ -89,6 +89,13 @@ def as_dict(self): out["tunes"] = {k: v.as_dict() for k, v in self._tunes.items()} return out + def plot(self): + nplots = len(self.tunes) + for i, ti in enumerate(self.tunes.values()): + plt.subplot(1, nplots, i + 1) + plt.scatter(ti.independent, ti.dependent) + plt.grid() + @property def ind_max(self): """The maximum independant (input) value for this arrangement.""" diff --git a/attune/_instrument.py b/attune/_instrument.py index 1e58c16..409a713 100644 --- a/attune/_instrument.py +++ b/attune/_instrument.py @@ -164,6 +164,12 @@ def load(self): """The POSIX timestamp for when this instrument was created, if it was stored.""" return self._load + def plot(self, arrangement=None): + arrangements = self.arrangments if arrangement is None else arrangement + for ar in arrangements: + plt.figure() + ar.plot() + def save(self, file): """Save the JSON representation into an open file.""" From 442d31e2af5601c6429fa8528ebba455b8ace4af Mon Sep 17 00:00:00 2001 From: Daniel Kohler <11864045+ddkohler@users.noreply.github.com> Date: Fri, 8 Apr 2022 22:46:42 -0500 Subject: [PATCH 2/3] working examples --- attune/_arrangement.py | 16 ++++++++++++---- attune/_instrument.py | 5 +++-- tests/instrument/test_plot.py | 25 +++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 tests/instrument/test_plot.py diff --git a/attune/_arrangement.py b/attune/_arrangement.py index 85801e0..4ea3762 100644 --- a/attune/_arrangement.py +++ b/attune/_arrangement.py @@ -4,6 +4,7 @@ from typing import Dict, Union import numpy as np +import matplotlib.pyplot as plt from ._tune import Tune from ._discrete_tune import DiscreteTune @@ -91,10 +92,17 @@ def as_dict(self): def plot(self): nplots = len(self.tunes) - for i, ti in enumerate(self.tunes.values()): - plt.subplot(1, nplots, i + 1) - plt.scatter(ti.independent, ti.dependent) - plt.grid() + axes = [plt.subplot(nplots, 1, i + 1) for i in range(nplots)] + for i, (name, ti) in enumerate(self.tunes.items()): + axes[i].scatter(ti.independent, ti.dependent) + axes[i].plot(ti.independent, ti.dependent) + axes[i].grid() + axes[i].set_ylabel(f"{name} ({ti.dep_units})") + # can I assume all independent vars are the same? + if i + 1 != nplots: + plt.xticks(visible=False) + else: + axes[i].set_xlabel(f"wavelength ({ti.ind_units})") @property def ind_max(self): diff --git a/attune/_instrument.py b/attune/_instrument.py index 409a713..86ff091 100644 --- a/attune/_instrument.py +++ b/attune/_instrument.py @@ -3,6 +3,7 @@ from datetime import datetime as _datetime from typing import Dict, Optional, Union +import matplotlib.pyplot as plt import json from ._arrangement import Arrangement @@ -165,8 +166,8 @@ def load(self): return self._load def plot(self, arrangement=None): - arrangements = self.arrangments if arrangement is None else arrangement - for ar in arrangements: + arrangements = self.arrangements if arrangement is None else arrangement + for ar in arrangements.values(): plt.figure() ar.plot() diff --git a/tests/instrument/test_plot.py b/tests/instrument/test_plot.py new file mode 100644 index 0000000..5ea29e8 --- /dev/null +++ b/tests/instrument/test_plot.py @@ -0,0 +1,25 @@ +import attune +import numpy as np + +def test_plot_arrangement(): + x = np.linspace(0, 2, 21) + a = attune.Tune(independent=x, dependent=x**0.5) + b = attune.Tune(independent=x, dependent=x**0.3) + arrangement = attune.Arrangement(name="test", tunes={"a": a, "b": b}) + arrangement.plot() + + +def test_plot_instrument(): + x = np.linspace(0, 2, 21) + a = attune.Tune(independent=x, dependent=x**0.5) + b = attune.Tune(independent=x, dependent=x**0.3) + arrangement = attune.Arrangement(name="test", tunes={"a": a, "b": b}) + instrument = attune.Instrument({"arr1":arrangement}, name="test") + instrument.plot() + + +if __name__ == "__main__": + import matplotlib.pyplot as plt + test_plot_arrangement() + test_plot_instrument() + plt.show() \ No newline at end of file From 73cd805d838513b520976931f031614775aa51aa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 9 Apr 2022 03:46:57 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/instrument/test_plot.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/instrument/test_plot.py b/tests/instrument/test_plot.py index 5ea29e8..4e163f7 100644 --- a/tests/instrument/test_plot.py +++ b/tests/instrument/test_plot.py @@ -1,6 +1,7 @@ import attune import numpy as np + def test_plot_arrangement(): x = np.linspace(0, 2, 21) a = attune.Tune(independent=x, dependent=x**0.5) @@ -14,12 +15,13 @@ def test_plot_instrument(): a = attune.Tune(independent=x, dependent=x**0.5) b = attune.Tune(independent=x, dependent=x**0.3) arrangement = attune.Arrangement(name="test", tunes={"a": a, "b": b}) - instrument = attune.Instrument({"arr1":arrangement}, name="test") + instrument = attune.Instrument({"arr1": arrangement}, name="test") instrument.plot() if __name__ == "__main__": import matplotlib.pyplot as plt + test_plot_arrangement() test_plot_instrument() - plt.show() \ No newline at end of file + plt.show()