Skip to content

feature(spm): Add trace/retrace reading for the .spm reader - #196

Open
derollins wants to merge 4 commits into
AFM-SPM:mainfrom
derollins:derollins/spm_retrace
Open

feature(spm): Add trace/retrace reading for the .spm reader#196
derollins wants to merge 4 commits into
AFM-SPM:mainfrom
derollins:derollins/spm_retrace

Conversation

@derollins

Copy link
Copy Markdown
Member

The .spm file reader opens channels by name however if there is both a trace and retrace channel within the file then only the trace channel is accessible. Trace and retrace are both meaningful and both can be important for analysis so it is important to be able to access both scan directions.

This PR makes both trace and retrace accessible for .spm files and exposes them with a new spm_channel_list() function.

Summary

  • the channel parameter still accepts a bare channel name as before ('Height Sensor' or 'Adhesion' etc.) and like before will open the trace channel, or if the trace channel is not available a retrace channel if available - this ensures backward compatibility,
  • the channel parameter also accepts an explicit direction ('Height Sensor trace' or 'Height Sensor retrace') which opens the trace or retrace channel respectively,
  • If the requested channel is missing or the explicitly requested direction (trace/retrace) is not available the raised error will now include all the available channels, both trace and retrace (i.e. 'Height Sensor trace', 'Height Sensor retrace', 'Phase trace', 'Phase retrace'...)
  • spm_channel_list() returns the available channels for a file (with trace/retrace directions), used internally to check channel presence and produce the informative errors. It may also be useful for building interactive channel pickers (e.g. the napari plugin).

Changes

  • Channel resolution in load_spm() now uses spm_channel_list() to build a list of available channels and matches the requested channel against it. Matching is case-insensitive. Because Bruker channel names contain spaces (e.g. 'Height Sensor'), the direction is a suffix on the full name ('Height Sensor retrace').
  • The requested direction is now passed through to pySPM (backward parameter) so retrace images actually load; previously the direction was never forwarded and only trace was returned.
  • Removed the previous error handling that caught pySPM's "channel not found" exception and re-raised it. The requested channel is now checked against the channel list up front, and a ValueError (listing the available channels) is raised before pySPM is called. As a result, any other pySPM errors now propagate directly rather than being caught and re-raised, this should help with debugging.

Backwards compatibility

Existing calls are unaffected, a bare channel name still returns the trace image preferentially as it always did. The new behaviour is opt-in via the direction suffix.

Tests

  • Loading a bare name, an explicit trace, and an explicit retrace channel from sample_0.spm.
  • Verifying (via mocks) that the requested direction reaches pySPM with the correct backward flag, including case-insensitive requests and the bare-name-defaults-to-trace case.
  • spm_channel_list() returns the expected channels with correct directions.
  • Unknown channels, and explicitly requested directions that don't exist, raise with the available channels listed.
  • Existing .spm tests updated for the new channel-list format. Full test suite passes.

One limitation: sample_0.spm doesn't contain a single channel present in both directions, so a direct "same channel, trace and retrace differ" test isn't possible against it. Direction handling is instead verified by checking the flag passed through to pySPM. This would require adding an extra file to the test resources, I'm sure I could find something suitable if you think this is important to have although it would increase the repo size.

NB: This feature may also be needed in other readers, .jpk, .h5-jpk and .ARIS already have trace/retrace reading but I'm not sure about the others. Some formats already have trace or retrace in channel names e.g. .ibw.

load_spm previously selected channels by name only, silently returning
the forward (trace) image and providing no way to load retrace. It also
only exposed available channels via an error message, not as data.

- Add spm_channel_list() to enumerate a file's channels with direction,
  keyed as '<name> trace' / '<name> retrace', reading only the parsed
  header (no pixel decode).
- Thread the direction through to pySPM's get_channel(backward=...) so
  retrace images actually load.
- For backward compatability accept bare names ('Height Sensor'), explicit direction ('Height
  Sensor retrace'), and any case; bare names default to trace (or
  retrace if that is the only direction present) and log which was used.

Backwards compatible: existing bare-name calls return trace as before.
… direction channel loading and channel list creation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant