Skip to content

Commit

Permalink
Remove plot test and glacier test (#135)
Browse files Browse the repository at this point in the history
* Removal of image comparison tests.

* Test glacier: add_temperature_bias

Adds some checks to make sure that glacier is progressing as intended
when there is a temperature bias scenario.
  • Loading branch information
Holmgren825 committed Feb 23, 2022
1 parent 5f6d52f commit d1239aa
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 125 deletions.
Binary file removed oggm_edu/tests/baseline_images/bed_base_plot.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed oggm_edu/tests/baseline_images/glacier_plot.png
Binary file not shown.
Binary file removed oggm_edu/tests/baseline_images/history_plot.png
Binary file not shown.
Binary file removed oggm_edu/tests/baseline_images/mass_balance_plot.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
109 changes: 41 additions & 68 deletions oggm_edu/tests/test_glacier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from oggm_edu import Glacier, SurgingGlacier, GlacierBed, MassBalance
import numpy as np
from numpy.testing import assert_equal
from matplotlib import pyplot as plt
import pytest

real_mb = MassBalance(ela=3000, gradient=5)
Expand Down Expand Up @@ -67,59 +65,49 @@ def test_progress_to_year():
assert len(glacier.state_history.time) == year + 1


# Some fancy plot testing.
@pytest.mark.mpl_image_compare(
baseline_dir="baseline_images", filename="glacier_plot.png"
)
def test_plot():
"""Make sure the base plot look as intended."""
def test_add_temperature_bias():
"""Test to make sure that the entire temperature bias mechanism works as intended."""
glacier = Glacier(bed=real_bed, mass_balance=real_mb)
# Plot the glacier.
glacier.plot()
# Retreive the figure.
fig = plt.gcf()
return fig


@pytest.mark.mpl_image_compare(
baseline_dir="baseline_images", filename="mass_balance_plot.png"
)
def test_plot_mass_balance():
"""Make sure the mass_balance plot look as intended."""
glacier = Glacier(bed=real_bed, mass_balance=real_mb)
# Plot the glacier.
glacier.plot_mass_balance()
# Retreive the figure.
fig = plt.gcf()
return fig


@pytest.mark.mpl_image_compare(
baseline_dir="baseline_images", filename="history_plot.png"
)
def test_plot_history():
"""Make sure the mass_balance plot look as intended."""
glacier = Glacier(bed=real_bed, mass_balance=real_mb)
glacier.progress_to_year(100)
# Plot the glacier.
fig, _, _, _ = glacier._create_history_plot_components()

return fig


@pytest.mark.mpl_image_compare(
baseline_dir="baseline_images", filename="state_history_plot.png"
)
def test_plot_state_history():
"""Make sure the mass_balance plot look as intended."""
glacier = Glacier(bed=real_bed, mass_balance=real_mb)
glacier.progress_to_year(100)
# Plot the glacier.
glacier.plot_state_history()

fig = plt.gcf()
# Add a temperature bias
duration = 10
bias = 2.0
glacier.add_temperature_bias(bias=2.0, duration=duration)
# Progress the glacier a few years.
year = 2
glacier.progress_to_year(year)
# Check the current temperature bias.
assert glacier.mass_balance.temp_bias == bias / duration * year

return fig
# Progress a bit further
year = 10
glacier.progress_to_year(year)
# We should have reached the final temp bias now.
assert glacier.mass_balance.temp_bias == bias
# This should've also have update the ELA, check against the original ELA.
assert glacier.ela == real_mb.ela + 150 * 2
assert glacier.age == 10
# Progress even further, bias should not continue to evolve
glacier.progress_to_year(20)
assert glacier.mass_balance.temp_bias == bias
assert glacier.ela == real_mb.ela + 150 * 2
# If we then set a new bias this should start to evolve again.
new_bias = -1.0
new_duration = 10
glacier.add_temperature_bias(bias=new_bias, duration=new_duration)
# Progress
year = 25
glacier.progress_to_year(year)
# What should the bias be now?
# We should be going from the previous bias towards the new bias.
assert glacier.mass_balance.temp_bias == bias + (
(new_bias - bias) / new_duration * 5
)

# Progress further
year = 35
glacier.progress_to_year(year)
assert glacier.mass_balance.temp_bias == new_bias
assert glacier.ela == real_mb.ela + 150 * new_bias


def test_surging_glacier():
Expand Down Expand Up @@ -148,18 +136,3 @@ def test_surging_glacier():
# No eq. state method.
with pytest.raises(Exception) as e_info:
surging_glacier.progress_to_equilibrium()


# Test the plotting.
@pytest.mark.mpl_image_compare(
baseline_dir="baseline_images", filename="surging_history_plot.png"
)
def test_surging_history_plot():
surging_glacier = SurgingGlacier(bed=real_bed, mass_balance=real_mb)

# Progress the surging glacier
year = 200
surging_glacier.progress_to_year(year)
surging_glacier.plot_history()
fig = plt.gcf()
return fig
11 changes: 0 additions & 11 deletions oggm_edu/tests/test_glacier_bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,3 @@ def test_constructor_attribute_assignment():
assert_equal(bed.bed_h, [2500, 2250, 2000, 1750, 1500])
# What should the widhts be? Linearly interpolated.
assert_equal(bed.widths * bed.map_dx, [500, 500, 500, 375, 250])


# Some fancy plot testing.
@pytest.mark.mpl_image_compare(
baseline_dir="baseline_images", filename="bed_base_plot.png"
)
def test_plot():
"""Make sure the base plot look as intended."""
bed = GlacierBed(top=3400, bottom=1000, width=500)
fig, _, _ = bed._create_base_plot()
return fig
46 changes: 0 additions & 46 deletions oggm_edu/tests/test_glacier_collection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from oggm_edu import GlacierBed, Glacier, MassBalance, GlacierCollection
import pytest
from matplotlib import pyplot as plt

mb = MassBalance(ela=3000, gradient=8)
bed = GlacierBed(top=3700, bottom=1500, width=600)
Expand Down Expand Up @@ -119,48 +118,3 @@ def test_progress_to_equilibrium():

collection.progress_to_equilibrium()
assert collection.glaciers[0].age == collection.glaciers[1].age


# Plot testing
@pytest.mark.mpl_image_compare(
baseline_dir="baseline_images", filename="collection_base_plot.png"
)
def test_plot():
"""Check the base collection plot"""
collection.plot()
fig = plt.gcf()
return fig


@pytest.mark.mpl_image_compare(
baseline_dir="baseline_images", filename="collection_base_plot_ela.png"
)
def test_plot_ela():
"""Check the base collection plot, where glaciers have different elas"""
# CHange the ela. But don't change the glacier
collection.change_attributes({"ela": [2500, 3000]})
collection.plot()
fig = plt.gcf()
# Change back the ela so other tests are preserved.
collection.change_attributes({"ela": [3000, 3000]})
return fig


@pytest.mark.mpl_image_compare(
baseline_dir="baseline_images", filename="collection_history_plot.png"
)
def test_plot_history():
"""Check the base collection plot"""
collection.plot_history()
fig = plt.gcf()
return fig


@pytest.mark.mpl_image_compare(
baseline_dir="baseline_images", filename="collection_mass_balance_plot.png"
)
def test_plot_mass_balance():
"""Check the base collection plot"""
collection.plot_mass_balance()
fig = plt.gcf()
return fig

0 comments on commit d1239aa

Please sign in to comment.