def test_parse_dicom_file(): link_df = load_pid_oid() pid = link_df['patient_id'][0] fid = 59 dicom_parser = parsers.DicomParser(pid, fid) dicom_image = dicom_parser.parse() assert dicom_image.shape == (256, 256, 3) fid = -30 dicom_parser = parsers.DicomParser(pid, fid) dicom_image = dicom_parser.parse() assert dicom_image is None
def test_parse_boolean_mask(): link_df = load_pid_oid() pid = link_df['patient_id'][0] oid = link_df['original_id'][0] fid = 59 dicom_parser = parsers.DicomParser(pid, fid) dicom_image = dicom_parser.parse() icontour_type = 'i' icontour_parser = parsers.ContourParser(oid, fid, icontour_type) icontour_polygon = icontour_parser.parse() imask_parser = parsers.MaskParser(dicom_image, icontour_polygon) imask = imask_parser.parse() assert imask.shape == dicom_image.shape ocontour_type = 'o' ocontour_parser = parsers.ContourParser(oid, fid, ocontour_type) ocontour_polygon = ocontour_parser.parse() omask_parser = parsers.MaskParser(dicom_image, ocontour_polygon) omask = omask_parser.parse() assert omask.shape == dicom_image.shape mask_parser = parsers.MaskParser(None, icontour_polygon) mask = mask_parser.parse() assert mask is None mask_parser = parsers.MaskParser(dicom_image, None) mask = mask_parser.parse() assert mask is None
def test_visualize_dicom_image(): link_df = load_pid_oid() pid = link_df['patient_id'][0] fid = 59 dicom_parser = parsers.DicomParser(pid, fid) dicom_parser.visualize()
def test__construct_dicom_filename(): link_df = load_pid_oid() pid = link_df['patient_id'][0] fid = 59 expected_fn = 'dicoms/' + pid + '/' + str(fid) + '.dcm' dicom_parser = parsers.DicomParser(pid, fid) dicom_fn = dicom_parser._construct_filename() assert dicom_fn == expected_fn fid = -123 dicom_parser = parsers.DicomParser(pid, fid) try: dicom_fn = dicom_parser._construct_filename() except IOError: print('passed detecting non-existing dicom file.')
def test__plot_input_output(): link_fn = 'link.csv' link_df = pd.read_csv(link_fn) pid = link_df['patient_id'][0] oid = link_df['original_id'][0] fid = 59 dicom_parser = parsers.DicomParser(pid, fid) dicom_image = dicom_parser.parse() icontour_type = 'i' icontour_parser = parsers.ContourParser(oid, fid, icontour_type) icontour_polygon = icontour_parser.parse() imask_parser = parsers.MaskParser(dicom_image, icontour_polygon) imask = imask_parser.parse() ocontour_type = 'o' ocontour_parser = parsers.ContourParser(oid, fid, ocontour_type) ocontour_polygon = ocontour_parser.parse() omask_parser = parsers.MaskParser(dicom_image, ocontour_polygon) omask = omask_parser.parse() train_pipe = pipeline.TrainingPipeline(link_fn, 8) train_pipe._plot_input_output(dicom_image, imask, omask)
def test__process_dicom_image(): link_df = load_pid_oid() pid = link_df['patient_id'][0] fid = 59 dicom_parser = parsers.DicomParser(pid, fid) fake_dicom_image = np.random.randint(0, 1000, size=[256, 256]) dicom_rgb = dicom_parser._process_dicom_image(fake_dicom_image) assert dicom_rgb.min() >= 0 assert dicom_rgb.max() <= 255 assert dicom_rgb.shape == (256, 256, 3)
def test_visualize_boolean_mask(): link_df = load_pid_oid() pid = link_df['patient_id'][0] fid = 59 dicom_parser = parsers.DicomParser(pid, fid) dicom_image = dicom_parser.parse() oid = link_df['original_id'][0] icontour_type = 'i' icontour_parser = parsers.ContourParser(oid, fid, icontour_type) icontour_polygon = icontour_parser.parse() imask_parser = parsers.MaskParser(dicom_image, icontour_polygon) imask_parser.visualize() ocontour_type = 'o' ocontour_parser = parsers.ContourParser(oid, fid, ocontour_type) ocontour_polygon = ocontour_parser.parse() omask_parser = parsers.MaskParser(dicom_image, ocontour_polygon) omask_parser.visualize()
def _parse_input_output(self, pid_oid, visualize): """Parse all the paired up DICOM, contour, and producing bool masks. Params: pid_oid: DataFrame stores: `patient_id`, `original_id`, list of `file_id` visualize: boolean - if True, invoke data visualization """ dicom_input = [] mask_output = [] for _, row in pid_oid.iterrows(): pid = row['patient_id'] oid = row['original_id'] fids = row['file_id'] for fid in fids: dicom = parsers.DicomParser(pid, fid).parse() masks = [] for contour_type in self._contour_types: contour = parsers.ContourParser(oid, fid, contour_type).parse() if dicom is not None and contour is not None: mask = parsers.MaskParser(dicom, contour).parse() masks.append(mask) if len(masks) == 2: dicom_input.append(dicom) mask_output.append(np.stack(masks, -1)) if visualize and dicom is not None and len(masks) == 2: self._plot_input_output(dicom, masks[0], masks[1]) self._inputs = np.stack(dicom_input, axis=0) self._targets = np.stack(mask_output, axis=0)