Skip to content

Commit

Permalink
Merge pull request #20 from kthyng/simplify_demo
Browse files Browse the repository at this point in the history
Simplify demo
  • Loading branch information
kthyng authored Aug 8, 2022
2 parents f49c04e + 5968baa commit 0103d45
Show file tree
Hide file tree
Showing 15 changed files with 540 additions and 50 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest"]
python-version: ["3.7", "3.8"]
os: ["macos-latest", "ubuntu-latest", "windows-latest"]
python-version: ["3.7", "3.8", "3.9"]
steps:
- uses: actions/checkout@v2
- name: Cache conda
Expand All @@ -22,9 +22,9 @@ jobs:
key: ${{ runner.os }}-conda-${{ env.CACHE_NUMBER }}-${{ hashFiles('ci/environment-py${{ matrix.python-version }}.yml') }}
- uses: conda-incubator/setup-miniconda@v2
with:
# mamba-version: "*" # activate this to build with mamba.
# channels: conda-forge, defaults # These need to be specified to use mamba
# channel-priority: true
mamba-version: "*" # activate this to build with mamba.
channels: conda-forge, defaults # These need to be specified to use mamba
channel-priority: true
environment-file: ci/environment-py${{ matrix.python-version }}.yml

activate-environment: test_env_model_assessor
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ repos:
- id: seed-isort-config

- repo: https://github.com/psf/black
rev: stable
rev: 22.3.0 # https://github.com/psf/black/issues/2964
hooks:
- id: black
language_version: python3
Expand Down
12 changes: 12 additions & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2

build:
os: "ubuntu-20.04"
tools:
python: "mambaforge-4.10"

conda:
environment: docs/environment.yml

sphinx:
configuration: docs/conf.py
2 changes: 1 addition & 1 deletion ci/environment-py3.7.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies:
- cartopy
- cf_xarray
- extract_model
- matplotlib
- matplotlib-base
- numpy
- ocean_data_gateway
- pandas
Expand Down
2 changes: 1 addition & 1 deletion ci/environment-py3.8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies:
- cartopy
- cf_xarray
- extract_model
- matplotlib
- matplotlib-base
- numpy
- ocean_data_gateway
- pandas
Expand Down
21 changes: 21 additions & 0 deletions ci/environment-py3.9.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: test_env_model_assessor
channels:
- conda-forge
dependencies:
- python=3.9
############## These will have to be adjusted to your specific project
- cartopy
- cf_xarray
- extract_model
- matplotlib-base
- numpy
- ocean_data_gateway
- pandas
- scipy
- xarray
##############
- pytest
- pip:
- codecov
- pytest-cov
- coverage[toml]
219 changes: 219 additions & 0 deletions docs/Demo.ipynb

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions docs/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ dependencies:
# - cartopy
# - matplotlib
# - ipython
- jupyter
# - jupyter
# - jupyterlab
- ocean_model_skill_assessor
# - xarray
# These are needed for the docs themselves
- numpydoc
# https://stackoverflow.com/questions/68642540/nbsphinx-causes-build-to-fail-when-building-jupyter-notebooks
- sphinx==4.0.2
- sphinx
- sphinx_rtd_theme
- ipython
- nbconvert
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Welcome to ocean-model-skill-assessor's documentation!
.. toctree::
:maxdepth: 3

Demo
Demo_workflows
api

Expand Down
219 changes: 219 additions & 0 deletions notebooks/Demo.ipynb

Large diffs are not rendered by default.

47 changes: 36 additions & 11 deletions ocean_model_skill_assessor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def run(
erddap=None,
figname_map=None,
figname_data_prefix="",
horizontal_interp=False,
local=None,
only_search=False,
only_searchplot=False,
Expand Down Expand Up @@ -238,6 +239,9 @@ def run(
Figure name for map showing data locations.
figname_data_prefix : str, optional
Prefix for figures for dataset-model comparisons.
horizontal_interp : bool, optional
If True, use `em.select()` to interpolate to the data location horizontally.
If False, use `em.sel2d()` to use the nearest grid point to the data location.
local : dict, optional
Inputs for local reader.
only_search : boolean, optional
Expand Down Expand Up @@ -361,18 +365,39 @@ def run(
continue

for variable in variables:
kwargs = dict(
da=dsm.cf[variable]
.cf.isel(Z=0)
.cf.sel(lon=slice(lon - 5, lon + 5), lat=slice(lat - 5, lat + 5)),
longitude=lon,
latitude=lat,
T=T,
iZ=Z,
locstream=True,
)

model_var = em.select(**kwargs).to_dataset()
if horizontal_interp:
kwargs = dict(
da=dsm.cf[variable]
.cf.isel(Z=0)
.cf.sel(lon=slice(lon - 5, lon + 5), lat=slice(lat - 5, lat + 5)),
longitude=lon,
latitude=lat,
T=T,
iZ=Z,
locstream=True,
)

model_var = em.select(**kwargs).to_dataset()

else:
kwargs = dict(
longitude=lon,
latitude=lat,
# T=T,
Z=0,
method="nearest",
)
if T is not None:
kwargs["T"] = T

# xoak doesn't work for 1D lon/lat coords
if dsm.cf["longitude"].ndim == dsm.cf["latitude"].ndim == 1:
model_var = dsm.cf[variable].cf.sel(**kwargs).to_dataset()

elif dsm.cf["longitude"].ndim == dsm.cf["latitude"].ndim == 2:
model_var = dsm.cf[variable].em.sel2dcf(**kwargs).to_dataset()

# Combine and align the two time series of variable
df = omsa.stats._align(data.cf[variable], model_var.cf[variable])
stats = df.omsa.compute_stats
Expand Down
2 changes: 1 addition & 1 deletion ocean_model_skill_assessor/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def compute_mean_square_error(
error = obs - model
if centered:
error += -obs.mean() + model.mean()
return (error ** 2).mean()
return (error**2).mean()


def compute_murphy_skill_score(
Expand Down
9 changes: 0 additions & 9 deletions readthedocs.yml

This file was deleted.

5 changes: 3 additions & 2 deletions tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
def test_time_series():
ref_times = pd.date_range(start="2000-12-30", end="2001-01-03", freq="6H")
reference = pd.DataFrame(
{"reference": np.sin(ref_times.view(int))}, index=ref_times
{"reference": np.sin(ref_times.values.astype("float32"))}, index=ref_times
)

sample_times = pd.date_range(start="2000-12-28", end="2001-01-04", freq="D")
sample = pd.DataFrame(
{"FAKE_SAMPLES": np.sin(sample_times.view(int))}, index=sample_times
{"FAKE_SAMPLES": np.sin(sample_times.values.astype("float32"))},
index=sample_times,
)

time_series.plot(reference, sample, "test")
34 changes: 18 additions & 16 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

class TestStats:
ref_times = pd.date_range(start="2000-12-30", end="2001-01-03", freq="6H")
obs = pd.DataFrame({"obs": np.sin(ref_times.view(int))}, index=ref_times)
obs = pd.DataFrame(
{"obs": np.sin(ref_times.values.astype("float32"))}, index=ref_times
)

model_times = pd.date_range(start="2000-12-28", end="2001-01-04", freq="D")
data = 1.25 * np.sin(model_times.view(int) + 2)
data = 1.25 * np.sin(model_times.values.astype("float32") + 2)
model = pd.DataFrame({"model": data}, index=model_times)

aligned_signals = stats._align(obs, model)
Expand All @@ -23,20 +25,20 @@ def test_align(self):

assert isinstance(self.aligned_signals, pd.DataFrame)
assert self.aligned_signals.shape == (5, 2)
assert np.isclose(self.aligned_signals["model"].mean(), 0.16391766802943322)
assert np.isclose(self.aligned_signals["obs"].mean(), 0.13113413442354657)
assert np.isclose(self.aligned_signals["model"].mean(), -0.35140932)
assert np.isclose(self.aligned_signals["obs"].mean(), -0.28112745)

def test_align_xr(self):

assert isinstance(self.aligned_signals_xr, pd.DataFrame)
assert self.aligned_signals_xr.shape == (5, 2)
assert np.isclose(self.aligned_signals_xr["model"].mean(), 0.16391766802943322)
assert np.isclose(self.aligned_signals_xr["obs"].mean(), 0.13113413442354657)
assert np.isclose(self.aligned_signals_xr["model"].mean(), -0.35140932)
assert np.isclose(self.aligned_signals_xr["obs"].mean(), -0.28112745)

def test_bias(self):
bias = stats.compute_bias(self.obs, self.model)

assert np.isclose(bias, 0.032783533605886636)
assert np.isclose(bias, -0.07028185)

def test_correlation_coefficient(self):
corr_coef = stats.compute_correlation_coefficient(self.obs, self.model)
Expand All @@ -46,35 +48,35 @@ def test_correlation_coefficient(self):
def test_index_of_agreement(self):
ioa = stats.compute_index_of_agreement(self.obs, self.model)

assert np.isclose(ioa, 0.9872247131609245)
assert np.isclose(ioa, 0.9845252502709627)

def test_mean_square_error(self):
mse = stats.compute_mean_square_error(self.obs, self.model, centered=False)

assert np.isclose(mse, 0.03156566684130121)
assert np.isclose(mse, 0.024080737)

def test_mean_square_error_centered(self):
mse = stats.compute_mean_square_error(self.obs, self.model, centered=True)

assert np.isclose(mse, 0.03049090676561291)
assert np.isclose(mse, 0.019141199)

def test_murphy_skill_score(self):
mss = stats.compute_murphy_skill_score(self.obs, self.model)

assert np.isclose(mss, 0.935296965985732)
assert np.isclose(mss, 0.9213713780045509)

def test_root_mean_square_error(self):
rmse = stats.compute_root_mean_square_error(self.obs, self.model)

assert np.isclose(rmse, 0.17766729254790034)
assert np.isclose(rmse, 0.1551797)

def test_descriptive_statistics(self):
max, min, mean, std = stats.compute_descriptive_statistics(self.model, ddof=0)

assert np.isclose(max, 0.97866)
assert np.isclose(min, -0.906447)
assert np.isclose(mean, 0.039614)
assert np.isclose(std, 0.863681)
assert np.isclose(max, 0.882148)
assert np.isclose(min, -1.247736)
assert np.isclose(mean, -0.301843)
assert np.isclose(std, 0.757591)

def test_stats(self):
stats_output = stats.compute_stats(self.obs, self.model)
Expand Down

0 comments on commit 0103d45

Please sign in to comment.