Skip to content

Commit

Permalink
Fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed May 11, 2024
1 parent 495caca commit 69bb8c6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
2 changes: 2 additions & 0 deletions bw_aggregation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"AggregatedDatabase",
"AggregationContext",
"ObsoleteAggregatedDatapackage",
"Speedup",
)

__version__ = "0.0.1"
Expand All @@ -18,6 +19,7 @@

from .main import AggregatedDatabase, ObsoleteAggregatedDatapackage
from .override import AggregationContext
from .estimator import Speedup

DATABASE_BACKEND_MAPPING["aggregated"] = AggregatedDatabase
NODE_PROCESS_CLASS_MAPPING["aggregated"] = Activity
51 changes: 47 additions & 4 deletions bw_aggregation/estimator.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,65 @@
from dataclasses import dataclass
from time import time

from bw2calc import LCA
from bw2data import prepare_lca_inputs, Database

from .calculation import AggregationCalculator
from .calculator import AggregationCalculator
from .override import AggregationContext


@dataclass
class Speedup:
ratio: float
database_name: str
time_with_aggregation: float
time_without_aggregation: float
time_difference_absolute: float
time_difference_relative: float


class CalculationDifferenceEstimator:
def __init__(self, database_name: str):
pass
self.name = database_name
self.db = Database(database_name)

def difference(self) -> Speedup:
without = self.calculate_without_speedup()
with_ = self.calculate_with_speedup()
return Speedup()
return Speedup(
database_name=self.name,
time_difference_relative=with_ / without,
time_difference_absolute=with_ - without,
time_with_aggregation=with_,
time_without_aggregation=without,
)

def calculate_with_speedup(self):
from .main import AggregatedDatabase

process = self.db.random()

with AggregationContext({self.name: False}):
fu, data_objs, _ = prepare_lca_inputs({process: 1})
data_objs[-1] = AggregatedDatabase(self.name).process_aggregated(
in_memory=True
)

start = time()
lca = LCA(fu, data_objs=data_objs)
lca.lci()
end = time()

return end - start

def calculate_without_speedup(self):
process = self.db.random()

with AggregationContext({self.name: False}):
fu, data_objs, _ = prepare_lca_inputs({process: 1})

start = time()
lca = LCA(fu, data_objs=data_objs)
lca.lci()
end = time()

return end - start
6 changes: 4 additions & 2 deletions bw_aggregation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from .calculator import AggregationCalculator
from .override import AggregationContext, aggregation_override
from .estimator import CalculationDifferenceEstimator


class ObsoleteAggregatedDatapackage(Exception):
Expand Down Expand Up @@ -82,7 +83,7 @@ def estimate_speedup(database_name: str) -> float:
Prints to `stdout` and return a float, the ratio of calculation speed with aggregation
to speed without aggregation."""
pass
return CalculationDifferenceEstimator(database_name).difference()

@staticmethod
def convert_existing(database_name: str) -> None:
Expand Down Expand Up @@ -167,7 +168,7 @@ def write(self, data, process=True, searchable=True) -> None:
if process:
self.process_aggregated()

def process_aggregated(self, in_memory: bool = False) -> None:
def process_aggregated(self, in_memory: bool = False) -> Datapackage:
"""Create structured arrays for the aggregated biosphere emissions, and for unitary production."""
# Try to avoid race conditions - but no guarantee
self.metadata["aggregation_calculation_timestamp"] = dt.now().isoformat()
Expand Down Expand Up @@ -199,3 +200,4 @@ def process_aggregated(self, in_memory: bool = False) -> None:
)
if not in_memory:
dp.finalize_serialization()
return dp
7 changes: 7 additions & 0 deletions tests/test_estimation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from bw_aggregation import AggregatedDatabase, Speedup


def test_speedup_estimate(background):
speedup = AggregatedDatabase.estimate_speedup("a")
assert isinstance(speedup, Speedup)
assert speedup.time_difference_relative < 1

0 comments on commit 69bb8c6

Please sign in to comment.