Skip to content

Commit

Permalink
Adapt MC calculations in violin.py for BW 2.5 compatibility. + code c…
Browse files Browse the repository at this point in the history
…leanup.
  • Loading branch information
romainsacchi committed Jul 8, 2024
1 parent 5086b99 commit c4bd4a0
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 226 deletions.
134 changes: 14 additions & 120 deletions dev/Untitled.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,148 +2,42 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 9,
"id": "8a5241f6-74e4-466c-9790-7704b2ced3d1",
"metadata": {},
"outputs": [],
"source": [
"from d3blocks import D3Blocks\n",
"from polyviz import sankey, chord, force, violin, choro, treemap\n",
"from polyviz import sankey, chord, force, violin, choro, treemap, __version__, __file__\n",
"import brightway2 as bw\n",
"bw.projects.set_current(\"new\")"
"bw.projects.set_current(\"ei39\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 30,
"id": "f6ca17c9-5527-41f1-982d-e5dabd79dbfb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'maintenance, locomotive' (unit, RoW, None)"
"'SAF production and combustion, petroleum refinery operation, with syncrude' (kilogram, ES, None)"
]
},
"execution_count": 2,
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#act = bw.Database(\"ecoinvent 3.8 cutoff\").random()\n",
"act = [a for a in bw.Database(\"ecoinvent 3.8 cutoff\") if a[\"name\"] == \"maintenance, locomotive\"][0]\n",
"act = [a for a in bw.Database(\"synhelion_common\") if a[\"name\"] == \"SAF production and combustion, petroleum refinery operation, with syncrude\"][0]\n",
"act"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "17980b13-a441-450b-90f0-755abcbfc5b3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'operation, computer, desktop, with cathode ray tube display, active mode' (hour, CH, None)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"act = bw.Database(\"ecoinvent 3.8 cutoff\").random()\n",
"act"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "bde3273a-6ac1-4901-bbd4-59a7b3163a96",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'clinker production' (kilogram, RoW, None)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#act = bw.Database(\"ecoinvent 3.8 cutoff\").random()\n",
"act = [a for a in bw.Database(\"ecoinvent 3.8 cutoff\") if a[\"name\"] == \"clinker production\"][0]\n",
"act"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a17a4a8c-37f9-4f4b-b72b-e2e48ca31c60",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'operation, computer, desktop, with cathode ray tube display, active mode'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"act[\"reference product\"]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "de470b74-b3fd-461d-9d8c-61c5fafd247c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculating LCIA score...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/homebrew/Caskroom/miniforge/base/envs/ab/lib/python3.9/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 6.19e+13)\n",
" warnings.warn(msg, UmfpackWarning)\n"
]
},
{
"data": {
"text/plain": [
"'/Users/romain/GitHub/polyviz/dev/electricity high voltage production mix kilowatt hour CAYK IPCC 2013climate changeGWP 100a treemap.html'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"treemap(\n",
" activity=act,\n",
" method=('IPCC 2013', 'climate change', 'GWP 100a')\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 32,
"id": "53f0db41-723c-4105-aaba-4bb7ff449aa6",
"metadata": {},
"outputs": [
Expand All @@ -158,28 +52,28 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/homebrew/Caskroom/miniforge/base/envs/ab/lib/python3.9/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 6.19e+13)\n",
"/opt/homebrew/Caskroom/miniforge/base/envs/ab/lib/python3.9/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.21e+13)\n",
" warnings.warn(msg, UmfpackWarning)\n"
]
},
{
"data": {
"text/plain": [
"'/Users/romain/GitHub/polyviz/dev/operation computer desktop with cathode ray tube display active mode hour CH IPCC 2013climate changeGWP 100a sankey.html'"
"'/Users/romain/GitHub/polyviz/dev/SAF production and combustion petroleum refinery operation with syncrude kilogram ES IPCC 2021climate changeglobal warming potential GWP100 sankey.html'"
]
},
"execution_count": 4,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sankey(\n",
" activity=act,\n",
" level=3,\n",
" level=4,\n",
" cutoff=0.01,\n",
" #flow_type=\"kilogram\",\n",
" method=('IPCC 2013', 'climate change', 'GWP 100a')\n",
" method=('IPCC 2021', 'climate change', 'global warming potential (GWP100)')\n",
")"
]
},
Expand Down Expand Up @@ -2193,7 +2087,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion polyviz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
PolyViz init
"""

__version__ = (1, 0, 1)
__version__ = (1, 0, 2)

__all__ = (
"sankey",
Expand Down
11 changes: 3 additions & 8 deletions polyviz/chord.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@
from .utils import calculate_supply_chain, check_filepath

try:
from bw2data.backends.peewee import Activity as PeeweeActivity
from bw2data.backends.peewee import Activity
except ImportError:
PeeweeActivity = None

try:
from bw2data.backends import Activity as BW25Activity
except ImportError:
BW25Activity = None
from bw2data.backends import Activity


def chord(
activity: Union[PeeweeActivity, BW25Activity],
activity: Activity,
method: tuple = None,
flow_type: str = None,
level: int = 3,
Expand Down
17 changes: 5 additions & 12 deletions polyviz/choro.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,13 @@
from .utils import check_filepath, get_geo_distribution_of_impacts_for_choro_graph

try:
from bw2data.backends.peewee import Activity as PeeweeActivity
from bw2data.backends.peewee import Activity
except ImportError:
PeeweeActivity = None

try:
from bw2data.backends import Activity as BW25Activity
except ImportError:
BW25Activity = None

valid_types = tuple(filter(None, (PeeweeActivity, BW25Activity)))
from bw2data.backends import Activity


def choro(
activity: Union[PeeweeActivity, BW25Activity],
activity: Activity,
method: tuple,
cutoff: float = 0.001,
filepath: str = None,
Expand All @@ -49,8 +42,8 @@ def choro(

assert isinstance(method, tuple), "`method` should be a tuple."
assert isinstance(
activity, valid_types
), "`activity` should be a brightway2 activity."
activity, Activity
), "`activity` should be a Brightway activity."

# fetch unit of method
unit = bw2data.Method(method).metadata["unit"]
Expand Down
12 changes: 3 additions & 9 deletions polyviz/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""

from collections import defaultdict
from io import StringIO
from typing import List, Union

import bw2data
Expand All @@ -14,14 +13,9 @@
from .utils import calculate_lcia_score, get_gdp_per_country, get_region_definitions

try:
from bw2data.backends.peewee import Activity as PeeweeActivity
from bw2data.backends.peewee import Activity
except ImportError:
PeeweeActivity = None

try:
from bw2data.backends import Activity as BW25Activity
except ImportError:
BW25Activity = None
from bw2data.backends import Activity


def format_supply_chain_dataframe(
Expand Down Expand Up @@ -148,7 +142,7 @@ def add_country_column(dataframe: pd.DataFrame) -> pd.DataFrame:


def get_geo_distribution_of_impacts(
activity: Union[PeeweeActivity, BW25Activity],
activity: Activity,
method: tuple,
cutoff: float = 0.0001,
):
Expand Down
12 changes: 3 additions & 9 deletions polyviz/force.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@

from typing import Union

import bw2data
from d3blocks import D3Blocks

from .dataframe import format_supply_chain_dataframe
from .utils import calculate_supply_chain, check_filepath

try:
from bw2data.backends.peewee import Activity as PeeweeActivity
from bw2data.backends.peewee import Activity
except ImportError:
PeeweeActivity = None

try:
from bw2data.backends import Activity as BW25Activity
except ImportError:
BW25Activity = None
from bw2data.backends import Activity


def force(
activity: Union[PeeweeActivity, BW25Activity],
activity: Activity,
method: tuple,
level: int = 3,
cutoff: float = 0.01,
Expand Down
Loading

0 comments on commit c4bd4a0

Please sign in to comment.