Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing "name" attribute in exchanges created with Activity Browser #1242

Open
1 task done
raphaeljolivet opened this issue Feb 21, 2024 · 2 comments · May be fixed by #1243
Open
1 task done

Missing "name" attribute in exchanges created with Activity Browser #1242

raphaeljolivet opened this issue Feb 21, 2024 · 2 comments · May be fixed by #1243
Labels
bug Issues/PRs related to bugs

Comments

@raphaeljolivet
Copy link

Updating AB

  • Yes, I have updated AB and still experience this issue

What happened?

When creating exchanges with ActivityBrowser, they miss the attribute "name" in ._data.
This field is present for all exchanges of all activities imported with ecoinvent.
Isn't it a mandatory attribute in Brightway2 ?

As a consequence, it breaks when used in lca_algebraic

Relevant errors

No response

Operating system

Linux/Other (please specify above)

Conda environment

No response

@raphaeljolivet raphaeljolivet added the bug Issues/PRs related to bugs label Feb 21, 2024
@marc-vdm
Copy link
Member

marc-vdm commented Feb 21, 2024

Hey, thanks for opening this issue.

You're right, AB does currently not add a name field to an exchange, but they do seem to be automatically written on ecoinvent imports (for internal reference, they are not generated on imports of excel databases either, so this seems to be exclusive to ecoinvent).

The following code handles adding an exchange in AB, though note that we are working on updating the controllers right now (#1241):

def add_exchanges(self, from_keys: Iterator[tuple], to_key: tuple, new_values: dict = {}) -> None:
"""
Add new exchanges.
Optionally add new values also.
Parameters
----------
from_keys: The activities (keys) to create exchanges from
to_key: The activity (key) to create an exchange to
new_values: Values of the exchange, dict (from_keys as keys) with field names and values for the exchange
Returns
-------
"""
activity = bw.get_activity(to_key)
for key in from_keys:
technosphere_db = bc.is_technosphere_db(key[0])
exc = activity.new_exchange(input=key, amount=1)
if technosphere_db is True:
exc['type'] = 'technosphere'
elif technosphere_db is False:
exc['type'] = 'biosphere'
else:
exc['type'] = 'unknown'
# add optional exchange values
if new_vals := new_values.get(key, {}):
for field_name, value in new_vals.items():
if value:
exc[field_name] = value
exc.save()
bw.databases.set_modified(to_key[0])
AB_metadata.update_metadata(to_key)
signals.database_changed.emit(to_key[0])

Isn't it a mandatory attribute in Brightway2 ?

Judging by that we've never done this, it seems it is not mandatory. However, now that I'm thinking about it it would make a lot of sense if we do it, I can imagine this to be required once we implement multifunctionality properly though, and implementing this seems very easy.

I think it'd be good if we discuss this internally and see how we can best work with this.

That said, this doesn't immediately fix your problem, these fields don't exist. Even if/when we'd implement this, the field would only be applied to new exchanges, which would probably not be helpful for you at all, correct?

As multifunctionality is currently barely present in Brightway (and at least in AB, not implemented), I think it's safe to assume that the input product name is the exchange name? It could be an option to if exc.get('name') fails, that you instead read the exc.input.get("reference product")?

We're happy to hear other suggestions or ideas though!

@raphaeljolivet
Copy link
Author

I sure.
I was planning to do something like this.

I just wanted to share this issue with you so we could check whether it is a mandatory attribute or not.

Thanks for this feedback.

@marc-vdm marc-vdm linked a pull request Feb 21, 2024 that will close this issue
9 tasks
@marc-vdm marc-vdm modified the milestone: 2.9.7 Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues/PRs related to bugs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants