|
| 1 | +import datetime |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import time |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +from PIL import Image |
| 8 | +from pydicom.dataset import Dataset, FileMetaDataset |
| 9 | +from pydicom.uid import ExplicitVRLittleEndian, generate_uid |
| 10 | +from pynetdicom import AE |
| 11 | +from pynetdicom.sop_class import ( |
| 12 | + DigitalMammographyXRayImageStorageForPresentation, |
| 13 | +) |
| 14 | + |
| 15 | +from environment import Environment |
| 16 | +from models import WorklistItem |
| 17 | +from services.storage import MWLStorage, PACSStorage |
| 18 | + |
| 19 | +logging.basicConfig( |
| 20 | + level=os.getenv("LOG_LEVEL", "INFO").upper(), |
| 21 | + format=os.getenv("LOG_FORMAT", "%(asctime)s - %(name)s - %(levelname)s - %(message)s"), |
| 22 | +) |
| 23 | + |
| 24 | +logger = logging.getLogger(__name__) |
| 25 | + |
| 26 | + |
| 27 | +MWL_DB_PATH = os.getenv("MWL_DB_PATH", "/var/lib/pacs/worklist.db") |
| 28 | +PACS_DB_PATH = os.getenv("PACS_DB_PATH", "/var/lib/pacs/pacs.db") |
| 29 | +PACS_STORAGE_PATH = os.getenv("PACS_STORAGE_PATH", "/var/lib/pacs/storage") |
| 30 | +PACS_AET = os.getenv("PACS_AET", "SCREENING_PACS") |
| 31 | +PACS_ADDRESS = os.getenv("PACS_ADDRESS", "localhost") |
| 32 | +PACS_PORT = int(os.getenv("PACS_PORT", "4244")) |
| 33 | +DICOM_LATERALITIES = ["L", "R"] |
| 34 | +DICOM_VIEWS = ["CC", "MLO"] |
| 35 | +SAMPLE_IMAGES_PATH = os.getenv("SAMPLE_IMAGES_PATH", "/app/sample_images") |
| 36 | +EMULATED_PROCEDURE_DURATION_SECONDS = int(os.getenv("EMULATED_PROCEDURE_DURATION_SECONDS", "5")) |
| 37 | + |
| 38 | + |
| 39 | +class DicomExample: |
| 40 | + def __init__(self, worklist_item: WorklistItem, laterality: str, view: str, study_instance_uid: str): |
| 41 | + self.worklist_item = worklist_item |
| 42 | + self.laterality = laterality |
| 43 | + self.view = view |
| 44 | + self.data = self.generate_dicom(study_instance_uid) |
| 45 | + |
| 46 | + def generate_dicom(self, study_instance_uid: str) -> Dataset: |
| 47 | + img_path = f"{SAMPLE_IMAGES_PATH}/{self.laterality}{self.view}.jpg" |
| 48 | + img = Image.open(img_path).convert("L") |
| 49 | + columns, rows = img.size |
| 50 | + pixel_array = np.array(img, dtype=np.uint8) |
| 51 | + pixel_bytes = pixel_array.tobytes() |
| 52 | + if len(pixel_bytes) % 2 != 0: |
| 53 | + pixel_bytes += b"\x00" |
| 54 | + |
| 55 | + file_meta = FileMetaDataset() |
| 56 | + file_meta.MediaStorageSOPClassUID = DigitalMammographyXRayImageStorageForPresentation |
| 57 | + file_meta.MediaStorageSOPInstanceUID = generate_uid() |
| 58 | + file_meta.ImplementationClassUID = generate_uid() |
| 59 | + file_meta.TransferSyntaxUID = ExplicitVRLittleEndian |
| 60 | + |
| 61 | + ds = Dataset() |
| 62 | + ds.SOPClassUID = file_meta.MediaStorageSOPClassUID |
| 63 | + ds.SOPInstanceUID = file_meta.MediaStorageSOPInstanceUID |
| 64 | + ds.SamplesPerPixel = 1 |
| 65 | + ds.PhotometricInterpretation = "MONOCHROME2" |
| 66 | + ds.Rows = rows |
| 67 | + ds.Columns = columns |
| 68 | + ds.BitsAllocated = 8 |
| 69 | + ds.BitsStored = 8 |
| 70 | + ds.HighBit = 7 |
| 71 | + ds.PixelRepresentation = 0 |
| 72 | + ds.PixelData = pixel_bytes |
| 73 | + ds.ImageLaterality = self.laterality |
| 74 | + ds.ViewPosition = self.view |
| 75 | + |
| 76 | + ds.AccessionNumber = self.worklist_item.accession_number |
| 77 | + ds.PatientID = self.worklist_item.patient_id |
| 78 | + ds.PatientName = self.worklist_item.patient_name |
| 79 | + ds.PatientBirthDate = self.worklist_item.patient_birth_date |
| 80 | + ds.PatientSex = self.worklist_item.patient_sex |
| 81 | + ds.StudyDate = self.worklist_item.scheduled_date |
| 82 | + ds.StudyTime = self.worklist_item.scheduled_time |
| 83 | + ds.StudyInstanceUID = study_instance_uid |
| 84 | + ds.StudyID = self.worklist_item.accession_number |
| 85 | + ds.SeriesInstanceUID = generate_uid() |
| 86 | + ds.SeriesNumber = 1 |
| 87 | + ds.Modality = "MG" |
| 88 | + ds.InstanceNumber = 1 |
| 89 | + ds.file_meta = file_meta |
| 90 | + |
| 91 | + logger.debug( |
| 92 | + f"Generated DICOM for worklist item {self.worklist_item.accession_number} - {self.laterality}{self.view}" |
| 93 | + ) |
| 94 | + logger.debug(f"{ds}") |
| 95 | + |
| 96 | + return ds |
| 97 | + |
| 98 | + |
| 99 | +class ModalityEmulator: |
| 100 | + def __init__(self, mwl_storage: MWLStorage, pacs_storage: PACSStorage): |
| 101 | + self.mwl_storage = mwl_storage |
| 102 | + self.pacs_storage = pacs_storage |
| 103 | + |
| 104 | + def process_worklist_items(self, ae: AE): |
| 105 | + """ |
| 106 | + Queries the MWL for items scheduled for today and sends generated DICOM files to the PACS server for each item. |
| 107 | + """ |
| 108 | + today_formatted = datetime.date.today().strftime("%Y%m%d") |
| 109 | + worklist_items = self.mwl_storage.find_worklist_items( |
| 110 | + scheduled_date=today_formatted, |
| 111 | + ) |
| 112 | + |
| 113 | + if not worklist_items: |
| 114 | + return |
| 115 | + |
| 116 | + time.sleep(EMULATED_PROCEDURE_DURATION_SECONDS) |
| 117 | + |
| 118 | + assoc = ae.associate(PACS_ADDRESS, PACS_PORT, ae_title=PACS_AET) |
| 119 | + |
| 120 | + if assoc.is_established: |
| 121 | + logger.info(f"Connected to server {PACS_ADDRESS}:{PACS_PORT} ({PACS_AET})") |
| 122 | + |
| 123 | + for item in worklist_items: |
| 124 | + study_instance_uid = generate_uid() |
| 125 | + for laterality in DICOM_LATERALITIES: |
| 126 | + for view in DICOM_VIEWS: |
| 127 | + logger.info( |
| 128 | + f"Processing worklist item {item.accession_number} - generating DICOM for {laterality}{view}" |
| 129 | + ) |
| 130 | + dicom_example = DicomExample(item, laterality, view, study_instance_uid) |
| 131 | + assoc.send_c_store(dicom_example.data) |
| 132 | + logger.info(f"Sent DICOM for {laterality}{view} of worklist item {item.accession_number}") |
| 133 | + |
| 134 | + self.mwl_storage.update_status(item.accession_number, "COMPLETED") |
| 135 | + logger.info(f"Completed processing for worklist item {item.accession_number}") |
| 136 | + |
| 137 | + assoc.release() |
| 138 | + else: |
| 139 | + logger.error(f"Failed to connect to server {PACS_ADDRESS}:{PACS_PORT} ({PACS_AET})") |
| 140 | + |
| 141 | + |
| 142 | +def main(): |
| 143 | + if Environment().production: |
| 144 | + raise RuntimeError("Modality Emulator should not be run in production environment") |
| 145 | + |
| 146 | + logger.info("Modality Emulator Starting...") |
| 147 | + mwl_storage = MWLStorage(db_path=MWL_DB_PATH) |
| 148 | + pacs_storage = PACSStorage(db_path=PACS_DB_PATH) |
| 149 | + emulator = ModalityEmulator(mwl_storage, pacs_storage) |
| 150 | + ae = AE(ae_title="ModalityEmulator") |
| 151 | + ae.add_requested_context(DigitalMammographyXRayImageStorageForPresentation) |
| 152 | + |
| 153 | + while True: |
| 154 | + try: |
| 155 | + emulator.process_worklist_items(ae) |
| 156 | + time.sleep(5) |
| 157 | + except KeyboardInterrupt: |
| 158 | + logger.warning("\n Modality Emulator shutting down...") |
| 159 | + break |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + main() |
0 commit comments