Skip to content

Commit

Permalink
renaming the carry_metadata decorator to keep_attrs and use black for…
Browse files Browse the repository at this point in the history
…matting
  • Loading branch information
KMarkert committed Oct 8, 2021
1 parent ceaa7f4 commit ddf64f6
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 246 deletions.
28 changes: 20 additions & 8 deletions hydrafloods/corrections.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
from hydrafloods import geeutils, decorators


@decorators.carry_metadata
def slope_correction(image, elevation, model="volume", buffer=0, scale=1000, in_units="db",out_units="same"):
@decorators.keep_attrs
def slope_correction(
image,
elevation,
model="volume",
buffer=0,
scale=1000,
in_units="db",
out_units="same",
):
"""This function applies the slope correction on a Sentinel-1 image.
Function based on https:# doi.org/10.3390/rs12111867.
Adapted from https:# github.com/ESA-PhiLab/radiometric-slope-correction/blob/master/notebooks/1%20-%20Generate%20Data.ipynb
Expand Down Expand Up @@ -70,7 +78,7 @@ def _erode(image, distance):
returns:
ee.Image
"""
"""

d = (
image.Not()
Expand Down Expand Up @@ -128,11 +136,15 @@ def _masking(alpha_rRad, theta_iRad, buffer):
.get("aspect")
)

if in_units not in ["db","power"]:
raise ValueError("could not understand input units. needs to be either 'db' or 'power'")
if in_units not in ["db", "power"]:
raise ValueError(
"could not understand input units. needs to be either 'db' or 'power'"
)

if out_units not in ["same","other",None]:
raise ValueError("could not understand output units option. needs to be either 'same' or 'other'")
if out_units not in ["same", "other", None]:
raise ValueError(
"could not understand output units option. needs to be either 'same' or 'other'"
)

# Sigma0 to Power of input image
if in_units == "db":
Expand Down Expand Up @@ -214,7 +226,7 @@ def _masking(alpha_rRad, theta_iRad, buffer):
)


@decorators.carry_metadata
@decorators.keep_attrs
def illumination_correction(image, elevation, model="rotation", scale=90, sensor="LC8"):
"""This function applies a terrain correction to optical imagery based on solar and viewing geometry
Expand Down
32 changes: 14 additions & 18 deletions hydrafloods/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def clip_to_region(self, inplace=False):
Dataset | None: returns dataset with imagery clipped to self.region or none depending on inplace
"""

@decorators.carry_metadata
@decorators.keep_attrs
def clip(img):
"""Closure function to perform the clipping while carrying metadata"""
return ee.Image(img.clip(self.region))
Expand Down Expand Up @@ -411,7 +411,7 @@ def _aggregation(d):
outCls.collection = out_coll
return outCls

@decorators.carry_metadata
@decorators.keep_attrs
def band_pass_adjustment(self, img):
"""Method to apply linear band transformation to dataset image collection.
Expects that dataset has properties `self.gain` and `self.bias` set
Expand All @@ -427,7 +427,7 @@ def band_pass_adjustment(self, img):
.set("system:time_start", img.get("system:time_start"))
)

def pipe(self, steps, inplace=False, carry_metadata=True):
def pipe(self, steps, inplace=False, keep_attrs=True):
"""Method to pipe imagery within dataset through multiple functions at once.
Assumes the first argument into piped functions are and ee.Image
Expand Down Expand Up @@ -481,8 +481,8 @@ def _piper(funcs):
fs.append(pfunc)

# get the piped function
if carry_metadata:
one_shot = decorators.carry_metadata(_piper(fs))
if keep_attrs:
one_shot = decorators.keep_attrs(_piper(fs))
else:
one_shot = _piper(fs)

Expand Down Expand Up @@ -522,7 +522,7 @@ def __init__(self, *args, asset_id="COPERNICUS/S1_GRD", use_qa=True, **kwargs):

return

@decorators.carry_metadata
@decorators.keep_attrs
def qa(self, img):
"""Custom QA masking method for Sentinel1 backscatter based on view angle
Angle threshold values taken from https://doi.org/10.3390/rs13101954
Expand Down Expand Up @@ -586,9 +586,7 @@ class Sentinel1Asc(Sentinel1):
This Sentinel 1 dataset is in power units
"""

def __init__(
self, *args, asset_id="COPERNICUS/S1_GRD", use_qa=True, **kwargs
):
def __init__(self, *args, asset_id="COPERNICUS/S1_GRD", use_qa=True, **kwargs):
"""Initialize Sentinel1 Dataset class
args:
Expand All @@ -613,9 +611,7 @@ class Sentinel1Desc(Sentinel1):
This Sentinel dataset is in power units
"""

def __init__(
self, *args, asset_id="COPERNICUS/S1_GRD", use_qa=True, **kwargs
):
def __init__(self, *args, asset_id="COPERNICUS/S1_GRD", use_qa=True, **kwargs):
"""Initialize Sentinel1 Dataset class
args:
Expand Down Expand Up @@ -681,7 +677,7 @@ def __init__(

return

@decorators.carry_metadata
@decorators.keep_attrs
def qa(self, img):
"""Custom QA masking method for VIIRS VNP09GA dataset"""
cloudMask = geeutils.extract_bits(
Expand Down Expand Up @@ -724,7 +720,7 @@ def __init__(

return

@decorators.carry_metadata
@decorators.keep_attrs
def qa(self, img):
"""Custom QA masking method for MODIS MXD09GA dataset"""
qa = img.select("state_1km")
Expand Down Expand Up @@ -768,7 +764,7 @@ def __init__(

return

@decorators.carry_metadata
@decorators.keep_attrs
def qa(self, img):
"""Custom QA masking method for Landsat8 surface reflectance dataset"""
qa_band = img.select("pixel_qa")
Expand Down Expand Up @@ -827,7 +823,7 @@ def __init__(

return

@decorators.carry_metadata
@decorators.keep_attrs
def qa(self, img):
"""Custom QA masking method for Landsat7 surface reflectance dataset"""
qa_band = img.select("pixel_qa")
Expand Down Expand Up @@ -900,7 +896,7 @@ def __init__(

return

@decorators.carry_metadata
@decorators.keep_attrs
def qa(self, img):
"""Custom QA masking method for Landsat7 surface reflectance dataset"""
qa_band = img.select("pixel_qa")
Expand Down Expand Up @@ -958,7 +954,7 @@ def __init__(

return

@decorators.carry_metadata
@decorators.keep_attrs
def qa(self, img):
"""Custom QA masking method for Sentinel2 surface reflectance dataset"""
CLD_PRB_THRESH = 40
Expand Down
14 changes: 7 additions & 7 deletions hydrafloods/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from hydrafloods import geeutils, decorators


@decorators.carry_metadata
@decorators.keep_attrs
def lee_sigma(img, window=9, sigma=0.9, looks=4, tk=7, keep_bands=["angle"]):
"""Lee Sigma speckle filtering algorithm.
Implemented from interpreting https://doi.org/10.1109/TGRS.2008.2002881
Expand Down Expand Up @@ -128,7 +128,7 @@ def lee_sigma(img, window=9, sigma=0.9, looks=4, tk=7, keep_bands=["angle"]):


# The RL speckle filter
@decorators.carry_metadata
@decorators.keep_attrs
def refined_lee(image, keep_bands=["angle"]):
"""Refined Lee speckle filtering algorithm.
Algorithm adapted from https://groups.google.com/g/google-earth-engine-developers/c/ExepnAmP-hQ/m/7e5DnjXXAQAJ
Expand Down Expand Up @@ -342,7 +342,7 @@ def apply_filter(b):
return output


@decorators.carry_metadata
@decorators.keep_attrs
def gamma_map(img, window=7, enl=4.9, keep_bands=["angle"]):
"""Gamma Map speckle filtering algorithm.
Algorithm adapted from https://groups.google.com/g/google-earth-engine-developers/c/a9W0Nlrhoq0/m/tnGMC45jAgAJ.
Expand Down Expand Up @@ -432,7 +432,7 @@ def gamma_map(img, window=7, enl=4.9, keep_bands=["angle"]):
return output


@decorators.carry_metadata
@decorators.keep_attrs
def p_median(img, window=5, keep_bands=["angle"]):
"""P-Median filter for smoothing imagery.
Calculates the average from the median along cross and diagnal pixels of a window
Expand Down Expand Up @@ -483,7 +483,7 @@ def _band_filter(bname):
return reduced_bands.rename(band_names)


@decorators.carry_metadata
@decorators.keep_attrs
def perona_malik(img, n_iters=10, K=3, method=1):
"""Perona-Malik (anisotropic diffusion) convolution
Developed by Gennadii Donchyts see https://groups.google.com/g/google-earth-engine-developers/c/umGlt5qIN1I/m/PD8lsJ7qBAAJ
Expand Down Expand Up @@ -560,7 +560,7 @@ def _method_2(dI_W, dI_E, dI_N, dI_S):
return img


@decorators.carry_metadata
@decorators.keep_attrs
def open_binary(img, window=3, neighborhood=None):
"""Opening morphological filter. Opening is the dilation of the erosion of
values greater than 1.
Expand Down Expand Up @@ -588,7 +588,7 @@ def open_binary(img, window=3, neighborhood=None):
return opened


@decorators.carry_metadata
@decorators.keep_attrs
def close_binary(img, window=3, neighborhood=None):
"""Closing morphological filter. Closing is the erosion of the dialiation of
values greater than 1.
Expand Down
2 changes: 1 addition & 1 deletion hydrafloods/fusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def starfm(
coarseCollection, fineCollection=None, targetDate="1970-01-01", windowSize=33, A=0.5
):
@decorators.carry_metadata
@decorators.keep_attrs
def apply_starfm(img):
t = ee.Date(img.get("system:time_start"))

Expand Down
Loading

0 comments on commit ddf64f6

Please sign in to comment.