code: def test_best_write_scale_ftype():
      # Test best write scaling type
      # Types return better of (default, array type) unless scale overflows.
      # Return float type cannot be less capable than the input array type
      for dtt in IUINT_TYPES + FLOAT_TYPES:
          arr = np.arange(10, dtype=dtt)
          assert best_write_scale_ftype(arr, 1, 0) == better_float_of(
              dtt, np.float32
          )
          assert best_write_scale_ftype(
              arr, 1, 0, np.float64
          ) == better_float_of(dtt, np.float64)
          assert best_write_scale_ftype(
              arr, np.float32(2), 0
          ) == better_float_of(dtt, np.float32)
          assert best_write_scale_ftype(
              arr, 1, np.float32(1)
          ) == better_float_of(dtt, np.float32)
      # Overflowing ints with scaling results in upcast
      best_vals = ((np.float32, np.float64),)
      if np.longdouble in OK_FLOATS:
          best_vals += ((np.float64, np.longdouble),)
      for lower_t, higher_t in best_vals:
          # Information on this float
          L_info = type_info(lower_t)
          t_max = L_info["max"]
          nmant = L_info["nmant"]  # number of significand digits
          big_delta = lower_t(
              2 ** (floor_log2(t_max) - nmant)
          )  # delta below max
          # Even large values that don't overflow don't change output
          arr = np.array([0, t_max], dtype=lower_t)
          assert best_write_scale_ftype(arr, 1, 0) == lower_t
          # Scaling > 1 reduces output values, so no upcast needed
          assert (
              best_write_scale_ftype(arr, lower_t(1.01), 0) == lower_t
          )
          # Scaling < 1 increases values, so upcast may be needed (and is here)
          assert (
              best_write_scale_ftype(arr, lower_t(0.99), 0) == higher_t
          )
          # Large minus offset on large array can cause upcast
          assert (
              best_write_scale_ftype(arr, 1, -big_delta / 2.01)
              == lower_t
          )
          assert (
              best_write_scale_ftype(arr, 1, -big_delta / 2.0)
              == higher_t
          )
          # With infs already in input, default type returns
          arr[0] = np.inf
          assert best_write_scale_ftype(arr, lower_t(0.5), 0) == lower_t
          arr[0] = -np.inf
          assert best_write_scale_ftype(arr, lower_t(0.5), 0) == lower_t
  
imports: import bz2
  import functools
  import gzip
  import itertools
  import os
  import tempfile
  import threading
  import time
  import warnings
  from io import BytesIO
  from os.path import exists
  import numpy
  import pytest
  from numpy.testing import assert_array_almost_equal, assert_array_equal
  from packaging.version import Version
  from nibabel.testing import assert_allclose_safely, assert_dt_equal, error_warnings, suppress_warnings
  from ..casting import OK_FLOATS, floor_log2, sctypes, shared_range, type_info
  from ..openers import BZ2File, ImageOpener, Opener
  from ..optpkg import optional_package
  from ..tmpdirs import InTemporaryDirectory
  from ..volumeutils import _dt_min_max, _ftype4scaled_finite, _is_compressed_fobj, _write_data, apply_read_scaling, array_from_file, array_to_file, best_write_scale_ftype, better_float_of, fname_ext_ul_case, int_scinter_ftype, make_dt_codes, native_code, rec2dict, seek_tell, shape_zoom_affine, working_type, write_zeros
label_desc: Use image node_id 32 for calling a global function with example usage: test_best_write_scale_ftype() without return types