Skip to main content
# 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)

Release files for ModestImage 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ModestImage 0.2
File Size Uploaded
ModestImage-0.2.tar.gz 7.5 kB Details

Release files / ModestImage-0.2.tar.gz

Download URL ModestImage-0.2.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4b71af469f0c4ba73a915c6a96040cc54e81c9ce0c793e6d3e6bb72a99cb53dc
BLAKE2b-256 checksum
How to use checksums
18fbdd00478d57d6e70950831c936f5fa225f65a4241bd920c10b3b4cba5916d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.5

Release history Release notifications | RSS feed

This release

0.2 This release

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page