Exemplo n.º 1
0
    def test_save_image_stack(self):
        testdatadir = 'test_svimstack'
        if os.path.exists(testdatadir):
            shutil.rmtree(testdatadir, ignore_errors=True)
        szx = 30
        szy = 20
        szz = 120
        data3d = self.generate_waving_data(
            szx, szy, szz, value=150, dtype=np.uint8)
        # import sed3
        # ed = sed3.sed3(data3d)
        # ed.show()
        #
        dw = dwriter.DataWriter()

        dw.save_image_stack(data3d, testdatadir + '/soubory.png')
        dr = dreader.DataReader()
        data3dnew, metadata = dr.Get3DData(
            testdatadir, dataplus_format=False
            # 'sample_data/volumetrie/'
        )
        # import sed3
        # ed = sed3.sed3(data3dnew)
        # ed.show()
        self.assertEqual(
            np.sum(np.abs(data3d - data3dnew)),
            0
        )
        shutil.rmtree(testdatadir)
Exemplo n.º 2
0
    def test_write_and_read(self):
        filename = 'test_file.dcm'
        data = (np.random.random([30, 100, 120]) * 30).astype(np.int16)
        data[0:5, 20:60, 60:70] += 30
        metadata = {'voxelsize_mm': [1, 2, 3]}
        dw = dwriter.DataWriter()
        logger.debug(filename)
        dw.Write3DData(data, filename, filetype='dcm', metadata=metadata)

        dr = dreader.DataReader()
        newdata, newmetadata = dr.Get3DData(filename, dataplus_format=False)

        # print("meta ", metadata)
        # print("new meta ", newmetadata)

        # hack with -1024, because of wrong data reading
        self.assertEqual(data[10, 10, 10], newdata[10, 10, 10])
        self.assertEqual(data[2, 10, 1], newdata[2, 10, 1])
        # print(metadata["voxelsize_mm"])
        # print(newmetadata["voxelsize_mm"])
        self.assertEqual(metadata['voxelsize_mm'][0],
                         newmetadata['voxelsize_mm'][0])
        self. assertEqual(metadata['voxelsize_mm'][1],
                          newmetadata['voxelsize_mm'][1])
        self.assertEqual(metadata['voxelsize_mm'][2],
                         newmetadata['voxelsize_mm'][2])
        os.remove(filename)
Exemplo n.º 3
0
    def test_write_and_read(self):

        filename = 'test_file.dcm'
        data = (np.random.random([30, 100, 120]) * 30).astype(np.int16)
        data[0:5, 20:60, 60:70, ] += 30
        metadata = {'voxelsize_mm': [1, 2, 3]}
        dw = dwriter.DataWriter()
        dw.Write3DData(data, filename, filetype='dcm', metadata=metadata)

        dr = dreader.DataReader()
        newdata, newmetadata = dr.Get3DData(filename)

        # print  "meta ", metadata
        # print  "new meta ", newmetadata
        # hack with -1024, because of wrong data reading
        self. assertEqual(data[10, 10, 10], newdata[10, 10, 10])
        self. assertEqual(data[2, 10, 1], newdata[2, 10, 1])
        self. assertEqual(
            metadata['voxelsize_mm'][0], newmetadata['voxelsize_mm'][0])
# @TODO there is a bug in SimpleITK. slice voxel size must be same
        # self. assertEqual(metadata['voxelsize_mm'][1],
        # newmetadata['voxelsize_mm'][1])
        self. assertEqual(
            metadata['voxelsize_mm'][2], newmetadata['voxelsize_mm'][2])
        os.remove(filename)
Exemplo n.º 4
0
    def test_add_overlay_to_copied_dir(self):
        """
        writes 3d label to copied dicom files
        """
        filedir = 'test_outputs_dir'
        n_files = 3

        # number of tested overlay
        i_overlay = 6

        if not os.path.exists(filedir):
            os.mkdir(filedir)

# open copied data to obtain dcmfilefilelist
        dr = dreader.DataReader()
        data3d, metadata = dr.Get3DData(
            op.join(SAMPLE_DATA_DIR, 'jatra_5mm/'), dataplus_format=False
            # 'sample_data/volumetrie/'
        )
# for test we are working only with small number of files (n_files)
        metadata['dcmfilelist'] = metadata['dcmfilelist'][:n_files]

# create overlay
        overlay = np.zeros([n_files, 512, 512], dtype=np.uint8)
        overlay[:, 450:500, 30:100] = 1
# if there is more slides, try more complicated overlay
        overlay[0, 430:460, 20:110] = 1
        overlay[-1:, 470:520, 10:120] = 1

        overlays = {i_overlay: overlay}

        dw = dwriter.DataWriter()
        dw.DataCopyWithOverlay(metadata['dcmfilelist'], filedir, overlays)

# try read written data
        dr = dreader.DataReader()
        newdata, newmetadata = dr.Get3DData(filedir, dataplus_format=False)

        # test old function
        newoverlay = dr.GetOverlay()
        newoverlay = dr.get_overlay()


        self.assertTrue((newoverlay[i_overlay] == overlays[i_overlay]).all())

        # os.remove(filename)
        shutil.rmtree(filedir)
Exemplo n.º 5
0
    def test_write_dicom_from_scratch(self):
        """
        Reads dicom data and from slices and stores into one Dicom file. This
        file is then readed and compared with input data.
        """
        filename = 'tests/output_dcm3d.dcm'
        dr = dreader.DataReader()
        path_to_data = op.join(SAMPLE_DATA_DIR, 'jatra_5mm')
        logger.debug(SAMPLE_DATA_DIR)
        logger.debug(path_to_data)
        data3d, metadata = dr.Get3DData(
            path_to_data, dataplus_format=False
            # 'sample_data/volumetrie/'
        )
        dw = dwriter.DataWriter()
        dw.Write3DData(data3d, filename, filetype='dcm', metadata=metadata)

        data3d_n, metadata_n = dr.Get3DData(filename, dataplus_format=False)
        self.assertEqual(data3d[10, 11, 12], data3d_n[10, 11, 12])
        os.remove(filename)
Exemplo n.º 6
0
    def test_save_image_stack_with_writer(self):
        testdatadir = 'test_svimstack_with_writer'
        data3d = self.generate_waving_data(
            szx=30, szy=20, szz=10, value=150, dtype=np.uint8)
        dw = dwriter.DataWriter()

        dw.Write3DData(data3d, testdatadir + '/soubory.png',
                       filetype='image_stack')
        dr = dreader.DataReader()
        data3dnew, metadata = dr.Get3DData(
            testdatadir, dataplus_format=False
        )
        # import sed3
        # ed = sed3.sed3(data3dnew)
        # ed.show()
        self.assertEqual(
            np.sum(np.abs(data3d - data3dnew)),
            0
        )
        shutil.rmtree(testdatadir)
Exemplo n.º 7
0
    def test_add_overlay_and_read_one_file_with_overlay(self):
        filename = 'tests_outputs/test_file.dcm'
        filedir = os.path.dirname(filename)

        # number of tested overlay
        i_overlay = 6

        if not os.path.exists('tests_outputs'):
            os.mkdir('tests_outputs')

        data = (np.random.random([30, 100, 120]) * 30).astype(np.int16)
        data[0:5, 20:60, 60:70] += 30
        overlay = np.zeros([512, 512], dtype=np.uint8)
        overlay[450:500, 30:100] = 1

        # metadata = {'voxelsize_mm': [1, 2, 3]}
        dw = dwriter.DataWriter()
        dw.add_overlay_to_slice_file(
            # 'sample_data/jatra_5mm/IM-0001-0019.dcm',
            op.join(SAMPLE_DATA_DIR, 'volumetrie/volumetry_slice.DCM'),
            overlay,
            i_overlay,
            filename
        )
        # import ipdb; ipdb.set_trace()
        # print("asdfa")
        dr = dreader.DataReader()
        newdata, newmetadata = dr.Get3DData('tests_outputs', dataplus_format=False)
        newoverlay = dr.get_overlay()
        # print(newoverlay)

        # ed = pyed.py3DSeedEditor(newoverlay[6])
        # ed.show()
        # print("oioiu")
        self.assertTrue((newoverlay[i_overlay] == overlay).all())

        # os.remove(filename)
        shutil.rmtree(filedir)
Exemplo n.º 8
0
    def test_write_dcm_slices_datap(self):
        # Z nějakého důvodu plošné snímky neukládají tloušťku řezu. Jinak by test měl fungovat.
        datadir = "test_dcm"
        filename = datadir + '/test_file{slice_position:07.3f}.dcm'
        data = (np.random.random([30, 100, 120]) * 30).astype(np.int16)
        data[0:5, 20:60, 60:70] += 30
        datap = {'voxelsize_mm': [1, 2, 3], "data3d": data}
        dw = dwriter.DataWriter()
        dw.Write3DData(datap, filename, filetype='auto')

        dr = dreader.DataReader()
        newdata, newmetadata = dr.Get3DData(datadir, dataplus_format=False)

        # print("meta ", metadata)
        # print("new meta ", newmetadata)

        # hack with -1024, because of wrong data reading
        self.assertEqual(data[10, 10, 10], newdata[10, 10, 10])
        self.assertEqual(data[2, 10, 1], newdata[2, 10, 1])
        self.assertEqual(datap['voxelsize_mm'][0],
                         newmetadata['voxelsize_mm'][0])
        # os.removedirs()
        shutil.rmtree(datadir)
Exemplo n.º 9
0
    def test_add_overlay_and_read_one_file_with_overlay(self):
        filename = 'tests_outputs/test_file.dcm'
        filedir = os.path.dirname(filename)

        # number of tested overlay
        i_overlay = 6

        if not os.path.exists('tests_outputs'):
            os.mkdir('tests_outputs')

        data = (np.random.random([30, 100, 120]) * 30).astype(np.int16)
        data[0:5, 20:60, 60:70] += 30
        overlay = np.zeros([512, 512], dtype=np.uint8)
        overlay[450:500, 30:100] = 1

        # metadata = {'voxelsize_mm': [1, 2, 3]}
        dw = dwriter.DataWriter()

        dw.add_overlay_to_slice_file(
            # 'sample_data/jatra_5mm/IM-0001-0019.dcm',
            lisa.dataset.join_sdp('volumetrie/volumetry_slice.DCM'),
            overlay,
            i_overlay,
            filename
        )
        dr = dreader.DataReader()
        newdata, newmetadata = dr.Get3DData('tests_outputs')
        newoverlay = dr.GetOverlay()
        # print overlay

        # ed = pyed.sed3(newoverlay[6])
        # ed.show()
        self.assertTrue((newoverlay[i_overlay] == overlay).all())

        # os.remove(filename)
        shutil.rmtree(filedir)