2

I am trying to make an algorithm using just numpy (i saw others using PIL, but it has some drawbacks) that can compare and plot the difference between two maps that show ice levels from different years. I load the images and set NaNs to zero, as I have some.

data = np.load(filename)
data[np.isnan(data)]=0

The data arrays contain values between 0 and 100 and represent concentration levels (100 is the deep blue). The data looks like this:

Ice maps

I am trying to compute the difference so that a loss in ice over time will correspond to a negative value, and a gain in ice will correspond to a positive value. The ice is denoted by the blue color in the plots above. Any hints? Comparing element by element seems to be not the best idea...

Aidenhjj
  • 1,249
  • 1
  • 14
  • 27
nyw
  • 195
  • 2
  • 11

1 Answers1

0

To get the difference between 2 same sized numpy arrays of data, just take one from the other:

diff = img1 - img2

Numpy is basically a Python wrapper for an underlying C code base, designed for these sorts of operations. Although underneath it is comparing element to element (as you say above); it is significantly faster at these sorts of operations.

Aidenhjj
  • 1,249
  • 1
  • 14
  • 27
  • Hey, thanks! I'll look into it right now :) The format is .npy. That's why I asked for an algorithm that uses just numpy – nyw Mar 01 '17 at 18:14
  • 1
    FYI run `data.shape` to get the shape of the array. – Aidenhjj Mar 01 '17 at 18:16
  • The shape is just the pixel size (550,500). I was writing when you commented :D – nyw Mar 01 '17 at 18:18
  • what do you get if you do `data[0,0]`? – Aidenhjj Mar 01 '17 at 18:20
  • 0. The value of that pixel – nyw Mar 01 '17 at 18:21
  • I see, so they're not images, they're just arrays that you have plotted (using matplotlib?). I'll update my answer. What is the threshold that takes you into blue colours? – Aidenhjj Mar 01 '17 at 18:22
  • Yes! Used np.save('image.npy', data). The threshold is a concentration bigger than 99% of the pixels. The values of the data array are floats between 0 and 100 and represent concentration. 100% represents the deep blue, and 0 the red. – nyw Mar 01 '17 at 18:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136985/discussion-between-aidenhjj-and-nyw). – Aidenhjj Mar 01 '17 at 18:30