code: def test_read_write_data(self):
      # Check reading and writing of data
      hdr = self.header_class()
      # Trying to read data from an empty header gives no data
      bytes = hdr.data_from_fileobj(BytesIO())
      assert len(bytes) == 0
      # Setting no data into an empty header results in - no data
      str_io = BytesIO()
      hdr.data_to_fileobj([], str_io)
      assert str_io.getvalue() == b""
      # Setting more data then there should be gives an error
      with pytest.raises(HeaderDataError):
          hdr.data_to_fileobj(np.zeros(3), str_io)
      # Test valid write
      hdr.set_data_shape((1, 2, 3))
      hdr.set_data_dtype(np.float32)
      S = BytesIO()
      data = np.arange(6, dtype=np.float64)
      # data have to be the right shape
      with pytest.raises(HeaderDataError):
          hdr.data_to_fileobj(data, S)
      data = data.reshape((1, 2, 3))
      # and size
      with pytest.raises(HeaderDataError):
          hdr.data_to_fileobj(data[:, :, :-1], S)
      with pytest.raises(HeaderDataError):
          hdr.data_to_fileobj(data[:, :-1, :], S)
      # OK if so
      hdr.data_to_fileobj(data, S)
      # Read it back
      data_back = hdr.data_from_fileobj(S)
      # Should be about the same
      assert_array_almost_equal(data, data_back)
      # but with the header dtype, not the data dtype
      assert hdr.get_data_dtype() == data_back.dtype
      # this is with native endian, not so for swapped
      S2 = BytesIO()
      hdr2 = hdr.as_byteswapped()
      hdr2.set_data_dtype(np.float32)
      hdr2.set_data_shape((1, 2, 3))
      hdr2.data_to_fileobj(data, S2)
      data_back2 = hdr2.data_from_fileobj(S2)
      # Compares the same
      assert_array_almost_equal(data_back, data_back2)
      # Same dtype names
      assert data_back.dtype.name == data_back2.dtype.name
      # But not the same endianness
      assert data.dtype.byteorder != data_back2.dtype.byteorder
      # Try scaling down to integer
      hdr.set_data_dtype(np.uint8)
      S3 = BytesIO()
      # Analyze header cannot do scaling, so turn off scaling with
      # 'rescale=False'
      with np.errstate(invalid="ignore"):
          hdr.data_to_fileobj(data, S3, rescale=False)
      data_back = hdr.data_from_fileobj(S3)
      assert_array_almost_equal(data, data_back)
      # If the header can't do scaling, rescale raises an error
      if not hdr.has_data_slope:
          with pytest.raises(HeaderTypeError):
              hdr.data_to_fileobj(data, S3)
          with pytest.raises(HeaderTypeError):
              hdr.data_to_fileobj(data, S3, rescale=True)
      # If not scaling we lose precision from rounding
      data = np.arange(6, dtype=np.float64).reshape((1, 2, 3)) + 0.5
      with np.errstate(invalid="ignore"):
          hdr.data_to_fileobj(data, S3, rescale=False)
      data_back = hdr.data_from_fileobj(S3)
      assert not np.allclose(data, data_back)
      # Test RGB image
      dtype = np.dtype([("R", "uint8"), ("G", "uint8"), ("B", "uint8")])
      data = np.ones((1, 2, 3), dtype)
      hdr.set_data_dtype(dtype)
      S4 = BytesIO()
      hdr.data_to_fileobj(data, S4)
      data_back = hdr.data_from_fileobj(S4)
      assert_array_equal(data, data_back)
  
imports: import itertools
  import logging
  import os
  import pickle
  import re
  from io import BytesIO, StringIO
  import numpy
  import pytest
  from numpy.testing import assert_array_almost_equal, assert_array_equal
  from ..None import imageglobals
  from ..analyze import AnalyzeHeader, AnalyzeImage
  from ..arraywriters import WriterError
  from ..casting import sctypes_aliases
  from ..nifti1 import Nifti1Header
  from ..optpkg import optional_package
  from ..spatialimages import HeaderDataError, HeaderTypeError, supported_np_types
  from ..testing import assert_dt_equal, bytesio_filemap, bytesio_round_trip, data_path, suppress_warnings
  from ..tmpdirs import InTemporaryDirectory
  from .None import test_spatialimages
  from .None import test_wrapstruct
label_desc: Use image node_id 15 for calling the TestAnalyzeHeader obj's underlying member method code with example usage: obj.test_read_write_data() without return types