Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Initialize callbacks using params emitted in Bluesky documents#959

Merged
d-perl merged 19 commits into
mainfrom
950_init_callbacks_w_document
Nov 21, 2023
Merged

Initialize callbacks using params emitted in Bluesky documents#959
d-perl merged 19 commits into
mainfrom
950_init_callbacks_w_document

Conversation

@d-perl

@d-perl d-perl commented Nov 7, 2023

Copy link
Copy Markdown
Contributor

Fixes #950
Fixes #372

Callback classes now all get their params from the "hyperion_internal_params" emitted in the outer start document of each plan.

Adds an IspybIds to clean up some type checking issues

80% of line count is just tests being put into classes so I could mock stuff better

To test:

  1. Run tests

@d-perl d-perl changed the title 950 Initialize callbacks using params emitted in BlueSky documents 950 Initialize callbacks using params emitted in Bluesky documents Nov 7, 2023

@DominicOram DominicOram left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good start, thanks! I love pulling all the strings into constants. Some comments in code.

Maybe I'm being dumb but why can't we use a normal constructor instead of the setup method?

Comment thread src/hyperion/experiment_plans/flyscan_xray_centre_plan.py
self.params: GridscanInternalParameters | RotationInternalParameters | None = (
None
)
self.ispyb: StoreRotationInIspyb | Store3DGridscanInIspyb | Store2DGridscanInIspyb

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should: Don't we want this to be anything that inherits from StoreInIspyb?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

base StoreInIspyb doesn't have all the attributes, type checker will be mad

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Which attributes? The only errors I see when I set it to StoreInIspyb are the arguments/return types on begin_deposition, which is a legitimate issue in the StoreInIspyb class

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I fixed it properly :)

Comment thread src/hyperion/external_interaction/callbacks/ispyb_callback_base.py
Comment on lines +74 to +76
assert isinstance(self.params, GridscanInternalParameters) or isinstance(
self.params, RotationInternalParameters
), "ISPyB handler params set with wrong type"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should: I don't think this is very pythonic, we should just try and use it. Also, mypy should pick it up hopefully


def start(self, doc: dict):
if doc.get("subplan_name") == ROTATION_OUTER_PLAN:
self.run_uid = doc.get("uid")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should: run_uid is never used, remove it?

if self.params.experiment_params.is_3d_grid_scan
else Store2DGridscanInIspyb(self.ispyb_config, self.params)
)
self.run_start_uid = doc.get("uid")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should: run_start_uid is never used, can we remove it?

self.ispyb: StoreRotationInIspyb = StoreRotationInIspyb(
self.ispyb_config, self.params
)
self.ispyb_ids: tuple[int, int] | tuple[None, None] = (None, None)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should: Do we want to reset the ids for every start document we get? Why does the grid ispyb callback do this on init rather than on start? Feels like they should have the same behaviour

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good point, but they will diverge soon, when the rotation scan one should check if it should be reset at all

assert (
self.ispyb.params is not None
), "ISPyB handler attached to Zocalo handler did not recieve parameters"
zocalo_environment = self.ispyb.params.hyperion_params.zocalo_environment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should: Can we just get this out of the metadata rather than the ispyb object?

Comment on lines +59 to +64
self.zocalo_interactor = ZocaloInteractor(
self.ispyb.params.hyperion_params.zocalo_environment
)
self.grid_position_to_motor_position: Callable[
[ndarray], ndarray
] = self.ispyb.params.experiment_params.grid_position_to_motor_position

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should: I don't like that we have to go through the ispyb object to get these, it seems really tightly coupled. Can we have a chat about a better way?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't think it was a problem since we're pulling ispyb IDs from there anyway, but we can just get it from the parameters, that's fine

@codecov

codecov Bot commented Nov 9, 2023

Copy link
Copy Markdown

Codecov Report

Attention: 7 lines in your changes are missing coverage. Please review.

Comparison is base (1f9bec6) 93.71% compared to head (2e50c4f) 93.79%.
Report is 13 commits behind head on main.

Files Patch % Lines
...erion/experiment_plans/flyscan_xray_centre_plan.py 83.33% 2 Missing ⚠️
...ernal_interaction/callbacks/ispyb_callback_base.py 92.30% 2 Missing ⚠️
...l_interaction/callbacks/rotation/ispyb_callback.py 88.23% 2 Missing ⚠️
...al_interaction/callbacks/plan_reactive_callback.py 96.87% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #959      +/-   ##
==========================================
+ Coverage   93.71%   93.79%   +0.07%     
==========================================
  Files          56       57       +1     
  Lines        2673     2755      +82     
==========================================
+ Hits         2505     2584      +79     
- Misses        168      171       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@d-perl
d-perl requested a review from DominicOram November 10, 2023 11:49

@DominicOram DominicOram left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Some missed comments from before. Mostly there now.

Maybe I'm being dumb but why can't we use a normal constructor instead of the setup method?

Question still stands.

Comment on lines +316 to +319
{
"subplan_name": GRIDSCAN_OUTER_PLAN,
"hyperion_internal_parameters": test_fgs_params.json(),
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: Pull this into a variable and use below.

self.ispyb_ids: tuple = (None, None, None)

def start(self, doc: dict):
if doc.get("subplan_name") == "run_gridscan_move_and_tidy":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@dperl-dls still valid comment

Comment on lines +43 to +45
LOGGER.info(
"Nexus writer recieved start document with experiment parameters."
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@dperl-dls still valid comment

ispyb_config: str,
experiment_type: str,
parameters: GridscanInternalParameters = None,
parameters: GridscanInternalParameters | None = None,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should: When do we ever pass None into here in production? Do we need to allow it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good spot, I cleaned up a bunch of other ones in here too

@d-perl

d-perl commented Nov 14, 2023

Copy link
Copy Markdown
Contributor Author

why can't we use a normal constructor instead of the setup method?

The constructor of a dataclass requires values for the fields as arguments, but the zocalo callback needs to be instantiated with the ispyb handler as an argument; you can't set the fields of self anywhere (even in init) for a frozen dataclass; we need frozen=True, order=True to guarantee that casting it to a list gives the list in the right order

We will also shortly remove these callback collections completely

@DominicOram DominicOram left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Great, thank you! Sorry for such a long back and forth

@DominicOram DominicOram changed the title 950 Initialize callbacks using params emitted in Bluesky documents Initialize callbacks using params emitted in Bluesky documents Nov 20, 2023
@d-perl
d-perl merged commit c856911 into main Nov 21, 2023
@d-perl
d-perl deleted the 950_init_callbacks_w_document branch November 21, 2023 10:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

initialise ISPyB callback with params emitted from plan rather than passed in Improve ispyb deposition variable

2 participants