|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from pydantic import BaseModel, ValidationError |
| 5 | +from workflows.recipe import RecipeWrapper, wrap_subscribe |
| 6 | + |
| 7 | +from cryoemservices.services.common_service import CommonService |
| 8 | +from cryoemservices.util.models import MockRW |
| 9 | + |
| 10 | + |
| 11 | +class AlignImagesParameters(BaseModel): |
| 12 | + id_ref: int # ISPyB Atlas atlasId |
| 13 | + image_ref: Path |
| 14 | + pixel_size_ref: float |
| 15 | + id_mov: int # ISPyB Atlas atlasId |
| 16 | + image_mov: Path |
| 17 | + pixel_size_mov: float |
| 18 | + |
| 19 | + |
| 20 | +class AlignImagesService(CommonService): |
| 21 | + """ |
| 22 | + A CryoEM service to align to images to one another |
| 23 | + """ |
| 24 | + |
| 25 | + _logger_name = __name__ |
| 26 | + |
| 27 | + def initializing(self): |
| 28 | + """Subscribe to a queue. Received messages must be acknowledged.""" |
| 29 | + self.log.info("Correlative image alignment service starting") |
| 30 | + # Subscribe service to RMQ queue |
| 31 | + wrap_subscribe( |
| 32 | + self._transport, |
| 33 | + self._environment["queue"] or "correlative.align_images", |
| 34 | + self.call_align_images, |
| 35 | + acknowledgement=True, |
| 36 | + allow_non_recipe_messages=True, |
| 37 | + ) |
| 38 | + |
| 39 | + def call_align_images( |
| 40 | + self, |
| 41 | + rw: RecipeWrapper | None, |
| 42 | + header: dict[str, Any], |
| 43 | + message: dict[str, Any] | None, |
| 44 | + ): |
| 45 | + """Pass incoming message to the relevant plugin function.""" |
| 46 | + # Encase message in ReceipeWrapper if none was provided |
| 47 | + if not rw: |
| 48 | + self.log.info("Received a simple message") |
| 49 | + if not isinstance(message, dict): |
| 50 | + self.log.error("Rejected invalid simple message") |
| 51 | + self._reject_message(header, requeue=False) |
| 52 | + return |
| 53 | + # Create a wrapper-like object to be passed to functions |
| 54 | + rw = MockRW(self._transport) |
| 55 | + rw.recipe_step = {"paramters": message} |
| 56 | + |
| 57 | + try: |
| 58 | + if isinstance(message, dict): |
| 59 | + params = AlignImagesParameters( |
| 60 | + **{**rw.recipe_step.get("parameters", {}), **message} |
| 61 | + ) |
| 62 | + else: |
| 63 | + params = AlignImagesParameters( |
| 64 | + **{**rw.recipe_step.get("parameters", {})} |
| 65 | + ) |
| 66 | + except (ValidationError, TypeError) as e: |
| 67 | + self.log.error( |
| 68 | + f"AlignImagesParameters validation failed for message: {message} " |
| 69 | + f"and recipe parameters: {rw.recipe_step.get('parameters', {})} " |
| 70 | + f"with exception: {e}" |
| 71 | + ) |
| 72 | + self._reject_message(header, transport=rw.transport, requeue=False) |
| 73 | + return |
| 74 | + |
| 75 | + # Acknowledge receipt of parameters |
| 76 | + self.log.info( |
| 77 | + "Running image alignment with the following parameters:\n" |
| 78 | + f"{params.model_dump(mode='json')}" |
| 79 | + ) |
| 80 | + |
| 81 | + ############################################################################### |
| 82 | + # Image alignment logic goes here |
| 83 | + ############################################################################### |
| 84 | + |
| 85 | + # Ack message after completion |
| 86 | + rw.transport.ack(header) |
| 87 | + return |
0 commit comments