Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
10 changes: 7 additions & 3 deletions src/scanspec2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ def setpoints(self, indexes: np.ndarray) -> dict[AxisT, np.ndarray]:
"""Evaluate linear positions at *indexes*."""
result: dict[AxisT, np.ndarray] = {}
for ax, (start, stop) in self.axis_ranges.items():
if self._length <= 1:
step = stop - start
# If inquired for a single index the step size is
# the distance normalized by the original lenght.
if len(indexes) <= 1:
step = (stop - start) / self._length
# If inquired for multiple indexes step size becomes
# the original range normalized over the number of indexes.
else:
step = (stop - start) / (self._length - 1)
step = (stop - start) / (len(indexes) - 1)
first = start - step / 2
result[ax] = first + indexes * step
return result
Expand Down
27 changes: 27 additions & 0 deletions tests/scanspec2/__init__.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure we've got this helper somewhere else in tests/scanspec2 already...

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Any

import pytest


def approx(
expected: Any,
rel: float | None = None,
abs: float | None = None,
nan_ok: bool = False,
) -> Any:
"""
Temporary loosely typed wrapper around approx.
To be removed pending:
https://github.com/pytest-dev/pytest/issues/7469

Args:
expected: Expected value
rel: Relative tolerance. Defaults to None.
abs: Absolute tolerance. Defaults to None.
nan_ok: Permit nan. Defaults to False.

Returns:
Any: Approximate comparator
"""

return pytest.approx(expected, rel=rel, abs=abs, nan_ok=nan_ok) # type: ignore
79 changes: 79 additions & 0 deletions tests/scanspec2/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
Static,
)

from . import approx

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -1012,3 +1014,80 @@ def test_zip_rejects_monitors():
)
with pytest.raises(ValueError, match="Zip does not accept.*monitors"):
Linspace("y", 0, 1, 5).zip(a).compile() # type: ignore[reportArgumentType]


def test_acquire_fly_scan_window_positions():
# Detector with total acquisition time of 1s
det = DetectorGroup(1, 10, 0.9, 0.1, ["spec_det1"])
spec: Acquire[str, str, Never] = Acquire(
spec=Linspace("x", 0, 10, 10), fly=True, detectors=[det], stream_name="spec"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The numbers make my head hurt. Please could you redo the test with Linspace("x", 11, 20, 10)

)
scan = spec.compile()
ws = windows(scan)
assert len(ws) == 1
for w in ws:
# Return positions spaced 1s apart
for p in w.positions(1, None):
assert len(p["x"]) == 10
assert p["x"] == approx(
[
-0.55555556,
0.67901235,
1.91358025,
3.14814815,
4.38271605,
5.61728395,
6.85185185,
8.08641975,
9.32098765,
10.55555556,
]
)
Comment on lines +1028 to +1045

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I would expect this to have size 11, and be [0.5, ... 10.5] as we are returning bounds rather than midpoints

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this would be the expected behaviour. Currently all the method does is shift the first point to be the equivalent to the first lower bound and then move all the subsequent points based on that, but it does not increase the expected number of points to also include the last upper bound.
That should be an easy enough change to make though if that's the intended behaviour.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the intended behaviour. The Spec should say "I want 10 points, each is 1s long". The positions function is asked "Give me points every 1s. That means it gets 11 points, from lower bound of the first point to upper bound of the last point. If it doesn't do this then we should modify it so it does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll push the changes that implement this logic.

det = DetectorGroup(1, 10, 1, 1, ["spec_det1"])
spec: Acquire[str, str, Never] = Acquire(
spec=Linspace("x", 0, 10, 10), fly=True, detectors=[det], stream_name="spec"
)
scan = spec.compile()
ws = windows(scan)
assert len(ws) == 1
for w in ws:
# Return positions spaced 1s apart
for p in w.positions(1, None):
assert len(p["x"]) == 20
assert p["x"] == approx(
[
-0.26315789,
0.29085873,
0.84487535,
1.39889197,
1.95290859,
2.50692521,
3.06094183,
3.61495845,
4.16897507,
4.72299169,
5.27700831,
5.83102493,
6.38504155,
6.93905817,
7.49307479,
8.04709141,
8.60110803,
9.15512465,
9.70914127,
10.26315789,
]
)


def test_acquire_step_scan_window_positions():
det = DetectorGroup(1, 10, 1, 1, ["spec_det1"])
spec: Acquire[str, str, Never] = Acquire(
spec=Linspace("x", 0, 10, 10), fly=False, detectors=[det], stream_name="spec"
)
scan = spec.compile()
ws = windows(scan)
pos = np.linspace(0, 10, 10, endpoint=False)
assert len(ws) == 10
for w, p in zip(ws, pos, strict=True):
assert w.static_axes == {"x": p}
Loading