bw-functional 0b87__tar.gz → 0b90__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. {bw_functional-0b87/bw_functional.egg-info → bw_functional-0b90}/PKG-INFO +1 -1
  2. {bw_functional-0b87 → bw_functional-0b90}/bw_functional/__init__.py +21 -5
  3. {bw_functional-0b87 → bw_functional-0b90}/bw_functional/node_classes.py +41 -4
  4. bw_functional-0b90/bw_functional/update.py +32 -0
  5. {bw_functional-0b87 → bw_functional-0b90/bw_functional.egg-info}/PKG-INFO +1 -1
  6. {bw_functional-0b87 → bw_functional-0b90}/bw_functional.egg-info/SOURCES.txt +1 -0
  7. {bw_functional-0b87 → bw_functional-0b90}/tests/test_product_node.py +4 -4
  8. {bw_functional-0b87 → bw_functional-0b90}/LICENSE +0 -0
  9. {bw_functional-0b87 → bw_functional-0b90}/MANIFEST.in +0 -0
  10. {bw_functional-0b87 → bw_functional-0b90}/README.md +0 -0
  11. {bw_functional-0b87 → bw_functional-0b90}/bw_functional/allocation.py +0 -0
  12. {bw_functional-0b87 → bw_functional-0b90}/bw_functional/convert.py +0 -0
  13. {bw_functional-0b87 → bw_functional-0b90}/bw_functional/database.py +0 -0
  14. {bw_functional-0b87 → bw_functional-0b90}/bw_functional/edge_classes.py +0 -0
  15. {bw_functional-0b87 → bw_functional-0b90}/bw_functional.egg-info/dependency_links.txt +0 -0
  16. {bw_functional-0b87 → bw_functional-0b90}/bw_functional.egg-info/requires.txt +0 -0
  17. {bw_functional-0b87 → bw_functional-0b90}/bw_functional.egg-info/top_level.txt +0 -0
  18. {bw_functional-0b87 → bw_functional-0b90}/pyproject.toml +0 -0
  19. {bw_functional-0b87 → bw_functional-0b90}/setup.cfg +0 -0
  20. {bw_functional-0b87 → bw_functional-0b90}/setup.py +0 -0
  21. {bw_functional-0b87 → bw_functional-0b90}/tests/test_allocation.py +0 -0
  22. {bw_functional-0b87 → bw_functional-0b90}/tests/test_database.py +0 -0
  23. {bw_functional-0b87 → bw_functional-0b90}/tests/test_mf_node.py +0 -0
  24. {bw_functional-0b87 → bw_functional-0b90}/tests/test_process_node.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bw-functional
3
- Version: 0b87
3
+ Version: 0b90
4
4
  Summary: Code for handling multifunctional activities in Brightway
5
5
  Author-email: Chris Mutel <cmutel@gmail.com>, Marin Visscher <m.r.visscher@cml.leidenuniv.nl>
6
6
  Maintainer-email: Marin Visscher <m.r.visscher@cml.leidenuniv.nl>
@@ -1,5 +1,4 @@
1
1
  __all__ = (
2
- "__version__",
3
2
  "allocation_strategies",
4
3
  "generic_allocation",
5
4
  "Process",
@@ -9,11 +8,12 @@ __all__ = (
9
8
  "FunctionalSQLiteDatabase",
10
9
  "property_allocation",
11
10
  "convert_sqlite_to_functional_sqlite",
12
- "convert_functional_sqlite_to_sqlite"
11
+ "convert_functional_sqlite_to_sqlite",
12
+ "update",
13
13
  )
14
- # import os
15
- __version__ = "0.0.2"
16
14
 
15
+ from importlib import metadata
16
+ # import os
17
17
  from logging import getLogger
18
18
 
19
19
  from bw2data import labels
@@ -24,6 +24,7 @@ from .database import FunctionalSQLiteDatabase
24
24
  from .node_classes import Process, Product
25
25
  from .edge_classes import MFExchange, MFExchanges
26
26
  from .convert import convert_sqlite_to_functional_sqlite, convert_functional_sqlite_to_sqlite
27
+ from .update import update, latest
27
28
 
28
29
  log = getLogger(__name__)
29
30
 
@@ -38,9 +39,10 @@ if "nonfunctional" not in labels.node_types:
38
39
 
39
40
  # make sure allocation happens on parameter changes
40
41
  def _init_signals():
41
- from bw2data.signals import on_activity_parameter_recalculate
42
+ from bw2data.signals import on_activity_parameter_recalculate, project_changed
42
43
 
43
44
  on_activity_parameter_recalculate.connect(_check_parameterized_exchange_for_allocation)
45
+ project_changed.connect(_check_and_update)
44
46
 
45
47
  def _check_parameterized_exchange_for_allocation(_, name):
46
48
  import bw2data as bd
@@ -65,4 +67,18 @@ def _check_parameterized_exchange_for_allocation(_, name):
65
67
  continue
66
68
  process.allocate()
67
69
 
70
+ def _check_and_update(dataset):
71
+ if dataset.data is None:
72
+ dataset.data = {}
73
+ dataset.save()
74
+
75
+ current = dataset.data.get("bw_functional_version")
76
+
77
+ if current != latest:
78
+ log.info(f"Updating {dataset.name} to latest bw_functional datastructure version {latest}")
79
+ dataset.data["bw_functional_version"] = update(current)
80
+ dataset.save()
81
+
82
+ return
83
+
68
84
  _init_signals()
@@ -11,6 +11,13 @@ from .edge_classes import MFExchanges, MFExchange
11
11
  log = getLogger(__name__)
12
12
 
13
13
 
14
+ INHERITED_FIELDS = [
15
+ "database",
16
+ "location",
17
+ "name",
18
+ ]
19
+
20
+
14
21
  class MFActivity(Activity):
15
22
  """
16
23
  A class representing an activity of the functional_sqlite backend.
@@ -192,9 +199,19 @@ class Process(MFActivity):
192
199
 
193
200
  super().save(signal, data_already_set, force_insert)
194
201
 
195
- if not created and old.data.get("allocation") != self.get("allocation"):
202
+ if created:
203
+ return
204
+
205
+ if old.data.get("allocation") != self.get("allocation"):
196
206
  self.allocate()
197
207
 
208
+ if changed_fields := [field for field in INHERITED_FIELDS if self.get(field) != old.data.get(field)]:
209
+ log.info(f"Updating inherited fields {changed_fields} for products of process {self}")
210
+ for product in self.products():
211
+ for field in changed_fields:
212
+ product._set_inherited(field, self.get(field))
213
+ product.save()
214
+
198
215
  def copy(self, *args, **kwargs):
199
216
  """
200
217
  Create a copy of the process.
@@ -209,8 +226,9 @@ class Process(MFActivity):
209
226
  act = super().copy(*args, **kwargs)
210
227
  act.production().delete() # Delete the production exchanges to avoid duplicates
211
228
 
229
+ database = kwargs.get("database", self["database"])
212
230
  for product in self.products():
213
- prod_copy = product.copy(processor=act.key)
231
+ prod_copy = product.copy(processor=act.key, database=database)
214
232
 
215
233
  edge_data = product.processing_edge.as_dict()
216
234
  edge_data["input"] = prod_copy.key
@@ -244,9 +262,16 @@ class Process(MFActivity):
244
262
  Returns:
245
263
  Product: A new product.
246
264
  """
265
+ if kwargs.get("reference product") is None:
266
+
267
+ kwargs["reference product"] = kwargs.get("product", kwargs.get("name", f"Unnamed {type}"))
268
+
247
269
  kwargs["type"] = type
248
270
  kwargs["processor"] = self.key
249
- kwargs["database"] = self["database"]
271
+
272
+ for field in INHERITED_FIELDS:
273
+ kwargs[field] = self.get(field)
274
+
250
275
  kwargs["properties"] = {p: self.property_template(p) for p in self.available_properties()}
251
276
  return Product(**kwargs)
252
277
 
@@ -287,7 +312,7 @@ class Process(MFActivity):
287
312
  "normalize": normalize.pop() if normalize else True
288
313
  }
289
314
 
290
- def products(self):
315
+ def products(self) -> list["Product"]:
291
316
  """
292
317
  Retrieve the products (products or wastes) associated with the process.
293
318
 
@@ -366,6 +391,18 @@ class Product(MFActivity):
366
391
  products, including saving, deleting, and validating them, as well as handling
367
392
  processing edges and substitution.
368
393
  """
394
+ def __setitem__(self, key, value):
395
+ if key in INHERITED_FIELDS:
396
+ raise KeyError(f"Field '{key}' is inherited from the processor and cannot be set directly.")
397
+ if key in ["product", "reference product"]:
398
+ # explicit synonyms
399
+ super().__setitem__("product", value)
400
+ super().__setitem__("reference product", value)
401
+ return
402
+ super().__setitem__(key, value)
403
+
404
+ def _set_inherited(self, key, value):
405
+ super().__setitem__(key, value)
369
406
 
370
407
  def save(self, signal: bool = True, data_already_set: bool = False, force_insert: bool = False):
371
408
  """
@@ -0,0 +1,32 @@
1
+
2
+ latest = "0b89"
3
+
4
+ def update(version: str | None) -> str:
5
+ if version is None:
6
+ update_0b89()
7
+ return "0b89"
8
+ return version
9
+
10
+ def update_0b89():
11
+ import bw2data as bd
12
+ import tqdm
13
+ from .node_classes import Process
14
+
15
+ bf_db_names = [name for name, dict in bd.databases.items() if dict.get("backend") == "functional_sqlite"]
16
+ for db_name in bf_db_names:
17
+ database = bd.Database(db_name)
18
+
19
+ for process in tqdm.tqdm(database, desc=f"Updating {db_name} to bw_functional 0b89", total=len(database)):
20
+ if not isinstance(process, Process):
21
+ continue
22
+
23
+ for product in process.products():
24
+ if not product.get("product"):
25
+ product["product"] = product["name"]
26
+
27
+ product._set_inherited("name", process["name"])
28
+ product._set_inherited("database", process["database"])
29
+ product._set_inherited("location", process["location"])
30
+
31
+ product.save()
32
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bw-functional
3
- Version: 0b87
3
+ Version: 0b90
4
4
  Summary: Code for handling multifunctional activities in Brightway
5
5
  Author-email: Chris Mutel <cmutel@gmail.com>, Marin Visscher <m.r.visscher@cml.leidenuniv.nl>
6
6
  Maintainer-email: Marin Visscher <m.r.visscher@cml.leidenuniv.nl>
@@ -9,6 +9,7 @@ bw_functional/convert.py
9
9
  bw_functional/database.py
10
10
  bw_functional/edge_classes.py
11
11
  bw_functional/node_classes.py
12
+ bw_functional/update.py
12
13
  bw_functional.egg-info/PKG-INFO
13
14
  bw_functional.egg-info/SOURCES.txt
14
15
  bw_functional.egg-info/dependency_links.txt
@@ -172,10 +172,10 @@ def test_product_virtual_edges(basic):
172
172
  product = basic.get(code="2")
173
173
  edges = product.virtual_edges
174
174
 
175
- assert len(edges) == 1
176
- assert edges[0]["input"] == ('basic', 'a')
177
- assert edges[0]["output"] == product.key
178
- assert edges[0]["amount"] == 5
175
+ assert len(edges) == 2
176
+ assert edges[1]["input"] == ('basic', 'a')
177
+ assert edges[1]["output"] == product.key
178
+ assert edges[1]["amount"] == 5
179
179
 
180
180
 
181
181
  def test_product_new_edge(basic):
File without changes
File without changes
File without changes
File without changes
File without changes