Skip to content

Commit

Permalink
Fixed bug #33 : getOutputAmount was wrong with activities having circ…
Browse files Browse the repository at this point in the history
…ular input exchanges with themselves
  • Loading branch information
raphaeljolivet committed Nov 21, 2023
1 parent fd2f3c2 commit 5911a4d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Fix bug in ActivityExtended#getExchanges introduced by merge #14
* Added breakdown of impacts by arbitrary attribute with the parameter `axis` of `compute_impacts`
* Added `functional_unit` in compute_impacts : You are not obliged to define a custom activity anymore
* findActivities() is now case insensitive by default
* Fixed bug #33 : getOutputAmount was wrong with activities having circular input exchanges with themselves


# 1.0.4

Expand Down
7 changes: 2 additions & 5 deletions lca_algebraic/base_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def error(*args, **kwargs):


def _isOutputExch(exc) :
return exc.get('input') == exc.get('output') or exc.get("type") == "production"
return exc.get("type") == "production"


def _isnumber(value):
Expand Down Expand Up @@ -67,10 +67,7 @@ def Min(a, b) :
def _actDesc(act: Activity):
"""Generate pretty name for activity + basic information """
name = _actName(act)
amount = 1
for ex in act.exchanges() :
if _isOutputExch(ex):
amount = ex['amount']
amount = act.getOutputAmount()

return "%s (%f %s)" % (name, amount, act['unit'])

Expand Down
12 changes: 5 additions & 7 deletions lca_algebraic/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,11 @@ def getAmount(self, *args, sum=False, **kargs):
def getOutputAmount(self):

""" Return the amount of the production : 1 if none is found """
res = 1
res = 1.0

for exch in self.exchanges() :
if exch['input'] == exch['output']:
# Not 1 ?
if exch['amount'] != 1:
res = exch['amount']
if (exch['input'] == exch['output']) and (exch["type"] == "production"):
res = exch['amount']
break
return res

Expand Down Expand Up @@ -782,7 +781,7 @@ def newSwitchAct(dbname, name, paramDef: ParamDef, acts_dict: Dict[str, Activity
return res


def printAct(*args, impact=None, **params):
def printAct(*args, **params):
"""
Print activities and their exchanges.
If parameter values are provided, formulas will be evaluated accordingly.
Expand All @@ -793,7 +792,6 @@ def printAct(*args, impact=None, **params):

activities = args


for act in activities:
with DbContext(act.key[0]) :
inputs_by_ex_name = dict()
Expand Down
8 changes: 5 additions & 3 deletions lca_algebraic/lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,9 @@ def act_to_symbol(sub_act):

return act_symbols[(db_name, code)]

def rec_func(act: ActivityExtended, parents=[]):
# Local cache of expressions

def actToExpressionRec(act: ActivityExtended, parents=[]):

res = 0
outputAmount = act.getOutputAmount()
Expand Down Expand Up @@ -618,7 +620,7 @@ def rec_func(act: ActivityExtended, parents=[]):
if sub_act in parents :
raise Exception("Found recursive activities : " + ", ".join(_actName(act) for act in (parents + [sub_act])))

act_expr = rec_func(sub_act, parents)
act_expr = actToExpressionRec(sub_act, parents)

avoidedBurden = 1

Expand All @@ -636,7 +638,7 @@ def rec_func(act: ActivityExtended, parents=[]):

return res

expr = rec_func(act)
expr = actToExpressionRec(act)

if isinstance(expr, float) :
expr = simplify(expr)
Expand Down

0 comments on commit 5911a4d

Please sign in to comment.