diff --git a/src/openflexure_microscope_server/scan_planners.py b/src/openflexure_microscope_server/scan_planners.py index b30cacc7..065b3258 100644 --- a/src/openflexure_microscope_server/scan_planners.py +++ b/src/openflexure_microscope_server/scan_planners.py @@ -33,7 +33,7 @@ def enforce_xy_tuple(value: XYPos) -> XYPos: :raises ValueError: if the input cannot be coerced to a tuple of length 2. """ if not isinstance(value, (list, tuple)): - raise ValueError("2 value tuple expected") + raise TypeError("2 value tuple expected") if not len(value) == 2: raise ValueError("2 value tuple expected") if isinstance(value, list): @@ -49,7 +49,7 @@ def enforce_xyz_tuple(value: XYZPos) -> XYZPos: :raises ValueError: if the input cannot be coerced to a tuple of length 3. """ if not isinstance(value, (list, tuple)): - raise ValueError("3 value tuple expected") + raise TypeError("3 value tuple expected") if not len(value) == 3: raise ValueError("3 value tuple expected") if isinstance(value, list): diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index dc864e11..d5058fe8 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -61,7 +61,7 @@ def _get_scans_dir(config: dict) -> Optional[str]: try: return config["things"]["/smart_scan/"]["kwargs"]["scans_folder"] except KeyError as e: - msg = "Configuration error smart scan should have scans_folder kwarg set" + msg = "Configuration error: smart scan should have scans_folder kwarg set" raise RuntimeError(msg) from e return None diff --git a/src/openflexure_microscope_server/things/autofocus.py b/src/openflexure_microscope_server/things/autofocus.py index 0e11d456..a6c0e92a 100644 --- a/src/openflexure_microscope_server/things/autofocus.py +++ b/src/openflexure_microscope_server/things/autofocus.py @@ -26,6 +26,10 @@ from .camera import CameraDependency as CameraClient from .stage import StageDependency as Stage +class NotStreamingError(RuntimeError): + """No images captured from stream. The camera is almost certainly not streaming.""" + + class StackParams: """A class for holding for stack parameters, and returning computed ones.""" @@ -310,8 +314,9 @@ class JPEGSharpnessMonitor: """Return the z position of the sharpest image on a given move.""" _, jpeg_heights, jpeg_sizes = self.move_data(data_index) if len(jpeg_sizes) == 0: - raise ValueError( - "No images were captured during the move of the stage. Perhaps the camera is not streaming images?" + raise NotStreamingError( + "No images were captured during the move of the stage. " + "Perhaps the camera is not streaming images?" ) return jpeg_heights[np.argmax(jpeg_sizes)] diff --git a/tests/test_scan_planners.py b/tests/test_scan_planners.py index 7a9344f9..0b1adca2 100644 --- a/tests/test_scan_planners.py +++ b/tests/test_scan_planners.py @@ -14,11 +14,15 @@ from .utilities import scan_test_helpers def test_enforce_xy_tuple(): """Check that 2 value tuples (or ValueErrors) are always returned.""" bad_len_vals = [[1], [], (1,), (2, 4, 4), [1, 4, 5, 7]] - bad_type_vals = ["hi", 1, {"this": "that"}, {1, 2}] - for value in bad_len_vals + bad_type_vals: + for value in bad_len_vals: with pytest.raises(ValueError): scan_planners.enforce_xy_tuple(value) + bad_type_vals = ["hi", 1, {"this": "that"}, {1, 2}] + for value in bad_type_vals: + with pytest.raises(TypeError): + scan_planners.enforce_xy_tuple(value) + assert (1, 6) == scan_planners.enforce_xy_tuple((1, 6)) assert (1, 6) == scan_planners.enforce_xy_tuple([1, 6]) @@ -26,11 +30,15 @@ def test_enforce_xy_tuple(): def test_enforce_xyz_tuple(): """Check that 3 value tuples (or ValueErrors) are always returned.""" bad_len_vals = [[1], [], (1,), (2, 4), [1, 4, 5, 7]] - bad_type_vals = ["hi!", 1, {"this": "that"}, {1, 2, 3}] - for value in bad_len_vals + bad_type_vals: + for value in bad_len_vals: with pytest.raises(ValueError): scan_planners.enforce_xyz_tuple(value) + bad_type_vals = ["hi!", 1, {"this": "that"}, {1, 2, 3}] + for value in bad_type_vals: + with pytest.raises(TypeError): + scan_planners.enforce_xyz_tuple(value) + assert (1, 6, 2) == scan_planners.enforce_xyz_tuple((1, 6, 2)) assert (1, 6, 6) == scan_planners.enforce_xyz_tuple([1, 6, 6])