def pull_and_send(self, items: Iterable, source: Orthanc, domain: str, dest: Orthanc, anonymize=False): def mkq(d: Dixel): return {"StudyInstanceUID": d.tags["StudyInstanceUID"]} for d in items: sham_oid = ShamDixel.sham_oid(d) logging.debug(sham_oid) if dest.exists(sham_oid): logging.debug("SKIPPING {}".format(d.tags["PatientName"])) continue if not source.exists(d): source.rfind(mkq(d), domain, level=DicomLevel.STUDIES, retrieve=True) else: logging.debug("SKIPPING PULL for {}".format( d.tags["PatientName"])) replacement_map = ShamDixel.orthanc_sham_map(d) anon_id = source.anonymize(d, replacement_map=replacement_map) source.psend(anon_id, dest) source.delete(anon_id) source.delete(d)
def test_anon(setup_orthanc0): O = Orthanc() dicom_dir = find_resource("resources/dcm") D = DcmDir(path=dicom_dir) d = D.get("IM2263", view=DixelView.TAGS_FILE) O.put(d) d.tags["AccessionNumber"] = "123456" d.tags["PatientBirthDate"] = "20000101" d.tags["PatientID"] = "ABC" d.tags["PatientName"] = "XYZ" d.level = DicomLevel.STUDIES e = ShamDixel.from_dixel(d) rep = e.orthanc_sham_map() O.anonymize("959e4e9f-e954be4e-11917c87-09d0f98f-7cc39128", level=DicomLevel.STUDIES, replacement_map=rep)
def _handle_instance_in_dcm_dir(item: Dixel, orth: Orthanc, salt: str): orth.put(item) anon = ShamDixel.from_dixel(item, salt=salt) afile = orth.anonymize(anon, replacement_map=anon.orthanc_sham_map()) anon.file = afile orth.put(anon) orth.delete(item) anon_study_id = anon.sham_parent_oid(DCMLv.STUDIES) logging.debug(anon_study_id) logging.debug(tagged_studies) if anon_study_id not in tagged_studies: logging.debug("Tagging parent study: {}".format(anon_study_id)) siren_info = pack_siren_info(anon) orth.gateway.put_metadata(anon_study_id, DCMLv.STUDIES, "siren_info", siren_info) tagged_studies.append(anon_study_id)
def pull_and_save(self, items: Iterable, source: Orthanc, domain: str, dest: DcmDir, anonymize=False): def mkq(d: Dixel): return {"StudyInstanceUID": d.tags["StudyInstanceUID"]} for d in items: working_level = DicomLevel.STUDIES if anonymize: if working_level == DicomLevel.SERIES: d_fn = "{}-{}.zip".format( d.meta["ShamAccessionNumber"][0:6], d.meta["ShamSeriesDescription"]) else: d_fn = "{}.zip".format(d.meta["ShamAccessionNumber"][0:16]) else: if working_level == DicomLevel.SERIES: d_fn = "{}-{}-{}.zip".format( d.tags["PatientName"][0:6], d.tags["AccessionNumber"][0:8], d.tags["SeriesDescription"]) else: d_fn = "{}-{}.zip".format(d.tags["PatientName"][0:6], d.tags["AccessionNumber"][0:8]) if dest.exists(d_fn): logging.debug("SKIPPING {}".format(d.tags["PatientName"])) continue if not source.exists(d): source.rfind(mkq(d), domain, level=working_level, retrieve=True) else: logging.debug("SKIPPING PULL for {}".format( d.tags["PatientName"])) if anonymize: try: replacement_map = ShamDixel.orthanc_sham_map(d) anon_id = source.anonymize(d, replacement_map=replacement_map) e = source.get(anon_id, level=working_level, view=DixelView.FILE) e.meta["FileName"] = d_fn logging.debug(e) dest.put(e) source.delete(e) except (HTTPError, GatewayConnectionError) as e: logging.error("Failed to anonymize dixel") logging.error(e) with open("errors.txt", "a+") as f: f.write(d.tags["AccessionNumber"] + "\n") else: d = source.get(d, level=working_level, view=DixelView.FILE) dest.put(d) try: source.delete(d) except GatewayConnectionError as e: logging.error("Failed to delete dixel") logging.error(e)