|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | +import pytest |
| 5 | + |
| 6 | +from platemapper.platemapper import PlateMapper |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def plate_mapper(): |
| 11 | + return PlateMapper(96) |
| 12 | + |
| 13 | + |
| 14 | +def test_plate_mapper_init_empty(): |
| 15 | + pm = PlateMapper() |
| 16 | + plate_map = pm.plate_map |
| 17 | + assert isinstance(plate_map, pd.DataFrame) |
| 18 | + assert pm.pivoted_plate_map is None |
| 19 | + assert pm.styled_plate_map is None |
| 20 | + assert len(plate_map) == 96 |
| 21 | + assert len(plate_map.columns) == 3 |
| 22 | + assert "row" in plate_map.columns |
| 23 | + assert "A" in plate_map["row"].values |
| 24 | + assert "column" in plate_map.columns |
| 25 | + assert 1 in plate_map["column"].values |
| 26 | + assert "well_id" in plate_map.columns |
| 27 | + assert "A1" in plate_map["well_id"].values |
| 28 | + |
| 29 | + |
| 30 | +def test_plate_mapper_init_with_plate_size(): |
| 31 | + pm = PlateMapper(384) |
| 32 | + plate_map = pm.plate_map |
| 33 | + assert len(plate_map) == 384 |
| 34 | + assert len(plate_map.columns) == 3 |
| 35 | + assert "P" in plate_map["row"].values # 16th letter |
| 36 | + assert 24 in plate_map["column"].values |
| 37 | + |
| 38 | + |
| 39 | +def test_plate_mapper_leading_zeroes(): |
| 40 | + pm = PlateMapper(leading_zeroes=True) |
| 41 | + assert "A" in pm.plate_map["row"].values |
| 42 | + assert "01" in pm.plate_map["column"].values |
| 43 | + assert "12" in pm.plate_map["column"].values |
| 44 | + assert "A01" in pm.plate_map["well_id"].values |
| 45 | + assert "H12" in pm.plate_map["well_id"].values |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def treatments(): |
| 50 | + return { |
| 51 | + "Treatment1": {"Condition1": ["A1", "B2"], "Condition2": ["C3"]}, |
| 52 | + "Treatment2": {"Condition3": ["D4:E5"]}, |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +def test_plate_mapper_init_with_treatments(treatments): |
| 57 | + pm = PlateMapper(96, treatments=treatments) |
| 58 | + plate_map = pm.plate_map |
| 59 | + pivoted_pm = pm.pivoted_plate_map |
| 60 | + |
| 61 | + assert isinstance(plate_map, pd.DataFrame) |
| 62 | + assert isinstance(pivoted_pm, pd.DataFrame) |
| 63 | + assert "Treatment1" in plate_map.columns |
| 64 | + assert "Treatment2" in plate_map.columns |
| 65 | + assert ( |
| 66 | + plate_map.loc[plate_map["well_id"] == "A1", "Treatment1"].values[0] |
| 67 | + == "Condition1" |
| 68 | + ) |
| 69 | + assert ( |
| 70 | + plate_map.loc[plate_map["well_id"] == "B2", "Treatment1"].values[0] |
| 71 | + == "Condition1" |
| 72 | + ) |
| 73 | + assert ( |
| 74 | + plate_map.loc[plate_map["well_id"] == "C3", "Treatment1"].values[0] |
| 75 | + == "Condition2" |
| 76 | + ) |
| 77 | + assert ( |
| 78 | + plate_map.loc[plate_map["well_id"] == "D4", "Treatment2"].values[0] |
| 79 | + == "Condition3" |
| 80 | + ) |
| 81 | + assert ( |
| 82 | + plate_map.loc[plate_map["well_id"] == "E5", "Treatment2"].values[0] |
| 83 | + == "Condition3" |
| 84 | + ) |
| 85 | + assert ( |
| 86 | + plate_map.loc[plate_map["well_id"] == "E4", "Treatment2"].values[0] |
| 87 | + == "Condition3" |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +def test_plate_mapper_init_with_treatments_and_leading_zeroes(treatments): |
| 92 | + pm = PlateMapper(96, treatments=treatments, leading_zeroes=True) |
| 93 | + plate_map = pm.plate_map |
| 94 | + |
| 95 | + assert plate_map.loc[plate_map["well_id"] == "A01", "Treatment1"].values[0] |
| 96 | + |
| 97 | + |
| 98 | +def test_plate_mapper_get_pivoted_plate_map( |
| 99 | + plate_mapper: PlateMapper, treatments: dict[str, dict[str, list[str]]] |
| 100 | +): |
| 101 | + plate_mapper.assign_treatments(treatments) |
| 102 | + plate_map_pivot = plate_mapper.get_pivoted_plate_map("Treatment1") |
| 103 | + |
| 104 | + assert isinstance(plate_map_pivot, pd.DataFrame) |
| 105 | + assert len(plate_map_pivot) == 8 |
| 106 | + assert len(plate_map_pivot.columns) == 12 |
| 107 | + assert "A" in plate_map_pivot.index |
| 108 | + assert "Condition1" in plate_map_pivot.values |
| 109 | + assert "Condition2" in plate_map_pivot.values |
| 110 | + |
| 111 | + |
| 112 | +def test_plate_mapper_get_styled_plate_map( |
| 113 | + plate_mapper: PlateMapper, treatments: dict[str, dict[str, list[str]]] |
| 114 | +): |
| 115 | + plate_mapper.assign_treatments(treatments) |
| 116 | + plate_map_styled = plate_mapper.get_styled_plate_map("Treatment1") |
| 117 | + |
| 118 | + assert isinstance(plate_map_styled, pd.io.formats.style.Styler) |
| 119 | + assert plate_map_styled.caption == "Treatment1 Plate Map" |
0 commit comments