code: def test_a2f_nan2zero_range():
      # array_to_file should check if nan can be represented as zero
      # This comes about when the writer can't write the value (-intercept /
      # divslope) because it does not fit in the output range.  Input clipping
      # should not affect this
      fobj = BytesIO()
      # No problem for input integer types - they don't have NaNs
      for dt in INT_TYPES:
          arr_no_nan = np.array([-1, 0, 1, 2], dtype=dt)
          # No errors from explicit thresholding (nor for input float types)
          back_arr = write_return(
              arr_no_nan, fobj, np.int8, mn=1, nan2zero=True
          )
          assert_array_equal([1, 1, 1, 2], back_arr)
          back_arr = write_return(
              arr_no_nan, fobj, np.int8, mx=-1, nan2zero=True
          )
          assert_array_equal([-1, -1, -1, -1], back_arr)
          # Pushing zero outside the output data range does not generate error
          back_arr = write_return(
              arr_no_nan, fobj, np.int8, intercept=129, nan2zero=True
          )
          assert_array_equal([-128, -128, -128, -127], back_arr)
          back_arr = write_return(
              arr_no_nan,
              fobj,
              np.int8,
              intercept=257.1,
              divslope=2,
              nan2zero=True,
          )
          assert_array_equal([-128, -128, -128, -128], back_arr)
      for dt in CFLOAT_TYPES:
          arr = np.array([-1, 0, 1, np.nan], dtype=dt)
          # Error occurs for arrays without nans too
          arr_no_nan = np.array([-1, 0, 1, 2], dtype=dt)
          complex_warn = (
              (ComplexWarning,)
              if np.issubdtype(dt, np.complexfloating)
              else ()
          )
          # Casting nan to int will produce a RuntimeWarning in numpy 1.24
          nan_warn = (RuntimeWarning,) if FP_RUNTIME_WARN else ()
          c_and_n_warn = complex_warn + nan_warn
          # No errors from explicit thresholding
          # mn thresholding excluding zero
          with pytest.warns(
              complex_warn
          ) if complex_warn else error_warnings():
              assert_array_equal(
                  [1, 1, 1, 0], write_return(arr, fobj, np.int8, mn=1)
              )
          # mx thresholding excluding zero
          with pytest.warns(
              complex_warn
          ) if complex_warn else error_warnings():
              assert_array_equal(
                  [-1, -1, -1, 0],
                  write_return(arr, fobj, np.int8, mx=-1),
              )
          # Errors from datatype threshold after scaling
          with pytest.warns(
              complex_warn
          ) if complex_warn else error_warnings():
              back_arr = write_return(arr, fobj, np.int8, intercept=128)
          assert_array_equal([-128, -128, -127, -128], back_arr)
          with pytest.raises(ValueError):
              write_return(arr, fobj, np.int8, intercept=129)
          with pytest.raises(ValueError):
              write_return(arr_no_nan, fobj, np.int8, intercept=129)
          # OK with nan2zero false, but we get whatever nan casts to
          with pytest.warns(
              c_and_n_warn
          ) if c_and_n_warn else error_warnings():
              nan_cast = np.array(np.nan, dtype=dt).astype(np.int8)
          with pytest.warns(
              c_and_n_warn
          ) if c_and_n_warn else error_warnings():
              back_arr = write_return(
                  arr, fobj, np.int8, intercept=129, nan2zero=False
              )
          assert_array_equal([-128, -128, -128, nan_cast], back_arr)
          # divslope
          with pytest.warns(
              complex_warn
          ) if complex_warn else error_warnings():
              back_arr = write_return(
                  arr, fobj, np.int8, intercept=256, divslope=2
              )
          assert_array_equal([-128, -128, -128, -128], back_arr)
          with pytest.raises(ValueError):
              write_return(
                  arr, fobj, np.int8, intercept=257.1, divslope=2
              )
          with pytest.raises(ValueError):
              write_return(
                  arr_no_nan, fobj, np.int8, intercept=257.1, divslope=2
              )
          # OK with nan2zero false
          with pytest.warns(
              c_and_n_warn
          ) if c_and_n_warn else error_warnings():
              back_arr = write_return(
                  arr,
                  fobj,
                  np.int8,
                  intercept=257.1,
                  divslope=2,
                  nan2zero=False,
              )
          assert_array_equal([-128, -128, -128, nan_cast], back_arr)
  
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 23 for calling a global function with example usage: test_a2f_nan2zero_range() without return types