code: def _ftype4scaled_finite(
      tst_arr: np.ndarray,
      slope: npt.ArrayLike,
      inter: npt.ArrayLike,
      direction: ty.Literal["read", "write"] = "read",
      default: type[np.floating] = np.float32,
  ) -> type[np.floating]:
      """Smallest float type for scaling of `tst_arr` that does not overflow"""
      assert direction in ("read", "write")
      if default not in OK_FLOATS and default is np.longdouble:
          # Omitted longdouble
          return default
      def_ind = OK_FLOATS.index(default)
      # promote to arrays to avoid numpy scalar casting rules
      tst_arr = np.atleast_1d(tst_arr)
      slope = np.atleast_1d(slope)
      inter = np.atleast_1d(inter)
      for ftype in OK_FLOATS[def_ind:]:
          tst_trans = tst_arr.copy()
          slope = slope.astype(ftype)
          inter = inter.astype(ftype)
          try:
              with warnings.catch_warnings():
                  # Error on overflows to short circuit the logic
                  warnings.filterwarnings(
                      "error", ".*overflow.*", RuntimeWarning
                  )
                  if (
                      direction == "read"
                  ):  # as in reading of image from disk
                      if slope != 1.0:
                          tst_trans = tst_trans * slope
                      if inter != 0.0:
                          tst_trans = tst_trans + inter
                  elif direction == "write":
                      if inter != 0.0:
                          tst_trans = tst_trans - inter
                      if slope != 1.0:
                          tst_trans = tst_trans / slope
              # Double-check that result is finite
              if np.all(np.isfinite(tst_trans)):
                  return ftype
          except RuntimeWarning:
              pass
      raise ValueError("Overflow using highest floating point type")
  
imports: from __future__ import annotations
  import io
  import sys
  import typing
  import warnings
  from functools import reduce
  from operator import getitem, mul
  from os.path import exists, splitext
  import numpy
  from ._compression import COMPRESSED_FILE_LIKES
  from .casting import OK_FLOATS, shared_range
  from .externals.oset import OrderedSet
label_desc: Use image node_id 16 for calling a global function with example usage: _ftype4scaled_finite(tst_arr, slope, inter, direction, default) and returns: default, ftype