Skip to content

Commit

Permalink
Update wrt master
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Oct 31, 2023
1 parent 317e15b commit 55eba6e
Show file tree
Hide file tree
Showing 48 changed files with 886 additions and 610 deletions.
23 changes: 8 additions & 15 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,12 @@ jobs:
run: |
pip install pathlib
pip install -r requirements.txt --upgrade pip
pip install -e .
pip install pytest
pip install pytest-cov
pip install coveralls
pip install -e .[test]
- name: Run tests
run: |
pytest -m "not ecoinvent" -s --cov=premise
coveralls -v --service=github-actions
pytest -m "not ecoinvent" -s
env:
GITHUB_TOKEN: ${{ secrets.COVERALLS }}
IAM_FILES_KEY: ${{ secrets.IAM_FILES_KEY }}
Expand Down Expand Up @@ -114,26 +111,22 @@ jobs:
run: >-
python -m
build
--sdist
--wheel
--outdir dist/
.
- name: Publish distribution 📦 to PyPI if Release
if: startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@master
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: ${{ secrets.PYPI_USER }}
password: ${{ secrets.PYPI_PASSWORD }}
skip_existing: true
skip-existing: true

- name: Publish distribution 📦 to Test PyPI if Push
uses: pypa/gh-action-pypi-publish@master
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: ${{ secrets.TEST_PYPI_USER }}
password: ${{ secrets.TEST_PYPI_PASSWORD }}
repository_url: https://test.pypi.org/legacy/
skip_existing: true
repository-url: https://test.pypi.org/legacy/
skip-existing: true

conda:
name: (${{ matrix.python-version }}, ubuntu-latest)
Expand Down Expand Up @@ -229,4 +222,4 @@ jobs:
conda install bottleneck
pytest -m "not ecoinvent"
env:
IAM_FILES_KEY: ${{ secrets.IAM_FILES_KEY }}
IAM_FILES_KEY: ${{ secrets.IAM_FILES_KEY }}
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ As it now evolves towards a more IAM-neutral approach, a change of name was cons

Scientific publication available here: [Sacchi et al, 2022](https://doi.org/10.1016/j.rser.2022.112311).

What's new in 1.8.0?
====================

- Added support for brightway 2.5
- Added support for Python 3.11
- Uses bw2io 0.8.10
- Adds electricity storage in electricity markets -- see [docs](https://premise.readthedocs.io/en/latest/transform.html#storage)
- Adds [scenario explorer dashboard](https://premisedash-6f5a0259c487.herokuapp.com/)

What's new in 1.5.0?
====================

Expand Down Expand Up @@ -52,7 +61,7 @@ ecoinvent 3 to reflect projected energy policy trajectories.

Requirements
------------
* **Python 3.9**
* **Python 3.9, 3.10 or 3.11**
* License for [ecoinvent 3][1]
* Some IAM output files come with the library and are located by default in the subdirectory "/data/iam_output_files". **If you wish to use
those files, you need to request (by [email](mailto:[email protected])) an encryption key from the developers**.
Expand All @@ -67,11 +76,8 @@ Two options:
A development version with the latest advancements (but with the risks of unseen bugs),
is available from Anaconda Cloud:


conda config --append conda-forge
conda config --append cmutel
conda config --append romainsacchi
conda install premise

conda install -c romainsacchi premise


For a more stable and proven version, from Pypi:
Expand All @@ -88,17 +94,22 @@ The best way is to follow [the examples from the Jupyter Notebook](https://githu

# Support

Do not hesitate to contact the development team at [[email protected]](mailto:[email protected])
or [[email protected]](mailto:[email protected]).
Do not hesitate to contact [[email protected]](mailto:[email protected]).

## Maintainers
## Contributors

* [Romain Sacchi](https://github.com/romainsacchi)
* [Alois Dirnaichner](https://github.com/Loisel)
* [Tom Mike Terlouw](https://github.com/tomterlouw)
* [Laurent Vandepaer](https://github.com/lvandepaer)
* [Chris Mutel](https://github.com/cmutel/)


## Maintainers

* [Romain Sacchi](https://github.com/romainsacchi)
* [Chris Mutel](https://github.com/cmutel/)

## Contributing

See [contributing](https://github.com/polca/premise/blob/master/CONTRIBUTING.md).
Expand Down
23 changes: 12 additions & 11 deletions conda/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ requirements:
- python
- setuptools
run:
- bottleneck
- bw2data
- bw2io >=0.8.10
- constructive_geometries>=0.8.2
- cryptography
- datapackage
- numpy
- pandas
- bw2io >=0.8.10
- bw2data
- wurst
- xarray
- platformdirs
- prettytable
- pyarrow
- pycountry
- cryptography
- pyYaml
- sparse>=0.14.0
- schema
- datapackage
- requests
- bottleneck
- constructive_geometries>=0.8.2
- pyarrow
- schema
- sparse>=0.14.0
- wurst
- xarray

test:
imports:
Expand Down
13 changes: 8 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
author = "Romain Sacchi, Alois Dirnaichner, Chris Mutel"

# The full version, including alpha/beta/rc tags
release = "1.5.0-alpha"
release = "1.7.2"

# -- General configuration ---------------------------------------------------

Expand All @@ -34,28 +34,31 @@
extensions = [
"sphinx.ext.autodoc", # Core library for html generation from docstrings
"sphinx.ext.autosummary", # Create neat summary tables
"sphinx_rtd_theme",
]
autosummary_generate = True # Turn on sphinx.ext.autosummary

master_doc = "index"

autodoc_mock_imports = [
"numpy",
"wurst==0.3.3",
"bw2io==0.8.7",
"pandas",
"bw2io >=0.8.10",
"bw2data",
"wurst",
"xarray",
"prettytable",
"pycountry",
"cryptography",
"premise_gwp",
"pyYaml",
"sparse",
"sparse>=0.14.0",
"schema",
"datapackage",
"requests",
"bottleneck",
"constructive_geometries>=0.8.2",
"pyarrow",
"premise",
]

Expand All @@ -77,6 +80,6 @@
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
html_static_path = []

# html_sidebars = { '**': ['globaltoc.html', 'relations.html', 'sourcelink.html', 'searchbox.html'] }
7 changes: 1 addition & 6 deletions premise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
__all__ = ("NewDatabase", "clear_cache", "get_regions_definition")
__version__ = (1, 7, 2)
__version__ = (1, 8, 1)

from pathlib import Path

DATA_DIR = Path(__file__).resolve().parent / "data"
INVENTORY_DIR = Path(__file__).resolve().parent / "data" / "additional_inventories"
VARIABLES_DIR = Path(__file__).resolve().parent / "iam_variables_mapping"

from .ecoinvent_modification import NewDatabase
from .utils import clear_cache, get_regions_definition
9 changes: 4 additions & 5 deletions premise/activity_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
mapping between ``premise`` and ``ecoinvent`` terminology.
"""

import sys
from collections import defaultdict
from pathlib import Path
from typing import List, Union

import yaml
from wurst import searching as ws

from . import DATA_DIR, VARIABLES_DIR
from .filesystem_constants import DATA_DIR, VARIABLES_DIR

POWERPLANT_TECHS = VARIABLES_DIR / "electricity_variables.yaml"
FUELS_TECHS = VARIABLES_DIR / "fuels_variables.yaml"
Expand Down Expand Up @@ -269,8 +268,8 @@ def generate_sets_from_filters(self, filtr: dict, database=None) -> dict:

# check if all keys have values
# if not, print warning
for key, val in mapping.items():
if not val:
print(f"Warning: No activities found for {key} -- revise mapping.")
# for key, val in mapping.items():
# if not val:
# print(f"Warning: No activities found for {key} -- revise mapping.")

return mapping
30 changes: 30 additions & 0 deletions premise/brightway2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from bw2data import databases
from bw2io.importers.base_lci import LCIImporter
from wurst.linking import change_db_name, check_internal_linking, link_internal

from .utils import reset_all_codes


class BW2Importer(LCIImporter):
def __init__(self, db_name, data):
self.db_name = db_name
self.data = data
for act in self.data:
act["database"] = self.db_name

# we override `write_database`
# to allow existing databases
# to be overwritten
def write_database(self):
if self.db_name in databases:
print(f"Database {self.db_name} already exists: it will be overwritten.")
super().write_database()


def write_brightway_database(data, name):
# Restore parameters to Brightway2 format
# which allows for uncertainty and comments
change_db_name(data, name)
link_internal(data)
check_internal_linking(data)
BW2Importer(name, data).write_database()
86 changes: 86 additions & 0 deletions premise/brightway25.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import itertools
from copy import copy

from bw2data import Database, databases
from bw2io.importers.base_lci import LCIImporter
from wurst.linking import change_db_name, check_internal_linking, link_internal


class BW25Importer(LCIImporter):
def __init__(self, db_name, data):
self.db_name = db_name
self.data = data
for act in self.data:
act["database"] = self.db_name

# we override `write_database`
# to allow existing databases
# to be overwritten
def write_database(self):
def no_exchange_generator(data):
for ds in data:
cp = copy(ds)
cp["exchanges"] = []
yield cp

if self.db_name in databases:
print(f"Database {self.db_name} already exists: " "it will be overwritten.")
super().write_database(
list(no_exchange_generator(self.data)), backend="iotable"
)

dependents = {exc["input"][0] for ds in self.data for exc in ds["exchanges"]}
lookup = {
obj.key: obj.id
for obj in itertools.chain(*[Database(label) for label in dependents])
}

def technosphere_generator(data, lookup):
for ds in data:
target = lookup[(ds["database"], ds["code"])]
for exc in ds["exchanges"]:
if exc["type"] in (
"substitution",
"production",
"generic production",
):
yield {
"row": lookup[exc["input"]],
"col": target,
"amount": exc["amount"],
"flip": False,
}
elif exc["type"] == "technosphere":
yield {
"row": lookup[exc["input"]],
"col": target,
"amount": exc["amount"],
"flip": True,
}

def biosphere_generator(data, lookup):
for ds in data:
target = lookup[(ds["database"], ds["code"])]
for exc in ds["exchanges"]:
if exc["type"] == "biosphere":
yield {
"row": lookup[exc["input"]],
"col": target,
"amount": exc["amount"],
"flip": False,
}

Database(self.db_name).write_exchanges(
technosphere_generator(self.data, lookup),
biosphere_generator(self.data, lookup),
list(dependents),
)


def write_brightway_database(data, name):
# Restore parameters to Brightway2 format
# which allows for uncertainty and comments
change_db_name(data, name)
link_internal(data)
check_internal_linking(data)
BW25Importer(name, data).write_database()
7 changes: 1 addition & 6 deletions premise/cement.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@
"""

import logging.config
from collections import defaultdict
from pathlib import Path

import yaml

from .logger import create_logger
from .transformation import BaseTransformation, Dict, IAMDataCollection, List, np, ws
from .utils import DATA_DIR

logger = create_logger("cement")

Expand Down Expand Up @@ -376,7 +371,7 @@ def build_clinker_production_datasets(self) -> Dict[str, dict]:

# Carbon capture rate: share of capture of total CO2 emitted
carbon_capture_rate = self.get_carbon_capture_rate(
loc=dataset["location"], sector="cement, dry feed rotary kiln"
loc=dataset["location"], sector="cement"
)

dataset["log parameters"].update(
Expand Down
Loading

0 comments on commit 55eba6e

Please sign in to comment.