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

1217 Use event from grid detect to do ispyb deposition #1279

Merged

Conversation

rtuck99
Copy link
Contributor

@rtuck99 rtuck99 commented Mar 22, 2024

Fixes #1217

Link to dodal PR (if required):N/A
(remember to update setup.cfg with the dodal commit tag if you need it for tests to pass!)

To test:

  1. Unit + System tests pass
  2. Grid scan details in ispyb and snapshots in syncweb should continue to populate

@rtuck99 rtuck99 requested review from DominicOram and d-perl March 22, 2024 15:57
@rtuck99 rtuck99 changed the base branch from main to 1218_final_tidy_of_the_great_ispyb_refactor March 22, 2024 15:58
@rtuck99 rtuck99 force-pushed the 1218_final_tidy_of_the_great_ispyb_refactor branch from 20d7c37 to 6a2a0a9 Compare March 22, 2024 16:20
@rtuck99 rtuck99 force-pushed the 1217_use_event_from_grid_detect_to_do_ispyb_deposition branch from af06d87 to de7235f Compare March 22, 2024 16:59
@rtuck99 rtuck99 force-pushed the 1218_final_tidy_of_the_great_ispyb_refactor branch from 6a2a0a9 to 37e8849 Compare April 2, 2024 08:56
@rtuck99 rtuck99 force-pushed the 1217_use_event_from_grid_detect_to_do_ispyb_deposition branch from de7235f to 7864007 Compare April 2, 2024 09:03
@rtuck99 rtuck99 force-pushed the 1218_final_tidy_of_the_great_ispyb_refactor branch from 37e8849 to 266070d Compare April 2, 2024 13:48
@rtuck99 rtuck99 force-pushed the 1217_use_event_from_grid_detect_to_do_ispyb_deposition branch from 7864007 to 831d8e8 Compare April 2, 2024 13:49
@rtuck99 rtuck99 force-pushed the 1218_final_tidy_of_the_great_ispyb_refactor branch from 266070d to 5b83260 Compare April 8, 2024 11:16
@rtuck99 rtuck99 force-pushed the 1217_use_event_from_grid_detect_to_do_ispyb_deposition branch from 928fd92 to 88ad151 Compare April 8, 2024 12:47
Base automatically changed from 1218_final_tidy_of_the_great_ispyb_refactor to main April 8, 2024 17:58
Copy link
Collaborator

@DominicOram DominicOram left a comment

Choose a reason for hiding this comment

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

Thanks! Partly reviewed but ran out of time to go as deep as I wanted, sorry.
Nonetheless, some comments in code. Also, I think with the merge of #1297 some of the code reduces down.

Comment on lines 45 to 57
def ispyb_activation_wrapper(parameters, plan_generator):
@bpp.run_decorator(
md={
"activate_callbacks": ["GridscanISPyBCallback"],
"subplan_name": CONST.PLAN.GRID_DETECT_AND_DO_GRIDSCAN,
"hyperion_internal_parameters": parameters.json(),
}
)
@wraps(plan_generator)
def wrapped():
return plan_generator

yield from wrapped()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should:

Suggested change
def ispyb_activation_wrapper(parameters, plan_generator):
@bpp.run_decorator(
md={
"activate_callbacks": ["GridscanISPyBCallback"],
"subplan_name": CONST.PLAN.GRID_DETECT_AND_DO_GRIDSCAN,
"hyperion_internal_parameters": parameters.json(),
}
)
@wraps(plan_generator)
def wrapped():
return plan_generator
yield from wrapped()
def ispyb_activation_wrapper(parameters, plan_generator):
return bpp.run_wrapper(
plan_generator,
md={
"activate_callbacks": ["GridscanISPyBCallback"],
"subplan_name": CONST.PLAN.GRID_DETECT_AND_DO_GRIDSCAN,
"hyperion_internal_parameters": parameters.json(),
},
)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I think you could then switch the parameters and plan_generator to more closely match the run_wrapper syntax

)
initial_collection_info = replace(
initial_collection_info,
data_collection_info = replace(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should: Is this now taking an empty data collection and recreating it from the existing event_sourced_data_collection_info? Why can't we just use the event_sourced_data_collection_info itself?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar in the ispyb_callback for xray centring.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, I should have spotted this!

Comment on lines 157 to 166
grid_scan_info = GridScanInfo(
upper_left_px=numpy.array(
[data["oav_snapshot_top_left_x"], data["oav_snapshot_top_left_y"]]
),
x_steps=data["oav_snapshot_num_boxes_x"],
y_steps=data["oav_snapshot_num_boxes_y"],
# convert pixels back to mm
x_step_size_mm=data["oav_snapshot_box_width"]
* self.params.hyperion_params.ispyb_params.microns_per_pixel_x
/ 1000,
y_step_size_mm=data["oav_snapshot_box_width"]
* self.params.hyperion_params.ispyb_params.microns_per_pixel_y
/ 1000,
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should: Is the only reason we need the grid scan info for constructing the comment? It seems messy to make this object here, use it once, then use the same parameters to make a DataCollectionGridInfo straight after. Could we change construct_comment_for_gridscan to take a DataCollectionGridInfo or to just take the parameters it needs then we can get rid of GridScanInfo?

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 will get rid of it

assert (
ispyb_ids.data_collection_group_id
), "Attempted to store scan data without a collection group"
assert (
ispyb_ids.data_collection_ids
), "Attempted to store scan data without a collection"
print(f"OBJECT PASSED IN IS {ispyb_ids}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should: I assume this is for debugging? Can you remove please? Same with other places in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doh

self.ispyb_ids, None, [scan_data_info]
)
self._oav_snapshot_event_idx += 1
pass
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should: What is this pass doing?

Comment on lines 107 to 105
case CONST.DESCRIPTORS.ISPYB_HARDWARE_READ:
self._handle_ispyb_hardware_read(doc)
case CONST.DESCRIPTORS.OAV_SNAPSHOT_TRIGGERED:
self._handle_oav_snapshot_triggered(doc)
case CONST.DESCRIPTORS.ISPYB_TRANSMISSION_FLUX_READ:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should: Why do each of these cases have different behavior w.r.t actually updating ispyb?

  • CONST.DESCRIPTORS.ISPYB_HARDWARE_READ - doesn't update ispyb at all
  • CONST.DESCRIPTORS.OAV_SNAPSHOT_TRIGGERED - Updates ispyb inside _handle_oav_snapshot_triggered
  • CONST.DESCRIPTORS.ISPYB_TRANSMISSION_FLUX_READ - Updates ispyb outside _handle_ispyb_transmission_flux_read

It seems to me that they should all update ispyb and they should do it in a consistent way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

historical reasons really, that's the original underlying behaviour that has always been there.

@rtuck99 rtuck99 force-pushed the 1217_use_event_from_grid_detect_to_do_ispyb_deposition branch from b5efe47 to 5dd5eb9 Compare April 11, 2024 13:19
@rtuck99 rtuck99 requested a review from DominicOram April 12, 2024 09:25
Copy link
Collaborator

@DominicOram DominicOram left a comment

Choose a reason for hiding this comment

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

Apologies, some more comments in code.

self._sample_barcode,
)
ISPYB_LOGGER.info(f"Recieved ISPYB IDs: {self.ispyb_ids}")
match event_descriptor.get("name"):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could: Now that these all call the update_deposition maybe it would be better to have each of these cases return the update and then call the update_deposition here?

Comment on lines 133 to 147
data_collection_info = DataCollectionInfo()
data_collection_info.xtal_snapshot1 = data.get(
"oav_snapshot_last_path_full_overlay"
)
data_collection_info.xtal_snapshot2 = data.get("oav_snapshot_last_path_outer")
data_collection_info.xtal_snapshot3 = data.get("oav_snapshot_last_saved_path")
data_collection_info.n_images = (
data["oav_snapshot_num_boxes_x"] * data["oav_snapshot_num_boxes_y"]
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: We could just do this in the instantiation like _handle_ispyb_hardware_read:

data_collection_info = DataCollectionInfo(
    xtal_snapshot1 = data.get("oav_snapshot_last_path_full_overlay"),
    ....
)

return "Hyperion rotation scan"
return
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should: This function is no longer used, can we remove?

from hyperion.external_interaction.ispyb.data_model import (
DataCollectionGridInfo,
DataCollectionInfo,
)
from hyperion.external_interaction.ispyb.ispyb_dataclass import Orientation


def populate_xz_data_collection_info(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should: Why does this need full_params?

detector_params,
) -> DataCollectionInfo:
assert (
detector_params.omega_start is not None
and detector_params.run_number is not None
and ispyb_params is not None
and full_params is not None
), "StoreGridscanInIspyb failed to get parameters"
omega_start = detector_params.omega_start + 90
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should: We should be able to read the omega from the smargon when we do the pin tip detection, can we make an issue for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have made #1327 but is pin tip detection the right place or should it be done during read_hardware_for_ispyb_pre_collection?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It can't be done during read_hardware_for_ispyb_pre_collection because we want the omega for each grid. If we read omega at pre_collection we only get the first grid's omega. We would then have to add the 90 deg in the callback again, which I was trying to avoid. Basically I want the logic of what we're doing to be in the plan, the callback should just be blindly funneling it into ispyb

ScanDataInfo(
data_collection_info=populate_remaining_data_collection_info(
None,
None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should: Shouldn't the dcgid here be the group made above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we haven't made it yet, we've only populated the DataCollectionGroupInfo but it hasn't been posted to the database so there's no id, leaving it as none will cause it to be populate during the call to begin_deposition

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, that seems a bit convoluted but I guess we can tidy up in #1293

grid_scan_info,
self.params,
self.params.hyperion_params.ispyb_params,
scan_data_infos = [
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should: Rather than do anything specific upfront could we get away with doing all the specific stuff in _handle_oav_snapshot_triggered? e.g. this could be something like:

number_of_collections = 2 if self.is_3d_gridscan() else 1

scan_data_infos = [ScanDataInfo(data_collection_info=populate_remaining_data_collection_info(
                            None,
                            None,
                            DataCollectionInfo(
                                axis_range=0,
                                axis_end=omega_start,
                            ),
                            self.params.hyperion_params.detector_params,
                            self.params.hyperion_params.ispyb_params,
                        ) for range in (number_of_collections)]

Then handle run_number and omega_start in _handle_oav_snapshot_triggered?

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 guess this is a wider question than for this one particular change but given that we are planning to move to ispyb-eye is it better to defer some of our internal changes until that point? At least until it's clearer how start / event documents might map to ispyb-eye calls? Unless you were proposing this with that already in mind.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure, happy to park this :)

rtuck99 added 9 commits April 23, 2024 09:15
* Move ispyb callback activation to before grid detect
* Fixup all the broken unit tests
* Separate descriptor constants from plan names
* Move callback activation wrapper to callback module
* Remove upsert_dc_grid calls from hardware read ispyb updates
* Add units to GridScanInfo fields
* Move grid comment updates from hardware read to oav snapshot event
* Fix system tests
* Change comment to be specified as a value instead of a factory
…ivity_start() and eager create both xy and xz DataCollection, as per PR feedback
* Remove unused methods and method parameters
* Extract common call to update_deposition() from ispyb_callback_base handler methods
* Tidy up initialisation of DataCollectionInfo
@rtuck99 rtuck99 requested a review from DominicOram April 23, 2024 11:17
Copy link
Collaborator

@DominicOram DominicOram left a comment

Choose a reason for hiding this comment

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

Great, thank you! I think this all looks a lot cleaner now

@DominicOram DominicOram merged commit d2c9c63 into main Apr 23, 2024
6 checks passed
@DominicOram DominicOram deleted the 1217_use_event_from_grid_detect_to_do_ispyb_deposition branch April 23, 2024 16:11
olliesilvester pushed a commit to olliesilvester/mx-bluesky that referenced this pull request Aug 23, 2024
…Source/1217_use_event_from_grid_detect_to_do_ispyb_deposition

1217 Use event from grid detect to do ispyb deposition
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.

Use event from grid detect to do ispyb deposition
2 participants