Skip to content

Commit

Permalink
Adding tests for Histogram and Windrose
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Leukauf authored and Daniel Leukauf committed May 23, 2022
1 parent 4f0aac6 commit 6d34e9a
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 84 deletions.
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ parentdir_prefix =
minversion=6.0
addopts =
-m "not long and not postproc and not config_req"
--cov-report term-missing --cov-branch --cov-report xml --cov-report term
--cov-report term-missing --cov-branch
--cov-report xml --cov-report html --cov-report term
--cov=wrftamer -vv --strict-markers -rfE
markers =
long: marks all tests that take a long time
Expand Down
96 changes: 83 additions & 13 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
prep_zt_data,
prep_profile_data,
get_limits_and_labels,
prep_windrose_data
)
from wrftamer.plotting.mpl_plots import create_mpl_plot, Profile, TimeSeries, Obs_vs_Mod
from wrftamer.plotting.mpl_plots import create_mpl_plot, Profile, TimeSeries, Obs_vs_Mod, Histogram
from wrftamer.plotting.hv_plots import create_hv_plot


Expand Down Expand Up @@ -57,9 +58,7 @@ def test_mpl_timeseries(infos, obs_data, mod_data):
infos[ii]["plottype"] = plottype

data, units, description = prep_ts_data(obs_data[ii], mod_data[ii], infos[ii])
plot_infos = get_limits_and_labels(
plottype, var, data, units=units, description=description
)
plot_infos = get_limits_and_labels(plottype, var, data, units=units, description=description)

create_mpl_plot(data, plot_infos)

Expand All @@ -75,9 +74,58 @@ def test_hv_timeseries(infos, obs_data, mod_data):
infos[ii]["plottype"] = plottype

data, units, description = prep_ts_data(obs_data[ii], mod_data[ii], infos[ii])
plot_infos = get_limits_and_labels(
plottype, var, data, units=units, description=description
)
plot_infos = get_limits_and_labels(plottype, var, data, units=units, description=description)

plot_infos["Expvec"] = infos[ii]["Expvec"]
plot_infos["Obsvec"] = infos[ii]["Obsvec"]
plot_infos['proj_name'] = infos[ii]['proj_name']
plot_infos['loc'] = infos[ii]['loc']
plot_infos['lev'] = infos[ii]['lev']
plot_infos['anemometer'] = infos[ii]['anemometer']

create_hv_plot(plot_infos, data=data)


def test_mpl_histogram(infos, obs_data, mod_data):
maxnum = len(infos)

for ii in range(0, maxnum):
var = infos[ii]["var"]
plottype = "Histogram"
infos[ii]["plottype"] = plottype

data, units, description = prep_ts_data(obs_data[ii], mod_data[ii], infos[ii])
plot_infos = get_limits_and_labels(plottype, var, data, units=units, description=description)

plot_infos["Expvec"] = infos[ii]["Expvec"]
plot_infos["Obsvec"] = infos[ii]["Obsvec"]
plot_infos['proj_name'] = infos[ii]['proj_name']
plot_infos['loc'] = infos[ii]['loc']
plot_infos['lev'] = infos[ii]['lev']
plot_infos['anemometer'] = infos[ii]['anemometer']

create_mpl_plot(data, plot_infos)

Histogram(data=data, **plot_infos)


def test_hv_histogram(infos, obs_data, mod_data):
maxnum = len(infos)

for ii in range(0, maxnum):
var = infos[ii]["var"]
plottype = "Histogram"
infos[ii]["plottype"] = plottype

data, units, description = prep_ts_data(obs_data[ii], mod_data[ii], infos[ii])
plot_infos = get_limits_and_labels(plottype, var, data, units=units, description=description)

plot_infos["Expvec"] = infos[ii]["Expvec"]
plot_infos["Obsvec"] = infos[ii]["Obsvec"]
plot_infos['proj_name'] = infos[ii]['proj_name']
plot_infos['loc'] = infos[ii]['loc']
plot_infos['lev'] = infos[ii]['lev']
plot_infos['anemometer'] = infos[ii]['anemometer']

create_hv_plot(plot_infos, data=data)

Expand All @@ -90,12 +138,8 @@ def test_mpl_profiles(infos, obs_data, mod_data):
plottype = "Profiles"
infos[ii]["plottype"] = plottype

data, units, description = prep_profile_data(
obs_data[ii], mod_data[ii], infos[ii]
)
plot_infos = get_limits_and_labels(
plottype, var, data, units=units, description=description
)
data, units, description = prep_profile_data(obs_data[ii], mod_data[ii], infos[ii])
plot_infos = get_limits_and_labels(plottype, var, data, units=units, description=description)

create_mpl_plot(data, plot_infos)

Expand Down Expand Up @@ -179,6 +223,10 @@ def test_hv_obs_vs_mod(infos, obs_data, mod_data):

plot_infos["Expvec"] = infos[ii]["Expvec"]
plot_infos["Obsvec"] = infos[ii]["Obsvec"]
plot_infos['proj_name'] = infos[ii]['proj_name']
plot_infos['loc'] = infos[ii]['loc']
plot_infos['lev'] = infos[ii]['lev']
plot_infos['anemometer'] = infos[ii]['anemometer']

create_hv_plot(data=data, infos=plot_infos)

Expand All @@ -203,3 +251,25 @@ def test_hv_not_implemented():

plot_infos = {"plottype": "Map", "var": "WSP"} # will just inform user
create_hv_plot(plot_infos, map_data=None)

def test_mpl_windrose(infos, obs_data, mod_data):

maxnum = len(infos)

for ii in range(0, maxnum):

var = 'WSP'
plottype = "Windrose"
infos[ii]["plottype"] = plottype

data, units, description = prep_windrose_data(obs_data[ii], mod_data[ii], infos[ii])
plot_infos = get_limits_and_labels(plottype, var, data, units=units, description=description)

plot_infos["Expvec"] = infos[ii]["Expvec"]
plot_infos["Obsvec"] = infos[ii]["Obsvec"]
plot_infos['proj_name'] = infos[ii]['proj_name']
plot_infos['loc'] = infos[ii]['loc']
plot_infos['lev'] = infos[ii]['lev']
plot_infos['anemometer'] = infos[ii]['anemometer']

create_mpl_plot(data=data, infos=plot_infos)
12 changes: 5 additions & 7 deletions tests/test_project_and_experiment_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ def test_nonexisting_project(base_test_env):
test.list_exp()

# adding an experiment to a project that does not exist should work (since the project is created on the fly!)
configfile = (
os.path.split(os.path.realpath(__file__))[0] + "/resources/configure_test.yaml"
)
configfile = os.path.split(os.path.realpath(__file__))[0] + "/resources/configure_test.yaml"
test.exp_create("Some_Random_Exp_Name", "Some_Random_Comment", configfile)
test.remove(force=True, verbose=False)

Expand Down Expand Up @@ -83,15 +81,15 @@ def test_project(testprojects):
# check that testproject variables have been renamed (tests further down depend on this)
if (
testproject1.name == proj_name2
and Path(testproject1.proj_tab).stem == proj_name2
and Path(testproject1.proj_path).stem == proj_name2
and Path(testproject1.tamer_path).stem == proj_name2
):
pass
else:
raise ValueError

# check if the files with the expected new names really exist
if os.path.isdir(testproject1.proj_tab):
if os.path.isdir(testproject1.proj_path):
pass
else:
raise FileNotFoundError
Expand All @@ -117,8 +115,8 @@ def test_project(testprojects):
# Rewriting xls sheet should work (but won't do anything, since no experiments have been created)
testproject1.rewrite_xls()

# Case damaged project. proj_tab missing, but tamer_path intact
shutil.rmtree(testproject1.proj_tab)
# Case damaged project. proj_path missing, but tamer_path intact
shutil.rmtree(testproject1.proj_path)
with pytest.raises(FileNotFoundError):
testproject1.rename(proj_name2)

Expand Down
49 changes: 25 additions & 24 deletions tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,37 @@
expect_xa4 = xr.load_dataset(data_path + "testres4.nc")


def test_stats1(): # dataframe without attrs
with pytest.raises(AttributeError):
res = Statistics(test_pd)
def test_stats1():

infos = dict()
infos['proj_name'] = 'test'
infos['loc'] = 'loc'
infos['var'] = 'WSP'
infos['lev'] = '5'
infos['anemometer'] = 'USA'
infos['Expvec'] = ['model']
infos['Obsvec'] = ['obs']

def test_stats2():
test_pd.proj_name = "test"
test_pd.location = "loc"
test_pd.variable = "WSP"
test_pd.level = 5
test_pd.anemometer = "USA"
test_pd.Expvec = ["model"]
test_pd.Obsvec = ["obs"]
res = Statistics(test_pd)

if max(abs(res.values[0, 4::] - expect_pd.values[0, 4::])) > 1e-10:
res = Statistics(test_pd, **infos)

if max(abs(res.values[0, 5::] - expect_pd.values[0, 5::])) > 1e-10:
raise ValueError


def test_stats3():
test_pd.proj_name = "test"
test_pd.location = "loc"
test_pd.variable = "DIR"
test_pd.level = 5
test_pd.anemometer = "USA"
test_pd.Expvec = ["model"]
test_pd.Obsvec = ["obs"]
res = Statistics(test_pd)
def test_stats2():

infos = dict()
infos['proj_name'] = 'test'
infos['loc'] = 'loc'
infos['var'] = 'DIR'
infos['lev'] = '5'
infos['anemometer'] = 'USA'
infos['Expvec'] = ['model']
infos['Obsvec'] = ['obs']

res = Statistics(test_pd, **infos)

if max(abs(res.values[0, 4:8] - expect_pd.values[0, 4:8])) > 1e-10:
if max(abs(res.values[0, 5:8] - expect_pd.values[0, 5:8])) > 1e-10:
raise ValueError


Expand Down
11 changes: 2 additions & 9 deletions wrftamer/plotting/hv_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def create_hv_plot(infos: dict, data=None, map_data=None):
title = infos.get("title", "")

if plottype == "Timeseries":

stats = Statistics(data, **infos)

size = 5
Expand Down Expand Up @@ -73,6 +74,7 @@ def create_hv_plot(infos: dict, data=None, map_data=None):
figure = pn.Column(figure, stats)

elif plottype == 'Histogram':

stats = Statistics(data, **infos)

size = 5
Expand Down Expand Up @@ -241,15 +243,6 @@ def Map_hvplots(map_data, **infos):
coastline = infos.get("coastline", "10m")
levels = infos.get("levels", 25)

# TODO: fix hvplot for maps. Make projections work.
# -> Projekt for Moritz. I am almost there.

"""
# This command no longer works with the current version of the packages.
# I need a major cleanup, find a workaround and/or change the packages.
# Affected packages are most likely shapely, cartopy and maybe others.
"""

stand_lon = map_data.projection.stand_lon
moad_cen_lat = map_data.projection.moad_cen_lat

Expand Down
28 changes: 7 additions & 21 deletions wrftamer/wrfplotter_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ def extract_data_from_wrfout(self, filename: PosixPath, dom: str, var: str, ml:
data = wrf.getvar(fid, "tk", timeidx=idx, squeeze=False)[:, ml, :, :]
data.attrs["model_level"] = ml
elif var == "PT":
data = wrf.getvar(fid, "theta", units="K", timeidx=idx, squeeze=False)[
:, ml, :, :
]
data = wrf.getvar(fid, "theta", units="K", timeidx=idx, squeeze=False)[:, ml, :, :]
data.attrs["model_level"] = ml
elif var == "WSP":
data = wrf.getvar(fid, "uvmet_wspd_wdir", units="m s-1", timeidx=idx, squeeze=False)[0][:, ml, :, :]
Expand All @@ -88,23 +86,17 @@ def extract_data_from_wrfout(self, filename: PosixPath, dom: str, var: str, ml:
data.attrs["units"] = "m s-1"
data.attrs["model_level"] = ml
elif var == "DIR":
data = wrf.getvar(
fid, "uvmet_wspd_wdir", units="m s-1", timeidx=idx, squeeze=False
)[1][:, ml, :, :]
data = wrf.getvar(fid, "uvmet_wspd_wdir", units="m s-1", timeidx=idx, squeeze=False)[1][:, ml, :, :]
data.name = "DIR"
data = data.drop_vars("wspd_wdir")
data.attrs["description"] = "earth rotated wdir"
data.attrs["units"] = "degrees"
data.attrs["model_level"] = ml
elif var in ["U", "V", "W"]:
data = wrf.getvar(
fid, var.lower() + "a", units="m s-1", timeidx=idx, squeeze=False
)[:, ml, :, :]
data = wrf.getvar(fid, var.lower() + "a", units="m s-1", timeidx=idx, squeeze=False)[:, ml, :, :]
data.attrs["model_level"] = ml
elif var == "WSP10":
data = wrf.getvar(
fid, "wspd_wdir10", units="m s-1", timeidx=idx, squeeze=False
)[0][:, :, :]
data = wrf.getvar(fid, "wspd_wdir10", units="m s-1", timeidx=idx, squeeze=False)[0][:, :, :]
data.name = "WSP10"
data = data.drop_vars("wspd_wdir")
data.attrs["units"] = "m s-1"
Expand All @@ -117,9 +109,7 @@ def extract_data_from_wrfout(self, filename: PosixPath, dom: str, var: str, ml:
data.attrs["units"] = "degrees"
data.attrs["model_level"] = "sfc"
elif var in ["PRES", "P"]:
data = wrf.getvar(fid, "p", units="Pa", timeidx=idx, squeeze=False)[
:, ml, :, :
]
data = wrf.getvar(fid, "p", units="Pa", timeidx=idx, squeeze=False)[:, ml, :, :]
data.attrs["model_level"] = ml
# These lines should work in principle. However, I do not have testdata at the ready, so remove these lines.
# elif var in ["QV", "QVAPOR"]:
Expand Down Expand Up @@ -325,9 +315,7 @@ def plot(self, map_t="Cartopy", store=False, **kwargs) -> None:
# ----------------------------------------------------------------------------------------------------------------------

# some helperfunctions
def get_list_of_filenames(
name_of_dataset: str, dtstart: dt.datetime, dtend: dt.datetime
):
def get_list_of_filenames(name_of_dataset: str, dtstart: dt.datetime, dtend: dt.datetime):
# this is for reading...

# The user provides an observation path and a stationname to be read in.
Expand Down Expand Up @@ -378,9 +366,7 @@ def get_list_of_filenames(
return filenames


def get_list_of_filenames2(
name_of_dataset: str, dtstart: dt.datetime, dtend: dt.datetime
):
def get_list_of_filenames2(name_of_dataset: str, dtstart: dt.datetime, dtend: dt.datetime):
# this is for writing...
# TODO: combine the two routines.
# TODO: create a rule when to split by time and create multiple files...
Expand Down
Loading

0 comments on commit 6d34e9a

Please sign in to comment.