|
| 1 | +import warnings |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | +from robotools import Labware, Trough |
| 7 | + |
| 8 | + |
| 9 | +def assert_labware_equal(original: Labware, restored: Labware): |
| 10 | + assert isinstance(restored, Labware) |
| 11 | + assert len(restored.history) == len(original.history) |
| 12 | + # compare history entries |
| 13 | + for (olabel, ovols), (rlabel, rvols) in zip(original.history, restored.history, strict=True): |
| 14 | + assert rlabel == olabel |
| 15 | + np.testing.assert_array_equal(ovols, rvols) |
| 16 | + # compare composition of all wells |
| 17 | + for w in original.wells.flatten(): |
| 18 | + assert original.get_well_composition(w) == restored.get_well_composition(w) |
| 19 | + return |
| 20 | + |
| 21 | + |
| 22 | +def test_labware_to_dict_from_dict(): |
| 23 | + # create a labware and change it's state |
| 24 | + mtp = Labware( |
| 25 | + "emtepe", |
| 26 | + 2, |
| 27 | + 3, |
| 28 | + min_volume=30, |
| 29 | + max_volume=240, |
| 30 | + initial_volumes=50, |
| 31 | + ) |
| 32 | + mtp.add("B02", 50, label="add toxic stuff", compositions=[{"toxin": 0.5, "water": 0.5}]) |
| 33 | + |
| 34 | + # encode and recreate |
| 35 | + mtp_dict = mtp.to_dict() |
| 36 | + # the dict must not contain numpy arrays |
| 37 | + # that's why we can equals compare it here. |
| 38 | + assert mtp_dict == { |
| 39 | + "name": "emtepe", |
| 40 | + "rows": 2, |
| 41 | + "columns": 3, |
| 42 | + "min_volume": 30, |
| 43 | + "max_volume": 240, |
| 44 | + "volumes": [[50.0, 50.0, 50.0], [50.0, 100.0, 50.0]], |
| 45 | + "history": [[[50.0, 50.0, 50.0], [50.0, 50.0, 50.0]], [[50.0, 50.0, 50.0], [50.0, 100.0, 50.0]]], |
| 46 | + "labels": ["initial", "add toxic stuff"], |
| 47 | + "composition": { |
| 48 | + "emtepe.A01": [[1.0, 0.0, 0.0], [0.0, 0.0, 0.0]], |
| 49 | + "emtepe.A02": [[0.0, 1.0, 0.0], [0.0, 0.0, 0.0]], |
| 50 | + "emtepe.A03": [[0.0, 0.0, 1.0], [0.0, 0.0, 0.0]], |
| 51 | + "emtepe.B01": [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]], |
| 52 | + "emtepe.B02": [[0.0, 0.0, 0.0], [0.0, 0.5, 0.0]], |
| 53 | + "emtepe.B03": [[0.0, 0.0, 0.0], [0.0, 0.0, 1.0]], |
| 54 | + "toxin": [[0.0, 0.0, 0.0], [0.0, 0.25, 0.0]], |
| 55 | + "water": [[0.0, 0.0, 0.0], [0.0, 0.25, 0.0]], |
| 56 | + }, |
| 57 | + "is_trough": False, |
| 58 | + "virtual_rows": None, |
| 59 | + } |
| 60 | + |
| 61 | + restored = Labware.from_dict(mtp_dict) |
| 62 | + |
| 63 | + assert_labware_equal(mtp, restored) |
| 64 | + assert isinstance(mtp, Labware) |
| 65 | + pass |
| 66 | + |
| 67 | + |
| 68 | +def test_trough_to_dict_from_dict(): |
| 69 | + # create a labware and change it's state |
| 70 | + orig = Trough( |
| 71 | + "buffers", |
| 72 | + 8, |
| 73 | + 2, |
| 74 | + min_volume=30_000, |
| 75 | + max_volume=200_000, |
| 76 | + initial_volumes=[100_000, 50_000], |
| 77 | + column_names=["left", "right"], |
| 78 | + ) |
| 79 | + orig.add("A01", 50, label="add toxic stuff", compositions=[{"toxin": 0.5, "water": 0.5}]) |
| 80 | + |
| 81 | + # encode and recreate |
| 82 | + assert orig.is_trough |
| 83 | + data = orig.to_dict() |
| 84 | + assert data["is_trough"] is True |
| 85 | + restored = Trough.from_dict(data) |
| 86 | + assert isinstance(restored, Trough) |
| 87 | + |
| 88 | + assert_labware_equal(orig, restored) |
| 89 | + pass |
| 90 | + |
| 91 | + |
| 92 | +def test_labware_from_dict_can_return_troughs(): |
| 93 | + # create a labware and change it's state |
| 94 | + orig = Trough( |
| 95 | + "buffers", |
| 96 | + 8, |
| 97 | + 2, |
| 98 | + min_volume=30_000, |
| 99 | + max_volume=200_000, |
| 100 | + initial_volumes=[100_000, 50_000], |
| 101 | + column_names=["left", "right"], |
| 102 | + ) |
| 103 | + orig.add("A01", 50, label="add toxic stuff", compositions=[{"toxin": 0.5, "water": 0.5}]) |
| 104 | + |
| 105 | + # encode and recreate |
| 106 | + assert orig.is_trough |
| 107 | + data = orig.to_dict() |
| 108 | + assert data["is_trough"] is True |
| 109 | + with pytest.warns(UserWarning, match="downcasting"): |
| 110 | + restored = Labware.from_dict(data) |
| 111 | + assert isinstance(restored, Trough) |
| 112 | + |
| 113 | + assert_labware_equal(orig, restored) |
| 114 | + pass |
| 115 | + |
| 116 | + |
| 117 | +def test_from_dict_warns_about_downcasting_trough(): |
| 118 | + class MyTrough(Trough): |
| 119 | + pass |
| 120 | + |
| 121 | + # create a labware and change it's state |
| 122 | + orig = MyTrough( |
| 123 | + "buffers", |
| 124 | + 8, |
| 125 | + 2, |
| 126 | + min_volume=30_000, |
| 127 | + max_volume=200_000, |
| 128 | + initial_volumes=[100_000, 50_000], |
| 129 | + column_names=["left", "right"], |
| 130 | + ) |
| 131 | + orig.add("A01", 50, label="add toxic stuff", compositions=[{"toxin": 0.5, "water": 0.5}]) |
| 132 | + |
| 133 | + # encode and recreate |
| 134 | + assert orig.is_trough |
| 135 | + data = orig.to_dict() |
| 136 | + assert data["is_trough"] is True |
| 137 | + |
| 138 | + # robotools doesn't know about MyTrough, so we'll get a Trough with a warning |
| 139 | + with pytest.warns(UserWarning, match="downcasting"): |
| 140 | + restored = Labware.from_dict(data) |
| 141 | + # instantiation worked, but the type was downcasted |
| 142 | + assert isinstance(restored, Trough) |
| 143 | + # we can avoid downcasting by passing the trough type explicitly |
| 144 | + with warnings.catch_warnings(): |
| 145 | + warnings.simplefilter("error") |
| 146 | + restored2 = Labware.from_dict(data, cls_trough=MyTrough) |
| 147 | + assert isinstance(restored2, MyTrough) |
| 148 | + # or by calling through the custom type |
| 149 | + with warnings.catch_warnings(): |
| 150 | + warnings.simplefilter("error") |
| 151 | + restored3 = MyTrough.from_dict(data) |
| 152 | + assert isinstance(restored3, MyTrough) |
| 153 | + pass |
0 commit comments