Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MoV code to use xCDAT #1020

Merged
merged 101 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
7c54910
add more stats for MoV driver
lee1043 Jan 8, 2024
86ca9b2
update
lee1043 Jan 9, 2024
a15e5c8
update eofs to v1.4.1
lee1043 Jan 10, 2024
8ca46ad
clean up
lee1043 Jan 11, 2024
9cc5783
pre-commit fix
lee1043 Jan 11, 2024
05fafeb
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Jan 11, 2024
5dc869f
some functions moved to io
lee1043 Jan 11, 2024
585c867
clean up
lee1043 Jan 12, 2024
1579cf1
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Jan 12, 2024
80dbdba
update
lee1043 Jan 14, 2024
60feadc
clean up
lee1043 Jan 15, 2024
53ec879
duplicate string constructor to io because of circular import error
lee1043 Jan 16, 2024
1e2073f
pre-commit fix
lee1043 Jan 16, 2024
ed93d69
use fill_template from io instead of utils
lee1043 Jan 16, 2024
79902b7
use calcTCOR from newer position and pre-commit fix
lee1043 Jan 16, 2024
e4d3498
update
lee1043 Jan 17, 2024
a087755
update
lee1043 Jan 17, 2024
348b859
clean up, add regrid utils
lee1043 Jan 17, 2024
ccbc08d
debug and updates
lee1043 Jan 18, 2024
02d3068
bug fix (continue)
lee1043 Jan 18, 2024
a0a716a
bug fix
lee1043 Jan 24, 2024
0fa57f6
add north test as a part of the driver
lee1043 Jan 24, 2024
ce979f7
bug fix
lee1043 Jan 24, 2024
e495fee
bug fix
lee1043 Jan 25, 2024
8a606bc
pre-commit fix
lee1043 Jan 25, 2024
ad84f7a
clean up
lee1043 Jan 25, 2024
2d89f57
pre-commit fix
lee1043 Jan 25, 2024
3c1a873
bug fix
lee1043 Jan 26, 2024
60772d4
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Jan 26, 2024
052d3e8
bug fix
lee1043 Jan 26, 2024
c1a28b1
bug fix
lee1043 Jan 26, 2024
8d10543
simplify, clean up, add link to PMP installation
lee1043 Jan 26, 2024
6bb67ae
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Jan 26, 2024
64a8b31
add logo and clean up in the demo notebook
lee1043 Jan 27, 2024
28e6cf8
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Jan 27, 2024
f968029
clean up
lee1043 Jan 30, 2024
5f039de
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Feb 1, 2024
8c8caeb
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Feb 2, 2024
f2c1593
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Feb 7, 2024
426e7fc
logic simplified
lee1043 Feb 7, 2024
8d4f9de
clean up
lee1043 Feb 8, 2024
6bc7232
update
lee1043 Feb 12, 2024
84e5f9d
Merge pull request #1060 from PCMDI/feature/lee1043-mov-modularize
lee1043 Feb 22, 2024
fbca25c
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Feb 22, 2024
c9c5d3c
make code calender flexible -- reduced calendar dependency
lee1043 Feb 22, 2024
131df5c
pre-commit fix
lee1043 Feb 22, 2024
65b69c4
bug fix
lee1043 Feb 26, 2024
cf6fb8a
move timeseries adjustment in a new separate file
lee1043 Feb 26, 2024
ae02779
Merge branch 'feature/1012_lee1043_stats-MoV_xcdat' into feature/lee1…
lee1043 Feb 26, 2024
dd09f56
Merge pull request #1062 from PCMDI/feature/lee1043-mov-modularize
lee1043 Feb 26, 2024
be22674
separate adjust timeseries
lee1043 Feb 26, 2024
5f9ca3e
clean up
lee1043 Feb 26, 2024
51af8ca
Merge branch 'feature/1012_lee1043_stats-MoV_xcdat' of github.com:PCM…
lee1043 Feb 26, 2024
dd91e78
clean up
lee1043 Feb 26, 2024
3df31a1
update
lee1043 Feb 27, 2024
0544c51
Merge branch 'feature/1012_lee1043_stats-MoV_xcdat' of github.com:PCM…
lee1043 Feb 29, 2024
12ed70b
clean up
lee1043 Feb 29, 2024
156a3e4
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Mar 4, 2024
cc569fb
rename to simplify
lee1043 Mar 4, 2024
8cdfa78
clean up + bug fix
lee1043 Mar 7, 2024
61da5b6
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Mar 7, 2024
496ed5f
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Mar 12, 2024
3b2dceb
pre-commit fix
lee1043 Mar 12, 2024
8ec3dac
clean up
lee1043 Mar 12, 2024
000fe20
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Mar 15, 2024
9c09dc4
add demo script but for pcmdi internal
lee1043 Apr 4, 2024
81c794f
bug fix
lee1043 Apr 4, 2024
db0a396
bug fix
lee1043 Apr 4, 2024
00fc735
add missing bounds for sanity check
lee1043 Apr 4, 2024
f2f6736
in progress...
lee1043 Apr 4, 2024
0e18825
clean up..
lee1043 Apr 4, 2024
31c4298
fix bug for SAM region
lee1043 Apr 4, 2024
c6f6a81
enable automatic assignment of eofn_obs and eofn_mod by mode name
lee1043 Apr 4, 2024
f84c310
pre-commit clean up
lee1043 Apr 4, 2024
dee6096
remove eofn_obs and eofn_mod from pcmdi params
lee1043 Apr 5, 2024
77158d4
clean up
lee1043 Apr 5, 2024
6b2a562
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Apr 5, 2024
a5fa6cd
bug fix
lee1043 Apr 15, 2024
219334b
bug fix for sign flip -- revealed by SAM test
lee1043 Apr 15, 2024
f48ea9a
update required xcdat version regarding https://github.com/PCMDI/pcmd…
lee1043 Apr 16, 2024
5880600
pre-commit fix
lee1043 Apr 16, 2024
5ad2b21
moved missing bounds adding to io function
lee1043 Apr 16, 2024
376bffb
bug fix for centered rmse
lee1043 Apr 17, 2024
b115384
pre-commit fix
lee1043 Apr 17, 2024
b14c5fb
reduce potential memory usage
lee1043 Apr 17, 2024
719d4a3
bug fix: normalize by map std for centered RMSE calculation
lee1043 Apr 19, 2024
c4d2791
keep updated
lee1043 Apr 24, 2024
113d045
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Apr 24, 2024
d886049
clean up and simplified
lee1043 Apr 24, 2024
6a414c6
Merge branch 'feature/1012_lee1043_stats-MoV_xcdat' of github.com:PCM…
lee1043 Apr 24, 2024
436cfc9
initial commit for custom season capability
lee1043 Apr 26, 2024
e600190
add custom season capability
lee1043 Apr 26, 2024
b9e5aea
updated notebook to include custom season
lee1043 Apr 26, 2024
c6d9e3c
Merge pull request #1085 from PCMDI/feature/1012_lee1043_stats-MoV_xc…
lee1043 Apr 26, 2024
5aac6f7
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Apr 26, 2024
abbbd9a
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 Apr 30, 2024
91bb1c9
bug fix
lee1043 May 1, 2024
1392682
clean up
lee1043 May 1, 2024
893c5b3
pre-commit fix
lee1043 May 1, 2024
04829b1
Merge branch 'main' into feature/1012_lee1043_stats-MoV_xcdat
lee1043 May 2, 2024
2bd8aec
clean up, more debug printout added
lee1043 May 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
add more stats for MoV driver
  • Loading branch information
lee1043 committed Jan 8, 2024
commit 7c54910a3580c5be9b31fe3c86cb3d72d2e7dd37
7 changes: 6 additions & 1 deletion pcmdi_metrics/stats/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .compute_statistics import (
from .compute_statistics_dataset import (
annual_mean,
bias_xy,
bias_xyt,
Expand All @@ -15,3 +15,8 @@
std_xyt,
zonal_mean,
)

from .compute_statistics_dataArray import (
calculate_temporal_correlation,
calculate_spatial_correlation
)
51 changes: 51 additions & 0 deletions pcmdi_metrics/stats/compute_statistics_dataArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from pcmdi_metrics.io import get_time_key, get_latitude_key, get_longitude_key
import xarray as xr
from typing import Union


def calculate_temporal_correlation(data_array1: xr.DataArray, data_array2: xr.DataArray, dim:Union [str, tuple] = None) -> float:
return _calculate_correlation(data_array1, data_array2, method='temporal', dim=None)


def calculate_spatial_correlation(data_array1: xr.DataArray, data_array2: xr.DataArray, wtarray: xr.DataArray = None, dim:Union [str, tuple] = None) -> float:
return _calculate_correlation(data_array1, data_array2, method='spatial', wtarray=wtarray, dim=None)


def _calculate_correlation(data_array1: xr.DataArray, data_array2: xr.DataArray, wtarray: xr.DataArray = None, method='temporal', dim:Union [str, tuple] = None) -> float:
"""
Calculate correlation between two xarray DataArrays.

Parameters:
- data_array1 (xarray.DataArray): First data array.
- data_array2 (xarray.DataArray): Second data array.
- method (str): Method for correlation calculation ('temporal' or 'spatial').
- dim (tuple or str): Dimensions along which to compute the correlation.
For 'temporal' correlation, specify the time dimension.
For 'spatial' correlation, specify the spatial dimensions.

Returns:
- correlation (float): Correlation coefficient.
"""

if method not in ['temporal', 'spatial']:
raise ValueError("Invalid method. Use 'temporal' or 'spatial'.")

if method == 'temporal':
# Check if the specified dimension exists in both DataArrays
if dim is None:
dim = get_time_key(data_array1)
if (dim not in data_array1.dims or dim not in data_array2.dims):
raise ValueError(f"Temporal dimension '{dim}' not found in both input DataArrays.")

elif method == 'spatial':
# Check if the specified dimensions exist in both DataArrays
if dim is None:
lat_key = get_latitude_key(data_array1)
lon_key = get_longitude_key(data_array1)
dim = (lat_key, lon_key)
if (not isinstance(dim, tuple) and dim not in data_array1.dims) or (isinstance(dim, tuple) and any(d not in data_array1.dims or d not in data_array2.dims for d in dim)):
raise ValueError(f"Spatial dimension '{dim}' not found in both input DataArrays.")

correlation = xr.corr(data_array1, data_array2, dim=dim).values.item()

return correlation
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

import numpy as np
import xcdat as xc
import xarray as xr
from typing import Union



def check_data_convert_to_ds_if_needed(da: Union[xr.Dataset, xr.DataArray], var: str=None):
if isinstance(da, xr.Dataset):
return da
elif isinstance(da, xr.DataArray):
if var is None:
var = "variable" # assign arbitrary variable name
return da.to_dataset(name=var)
else:
raise TypeError("Input must be an instance of DataArray or Dataset")



def annual_mean(dm, do, var=None):
Expand All @@ -13,6 +28,10 @@ def annual_mean(dm, do, var=None):
"Contact": "[email protected]",
"Comments": "Assumes input are 12 months climatology",
}

dm = check_data_convert_to_ds_if_needed(dm, var)
do = check_data_convert_to_ds_if_needed(do, var)

dm_am = dm.temporal.average(var)
do_am = do.temporal.average(var)
return dm_am, do_am # DataSets
Expand Down Expand Up @@ -64,6 +83,10 @@ def bias_xy(dm, do, var=None, weights=None):
"Abstract": "Compute Full Average of Model - Observation",
"Contact": "[email protected]",
}

dm = check_data_convert_to_ds_if_needed(dm, var)
do = check_data_convert_to_ds_if_needed(do, var)

dif = dm[var] - do[var]
if weights is None:
weights = dm.spatial.get_weights(axis=["X", "Y"])
Expand All @@ -79,6 +102,10 @@ def bias_xyt(dm, do, var=None):
"Abstract": "Compute Full Average of Model - Observation",
"Contact": "[email protected]",
}

dm = check_data_convert_to_ds_if_needed(dm, var)
do = check_data_convert_to_ds_if_needed(do, var)

ds = dm.copy(deep=True)
ds["dif"] = dm[var] - do[var]
stat = (
Expand All @@ -95,6 +122,10 @@ def cor_xy(dm, do, var=None, weights=None):
"Abstract": "Compute Spatial Correlation",
"Contact": "[email protected]",
}

dm = check_data_convert_to_ds_if_needed(dm, var)
do = check_data_convert_to_ds_if_needed(do, var)

if weights is None:
weights = dm.spatial.get_weights(axis=["X", "Y"])

Expand Down Expand Up @@ -123,6 +154,8 @@ def mean_xy(d, var=None, weights=None):
"Contact": "[email protected]",
}

d = check_data_convert_to_ds_if_needed(d, var)

if weights is None:
weights = d.spatial.get_weights(axis=["X", "Y"])
stat = float(d[var].weighted(weights).mean(("lon", "lat")))
Expand All @@ -140,6 +173,10 @@ def meanabs_xy(dm, do, var=None, weights=None):
}
if weights is None:
weights = dm.spatial.get_weights(axis=["X", "Y"])

dm = check_data_convert_to_ds_if_needed(dm, var)
do = check_data_convert_to_ds_if_needed(do, var)

dif = abs(dm[var] - do[var])
stat = dif.weighted(weights).mean(("lon", "lat"))
return float(stat)
Expand All @@ -154,6 +191,10 @@ def meanabs_xyt(dm, do, var=None):
+ "Absolute Difference Between Model And Observation",
"Contact": "[email protected]",
}

dm = check_data_convert_to_ds_if_needed(dm, var)
do = check_data_convert_to_ds_if_needed(do, var)

ds = dm.copy(deep=True)
ds["absdif"] = abs(dm[var] - do[var])
stat = (
Expand All @@ -172,6 +213,10 @@ def rms_0(dm, do, var=None, weighted=True):
"Abstract": "Compute Root Mean Square over the first axis",
"Contact": "[email protected]",
}

dm = check_data_convert_to_ds_if_needed(dm, var)
do = check_data_convert_to_ds_if_needed(do, var)

dif_square = (dm[var] - do[var]) ** 2
if weighted:
weights = dm.spatial.get_weights(axis=["Y"])
Expand All @@ -189,6 +234,10 @@ def rms_xy(dm, do, var=None, weights=None):
"Abstract": "Compute Spatial Root Mean Square",
"Contact": "[email protected]",
}

dm = check_data_convert_to_ds_if_needed(dm, var)
do = check_data_convert_to_ds_if_needed(do, var)

dif_square = (dm[var] - do[var]) ** 2
if weights is None:
weights = dm.spatial.get_weights(axis=["X", "Y"])
Expand All @@ -204,6 +253,10 @@ def rms_xyt(dm, do, var=None):
"Abstract": "Compute Spatial and Temporal Root Mean Square",
"Contact": "[email protected]",
}

dm = check_data_convert_to_ds_if_needed(dm, var)
do = check_data_convert_to_ds_if_needed(do, var)

ds = dm.copy(deep=True)
ds["diff_square"] = (dm[var] - do[var]) ** 2
ds["diff_square_sqrt"] = np.sqrt(
Expand All @@ -221,6 +274,10 @@ def rmsc_xy(dm, do, var=None, weights=None):
"Abstract": "Compute Centered Spatial Root Mean Square",
"Contact": "[email protected]",
}

dm = check_data_convert_to_ds_if_needed(dm, var)
do = check_data_convert_to_ds_if_needed(do, var)

if weights is None:
weights = dm.spatial.get_weights(axis=["X", "Y"])

Expand Down Expand Up @@ -259,6 +316,8 @@ def std_xy(ds, var=None, weights=None):
if weights is None:
weights = ds.spatial.get_weights(axis=["X", "Y"])

ds = check_data_convert_to_ds_if_needed(ds, var)

lat_key = xc.axis.get_dim_keys(ds, axis="Y")
lon_key = xc.axis.get_dim_keys(ds, axis="X")

Expand All @@ -278,6 +337,7 @@ def std_xyt(d, var=None):
"Contact": "[email protected]",
}
ds = d.copy(deep=True)
ds = check_data_convert_to_ds_if_needed(ds, var)
average = d.spatial.average(var, axis=["X", "Y"]).temporal.average(var)[var]
ds["anomaly"] = (d[var] - average) ** 2
variance = (
Expand All @@ -296,6 +356,9 @@ def zonal_mean(dm, do, var=None):
"Contact": "[email protected]",
"Comments": "",
}
dm = check_data_convert_to_ds_if_needed(dm, var)
do = check_data_convert_to_ds_if_needed(do, var)

dm_zm = dm.spatial.average(var, axis=["X"])
do_zm = do.spatial.average(var, axis=["X"])
return dm_zm, do_zm # DataSets