Giter Club home page Giter Club logo

pyhector's Introduction

pyhector

PyPI Version PyPI Python Versions
Docs Launch Binder
JOSS Zenodo

pyhector is a Python interface for the simple global climate carbon-cycle model Hector.

pyhector makes the simple climate model Hector easily installable and usable from Python and can for example be used in the analysis of mitigation scenarios, in integrated assessment models, complex climate model emulation, and uncertainty analyses.

Hector is written in C++ and developed at the Pacific Northwest National Laboratory.

See the Hector repository and documentation website for further information.

The Python interface pyhector is developed by Sven Willner and Robert Gieseke.

Pyhector uses pybind11 to wrap Hector's API. The version of Hector used can be read using Pyhector's __hector_version__ field.

Installation

Prerequisites

Hector requires Boost, so to install and use pyhector you need to have the filesystem and system modules of Boost installed (see also the Hector build instructions).

On Ubuntu/Debian these can be installed by invoking

sudo apt-get install libboost-filesystem-dev libboost-system-dev

On macOS Boost is available through the Homebrew package manager, it might be advisable to use a Homebrew installed Python for installing pyhector:

brew install boost

Windows is (as Hector) in principle supported but not yet tested for pyhector. Pull request with installation notes for Windows are welcome.

Install using pip

You can install pyhector from PyPI by invoking

pip install pyhector

Usage

This repository also contains a Jupyter Notebook you can run live and experiment with, courtesy of the Binder project. The notebook can be viewed as a static version using nbviewer.

Basic example

import pyhector

output = pyhector.run(pyhector.ssp126)

Advanced example

import pyhector
import matplotlib.pyplot as plt
from pyhector import ssp119, ssp126, ssp245, ssp370, ssp434, ssp460, ssp534_over, ssp585

for ssp in [ssp119, ssp126, ssp245, ssp370, ssp434, ssp460, ssp534_over, ssp585]:
    output = pyhector.run(ssp, {"core": {"endDate": 2100}})
    temp = output["temperature.global_tas"]
    temp = temp.loc[1850:] - temp.loc[1850:1900].mean()
    temp.plot(label=ssp.name)
plt.title("Global mean temperature")
plt.ylabel("°C over pre-industrial (1850-1900 mean)")
plt.legend(loc="best")
plt.show()

Temperature Plot of RCP scenarios

Development

For local development you can clone the repository, update the dependencies and install in a virtual environment with pip.

git clone https://github.com/openclimatedata/pyhector.git --recursive
cd pyhector
python3 -m venv venv
./venv/bin/pip install --editable --verbose .

To update pyhector and all submodules you can run

git pull --recurse-submodules
git submodule update --init --recursive
./venv/bin/pip install --editable .

Tests can be run locally with

python setup.py test

pyhector's People

Contributors

rgieseke avatar swillner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyhector's Issues

Fix notebook

  • variables -> output
  • update binder badge to beta

Introducing new output variables sometimes errors

But not predictable ...

pytest tests/test_pyhector.py::test_output_variables

================ test session starts ================
platform linux -- Python 3.5.3, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /home/robert/openclimatedata/pyhector, inifile: setup.cfg
plugins: cov-2.5.1
collected 1 item

tests/test_pyhector.py F

===================== FAILURES
___________ test_output_variables

    def test_output_variables():
        results = pyhector.run(rcp26)
        assert len(results.columns) == 3
>       results = pyhector.run(rcp26, outputs="all")

tests/test_pyhector.py:74:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
pyhector/__init__.py:414: in run
    h.run()
pyhector/__init__.py:134: in run
    self._check(self.__run_size)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pyhector.Hector object at 0x7f3e03beb828>, v = -1

    def _check(self, v):
        if v < 0:
>           raise HectorException(_lib.hector_get_last_error())
E           pyhector.HectorException: b'Assertion failed: units mismatch'

pyhector/__init__.py:129: HectorException
============= 1 failed in 0.46 seconds ===============================

Output variables requiring date?

The following output variables fail with something like

PyHectorException: b'Assertion failed: Date required for atmospheric CH4'
  • OH.TAU_OH
  • ozone.O3
  • N2O.N2O
  • CH4.CH4

Add __version__ attribute

Options:

Python-Git-Version

https://github.com/aebrahim/python-git-version

Python-Versioneer

This looks good:

This is a tool for managing a recorded version number in distutils-based python projects. The goal is to remove the tedious and error-prone "update the embedded version string" step from your release process. Making a new release should be as easy as recording a new tag in your version-control system, and maybe making new tarballs.

https://github.com/warner/python-versioneer

Stackoverflow Discussion

http://stackoverflow.com/questions/17791481/creating-a-version-attribute-for-python-packages-without-getting-into-troubl

Problem with PyBind on ARM Mac OS 10.14

This line of code is failing to compile:

array.reset(new py::array_t<double>{{values.size()}, data, handle});

I don't know enough about PyBind to figure it out.

clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/Headers -arch arm64 -arch x86_64 -Werror=implicit-function-declaration -Iinclude -Ihector/inst/include -I/Users/james/pyhector/venv/lib/python3.8/site-packages/pybind11/include -I/Users/james/pyhector/venv/lib/python3.8/site-packages/pybind11/include -I/Users/james/pyhector/venv/include -I/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8 -c src/Observable.cpp -o build/temp.macosx-10.14-arm64-3.8/src/Observable.o -std=c++11 src/Observable.cpp:87:42: error: non-constant-expression cannot be narrowed from type 'std::vector<double>::size_type' (aka 'unsigned long') to 'pybind11::ssize_t' (aka 'long') in initializer list [-Wc++11-narrowing] array.reset(new py::array_t<double>{{values.size()}, data, handle}); ^~~~~~~~~~~~~ src/Observable.cpp:87:42: note: insert an explicit cast to silence this issue array.reset(new py::array_t<double>{{values.size()}, data, handle}); ^~~~~~~~~~~~~ static_cast<ssize_t>( ) src/Observable.cpp:87:41: warning: braces around scalar initializer [-Wbraced-scalar-init] array.reset(new py::array_t<double>{{values.size()}, data, handle}); ^~~~~~~~~~~~~~~ 1 warning and 1 error generated. error: command 'clang' failed with exit status 1

Logging re-enabled

On a clean checkout I get logging output from Hector, not sure whether it's a change here, upstream, or in hector-wrapper.

Parse units in ini files

H0=35.0,pptv ; preindustrial concentration, pptv

See also JGCRI/hector#79

  • requires adding a field 'unit' to config object

Do we need changes on the C++ side, @swillner? Or is this ignored anyway? We could also compare with an expected unit on the Python side if we wanted ...

Rename output variables

Shorter, consistent names, e.g.

Maybe:
"temperature.Tgav" => "Surface_Temp_Global"

"simpleNbox.Ca" => "Conc_CO2"
"HFC227ea_halocarbon.hc_concentration" => "Conc_HFC227ea"

"forcing.Ftot" => "Forcing_Total"
"forcing.FCO2" => "Forcing_CO2"

Upper case desirable for standing out in Pandas DataFrames.
Camelcase oder under scores?

Update tests

Hector added a number of tests using its R wrapper.

We should add these too (or some of them).

Trouble installing

Hi,

I am trying to install pyhector on my mac OS X 10.11. I'm having some issues, see below. It's not able to find emplace_back and std::stop, plus the "error: command 'usr/bin/clang' failed with exit status 1". Googling around, it looked like maybe this is something related to libstdc++, but I couldn't figure out how to deal with it. Any help appreciated, thanks!

pyhector_install

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.