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 to Hector 3 #63

Merged
merged 55 commits into from
Mar 6, 2024
Merged

Update to Hector 3 #63

merged 55 commits into from
Mar 6, 2024

Conversation

rgieseke
Copy link
Collaborator

@rgieseke rgieseke commented Jun 7, 2023

@rgieseke
Copy link
Collaborator Author

rgieseke commented Jun 7, 2023

Test script

import pyhector
from copy import deepcopy
from pyhector.default_config import _default_config

print(pyhector.__hector_version__)

h = pyhector.Hector()

h.set_emissions(pyhector.ssp126)

parameters = deepcopy(_default_config)
h.config(parameters)

h.run(until=2100)

Prints the expected Hector version and runs.

However, adding an observable fails:

import pyhector
from copy import deepcopy
from pyhector.default_config import _default_config

print(pyhector.__hector_version__)

h = pyhector.Hector()

h.set_emissions(pyhector.ssp126)

parameters = deepcopy(_default_config)
h.config(parameters)

h.add_observable(
    "simpleNbox",
    "atmos_co2",
    False
)

h.run(until=2100)

Error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File ~/Work/pyhector/session.py:20
     12 h.config(parameters)
     14 h.add_observable(
     15     "simpleNbox",
     16     "atmos_co2",
     17     False
     18 )
---> 20 h.run(until=2100)

ValueError: vector::_M_fill_insert

@swillner
Copy link
Collaborator

(hint for enabling debug compilation: add to Pybind11Extension in setup.py: extra_compile_args=["-g3"])

@rgieseke
Copy link
Collaborator Author

With the now compiling and running version only a single value is returned in the array.

import pyhector
from copy import deepcopy
from pyhector.default_config import _default_config

print(pyhector.__hector_version__)

h = pyhector.Hector()

h.set_emissions(pyhector.ssp126)

parameters = deepcopy(_default_config)
h.config(parameters)

h.add_observable(
	"simpleNbox",
	"CO2_concentration",
	True
)

h.run()

out = h.get_observable("simpleNbox", "CO2_concentration")

print(out)

@rgieseke rgieseke marked this pull request as ready for review February 27, 2024 18:39
@rgieseke
Copy link
Collaborator Author

rgieseke commented Mar 1, 2024

Pyhector v2.x results and output streams from the C++ command line version are in good agreement, but the tests are not catching larger differences, like volcanic forcing changes, missing albedo forcing, or parameter changes introduced with the Hector v3 update.

With Pyhector 2.5.0.2

import pyhector

df = pyhector.run(pyhector.rcp26)

outstream = pyhector.read_hector_output(
    "https://github.com/openclimatedata/pyhector/raw/main/tests/data/outputstream_rcp26.csv"
)

df["temperature.Tgav"].plot(color="orange")
outstream["Tgav"].plot(linestyle=":", color="blue")

image

I think the Pandas testing function uses numpy's isclose which actually does

absolute(a - b) <= (atol + rtol * absolute(b))

The test only changes the rtol default.

assert_series_equal(
    output["temperature.global_tas"],
    original.global_tas,
    check_names=False,
    check_exact=False,
    rtol=3,
)

@rgieseke
Copy link
Collaborator Author

rgieseke commented Mar 1, 2024

I'm not entirely sure how command line Hector sets its output precision for the CSV file, but it appears to have four significant figures.
One way to make a comparison between Pyhector output and the CSV streams could be to compare the difference between these significant figures, ignoring the exponent and checking that there are only floating point rounding differences.

With 88dcebf

import os
import pyhector
from pyhector import (
    ssp119,
    ssp126,
    ssp245,
    ssp370,
    ssp434,
    ssp460,
    ssp534_over,
    ssp585,
    read_hector_output
)

import matplotlib.pyplot as plt


ssps = {
    "ssp119": ssp119,
    "ssp126": ssp126,
    "ssp245": ssp245,
    "ssp370": ssp370,
    "ssp434": ssp434,
    "ssp460": ssp460,
    "ssp534-over": ssp534_over,
    "ssp585": ssp585,
}


for name, scenario in list(ssps.items())[-2:-1]:
    df = pyhector.run(scenario)
    out = read_hector_output(os.path.join("tests/data/outputstream_{}.csv".format(name)))
    
    fig, ax = plt.subplots(1,3, figsize=(14,6))
    out["global_tas"].plot(label="cli outputstream", ax=ax[0])
    df["temperature.global_tas"].plot(label="pyhector", ax=ax[0])
    ax[0].legend()
    ax[0].set_title("Temperature")
    
    out["CO2_concentration"].plot(label="cli outputstream", ax=ax[1])
    df["simpleNbox.CO2_concentration"].plot(label="pyhector", ax=ax[1])
    ax[1].legend()
    ax[1].set_title("Concentration")

    (out["global_tas"].map(lambda x: float(f'{x:.5e}'[:7])) - 
     df["temperature.global_tas"].map(lambda x: float(f'{x:.5e}'[:7]))).plot(ax=ax[2])
    ax[2].set_title('Subtracting significant digits (Temp.)')
    
    fig.suptitle(name)

    assert (out["global_tas"].map(lambda x: float(f'{x:.5e}'[:7])) - 
        df["temperature.global_tas"].map(lambda x: float(f'{x:.5e}'[:7])).abs() < 6e-4).all()

image

This should catch small changes, e.g. like

df = pyhector.run(scenario, {"temperature": {"S": 3.01}})

@rgieseke rgieseke requested a review from swillner March 5, 2024 13:38
Copy link
Collaborator

@swillner swillner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome!

include/Hector.h Outdated Show resolved Hide resolved
include/Observable.h Outdated Show resolved Hide resolved
src/main.cpp Outdated Show resolved Hide resolved
rgieseke and others added 3 commits March 6, 2024 10:37
Co-authored-by: Sven Willner <sven.willner@gmail.com>
Co-authored-by: Sven Willner <sven.willner@gmail.com>
Co-authored-by: Sven Willner <sven.willner@gmail.com>
@rgieseke rgieseke merged commit 6fd7db5 into main Mar 6, 2024
2 checks passed
@rgieseke rgieseke deleted the hector-3 branch March 6, 2024 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants