def test_exists(): resources_dir = find_resource("resources/dcm") D = DcmDir(path=resources_dir) logging.debug(D) assert (D.check()) assert (D.exists("IM2263")) assert (not D.exists("abcd")) D = DcmDir(path=resources_dir, subpath_width=3, subpath_depth=2) assert (D.check()) assert (D.exists("IM2263")) assert (not D.exists("abcd")) logging.debug("Checking round-trip") d = D.get("IM2263") logging.debug(pformat(d.tags)) dd = d.json() ee = json.loads(dd) e = Serializable.Factory.create(**ee) logging.debug(d) logging.debug(e) assert (d.tags == e.tags)
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)