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

Reduce timeouts and sleeps where possible to optimize tests. #210

Merged
merged 4 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions nion/device_kit/CameraDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,17 +304,20 @@ def __direct_acquire(self, cancel_event: threading.Event, do_sync: bool = False)
# if do_sync is True, the scan device will sync with its acquisition thread before
# advancing the pixel. this avoids race conditions of when the pixel count gets reset.
# this will be set only for the first frame in a sequence.
start = time.time()
start = time.perf_counter()
readout_area = self.readout_area
binning_shape = Geometry.IntSize(self.__binning, self.__binning if self.__symmetric_binning else 1)
# scan device is only set during synchronized acquisition
if self.__scan_device:
self.__scan_device.advance_pixel(do_sync=do_sync)
xdata = self.__simulator.get_frame_data(Geometry.IntRect.from_tlbr(*readout_area), binning_shape, self.__exposure, self.__instrument.scan_context, self.__instrument.probe_position)
self.__acquired_one_event.set()
elapsed = time.time() - start
elapsed = time.perf_counter() - start
wait_s = max(self.__exposure - elapsed, 0)
if not cancel_event.wait(wait_s):
actual_elapsed_s = time.perf_counter() - start
if actual_elapsed_s < self.__exposure:
time.sleep(self.__exposure - actual_elapsed_s) # adjust in case the wait returns early (observed on python 3.13/windows)
# thread event was not triggered during wait; signal that we have data
xdata._set_timestamp(DateTime.utcnow())
self.__xdata_buffer = xdata
Expand Down Expand Up @@ -433,7 +436,7 @@ def grab_partial(self, *, update_period: float = 1.0) -> typing.Tuple[bool, bool

class CameraSettings(camera_base.CameraSettings):

def __init__(self, camera_id: str):
def __init__(self, camera_id: str, base_exposure: float = 0.1):
# these events must be defined
self.current_frame_parameters_changed_event = Event.Event()
self.record_frame_parameters_changed_event = Event.Event()
Expand All @@ -456,9 +459,9 @@ def __init__(self, camera_id: str):

# configure profiles
self.__settings = [
camera_base.CameraFrameParameters({"exposure_ms": 100, "binning": 2}),
camera_base.CameraFrameParameters({"exposure_ms": 200, "binning": 2}),
camera_base.CameraFrameParameters({"exposure_ms": 500, "binning": 1}),
camera_base.CameraFrameParameters({"exposure_ms": 1 * 1000 * base_exposure, "binning": 2}),
camera_base.CameraFrameParameters({"exposure_ms": 2 * 1000 * base_exposure, "binning": 2}),
camera_base.CameraFrameParameters({"exposure_ms": 5 * 1000 * base_exposure, "binning": 1}),
]

self.__current_settings_index = 0
Expand Down
4 changes: 2 additions & 2 deletions nion/device_kit/ScanDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, frame_number: int, channels: typing.List[Channel], frame_para
self.complete = False
self.bad = False
self.data_count = 0
self.start_time = time.time()
self.start_time = time.perf_counter()
self.scan_data: typing.Optional[typing.List[_NDArray]] = None


Expand Down Expand Up @@ -298,7 +298,7 @@ def read_partial(self, frame_number: typing.Optional[int], pixels_to_skip: int)
pixels_remaining = min(total_pixels - current_frame.data_count, int(time_slice * 1e6 / frame_parameters.pixel_time_us) + 1)
pixel_wait = min(pixels_remaining * frame_parameters.pixel_time_us / 1E6, time_slice)
time.sleep(pixel_wait)
target_count = min(int((time.time() - current_frame.start_time) / (frame_parameters.pixel_time_us / 1E6)), total_pixels)
target_count = min(int((time.perf_counter() - current_frame.start_time) / (frame_parameters.pixel_time_us / 1E6)), total_pixels)
if (new_pixels := target_count - self.__scan_simulator.current_pixel_flat) > 0:
self.__scan_simulator._advance_pixel(new_pixels)

Expand Down
4 changes: 2 additions & 2 deletions nion/instrumentation/Acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3036,7 +3036,7 @@ def acquire(data_stream: DataStream, *, error_handler: typing.Optional[typing.Ca
last_progress = next_progress
last_progress_time = time.time()
assert time.time() - last_progress_time < TIMEOUT
time.sleep(0.05) # play nice with other threads
time.sleep(0.005) # play nice with other threads
if data_stream.is_finished:
assert data_stream.progress == 1.0
except Exception as e:
Expand Down Expand Up @@ -3110,7 +3110,7 @@ def wait_acquire(self, timeout: float = 60.0, *, on_periodic: typing.Callable[[]
start = time.time()
while self.__task and not self.__task.done() and time.time() - start < timeout:
on_periodic()
time.sleep(0.05) # don't take all the CPU
time.sleep(0.005) # don't take all the CPU
on_periodic() # one more periodic for clean up

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def get_dimensional_calibrations(self, readout_area: typing.Optional[Geometry.In
def get_frame_data(self, readout_area: Geometry.IntRect, binning_shape: Geometry.IntSize, exposure_s: float, scan_context: stem_controller.ScanContext, probe_position: typing.Optional[Geometry.FloatPoint]) -> DataAndMetadata.DataAndMetadata:
self.__data_value += 1
shape = self.__sensor_dimensions if self.__sensor_dimensions else readout_area.size
data = numpy.random.randn(shape.height // binning_shape.height, shape.width // binning_shape.width) * exposure_s
data = numpy.random.rand(shape.height // binning_shape.height, shape.width // binning_shape.width) * exposure_s
return DataAndMetadata.new_data_and_metadata(data)


Expand Down Expand Up @@ -211,7 +211,7 @@ def __init__(self) -> None:
self.eels_camera_device_id = "test_eels_camera"
self.instrument = InstrumentDevice.Instrument(self.instrument_id, ValueManager(), AxisManager())
self.scan_module = ScanModule(self.instrument, "test_scan_device", ScanDataGenerator())
self.ronchigram_camera_settings = CameraDevice.CameraSettings(self.ronchigram_camera_device_id)
self.eels_camera_settings = CameraDevice.CameraSettings(self.eels_camera_device_id)
self.ronchigram_camera_settings = CameraDevice.CameraSettings(self.ronchigram_camera_device_id, 0.005)
self.eels_camera_settings = CameraDevice.CameraSettings(self.eels_camera_device_id, 0.005)
self.ronchigram_camera_device = CameraDevice.Camera(self.ronchigram_camera_device_id, "ronchigram", "Ronchigram", CameraSimulator(None), self.instrument)
self.eels_camera_device = CameraDevice.Camera(self.eels_camera_device_id, "eels", "EELS", CameraSimulator(Geometry.IntSize(256, 1024)), self.instrument)
Loading
Loading