Skip to main content

Unmap data from pseudocolor images.

Project description

unmap

Run tests Build docs PyPI version PyPI versions PyPI license

Unmap data from pseudocolor images, with knowledge of the colourmap (for now!).

Installation

You can install this package with pip:

pip install unmap

Documentation

Read the documentation

Example

Let's grab an image from the web and unmap it. First we'll download the image:

from io import BytesIO

import requests
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt


def get_image_from_web(uri):
    data = requests.get(uri).content
    img = Image.open(BytesIO(data)).convert('RGB')
    rgb_im = np.asarray(img)[..., :3] / 255.
    return rgb_im

# An image from Hugh Pumprey's blog post Colours for Contours, licensed CC BY
# https://blogs.ed.ac.uk/hughpumphrey/2017/06/29/colours-for-contours/
uri = 'https://blogs.ed.ac.uk/hughpumphrey/wp-content/uploads/sites/958/2017/06/jeti.png'
img = get_image_from_web(uri)

The image looks like this:

Hugh_Pumphrey_CC-BY_original.png

Now we'll unmap it:

import unmap

data = unmap.unmap(img, cmap='jet')
plt.imshow(data)

This results in:

The unmapped data.

Notice that the data is there, but so is the colourbar. Also, the new data's colourbar shows that our dataset ranges from 0 to 1, but we can see from the colourbar in the original plot that it should range from about 0.4 to 1.333. So let's add vrange and crop arguments to deal with these issues:

data = unmap.unmap(img, cmap='jet', vrange=(0.400, 1.333), crop=(115, 72, 690, 647))

Which gives us:

The final result, cropped to the data area.

We could even plot this side by side with the original data, using the same colourmap, as a rough QC:

Comparison between input and output.

The extents of the data area are different. We could pass the extent argument to plt.imshow to fix this, but an even better idea is to put the data in an xarray.DataArray, because then the coordinates are attached to the data in a much more useful way. You could do this on your own, or you can use the Gio library's unmap_to_dataarray() function to do it for you.

Testing

You can run the tests (requires pytest and pytest-cov) with

pytest

Building

This repo uses PEP 517-style packaging. Read more about this and about Python packaging in general.

Building the project requires build, so first:

pip install build

Then to build unmap locally:

python -m build

The builds both .tar.gz and .whl files, either of which you can install with pip.

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

unmap-0.1.1.tar.gz (2.8 MB view hashes)

Uploaded Source

Built Distribution

unmap-0.1.1-py3-none-any.whl (11.3 kB view hashes)

Uploaded Python 3

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