当前位置: 首页>>代码示例>>Python>>正文


Python DicomBytesIO.seek方法代码示例

本文整理汇总了Python中pydicom.filebase.DicomBytesIO.seek方法的典型用法代码示例。如果您正苦于以下问题:Python DicomBytesIO.seek方法的具体用法?Python DicomBytesIO.seek怎么用?Python DicomBytesIO.seek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pydicom.filebase.DicomBytesIO的用法示例。


在下文中一共展示了DicomBytesIO.seek方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_charset_patient_names

# 需要导入模块: from pydicom.filebase import DicomBytesIO [as 别名]
# 或者: from pydicom.filebase.DicomBytesIO import seek [as 别名]
    def test_charset_patient_names(self, filename, patient_name):
        """Test patient names are correctly decoded and encoded."""
        # check that patient names are correctly read
        file_path = get_charset_files(filename + '.dcm')[0]
        ds = dcmread(file_path)
        ds.decode()
        assert patient_name == ds.PatientName

        # check that patient names are correctly written back
        fp = DicomBytesIO()
        fp.is_implicit_VR = False
        fp.is_little_endian = True
        ds.save_as(fp, write_like_original=False)
        fp.seek(0)
        ds = dcmread(fp)
        assert patient_name == ds.PatientName

        # check that patient names are correctly written back
        # without original byte string (PersonName3 only)
        if hasattr(ds.PatientName, 'original_string'):
            ds.PatientName.original_string = None
            fp = DicomBytesIO()
            fp.is_implicit_VR = False
            fp.is_little_endian = True
            ds.save_as(fp, write_like_original=False)
            fp.seek(0)
            ds = dcmread(fp)
            assert patient_name == ds.PatientName
开发者ID:scaramallion,项目名称:pydicom,代码行数:30,代码来源:test_charset.py

示例2: test_changed_character_set

# 需要导入模块: from pydicom.filebase import DicomBytesIO [as 别名]
# 或者: from pydicom.filebase.DicomBytesIO import seek [as 别名]
 def test_changed_character_set(self):
     # Regression test for #629
     multiPN_name = get_charset_files("chrFrenMulti.dcm")[0]
     ds = dcmread(multiPN_name)  # is Latin-1
     ds.SpecificCharacterSet = 'ISO_IR 192'
     from pydicom.filebase import DicomBytesIO
     fp = DicomBytesIO()
     ds.save_as(fp, write_like_original=False)
     fp.seek(0)
     ds_out = dcmread(fp)
     # we expect UTF-8 encoding here
     assert b'Buc^J\xc3\xa9r\xc3\xb4me' == ds_out.get_item(0x00100010).value
开发者ID:scaramallion,项目名称:pydicom,代码行数:14,代码来源:test_charset.py

示例3: test_dcmread_does_not_raise

# 需要导入模块: from pydicom.filebase import DicomBytesIO [as 别名]
# 或者: from pydicom.filebase.DicomBytesIO import seek [as 别名]
 def test_dcmread_does_not_raise(self):
     """Test that reading from DicomBytesIO does not raise on EOF.
     Regression test for #358."""
     ds = dcmread(mr_name)
     fp = DicomBytesIO()
     ds.save_as(fp, write_like_original=True)
     fp.seek(0)
     de_gen = data_element_generator(fp, False, True)
     try:
         while True:
             next(de_gen)
     except StopIteration:
         pass
     except EOFError:
         self.fail('Unexpected EOFError raised')
开发者ID:kayarre,项目名称:pydicom,代码行数:17,代码来源:test_filereader.py

示例4: write_file_meta_info

# 需要导入模块: from pydicom.filebase import DicomBytesIO [as 别名]
# 或者: from pydicom.filebase.DicomBytesIO import seek [as 别名]
def write_file_meta_info(fp, file_meta, enforce_standard=True):
    """Write the File Meta Information elements in `file_meta` to `fp`.

    If `enforce_standard` is True then the file-like `fp` should be positioned
    past the 128 byte preamble + 4 byte prefix (which should already have been
    written).

    DICOM File Meta Information Group Elements
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    From the DICOM standard, Part 10 Section 7.1, any DICOM file shall contain
    a 128-byte preamble, a 4-byte DICOM prefix 'DICM' and (at a minimum) the
    following Type 1 DICOM Elements (from Table 7.1-1):
        * (0002,0000) FileMetaInformationGroupLength, UL, 4
        * (0002,0001) FileMetaInformationVersion, OB, 2
        * (0002,0002) MediaStorageSOPClassUID, UI, N
        * (0002,0003) MediaStorageSOPInstanceUID, UI, N
        * (0002,0010) TransferSyntaxUID, UI, N
        * (0002,0012) ImplementationClassUID, UI, N

    If `enforce_standard` is True then (0002,0000) will be added/updated,
    (0002,0001) and (0002,0012) will be added if not already present and the
    other required elements will be checked to see if they exist. If
    `enforce_standard` is False then `file_meta` will be written as is after
    minimal validation checking.

    The following Type 3/1C Elements may also be present:
        * (0002,0013) ImplementationVersionName, SH, N
        * (0002,0016) SourceApplicationEntityTitle, AE, N
        * (0002,0017) SendingApplicationEntityTitle, AE, N
        * (0002,0018) ReceivingApplicationEntityTitle, AE, N
        * (0002,0100) PrivateInformationCreatorUID, UI, N
        * (0002,0102) PrivateInformation, OB, N

    If `enforce_standard` is True then (0002,0013) will be added/updated.

    Encoding
    ~~~~~~~~
    The encoding of the File Meta Information shall be Explicit VR Little
    Endian

    Parameters
    ----------
    fp : file-like
        The file-like to write the File Meta Information to.
    file_meta : pydicom.dataset.Dataset
        The File Meta Information DataElements.
    enforce_standard : bool
        If False, then only the File Meta Information elements already in
        `file_meta` will be written to `fp`. If True (default) then a DICOM
        Standards conformant File Meta will be written to `fp`.

    Raises
    ------
    ValueError
        If `enforce_standard` is True and any of the required File Meta
        Information elements are missing from `file_meta`, with the
        exception of (0002,0000), (0002,0001) and (0002,0012).
    ValueError
        If any non-Group 2 Elements are present in `file_meta`.
    """
    validate_file_meta(file_meta, enforce_standard)

    if enforce_standard and 'FileMetaInformationGroupLength' not in file_meta:
        # Will be updated with the actual length later
        file_meta.FileMetaInformationGroupLength = 0

    # Write the File Meta Information Group elements
    # first write into a buffer to avoid seeking back, that can be
    # expansive and is not allowed if writing into a zip file
    buffer = DicomBytesIO()
    buffer.is_little_endian = True
    buffer.is_implicit_VR = False
    write_dataset(buffer, file_meta)

    # If FileMetaInformationGroupLength is present it will be the first written
    #   element and we must update its value to the correct length.
    if 'FileMetaInformationGroupLength' in file_meta:
        # Update the FileMetaInformationGroupLength value, which is the number
        #   of bytes from the end of the FileMetaInformationGroupLength element
        #   to the end of all the File Meta Information elements.
        # FileMetaInformationGroupLength has a VR of 'UL' and so has a value
        #   that is 4 bytes fixed. The total length of when encoded as
        #   Explicit VR must therefore be 12 bytes.
        file_meta.FileMetaInformationGroupLength = buffer.tell() - 12
        buffer.seek(0)
        write_data_element(buffer, file_meta[0x00020000])

    fp.write(buffer.getvalue())
开发者ID:pieper,项目名称:pydicom,代码行数:90,代码来源:filewriter.py

示例5: TestWriteFileMetaInfo

# 需要导入模块: from pydicom.filebase import DicomBytesIO [as 别名]
# 或者: from pydicom.filebase.DicomBytesIO import seek [as 别名]
class TestWriteFileMetaInfo(unittest.TestCase):
    """Unit tests for _write_file_meta_info."""
    def setUp(self):
        """Create an empty file-like for use in testing."""
        self.fp = DicomBytesIO()

    def test_bad_elements(self):
        """Test that non-group 2 elements aren't written to the file meta."""
        meta = Dataset()
        meta.PatientID = '12345678'
        meta.MediaStorageSOPClassUID = '1.1'
        meta.MediaStorageSOPInstanceUID = '1.2'
        meta.TransferSyntaxUID = '1.3'
        meta.ImplementationClassUID = '1.4'
        self.assertRaises(ValueError, _write_file_meta_info, self.fp, meta)

    def test_missing_elements(self):
        """Test that missing required elements raises ValueError."""
        meta = Dataset()
        self.assertRaises(ValueError, _write_file_meta_info, self.fp, meta)
        meta.MediaStorageSOPClassUID = '1.1'
        self.assertRaises(ValueError, _write_file_meta_info, self.fp, meta)
        meta.MediaStorageSOPInstanceUID = '1.2'
        self.assertRaises(ValueError, _write_file_meta_info, self.fp, meta)
        meta.TransferSyntaxUID = '1.3'
        self.assertRaises(ValueError, _write_file_meta_info, self.fp, meta)
        meta.ImplementationClassUID = '1.4'
        _write_file_meta_info(self.fp, meta)

    def test_prefix(self):
        """Test that the 'DICM' prefix is present."""
        meta = Dataset()
        meta.MediaStorageSOPClassUID = '1.1'
        meta.MediaStorageSOPInstanceUID = '1.2'
        meta.TransferSyntaxUID = '1.3'
        meta.ImplementationClassUID = '1.4'
        _write_file_meta_info(self.fp, meta)

        self.fp.seek(0)
        prefix = self.fp.read(4)
        self.assertEqual(prefix, b'DICM')

    def test_group_length(self):
        """Test that the value for FileMetaInformationGroupLength is OK."""
        meta = Dataset()
        meta.MediaStorageSOPClassUID = '1.1'
        meta.MediaStorageSOPInstanceUID = '1.2'
        meta.TransferSyntaxUID = '1.3'
        meta.ImplementationClassUID = '1.4'
        _write_file_meta_info(self.fp, meta)

        # 78 in total, - 4 for prefix, - 12 for group length = 62
        self.fp.seek(12)
        self.assertEqual(self.fp.read(4), b'\x3E\x00\x00\x00')

    def test_group_length_updated(self):
        """Test that FileMetaInformationGroupLength gets updated if present."""
        meta = Dataset()
        meta.FileMetaInformationGroupLength = 100 # Actual length
        meta.MediaStorageSOPClassUID = '1.1'
        meta.MediaStorageSOPInstanceUID = '1.2'
        meta.TransferSyntaxUID = '1.3'
        meta.ImplementationClassUID = '1.4'
        _write_file_meta_info(self.fp, meta)

        self.fp.seek(12)
        self.assertEqual(self.fp.read(4), b'\x3E\x00\x00\x00')
        # Check original file meta is unchanged/updated
        self.assertEqual(meta.FileMetaInformationGroupLength, 62)
        self.assertEqual(meta.FileMetaInformationVersion, b'\x00\x01')
        self.assertEqual(meta.MediaStorageSOPClassUID, '1.1')
        self.assertEqual(meta.MediaStorageSOPInstanceUID, '1.2')
        self.assertEqual(meta.TransferSyntaxUID, '1.3')
        self.assertEqual(meta.ImplementationClassUID, '1.4')

    def test_version(self):
        """Test that the value for FileMetaInformationVersion is OK."""
        meta = Dataset()
        meta.MediaStorageSOPClassUID = '1.1'
        meta.MediaStorageSOPInstanceUID = '1.2'
        meta.TransferSyntaxUID = '1.3'
        meta.ImplementationClassUID = '1.4'
        _write_file_meta_info(self.fp, meta)

        self.fp.seek(16 + 12)
        self.assertEqual(self.fp.read(2), b'\x00\x01')

    def test_filelike_position(self):
        """Test that the file-like's ending position is OK."""
        # 4 bytes prefix
        # 8 + 4 bytes FileMetaInformationGroupLength
        # 12 + 2 bytes FileMetaInformationVersion
        # 8 + 4 bytes MediaStorageSOPClassUID
        # 8 + 4 bytes MediaStorageSOPInstanceUID
        # 8 + 4 bytes TransferSyntaxUID
        # 8 + 4 bytes ImplementationClassUID
        # 78 bytes total
        meta = Dataset()
        meta.MediaStorageSOPClassUID = '1.1'
        meta.MediaStorageSOPInstanceUID = '1.2'
#.........这里部分代码省略.........
开发者ID:bastula,项目名称:pydicom,代码行数:103,代码来源:test_filewriter.py


注:本文中的pydicom.filebase.DicomBytesIO.seek方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。