Skip to content

Commit

Permalink
Merge pull request #78 from GeoscienceAustralia/seasonal_correction
Browse files Browse the repository at this point in the history
Add seasonal correction to correlation calculation
  • Loading branch information
vnewey authored Mar 22, 2024
2 parents 201d3ef + 7b8b57a commit 7a7dd6a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
32 changes: 30 additions & 2 deletions intertidal/elevation.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def ds_to_flat(
max_freq=0.99,
min_correlation=0.15,
corr_method="pearson",
correct_seasonality=False,
valid_mask=None,
):
"""
Expand Down Expand Up @@ -77,6 +78,13 @@ def ds_to_flat(
corr_method : str, optional
Correlation method to use. Defaults to "pearson", also supports
"spearman".
correct_seasonality : bool, optional
If True, remove any seasonal signal from the tide height data
by subtracting monthly mean tide height from each value. This
can reduce false tide correlations in regions where tide heights
correlate with seasonal changes in surface water. Note that
seasonally corrected tides are only used to identify potentially
tide influenced pixels - not for elevation modelling itself.
valid_mask : xr.DataArray, optional
A boolean mask used to optionally constrain the analysis area,
with the same spatial dimensions as `satellite_ds`. For example,
Expand Down Expand Up @@ -123,13 +131,22 @@ def ds_to_flat(
# correlation. This prevents small changes in NDWI beneath the water
# surface from producing correlations with tide height.
wet_dry = flat_ds[index] > ndwi_thresh

# Use either tides directly or correct to remove seasonal signal
if correct_seasonality:
print("Removing seasonal signal before calculating tide correlations")
gb = flat_ds.tide_m.groupby('time.month')
tide_array = (gb - gb.mean())
else:
tide_array = flat_ds.tide_m

if corr_method == "pearson":
corr = xr.corr(wet_dry, flat_ds.tide_m, dim="time").rename("qa_ndwi_corr")
corr = xr.corr(wet_dry, tide_array, dim="time").rename("qa_ndwi_corr")
elif corr_method == "spearman":
import xskillscore

corr = xskillscore.spearman_r(
flat_ds[index], flat_ds.tide_m, dim="time", skipna=True, keep_attrs=True
flat_ds[index], tide_array, dim="time", skipna=True, keep_attrs=True
).rename("qa_ndwi_corr")

# TODO: investigate alternative function from DEA Tools
Expand Down Expand Up @@ -752,6 +769,7 @@ def elevation(
min_correlation=0.15,
windows_n=100,
window_prop_tide=0.15,
correct_seasonality=False,
max_workers=None,
tide_model="FES2014",
tide_model_dir="/var/share/tide_models",
Expand Down Expand Up @@ -788,6 +806,14 @@ def elevation(
window_prop_tide : float, optional
Proportion of the tide range to use for each window radius in
the per-pixel rolling median calculation, by default 0.15
correct_seasonality : bool, optional
If True, remove any seasonal signal from the tide height data
by subtracting monthly mean tide height from each value prior to
correlation calculations. This can reduce false tide correlations
in regions where tide heights correlate with seasonal changes in
surface water. Note that seasonally corrected tides are only used
to identify potentially tide influenced pixels - not for elevation
modelling itself.
max_workers : int, optional
Maximum number of worker processes to use for parallel execution
in the per-pixel rolling median calculation. Defaults to None,
Expand Down Expand Up @@ -873,6 +899,7 @@ def elevation(
min_freq=min_freq,
max_freq=max_freq,
min_correlation=min_correlation,
correct_seasonality=correct_seasonality,
valid_mask=valid_mask,
)

Expand Down Expand Up @@ -1172,6 +1199,7 @@ def intertidal_cli(
min_correlation=min_correlation,
windows_n=windows_n,
window_prop_tide=window_prop_tide,
correct_seasonality=True,
tide_model=tide_model,
tide_model_dir=tide_model_dir,
run_id=run_id,
Expand Down
2 changes: 1 addition & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Integration tests
This directory contains tests that are run to verify that DEA Intertidal code runs correctly. The ``test_intertidal.py`` file runs a small-scale full workflow analysis over an intertidal flat in the Gulf of Carpentaria using the DEA Intertidal [Command Line Interface (CLI) tools](../notebooks/Intertidal_CLI.ipynb), and compares these results against a LiDAR validation DEM to produce some simple accuracy metrics.

The latest integration test completed at **2024-03-14 12:50**. Compared to the previous run, it had an:
The latest integration test completed at **2024-03-22 14:25**. Compared to the previous run, it had an:
- RMSE accuracy of **0.14 m ( :heavy_minus_sign: no change)**
- MAE accuracy of **0.12 m ( :heavy_minus_sign: no change)**
- Bias of **0.12 m ( :heavy_minus_sign: no change)**
Expand Down
1 change: 1 addition & 0 deletions tests/validation.csv
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ time,Correlation,RMSE,MAE,R-squared,Bias,Regression slope
2024-03-13 00:54:01.731099+00:00,0.975,0.141,0.121,0.95,0.116,1.11
2024-03-13 23:17:46.582372+00:00,0.975,0.141,0.121,0.95,0.116,1.11
2024-03-14 01:50:20.512235+00:00,0.975,0.141,0.121,0.95,0.116,1.11
2024-03-22 03:25:50.523558+00:00,0.975,0.141,0.121,0.95,0.116,1.11
Binary file modified tests/validation.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7a7dd6a

Please sign in to comment.