brightway-lca / bw_hestia_bridge Goto Github PK
View Code? Open in Web Editor NEWConsume the HESTIA API in Brightway
Home Page: https://docs.brightway.dev/projects/hestiabridge/
License: MIT License
Consume the HESTIA API in Brightway
Home Page: https://docs.brightway.dev/projects/hestiabridge/
License: MIT License
The biosphere flows need to be mapped to ecoinvent as well.
_hestia_api/
__init__.py
query.py
search_hestia(element: str)
get_hestia_node(node_id: str)
credentials.py
login_to_hestia(email: str, pwd: str)
set_token(token: str)
utils/
__init__.py
version.py
appdirs.py
appdirs (MIT) is to get folders to store the token and cache requests
I'd propose to load everything from _hestia_api
into the main __init__.py
.
Currently, linking strategy for "znex7uqxsfqn" fails with
Applying strategy: link_iterable_by_fields
Couldn't apply strategy link_iterable_by_fields:
Object in source database can't be uniquely linked to target database.
Problematic dataset is:
{'cycle_id': 'znex7uqxsfqn',
'filename': '(missing)',
'term_id': 'excretaBeefCattleExceptFeedlotFedKgN',
'transformation_id': '3'}
Possible targets include (at least one not shown):
[{'cycle_id': 'znex7uqxsfqn',
'filename': '(missing)',
'term_id': 'excretaBeefCattleExceptFeedlotFedKgN',
'transformation_id': '3'},
{'cycle_id': 'znex7uqxsfqn',
'filename': '(missing)',
'term_id': 'excretaBeefCattleExceptFeedlotFedKgN',
'transformation_id': '3'},
{'cycle_id': 'znex7uqxsfqn',
'filename': '(missing)',
'term_id': 'excretaBeefCattleExceptFeedlotFedKgN',
'transformation_id': '3'}]
This cycle: https://www-staging.hestia.earth/cycle/s_iwp86ylxhd?dataState=recalculated#impactDriverChart
Consumes one of the outputs of this cycle: https://www-staging.hestia.earth/cycle/ribhbymfmmt3?dataState=recalculated#impactDriverChart
(though one is 2013, one is 2014, I am not sure if there could be another cycle which is more cleanly linked...)
It would be great, when turning the cycle into a process that Brightway can handle, to be able to find these kinds of links by calling the API.
# all referring to staging-api
node = bhb.get_hestia_node({"@type": "cycle", "@id": "cx_dtpwud1iu"})
node_output = [i for i in node["products"] if i["term"]["@id"] == "bananaFruit"][0]
# TODO: Search the hestia cycles if a cycle has node_output as an input
# Perhaps in this style?:
bhb.search_hestia({"cycle.inputs.term.@id": node_output["term"]["@id"]})
# And probably in other contexts as well, like looking for cycles with a specific product
node_input = [i for i in node["inputs"] if i["term"]["@id"] == "saplings"][0]
# Perhaps in this style?
bhb.search_hestia({"cycle.products.term.@id": node_input["term"]["@id"]})
We're never going to upload and this is the only thing that requires a token so I'll remove the credentials unless someone has a good reason to keep them.
https://pypi.org/project/hestia-api/
So far, we only have a preliminary version for the milestone. There remain some gaps that need to be filled for Hestia flows that don't have an ecoivent equivalent.
is a good starting point.
While
bhb.search_hestia({"products.term.name": "Saplings", "@type": "cycle"})
yields perfect results,
bhb.search_hestia({"products.term.name": "Soybean, seed (whole)", "@type": "impactassessment"})
yields only an emtpy list. But there is actually at least one Impact Assessment who has "Soybean, seed (whole)" as a product name. See:
https://www-staging.hestia.earth/impactassessment/bv2ktx0jjkub?dataState=recalculated
To avoid having to search for the API URL, it will be included in the source and switch between stable and staging will just be done via:
set_config("use_staging", True/False)
save_config() # optional, to remember the config from one run to another
In the Hestia ontology, a Transformation is: "The Transformation of one Product to a new Product in the same Cycle."
This is complicated because transformations can branch (product A goes down one branch, product B down the other), and there can be allocation. Also, the branches can have multiple steps, so in theory one cycle can produce many products, all linked together in an arborescence.
This should be well tested...
When not setting use_staging
to True
the following occurs:
>>> import bw_hestia_bridge as bhb
>>> bhb.get_hestia_node("zcuqxv54gn2x")
Traceback (most recent call last):
File "miniconda3\envs\hestia\lib\site-packages\IPython\core\interactiveshell.py", line 3442, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-16-a0f08a0feebb>", line 1, in <module>
bhb.get_hestia_node("zcuqxv54gn2x")
File "hestia\bw_hestia_bridge\bw_hestia_bridge\hestia_api\querying.py", line 154, in get_hestia_node
node_type = node_type or get_node_type(node_id)
File "hestia\bw_hestia_bridge\bw_hestia_bridge\hestia_api\querying.py", line 169, in get_node_type
return search_hestia({"@id": node_id}, how="exact")[0]["@type"].lower()
IndexError: list index out of range
>>> bhb.get_cycle_graph("zcuqxv54gn2x")
Traceback (most recent call last):
File "miniconda3\envs\hestia\lib\site-packages\IPython\core\interactiveshell.py", line 3442, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-17-cbfabe4f7c90>", line 1, in <module>
bhb.get_cycle_graph("zcuqxv54gn2x")
File "hestia\bw_hestia_bridge\bw_hestia_bridge\hestia_api\cycle_graph.py", line 48, in get_cycle_graph
eid = elt["@id"]
TypeError: string indices must be integers
The node with id "zcuqxv54gn2x" only exists on the staging server. It would be helpful to get somehow the information as a return of the called function that the node is not present via the implicitly or explicitly chosen API.
Note: this is not urgent at all but I think we need to make a decision at some point.
At the moment, having the staging
argument in the HestiaImporter
together with the config is problematic as we have a class argument that permanently overrides the library's state (if you forget to set it, and were on staging, you're back on stable).
In my mind, if you're going to use staging, you can just use set_config("use_staging", True)
at the beginning of your script.
You can also use save_config()
to make this change permanent and not have to set it anymore or use a "use_staging" environment variable. This is documented here.
I do not believe that people are going to switch between stable and staging dynamically within a script.
If you think this is the assumption we should be working with, then probably we need to remove the config altogether.
As we've mostly been working on staging and this might be the case for quite some time, the config avoid having to provide a staging=True
argument to all functions, which I personally appreciate, together with the fact that you only need one command to switch your whole script from one to the other, but maybe it's not what is actually needed, especially in the long run.
I just think we need to decide once and for all what to implement because the current mixed situation is not satisfactory.
Enable get_hestia_node
to work with both a dict
(returned from the search), or an id and a type argument.
Would be good to set up tests for the various functions.
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
This repository currently has no open or pending branches.
.github/workflows/python-package-deploy.yml
actions/setup-python v5
.github/workflows/python-test.yml
actions/checkout v4
actions/setup-python v5
pyproject.toml
setuptools >=61.0
requirements.txt
bw2data >= 4.0.dev29
bw2io >= 0.9.DEV23
People in the Hackathon added the EcoInvent ids in the Hestia database so we should be able to use this directly to map to EcoInvent.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.