def test_view_classifier(): # These weights classify as AP (pos) or Lateral (neg) fp = find_resource("resources/models/pose/view_classifier.h5") _model = get_mobilenet(0, weights=None) _model.load_weights(fp) fp = find_resource("resources/scouts") D = DcmDir(path=fp) # This one is lateral d = D.get("ct_scout_01.dcm", view=DixelView.PIXELS) prediction = d.get_prediction(_model) logging.debug("Prediction is {}".format(prediction)) assert (prediction < 0.5) # prediction = get_prediction( model, image ) # positive = "AP" # negative = "Lateral" # if prediction >= 0.5: # logging.debug("Predicted: {} ({})".format( positive, round(prediction, 2) )) # else: # logging.debug("Predicted: {} ({})".format( negative, round(1.0-prediction, 2) )) # This one is an AP d = D.get("ct_scout_02.dcm", view=DixelView.PIXELS) prediction = d.get_prediction(_model) logging.debug("Prediction is {}".format(prediction)) assert (prediction >= 0.5)
def test_reader(): dcm_file = find_resource("resources/dcm/IM2263") json_file = find_resource("resources/reports/screening_rpt_anon.txt") DcmFileHandler().get(dcm_file) with pytest.raises(DicomFormatError): DcmFileHandler().get(json_file)
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 test_orthanc_upload(setup_orthanc0): logging.debug("Test Orthanc Upload") O = Orthanc() dicom_dir = find_resource("resources/dcm") D = DcmDir(path=dicom_dir) d = D.get("IM2263", view=DixelView.TAGS_FILE) O.put(d) q = {"PatientID": "AW15119516.678.1392297407"} result = O.find(q) if result: id = result[0] logging.debug(id) result = O.exists(id) logging.debug(result) assert (result) O.delete(d) result = O.exists(id) assert (not result)
def test_ba_estimator(): # These weights classify as AP (pos) or Lateral (neg) model_path = find_resource("resources/models/bone_age") image_path = "/Users/derek/Data/DICOM/bone_age" predict_ba(dicom_folder=image_path, models_folder=model_path)
def test_zip_reader(): p = find_resource("resources") D = DcmDir(path=p) f = D.get("dcm/IM2263", view=DixelView.TAGS_FILE) gs = D.get_zipped("dcm_zip/test.zip") for _g in gs: if "2263" in _g.meta["FileName"]: g = _g break logging.debug(f) logging.debug(g) assert(f.tags["StudyInstanceUID"] == g.tags["StudyInstanceUID"]) from binascii import hexlify logging.debug("f file:") logging.debug( hexlify(f.file[1024:2048]) ) logging.debug("g file:") logging.debug( hexlify(g.file[1024:2048]) ) assert f.file == g.file
def test_psend(setup_orthanc0, setup_orthanc1): O = Orthanc(peername="peer0") print(O) O.check() O2 = Orthanc(port=8043, peername="peer0") print(O2) O2.check() dicom_dir = find_resource("resources/dcm") D = DcmDir(path=dicom_dir) d = D.get("IM2263", view=DixelView.TAGS_FILE) O2.put(d) logging.debug(O2.gateway._get("peers")) O2.psend(d.oid(), O) e = O.get(d.oid(), level=DicomLevel.INSTANCES) logging.debug(e) assert d.oid() == e.oid()
def orth_test_runner(): O = Orthanc() dicom_dir = find_resource("resources/dcm") D = DcmDir(path=dicom_dir) print("Starting script") time.sleep(1) d = D.get("IM2263", view=DixelView.FILE) O.put(d) O.check() print("Ending script")
def test_cfg_handler(): cfg_file = find_resource("resources/config/td_bridge.cfg.jdf") with open(cfg_file) as f: source = f.read() data = SimpleConfigParser().loads(source) logging.debug(pformat(data)) out = SimpleConfigParser().dumps(data) assert out.strip() in source
def test_mock_svcs(): C.start_swarm() C.clean_swarm() c = C.api_client() admin_stack = find_resource("platform/docker-stacks/admin/admin-stack.yml") cmd = ["docker", "stack", "deploy", "-c", admin_stack, "admin"] subprocess.run(cmd) service_names = [x['Spec']['Name'] for x in c.services()] assert ("admin_portainer" in service_names) assert ("admin_traefik" in service_names) mock_stack = find_resource( "platform/docker-stacks/diana-workers/mock-stack.yml") cmd = ["docker", "stack", "deploy", "-c", mock_stack, "mock"] # Don't forget to set the password, or docker-compose will # interpret it as empty rather than default os.environ["ORTHANC_PASSWORD"] = "******" subprocess.run(cmd) service_names = [x['Spec']['Name'] for x in c.services()] assert ("mock_diana-mock" in service_names) assert ("mock_orthanc-mock" in service_names) # Pause to generate some data time.sleep(20) O = Orthanc(path="orthanc-mock", port=80) info = O.gateway._get("statistics") # At least 100 new images in the PACS assert (info['CountInstances'] > 100) C.clean_swarm()
def test_cli_svc_check(setup_orthanc0, setup_redis): runner = CliRunner() services_file = find_resource("resources/test_services.yml") result = runner.invoke(app, [ "-s", "{redis_bad2: {ctype: Redis, port: 9999}}", "-S", services_file, "check" ]) print(result.output) assert ("orthanc: Ready" in result.output) assert ("orthanc_bad: Not Ready" in result.output) assert ("redis: Ready" in result.output) assert ("redis_bad: Not Ready" in result.output) assert ("redis_bad2: Not Ready" in result.output)
def test_simplify(): dir = find_resource("resources/dose") items = ["dose_tags_anon"] for item in items: fn = "{}.json".format(item) fp = os.path.join(dir, fn) with open(fp) as f: tags = json.load(f) tags = dicom_simplify(tags) # Have to compare in dumped space b/c loader doesn't convert times back into times tag_str = json.dumps(tags, indent=3, cls=SmartJSONEncoder, sort_keys=True) # logging.debug(pformat(tag_str)) fn = "{}.simple.json".format(item) fp = os.path.join(dir, fn) # with open(fp, 'w') as f: # f.write(tag_str) with open(fp) as f: simple_str = f.read() # logging.debug(tag_str) # logging.debug(simple_str) # a = tag_str # b = simple_str # for i, s in enumerate(difflib.ndiff(a, b)): # if s[0] == ' ': # continue # elif s[0] == '-': # print(u'Delete "{}" from position {}'.format(s[-1], i)) # elif s[0] == '+': # print(u'Add "{}" to position {}'.format(s[-1], i)) assert tag_str == simple_str
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 test_measurement(): path = find_resource("resources/scouts") D = DcmDir(path=path) fn_s1 = "ct_scout_01.dcm" d_s1 = D.get(fn_s1, view=DixelView.PIXELS) ret = d_s1.measure_scout() logging.debug(ret) assert (ret[0] == 'AP' and round(ret[1]) == 28) fn_s2 = "ct_scout_02.dcm" d_s2 = D.get(fn_s2, view=DixelView.PIXELS) ret = d_s2.measure_scout() logging.debug(ret) assert (ret[0] == 'LATERAL' and round(ret[1]) == 43)
def test_mock_dixel(): reset_mock_seed() d = MockStudy(study_datetime=ref_dt) inst = list(d.instances())[-1] # Check reproducibility reset_mock_seed() e = MockStudy(study_datetime=ref_dt) assert (inst.tags == list(e.instances())[-1].tags) # Check correctness logging.debug(inst.tags) assert (inst.tags == ref_inst_tags) logging.debug(len(list(d.instances()))) assert (len(list(d.instances())) == ref_n_instances) for item in d.instances(): logging.debug("{!s}".format(item)) # Check file creation inst.gen_file() inst_dcm = find_resource("resources/mock/inst.dcm") # Reset mock/inst.dcm with fresh data with open(inst_dcm, "wb") as f: f.write(inst.file) with open(inst_dcm, "rb") as f: data = f.read() assert (inst.file == data) # Test file legibility ds = pydicom.dcmread(BytesIO(inst.file)) logging.debug(ds) assert (ds.AccessionNumber == d.tags["AccessionNumber"]) # Test new item is unique f = MockStudy(study_datetime=ref_dt) for item in f.instances(): logging.debug("{!s}".format(item)) assert (d.tags["AccessionNumber"] != f.tags["AccessionNumber"])
def test_unzip(tmp_path): resources_dir = find_resource("resources/dcm_zip") fp = resources_dir / "test.zip" E = ImageDir(path=tmp_path, anonymizing=True) with open(fp, 'rb') as f: E.put_zipped(f) fn = "6ee6f414e4c779c8bb1f90baf45c000c-0004-0002.png" fp = Path( tmp_path / fn ) assert( fp.is_file() ) os.remove(fp) fn = "78496388f522585b71b90f374051f552-0016-0001.png" fp = Path( tmp_path / fn ) assert( fp.is_file() ) os.remove(fp)
def test_reader(): path = find_resource("resources/reports") with open(os.path.join(path, "screening_rpt_anon.txt"), "r") as f: text = f.readlines() r = LungScreeningReport(text=text) logging.debug(r) logging.debug(r.current_smoker()) logging.debug(r.lungrads()) logging.debug(r.radcat()) logging.debug(r.years_since_quit()) assert (r.current_smoker() == True) assert (r.lungrads() == "2S") assert (r.radcat() == ('3', True))
def test_subdirs(): D = DcmDir(recurse_style="ORTHANC") # for fp in D.recurse(): # logging.debug(fp) subdirs = list(D.subdirs()) assert (len(subdirs) == 256 * 256) assert ("./ff/ff" in subdirs) assert ("./00/00" in subdirs) p = find_resource("resources") D = DcmDir(path=p) subdirs = list(D.subdirs()) logging.debug(subdirs) assert (len(subdirs) >= 8)
def test_montage(): services_file = find_resource(".secrets/lifespan_services.yml") with open(services_file) as f: services = yaml.load(f) kwargs = services['montage'] del kwargs['ctype'] source = Montage(**services['montage']) assert (source.check()) qdict = { "q": "cta", "modality": 4, "exam_type": [8683, 7713, 8766], "start_date": "2016-11-17", "end_date": "2016-11-19" } worklist = source.find(qdict) assert (len(worklist) == 20)
def test_conversion(tmp_path): resources_dir = find_resource("resources/dcm") D = DcmDir(path=resources_dir) logging.debug(D) d = D.get("IM2263", view=DixelView.PIXELS) E = ImageDir(path=tmp_path) E.put(d) fn = "1.2.840.113619.2.181.90581140298.2577.1392297407726.4-0004-0002.png" fp = Path( tmp_path / fn ) assert( fp.is_file() ) os.remove(fp) F = ImageDir(path=tmp_path, anonymizing=True) F.put(d) fn = "6ee6f414e4c779c8bb1f90baf45c000c-0004-0002.png" fp = Path( tmp_path / fn ) assert( fp.is_file() ) os.remove(fp)
def mk_diana_rest(): service_path = find_resource("resources/test_services.yml") app.app.load_services(service_path) client = app.app.test_client() return client