From 566fa726663b2caccc07aac4dbc6ee63e1ae525c Mon Sep 17 00:00:00 2001 From: Rambaud Pierrick <12rambau@users.noreply.github.com> Date: Sat, 23 Nov 2024 11:13:06 +0000 Subject: [PATCH 1/3] fix: add back all the reduction parameters in the plotting functions --- geetools/ee_image.py | 131 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 111 insertions(+), 20 deletions(-) diff --git a/geetools/ee_image.py b/geetools/ee_image.py index 06a55a8e..45ffadff 100644 --- a/geetools/ee_image.py +++ b/geetools/ee_image.py @@ -1591,10 +1591,13 @@ def byBands( self, regions: ee.featurecollection, reducer: str | ee.Reducer = "mean", - scale: int = 10000, bands: list = [], regionId: str = "system:index", labels: list = [], + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + tileScale: float = 1, ) -> ee.Dictionary: """Compute a reducer for each band of the image in each region. @@ -1611,10 +1614,13 @@ def byBands( Parameters: regions: The regions to compute the reducer in. reducer: The name of the reducer or a reducer object to use. Default is "mean". - scale: The scale to use for the computation. Default is 10000m. regionId: The property used to label region. Defaults to "system:index". labels: The labels to use for the output dictionary. Default to the band names. bands: The bands to compute the reducer on. Default to all bands. + scale: The scale to use for the computation. Default is 10000m. + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. Returns: A dictionary with all the bands as keys and their values in each region as a list. @@ -1656,7 +1662,14 @@ def byBands( # retrieve the reduce bands for each feature image = self._obj.select(eeBands).rename(eeLabels) - fc = image.reduceRegions(collection=regions, reducer=red, scale=scale) + fc = image.reduceRegions( + collection=regions, + reducer=red, + scale=scale, + crs=crs, + crsTransform=crsTransform, + tileScale=tileScale, + ) # extract the data as a list of dictionaries (one for each label) aggregating # the values for each feature @@ -1668,10 +1681,13 @@ def byRegions( self, regions: ee.featurecollection, reducer: str | ee.Reducer = "mean", - scale: int = 10000, bands: list = [], regionId: str = "system:index", labels: list = [], + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + tileScale: float = 1, ) -> ee.Dictionary: """Compute a reducer in each region of the image for eah band. @@ -1688,10 +1704,14 @@ def byRegions( Parameters: regions: The regions to compute the reducer in. reducer: The name of the reducer or a reducer object to use. Default is "mean". - scale: The scale to use for the computation. Default is 10000m. regionId: The property used to label region. Defaults to "system:index". labels: The labels to use for the output dictionary. Default to the band names. bands: The bands to compute the reducer on. Default to all bands. + scale: The scale to use for the computation. Default is 10000m. + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. + Returns: A dictionary with all the bands as keys and their values in each region as a list. @@ -1733,7 +1753,14 @@ def byRegions( # retrieve the reduce bands for each feature image = self._obj.select(bands).rename(labels) - fc = image.reduceRegions(regions, red, scale) + fc = image.reduceRegions( + collection=regions, + reducer=red, + scale=scale, + crs=crs, + crsTransform=crsTransform, + tileScale=tileScale, + ) # extract the data as a list of dictionaries (one for each label) aggregating # we are force to turn the fc into a list because GEE don't accept to map a featureCollection @@ -1748,12 +1775,15 @@ def plot_by_regions( type: str, regions: ee.FeatureCollection, reducer: str | ee.Reducer = "mean", - scale: int = 10000, bands: list = [], regionId: str = "system:index", labels: list = [], colors: list = [], ax: Axes | None = None, + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + tileScale: float = 1, ) -> Axes: """Plot the reduced values for each region. @@ -1768,12 +1798,16 @@ def plot_by_regions( type: The type of plot to use. Defaults to "bar". can be any type of plot from the python lib `matplotlib.pyplot`. If the one you need is missing open an issue! regions: The regions to compute the reducer in. rreducer: The name of the reducer or a reducer object to use. Default is "mean". - scale: The scale to use for the computation. Default is 10000m. bands: The bands to compute the reducer on. Default to all bands. regionId: The property used to label region. Defaults to "system:index". labels: The labels to use for the output dictionary. Default to the band names. colors: The colors to use for the plot. Default to the default matplotlib colors. ax: The matplotlib axis to plot the data on. If None, a new figure is created. + scale: The scale to use for the computation. Default is 10000m. + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. + Returns: The matplotlib axis with the plot. @@ -1798,7 +1832,17 @@ def plot_by_regions( normClim.plot_by_regions(ecoregions, ee.Reducer.mean(), scale=10000) """ # get the data from the server - data = self.byBands(regions, reducer, scale, bands, regionId, labels).getInfo() + data = self.byBands( + regions=regions, + reducer=reducer, + bands=bands, + regionId=regionId, + labels=labels, + scale=scale, + crs=crs, + crsTransform=crsTransform, + tileScale=tileScale, + ).getInfo() # get all the id values, they must be string so we are forced to cast them manually # the default casting is broken from Python side: https://issuetracker.google.com/issues/329106322 @@ -1823,12 +1867,15 @@ def plot_by_bands( type: str, regions: ee.FeatureCollection, reducer: str | ee.Reducer = "mean", - scale: int = 10000, bands: list = [], regionId: str = "system:index", labels: list = [], colors: list = [], ax: Axes | None = None, + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + tileScale: float = 1, ) -> Axes: """Plot the reduced values for each bands. @@ -1844,12 +1891,15 @@ def plot_by_bands( type: The type of plot to use. Defaults to "bar". can be any type of plot from the python lib `matplotlib.pyplot`. If the one you need is missing open an issue! regions: The regions to compute the reducer in. reducer: The name of the reducer or a reducer object to use. Default is "mean". - scale: The scale to use for the computation. Default is 10000m. bands: The bands to compute the reducer on. Default to all bands. regionId: The property used to label region. Defaults to "system:index". labels: The labels to use for the output dictionary. Default to the band names. colors: The colors to use for the plot. Default to the default matplotlib colors. ax: The matplotlib axis to plot the data on. If None, a new figure is created. + scale: The scale to use for the computation. Default is 10000m. + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. Returns: The matplotlib axis with the plot. @@ -1874,7 +1924,17 @@ def plot_by_bands( normClim.plot_by_bands(ecoregions, ee.Reducer.mean(), scale=10000) """ # get the data from the server - data = self.byRegions(regions, reducer, scale, bands, regionId, labels).getInfo() + data = self.byRegions( + regions=regions, + reducer=reducer, + bands=bands, + regionId=regionId, + labels=labels, + scale=scale, + crs=crs, + crsTransform=crsTransform, + tileScale=tileScale, + ).getInfo() # get all the id values, they must be string so we are forced to cast them manually # the default casting is broken from Python side: https://issuetracker.google.com/issues/329106322 @@ -1901,23 +1961,33 @@ def plot_hist( bands: list = [], labels: list = [], colors: list = [], - scale: int = 10000, precision: int = 2, ax: Axes | None = None, + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + bestEffort: bool = False, + maxPixels: int = 10**7, + tileScale: float = 1, **kwargs, ) -> Axes: """Plot the histogram of the image bands. Parameters: bins: The number of bins to use for the histogram. Default is 30. + region: The region to compute the histogram in. Default is the image geometry. bands: The bands to plot the histogram for. Default to all bands. labels: The labels to use for the output dictionary. Default to the band names. colors: The colors to use for the plot. Default to the default matplotlib colors. + prescision: The number of decimal to keep for the histogram bins values. Default is 2. ax: The matplotlib axis to plot the data on. If None, a new figure is created. scale: The scale to use for the computation. Default is 10,000m. - prescision: The number of decimal to keep for the histogram bins values. Default is 2. + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + bestEffort: If the polygon would contain too many pixels at the given scale, compute and use a larger scale which would allow the operation to succeed. + maxPixels: The maximum number of pixels to reduce. default to 10**7. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. kwargs: Keyword arguments passed to the matplotlib fill_between() function. - region: The region to compute the histogram in. Default is the image geometry. Returns: The matplotlib axis with the plot. @@ -1950,16 +2020,37 @@ def plot_hist( # extract the data from the server image = self._obj.select(eeBands).rename(eeLabels).clip(region) - # compute the min and ma values of the bands so w can scale the bins of the histogram - min = image.reduceRegion(ee.Reducer.min(), region, scale).values().reduce(ee.Reducer.min()) - max = image.reduceRegion(ee.Reducer.max(), region, scale).values().reduce(ee.Reducer.max()) + # set the common parameters of the 3 reducers + params = { + "geometry": region, + "scale": scale, + "crs": crs, + "crsTransform": crsTransform, + "bestEffort": bestEffort, + "maxPixels": maxPixels, + "tileScale": tileScale, + } + + # compute the min and max values of the bands so w can scale the bins of the histogram + min = ( + image.reduceRegion(**{"reducer": ee.Reducer.min(), **params}) + .values() + .reduce(ee.Reducer.min()) + ) + max = ( + image.reduceRegion(**{"reducer": ee.Reducer.max(), **params}) + .values() + .reduce(ee.Reducer.max()) + ) # compute the histogram. The result is a dictionary with each band as key and the histogram # as values. The histograp is a list of [start of bin, value] pairs - reduce = image.reduceRegion(ee.Reducer.fixedHistogram(min, max, bins), region, scale) + reduce = image.reduceRegion( + **{"reducer": ee.Reducer.fixedHistogram(min, max, bins), **params} + ) raw_data = reduce.getInfo() - # massage raw data to reqhape them as usable source for a Axes plot + # massage raw data to reshape them as usable source for an Axes plot # first extract the x coordinates of the plot as a list of bins borders # every value is duplicated but the first one to create a scale like display. # the values are treated the same way we simply drop the last duplication to get the same size. From 80df34284b333461b856d5ccda7b4267e9acfd26 Mon Sep 17 00:00:00 2001 From: Rambaud Pierrick <12rambau@users.noreply.github.com> Date: Sat, 23 Nov 2024 11:18:27 +0000 Subject: [PATCH 2/3] style: cleaning --- geetools/ee_image.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/geetools/ee_image.py b/geetools/ee_image.py index 45ffadff..7e9f5699 100644 --- a/geetools/ee_image.py +++ b/geetools/ee_image.py @@ -2032,23 +2032,16 @@ def plot_hist( } # compute the min and max values of the bands so w can scale the bins of the histogram - min = ( - image.reduceRegion(**{"reducer": ee.Reducer.min(), **params}) - .values() - .reduce(ee.Reducer.min()) - ) - max = ( - image.reduceRegion(**{"reducer": ee.Reducer.max(), **params}) - .values() - .reduce(ee.Reducer.max()) - ) + min = image.reduceRegion(**{"reducer": ee.Reducer.min(), **params}) + min = min.values().reduce(ee.Reducer.min()) + + max = image.reduceRegion(**{"reducer": ee.Reducer.max(), **params}) + max = max.values().reduce(ee.Reducer.max()) # compute the histogram. The result is a dictionary with each band as key and the histogram # as values. The histograp is a list of [start of bin, value] pairs - reduce = image.reduceRegion( - **{"reducer": ee.Reducer.fixedHistogram(min, max, bins), **params} - ) - raw_data = reduce.getInfo() + reducer = ee.Reducer.fixedHistogram(min, max, bins) + raw_data = image.reduceRegion(**{"reducer": reducer, **params}).getInfo() # massage raw data to reshape them as usable source for an Axes plot # first extract the x coordinates of the plot as a list of bins borders From e289b4dc7403687ebffb348cd9f2cb7a9d01f3f5 Mon Sep 17 00:00:00 2001 From: Rambaud Pierrick <12rambau@users.noreply.github.com> Date: Sat, 23 Nov 2024 11:47:46 +0000 Subject: [PATCH 3/3] fix: add all parameters to the ic plotting methods --- geetools/ee_image_collection.py | 244 +++++++++++++++++++++++++++----- 1 file changed, 212 insertions(+), 32 deletions(-) diff --git a/geetools/ee_image_collection.py b/geetools/ee_image_collection.py index bd59dc3b..dac92328 100644 --- a/geetools/ee_image_collection.py +++ b/geetools/ee_image_collection.py @@ -1061,10 +1061,15 @@ def datesByBands( self, region: ee.Geometry, reducer: str | ee.Reducer = "mean", - scale: int = 10000, dateProperty: str = "system:time_start", bands: list = [], labels: list = [], + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + bestEffort: bool = False, + maxPixels: int | None = 10**7, + tileScale: float = 1, ) -> ee.Dictionary: """Reduce the data for each image in the collection by bands on a specific region. @@ -1081,10 +1086,15 @@ def datesByBands( Parameters: region: The region to reduce the data on. reducer: The name of the reducer or a reducer object use. Default is "mean". - scale: The scale in meters to use for the reduction. default is 10000m dateProperty: The property to use as date for each image. Default is "system:time_start". bands: The bands to reduce. If empty, all bands are reduced. labels: The labels to use for the bands. If empty, the bands names are used. + scale: The scale in meters to use for the reduction. default is 10000m + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + bestEffort: If the polygon would contain too many pixels at the given scale, compute and use a larger scale which would allow the operation to succeed. + maxPixels: The maximum number of pixels to reduce. Defaults to 1e7. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. Returns: A dictionary with the reduced values for each band and each date. @@ -1122,7 +1132,16 @@ def datesByBands( # create a list of dictionaries with the reduced values for each band def reduce(lbl: ee.String) -> ee.Dictionary: image = ic.select([lbl]).toBands().rename(dateList) - return image.reduceRegion(red, region, scale) + return image.reduceRegion( + reducer=red, + geometry=region, + scale=scale, + crs=crs, + crsTransform=crsTransform, + bestEffort=bestEffort, + maxPixels=maxPixels, + tileScale=tileScale, + ) return ee.Dictionary.fromLists(eeLabels, eeLabels.map(reduce)) @@ -1132,8 +1151,11 @@ def datesByRegions( regions: ee.FeatureCollection, label: str = "system:index", reducer: str | ee.Reducer = "mean", - scale: int = 10000, dateProperty: str = "system:time_start", + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + tileScale: float = 1, ) -> ee.Dictionary: """Reduce the data for each image in the collection by regions for a single band. @@ -1152,8 +1174,11 @@ def datesByRegions( regions: The regions to reduce the data on. label: The property to use as label for each region. Default is "system:index". reducer: The name of the reducer or a reducer object use. Default is "mean". - scale: The scale in meters to use for the reduction. default is 10000m dateProperty: The property to use as date for each image. Default is "system:time_start". + scale: The scale in meters to use for the reduction. default is 10000m + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. Returns: A dictionary with the reduced values for each region and each date. @@ -1188,7 +1213,14 @@ def to_string(date: ee.Date) -> ee.String: # reduce the data for each region image = self._obj.select([band]).toBands().rename(dateList) red = getattr(ee.Reducer, reducer)() if isinstance(reducer, str) else reducer - reduced = image.reduceRegions(regions, red, scale) + reduced = image.reduceRegions( + collection=regions, + reducer=red, + scale=scale, + crs=crs, + crsTransform=crsTransform, + tileScale=tileScale, + ) # create a list of dictionaries for each region and aggregate them into a dictionary values = reduced.toList(regions.size()).map(lambda f: ee.Feature(f).toDictionary(dateList)) @@ -1201,10 +1233,15 @@ def doyByBands( region: ee.Geometry, spatialReducer: str | ee.Reducer = "mean", timeReducer: str | ee.Reducer = "mean", - scale: int = 10000, dateProperty: str = "system:time_start", bands: list = [], labels: list = [], + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + bestEffort: bool = False, + maxPixels: int | None = 10**7, + tileScale: float = 1, ) -> ee.Dictionary: """Aggregate the images that occurs on the same day and then reduce each band on a single region. @@ -1222,10 +1259,15 @@ def doyByBands( region: The region to reduce the data on. spatialReducer: The name of the reducer or a reducer object to use for spatial reduction. Default is "mean". timeReducer: The name of the reducer or a reducer object to use for time reduction. Default is "mean". - scale: The scale in meters to use for the reduction. default is 10000m dateProperty: The property to use as date for each image. Default is "system:time_start". bands: The bands to reduce. If empty, all bands are reduced. labels: The labels to use for the bands. If empty, the bands names are used. + scale: The scale in meters to use for the reduction. default is 10000m + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + bestEffort: If the polygon would contain too many pixels at the given scale, compute and use a larger scale which would allow the operation to succeed. + maxPixels: The maximum number of pixels to reduce. Defaults to 1e7. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. Returns: A dictionary with the reduced values for each band and each day. @@ -1281,7 +1323,16 @@ def timeReduce(c: ee.imageCollection) -> ee.image: def spatialReduce(label: ee.String) -> ee.Dictionary: image = ic.select([label]).toBands().rename(doyList) - return image.reduceRegion(spatialRed, region, scale) + return image.reduceRegion( + reducer=spatialRed, + geometry=region, + scale=scale, + crs=crs, + crsTransform=crsTransform, + bestEffort=bestEffort, + maxPixels=maxPixels, + tileScale=tileScale, + ) return ee.Dictionary.fromLists(labels, ee.List(labels).map(spatialReduce)) @@ -1292,8 +1343,11 @@ def doyByRegions( label: str = "system:index", spatialReducer: str | ee.Reducer = "mean", timeReducer: str | ee.Reducer = "mean", - scale: int = 10000, dateProperty: str = "system:time_start", + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + tileScale: float = 1, ) -> ee.Dictionary: """Aggregate the images that occurs on the same day and then reduce a single band on multiple regions. @@ -1313,8 +1367,11 @@ def doyByRegions( label: The property to use as label for each region. Default is "system:index". spatialReducer: The name of the reducer or a reducer object to use for spatial reduction. Default is "mean". timeReducer: The name of the reducer or a reducer object to use for time reduction. Default is "mean". - scale: The scale in meters to use for the reduction. default is 10000m dateProperty: The property to use as date for each image. Default is "system:time_start". + scale: The scale in meters to use for the reduction. default is 10000m + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. Returns: A dictionary with the reduced values for each region and each day. @@ -1361,7 +1418,14 @@ def timeReduce(c: ee.imageCollection) -> ee.image: else spatialReducer ) image = ic.toBands().rename(doyList) - reduced = image.reduceRegions(regions, spatialRed, scale) + reduced = image.reduceRegions( + collection=regions, + reducer=spatialRed, + scale=scale, + crs=crs, + crsTransform=crsTransform, + tileScale=tileScale, + ) # create a list of dictionaries for each region and aggregate them into a dictionary values = reduced.toList(regions.size()).map(lambda f: ee.Feature(f).toDictionary(doyList)) @@ -1374,8 +1438,13 @@ def doyByYears( band: str, region: ee.Geometry, reducer: str | ee.Reducer = "mean", - scale: int = 10000, dateProperty: str = "system:time_start", + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + bestEffort: bool = False, + maxPixels: int | None = 10**7, + tileScale: float = 1, ) -> ee.Dictionary: """Aggregate for each year on a single region a single band. @@ -1393,8 +1462,13 @@ def doyByYears( band: The band to reduce. region: The region to reduce the data on. reducer: The name of the reducer or a reducer object to use. Default is "mean". - scale: The scale in meters to use for the reduction. default is 10000m dateProperty: The property to use as date for each image. Default is "system:time_start". + scale: The scale in meters to use for the reduction. default is 10000m + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + bestEffort: If the polygon would contain too many pixels at the given scale, compute and use a larger scale which would allow the operation to succeed. + maxPixels: The maximum number of pixels to reduce. Defaults to 1e7. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. Returns: A dictionary with the reduced values for each year and each day. @@ -1434,7 +1508,20 @@ def date_tag(i: ee.Image) -> ee.Image: def reduce(year: ee.Number) -> ee.Dictionary: c = ic.filter(ee.Filter.eq(year_metadata, year)) doyList = c.aggregate_array(doy_metadata).map(lambda d: ee.Number(d).int().format()) - return c.toBands().rename(doyList).reduceRegion(red, region, scale) + return ( + c.toBands() + .rename(doyList) + .reduceRegion( + reducer=red, + geometry=region, + scale=scale, + crs=crs, + crsTransform=crsTransform, + bestEffort=bestEffort, + maxPixels=maxPixels, + tileScale=tileScale, + ) + ) return ee.Dictionary.fromLists(yearKeys, yearList.map(reduce)) @@ -1442,12 +1529,17 @@ def plot_dates_by_bands( self, region: ee.Geometry, reducer: str | ee.Reducer = "mean", - scale: int = 10000, dateProperty: str = "system:time_start", bands: list = [], labels: list = [], colors: list = [], ax: Axes | None = None, + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + bestEffort: bool = False, + maxPixels: int | None = 10**7, + tileScale: float = 1, ) -> Axes: """Plot the reduced data for each image in the collection by bands on a specific region. @@ -1456,12 +1548,17 @@ def plot_dates_by_bands( Parameters: region: The region to reduce the data on. reducer: The name of the reducer or a reducer object to use. Default is "mean". - scale: The scale in meters to use for the reduction. default is 10000m dateProperty: The property to use as date for each image. Default is "system:time_start". bands: The bands to reduce. If empty, all bands are reduced. labels: The labels to use for the bands. If empty, the bands names are used. colors: The colors to use for the bands. If empty, the default colors are used. ax: The matplotlib axes to plot the data on. If None, a new figure is created. + scale: The scale in meters to use for the reduction. default is 10000m + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + bestEffort: If the polygon would contain too many pixels at the given scale, compute and use a larger scale which would allow the operation to succeed. + maxPixels: The maximum number of pixels to reduce. Defaults to 1e7. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. Returns: A matplotlib axes with the reduced values for each band and each date. @@ -1483,7 +1580,18 @@ def plot_dates_by_bands( collection.geetools.plot_dates_by_bands(region, "mean", 10000, "system:time_start") """ # get the reduced data - raw_data = self.datesByBands(region, reducer, scale, dateProperty, bands, labels).getInfo() + raw_data = self.datesByBands( + region=region, + reducer=reducer, + dateProperty=dateProperty, + bands=bands, + labels=labels, + scale=scale, + crs=crs, + crsTransform=crsTransform, + bestEffort=bestEffort, + maxPixels=maxPixels, + ).getInfo() # transform all the dates int datetime objects def to_date(dict): @@ -1502,10 +1610,13 @@ def plot_dates_by_regions( regions: ee.FeatureCollection, label: str = "system:index", reducer: str | ee.Reducer = "mean", - scale: int = 10000, dateProperty: str = "system:time_start", colors: list = [], ax: Axes | None = None, + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + tileScale: float = 1, ) -> Axes: """Plot the reduced data for each image in the collection by regions for a single band. @@ -1516,10 +1627,13 @@ def plot_dates_by_regions( regions: The regions to reduce the data on. label: The property to use as label for each region. Default is "system:index". reducer: The name of the reducer or a reducer object to use. Default is "mean". - scale: The scale in meters to use for the reduction. default is 10000m dateProperty: The property to use as date for each image. Default is "system:time_start". colors: The colors to use for the regions. If empty, the default colors are used. ax: The matplotlib axes to plot the data on. If None, a new figure is created. + scale: The scale in meters to use for the reduction. default is 10000m + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. Returns: A matplotlib axes with the reduced values for each region and each date. @@ -1545,7 +1659,17 @@ def plot_dates_by_regions( collection.geetools.plot_dates_by_regions("B1", regions, "name", "mean", 10000, "system:time_start") """ # get the reduced data - raw_data = self.datesByRegions(band, regions, label, reducer, scale, dateProperty).getInfo() + raw_data = self.datesByRegions( + band=band, + regions=regions, + label=label, + reducer=reducer, + dateProperty=dateProperty, + scale=scale, + crs=crs, + crsTransform=crsTransform, + tileScale=tileScale, + ).getInfo() # transform all the dates int datetime objects def to_date(dict): @@ -1563,12 +1687,17 @@ def plot_doy_by_bands( region: ee.Geometry, spatialReducer: str | ee.Reducer = "mean", timeReducer: str | ee.Reducer = "mean", - scale: int = 10000, dateProperty: str = "system:time_start", bands: list = [], labels: list = [], colors: list = [], ax: Axes | None = None, + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + bestEffort: bool = False, + maxPixels: int | None = 10**7, + tileScale: float = 1, ) -> Axes: """Plot the reduced data for each image in the collection by bands on a specific region. @@ -1578,12 +1707,17 @@ def plot_doy_by_bands( region: The region to reduce the data on. spatialReducer: The name of the reducer or a reducer object to use. Default is "mean". timeReducer: The name of the reducer or a reducer object to use. Default is "mean". - scale: The scale in meters to use for the reduction. default is 10000m dateProperty: The property to use as date for each image. Default is "system:time_start". bands: The bands to reduce. If empty, all bands are reduced. labels: The labels to use for the bands. If empty, the bands names are used. colors: The colors to use for the bands. If empty, the default colors are used. ax: The matplotlib axes to plot the data on. If None, a new figure is created. + scale: The scale in meters to use for the reduction. default is 10000m + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + bestEffort: If the polygon would contain too many pixels at the given scale, compute and use a larger scale which would allow the operation to succeed. + maxPixels: The maximum number of pixels to reduce. Defaults to 1e7. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. Returns: A matplotlib axes with the reduced values for each band and each day. @@ -1606,7 +1740,18 @@ def plot_doy_by_bands( """ # get the reduced data raw_data = self.doyByBands( - region, spatialReducer, timeReducer, scale, dateProperty, bands, labels + region=region, + spatialReducer=spatialReducer, + timeReducer=timeReducer, + dateProperty=dateProperty, + bands=bands, + labels=labels, + scale=scale, + crs=crs, + crsTransform=crsTransform, + bestEffort=bestEffort, + maxPixels=maxPixels, + tileScale=tileScale, ).getInfo() # transform all the dates strings into int object and reorder the dictionary @@ -1627,10 +1772,13 @@ def plot_doy_by_regions( label: str = "system:index", spatialReducer: str | ee.Reducer = "mean", timeReducer: str | ee.Reducer = "mean", - scale: int = 10000, dateProperty: str = "system:time_start", colors: list = [], ax: Axes | None = None, + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + tileScale: float = 1, ) -> Axes: """Plot the reduced data for each image in the collection by regions for a single band. @@ -1642,10 +1790,13 @@ def plot_doy_by_regions( label: The property to use as label for each region. Default is "system:index". spatialReducer: The name of the reducer or a reducer object to use. Default is "mean". timeReducer: The name of the reducer or a reducer object to use. Default is "mean". - scale: The scale in meters to use for the reduction. default is 10000m dateProperty: The property to use as date for each image. Default is "system:time_start". colors: The colors to use for the regions. If empty, the default colors are used. ax: The matplotlib axes to plot the data on. If None, a new figure is created. + scale: The scale in meters to use for the reduction. default is 10000m + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. Returns: A matplotlib axes with the reduced values for each region and each day. @@ -1672,7 +1823,16 @@ def plot_doy_by_regions( """ # get the reduced data raw_data = self.doyByRegions( - band, regions, label, spatialReducer, timeReducer, scale, dateProperty + band=band, + regions=regions, + label=label, + spatialReducer=spatialReducer, + timeReducer=timeReducer, + dateProperty=dateProperty, + scale=scale, + crs=crs, + crsTransform=crsTransform, + tileScale=tileScale, ).getInfo() # transform all the dates strings into int object and reorder the dictionary @@ -1691,10 +1851,15 @@ def plot_doy_by_years( band: str, region: ee.Geometry, reducer: str | ee.Reducer = "mean", - scale: int = 10000, dateProperty: str = "system:time_start", colors: list = [], ax: Axes | None = None, + scale: int = 10000, + crs: str | None = None, + crsTransform: list | None = None, + bestEffort: bool = False, + maxPixels: int | None = 10**7, + tileScale: float = 1, ) -> Axes: """Plot the reduced data for each image in the collection by years for a single band. @@ -1704,10 +1869,15 @@ def plot_doy_by_years( band: The band to reduce. region: The region to reduce the data on. reducer: The name of the reducer or a reducer object to use. Default is "mean". - scale: The scale in meters to use for the reduction. default is 10000m dateProperty: The property to use as date for each image. Default is "system:time_start". colors: The colors to use for the regions. If empty, the default colors are used. ax: The matplotlib axes to plot the data on. If None, a new figure is created. + scale: The scale in meters to use for the reduction. default is 10000m + crs: The projection to work in. If unspecified, the projection of the image's first band is used. If specified in addition to scale, rescaled to the specified scale. + crsTransform: The list of CRS transform values. This is a row-major ordering of the 3x2 transform matrix. This option is mutually exclusive with 'scale', and replaces any transform already set on the projection. + bestEffort: If the polygon would contain too many pixels at the given scale, compute and use a larger scale which would allow the operation to succeed. + maxPixels: The maximum number of pixels to reduce. Defaults to 1e7. + tileScale: A scaling factor between 0.1 and 16 used to adjust aggregation tile size; setting a larger tileScale (e.g., 2 or 4) uses smaller tiles and may enable computations that run out of memory with the default. Returns: A matplotlib axes with the reduced values for each year and each day. @@ -1728,7 +1898,18 @@ def plot_doy_by_years( collection.geetools.plot_doy_by_years("B1", ee.Geometry.Point(-122.262, 37.8719).buffer(10000), "mean", 10000, "system:time_start") """ # get the reduced data - raw_data = self.doyByYears(band, region, reducer, scale, dateProperty).getInfo() + raw_data = self.doyByYears( + band=band, + region=region, + reducer=reducer, + dateProperty=dateProperty, + scale=scale, + crs=crs, + crsTransform=crsTransform, + bestEffort=bestEffort, + maxPixels=maxPixels, + tileScale=tileScale, + ).getInfo() # transform all the dates strings into int object and reorder the dictionary def to_int(d): @@ -1754,8 +1935,7 @@ def reduceRegion( crsTransform: list | None = None, bestEffort: bool = False, maxPixels: int | None = None, - tileScale: int = 1, - **kwargs, + tileScale: float = 1, ) -> ee.Dictionary: """Apply a reducer to all the pixels in a specific region on each image of the collection.