code: def test_keep_file_open_true_false_invalid():
      # Test the behaviour of the keep_file_open __init__ flag, when it is set to
      # True or False. Expected behaviour is as follows:
      # keep_open | igzip present    | persist ImageOpener | igzip.drop_handles
      #           | and is gzip file |                     |
      # ----------|------------------|---------------------|-------------------
      # False     | False            | False               | n/a
      # False     | True             | True                | True
      # True      | False            | True                | n/a
      # True      | True             | True                | False
      #
      # Each test tuple contains:
      #  - file type - gzipped ('gz') or not ('bin'), or an open file handle
      #    ('open')
      #  - keep_file_open value passed to ArrayProxy
      #  - whether or not indexed_gzip is present
      #  - expected value for internal ArrayProxy._persist_opener flag
      #  - expected value for internal ArrayProxy._keep_file_open flag
      tests = [
          # open file handle - kfo and have_igzip are both irrelevant
          ("open", False, False, False, False),
          ("open", False, True, False, False),
          ("open", True, False, False, False),
          ("open", True, True, False, False),
          # non-gzip file - have_igzip is irrelevant, decision should be made
          # solely from kfo flag
          ("bin", False, False, False, False),
          ("bin", False, True, False, False),
          ("bin", True, False, True, True),
          ("bin", True, True, True, True),
          # gzip file. If igzip is present, we persist the ImageOpener.
          ("gz", False, False, False, False),
          ("gz", False, True, True, False),
          ("gz", True, False, True, True),
          ("gz", True, True, True, True),
      ]
  
      dtype = np.float32
      data = np.arange(1000, dtype=dtype).reshape((10, 10, 10))
      voxels = np.random.randint(0, 10, (10, 3))
  
      for test in tests:
          filetype, kfo, have_igzip, exp_persist, exp_kfo = test
          with InTemporaryDirectory(), mock.patch(
              "nibabel.openers.ImageOpener", CountingImageOpener
          ), patch_indexed_gzip(have_igzip):
              fname = f"testdata.{filetype}"
              # create the test data file
              if filetype == "gz":
                  with gzip.open(fname, "wb") as fobj:
                      fobj.write(data.tobytes(order="F"))
              else:
                  with open(fname, "wb") as fobj:
                      fobj.write(data.tobytes(order="F"))
              # pass in a file name or open file handle. If the latter, we open
              # two file handles, because we're going to create two proxies
              # below.
              if filetype == "open":
                  fobj1 = open(fname, "rb")
                  fobj2 = open(fname, "rb")
              else:
                  fobj1 = fname
                  fobj2 = fname
              try:
                  proxy = ArrayProxy(
                      fobj1, ((10, 10, 10), dtype), keep_file_open=kfo
                  )
                  # We also test that we get the same behaviour when the
                  # KEEP_FILE_OPEN_DEFAULT flag is changed
                  with patch_keep_file_open_default(kfo):
                      proxy_def = ArrayProxy(
                          fobj2, ((10, 10, 10), dtype)
                      )
                  # check internal flags
                  assert proxy._persist_opener == exp_persist
                  assert proxy._keep_file_open == exp_kfo
                  assert proxy_def._persist_opener == exp_persist
                  assert proxy_def._keep_file_open == exp_kfo
                  # check persist_opener behaviour - whether one imageopener is
                  # created for the lifetime of the ArrayProxy, or one is
                  # created on each access
                  if exp_persist:
                      assert (
                          _count_ImageOpeners(proxy, data, voxels) == 1
                      )
                      assert (
                          _count_ImageOpeners(proxy_def, data, voxels)
                          == 1
                      )
                  else:
                      assert (
                          _count_ImageOpeners(proxy, data, voxels) == 10
                      )
                      assert (
                          _count_ImageOpeners(proxy_def, data, voxels)
                          == 10
                      )
                  # if indexed_gzip is active, check that the file object was
                  # created correctly - the _opener.fobj will be a
                  # MockIndexedGzipFile, defined in test_openers.py
                  if filetype == "gz" and have_igzip:
                      assert proxy._opener.fobj._drop_handles == (
                          not exp_kfo
                      )
                  # if we were using an open file handle, check that the proxy
                  # didn't close it
                  if filetype == "open":
                      assert not fobj1.closed
                      assert not fobj2.closed
              finally:
                  del proxy
                  del proxy_def
                  if filetype == "open":
                      fobj1.close()
                      fobj2.close()
      # Test invalid values of keep_file_open
      with InTemporaryDirectory():
          fname = "testdata"
          with open(fname, "wb") as fobj:
              fobj.write(data.tobytes(order="F"))
  
          for invalid_kfo in (55, "auto", "cauto"):
              with pytest.raises(ValueError):
                  ArrayProxy(
                      fname,
                      ((10, 10, 10), dtype),
                      keep_file_open=invalid_kfo,
                  )
              with patch_keep_file_open_default(invalid_kfo):
                  with pytest.raises(ValueError):
                      ArrayProxy(fname, ((10, 10, 10), dtype))
  
imports: import contextlib
  import gzip
  import pickle
  from io import BytesIO
  from unittest import mock
  import numpy
  import pytest
  from numpy.testing import assert_array_almost_equal, assert_array_equal
  from packaging.version import Version
  from ..None import __version__
  from ..arrayproxy import ArrayProxy, get_obj_dtype, is_proxy, reshape_dataobj
  from ..deprecator import ExpiredDeprecationError
  from ..nifti1 import Nifti1Header, Nifti1Image
  from ..openers import ImageOpener
  from ..testing import memmap_after_ufunc
  from ..tmpdirs import InTemporaryDirectory
  from .test_fileslice import slicer_samples
  from .test_openers import patch_indexed_gzip
label_desc: Use image node_id 18 for calling a global function with example usage: test_keep_file_open_true_false_invalid() without return types