Applying different distributions to collections and images


In [1]:
import ee
ee.Initialize()

In [2]:
from geetools import tools

In [3]:
import ipygee as ui

In [4]:
p = ee.Geometry.Point([0, 0])

Build a Test Collection


In [5]:
# values = ee.List([0, 2, 5, 8, 10, 12, 15, 18, 22, 25, 28, 31, 35, 39, 42, 48, 51, 55])
# values = ee.List([-10, -8, -7, -1, -2, 0, 1, 5, 7, 8, 10, 12, 14, 16, 18, 20, 22, 24])  # With negative values
values = ee.List([-20, -32, -15, -8, -5, 0, 2, 4, 6, 8, 9, 10, 12, 14, 16, 18, 20, 23])

In [6]:
def build_collection(n):
    n = ee.Number(n)
    return ee.Image.constant(n).rename('test').set('test', n).toFloat()
images = values.map(build_collection)

In [7]:
collection = ee.ImageCollection.fromImages(images)

Linear Function

Using a band


In [8]:
help(tools.imagecollection.linearFunctionBand)


Help on function linearFunctionBand in module geetools.tools.imagecollection:

linearFunctionBand(collection, band, range_min=None, range_max=None, mean=None, output_min=None, output_max=None, name='linear_function')
    Apply a linear function over the bands across every image of the
    ImageCollection using the following formula:
    
    - a = abs(val-mean)
    - b = output_max-output_min
    - c = abs(range_max-mean)
    - d = abs(range_min-mean)
    - e = max(c, d)
    
    f(x) = a*(-1)*(b/e)+output_max
    
    :param band: the band to process
    :param range_min: the minimum pixel value in the parsed band. If None, it
        will be computed reducing the collection
    :param range_max: the maximum pixel value in the parsed band. If None, it
        will be computed reducing the collection
    :param output_min: the minimum value that will take the resulting band.
    :param output_max: the minimum value that will take the resulting band.
    :param mean: the value on the given range that will take the `output_max`
        value
    :param name: the name of the resulting band
    :return: the parsed collection in which every image will have an extra band
        that results of applying the linear function over every pixel in the
        image
    :rtype: ee.ImageCollection


In [9]:
linear_band1 = tools.imagecollection.linearFunctionBand(collection, 'test')

In [10]:
linear_band1_chart = ui.chart.Image.series(linear_band1, p, scale=1000, xProperty='test', bands=['linear_function'], label_bands=['default values'])

In [11]:
linear_band2 = tools.imagecollection.linearFunctionBand(collection, 'test', mean=10)

In [12]:
linear_band2_chart = ui.chart.Image.series(linear_band2, p, scale=1000, xProperty='test', bands=['linear_function'], label_bands=['mean=10'])

In [13]:
linear_band3 = tools.imagecollection.linearFunctionBand(collection, 'test', mean=10, output_min=0)

In [14]:
linear_band3_chart = ui.chart.Image.series(linear_band3, p, scale=1000, xProperty='test', bands=['linear_function'], label_bands=['mean=10 min=0'])

In [15]:
linear_band4 = tools.imagecollection.linearFunctionBand(collection, 'test', mean=10, output_min=10, output_max=20)

In [16]:
linear_band4_chart = ui.chart.Image.series(linear_band4, p, scale=1000, xProperty='test', bands=['linear_function'], label_bands=['mean=10 min=0 max=12'])

In [17]:
linear_chart = linear_band1_chart.cat(linear_band2_chart, linear_band3_chart, linear_band4_chart)
linear_chart.renderWidget(width=800)


Using a Property


In [18]:
linear_prop1 = tools.imagecollection.linearFunctionProperty(collection, 'test')

In [19]:
linear_prop1_chart = ui.chart.Image.series(linear_prop1, p, scale=1000, xProperty='test', properties=['LINEAR_FUNCTION'], label_properties=['default values'])

In [20]:
linear_prop2 = tools.imagecollection.linearFunctionProperty(collection, 'test', mean=10)

In [21]:
linear_prop2_chart = ui.chart.Image.series(linear_prop2, p, scale=1000, xProperty='test', properties=['LINEAR_FUNCTION'], label_properties=['mean=10'])

In [22]:
linear_prop3 = tools.imagecollection.linearFunctionProperty(collection, 'test', mean=10, output_min=0)

In [23]:
linear_prop3_chart = ui.chart.Image.series(linear_prop3, p, scale=1000, xProperty='test', properties=['LINEAR_FUNCTION'], label_properties=['mean=10 min=0'])

In [24]:
linear_prop4 = tools.imagecollection.linearFunctionProperty(collection, 'test', mean=10, output_min=0, output_max=10)

In [25]:
linear_prop4_chart = ui.chart.Image.series(linear_prop4, p, scale=1000, xProperty='test', properties=['LINEAR_FUNCTION'], label_properties=['mean=10 min=0 max=10'])

In [26]:
linear_prop5 = tools.imagecollection.linearFunctionProperty(collection, 'test', mean=10, output_min=5, output_max=20)

In [27]:
linear_prop5_chart = ui.chart.Image.series(linear_prop5, p, scale=1000, xProperty='test', properties=['LINEAR_FUNCTION'], label_properties=['mean=10 min=5 max=20'])

In [28]:
linear_chart_prop = linear_prop1_chart.cat(linear_prop2_chart, linear_prop3_chart, linear_prop4_chart, linear_prop5_chart)
linear_chart_prop.renderWidget(width=800)



In [29]:
linear_chart_prop.dataframe


Out[29]:
default values mean=10 mean=10 min=0 mean=10 min=0 max=10 mean=10 min=5 max=20
-32 -32 -32.000000 -3.552714e-15 0.000000 5.000000
-20 -20 -16.285714 6.571429e+00 2.857143 9.285714
-15 -15 -9.738095 9.309524e+00 4.047619 11.071429
-8 -8 -0.571429 1.314286e+01 5.714286 13.571429
-5 -5 3.357143 1.478571e+01 6.428571 14.642857
0 0 9.904762 1.752381e+01 7.619048 16.428571
2 2 12.523810 1.861905e+01 8.095238 17.142857
4 4 15.142857 1.971429e+01 8.571429 17.857143
6 6 17.761905 2.080952e+01 9.047619 18.571429
8 8 20.380952 2.190476e+01 9.523810 19.285714
9 9 21.690476 2.245238e+01 9.761905 19.642857
10 10 23.000000 2.300000e+01 10.000000 20.000000
12 12 20.380952 2.190476e+01 9.523810 19.285714
14 14 17.761905 2.080952e+01 9.047619 18.571429
16 16 15.142857 1.971429e+01 8.571429 17.857143
18 18 12.523810 1.861905e+01 8.095238 17.142857
20 20 9.904762 1.752381e+01 7.619048 16.428571
23 23 5.976190 1.588095e+01 6.904762 15.357143

Gauss Function

Over a band


In [30]:
gauss_band1 = tools.imagecollection.gaussFunctionBand(collection, 'test')

In [31]:
gauss_band1_chart = ui.chart.Image.series(gauss_band1, ee.Geometry.Point([0, 0]), scale=1000, xProperty='test', bands=['gauss'], label_bands=['defualt'])

In [32]:
gauss_band2 = tools.imagecollection.gaussFunctionBand(collection, 'test', mean=10)

In [33]:
gauss_band2_chart = ui.chart.Image.series(gauss_band2, ee.Geometry.Point([0, 0]), scale=1000, xProperty='test', bands=['gauss'], label_bands=['mean=10'])

In [34]:
gauss_band3 = tools.imagecollection.gaussFunctionBand(collection, 'test', mean=10, output_min=0.5)

In [35]:
gauss_band3_chart = ui.chart.Image.series(gauss_band3, ee.Geometry.Point([0, 0]), scale=1000, xProperty='test', bands=['gauss'], label_bands=['mean=10 min=0.5'])

Exponential function using the gauss function

Use as mean value the range_max


In [36]:
gauss_band4 = tools.imagecollection.gaussFunctionBand(collection, 'test', mean=23, output_min=0.5)

In [37]:
gauss_band4_chart = ui.chart.Image.series(gauss_band4, ee.Geometry.Point([0, 0]), scale=1000, xProperty='test', bands=['gauss'], label_bands=['Exponential'])

In [38]:
gauss_band_chart = gauss_band1_chart.cat(gauss_band2_chart, gauss_band3_chart, gauss_band4_chart)
gauss_band_chart.renderWidget(width=800)



In [39]:
gauss_band_chart.dataframe


Out[39]:
defualt mean=10 mean=10 min=0.5 Exponential
-32 0.066663 0.009418 0.500000 0.500000
-20 0.347201 0.092535 0.541954 0.503594
-15 0.551540 0.191495 0.591904 0.510812
-8 0.844293 0.424493 0.709511 0.539220
-5 0.936023 0.551540 0.773638 0.562733
0 1.000000 0.767618 0.882704 0.623294
2 0.989477 0.844293 0.921406 0.655646
4 0.958569 0.909185 0.954161 0.692359
6 0.909185 0.958569 0.979087 0.732741
8 0.844293 0.989477 0.994689 0.775695
9 0.807175 0.997359 0.998667 0.797684
10 0.767618 1.000000 1.000000 0.819730
12 0.683296 0.989477 0.994689 0.863029
14 0.595504 0.958569 0.979087 0.903555
16 0.508127 0.909185 0.954161 0.939209
18 0.424493 0.844293 0.921406 0.968001
20 0.347201 0.767618 0.882704 0.988236
23 0.246842 0.639581 0.818077 1.000000

Over a Property


In [40]:
gauss_prop1 = tools.imagecollection.gaussFunctionProperty(collection, 'test')

In [41]:
gauss_prop1_chart = ui.chart.Image.series(gauss_prop1, ee.Geometry.Point([0, 0]), scale=1000, xProperty='test', properties=['GAUSS'], label_properties=['defualt'])

In [42]:
gauss_prop2 = tools.imagecollection.gaussFunctionProperty(collection, 'test', mean=10)

In [43]:
gauss_prop2_chart = ui.chart.Image.series(gauss_prop2, ee.Geometry.Point([0, 0]), scale=1000, xProperty='test', properties=['GAUSS'], label_properties=['mean=10'])

In [44]:
gauss_prop3 = tools.imagecollection.gaussFunctionProperty(collection, 'test', mean=10, output_min=0.5)

In [45]:
gauss_prop3_chart = ui.chart.Image.series(gauss_prop3, ee.Geometry.Point([0, 0]), scale=1000, xProperty='test', properties=['GAUSS'], label_properties=['mean=10 min=0.5'])

In [46]:
gauss_prop = gauss_prop1_chart.cat(gauss_prop2_chart, gauss_prop3_chart)

In [47]:
gauss_prop.renderWidget(width=800)



In [48]:
gauss_prop.dataframe


Out[48]:
defualt mean=10 mean=10 min=0.5
-32 0.066663 0.009418 0.500000
-20 0.347201 0.092535 0.541954
-15 0.551540 0.191495 0.591904
-8 0.844293 0.424493 0.709511
-5 0.936023 0.551540 0.773638
0 1.000000 0.767618 0.882704
2 0.989477 0.844293 0.921406
4 0.958569 0.909185 0.954161
6 0.909185 0.958569 0.979087
8 0.844293 0.989477 0.994689
9 0.807175 0.997359 0.998667
10 0.767618 1.000000 1.000000
12 0.683296 0.989477 0.994689
14 0.595504 0.958569 0.979087
16 0.508127 0.909185 0.954161
18 0.424493 0.844293 0.921406
20 0.347201 0.767618 0.882704
23 0.246842 0.639581 0.818077

Normal Distribution (applies the Gauss function using a computed stdDev and mean)

Over a Property


In [49]:
normal_prop = tools.imagecollection.normalDistributionProperty(collection, 'test')

In [50]:
normal_prop_chart = ui.chart.Image.series(normal_prop, ee.Geometry.Point([0, 0]), scale=1000, xProperty='test', properties=['NORMAL_DISTRIBUTION'], label_properties=['defualt'])

In [51]:
normal_prop_chart.renderWidget(width=800)



In [52]:
normal_prop_chart.dataframe


Out[52]:
defualt
-32 0.001287
-20 0.007266
-15 0.012136
-8 0.020259
-5 0.023448
0 0.027123
2 0.027780
4 0.027901
6 0.027479
8 0.026538
9 0.025889
10 0.025133
12 0.023339
14 0.021253
16 0.018978
18 0.016618
20 0.014269
23 0.010943

Over a Band


In [53]:
normal_band = tools.imagecollection.normalDistributionBand(collection, 'test')

In [54]:
normal_band_chart = ui.chart.Image.series(normal_band, ee.Geometry.Point([0, 0]), scale=1000, xProperty='test', bands=['normal_distribution'], label_bands=['defualt'])

In [55]:
normal_band_chart.renderWidget(width=800)



In [56]:
normal_band_chart.dataframe


Out[56]:
defualt
-32 0.001287
-20 0.007266
-15 0.012136
-8 0.020259
-5 0.023448
0 0.027123
2 0.027780
4 0.027901
6 0.027479
8 0.026538
9 0.025889
10 0.025133
12 0.023339
14 0.021253
16 0.018978
18 0.016618
20 0.014269
23 0.010943

In [ ]: