Skip to content

Commit

Permalink
Small refactoring of tests (#279)
Browse files Browse the repository at this point in the history
* Small refactoring of tests
* Try ommit tests
* Better syntax
* Skip tests, __init__.py and mpi.py
  • Loading branch information
fmaussion committed Sep 8, 2017
1 parent d7e8a60 commit a54842d
Show file tree
Hide file tree
Showing 9 changed files with 1,079 additions and 1,014 deletions.
13 changes: 11 additions & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
[run]
source = oggm
omit = oggm/tests/*
omit =
*/tests/*
*/sandbox/*
*/mpi.py
*/__init__.py

[report]
omit = */sandbox*/*
omit =
*/sandbox/*
*/tests/*
*/mpi.py
*/__init__.py

# Regexes for lines to exclude from consideration
exclude_lines =
pragma: no cover
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ matrix:
fast_finish: true
include:
- env: OGGM_TEST_ENV=prepro MPL=
- env: OGGM_TEST_ENV=numerics MPL=
- env: OGGM_TEST_ENV=models MPL=
- env: OGGM_TEST_ENV=workflow MPL=--mpl
- env: OGGM_TEST_ENV=graphics MPL=--mpl
Expand Down
2 changes: 1 addition & 1 deletion oggm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

try:
from .version import version as __version__
except ImportError: # pragma: no cover
except ImportError:
raise ImportError('oggm is not properly installed. If you are running '
'from the source directory, please instead create a '
'new virtual environment (using conda or virtualenv) '
Expand Down
4 changes: 2 additions & 2 deletions oggm/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def plot_googlemap(gdir, ax=None):

@entity_task(log)
@_plot_map
def plot_domain(gdir, ax=None, salemmap=None): # pragma: no cover
def plot_domain(gdir, ax=None, salemmap=None):
"""Plot the glacier directory.
"""
Expand Down Expand Up @@ -631,7 +631,7 @@ def plot_modeloutput_section(gdir, model=None, ax=None, title=''):


@entity_task(log)
def plot_modeloutput_section_withtrib(gdir, model=None, title=''): # pragma: no cover
def plot_modeloutput_section_withtrib(gdir, model=None, title=''):
"""Plots the result of the model output along the flowline."""

n_tribs = len(model.fls) - 1
Expand Down
12 changes: 12 additions & 0 deletions oggm/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
RUN_SLOW_TESTS = False
RUN_DOWNLOAD_TESTS = False
RUN_PREPRO_TESTS = True
RUN_NUMERIC_TESTS = True
RUN_MODEL_TESTS = True
RUN_WORKFLOW_TESTS = True
RUN_GRAPHIC_TESTS = True
Expand All @@ -61,6 +62,7 @@
# Minimal tests
RUN_SLOW_TESTS = False
RUN_PREPRO_TESTS = True
RUN_NUMERIC_TESTS = True
RUN_MODEL_TESTS = True
RUN_WORKFLOW_TESTS = True
RUN_GRAPHIC_TESTS = True
Expand All @@ -70,21 +72,31 @@
env = os.environ.get('OGGM_TEST_ENV')
if env == 'prepro':
RUN_PREPRO_TESTS = True
RUN_NUMERIC_TESTS = False
RUN_MODEL_TESTS = False
RUN_WORKFLOW_TESTS = False
RUN_GRAPHIC_TESTS = False
if env == 'numerics':
RUN_PREPRO_TESTS = False
RUN_NUMERIC_TESTS = True
RUN_MODEL_TESTS = False
RUN_WORKFLOW_TESTS = False
RUN_GRAPHIC_TESTS = False
if env == 'models':
RUN_PREPRO_TESTS = False
RUN_NUMERIC_TESTS = False
RUN_MODEL_TESTS = True
RUN_WORKFLOW_TESTS = False
RUN_GRAPHIC_TESTS = False
if env == 'workflow':
RUN_PREPRO_TESTS = False
RUN_NUMERIC_TESTS = False
RUN_MODEL_TESTS = False
RUN_WORKFLOW_TESTS = True
RUN_GRAPHIC_TESTS = False
if env == 'graphics':
RUN_PREPRO_TESTS = False
RUN_NUMERIC_TESTS = False
RUN_MODEL_TESTS = False
RUN_WORKFLOW_TESTS = False
RUN_GRAPHIC_TESTS = True
Expand Down
199 changes: 199 additions & 0 deletions oggm/tests/funcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import shapely.geometry as shpg
import numpy as np

# Local imports
from oggm.core.models import flowline


def dummy_constant_bed(hmax=3000., hmin=1000., nx=200, map_dx=100.,
widths=3.):

dx = 1.

surface_h = np.linspace(hmax, hmin, nx)
bed_h = surface_h
widths = surface_h * 0. + widths
coords = np.arange(0, nx- 0.5, 1)
line = shpg.LineString(np.vstack([coords, coords * 0.]).T)
return [flowline.VerticalWallFlowline(line, dx, map_dx, surface_h,
bed_h, widths)]


def dummy_constant_bed_cliff(hmax=3000., hmin=1000., nx=200, map_dx=100.,
cliff_height=250.):
"""
I introduce a cliff in the bed to test the mass conservation of the models
Such a cliff could be real or a DEM error/artifact
"""
dx = 1.

surface_h = np.linspace(hmax, hmin, nx)

surface_h[50:] = surface_h[50:] - cliff_height

bed_h = surface_h
widths = surface_h * 0. + 1.

coords = np.arange(0, nx - 0.5, 1)
line = shpg.LineString(np.vstack([coords, coords * 0.]).T)
return [flowline.VerticalWallFlowline(line, dx, map_dx, surface_h,
bed_h, widths)]


def dummy_constant_bed_obstacle(hmax=3000., hmin=1000., nx=200):
"""
I introduce an obstacle in the bed
"""

map_dx = 100.
dx = 1.

surface_h = np.linspace(hmax, hmin, nx)

cliff_height = 200.0
surface_h[60:] = surface_h[60:] + cliff_height

bed_h = surface_h
widths = surface_h * 0. + 1.

coords = np.arange(0, nx - 0.5, 1)
line = shpg.LineString(np.vstack([coords, coords * 0.]).T)
return [flowline.VerticalWallFlowline(line, dx, map_dx, surface_h,
bed_h, widths)]


def dummy_bumpy_bed():
map_dx = 100.
dx = 1.
nx = 200

coords = np.arange(0, nx - 0.5, 1)
surface_h = np.linspace(3000, 1000, nx)
surface_h += 170. * np.exp(-((coords - 30) / 5) ** 2)

bed_h = surface_h
widths = surface_h * 0. + 3.
line = shpg.LineString(np.vstack([coords, coords * 0.]).T)
return [flowline.VerticalWallFlowline(line, dx, map_dx, surface_h,
bed_h, widths)]


def dummy_noisy_bed(map_dx=100.):
dx = 1.
nx = 200
np.random.seed(42)
coords = np.arange(0, nx - 0.5, 1)
surface_h = np.linspace(3000, 1000, nx)
surface_h += 100 * np.random.rand(nx) - 50.

bed_h = surface_h
widths = surface_h * 0. + 3.
line = shpg.LineString(np.vstack([coords, coords * 0.]).T)
return [flowline.VerticalWallFlowline(line, dx, map_dx, surface_h,
bed_h, widths)]


def dummy_parabolic_bed(hmax=3000., hmin=1000., nx=200, map_dx=100.,
default_shape=5.e-03,
from_other_shape=None, from_other_bed=None):
dx = 1.

surface_h = np.linspace(hmax, hmin, nx)
bed_h = surface_h * 1
shape = surface_h * 0. + default_shape
if from_other_shape is not None:
shape[0:len(from_other_shape)] = from_other_shape

if from_other_bed is not None:
bed_h[0:len(from_other_bed)] = from_other_bed

coords = np.arange(0, nx - 0.5, 1)
line = shpg.LineString(np.vstack([coords, coords * 0.]).T)
return [flowline.ParabolicFlowline(line, dx, map_dx, surface_h,
bed_h, shape)]


def dummy_mixed_bed(deflambdas=3.5, map_dx=100., mixslice=None):
dx = 1.
nx = 200

surface_h = np.linspace(3000, 1000, nx)
bed_h = surface_h
shape = surface_h * 0. + 3.e-03
if mixslice:
shape[mixslice] = np.NaN
else:
shape[10:20] = np.NaN
is_trapezoid = ~np.isfinite(shape)
lambdas = shape * 0.
lambdas[is_trapezoid] = deflambdas

widths_m = bed_h * 0. + 10
section = bed_h * 0.

coords = np.arange(0, nx - 0.5, 1)
line = shpg.LineString(np.vstack([coords, coords * 0.]).T)

fls = flowline.MixedFlowline(line=line, dx=dx, map_dx=map_dx,
surface_h=surface_h, bed_h=bed_h,
section=section, bed_shape=shape,
is_trapezoid=is_trapezoid,
lambdas=lambdas, widths_m=widths_m)

return [fls]


def dummy_trapezoidal_bed(hmax=3000., hmin=1000., nx=200):
map_dx = 100.
dx = 1.

surface_h = np.linspace(hmax, hmin, nx)
bed_h = surface_h
widths = surface_h * 0. + 1.6

lambdas = surface_h * 0. + 2

coords = np.arange(0, nx - 0.5, 1)
line = shpg.LineString(np.vstack([coords, coords * 0.]).T)

return [flowline.TrapezoidalFlowline(line, dx, map_dx, surface_h,
bed_h, widths, lambdas)]


def dummy_width_bed():
"""This bed has a width of 6 during the first 20 points and then 3"""

map_dx = 100.
dx = 1.
nx = 200

surface_h = np.linspace(3000, 1000, nx)
bed_h = surface_h
widths = surface_h * 0. + 3.
widths[0:20] = 6.

coords = np.arange(0, nx - 0.5, 1)
line = shpg.LineString(np.vstack([coords, coords * 0.]).T)
return [flowline.VerticalWallFlowline(line, dx, map_dx, surface_h,
bed_h, widths)]


def dummy_width_bed_tributary(map_dx=100.):
# bed with tributary glacier
dx = 1.
nx = 200

surface_h = np.linspace(3000, 1000, nx)
bed_h = surface_h
widths = surface_h * 0. + 3.
coords = np.arange(0, nx - 0.5, 1)
line = shpg.LineString(np.vstack([coords, coords * 0.]).T)

fl_0 = flowline.VerticalWallFlowline(line, dx, map_dx, surface_h, bed_h,
widths)
coords = np.arange(0, 19.1, 1)
line = shpg.LineString(np.vstack([coords, coords * 0. + 1]).T)
fl_1 = flowline.VerticalWallFlowline(line, dx, map_dx, surface_h[0:20],
bed_h[0:20], widths[0:20])
fl_1.set_flows_to(fl_0)
return [fl_1, fl_0]
Loading

0 comments on commit a54842d

Please sign in to comment.