Skip to content
Draft
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
15 changes: 15 additions & 0 deletions attune/_arrangement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -89,6 +90,20 @@ 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)
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):
"""The maximum independant (input) value for this arrangement."""
Expand Down
7 changes: 7 additions & 0 deletions attune/_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -164,6 +165,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.arrangements if arrangement is None else arrangement
for ar in arrangements.values():
plt.figure()
ar.plot()

def save(self, file):
"""Save the JSON representation into an open file."""

Expand Down
27 changes: 27 additions & 0 deletions tests/instrument/test_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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()