Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9f2e67e
WIP Merlin integration
jennmald Jun 13, 2025
57a4ae9
trigger mixins
jennmald Jun 17, 2025
7e97dae
fix modalbase
jennmald Jun 23, 2025
9757246
change name
jennmald Jun 23, 2025
c09d4a2
CDIModalSettings
jennmald Jun 23, 2025
2ee5e77
fix file writing templates
jennmald Jun 24, 2025
fd4940c
fix dtype
jennmald Jun 26, 2025
d5859fe
fix hdf5 warning
jennmald Jun 26, 2025
8f5f964
hdf5 with file store
jennmald Jun 26, 2025
3332a9a
debugging
jennmald Jun 27, 2025
b1ba21a
fix data type
jennmald Jun 27, 2025
3cc2f5c
clean up data types
jennmald Jun 27, 2025
0180fef
fix precommit
jennmald Jul 9, 2025
1b99844
Check for mutliple master files for each datum
thopkins32 May 27, 2025
e9bd7f4
pre-commit
thopkins32 May 27, 2025
916a1df
fix ruff
jennmald Jul 10, 2025
9d762ec
more fixes for mypy
jennmald Jul 11, 2025
f978692
ignore ophyd errors
jennmald Jul 11, 2025
887a4a1
add new line:
jennmald Jul 11, 2025
df2a8c1
finished trigger mixins
jennmald Jul 11, 2025
bca24de
passing mypy:
jennmald Jul 11, 2025
6c78c41
satisfy ruff
jennmald Jul 11, 2025
2a5eaf6
blank space
jennmald Jul 22, 2025
ffefd6f
fix eiger merge conflicts
jennmald Jul 22, 2025
7d57e6b
fix utils
jennmald Jul 22, 2025
c40433e
fix pyright
jennmald Jul 22, 2025
b4f097f
eof
jennmald Jul 22, 2025
9b0f72d
remove env
jennmald Jul 22, 2025
a6299a3
add suggestions for makedirs and fix a merge conflict error
jennmald Jul 24, 2025
24323a1
fix pre-commit
jennmald Jul 24, 2025
714f535
Update README.md
jennmald Jul 31, 2025
7c25a27
fix prettier
jennmald Jul 31, 2025
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@
[rtd-link]: https://cditools.readthedocs.io/en/latest/?badge=latest

<!-- prettier-ignore-end -->

## Pyright Configuration

The `pyrightconfig.json` file configures the
[Pyright type checker](https://github.com/microsoft/pyright) for this project.
It sets the type checking mode to "basic" for less strict analysis and disables
warnings about missing type stubs and untyped base classes. This helps minimize
unnecessary alerts from third-party libraries that lack type information,
allowing you to focus on type issues within your own codebase.
5 changes: 5 additions & 0 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"typeCheckingMode": "basic",
"reportMissingTypeStubs": false,
"reportUntypedBaseClass": false
}
Comment thread
jennmald marked this conversation as resolved.
191 changes: 191 additions & 0 deletions src/cditools/merlin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
from __future__ import annotations

import logging
from collections import OrderedDict

from ophyd import (
AreaDetector,
CamBase,
Device,
EpicsSignal,
HDF5Plugin,
ProcessPlugin,
ROIPlugin,
StatsPlugin,
TIFFPlugin,
TransformPlugin,
)
from ophyd import Component as Cpt
from ophyd.areadetector import EpicsSignalWithRBV
from ophyd.areadetector.base import ADComponent
from ophyd.areadetector.filestore_mixins import FileStorePluginBase, FileStoreTIFF
from ophyd.utils.paths import makedirs

from .trigger_mixins import CDIModalTrigger, FileStoreBulkReadable

logger = logging.getLogger(__name__)


class MerlinTiffPlugin(TIFFPlugin, FileStoreBulkReadable, FileStoreTIFF, Device):
def mode_external(self) -> None:
total_points = self.parent.mode_settings.total_points.get() # type: ignore[union-attr]
self.stage_sigs[self.num_capture] = total_points

def get_frames_per_point(self) -> object:
mode = self.parent.mode_settings.mode.get() # type: ignore[union-attr]
if mode == "external":
return 1
return self.parent.cam.num_images.get() # type: ignore[union-attr]

def describe(self) -> object:
ret = super().describe()
key = self.parent._image_name # type: ignore[union-attr]
ret[key].setdefault("dtype_str", "<u2") # type: ignore[attr-defined]
return ret


class MerlinDetectorCam(CamBase):
acquire = ADComponent(EpicsSignal, "Acquire")
quad_merlin_mode = ADComponent(EpicsSignalWithRBV, "QuadMerlinMode")


class MerlinDetector(AreaDetector):
cam = Cpt(
MerlinDetectorCam,
"cam1:",
read_attrs=[],
configuration_attrs=[
"image_mode",
"trigger_mode",
"acquire_time",
"acquire_period",
],
)


class MerlinFileStoreHDF5(FileStorePluginBase, FileStoreBulkReadable):
_spec = "TPX_HDF5"
filestore_spec = _spec

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.stage_sigs.update(
[
(self.file_template, "%s%s_%6.6d.h5"), # type: ignore[attr-defined]
(self.file_write_mode, "Stream"), # type: ignore[attr-defined]
(self.compression, "zlib"), # type: ignore[attr-defined]
(self.capture, 1), # type: ignore[attr-defined]
]
)

def stage(self) -> object:
logger.info("Staging")
staged = super().stage()
logger.info("Staging step 2")
res_kwargs = {"frame_per_point": 1}
logger.info("res_kwargs = {frame_per_point: %s}", res_kwargs["frame_per_point"])

logger.debug("Inserting resource with filename %s", self._fn)
logger.info("Inserting resource with filename %s", self._fn)
self._generate_resource(res_kwargs)
logger.info("generating resources")
logger.info("Staged")
return staged

def describe(self) -> OrderedDict[str, dict]:
ret = super().describe()
key = self.parent._image_name # type: ignore[union-attr]
ret[key].setdefault("dtype_str", "<u2") # type: ignore[attr-defined]
return ret # type: ignore[return-value]

def make_filename(self) -> tuple[str, str, str]:
fn, read_path, write_path = super().make_filename()
mode_settings = self.parent.mode_settings # type: ignore[union-attr]
if mode_settings.make_directories.get():
makedirs(read_path)
return fn, read_path, write_path


class HDF5PluginWithFileStore(HDF5Plugin, MerlinFileStoreHDF5):
def stage(self) -> object:
mode_settings = self.parent.mode_settings # type: ignore[union-attr]
total_points = mode_settings.total_points.get()
self.stage_sigs[self.num_capture] = total_points

# ensure that setting capture is the last thing that's done
self.stage_sigs.move_to_end(self.capture)
return super().stage()

def describe(self):
ret = super().describe()
key = self.parent._image_name # type: ignore[union-attr]
ret[key].setdefault("dtype_str", "<u2") # type: ignore[attr-defined]
return ret


class CDIMerlinDetector(CDIModalTrigger, MerlinDetector):
hdf5 = Cpt(
HDF5PluginWithFileStore,
"HDF1:",
read_attrs=[],
configuration_attrs=[],
write_path_template="/nsls2/data/tst/legacy/mock-proposals/2025-2/pass-56789/assets/merlin/%Y/%m/%d",
root="/nsls2/data/tst/legacy/mock-proposals/2025-2/pass-56789/assets/merlin",
Comment thread
mrakitin marked this conversation as resolved.
)

proc1 = Cpt(ProcessPlugin, "Proc1:")
stats1 = Cpt(StatsPlugin, "Stats1:")
stats2 = Cpt(StatsPlugin, "Stats2:")
stats3 = Cpt(StatsPlugin, "Stats3:")
stats4 = Cpt(StatsPlugin, "Stats4:")
stats5 = Cpt(StatsPlugin, "Stats5:")
transform1 = Cpt(TransformPlugin, "Trans1:")
roi1 = Cpt(ROIPlugin, "ROI1:")
roi2 = Cpt(ROIPlugin, "ROI2:")
roi3 = Cpt(ROIPlugin, "ROI3:")
roi4 = Cpt(ROIPlugin, "ROI4:")

def __init__(
self,
prefix,
*,
read_attrs: list[str] | None = None,
configuration_attrs: list[str] | None = None,
**kwargs,
):
if read_attrs is None:
read_attrs = ["hdf5", "cam"]
if configuration_attrs is None:
configuration_attrs = ["hdf5", "cam"]

if "hdf5" not in read_attrs:
# ensure that hdf5 is still added, or data acquisition will fail
read_attrs = [*list(read_attrs), "hdf5"]

super().__init__(
prefix,
configuration_attrs=configuration_attrs,
read_attrs=read_attrs,
**kwargs,
)

def mode_internal(self) -> None:
super().mode_internal()

count_time = self.count_time.get()
if isinstance(count_time, float):
self.stage_sigs[self.cam.acquire_time] = count_time
self.stage_sigs[self.cam.acquire_period] = count_time + 0.005

def mode_external(self) -> None:
super().mode_external()

# NOTE: these values specify a debounce time for external triggering so
# they should be set to < 0.5 the expected exposure time, or at
# minimum the lowest possible dead time = 1.64ms
expected_exposure = 0.001
min_dead_time = 0.00164
self.stage_sigs[self.cam.acquire_time] = expected_exposure
self.stage_sigs[self.cam.acquire_period] = expected_exposure + min_dead_time

self.cam.stage_sigs[self.cam.trigger_mode] = "Trigger Enable"
Loading
Loading