Skip to content

Commit

Permalink
Refactor CC metric, add explicit resolution kwargs to ERGAS (r-earthe…
Browse files Browse the repository at this point in the history
  • Loading branch information
aazuspan committed Jan 18, 2022
1 parent 479e75c commit 8147089
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions ee_extra/QA/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,14 @@ def _calculate(
class ERGAS(Metric):
"""Calculate image-wise Dimensionless Global Relative Error of Synthesis
(ERGAS) between an original and modified image with the same resolution and bands.
A value of 0 represents no error.
A value of 0 represents no error. ERGAS is intended to be used for assessing
pan-sharpening results, and results are weighted by the change in spatial resolution.
Args:
original : The original image to use as a reference.
modified : The modified image to compare to the original.
h : Spatial resolution of the sharpened image.
l : Spatial resolution of the unsharpened image.
kwargs : Additional keyword arguments passed to `ee.Image.reduceRegion`.
Returns:
Expand All @@ -208,11 +211,11 @@ class ERGAS(Metric):

@staticmethod
def _calculate(
original: ee.Image, modified: ee.Image, **kwargs: Any
original: ee.Image, modified: ee.Image, h: int, l: int, **kwargs: Any
) -> ee.Dictionary:

h = modified.projection().nominalScale()
l = original.projection().nominalScale()
h = ee.Number(h)
l = ee.Number(l)
msek = ee.Array(MSE(original, modified, **kwargs).values())
xbark = ee.Array(original.reduceRegion(ee.Reducer.mean(), **kwargs).values())

Expand Down Expand Up @@ -342,26 +345,14 @@ def _calculate(
modified.reduceRegion(ee.Reducer.mean(), **kwargs).values()
)

x_center = original.subtract(xbar)
y_center = modified.subtract(ybar)

numerator = ee.Array(
x_center.multiply(y_center)
.reduceRegion(ee.Reducer.sum(), **kwargs)
.values()
)

x_denom = ee.Array(
x_center.pow(2).reduceRegion(ee.Reducer.sum(), **kwargs).values()
)
y_denom = ee.Array(
y_center.pow(2).reduceRegion(ee.Reducer.sum(), **kwargs).values()
)

denom = x_denom.multiply(y_denom).sqrt()
a = original.subtract(xbar)
b = modified.subtract(ybar)

cc = numerator.divide(denom)
x1 = ee.Array(a.multiply(b).reduceRegion(ee.Reducer.sum(), **kwargs).values())
x2 = ee.Array(a.pow(2).reduceRegion(ee.Reducer.sum(), **kwargs).values())
x3 = ee.Array(b.pow(2).reduceRegion(ee.Reducer.sum(), **kwargs).values())

cc = x1.divide(x2.multiply(x3).sqrt())
return ee.Dictionary.fromLists(original.bandNames(), cc.toList())


Expand Down

0 comments on commit 8147089

Please sign in to comment.