|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +from sqlmodel import Session, select |
| 4 | + |
| 5 | +from murfey.util.db import ( |
| 6 | + DataCollection, |
| 7 | + DataCollectionGroup, |
| 8 | + ParticleSizes, |
| 9 | + ProcessingJob, |
| 10 | + TomogramPicks, |
| 11 | + TomographyProcessingParameters, |
| 12 | +) |
| 13 | +from murfey.workflows.tomo import picking |
| 14 | +from tests.conftest import ExampleVisit, get_or_create_db_entry |
| 15 | + |
| 16 | + |
| 17 | +@mock.patch("murfey.workflows.tomo.picking._pj_id_tomo_classification") |
| 18 | +def test_picked_tomogram_not_run_class2d( |
| 19 | + mock_pjid, murfey_db_session: Session, tmp_path |
| 20 | +): |
| 21 | + """Run the picker feedback with less particles than needed for classification""" |
| 22 | + mock_pjid.return_value = 1 |
| 23 | + |
| 24 | + # Insert table dependencies |
| 25 | + dcg_entry: DataCollectionGroup = get_or_create_db_entry( |
| 26 | + murfey_db_session, |
| 27 | + DataCollectionGroup, |
| 28 | + lookup_kwargs={ |
| 29 | + "id": 0, |
| 30 | + "session_id": ExampleVisit.murfey_session_id, |
| 31 | + "tag": "test_dcg", |
| 32 | + }, |
| 33 | + ) |
| 34 | + dc_entry: DataCollection = get_or_create_db_entry( |
| 35 | + murfey_db_session, |
| 36 | + DataCollection, |
| 37 | + lookup_kwargs={ |
| 38 | + "id": 0, |
| 39 | + "tag": "test_dc", |
| 40 | + "dcg_id": dcg_entry.id, |
| 41 | + }, |
| 42 | + ) |
| 43 | + get_or_create_db_entry( |
| 44 | + murfey_db_session, |
| 45 | + ProcessingJob, |
| 46 | + lookup_kwargs={ |
| 47 | + "id": 0, |
| 48 | + "recipe": "test_recipe", |
| 49 | + "dc_id": dc_entry.id, |
| 50 | + }, |
| 51 | + ) |
| 52 | + |
| 53 | + message = { |
| 54 | + "program_id": 0, |
| 55 | + "cbox_3d": f"{tmp_path}/AutoPick/job007/CBOX_3d/sample.cbox", |
| 56 | + "particle_count": 2, |
| 57 | + "particle_diameters": [10.1, 20.2], |
| 58 | + "pixel_size": 5.3, |
| 59 | + "register": "picked_tomogram", |
| 60 | + "tomogram": f"{tmp_path}/Tomograms/job006/tomograms/sample.mrc", |
| 61 | + } |
| 62 | + |
| 63 | + picking._register_picked_tomogram_use_diameter(message, murfey_db_session) |
| 64 | + |
| 65 | + mock_pjid.assert_called_once_with(0, "em-tomo-class2d", murfey_db_session) |
| 66 | + |
| 67 | + tomograms_db = murfey_db_session.exec( |
| 68 | + select(TomogramPicks).where(TomogramPicks.pj_id == 1) |
| 69 | + ).one() |
| 70 | + assert tomograms_db.tomogram == message["tomogram"] |
| 71 | + assert tomograms_db.cbox_3d == message["cbox_3d"] |
| 72 | + assert tomograms_db.particle_count == 2 |
| 73 | + assert tomograms_db.pixel_size == 5.3 |
| 74 | + |
| 75 | + added_picks = murfey_db_session.exec( |
| 76 | + select(ParticleSizes).where(ParticleSizes.pj_id == 1) |
| 77 | + ).all() |
| 78 | + assert len(added_picks) == 2 |
| 79 | + assert added_picks[0].particle_size == 10.1 |
| 80 | + assert added_picks[1].particle_size == 20.2 |
| 81 | + |
| 82 | + |
| 83 | +@mock.patch("murfey.workflows.tomo.picking._transport_object") |
| 84 | +@mock.patch("murfey.workflows.tomo.picking._pj_id_tomo_classification") |
| 85 | +def test_picked_tomogram_run_class2d( |
| 86 | + mock_pjid, mock_transport, murfey_db_session: Session, tmp_path |
| 87 | +): |
| 88 | + """Run the picker feedback with less particles than needed for classification""" |
| 89 | + mock_pjid.return_value = 1 |
| 90 | + |
| 91 | + # Insert table dependencies |
| 92 | + dcg_entry: DataCollectionGroup = get_or_create_db_entry( |
| 93 | + murfey_db_session, |
| 94 | + DataCollectionGroup, |
| 95 | + lookup_kwargs={ |
| 96 | + "id": 0, |
| 97 | + "session_id": ExampleVisit.murfey_session_id, |
| 98 | + "tag": "test_dcg", |
| 99 | + }, |
| 100 | + ) |
| 101 | + dc_entry: DataCollection = get_or_create_db_entry( |
| 102 | + murfey_db_session, |
| 103 | + DataCollection, |
| 104 | + lookup_kwargs={ |
| 105 | + "id": 0, |
| 106 | + "tag": "test_dc", |
| 107 | + "dcg_id": dcg_entry.id, |
| 108 | + }, |
| 109 | + ) |
| 110 | + processing_job_entry: ProcessingJob = get_or_create_db_entry( |
| 111 | + murfey_db_session, |
| 112 | + ProcessingJob, |
| 113 | + lookup_kwargs={ |
| 114 | + "id": 0, |
| 115 | + "recipe": "test_recipe", |
| 116 | + "dc_id": dc_entry.id, |
| 117 | + }, |
| 118 | + ) |
| 119 | + get_or_create_db_entry( |
| 120 | + murfey_db_session, |
| 121 | + TomographyProcessingParameters, |
| 122 | + lookup_kwargs={ |
| 123 | + "dcg_id": dcg_entry.id, |
| 124 | + "pixel_size": 1.34, |
| 125 | + "dose_per_frame": 1, |
| 126 | + "frame_count": 5, |
| 127 | + "tilt_axis": 0, |
| 128 | + "voltage": 300, |
| 129 | + "particle_diameter": 200, |
| 130 | + }, |
| 131 | + ) |
| 132 | + for particle in range(10001): |
| 133 | + get_or_create_db_entry( |
| 134 | + murfey_db_session, |
| 135 | + ParticleSizes, |
| 136 | + lookup_kwargs={"pj_id": processing_job_entry.id, "particle_size": 100}, |
| 137 | + ) |
| 138 | + |
| 139 | + message = { |
| 140 | + "program_id": 0, |
| 141 | + "cbox_3d": f"{tmp_path}/AutoPick/job007/CBOX_3d/sample.cbox", |
| 142 | + "particle_count": 2, |
| 143 | + "particle_diameters": [10.1, 20.2], |
| 144 | + "pixel_size": 5.3, |
| 145 | + "register": "picked_tomogram", |
| 146 | + "tomogram": f"{tmp_path}/Tomograms/job006/tomograms/sample.mrc", |
| 147 | + } |
| 148 | + |
| 149 | + # Create a data collection group for lookups |
| 150 | + grid_square = DataCollectionGroup( |
| 151 | + id=1, |
| 152 | + session_id=ExampleVisit.murfey_session_id, |
| 153 | + tag="session_tag", |
| 154 | + atlas_id=90, |
| 155 | + ) |
| 156 | + murfey_db_session.add(grid_square) |
| 157 | + murfey_db_session.commit() |
| 158 | + |
| 159 | + picking._register_picked_tomogram_use_diameter(message, murfey_db_session) |
| 160 | + |
| 161 | + mock_pjid.assert_called_once_with(0, "em-tomo-class2d", murfey_db_session) |
| 162 | + |
| 163 | + tomograms_db = murfey_db_session.exec( |
| 164 | + select(TomogramPicks).where(TomogramPicks.pj_id == 1) |
| 165 | + ).one() |
| 166 | + assert tomograms_db.tomogram == message["tomogram"] |
| 167 | + assert tomograms_db.cbox_3d == message["cbox_3d"] |
| 168 | + assert tomograms_db.particle_count == 2 |
| 169 | + assert tomograms_db.pixel_size == 5.3 |
| 170 | + |
| 171 | + mock_transport.assert_called_once() |
0 commit comments