code: def test_scaling_needed():
      # Structured types return True if dtypes same, raise error otherwise
      dt_def = [("f", "i4")]
      arr = np.ones(10, dt_def)
      for t in NUMERIC_TYPES:
          with pytest.raises(WriterError):
              ArrayWriter(arr, t)
          narr = np.ones(10, t)
          with pytest.raises(WriterError):
              ArrayWriter(narr, dt_def)
      assert not ArrayWriter(arr).scaling_needed()
      assert not ArrayWriter(arr, dt_def).scaling_needed()
      # Any numeric type that can cast, needs no scaling
      for in_t in NUMERIC_TYPES:
          for out_t in NUMERIC_TYPES:
              if np.can_cast(in_t, out_t):
                  aw = ArrayWriter(np.ones(10, in_t), out_t)
                  assert not aw.scaling_needed()
      for in_t in NUMERIC_TYPES:
          # Numeric types to complex never need scaling
          arr = np.ones(10, in_t)
          for out_t in COMPLEX_TYPES:
              assert not ArrayWriter(arr, out_t).scaling_needed()
      # Attempts to scale from complex to anything else fails
      for in_t in COMPLEX_TYPES:
          for out_t in FLOAT_TYPES + IUINT_TYPES:
              arr = np.ones(10, in_t)
              with pytest.raises(WriterError):
                  ArrayWriter(arr, out_t)
      # Scaling from anything but complex to floats is OK
      for in_t in FLOAT_TYPES + IUINT_TYPES:
          arr = np.ones(10, in_t)
          for out_t in FLOAT_TYPES:
              assert not ArrayWriter(arr, out_t).scaling_needed()
      # For any other output type, arrays with no data don't need scaling
      for in_t in FLOAT_TYPES + IUINT_TYPES:
          arr_0 = np.zeros(10, in_t)
          arr_e = []
          for out_t in IUINT_TYPES:
              assert not ArrayWriter(arr_0, out_t).scaling_needed()
              assert not ArrayWriter(arr_e, out_t).scaling_needed()
      # Going to (u)ints, non-finite arrays don't need scaling for writers that
      # can do scaling because these use finite_range to threshold the input data,
      # but ArrayWriter does not do this. so scaling_needed is True
      for in_t in FLOAT_TYPES:
          arr_nan = np.zeros(10, in_t) + np.nan
          arr_inf = np.zeros(10, in_t) + np.inf
          arr_minf = np.zeros(10, in_t) - np.inf
          arr_mix = np.array([np.nan, np.inf, -np.inf], dtype=in_t)
          for out_t in IUINT_TYPES:
              for arr in (arr_nan, arr_inf, arr_minf, arr_mix):
                  assert ArrayWriter(
                      arr, out_t, check_scaling=False
                  ).scaling_needed()
                  assert not SlopeArrayWriter(
                      arr, out_t
                  ).scaling_needed()
                  assert not SlopeInterArrayWriter(
                      arr, out_t
                  ).scaling_needed()
      # Floats as input always need scaling
      for in_t in FLOAT_TYPES:
          arr = np.ones(10, in_t)
          for out_t in IUINT_TYPES:
              # We need an arraywriter that will tolerate construction when
              # scaling is needed
              assert SlopeArrayWriter(arr, out_t).scaling_needed()
      # in-range (u)ints don't need scaling
      for in_t in IUINT_TYPES:
          in_info = np.iinfo(in_t)
          in_min, in_max = in_info.min, in_info.max
          for out_t in IUINT_TYPES:
              out_info = np.iinfo(out_t)
              out_min, out_max = out_info.min, out_info.max
              if in_min >= out_min and in_max <= out_max:
                  arr = np.array([in_min, in_max], in_t)
                  assert np.can_cast(arr.dtype, out_t)
                  # We've already tested this with can_cast above, but...
                  assert not ArrayWriter(arr, out_t).scaling_needed()
                  continue
              # The output data type does not include the input data range
              max_min = max(
                  in_min, out_min
              )  # 0 for input or output uint
              min_max = min(in_max, out_max)
              arr = np.array([max_min, min_max], in_t)
              assert not ArrayWriter(arr, out_t).scaling_needed()
              assert SlopeInterArrayWriter(
                  arr + 1, out_t
              ).scaling_needed()
              if in_t in INT_TYPES:
                  assert SlopeInterArrayWriter(
                      arr - 1, out_t
                  ).scaling_needed()
  
imports: import itertools
  from io import BytesIO
  from platform import machine, python_compiler
  import numpy
  import pytest
  from numpy.testing import assert_array_almost_equal, assert_array_equal
  from ..arraywriters import ArrayWriter, ScalingError, SlopeArrayWriter, SlopeInterArrayWriter, WriterError, get_slope_inter, make_array_writer
  from ..casting import int_abs, sctypes, shared_range, type_info
  from ..testing import assert_allclose_safely, suppress_warnings
  from ..volumeutils import _dt_min_max, apply_read_scaling, array_from_file
label_desc: Use image node_id 5 for calling a global function with example usage: test_scaling_needed() without return types