Skip to main content

Friendlier matplotlib interaction with large images

Project description

# ModestImage


*Friendlier matplotlib interaction with large images*

ModestImage extends the matplotlib AxesImage class, and avoids
unnecessary calculation and memory when rendering large images (where most
image pixels aren't visible on the screen). It has the following
benefits over AxesImage:

* Draw time is (roughly) independent of image size
* Large ``numpy.memmap`` arrays can be visualized, without making an
in-memory copy of the entire array. This enables visualization of
images too large to fit in memory.

## Installation

```
pip install ModestImage
```
or
```
easy_install ModestImage
```

## Using ModestImage


The easiest way is to use the modified ``imshow`` function:

```
import matplotlib.pyplot as plt
from modest_image import ModestImage, imshow

ax = plt.gca()
imshow(ax, image_array, vmin=0, vmax=10)
plt.show()
```

``imshow`` accepts all the keyword arguments that the matplotlib
function does. The ``vmin`` and ``vmax`` keywords aren't necessary
but, if they are not provided, the entire image will be scanned to
determine the min/max values. This can be slow if the array is huge.

To create a ModestImage artist directly:

```
artist = ModestImage(data=array)
```

## Looking at very big FITS images


```
import matplotlib.pyplot as plt
import pyfits
from modest_image import imshow

ax = plt.gca()
huge_array = pyfits.open('file_name.fits', memmap=True)[0].data
artist = imshow(ax, huge_array, vmin=0, vmax=10)
plt.show()
```

This opens almost instantly, with a modest memory footprint.

## Why is Matplotlib Image Drawing Slow?


For the first draw request after setting the color mapping or data
array, AxesImage (the default matplotlib image class) calculates the
RGBA value for every pixel in the data array. That's a lot of work for
large images, and usually overkill given that the final rendering is
limited by screen resolution (usually 100K-1M pixels) and not image
resolution (often much more).

AxesImage compensates for this by saving the results of this
scaling. This means that subsequent renderings that only change the
position or zoom level are very fast. However, in interactive
situations where the data array or intensity scale change often,
AxesImage wastes lots of time calculating RGBA values for every pixel
in a (potentially large) data set. It also makes several temporary
arrays with size comparable to the original array, wasting memory.

## How is ModestImage faster?

ModestImage resamples the image array at each draw request, extracting
a smaller image whose resolution and extent are matched to the screen
resolution. Thus, the RGBA scaling step is much faster, since it takes
place only for pixels relevant for the current rendering.

This scheme does not take advantage of AxesImage's caching, and thus
redraws after move and zoom operations are slightly slower. However,
draws after colormap and data changes are substantially faster, and most
redraws are fast enough for interactive use.

## Performance and Tests

``speed_test.py`` compares the peformance of ModestImage and
AxesImage. For a 1000x1000 pixel image:

```
Performace Tests for AxesImage

time_draw: 186 ms per operation
time_move: 19 ms per operation
time_move_zoom: 28 ms per operation

Performace Tests for ModestImage

time_draw: 25 ms per operation
time_move: 20 ms per operation
time_move_zoom: 28 ms per operation
```

``time_draw`` is the render time after the cache has been cleared
(e.g. after ``set_data`` has been called, or the colormap has been
changed). ModestImage is slightly slower than, though still
competetive with, AxesImage for move and zoom operations where
AxesImage uses cached data.

Unit tests can be found in the ``tests`` directory. ModestImage does not
always produce results identical to AxesImage at the pixel level, due to
how it downsamples images. The discrepancy is minor, however, and disappears
if no downsampling takes place (i.e. a screen pixel samples <= 1 data pixel)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ModestImage-0.2.tar.gz (7.5 kB view details)

Uploaded Source

File details

Details for the file ModestImage-0.2.tar.gz.

File metadata

  • Download URL: ModestImage-0.2.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.5

File hashes

Hashes for ModestImage-0.2.tar.gz
Algorithm Hash digest
SHA256 4b71af469f0c4ba73a915c6a96040cc54e81c9ce0c793e6d3e6bb72a99cb53dc
MD5 ea18e252831ebfba3e98b84a464ae344
BLAKE2b-256 18fbdd00478d57d6e70950831c936f5fa225f65a4241bd920c10b3b4cba5916d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page