This repository is a template for implementing a BrkRaw dataset hook (rules/specs/transforms + Python hook entry points).
- Copy this template into a new repository, then update
pyproject.tomlmetadata (name, entry point, dependencies) before publishing to PyPI. - Implement the dataset-specific hook in
src/brkraw_hook_template/hook.py. BrkRaw expects aHOOKdict that exposesget_dataobjandget_affine(and optionallyconvertfor a richer workflow). - Declare hook assets in
src/brkraw_hook_template/brkraw_hook.yaml, which references package-local directories likesrc/brkraw_hook_template/docs/,src/brkraw_hook_template/rules/,src/brkraw_hook_template/specs/, andsrc/brkraw_hook_template/transforms/. - Use
src/brkraw_hook_template/docs/as a writing template: define scope, document the rule/spec/transform flow, and record conversion notes and limitations. - Keep
build-backendonsetuptools/wheel. BrkRaw still has fragile file-fetch paths when hooks rely on hatch-based installers. - Optional maintainer tooling:
.vscode/tasks.jsoncontains release task placeholders,.github/workflows/contains CI/publish placeholders, andtests/contains a minimal hook install test.
- BrkRaw loads datasets via
from brkraw import load(seebrkraw/src/brkraw/__init__.py). The loader exposesget_scan(scan_id); hook code should operate on BrkRawScanobjects. get_dataobj(scan, **kwargs)andget_affine(scan, **kwargs)are called during conversion and should return the CLI-compatible types (typically numpy arrays, or tuples of arrays).- If conversion is multi-stage (trajectory reconstruction, RSS, caching, etc.), implement
convert(scan, metadata, **kwargs)and expose it viaHOOKso callers can opt into the richer workflow. - Always accept
**kwargs.brkraw convert(seebrkraw/src/brkraw/cli/commands/convert.py) forwards--hook-arg name:key=valueintohook_args_by_name={...}and ultimately into your hook functions (e.g.,--hook-arg template:offset=5). - Keep responsibilities separated:
rules/*.yamlfor detection/routing,specs/*.yamlfor metadata mapping,transforms/*.pyfor sanitisation helpers. Document shared transforms insrc/brkraw_hook_template/docs/(seesrc/brkraw_hook_template/docs/rule_spec_transform.md).
This section is for teams using an AI agent to help implement or extend a hook.
-
In your prompt, point the agent to this repository (
git@github.com:BrkRaw/brkraw-hook.git) and to at least one real hook example (e.g.,brkraw-mrsorbrkraw-sordino). -
Call out the non-negotiables: BrkRaw loads scans via
load(), hook code should operate onScan, and the hook must exposeHOOK = {'get_dataobj': ..., 'get_affine': ...}(plusconvertif needed).from brkraw import load loader = load("/path/to/source") scan = loader.get_scan(scan_id) metadata = scan.get_metadata(reco_id=1) dataobj = scan.get_dataobj(reco_id=1) affine = scan.get_affine(reco_id=1)
-
Tell the agent to wire options through
--hook-arg(kwargs), not by inventing new CLI flags. Ask it to implement a realconvert()(not a placeholder) when the workflow needs more thanget_dataobj/get_affine. -
Example prompt:
"Use git@github.com:BrkRaw/brkraw-hook.git as the base hook. Mimic brkraw-mrs: implement HOOK={'get_dataobj': ..., 'get_affine': ..., 'convert': ...} in hook.py, operate on Scan objects from brkraw.load(...).get_scan(scan_id), and use hook args (from --hook-arg) to enable optional preprocessing. Provide rule/spec/transform files that match the detection logic, and include a complete convert() implementation."
- BrkRaw's file-fetch path is fragile in some scenarios, so keep data local to the hook package or repository instead of relying on fragile remote bootstrapping paths.
- Rules should be the first line of defense: keep them focused on scan identifiers, modality flags, or Bruker sequence/method markers, then route to the right spec/hook.
info_specdrives the BrkRaw info parser (what appears inbrkraw info), whilemetadata_specfeedsget_metadataand sidecar generation. Both should map raw headers to BrkRaw metadata keys and call intotransforms/helpers for sanitisation (trim strings, convert units, inject constants).- Document transforms that are shared between specs so future hooks can reuse them; check
src/brkraw_hook_template/docs/rule_spec_transform.mdfor a general flow chart and template snippets. - Tests should exercise the hook integration path (install the hook, ensure rules/specs/transforms register) rather than only unit-testing isolated helpers.