Skip to content

Support scanspec deserialization in plans with pydantic #118

Closed

Description

@callumforrester

With apischema we could have Spec[Movable] and custom converters to deserialize a scanspec while simultaneously looking up devices. This has proved hard with pydantic and has required several workarounds. See plans.py in https://github.com/DiamondLightSource/blueapi/pull/90/files

Activity

  1. added
    enhancement
    on Apr 14, 2023
  2. callumforrester commented on Apr 19, 2023

    @callumforrester
    ContributorAuthor

    Some background:
    The bluesky worker takes the parameters of a plan and internally creates a pydantic model for future validation e.g.

    def my_plan(a: int, b: str = "b") -> Plan
        ...
    
    # Internally becomes something like
    
    class MyPlanModel(BaseModel):
        a: int
        b: str = "b"

    That way, when the plan parameters are send in JSON form, they can be parsed and validated by pydantic and we don't have to worry. However, what happens when your plan doesn't take a simple dictionary, list or primitive? What happens if it takes, say, a detector as an argument?

    def my_plan(a: int, b: Readable) -> Plan:
        ...

    We don't want to pass the whole Ophyd object as JSON because it has state. We need to pass a reference to it that we know the worker understands. At the moment we pass its name according to the worker's BlueskyContext, so we want some intermediary process to perform that lookup when needed. Before pydantic we used apischema, which had an ideal feature for this called conversions.

    I have written a similar feature of blueapi to work with pydantic called type validators:
    source
    tests

    so that our dynamically generated pydantic model can look roughly like this:

    def my_plan(a: int, b: Readable) -> Plan:
       ...
    
    # Becomes
    
    class MyPlanModel(BaseModel):
       a: int
       b: Readable
    
       @validator("b")
       def valdiate_b(self, val: str) -> Readable:
           return ctx.find_device(val)

    Which is all well and good, but what about the Readable type being placed at various type levels? For example:

    def my_weird_plan(
        a: Readable, 
        b: List[Readable], 
        c: Dict[str, Readable], 
        d: List[List[Readable]], 
        e: List[Dict[str, Set[Readable]]]) -> Plan:
        ...

    The code also copes with all of the above, as seen in the tests. The only thing left to do is make it work with scanspecs.
    The following already works

    def my_plan(spec: Spec[str]) -> Plan:
        ...

    In the plan, you can access a scanspec object where the axes are strings, which is helpful, but ideally you want them to be motors, so we need

    def my_plan(spec: Spec[Movable]) -> Plan:
        ...

    This is the last remaining case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions