I wrote a function to determine the proportion of raster pixels that contain each of a list of values that I would be interested in. I then wrote a function to apply the previous function to a 3d numpy array. I would like to know how to improve this and also whether it would be easier to apply a function like this to a list of raster files, rather than create a numpy array.
A test array and sample values:
import numpy as np test_arr = np.random.randint(0, 200, 200).reshape(2,10,10) values = [test_arr, test_arr]
Here are my functions:
def pixel_props(rast, values): """this function outputs a list with the proportions of the total raster pixels that contain each value.""" prop_list =  size = float(rast.size) for i in values: temp_count = np.count_nonzero(rast[rast == i]) prop_list.append(temp_count/size) return prop_list def raster_props(array, no_data): """this will apply the pixel_props function to a bunch of layers in a numpy 3d array""" master_matrix = [] master_matrix.append(no_data) for layer in array: master_matrix.append(pixel_props(layer, no_data)) return master_matrix