How to use the pydicom.filereader.read_file function in pydicom

To help you get started, we’ve selected a few pydicom examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pydicom / pydicom / tests / test_filewriter.py View on Github external
def compare(self, in_filename, decode=False):
        """Read Dataset from `in_filename`, write Dataset to file, then compare."""
        with open(in_filename, 'rb') as f:
            bytes_in = BytesIO(f.read())
            bytes_in.seek(0)

        ds = read_file(bytes_in)
        ds.save_as(self.file_out)
        self.file_out.seek(0)
        bytes_out = BytesIO(self.file_out.read())
        bytes_in.seek(0)
        bytes_out.seek(0)
        same, pos = bytes_identical(bytes_in.getvalue(), bytes_out.getvalue())
        self.assertTrue(same, "Read bytes is not identical to written bytes - "
                              "first difference at 0x%x" % pos)
github pydicom / pydicom / tests / test_filewriter.py View on Github external
def test_commandset_no_written(self):
        """Test that Command Set elements are not written when writing to standard"""
        ds = read_file(ct_name)
        preamble = ds.preamble[:]
        ds.MessageID = 3
        ds.save_as(self.fp, write_like_original=False)
        self.fp.seek(0)
        self.assertEqual(self.fp.read(128), preamble)
        self.assertEqual(self.fp.read(4), b'DICM')
        self.assertTrue('MessageID' in ds)

        fp = BytesIO(self.fp.getvalue()) # Workaround to avoid #358
        ds_out = read_file(fp)
        self.assertEqual(ds_out.preamble, preamble)
        self.assertTrue('PatientID' in ds_out)
        self.assertTrue('TransferSyntaxUID' in ds_out.file_meta)
        self.assertFalse('MessageID' in ds_out)
github pydicom / pydicom / tests / test_filereader.py View on Github external
def test_group_length_wrong(self):
        """Test file is read correctly even if FileMetaInformationGroupLength
        is incorrect.
        """
        bytestream = (b'\x02\x00\x00\x00\x55\x4C\x04\x00\x0A\x00\x00\x00'
                      b'\x02\x00\x02\x00\x55\x49\x16\x00\x31\x2e\x32\x2e'
                      b'\x38\x34\x30\x2e\x31\x30\x30\x30\x38\x2e\x35\x2e'
                      b'\x31\x2e\x31\x2e\x39\x00\x02\x00\x10\x00\x55\x49'
                      b'\x12\x00\x31\x2e\x32\x2e\x38\x34\x30\x2e\x31\x30'
                      b'\x30\x30\x38\x2e\x31\x2e\x32\x00'
                      b'\x20\x20\x10\x00\x02\x00\x00\x00\x01\x00\x20\x20'
                      b'\x20\x00\x06\x00\x00\x00\x4e\x4f\x52\x4d\x41\x4c')
        fp = BytesIO(bytestream)
        ds = read_file(fp, force=True)
        self.assertFalse(len(
            bytestream) - 12 == ds.file_meta.FileMetaInformationGroupLength)
        self.assertTrue(ds.file_meta.FileMetaInformationGroupLength == 10)
        self.assertTrue('MediaStorageSOPClassUID' in ds.file_meta)
        self.assertEqual(ds.file_meta.TransferSyntaxUID,
                         ImplicitVRLittleEndian)
        self.assertEqual(ds.Polarity, 'NORMAL')
        self.assertEqual(ds.ImageBoxPosition, 1)
github pydicom / pydicom / tests / test_filereader.py View on Github external
def test_no_dataset(self):
        """Test reading no elements or preamble produces empty Dataset"""
        bytestream = b''
        fp = BytesIO(bytestream)
        ds = read_file(fp, force=True)
        self.assertTrue(ds.preamble is None)
        self.assertEqual(ds.file_meta, Dataset())
        self.assertEqual(ds[:], Dataset())
github pydicom / pydicom / tests / test_filewriter.py View on Github external
def compare(self, in_filename, out_filename, decode=False):
        """Read file1, write file2, then compare.
        Return value as for files_identical.
        """
        dataset = read_file(in_filename)
        if decode:
            dataset.decode()

        dataset.save_as(out_filename)
        same, pos = files_identical(in_filename, out_filename)
        self.assertTrue(same,
                        "Files are not identical - first difference at 0x%x" % pos)
        if os.path.exists(out_filename):
            os.remove(out_filename)  # get rid of the file
github pydicom / pydicom / tests / test_filereader.py View on Github external
def test_read_UC_explicit_little(self):
        """Check creation of DataElement from byte data works correctly."""
        ds = read_file(self.fp_ex, force=True)
        ref_elem = ds.get(0x00189908)
        elem = DataElement(0x00189908, 'UC', ['A', 'B', 'C'])
        self.assertEqual(ref_elem, elem)

        ds = read_file(self.fp_ex, force=True)
        ref_elem = ds.get(0x00100212)
        elem = DataElement(0x00100212, 'UC', 'Test')
        self.assertEqual(ref_elem, elem)
github pydicom / pydicom / tests / test_filewriter.py View on Github external
def test_commandset(self):
        """Test written OK with command set"""
        ds = read_file(ct_name)
        del ds[:]
        del ds.preamble
        del ds.file_meta
        ds.CommandGroupLength = 8
        ds.MessageID = 1
        ds.MoveDestination = 'SOME_SCP'
        ds.Status = 0x0000
        ds.save_as(self.fp, write_like_original=True)
        self.fp.seek(0)
        self.assertRaises(EOFError, self.fp.read, 128)
        self.fp.seek(0)
        self.assertNotEqual(self.fp.read(4), b'DICM')
        # Ensure Command Set Elements written as little endian implicit VR
        self.fp.seek(0)
        #self.assertEqual(self.fp.read(12), b'\x00\x00\x00\x00\x04\x00\x00\x00\x08\x00\x00\x00')
github pydicom / pydicom / tests / test_jpeg_ls_pixel_data.py View on Github external
def setUp(self):
        self.jpeg_2k = read_file(jpeg2000_name)
        self.jpeg_2k_lossless = read_file(jpeg2000_lossless_name)
        self.mr_small = read_file(mr_name)
        self.emri_jpeg_2k_lossless = read_file(emri_jpeg_2k_lossless)
        self.emri_small = read_file(emri_name)
        self.original_handlers = pydicom.config.image_handlers
        pydicom.config.image_handlers = [jpeg_ls_handler, numpy_handler]
github pydicom / pydicom / tests / test_filereader.py View on Github external
def test_read_OL_explicit_little(self):
        """Check creation of OL DataElement from byte data works correctly."""
        ds = read_file(self.fp_ex, force=True)
        ref_elem = ds.get(0x00720075)
        elem = DataElement(0x00720075, 'OL',
                           b'\x00\x01\x02\x03\x04\x05\x06\x07'
                           b'\x01\x01\x02\x03')
        self.assertEqual(ref_elem, elem)
github pydicom / pydicom / tests / test_gdcm_pixel_data.py View on Github external
def setUp(self):
        self.jpeg_ls_lossless = read_file(jpeg_ls_lossless_name)
        self.mr_small = read_file(mr_name)
        self.emri_jpeg_ls_lossless = read_file(emri_jpeg_ls_lossless)
        self.emri_small = read_file(emri_name)
        self.original_handlers = pydicom.config.image_handlers
        pydicom.config.image_handlers = [gdcm_handler]