code: def test_optimize_slicer():
      # Analyze slice for fullness, contiguity, direction
      #
      # If all_full:
      # - make positive slicer
      # - decide if worth reading continuous block
      # - if so, modify as_read, as_returned accordingly, set contiguous / full
      # - if not, fill as_read for non-contiguous case
      # If not all_full
      # - make positive slicer
      for all_full in (True, False):
          for heuristic in (_always, _never, _partial):
              for is_slowest in (True, False):
                  # following tests not affected by all_full or optimization
                  # full - always passes through
                  assert optimize_slicer(
                      slice(None),
                      10,
                      all_full,
                      is_slowest,
                      4,
                      heuristic,
                  ) == (
                      slice(None),
                      slice(None),
                  )
                  # Even if full specified with explicit values
                  assert optimize_slicer(
                      slice(10), 10, all_full, is_slowest, 4, heuristic
                  ) == (
                      slice(None),
                      slice(None),
                  )
                  assert optimize_slicer(
                      slice(0, 10),
                      10,
                      all_full,
                      is_slowest,
                      4,
                      heuristic,
                  ) == (
                      slice(None),
                      slice(None),
                  )
                  assert optimize_slicer(
                      slice(0, 10, 1),
                      10,
                      all_full,
                      is_slowest,
                      4,
                      heuristic,
                  ) == (slice(None), slice(None))
                  # Reversed full is still full, but with reversed post_slice
                  assert optimize_slicer(
                      slice(None, None, -1),
                      10,
                      all_full,
                      is_slowest,
                      4,
                      heuristic,
                  ) == (slice(None), slice(None, None, -1))
      # Contiguous is contiguous unless heuristic kicks in, in which case it may
      # be 'full'
      assert optimize_slicer(
          slice(9), 10, False, False, 4, _always
      ) == (slice(0, 9, 1), slice(None))
      assert optimize_slicer(slice(9), 10, True, False, 4, _always) == (
          slice(None),
          slice(0, 9, 1),
      )
      # Unless this is the slowest dimension, and all_true is True, in which case
      # we don't update to full
      assert optimize_slicer(slice(9), 10, True, True, 4, _always) == (
          slice(0, 9, 1),
          slice(None),
      )
      # Nor if the heuristic won't update
      assert optimize_slicer(slice(9), 10, True, False, 4, _never) == (
          slice(0, 9, 1),
          slice(None),
      )
      assert optimize_slicer(
          slice(1, 10), 10, True, False, 4, _never
      ) == (
          slice(1, 10, 1),
          slice(None),
      )
      # Reversed contiguous still contiguous
      assert optimize_slicer(
          slice(8, None, -1), 10, False, False, 4, _never
      ) == (
          slice(0, 9, 1),
          slice(None, None, -1),
      )
      assert optimize_slicer(
          slice(8, None, -1), 10, True, False, 4, _always
      ) == (
          slice(None),
          slice(8, None, -1),
      )
      assert optimize_slicer(
          slice(8, None, -1), 10, False, False, 4, _never
      ) == (
          slice(0, 9, 1),
          slice(None, None, -1),
      )
      assert optimize_slicer(
          slice(9, 0, -1), 10, False, False, 4, _never
      ) == (
          slice(1, 10, 1),
          slice(None, None, -1),
      )
      # Non-contiguous
      assert optimize_slicer(
          slice(0, 10, 2), 10, False, False, 4, _never
      ) == (
          slice(0, 10, 2),
          slice(None),
      )
      # all_full triggers optimization, but optimization does nothing
      assert optimize_slicer(
          slice(0, 10, 2), 10, True, False, 4, _never
      ) == (
          slice(0, 10, 2),
          slice(None),
      )
      # all_full triggers optimization, optimization does something
      assert optimize_slicer(
          slice(0, 10, 2), 10, True, False, 4, _always
      ) == (
          slice(None),
          slice(0, 10, 2),
      )
      # all_full disables optimization, optimization does something
      assert optimize_slicer(
          slice(0, 10, 2), 10, False, False, 4, _always
      ) == (
          slice(0, 10, 2),
          slice(None),
      )
      # Non contiguous, reversed
      assert optimize_slicer(
          slice(10, None, -2), 10, False, False, 4, _never
      ) == (
          slice(1, 10, 2),
          slice(None, None, -1),
      )
      assert optimize_slicer(
          slice(10, None, -2), 10, True, False, 4, _always
      ) == (
          slice(None),
          slice(9, None, -2),
      )
      # Short non-contiguous
      assert optimize_slicer(
          slice(2, 8, 2), 10, False, False, 4, _never
      ) == (
          slice(2, 8, 2),
          slice(None),
      )
      # with partial read
      assert optimize_slicer(
          slice(2, 8, 2), 10, True, False, 4, _partial
      ) == (
          slice(2, 8, 1),
          slice(None, None, 2),
      )
      # If this is the slowest changing dimension, heuristic can upgrade None to
      # contiguous, but not (None, contiguous) to full
      # we've done this one already
      assert optimize_slicer(
          slice(0, 10, 2), 10, True, False, 4, _always
      ) == (
          slice(None),
          slice(0, 10, 2),
      )
      # if slowest, just upgrade to contiguous
      assert optimize_slicer(
          slice(0, 10, 2), 10, True, True, 4, _always
      ) == (
          slice(0, 10, 1),
          slice(None, None, 2),
      )
      # contiguous does not upgrade to full
      assert optimize_slicer(slice(9), 10, True, True, 4, _always) == (
          slice(0, 9, 1),
          slice(None),
      )
      # integer
      assert optimize_slicer(0, 10, True, False, 4, _never) == (
          0,
          "dropped",
      )
      # can be negative
      assert optimize_slicer(-1, 10, True, False, 4, _never) == (
          9,
          "dropped",
      )
      # or float
      assert optimize_slicer(0.9, 10, True, False, 4, _never) == (
          0,
          "dropped",
      )
      # should never get 'contiguous'
      with pytest.raises(ValueError):
          optimize_slicer(0, 10, True, False, 4, _partial)
      # full can be forced with heuristic
      assert optimize_slicer(0, 10, True, False, 4, _always) == (
          slice(None),
          0,
      )
      # but disabled for slowest changing dimension
      assert optimize_slicer(0, 10, True, True, 4, _always) == (
          0,
          "dropped",
      )
  
imports: import time
  from functools import partial
  from io import BytesIO
  from itertools import product
  from threading import Lock, Thread
  import numpy
  import pytest
  from numpy.testing import assert_array_equal
  from ..fileslice import _positive_slice, _simple_fileslice, calc_slicedefs, canonical_slicers, fileslice, fill_slicer, is_fancy, optimize_read_slicers, optimize_slicer, predict_shape, read_segments, slice2len, slice2outax, slicers2segments, strided_scalar, threshold_heuristic
label_desc: Use image node_id 13 for calling a global function with example usage: test_optimize_slicer() without return types