Skip to content

Multiple scan XY API Change#51

Open
CSSFrancis wants to merge 15 commits intodirectelectron:mainfrom
CSSFrancis:multiple-scans
Open

Multiple scan XY API Change#51
CSSFrancis wants to merge 15 commits intodirectelectron:mainfrom
CSSFrancis:multiple-scans

Conversation

@CSSFrancis
Copy link
Copy Markdown
Member

This pull request introduces several improvements and bug fixes to the deapi client and its test suite. The most significant changes are focused on enhancing the flexibility and robustness of scan pattern handling, improving test reliability and performance, and refining property and acquisition management. Below is a summary of the most important changes, grouped by theme.

Scan Pattern Handling and Client Improvements:

  • Enhanced set_xy_array in deapi/client.py to support lists of numpy arrays, handle both 2D and 3D input, ensure integer types, validate shapes, and correctly compute scan dimensions. This improves flexibility and error handling for scan pattern input.
  • Modified the property setter to convert boolean values to "On"/"Off" strings, ensuring consistent property value handling.
  • Changed the default pixel_format in get_result to "AUTO" for better compatibility.

Test Suite Reliability and Performance:

  • Updated test fixtures to use scope="session" for the client fixture, reducing setup/teardown overhead and improving test performance.
  • Removed unnecessary time.sleep calls and replaced them with more robust idle-waiting logic (e.g., wait_for_idle(client)), leading to more reliable and faster tests. [1] [2] [3]
  • Set up a consistent initial state in test fixtures and removed redundant property assignments from individual tests. [1] [2]

Test Coverage and Robustness:

  • Added additional property setting and validation in tests, such as explicitly setting hardware binning before testing software binning options, to ensure test correctness. [1] [2]
  • Updated scan and acquisition tests to use more appropriate parameter values and improved error handling for edge cases. [1] [2]

File Saving and Loading Tests:

  • Marked slow or unreliable tests with @pytest.mark.skip(reason="Slow and broken") to improve test suite reliability and focus on stable tests. [1] [2]
  • Moved import of libertem.api into the test function to avoid unnecessary imports and potential issues when the library is not available.

@CSSFrancis
Copy link
Copy Markdown
Member Author

pre-commit.ci autofix

@CSSFrancis
Copy link
Copy Markdown
Member Author

pre-commit.ci autofix

@CSSFrancis CSSFrancis requested review from CoolFanyu and Copilot May 7, 2026 19:58
@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 13.06122% with 426 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...api/tests/test_scanning/test_continual_scanning.py 13.40% 252 Missing ⚠️
deapi/client.py 7.54% 98 Missing ⚠️
deapi/simulated_server/fake_server.py 14.28% 36 Missing ⚠️
deapi/tests/test_client.py 0.00% 17 Missing ⚠️
deapi/tests/test_utils/test_utils.py 16.66% 15 Missing ⚠️
deapi/data_types.py 36.36% 7 Missing ⚠️
...sts/test_file_saving/test_file_loading_libertem.py 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the deapi client protocol and test suite to better support advanced scanning workflows, most notably multiple XY scan patterns and (commandVer 16) virtual image buffer retrieval, along with documentation and test reliability improvements.

Changes:

  • Extend the client scan-pattern API (set_xy_array) to accept multiple patterns and broaden input handling/validation.
  • Add client + simulated server support for virtual image buffer metadata/data retrieval (GET_VIRTUAL_IMAGE_INFO / GET_VIRTUAL_IMAGE) and expose VirtualImageInfo.
  • Update docs and tests (Sphinx tabs, new scan design doc, fixture scoping, reduced sleeps, skip flaky/slow file tests).

Reviewed changes

Copilot reviewed 16 out of 17 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
pyproject.toml Add Windows-only dependency and adjust optional test/doc extras.
doc/reference/scan_design.rst New design document describing scan/XY pattern behavior and usage examples.
doc/reference/index.rst Include design document section in reference toctree.
doc/conf.py Enable sphinx_tabs extension for tabbed code examples.
deapi/version.py Change commandVersion definition (now hard-coded).
deapi/tests/test_utils/test_utils.py Refactor gain-reference setup into a session fixture; reduce sleeps/poll instead.
deapi/tests/test_scanning/test_continual_scanning.py Add comprehensive “continual scanning” server tests for repeated/multi-pattern scans and timing edge cases.
deapi/tests/test_scanning/init.py Package marker for scanning tests.
deapi/tests/test_file_saving/test_scan_pattern_saving.py Skip slow/broken scan saving test.
deapi/tests/test_file_saving/test_file_loading_rsciio.py Skip slow/broken RSCIO loading test; remove an extra sleep.
deapi/tests/test_file_saving/test_file_loading_libertem.py Move libertem import into test function.
deapi/tests/test_client.py Adjust fixtures/state handling; update timings and a few property interactions.
deapi/tests/conftest.py Make client fixture session-scoped and remove teardown sleep.
deapi/simulated_server/fake_server.py Implement fake handlers for virtual image buffer info/data commands.
deapi/data_types.py Add VirtualImageInfo type for virtual image buffer metadata/dtype mapping.
deapi/client.py Add virtual image buffer APIs, queue flags in start_acquisition, boolean property normalization, enhance set_xy_array, default pixel_format to AUTO.
deapi/init.py Export VirtualImageInfo from package root.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread deapi/client.py
Comment on lines +1270 to 1288
if isinstance(queue_virtual_buffers, bool):
vb = [queue_virtual_buffers] * 5
else:
if len(queue_virtual_buffers) != 5:
raise ValueError(
f"queue_virtual_buffers must be a list of exactly 5 booleans, "
f"got {len(queue_virtual_buffers)}."
)
vb = list(queue_virtual_buffers)

if self.width * self.height == 0:
log.error(" Image size is 0! ")
else:
bytesize = 0
command = self._addSingleCommand(
self.START_ACQUISITION,
None,
[number_of_acquisitions, request_movie_buffer],
[number_of_acquisitions, request_movie_buffer] + vb,
)
Comment thread deapi/client.py
Comment on lines +1444 to +1445
positions[i] = pos.astype(np.int32)
elif pos.ndim != 2 or pos.shape[1] != 2:
Comment thread deapi/client.py
num_positions.append(len(pos))

vals_to_send = [int(new_width), int(new_height)] + num_positions
print("Vals to send:", vals_to_send)
Comment thread deapi/client.py
Default is 5000.
virtual_image_info : VirtualImageInfo, optional
Pre-fetched virtual image metadata (width, height, data type). If
``None`` (default), :meth:`get_virtual_image_info` is called
Comment thread deapi/version.py
@@ -1,3 +1,3 @@
version = "5.3.0"
versionInfo = list(map(int, version.split(".")))
Comment on lines +248 to +255
client["Scan - Size X"] = 4
client["Scan - Size Y"] = 4
client["Scan - Repeats"] = 2
client["Scan - Camera Frames Per Point"] = 8
client["Scan - Enable"] = True
client["Test Pattern"] = "SW Frame Number"
client["Use DE Camera"] = "Use Frame Time"

client["Autosave Movie"] = "On"
client["Autosave Virtual Image 0"] = "On"
client["Autosave 4D File Format"] = "MRC"
client["Use DE Camera"] = "Use Frame Time"
Comment thread deapi/client.py
Comment on lines +1454 to +1460
if positions.ndim > 3:
log.error(
"Positions must be a 2D array of shape (N, 2) or 3D array of shape (M, N, 2)"
)
return False
elif positions.ndim == 2:
positions = positions[np.newaxis, :, :]
Comment thread deapi/client.py
Comment on lines +1996 to +2017
def get_virtual_image_buffer_info(self) -> VirtualImageInfo:
"""
Get information about the virtual image buffer from DE-Server.

Returns the buffer size, dimensions, and data type for the virtual image
produced by the active virtual detector configuration.

Returns
-------
VirtualImageInfo
Object containing:
- ``buffer_size`` : total bytes of one virtual image frame
- ``width`` : image width in pixels
- ``height`` : image height in pixels
- ``data_type`` : :class:`~deapi.DataType` of each pixel
"""
command = self._addSingleCommand(self.GET_VIRTUAL_IMAGE_INFO, None, None)
response = self._sendCommand(command)

info = VirtualImageInfo()
if response:
values = self.__getParameters(response.acknowledge[0])
Comment on lines 38 to 45
) # MRC file loading in LiberTEM is broken!
@pytest.mark.server
def test_save_4DSTEM(self, client, file_format):
import libertem.api as lt

if not os.path.exists("D:\Temp"):
os.mkdir("D:\Temp")
temp_dir = "D:\Temp"
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.

3 participants