1

I am currently trying to use list comprehensions to filter some values on a data cube with some images, however I got lost to make the jump from 2 (as we can see in here or here) to 3 dimensions.

For a single image, the line of code that accomplishes what I want is:

AM2 = [[x if x > 1e-5 else 0 for x in line] for line in AM[0]]

How do I take this to also consider the different images that are stacked on the top of each other? I assume I would need to add a third nested loop but so far all my attempts to do so failed.

In my particular case the datacube is composed of numpy arrays having the dimensions of (100x400x900). Are lists comprehensions still advised to be used for filtering values over that volume of data?

Thanks for your time.

Chicrala
  • 994
  • 12
  • 23
  • 1
    As FHTMitchell mentions, you generally don't want to use _any_ kind of Python `for` loop with Numpy arrays, unless the program logic gives you no alternative. You should let Numpy handle the looping for you, since it can do it at compiled speed. – PM 2Ring Apr 25 '18 at 12:30

2 Answers2

3

Don't use list comprehensions for numpy arrays, you lose their speed and power. Instead use numpy advanced indexing. For example your comprehension can be written as

AM2 = AM.copy() # USe AM2 = AM.copy()[0] if you just want the first row as in your example
AM2[AM2 < 1e-5] = 0
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
1

For pure Python nested lists, try this:

AM2 = [[x if x > 1e-5 else 0 for x in line] for A in AM for line in A]

See @FHTMitchell's answer if these are numpy arrays.

James
  • 32,991
  • 4
  • 47
  • 70