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

129 edge detection #143

Merged
merged 33 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c66ac65
Add edge detection to dodal
Jul 28, 2023
79c5803
refactor to use numpy for most operations
Aug 8, 2023
0fe133c
Minor tidying/commenting
Aug 8, 2023
d354195
pre v2 refactor
Aug 9, 2023
096e428
Begin ophyd v2 refactor
Aug 9, 2023
df955ca
HACK (but it can get pva data)
Aug 9, 2023
0f47def
Refactor to use Ophyd v2
Aug 11, 2023
9b5fed2
Tidy ups
Aug 11, 2023
3d04cde
refactoring
Aug 11, 2023
8fd023c
Minor refactor
Aug 14, 2023
58493d6
Placate linters
Aug 14, 2023
9cb32c2
Merge remote-tracking branch 'origin/main' into allow_both_v1_and_v2_…
Aug 14, 2023
aa39ed6
Initial test with v1/v2 ophyd devices
Aug 15, 2023
3c26bd4
Refactor
Aug 15, 2023
498ce53
Correct type hints
Aug 15, 2023
e974644
Merge remote-tracking branch 'origin/main' into 129_edge_detection
Aug 15, 2023
d752192
Merge branch '129_edge_detection' into allow_both_v1_and_v2_ophyd_dev…
Aug 15, 2023
08ac04b
Remove unused import
Aug 16, 2023
e782d4d
Make parameters settable externally
Aug 16, 2023
cd88112
Add further unit tests
Aug 16, 2023
732a194
Placate linters
Aug 16, 2023
10f1fb2
Fix first batch of review comments
Aug 18, 2023
ee7c732
Add enum
Aug 18, 2023
2d8f2de
Address review comments
Aug 18, 2023
67a119e
Linters
Aug 18, 2023
72a6b62
Linters, again.
Aug 18, 2023
11475dc
Support older versions of python
Aug 18, 2023
d400d81
Fix test
Aug 18, 2023
be93998
use f-string
Aug 18, 2023
927f3de
Add requested test for passing through connection timeouts.
Aug 21, 2023
bfeac35
Address further review comments
Aug 21, 2023
6552557
Address straggling review comments
Aug 22, 2023
be318f9
Start runengine to prevent test flakiness
Aug 22, 2023
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
10 changes: 2 additions & 8 deletions src/dodal/beamlines/i23.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from dodal.beamlines.beamline_utils import device_instantiation
from dodal.beamlines.beamline_utils import set_beamline as set_utils_beamline
from dodal.devices.i23.gonio import Gonio
from dodal.devices.oav.pin_image_recognition import PinTipDetection

Check warning on line 4 in src/dodal/beamlines/i23.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/beamlines/i23.py#L4

Added line #L4 was not covered by tests
from dodal.log import LOGGER
from dodal.log import set_beamline as set_log_beamline
from dodal.utils import get_beamline_name
from dodal.utils import get_beamline_name, get_hostname, skip_device

Check warning on line 6 in src/dodal/beamlines/i23.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/beamlines/i23.py#L6

Added line #L6 was not covered by tests

BL = get_beamline_name("i23")
set_log_beamline(BL)
Expand All @@ -22,18 +21,13 @@
)


@skip_device(lambda: not get_hostname().startswith("i23-ws"))
Tom-Willemsen marked this conversation as resolved.
Show resolved Hide resolved
def oav_pin_tip_detection(

Check warning on line 25 in src/dodal/beamlines/i23.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/beamlines/i23.py#L24-L25

Added lines #L24 - L25 were not covered by tests
wait_for_connection: bool = True, fake_with_ophyd_sim: bool = False
) -> PinTipDetection:
"""Get the i23 OAV pin-tip detection device"""

if get_beamline_name("") != "i23":
LOGGER.warning(
"Not running on i23 - forcing pin tip detection into simulation mode"
)
fake_with_ophyd_sim = True

return device_instantiation(

Check warning on line 30 in src/dodal/beamlines/i23.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/beamlines/i23.py#L30

Added line #L30 was not covered by tests
PinTipDetection,
"pin_tip_detection",
"-DI-OAV-01:",
Expand Down
5 changes: 3 additions & 2 deletions src/dodal/devices/oav/pin_image_recognition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@

Used for pin tip centring workflow.

Note that if the sample is off-screen, this class will return the centre as the "edge"
of the image.
Note that if the tip of the sample is off-screen, this class will return the centre as the "edge"
of the image. If the entire sample if off-screen (i.e. no suitable edges were detected at all)
then it will return (None, None).
"""

def __init__(self, prefix: str, name: str = ""):
Expand Down Expand Up @@ -176,7 +177,7 @@
)

async def describe(self) -> dict[str, Descriptor]:
return OrderedDict(

Check warning on line 180 in src/dodal/devices/oav/pin_image_recognition/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/devices/oav/pin_image_recognition/__init__.py#L180

Added line #L180 was not covered by tests
[
(
self._name,
Expand Down
1 change: 1 addition & 0 deletions src/dodal/devices/oav/pin_image_recognition/manual_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@

It is otherwise unused.
Tom-Willemsen marked this conversation as resolved.
Show resolved Hide resolved
"""
# pragma: no cover
import asyncio

Check warning on line 9 in src/dodal/devices/oav/pin_image_recognition/manual_test.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/devices/oav/pin_image_recognition/manual_test.py#L9

Added line #L9 was not covered by tests

from dodal.devices.oav.pin_image_recognition import PinTipDetection

Check warning on line 11 in src/dodal/devices/oav/pin_image_recognition/manual_test.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/devices/oav/pin_image_recognition/manual_test.py#L11

Added line #L11 was not covered by tests

if __name__ == "__main__":
x = PinTipDetection(prefix="BL03I-DI-OAV-01:", name="edgeDetect")

Check warning on line 14 in src/dodal/devices/oav/pin_image_recognition/manual_test.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/devices/oav/pin_image_recognition/manual_test.py#L13-L14

Added lines #L13 - L14 were not covered by tests

async def acquire():
await x.connect()
img = await x.array_data.read()
tip = await x.read()
return img, tip

Check warning on line 20 in src/dodal/devices/oav/pin_image_recognition/manual_test.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/devices/oav/pin_image_recognition/manual_test.py#L16-L20

Added lines #L16 - L20 were not covered by tests

img, tip = asyncio.get_event_loop().run_until_complete(

Check warning on line 22 in src/dodal/devices/oav/pin_image_recognition/manual_test.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/devices/oav/pin_image_recognition/manual_test.py#L22

Added line #L22 was not covered by tests
asyncio.wait_for(acquire(), timeout=10)
)
print(tip)
print("Tip: {}".format(tip["edgeDetect"]["value"]))

Check warning on line 26 in src/dodal/devices/oav/pin_image_recognition/manual_test.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/devices/oav/pin_image_recognition/manual_test.py#L25-L26

Added lines #L25 - L26 were not covered by tests

try:
import matplotlib.pyplot as plt

Check warning on line 29 in src/dodal/devices/oav/pin_image_recognition/manual_test.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/devices/oav/pin_image_recognition/manual_test.py#L28-L29

Added lines #L28 - L29 were not covered by tests

plt.imshow(img[""]["value"].reshape(768, 1024, 3))
plt.show()
except ImportError:
print("matplotlib not available; cannot show acquired image")

Check warning on line 34 in src/dodal/devices/oav/pin_image_recognition/manual_test.py

View check run for this annotation

Codecov / codecov/patch

src/dodal/devices/oav/pin_image_recognition/manual_test.py#L31-L34

Added lines #L31 - L34 were not covered by tests
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch

import numpy as np
import pytest
Expand Down Expand Up @@ -54,6 +54,9 @@ async def test_numeric_soft_parameters_can_be_changed():
await device.close_iterations.set(20)
await device.min_tip_height.set(25)
await device.scan_direction.set(-1)
await device.preprocess.set(2)
await device.preprocess_ksize.set(3)
await device.preprocess_iterations.set(4)

assert await device.timeout.get_value() == 100.0
assert await device.canny_lower.get_value() == 5
Expand All @@ -62,6 +65,31 @@ async def test_numeric_soft_parameters_can_be_changed():
assert await device.close_iterations.get_value() == 20
assert await device.min_tip_height.get_value() == 25
assert await device.scan_direction.get_value() == -1
assert await device.preprocess.get_value() == 2
assert await device.preprocess_ksize.get_value() == 3
assert await device.preprocess_iterations.get_value() == 4


@pytest.mark.asyncio
async def test_invalid_processing_func_uses_identity_function():
device = await _get_pin_tip_detection_device()

set_sim_value(device.preprocess, 50) # Invalid index

with patch.object(
MxSampleDetect, "__init__", return_value=None
) as mock_init, patch.object(
MxSampleDetect, "processArray", return_value=((None, None), None)
):
await device.read()

mock_init.assert_called_once()

captured_func = mock_init.call_args[1]["preprocess"]

# Assert captured preprocess function is the identitiy function
arg = object()
assert arg == captured_func(arg)


@pytest.mark.parametrize(
Expand Down
Loading