Geophysical tutorial – How to evaluate and compare colormaps in Python

These below are two copies of a seismic horizon from the open source Penobscot 3D seismic survey  coloured using two different colormaps (data from Hall, 2014).

horizon_comparison

Figure 1

Do you think either of them is ‘better’?  If yes, can you explain why? If you are unsure and you want to learn how to answer such questions using perceptual principles and open source Python code, you can read my tutorial Evaluate and compare colormaps (Niccoli, 2014), one of the awesome Geophysical Tutorials from The Leading Edge. In the process you will learn many things, including how to calculate an RGB colormap’s intensity using a simple formula:

import numpy as np
ntnst = 0.2989 * rgb[:,0] + 0.5870 * rgb[:,1] + 0.1140 * rgb[:,2] # get the intensity
intensity = np.rint(ntnst) # rounds up to nearest integer

…and  how to display  the colormap as a colorbar with an overlay plot of the intensity as in Figure 2.

intensity_dots

Figure 2

 

Reference

Hall, M. (2014) Smoothing surfaces and attributes. The Leading Edge 33, no. 2, 128–129. Open access at: https://github.com/seg/tutorials#february-2014

Niccoli, M. (2014) Evaluate and compare colormaps. The Leading Edge 33, no. 8.,  910–912. Open access at: https://github.com/seg/tutorials#august-2014

New Matlab isoluminant colormap for azimuth data

I recently added to my Matlab File Exchange function, Perceptually improved colormaps, a colormap for periodic data like azimuth or phase. I am going to briefly showcase it using data from my degree thesis in geology, which I used before, for example in Visualization tips for geoscientists – Matlab. Figure 1, from that post, shows residual gravity anomalies in milligals.

data cube1_final_shading_slope

Figure 1

Often we’re interested in characterizing these anomalies by calculating the direction of maximum dip at each point on the surface, and for that direction display the azimuth, or dip azimuth.  I’ve done this for the surface of residual anomalies from Figure 1 and displayed the azimuth in Figure 2. Azimuth from 0 to 360 degrees are color-coded using Jet, Matlab’s standard colormap (until recently). Typically I do not trust azimuth values when the dip is close to zero because it is often contaminated by noise so I would use shading to de-saturate the colors where dip has the lowest values, but for ease of discussion I haven’t done so in this case.

Figure 2. Azimuth values color-coded with Jet.

Figure 2. Azimuth values color-coded with Jet.

There are two problems with Figure 2. First, the well-known problems with the jet colormap. For example, blue is too dark and blue areas appear as bands of constant colour. Yellow is much lighter than any other colour so we see artificial yellow edges that are not really present in the data. But there is an additional issue in Figure 2 because azimuths close in value to 0 and 360 degrees are colored with blue and red, respectively, instead of a single color as they should, causing an additional artificial edge.

In Figure 3 I recolored the map using a colormap that replicates those used in many geophysical software tools to display azimuth or phase data. This is better because it wraps around at 360 degrees but the perceptual issues are unresolved: in this case red, yellow and blue all appear as sharp perceptual edges.

standardAZ

Figure 3. Azimuth values color-coded with generic azimuth colormap.

 

isoAZ

Figure 4. Azimuth values color-coded with isoluminant azimuth colormap.

 

In Figure 4 I used my new colormap, called isoAZ (for isoluminant azimuth). This colormap is much better because not only does it wraps around at 360 degrees, but also lightness is held constant for all colors, which eliminates the perceptual anomalies. All the artificial yellow, red, and blue edges are gone, only real edges are left. This can be more easily appreciated in the figure below: if you hover with your mouse over it you are able to switch back and forth between Figure 3 and Figure 4.

isoAZstandardAZ

From an interpretation point of view, azimuths 180 degrees apart are of opposing colours, which is ideal for dip azimuth data because it allows us to easily recognize folds where dips of opposite direction are juxtaposed at an edge. One example is the sharp edge in the northwest quadrant of Figure 4, where magenta is juxtaposed to green. If you look at Figure 1 you see that there’s a relative high in this area (the edge in Figure 4) with dips of opposite direction on either side (East and West, or 0 and 360 degrees).

The colormap was created in the Lightness-Chroma-Hue color space, a polar transform of the Lab color space, where lightness is the vertical axis and at each value of lightness, chroma is the radial coordinate and hue the polar angle. One limitation of this approach is that due to theirregular  shape of the color gamut section at each lightness value, we can never exceed  chroma values  of about 38-40 (at lightness = 65 in Matlab; in Python, with extensive trial and error, I have not been able to go past 36 using the Scikit-image Color module), which make the resulting colors pale, pastely.

it creates For those that want to experiment with it further, I used just a few lines of code similar to the ones below:

radius = 38; % chroma
theta = linspace(0, 2*pi, 256)'; % hue
a = radius * cos(theta);
b = radius * sin(theta);
L = (ones(1, 256)*65)'; % lightness
Lab = [L, a, b];
RGB=colorspace('RGB<-Lab',Lab(end:-1:1,:));

This code is a modification from an example by Steve Eddins on a post on his Matlab Central blog. In Steve’s example the colormap cycles through the hues as lightness increases monotonically (which by the way is an excellent way to generate a perceptual rainbow). In this case lightness is kept constant and hue cycles through the entire 360 degrees and wraps around. Also, instead of using the Image Processing Toolbox, I used  Colorspace, a free function from Matlab File Exchange, for the color space transformations.

For data like fracture orientation where azimuths 180 degrees apart are equivalent it is better to stack two of these isoluminant colormaps in a row. In this way we place opposing colors 90 degrees apart, whereas color 180 degrees apart are the same. You can do it using Matlab commands repmat or vertcat, as below:

radius = 38; % chroma
theta = linspace(0, 2*pi, 128)'; % hue
a = radius * cos(theta);
b = radius * sin(theta);
L = (ones(1, 128)*65)'; % lightness
Lab = [L, a, b];
rgb=colorspace('RGB<-Lab',Lab(end:-1:1,:));
RGB=vertcat(rgb,rgb);

Colormap compromise

At the end the series The rainbow is dead…long live the rainbow! I introduced CubicYF, a rainbow-like color palette with a slightly compressive, monotonic increasing lightness profile. The red color is missing from the palette because green and red at people with deuteranopia, the most common type of color vision deficiency, confuse red and green, so I took one out. An alternative solution to taking the red out is to have a yellow-like color with lower lightness followed by a red-like color of higher lightness, like in the LinearL palette described The rainbow is dead…long live the rainbow! – Part 5. A third solution, which is a compromise in perceptual terms, is CubicL (sometimes I called it cube1), the color palette I used with the gravity data from my degree thesis in geology in the series Visualization tips for geoscientists. An example map from the series in which I used CubicL is in Figure 1 below:

data cube1_final_shading_slope

Figure 1

To generate CubicL  I used a compressive function for the increase in lightness very much like the one for CubicYF, except in this case I allowed lightness to decrease from about 90 in the yellow portion of the colormap to about 80 to get to the red. As a result, there is an inversion in the lightness trend, indicated by the arrow in the bottom row in Figure 2. However, I believe this is an acceptable compromise, and one that is pleasing for people accustomed to rainbow (it has red), because the inversion is smooth, with a gentle downward-facing concavity. I also believe this colormap is still a perceptual improvement over the rainbow or spectrum so commonly used in seismic visualization, which typically have at least 3 sharp lightness inversions (indicated by arrows in the top row of Figure 2 below).

spectrum_vs_cubeY

Figure 2

CubicL is one of the colormaps available in my Matlab File Exchange function Perceptually Improved Colormaps.

What your brain does with colours when you are not “looking” – part 1

When I published the last post of my series The rainbow is dead…long live the rainbow! there was a great discussion in the comments section with Giuliano Bernardi, a Ph.D. student at the University of Leuven, on the use of different colour palettes in audio spectrogram visualization.

Since then Giuliano has been kind enough to provide me with the data for one of his spectrograms, so I am resuming the discussion. Below here is a set of 5 figures generated in Matlab from the same data using different colormaps. With this post I’d like to get readers involved and ask to cast your vote for the colormap you prefer, and even drop a line in the comments section to tell us the reason for your preference.

In the second post I’ll show the data displayed with the same 5 colormaps but using a different type of visualization, which will reveal what our brain is doing with the colours (without our full knowledge and consent), and then I will ask again to vote for your favourite.

 

 

spectrogram_jet

A – Jet colormap

spectrogram_gray

B – Gray scale

spectrogram_lin_L_rainbow

C – Linear Lightness rainbow

spectrogram_mod_heated_body

D – Modified heated body (linear Lightness)

spectrogram_CubicYF

E – Cube-law Lightness rainbow

Visualize Mt St Helen with Python and a custom color palette

Evan Bianco of Agile Geoscience wrote a wonderful post on how to use python to import, manipulate, and display digital elevation data for Mt St Helens before and after the infamous 1980 eruption. He also calculated the difference between the two surfaces to calculate the volume that was lost because of the eruption to further showcase Python’s capabilities. I encourage readers to go through the extended version of the exercise by downloading his iPython Notebook and the two data files here and here.

I particularly like Evan’s final visualization (consisting of stacked before eruption, difference, and after eruption surfaces) which he created in Mayavi, a 3D data visualization module for Python. So much so that I am going to piggy back on his work, and show how to import a custom palette in Mayavi, and use it to color one of the surfaces.

Python Code

This first code block imports the linear Lightness palette. Please refer to my last post for instructions on where to download the file from.

import numpy as np
# load 256 RGB triplets in 0-1 range:
LinL = np.loadtxt('Linear_L_0-1.txt') 
# create r, g, and b 1D arrays:
r=LinL[:,0] 
g=LinL[:,1]
b=LinL[:,2]
# create R,G,B, and ALPHA 256*4 array in 0-255 range:
r255=np.array([floor(255*x) for x in r],dtype=np.int) 
g255=np.array([floor(255*x) for x in g],dtype=np.int)
b255=np.array([floor(255*x) for x in b],dtype=np.int)
a255=np.ones((256), dtype=np.int); a255 *= 255;
RGBA255=zip(r255,g255,b255,a255)

This code block imports the palette into Mayavi and uses it to color the Mt St Helens after the eruption surface. You will need to have run part of Evan’s code to get the data.

from mayavi import mlab
# create a figure with white background
mlab.figure(bgcolor=(1, 1, 1))
# create surface and passes it to variable surf
surf=mlab.surf(after, warp_scale=0.2)
# import palette
surf.module_manager.scalar_lut_manager.lut.table = RGBA255
# push updates to the figure
mlab.draw()
mlab.show()

After_Linear_L

Reference

Mayavi custom colormap example

 

Related Posts (MyCarta)

The rainbow is dead…long live the rainbow! – Part 5 – CIE Lab linear L* rainbow

Convert color palettes to Python Matplotlib colormaps

Related Posts (external)

How much rock was erupted from Mt St Helens

Some photos of Northern British Columbia wildlife and geology

Introduction

Last week I went  on a helicopter ride with Gerry, my father in-law, to count of Kokanee Salmon in the  Camp Creek near Valemount, BC. We were invited by Curtis Culp of Dunster, BC, which is in charge of this  conservation effort run by BC Hydro. Here’s a picture of Gerry leaving the chopper (from Yellowhead Helicopters).

chopper

Kokanee salmon is a land-locked relative of Sockeye salmon. This means that they spend all their life in inland lakes, never seeing the ocean. For spawning  they enter inlet streams of the lake where they live. Camp Creek is a smaller tributary of the Canoe River, inlet of the Kinbasket Lake, where these Kokanee live.

The number of fish is estimated visually from the helicopter using a hand-held tally counter (every ~100-fish patch is a click). As a matter of facts, Curtis and Gerry counted fish, and I went along for the fun. Their estimates were really close, coming in at 15,000 and 15,400 in ~35 minutes over a ~15 km stretch of the Camp Creek. I counted 15 between Bald eagle and Golden eagle, and took some photos. Here they are!

Wildlife and nature photos

The first two are photos looking straight down the Camp Creek. Believe it or not, there’s fish there. See the dark spots? Those are Kokanee Salmon. And the job was to count them, so I am glad I did not have to (although it was easier to the naked eye).

Kokanee4

Kokanee3

The next two are a couple of photos taken at the ground level, courtesy of Curtis. Here the salmon is easy to see.

Kokanee ground

Kokanee ground1

The next two are also photos of the creek from the helicopter. There’s fish in there but I can only say it because I saw them, I can’t quite make them up in the photos. I love the shots though, the crystal clear water and the shadows.

Kokanee1

Kokanee2

The following two are photos with eagles. I could not believe how tiny they look, since even at this distance they seemed huge to the naked eye. There is a bald eagle in the first photo (middle left) , the other two (in the middle of the second photo) are too tiny, it is hard to say.

eagle

two_eagles

This last one is a photo of the trees, just looking down. I find it mesmerizing.trees

Upon looking at all the photos (I took about 80) I have to say that as much as I love my iPhone 4S, they are not nearly as good as I had wished for. Certainly far from the photos I shot during a claim staking trip in the Cassiar Mountains near Watson Lake, Yukon using a Canon FTb 35 mm (one of these days I’ll have to get those photos out of the attic and publish some of them). I often think of going back to my reflex camera, although I hear the iPhone 5 camera is a big improvement, with the iPhone 5S being even faster, so there’s hope.

Bonus photo

Here’s a beautiful elk. I took it another day, on the highway just outside of Jasper, but I thought it would fit in here.

elk

Geology photos

I love meander rivers so I took a whole lot of photos of the creek. The first one shows a nice sandbar right where we started the counting.

start

This next one is a nice shot of the meandering creek looking back.

Camp Creek

In this third one you can see two nicely developed meander loops with point bars.

point_bar

Last, but not least, a really tight meander. I love this photo, it’s my overall favourite.

tight_loop

Human activity photos

I am also including some photos showing the human footprint on the land. This first one is a clearing – I am not certain for what purpose, likely a new development. The circular patches are places where the logs were collected and burned. Quite the footprint, seen from here.

clearing

Next is something I did not expect to see here, a golf course – although I probably should have…. they are omnipresent, and often obnoxious, to say the least.

golf_course

This is one I quite like: the creek, the railway, and the Yellowhead highway, all running next to one another.

three_tracks

The team

Finally, a shot of Gerry and I in the back of the chopper and one of Gerry counting.

duo

counting

Beautiful Geology from space

In my post Our Earth truly is art I talked about Earth as Art, NASA’s  e-book collection of wonderful satellite images of our planet, and posted my top 3 picks.

In NASA’s Perpetual Ocean animation I talk about a beautiful convergence of maps and art: The Turbulence of Van Gogh and the Labrador Shelf Current, and NASA’s Perpetual Ocean animation.

Here’s another gem: Van Gogh from Space Landsat 7 Acquired 7/13/2005, winner of NASA’s public contest to select the Top Five ‘Earth as Art’ Winners

670107main_van_gogh_from_space

Geology photo quiz #1

Take a look at the photo below, which I took it on the way up to McBride Peak (in McBride, British Columbia). It is a view up the Sunbeam Creek, part of an Ecological Reserve. Question: why would (only) part of the creek be so white? My father-in-law and I had been wondering since a previous hike to the top of the Peak, and speculations were running rampant. Finally, yesterday, we decided to hike up to the top again, then go down to the creek to find out. I think we did, and it was a great hike and a lot of fun. I am in the process of writing a nice post on this geo-adventure, but I though in the meantime I’d post the photo and make it a quiz.

IMG_7201

I will give readers two clues:

1) the mysterious white “stuff” sits in a creek where water is actually running;

2) this is a south face, exposed to the sun all day long, so it couldn’t be snow or ice.

Below is a close-up photo. So, what do you think it is?
Or at least, what do you think it could be?

IMG_7348