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

make device factory decorator #597

Open
wants to merge 67 commits into
base: main
Choose a base branch
from

Conversation

stan-dot
Copy link
Contributor

@stan-dot stan-dot commented Jun 5, 2024

Fixes #483

Instructions to reviewer on how to test:

  1. Do thing run i22 beamline
  2. Confirm lazy loading is possible

Checks for reviewer

  • Would the PR title make sense to a scientist on a set of release notes
  • If a new device has been added does it follow the standards
  • If changing the API for a pre-existing device, ensure that any beamlines using this device have updated their Bluesky plans accordingly
  • Have the connection tests for the relevant beamline(s) been run via dodal connect ${BEAMLINE}

@stan-dot stan-dot self-assigned this Jun 5, 2024
@stan-dot stan-dot requested a review from coretl June 5, 2024 16:22
@stan-dot stan-dot added documentation Improvements or additions to documentation enhancement New feature or request python Pull requests that update Python code labels Jun 5, 2024
@stan-dot stan-dot force-pushed the 483-make-device_factory-decorator-to-ease-making-and-connecting-ophyd-async-devices branch from 94124b4 to 27c1b79 Compare June 7, 2024 08:12
Copy link
Collaborator

@coretl coretl left a comment

Choose a reason for hiding this comment

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

No idea if the rest of the code is right until some tests are written...

src/dodal/beamlines/i22.py Outdated Show resolved Hide resolved
@coretl
Copy link
Collaborator

coretl commented Jun 7, 2024

To clarify, the code in the original ticket is an idea, but has never been run or tests. It is expected that it is wrong in some way, and writing tests to exercise it will reveal how it is wrong...

@stan-dot stan-dot changed the title Initial draft changes make device factory decorator Jun 10, 2024
@stan-dot stan-dot force-pushed the 483-make-device_factory-decorator-to-ease-making-and-connecting-ophyd-async-devices branch 3 times, most recently from f2ad915 to fb5e142 Compare June 12, 2024 14:29
@stan-dot stan-dot force-pushed the 483-make-device_factory-decorator-to-ease-making-and-connecting-ophyd-async-devices branch 4 times, most recently from 26368ca to 6cb1db6 Compare June 20, 2024 14:14
@stan-dot
Copy link
Contributor Author

stan-dot commented Jun 20, 2024

do we want to add 'skip device' to the factory too?

we could have just one decorator

@coretl
Copy link
Collaborator

coretl commented Jun 21, 2024

do we want to add 'skip device' to the factory too?

we could have just one decorator

This is a question for @callumforrester and @DominicOram. I don't know what a skipped device actually is? If we have a decorator here, then maybe we could "skip" it by omitting the decorator?

@DominicOram
Copy link
Contributor

skip was designed to let you provide logic on when to not instantiate a device. This is useful for:

  • Running the i03 devices against s03, where not all the devices are simulated
  • Running the i03 devices from a workstation, where we do not have PVA access to the beamline

@stan-dot
Copy link
Contributor Author

it's an architectural question. We can have all the logic that belongs to 'making device class instances' inside a bunch of nested decorators that need to be in right order. OR we can have 1 factory class that accepts a dictionary of features (lazy, mock, skip, directory provicder, post-create script) in one place.

@coretl
Copy link
Collaborator

coretl commented Jun 21, 2024

it's an architectural question. We can have all the logic that belongs to 'making device class instances' inside a bunch of nested decorators that need to be in right order. OR we can have 1 factory class that accepts a dictionary of features (lazy, mock, skip, directory provicder, post-create script) in one place.

I have a weak preference for a single decorator, but defer to @callumforrester and @DominicOram for a decision

@DominicOram
Copy link
Contributor

Single decorator works for me.

@stan-dot
Copy link
Contributor Author

ok I'll prepare a proof of concept for a single decorator device_factory with some (hopefully) sensible default args so that we can put the least props every time.

@stan-dot stan-dot force-pushed the 483-make-device_factory-decorator-to-ease-making-and-connecting-ophyd-async-devices branch from 6cb1db6 to 49e3bd2 Compare June 25, 2024 15:27
@stan-dot
Copy link
Contributor Author

@coretl please checkout the stuff in here

@stan-dot
Copy link
Contributor Author

stan-dot commented Jun 26, 2024

device_factory = cast(Callable[..., T], make_fake_device(device_factory))

beamline_utils.py 180

but that is inside ophyd, not sure how it works for ophyd-async

@stan-dot stan-dot force-pushed the 483-make-device_factory-decorator-to-ease-making-and-connecting-ophyd-async-devices branch from 0ecf386 to 656f918 Compare June 27, 2024 08:13
@stan-dot stan-dot force-pushed the 483-make-device_factory-decorator-to-ease-making-and-connecting-ophyd-async-devices branch from 00c641b to bc4cf6f Compare September 25, 2024 10:18
Comment on lines 84 to 92
try:
fake_with_ophyd_sim: object | None = kwargs.pop("fake_with_ophyd_sim")
assert isinstance(
fake_with_ophyd_sim, bool | None
), "fake_with_ophyd_sim must be a boolean"
mock = fake_with_ophyd_sim or mock
# the instantiation of the device
except KeyError:
pass
Copy link
Collaborator

Choose a reason for hiding this comment

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

@DominicOram are we thinking that device_factory will be used with ophyd v1 devices? If not, then we should delete this section

Copy link
Contributor

Choose a reason for hiding this comment

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

We will likely have ophyd v1 devices around for a couple more months. If I can mix and match old and new ways of instantiating devices in the same beamline file I'm happy to have device_factory just on the ophyd-async ones. If so I would like a test confirming that mixing them like this works as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had in mind supporting either there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

todo: make a test if it works with ophyd v1 devices

Copy link
Contributor Author

Choose a reason for hiding this comment

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

VGonio at i24 is one such device, testing with it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's not a complicated device, could be easily migrated to ophyd_async

Copy link
Contributor Author

Choose a reason for hiding this comment

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

only that, Eiger and some AreaDetector implementations and some helpers are using the old ophyd imports.

not sure how does the section indicated map to the difference between the two ophyd implementations

src/dodal/common/beamlines/device_factory.py Outdated Show resolved Hide resolved
src/dodal/common/beamlines/beamline_utils.py Outdated Show resolved Hide resolved
@stan-dot
Copy link
Contributor Author

I literally just added a docstring and the CI started failing. what is going on???

f224a17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request python Pull requests that update Python code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make device_factory decorator to ease making and connecting ophyd-async Devices
7 participants