Skip to content

Fixes to gatan2 / SerialEM plugin - #170

Merged
Baharis merged 2 commits into
instamatic-dev:mainfrom
Baharis:Jaedong-gatan
Sep 12, 2026
Merged

Fixes to gatan2 / SerialEM plugin#170
Baharis merged 2 commits into
instamatic-dev:mainfrom
Baharis:Jaedong-gatan

Conversation

@Baharis

@Baharis Baharis commented Sep 9, 2026

Copy link
Copy Markdown
Member

This PR encapsulates several fixes performed by Jaedong Kang (@jdkang0810) from the Korea Advanced Institute of Science and Technology, who adapted current version of Instamatic to an FEI / Gatan Rio16 setup. The changes were reviewed, refactored, and mostly accepted by me. Text below comes from our e-mail exchange and was written by Jaedong and modified by me.

The changes were performed for Python 3.11+ while using SerialEMCCD/SEMCCD plugin(GMS 3.42) in DigitalMicrograph(DM 3.42.3048), leading to the following communication scheme: Instamatic → CameraGatan2 / GatanSocket → SerialEMCCD/SEMCCD plugin in DigitalMicrograph → Rio16.

Issues and changes

  • On the 64-bit Python installation, numpy.int_ is 64-bit, whereas the Windows C long values expected by the SEMCCD socket protocol are 32-bit. This causes incorrect message lengths and errors such as Command wrong length: needed=16 numBytes=104. Thus, gatansocket3.py was modified to explicitly encode integers as 32-bit little-endian (<i4 / np.int32).
  • DM/SEMCCD Port obtained from the environment variable is explicitly converted to an integer.
  • Host can now be used instead of always forcing 127.0.0.1.
  • Socket timeouts were added (controlled by a new SERIALEMCCD_TIMEOUT, 10 seconds by default).
  • The connection can reconnect and retry once if a send operation fails, making communication considerably more stable;
  • camera_gatan2.py was modified to favor a standard argument name binsize rather than binning, though both are accepted [@jdkang0810 was subtle about it, but I added an explicit warning that use of binning is deprecated!].
  • The image dimensions and ROI sent to the SEMCCD plugin are now dynamically adjusted for binning – no need to calculate them manually in the config file, as was required before.
  • The corrections argument used for image acquisition was changed from 0 to -1, allowing DM to use Rio16's own default settings instead of disabling them.
  • The image-transfer code in gatansocket3.py was modified to receive images into a 1D bytearray first, followed by conversion to a uint16 NumPy array, making transfer more robust, particularly for the current Python version and larger Rio16 images.
  • [A naive implementation of get_movie was added to camera_gatan2.py to better integrate it with new code - Daniel]

@Baharis
Baharis requested a review from stefsmeets September 9, 2026 17:32
@Baharis Baharis self-assigned this Sep 9, 2026
@Baharis Baharis added the bug label Sep 9, 2026
@Baharis

Baharis commented Sep 9, 2026

Copy link
Copy Markdown
Member Author

@jdkang0810 This PR only concerns changes to the gatan2 camera; I'm still thinking if and how introduce the others. I modified your code quite a bit to match code style, also I'm annoying like that and like short lines and f'{this=}' syntax. I moved some comments to doctrings. I also decided not to add type checks inside gatansocket3.py (it is a low-level object and these should be already checked earlier) and hard-coded binning assertion in {1, 2, 4}. Other than that, IMO this is a great fix that might have otherwise been an issue for others attempting to connect a Rio camera.

Judging by the state of this code, I doubt we have any active developers relying on gatansocket3.py / camera_gatan2.py, but just in case I will let this PR sit for a few days in case anyone has any comments.

@Baharis
Baharis merged commit ded7d4b into instamatic-dev:main Sep 12, 2026
6 checks passed
@Baharis
Baharis deleted the Jaedong-gatan branch September 12, 2026 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant