lucent.misc.io package

Submodules

lucent.misc.io.collapse_channels module

Convert an “image” with n channels into 3 RGB channels.

lucent.misc.io.collapse_channels.hue_to_rgb(ang: int, warp: Optional[bool] = True) ndarray

Produce an RGB unit vector corresponding to a hue of a given angle.

Parameters
  • ang (int) – Angle in degrees

  • warp (Optional[bool], optional) – Whether to warp the angle away from the primary colors (RGB). Helps make equally-spaced angles more visually distinguishable, defaults to True

Returns

RGB unit vector corresponding to the given angle

Return type

np.ndarray

lucent.misc.io.serialize_array module

Utilities for normalizing arrays and converting them to images.

lucent.misc.io.serialize_array.array_to_jsbuffer(array: ndarray) str

Serialize 1d NumPy array to JS TypedArray.

Data is serialized to base64-encoded string, which is much faster and memory-efficient than json list serialization.

Parameters

array (np.ndarray) – 1d NumPy array, dtype must be one of JS_ARRAY_TYPES.

Returns

JS code that evaluates to a TypedArray as string.

Return type

str

Raises

TypeError: if array dtype or shape not supported.

lucent.misc.io.serialize_array.serialize_array(array: ndarray, domain: Optional[Union[List, Tuple]] = (0, 1), fmt: Optional[str] = 'png', quality: Optional[int] = 70) BytesIO

Given any 3-rank array, returns byte representation of image encoding.

Parameters
  • array (np.ndarray) – NumPy array of rank 3

  • domain (Optional[Union[List, Tuple]], optional) – expected range of values in array, if explicitly set to None will use the array’s own range of values and normalize them, defaults to (0, 1)

  • fmt (Optional[str], optional) – string describing desired file format, defaults to ‘png’, defaults to ‘png’

  • quality (Optional[int], optional) – specifies compression quality from 0 to 100 for lossy formats, defaults to 70

Returns

image data as BytesIO buffer

Return type

BytesIO

lucent.misc.io.showing module

Methods for displaying images from Numpy arrays.

lucent.misc.io.showing.image(array: ndarray, width: Optional[int] = None, domain: Optional[Tuple] = None, fmt: Optional[str] = 'png') None

Display an image.

Parameters
  • array (np.ndarray) – a numpy array to be displayed

  • width (Optional[int], optional) – width of output image, scaled using nearest neighbor interpolation. size unchanged if None, defaults to None

  • domain (Optional[Tuple], optional) – expected range of values in array, if left None will be set to (0, 1), if explicitly set to None will use array’s own value range, defaults to None

  • fmt (Optional[str], optional) – string describing desired file format, defaults to ‘png’

lucent.misc.io.showing.images(arrays: List[ndarray], labels: Optional[List[str]] = None, domain: Optional[Tuple] = None, width: Optional[int] = None) None

Display a list of images with optional labels.

Parameters
  • arrays (List[np.ndarray]) – A list of NumPy arrays representing images

  • labels (List[str]) – labels: A list of strings to label each image. Defaults to image index if None

  • width (Optional[int], optional) – width of output image, scaled using nearest neighbor interpolation. size unchanged if None, defaults to None

  • domain (Optional[Tuple], optional) – expected range of values in array, if left None will be set to (0, 1), if explicitly set to None will use array’s own value range, defaults to None

lucent.misc.io.showing.show(thing: Any, domain: Optional[Union[List, Tuple]] = (0, 1), **kwargs) None

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:

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

Parameters
  • thing (Any) – Object that should be displayed.

  • domain (Optional[Union[Tuple, List]], optional) – 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), defaults to (0, 1)

Module contents