Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Sep 12, 2024
1 parent 61a8c8a commit 79c3fbe
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 277 deletions.
15 changes: 11 additions & 4 deletions bw2io/export/excel.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from pathlib import Path
from typing import Optional, List
import collections
import numbers
import os
from pathlib import Path
from typing import List, Optional

import xlsxwriter
from bw2data import Database, projects
Expand Down Expand Up @@ -289,7 +289,11 @@ def write_lci_excel(database_name, objs=None, sections=None, dirpath=None):


def write_lci_matching(
db: List[dict], database_name: str, only_unlinked: bool=False, only_activity_names: bool=False, output_dir: Optional[Path] = None
db: List[dict],
database_name: str,
only_unlinked: bool = False,
only_activity_names: bool = False,
output_dir: Optional[Path] = None,
):
"""
Write matched and unmatched exchanges to Excel file
Expand Down Expand Up @@ -357,7 +361,10 @@ def write_row(sheet, row, data, exc=True):
safe_name = safe_filename(database_name, False)
suffix = "-unlinked" if only_unlinked else ("-names" if only_activity_names else "")

filepath = Path(output_dir or projects.output_dir) / f"db-matching-{safe_name}{suffix}.xlsx"
filepath = (
Path(output_dir or projects.output_dir)
/ f"db-matching-{safe_name}{suffix}.xlsx"
)

workbook = xlsxwriter.Workbook(filepath)
bold = workbook.add_format({"bold": True})
Expand Down
11 changes: 5 additions & 6 deletions bw2io/importers/base_lci.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
import functools
import itertools
import warnings
from typing import Optional, Tuple, Callable, List, Set, Union
from pathlib import Path
from typing import Callable, List, Optional, Set, Tuple, Union

from bw2data import Database, config, databases, labels, parameters, get_node, projects
import randonneur as rn
import randonneur_data as rd
from bw2data import Database, config, databases, get_node, labels, parameters, projects
from bw2data.data_store import ProcessedDataStore
from bw2data.errors import UnknownObject
from bw2data.parameters import (
ActivityParameter,
DatabaseParameter,
ParameterizedExchange,
ProjectParameter,
)
from bw2data.errors import UnknownObject
import randonneur as rn
import randonneur_data as rd

from ..errors import NonuniqueCode, StrategyError, WrongDatabase
from ..export.excel import write_lci_matching
Expand All @@ -35,7 +35,6 @@
from ..utils import activity_hash
from .base import ImportBase


EXCHANGE_SPECIFIC_KEYS = (
"amount",
"functional",
Expand Down
4 changes: 2 additions & 2 deletions bw2io/importers/simapro_block_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Optional, Union
from uuid import uuid4

from bw2data import Database, config, databases, labels, get_node
from bw2data import Database, config, databases, get_node, labels
from bw2data.errors import UnknownObject
from bw_simapro_csv import SimaProCSV

Expand All @@ -25,14 +25,14 @@
normalize_biosphere_names,
normalize_simapro_biosphere_categories,
normalize_simapro_biosphere_names,
normalize_simapro_labels_to_brightway_standard,
normalize_units,
override_process_name_using_single_functional_exchange,
set_code_by_activity_hash,
set_metadata_using_single_functional_exchange,
split_simapro_name_geo,
strip_biosphere_exc_locations,
update_ecoinvent_locations,
normalize_simapro_labels_to_brightway_standard,
)
from ..utils import activity_hash
from .base_lci import LCIImporter
Expand Down
6 changes: 4 additions & 2 deletions bw2io/strategies/generic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import defaultdict
import numbers
import pprint
from collections import defaultdict
from copy import deepcopy
from typing import Iterable, List, Optional, Union

Expand Down Expand Up @@ -797,7 +797,9 @@ def match_against_only_available_in_given_context_tree(

try:
exc["input"] = mapping[
tuple([exc.get(field) for field in ffields] + [exc['categories'][0]])
tuple(
[exc.get(field) for field in ffields] + [exc["categories"][0]]
)
]
except KeyError:
continue
Expand Down
74 changes: 43 additions & 31 deletions bw2io/strategies/products.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from pprint import pformat
from uuid import uuid4
from typing import List
import bw2data as bd
from uuid import uuid4

import bw2data as bd

EDGE_CORE_COLUMNS = [
"name",
Expand All @@ -23,51 +23,63 @@

def create_products_as_new_nodes(data: List[dict]) -> List[dict]:
"""
Create new product nodes and link to them if needed.
Create new product nodes and link to them if needed.
We create new `product` if the following conditions are met:
We create new `product` if the following conditions are met:
* The dataset is not multifunctional (
`dataset.get("type") != bd.labels.multifunctional_node_default`). Multifunctional datasets
handle product creation separately.
* The edge is functional (`obj.get("functional") is True`)
* The edge is unlinked (`obj.get("input")` is falsey)
* The given edge has a `name`, and that `name` is different than the dataset `name`
* The combination of `name` and `location` is not present in the other dataset nodes. If no
`location` attribute is given for the edge under consideration, we use the `location` of the
dataset.
* The dataset is not multifunctional (
`dataset.get("type") != bd.labels.multifunctional_node_default`). Multifunctional datasets
handle product creation separately.
* The edge is functional (`obj.get("functional") is True`)
* The edge is unlinked (`obj.get("input")` is falsey)
* The given edge has a `name`, and that `name` is different than the dataset `name`
* The combination of `name` and `location` is not present in the other dataset nodes. If no
`location` attribute is given for the edge under consideration, we use the `location` of the
dataset.
Create new nodes, and links the originating edges to the new product nodes.
Create new nodes, and links the originating edges to the new product nodes.
Modifies data in-place, and returns the modified `data`.
Modifies data in-place, and returns the modified `data`.
"""
combos = {(ds.get("name"), ds.get("location")) for ds in data}
nodes = []

for ds in data:
if ds.get('type') == bd.labels.multifunctional_node_default:
if ds.get("type") == bd.labels.multifunctional_node_default:
# Has its own product handling
continue
for edge in ds.get('exchanges', []):
if edge.get('functional') and not edge.get('input') and edge.get('name') and edge['name'] != ds.get('name'):
for edge in ds.get("exchanges", []):
if (
edge.get("functional")
and not edge.get("input")
and edge.get("name")
and edge["name"] != ds.get("name")
):
if not ds.get("database"):
raise KeyError("""
raise KeyError(
"""
Can't create a new `product` node, as dataset is missing `database` attribute:
{}""".format(pformat(ds)))
key = (edge['name'], edge.get('location') or ds.get('location'))
{}""".format(
pformat(ds)
)
)
key = (edge["name"], edge.get("location") or ds.get("location"))
if key not in combos:
code = uuid4().hex
nodes.append({
'name': edge['name'],
'location': key[1] or bd.config.global_location,
'unit': edge.get('unit') or ds.get('unit'),
'exchanges': [],
'code': code,
'type': bd.labels.product_node_default,
'database': ds['database'],
} | {k: v for k, v in edge.items() if k not in EDGE_CORE_COLUMNS})
edge['input'] = (ds['database'], code)
nodes.append(
{
"name": edge["name"],
"location": key[1] or bd.config.global_location,
"unit": edge.get("unit") or ds.get("unit"),
"exchanges": [],
"code": code,
"type": bd.labels.product_node_default,
"database": ds["database"],
}
| {k: v for k, v in edge.items() if k not in EDGE_CORE_COLUMNS}
)
edge["input"] = (ds["database"], code)
combos.add(key)

if nodes:
Expand Down
10 changes: 5 additions & 5 deletions bw2io/strategies/simapro.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,9 +920,9 @@ def normalize_simapro_labels_to_brightway_standard(db: List[dict]) -> List[dict]
labels.
"""
for ds in db:
for exc in filter(lambda x: 'input' not in x, ds.get('exchanges', [])):
if 'context' in exc and 'categories' not in exc:
exc['categories'] = tuple(exc['context'])
if 'identifier' in exc and 'code' not in exc:
exc['code'] = exc['identifier']
for exc in filter(lambda x: "input" not in x, ds.get("exchanges", [])):
if "context" in exc and "categories" not in exc:
exc["categories"] = tuple(exc["context"])
if "identifier" in exc and "code" not in exc:
exc["code"] = exc["identifier"]
return db
16 changes: 8 additions & 8 deletions bw2io/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math
import hashlib
import json
import math
import os
import pprint
from numbers import Number
Expand Down Expand Up @@ -123,8 +123,8 @@ def rescale_exchange(exc: dict, factor: float) -> dict:
exc.update(
{
"uncertainty type": UndefinedUncertainty.id,
"loc": exc['amount'] * factor,
"amount": exc['amount'] * factor,
"loc": exc["amount"] * factor,
"amount": exc["amount"] * factor,
}
)
for field in ("scale", "shape", "minimum", "maximum", "negative"):
Expand All @@ -138,16 +138,16 @@ def rescale_exchange(exc: dict, factor: float) -> dict:
exc.update(
{
"scale": abs(exc["scale"] * factor),
"loc": exc['amount'] * factor,
"amount": exc['amount'] * factor,
"loc": exc["amount"] * factor,
"amount": exc["amount"] * factor,
}
)
elif exc["uncertainty type"] == LognormalUncertainty.id:
exc.update(
{
"loc": math.log(abs(exc['amount'] * factor)),
"negative": (exc['amount'] * factor) < 0,
"amount": exc['amount'] * factor,
"loc": math.log(abs(exc["amount"] * factor)),
"negative": (exc["amount"] * factor) < 0,
"amount": exc["amount"] * factor,
}
)
elif exc["uncertainty type"] in (TriangularUncertainty.id, UniformUncertainty.id):
Expand Down
3 changes: 1 addition & 2 deletions tests/base_lci_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from bw2data.parameters import *
from bw2data.tests import bw2test

from bw2io.errors import StrategyError
from bw2io.errors import NonuniqueCode, WrongDatabase
from bw2io.errors import NonuniqueCode, StrategyError, WrongDatabase
from bw2io.importers.base_lci import LCIImporter

DATA = [
Expand Down
5 changes: 3 additions & 2 deletions tests/simapro_block.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from bw2io.importers import SimaProBlockCSVImporter
from bw2data.tests import bw2test
from bw2data import Database, get_node
from bw2data.tests import bw2test

from bw2io.importers import SimaProBlockCSVImporter


class Mock(SimaProBlockCSVImporter):
Expand Down
7 changes: 1 addition & 6 deletions tests/strategies/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,12 +720,7 @@ def test_match_against_top_level_context_custom_kinds():
]
}
]
assert (
match_against_top_level_context(
given, "foo", kinds=["other"]
)
== expected
)
assert match_against_top_level_context(given, "foo", kinds=["other"]) == expected


@bw2test
Expand Down
45 changes: 23 additions & 22 deletions tests/strategies/simapro.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,26 +306,27 @@ def test_override_process_name_using_single_functional_exchange():


def test_normalize_simapro_labels_to_brightway_standard():
given = [{
'exchanges': [{
'input': True,
'context': 'something'
}, {
'context': ['foo'],
}, {
'identifier': 'abcde'
}]
}]
expected = [{
'exchanges': [{
'input': True,
'context': 'something'
}, {
'categories': ('foo',),
'context': ['foo'],
}, {
'code': 'abcde',
'identifier': 'abcde'
}]
}]
given = [
{
"exchanges": [
{"input": True, "context": "something"},
{
"context": ["foo"],
},
{"identifier": "abcde"},
]
}
]
expected = [
{
"exchanges": [
{"input": True, "context": "something"},
{
"categories": ("foo",),
"context": ["foo"],
},
{"code": "abcde", "identifier": "abcde"},
]
}
]
assert normalize_simapro_labels_to_brightway_standard(given) == expected
2 changes: 1 addition & 1 deletion tests/strategies/simapro_name_splitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_splitting_exchanges():
{"name": "Absorption chiller 100kW /CH/I U"},
{"name": "Cheese/CH"},
],
}
},
]
result = [
{
Expand Down
Loading

0 comments on commit 79c3fbe

Please sign in to comment.