code: def test_casting():
      for ft in sctypes["float"]:
          for it in sctypes["int"] + sctypes["uint"]:
              ii = np.iinfo(it)
              arr = [
                  ii.min - 1,
                  ii.max + 1,
                  -np.inf,
                  np.inf,
                  np.nan,
                  0.2,
                  10.6,
              ]
              farr_orig = np.array(arr, dtype=ft)
              # We're later going to test if we modify this array
              farr = farr_orig.copy()
              mn, mx = shared_range(ft, it)
              with np.errstate(invalid="ignore"):
                  iarr = float_to_int(farr, it)
              exp_arr = np.array([mn, mx, mn, mx, 0, 0, 11], dtype=it)
              assert_array_equal(iarr, exp_arr)
              # Now test infmax version
              with np.errstate(invalid="ignore"):
                  iarr = float_to_int(farr, it, infmax=True)
                  im_exp = np.array(
                      [mn, mx, ii.min, ii.max, 0, 0, 11], dtype=it
                  )
              # Float16 can overflow to infs
              if farr[0] == -np.inf:
                  im_exp[0] = ii.min
              if farr[1] == np.inf:
                  im_exp[1] = ii.max
              assert_array_equal(iarr, im_exp)
              # NaNs, with nan2zero False, gives error
              with pytest.raises(CastingError):
                  float_to_int(farr, it, False)
              # We can pass through NaNs if we really want
              exp_arr[arr.index(np.nan)] = ft(np.nan).astype(it)
              with np.errstate(invalid="ignore"):
                  iarr = float_to_int(farr, it, nan2zero=None)
              assert_array_equal(iarr, exp_arr)
              # Confirm input array is not modified
              nans = np.isnan(farr)
              assert_array_equal(nans, np.isnan(farr_orig))
              assert_array_equal(
                  farr[nans == False], farr_orig[nans == False]
              )
      # Test scalars work and return scalars
      assert_array_equal(float_to_int(np.float32(0), np.int16), [0])
      # Test scalar nan OK
      with np.errstate(invalid="ignore"):
          assert_array_equal(float_to_int(np.nan, np.int16), [0])
      # Test nans give error if not nan2zero
      with pytest.raises(CastingError):
          float_to_int(np.nan, np.int16, False)
  
file_path: nibabel/nibabel/tests/test_casting.py
imports: import os
  from platform import machine
  import numpy
  import pytest
  from numpy.testing import assert_array_equal
  from ..casting import CastingError, able_int_type, best_float, float_to_int, floor_log2, int_abs, longdouble_precision_improved, sctypes, shared_range, ulp
  from ..testing import suppress_warnings
label_desc: Use image node_id 3 for calling a global function with example usage: test_casting() without return types