Skip to content

Implement the NullPatternGenerator #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
3 changes: 2 additions & 1 deletion src/ophyd_async/sim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
VerticalMirror,
)
from ._motor import FlySimMotorInfo, SimMotor
from ._pattern_generator import PatternGenerator
from ._pattern_generator import NullPatternGenerator, PatternGenerator
from ._point_detector import SimPointDetector
from ._stage import SimStage

Expand All @@ -18,6 +18,7 @@
"FlySimMotorInfo",
"SimStage",
"PatternGenerator",
"NullPatternGenerator",
"SimPointDetector",
"SimBlobDetector",
"VerticalMirror",
Expand Down
28 changes: 28 additions & 0 deletions src/ophyd_async/sim/_pattern_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,31 @@ def close_file(self):
if self._file:
self._file.close()
self._file = None


class NullPatternGenerator(PatternGenerator):
"""A pattern generator that generates all zeros and saves no file."""

def __init__(self, sleep=asyncio.sleep):
super().__init__(sleep)
self.n_images = 0

def open_file(self, path: Path, width: int, height: int):
pass

async def write_images_to_file(
self, exposure: float, period: float, number_of_frames: int
):
self.n_images += number_of_frames

def generate_point(self, channel: int = 1, high_energy: bool = False) -> float:
return 0.0

async def wait_for_next_index(self, timeout: float):
pass

def get_last_index(self) -> int:
return self.n_images

def close_file(self):
pass
41 changes: 41 additions & 0 deletions tests/sim/test_pattern_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from collections import defaultdict

import bluesky.plans as bp
from bluesky import RunEngine

from ophyd_async.core import StaticPathProvider
from ophyd_async.sim import NullPatternGenerator, SimBlobDetector
from ophyd_async.testing import assert_emitted


async def test_null_pattern_generator_does_nothing(RE: RunEngine):
pattern_generator = NullPatternGenerator()
path_provider = StaticPathProvider(lambda _: "null_file", "/")
detector = SimBlobDetector(
path_provider, pattern_generator=pattern_generator, name="det"
)
docs = defaultdict(list)
RE.subscribe(lambda name, doc: docs[name].append(doc))
RE(bp.count([detector], num=2))

assert_emitted(
docs, start=1, descriptor=1, stream_resource=2, stream_datum=4, event=2, stop=1
)
assert docs["descriptor"][0]["data_keys"] == {
"det": {
"source": "sim://pattern-generator-hdf-file",
"shape": [1, 240, 320],
"dtype": "array",
"dtype_numpy": "|u1",
"object_name": "det",
"external": "STREAM:",
},
"det-sum": {
"source": "sim://pattern-generator-hdf-file",
"shape": [],
"dtype": "number",
"dtype_numpy": "<i8",
"object_name": "det",
"external": "STREAM:",
},
}
Loading