def show()

in lucid/misc/io/showing.py [0:0]


def show(thing, domain=(0, 1), **kwargs):
  """Display a numpy array without having to specify what it represents.

  This module will attempt to infer how to display your tensor based on its
  rank, shape and dtype. rank 4 tensors will be displayed as image grids, rank
  2 and 3 tensors as images.

  For tensors of rank 3 or 4, the innermost dimension is interpreted as channel.
  Depending on the size of that dimension, different types of images will be
  generated:

    shp[-1]
      = 1  --  Black and white image.
      = 2  --  See >4
      = 3  --  RGB image.
      = 4  --  RGBA image.
      > 4  --  Collapse into an RGB image.
               If all positive: each dimension gets an evenly spaced hue.
               If pos and neg: each dimension gets two hues
                  (180 degrees apart) for positive and negative.

  Common optional arguments:

    domain: range values can be between, for displaying normal images
      None  = infer domain with heuristics
      (a,b) = clip values to be between a (min) and b (max).

    w: width of displayed images
      None  = display 1 pixel per value
      int   = display n pixels per value (often used for small images)

    labels: if displaying multiple objects, label for each object.
      None  = label with index
      []    = no labels
      [...] = label with corresponding list item

  """
  def collapse_if_needed(arr):
    K = arr.shape[-1]
    if K not in [1,3,4]:
      log.debug("Collapsing %s channels into 3 RGB channels." % K)
      return collapse_channels(arr)
    else:
      return arr


  if isinstance(thing, np.ndarray):
    rank = len(thing.shape)

    if rank in [3,4]:
      thing = collapse_if_needed(thing)

    if rank == 4:
      log.debug("Show is assuming rank 4 tensor to be a list of images.")
      images(thing, domain=domain, **kwargs)
    elif rank in (2, 3):
      log.debug("Show is assuming rank 2 or 3 tensor to be an image.")
      image(thing, domain=domain, **kwargs)
    else:
      log.warning("Show only supports numpy arrays of rank 2-4. Using repr().")
      print(repr(thing))
  elif isinstance(thing, (list, tuple)):
    log.debug("Show is assuming list or tuple to be a collection of images.")

    if isinstance(thing[0], np.ndarray) and len(thing[0].shape) == 3:
      thing = [collapse_if_needed(t) for t in thing]

    images(thing, domain=domain, **kwargs)
  else:
    log.warning("Show only supports numpy arrays so far. Using repr().")
    print(repr(thing))