|
| 1 | +"""Convert labware offset creation requests and stored elements between legacy and new.""" |
| 2 | + |
| 3 | +from opentrons_shared_data.robot.types import RobotType |
| 4 | +from opentrons_shared_data.deck.types import DeckDefinitionV5 |
| 5 | +from opentrons.types import DeckSlotName |
| 6 | +from .errors import ( |
| 7 | + OffsetLocationInvalidError, |
| 8 | + FixtureDoesNotExistError, |
| 9 | +) |
| 10 | +from .types import ( |
| 11 | + LabwareOffsetCreate, |
| 12 | + LegacyLabwareOffsetCreate, |
| 13 | + LabwareOffsetCreateInternal, |
| 14 | + LegacyLabwareOffsetLocation, |
| 15 | + LabwareOffsetLocationSequence, |
| 16 | + OnLabwareOffsetLocationSequenceComponent, |
| 17 | + OnAddressableAreaLocationSequenceComponent, |
| 18 | + OnModuleOffsetLocationSequenceComponent, |
| 19 | + ModuleModel, |
| 20 | +) |
| 21 | +from .resources import deck_configuration_provider |
| 22 | + |
| 23 | + |
| 24 | +def standardize_labware_offset_create( |
| 25 | + request: LabwareOffsetCreate | LegacyLabwareOffsetCreate, |
| 26 | + robot_type: RobotType, |
| 27 | + deck_definition: DeckDefinitionV5, |
| 28 | +) -> LabwareOffsetCreateInternal: |
| 29 | + """Turn a union of old and new labware offset create requests into a new one.""" |
| 30 | + location_sequence, legacy_location = _locations_for_create( |
| 31 | + request, robot_type, deck_definition |
| 32 | + ) |
| 33 | + return LabwareOffsetCreateInternal( |
| 34 | + definitionUri=request.definitionUri, |
| 35 | + locationSequence=location_sequence, |
| 36 | + legacyLocation=legacy_location, |
| 37 | + vector=request.vector, |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def _legacy_offset_location_to_offset_location_sequence( |
| 42 | + location: LegacyLabwareOffsetLocation, deck_definition: DeckDefinitionV5 |
| 43 | +) -> LabwareOffsetLocationSequence: |
| 44 | + sequence: LabwareOffsetLocationSequence = [] |
| 45 | + if location.definitionUri: |
| 46 | + sequence.append( |
| 47 | + OnLabwareOffsetLocationSequenceComponent(labwareUri=location.definitionUri) |
| 48 | + ) |
| 49 | + if location.moduleModel: |
| 50 | + sequence.append( |
| 51 | + OnModuleOffsetLocationSequenceComponent(moduleModel=location.moduleModel) |
| 52 | + ) |
| 53 | + cutout_id = deck_configuration_provider.get_cutout_id_by_deck_slot_name( |
| 54 | + location.slotName |
| 55 | + ) |
| 56 | + possible_cutout_fixture_id = location.moduleModel.value |
| 57 | + try: |
| 58 | + addressable_area = deck_configuration_provider.get_labware_hosting_addressable_area_name_for_cutout_and_cutout_fixture( |
| 59 | + cutout_id, possible_cutout_fixture_id, deck_definition |
| 60 | + ) |
| 61 | + sequence.append( |
| 62 | + OnAddressableAreaLocationSequenceComponent( |
| 63 | + addressableAreaName=addressable_area |
| 64 | + ) |
| 65 | + ) |
| 66 | + except FixtureDoesNotExistError: |
| 67 | + # this is an OT-2 (or this module isn't supported in the deck definition) and we should use a |
| 68 | + # slot addressable area name |
| 69 | + sequence.append( |
| 70 | + OnAddressableAreaLocationSequenceComponent( |
| 71 | + addressableAreaName=location.slotName.value |
| 72 | + ) |
| 73 | + ) |
| 74 | + |
| 75 | + else: |
| 76 | + # Slight hack: we should have a more formal association here. However, since the slot |
| 77 | + # name is already standardized, and since the addressable areas for slots are just the |
| 78 | + # name of the slots, we can rely on this. |
| 79 | + sequence.append( |
| 80 | + OnAddressableAreaLocationSequenceComponent( |
| 81 | + addressableAreaName=location.slotName.value |
| 82 | + ) |
| 83 | + ) |
| 84 | + return sequence |
| 85 | + |
| 86 | + |
| 87 | +def _offset_location_sequence_to_legacy_offset_location( |
| 88 | + location_sequence: LabwareOffsetLocationSequence, deck_definition: DeckDefinitionV5 |
| 89 | +) -> LegacyLabwareOffsetLocation: |
| 90 | + if len(location_sequence) == 0: |
| 91 | + raise OffsetLocationInvalidError( |
| 92 | + "Offset locations must contain at least one component." |
| 93 | + ) |
| 94 | + last_element = location_sequence[-1] |
| 95 | + if not isinstance(last_element, OnAddressableAreaLocationSequenceComponent): |
| 96 | + raise OffsetLocationInvalidError( |
| 97 | + "Offset locations must end with an addressable area." |
| 98 | + ) |
| 99 | + labware_uri: str | None = None |
| 100 | + module_model: ModuleModel | None = None |
| 101 | + for location in location_sequence[:-1]: |
| 102 | + if isinstance(location, OnAddressableAreaLocationSequenceComponent): |
| 103 | + raise OffsetLocationInvalidError( |
| 104 | + "Addressable areas may only be the final element of an offset location." |
| 105 | + ) |
| 106 | + elif isinstance(location, OnLabwareOffsetLocationSequenceComponent): |
| 107 | + if labware_uri is not None: |
| 108 | + # We only take the first location |
| 109 | + continue |
| 110 | + if module_model is not None: |
| 111 | + # Labware can't be underneath modules |
| 112 | + raise OffsetLocationInvalidError( |
| 113 | + "Labware must not be underneath a module." |
| 114 | + ) |
| 115 | + labware_uri = location.labwareUri |
| 116 | + elif isinstance(location, OnModuleOffsetLocationSequenceComponent): |
| 117 | + if module_model is not None: |
| 118 | + # Bad, somebody put more than one module in here |
| 119 | + raise OffsetLocationInvalidError( |
| 120 | + "Only one module location may exist in an offset location." |
| 121 | + ) |
| 122 | + module_model = location.moduleModel |
| 123 | + else: |
| 124 | + raise OffsetLocationInvalidError( |
| 125 | + f"Invalid location component in offset location: {repr(location)}" |
| 126 | + ) |
| 127 | + ( |
| 128 | + cutout_id, |
| 129 | + cutout_fixtures, |
| 130 | + ) = deck_configuration_provider.get_potential_cutout_fixtures( |
| 131 | + last_element.addressableAreaName, deck_definition |
| 132 | + ) |
| 133 | + slot_name = deck_configuration_provider.get_deck_slot_for_cutout_id(cutout_id) |
| 134 | + return LegacyLabwareOffsetLocation( |
| 135 | + slotName=slot_name, moduleModel=module_model, definitionUri=labware_uri |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +def _locations_for_create( |
| 140 | + request: LabwareOffsetCreate | LegacyLabwareOffsetCreate, |
| 141 | + robot_type: RobotType, |
| 142 | + deck_definition: DeckDefinitionV5, |
| 143 | +) -> tuple[LabwareOffsetLocationSequence, LegacyLabwareOffsetLocation]: |
| 144 | + if isinstance(request, LabwareOffsetCreate): |
| 145 | + return ( |
| 146 | + request.locationSequence, |
| 147 | + _offset_location_sequence_to_legacy_offset_location( |
| 148 | + request.locationSequence, deck_definition |
| 149 | + ), |
| 150 | + ) |
| 151 | + else: |
| 152 | + normalized = request.location.model_copy( |
| 153 | + update={ |
| 154 | + "slotName": request.location.slotName.to_equivalent_for_robot_type( |
| 155 | + robot_type |
| 156 | + ) |
| 157 | + } |
| 158 | + ) |
| 159 | + return ( |
| 160 | + _legacy_offset_location_to_offset_location_sequence( |
| 161 | + normalized, deck_definition |
| 162 | + ), |
| 163 | + normalized, |
| 164 | + ) |
0 commit comments