Skip to content
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

feat: Remove sdk, use all python #135

Merged
merged 73 commits into from
Jun 14, 2023
Merged

feat: Remove sdk, use all python #135

merged 73 commits into from
Jun 14, 2023

Conversation

tlambert03
Copy link
Owner

@tlambert03 tlambert03 commented Jun 12, 2023

This re-implements the complete sdk in python, with many thanks to the great folks at LIM for all their help.

cleaned up version of #122
closes #122

@tlambert03
Copy link
Owner Author

some internal notes for myself... to keep track of what's going on here (copied here from #122 (comment))

  • nd2._pysdk._pysdk.ND2Reader is the new pure class that takes the place of the cython sdk wrapper _sdk.latest.ND2Reader

  • as a first pass, I'm re-implementing in python all of the metadata-reading methods from the sdk wrapper (those that returned structured objects). These include:

    class ND2Reader:
        @property
        def attributes(self) -> structures.Attributes: ...
        def metadata(self) -> structures.Metadata: ...
        def frame_metadata(self, seq_index: int) -> structures.FrameMetadata: ...
        def experiment(self) -> list[structures.ExpLoop]: ...
        def text_info(self) -> structures.TextInfo:
  • they each generally begin with parsing a specific chunk using ND2Reader._decode_chunk:

    attributes -> b"ImageAttributesLV!"
    _raw_image_metadata -> b"ImageMetadataSeqLV|0!"
    text_info -> b"ImageTextInfoLV!"
    experiment -> b"ImageMetadataLV!"
    
  • _decode_chunk loads the raw bytes for the key from the chunk map and then dispatches to one of two parsers based on the nd2 version, each of which is supposed to return a dict[str, Any]:

    • nd2 version < 3.0 uses nd2._xml.parse_variant_xml
    • nd2 version > 3.0 uses nd2._pysdk._decode.decode_CLxLiteVariant_json
  • there are then a number of functions in the nd2._pysdk._parse module that take the parsed data for each metadata section and convert them to "structured" objects (i.e. data class objects from the nd2.structures module). It's possible that the return type of these functions might be relaxed to simply return another "cleaned up dict" that could be passed to these data structures. but for now the actual data structure is returned. The "main" functions include:

    • nd2._pysdk._parse.load_metadata
    • nd2._pysdk._parse.load_exp_loop
    • nd2._pysdk._parse.load_attributes
    • nd2._pysdk._parse.load_text_info
    • nd2._pysdk._parse.load_global_metadata
    • nd2._pysdk._parse.load_metadata
    • nd2._pysdk._parse.load_frame_metadata
  • reading of image data itself is probably the simplest... it pulls the file offset for the requested frame index from the frame offsets (keys in the chunk map that start with b"ImageDataSeq|"), and then returns a numpy array from the memmaped file at the corresponding offset. If the frame is compressed, it uses zlib.decompress on the data

@tlambert03
Copy link
Owner Author

Everything is working and tests are passing. Going to merge this and do a little more cleanup in follow up PRs

@tlambert03 tlambert03 merged commit a840a29 into main Jun 14, 2023
@tlambert03 tlambert03 deleted the native-legacy-wip branch June 14, 2023 14:54
@tlambert03 tlambert03 added refactor enhancement New feature or request labels Jun 15, 2023
@tlambert03 tlambert03 changed the title refactor: Remove sdk, use all python feat: Remove sdk, use all python Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant