-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |