Skip to content

Commit

Permalink
Add map_ind* methods (#94)
Browse files Browse the repository at this point in the history
* Add map_ind* methods

* if -> elif

* Add offset tests

* tests for maps
  • Loading branch information
ksunden committed Sep 24, 2020
1 parent a08c48c commit 0c8f011
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 35 deletions.
1 change: 1 addition & 0 deletions attune/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ._holistic import *
from ._instrument import *
from ._intensity import *
from ._map import *
from ._motor import *
from ._note import *
from ._offset import *
Expand Down
2 changes: 2 additions & 0 deletions attune/_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def __call__(self, ind_value, arrangement_name=None) -> Note:
valid.append(arrangement)
if len(valid) == 1:
arrangement = valid[0]
elif len(valid) == 0:
raise Exception(f"There are no valid arrangements at {ind_value}.")
else:
raise Exception("There are multiple valid arrangements! You must specify one.")
# call arrangement
Expand Down
33 changes: 29 additions & 4 deletions attune/_map.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
def map_ind_points(tune, setpoints, units="same"):
...
__all__ = ["map_ind_points", "map_ind_limits"]

import copy

def map_ind_limits(tune, min, max, units="same"):
...
import numpy as np

from ._transition import Transition
from ._tune import Tune


def map_ind_points(instrument, arrangement, tune, setpoints, units=None):
md = {"arrangement": arrangement, "tune": tune, "setpoints": setpoints, "units": units}
to_replace = instrument[arrangement][tune]
if units is not None:
setpoints = wt.units.convert(setpoints, units, to_replace.ind_units)
instr = copy.deepcopy(instrument)
instr[arrangement]._tunes[tune] = Tune(
setpoints, to_replace(setpoints), dep_units=to_replace.dep_units
)
instr._transition = Transition("map_ind_points", instrument, metadata=md)
instr._load = None
return instr


def map_ind_limits(instrument, arrangement, tune, min, max, units=None):
to_replace = instrument[arrangement][tune]
points = np.linspace(min, max, len(to_replace))
instr = map_ind_points(instrument, arrangement, tune, points, units)
md = {"arrangement": arrangement, "tune": tune, "min": min, "max": max, "units": units}
instr._transition = Transition("map_ind_limits", instrument, metadata=md)
return instr
2 changes: 1 addition & 1 deletion attune/_offset.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def offset_to(
):
to_offset = instrument[arrangement][tune]
current = to_offset(setpoint, ind_units=setpoint_units, dep_units=destination_units)
offset = current - destination
offset = destination - current
instr = offset_by(instrument, arrangement, tune, offset)
md = {
"arrangement": arrangement,
Expand Down
3 changes: 3 additions & 0 deletions attune/_tune.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def __call__(self, ind_value, *, ind_units=None, dep_units=None):
ret = wt.units.convert(ret, self._dep_units, dep_units)
return ret

def __len__(self):
return len(self.independent)

def __eq__(self, other):
if not np.allclose(self.independent, other.independent):
return False
Expand Down
30 changes: 0 additions & 30 deletions tests/Curve/offset.py

This file was deleted.

32 changes: 32 additions & 0 deletions tests/map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import attune
import numpy as np

import pytest


def test_map_ind_points():
tune = attune.Tune(np.linspace(1300, 1400, 20), np.linspace(-5, 5, 20))
arr = attune.Arrangement("test_map", {"test": tune})
inst0 = attune.Instrument({"test_map": arr}, {"test": attune.Motor("tune")})

inst1 = attune.map_ind_points(inst0, "test_map", "test", np.linspace(1310, 1450, 25))

test_points = np.linspace(1310, 1400, 14)
np.testing.assert_allclose(
inst1["test_map"]["test"](test_points), inst0["test_map"]["test"](test_points)
)
assert len(inst1["test_map"]["test"]) == 25


def test_map_ind_limits():
tune = attune.Tune(np.linspace(1300, 1400, 20), np.linspace(-5, 5, 20))
arr = attune.Arrangement("test_map", {"test": tune})
inst0 = attune.Instrument({"test_map": arr}, {"test": attune.Motor("tune")})

inst1 = attune.map_ind_limits(inst0, "test_map", "test", 1310, 1450)

test_points = np.linspace(1310, 1400, 14)
np.testing.assert_allclose(
inst1["test_map"]["test"](test_points), inst0["test_map"]["test"](test_points)
)
assert len(inst1["test_map"]["test"]) == len(inst0["test_map"]["test"])
32 changes: 32 additions & 0 deletions tests/offset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import attune
import numpy as np

import pytest


def test_offset_by():
tune = attune.Tune(np.linspace(1300, 1400, 20), np.linspace(-5, 5, 20))
arr = attune.Arrangement("test_offset", {"test": tune})
inst0 = attune.Instrument({"test_offset": arr}, {"test": attune.Motor("tune")})

inst1 = attune.offset_by(inst0, "test_offset", "test", 1.0)

np.testing.assert_allclose(
inst1["test_offset"]["test"].dependent, inst0["test_offset"]["test"].dependent + 1.0
)
np.testing.assert_allclose(
inst1["test_offset"]["test"].independent, inst0["test_offset"]["test"].independent
)


def test_offset_to():
tune = attune.Tune(np.linspace(1300, 1400, 20), np.linspace(-5, 5, 20))
arr = attune.Arrangement("test_offset", {"test": tune})
inst0 = attune.Instrument({"test_offset": arr}, {"test": attune.Motor("tune")})

inst1 = attune.offset_to(inst0, "test_offset", "test", 1.0, 1300)

np.testing.assert_allclose(inst1["test_offset"]["test"].dependent, np.linspace(1, 11, 20))
np.testing.assert_allclose(
inst1["test_offset"]["test"].independent, inst0["test_offset"]["test"].independent
)

0 comments on commit 0c8f011

Please sign in to comment.