|
| 1 | +# Measurement API Reimplementation Plan (Python) |
| 2 | + |
| 3 | +## Goals |
| 4 | + |
| 5 | +1. Reimplement the Python measurement interface on top of the stable low-level `hdf5.py` wrapper (without changing `hdf5.py`). |
| 6 | +2. Support both **reading** and **writing** measurements with serializer-driven behavior. |
| 7 | +3. Use the serializer abstractions from `ecal.msg` (`common`, `proto`, `string`) instead of hardcoded protobuf-only behavior. |
| 8 | +4. Add clear samples for: |
| 9 | + - binary measurement read/write, |
| 10 | + - protobuf measurement read/write, |
| 11 | + - string measurement read/write, |
| 12 | + following the naming and structure style used by existing Python communication samples. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Phase 1: Baseline and API contract definition |
| 17 | + |
| 18 | +1. Inventory current measurement APIs: |
| 19 | + - `measurement.py` (reader abstractions), |
| 20 | + - `writer.py` (writer abstractions), |
| 21 | + - current measurement samples. |
| 22 | +2. Define a target public API that unifies reading and writing: |
| 23 | + - explicit `MeasurementReader` and `MeasurementWriter` entry points, |
| 24 | + - channel-level typed access based on serializers, |
| 25 | + - raw binary access for all channels. |
| 26 | +3. Preserve compatibility where practical: |
| 27 | + - keep legacy class/function names as thin wrappers (deprecated) if needed, |
| 28 | + - avoid changing data layout or low-level HDF5 behavior. |
| 29 | + |
| 30 | +Deliverable: short API sketch in docstrings / module comments before implementation. |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +## Phase 2: Serializer-driven type model for measurement |
| 35 | + |
| 36 | +1. Introduce a measurement-local datatype info object compatible with `ecal.msg.common.serializer.DataTypeInfo` protocol: |
| 37 | + - fields: `name`, `encoding`, `descriptor`. |
| 38 | +2. Implement robust conversion helpers: |
| 39 | + - parse channel type from HDF5 (`"encoding:type"`, fallback to empty encoding), |
| 40 | + - compose channel type string when writing, |
| 41 | + - map HDF5 channel metadata <-> serializer datatype info. |
| 42 | +3. Provide a reusable serializer selection strategy: |
| 43 | + - caller passes a serializer instance explicitly for typed channels, |
| 44 | + - optional convenience constructors for protobuf / string. |
| 45 | + |
| 46 | +Deliverable: internal helpers that isolate metadata parsing and serializer matching logic. |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +## Phase 3: Reading reimplementation |
| 51 | + |
| 52 | +1. Build a new binary-first reader core on top of `hdf5.Meas`: |
| 53 | + - channel metadata object, |
| 54 | + - channel entry object with `rcv_timestamp`, `snd_timestamp`, `payload`. |
| 55 | +2. Add typed read adapters: |
| 56 | + - `read_binary(...)` returns raw bytes, |
| 57 | + - `read_typed(serializer=...)` uses `accepts_data_with_type(...)` + `deserialize(...)`. |
| 58 | +3. Support protobuf and string through msg serializers: |
| 59 | + - protobuf: `ecal.msg.proto.serializer.Serializer` / `DynamicSerializer`, |
| 60 | + - string: `ecal.msg.string.serializer.Serializer`. |
| 61 | +4. Define explicit behavior for type mismatches and deserialization failures: |
| 62 | + - raise informative exceptions with channel + datatype context, |
| 63 | + - optionally provide iteration mode that reports and skips malformed entries. |
| 64 | +5. Provide dynamic-channel support: |
| 65 | + - expose a `DynamicChannelReader` path that selects deserialization based on channel metadata, |
| 66 | + - for protobuf channels, use `ecal.msg.proto.serializer.DynamicSerializer` to return dynamically typed protobuf objects. |
| 67 | + |
| 68 | +Deliverable: measurement reading path that no longer hardcodes protobuf logic in `measurement.py`. |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +## Phase 4: Writing reimplementation |
| 73 | + |
| 74 | +1. Build writer channels that always store metadata from serializer datatype info: |
| 75 | + - `set_channel_type(encoding:type)` and `set_channel_description(descriptor)`. |
| 76 | +2. Implement generic typed writer: |
| 77 | + - accepts serializer + message object, |
| 78 | + - serializes to bytes and writes timestamps. |
| 79 | +3. Implement explicit binary writer: |
| 80 | + - accepts bytes payload directly, |
| 81 | + - optional metadata override (`encoding`, `type`, `descriptor`) for non-empty typed binary streams. |
| 82 | +4. Add convenience factories: |
| 83 | + - protobuf writer channel from protobuf type, |
| 84 | + - string writer channel, |
| 85 | + - raw binary writer channel. |
| 86 | +5. Ensure writer can create all three required sample formats (binary, protobuf, string). |
| 87 | + |
| 88 | +Deliverable: `writer.py` no longer protobuf-only in `create_channel`; supports binary and string cleanly. |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## Phase 5: Backward compatibility and migration |
| 93 | + |
| 94 | +1. Keep old symbols where feasible and redirect to new internals. |
| 95 | +2. Mark legacy-only APIs as deprecated in docstrings. |
| 96 | +3. Document migration examples: |
| 97 | + - old protobuf writer usage -> new serializer-based usage. |
| 98 | + |
| 99 | +Deliverable: existing users are not broken abruptly while new APIs are available. |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## Phase 6: Samples (new high-level measurement examples) |
| 104 | + |
| 105 | +Create six samples under `lang/python/samples/measurement/` using consistent naming: |
| 106 | + |
| 107 | +1. `binary_write.py` |
| 108 | +2. `binary_read.py` |
| 109 | +3. `person_write.py` (protobuf) |
| 110 | +4. `person_read.py` (protobuf) |
| 111 | +5. `string_write.py` |
| 112 | +6. `string_read.py` |
| 113 | + |
| 114 | +Sample conventions: |
| 115 | + |
| 116 | +- mirror style of `samples/core/pubsub/*_send.py` and `*_receive.py` naming, |
| 117 | +- minimal dependencies and predictable output, |
| 118 | +- configurable path to output measurement directory, |
| 119 | +- comments that explain datatype metadata and serializer usage. |
| 120 | + |
| 121 | +Also add/update sample README with run order and expected output. |
| 122 | + |
| 123 | +Deliverable: discoverable, task-oriented examples for all requested formats. |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## Phase 7: Test plan |
| 128 | + |
| 129 | +1. Add or extend Python tests for measurement API: |
| 130 | + - roundtrip binary, |
| 131 | + - roundtrip protobuf, |
| 132 | + - roundtrip string, |
| 133 | + - serializer mismatch behavior, |
| 134 | + - metadata preservation (`encoding`, `name/type`, `descriptor`). |
| 135 | +2. Keep tests deterministic (fixed timestamps / payloads). |
| 136 | +3. Run targeted test subset and broader suite as available. |
| 137 | + |
| 138 | +Deliverable: automated confidence for read/write and serializer integration. |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +## Phase 8: Rollout checklist |
| 143 | + |
| 144 | +1. Verify imports and package paths for both `ecalhdf5` and `msg` modules. |
| 145 | +2. Ensure no modifications to `hdf5.py`. |
| 146 | +3. Validate sample scripts execute in sequence: |
| 147 | + - write -> read for each format. |
| 148 | +4. Update changelog / release notes if required by repository practice. |
| 149 | + |
| 150 | +Deliverable: complete feature with docs, samples, and tests ready for review. |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## Suggested implementation order |
| 155 | + |
| 156 | +1. Metadata helper + datatype info bridge. |
| 157 | +2. New reader binary core and typed adapter. |
| 158 | +3. New writer generic channel + binary/proto/string convenience. |
| 159 | +4. Compatibility wrappers. |
| 160 | +5. New samples + README updates. |
| 161 | +6. Automated tests and final polish. |
0 commit comments