Closed juliana-steinbach closed 3 months ago
Hi @juliana-steinbach,
yes, starting premise v.2.0.0
, the new method is .update()
. To run external scenarios: ndb.update("external")
. Also, external scenarios need to be provided in the scenario lists now, like so:
import brightway2 as bw
from premise import NewDatabase
from datapackage import Package
fp = r"https://raw.githubusercontent.com/premise-community-scenarios/energy-perspective-2050-switzerland/main/datapackage.json"
ep2050 = Package(fp)
bw.projects.set_current("your_bw_project")
ndb = NewDatabase(
scenarios = [
{"model":"image", "pathway":"SSP2-Base", "year":2050, "external scenarios": [{"scenario": "Business As Usual", "data": ep2050}]},
{"model":"image", "pathway":"SSP2-RCP26", "year":2030, "external scenarios": [{"scenario": "ZERO Basis", "data": ep2050}]},
],
source_db="ecoinvent 3.8 cutoff",
source_version="3.8",
key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
)
ndb.update("external") # or ndb.update() for all sectors plus external
Also, you should probably update to 2.1.1.dev9
.
Hi Romain, thank you for the reply, I updated it to the latest version and modified the code to reflect the changes implemented, but I am now facing the following error when ndb.updating:
fp = r"https://raw.githubusercontent.com/premise-community-scenarios/energy-perspective-2050-switzerland/main/datapackage.json" ep2050 = Package(fp)
scenarios = [
{"model":"image", "pathway":"SSP2-Base", "year":2050, "external scenarios": [{"scenario": "Business As Usual", "data": ep2050}]},
{"model":"image", "pathway":"SSP2-RCP26", "year":2030, "external scenarios": [{"scenario": "ZERO Basis", "data": ep2050}]},
]
ndb = NewDatabase(
scenarios = scenarios,
source_db="ei_3.9.1",
source_version="3.9.1",
key='*****',
)
ndb.update()
InvalidURL Traceback (most recent call last) File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\premise\inventory_imports.py:988, in AdditionalInventory.download_file(self, url, local_path) 987 try: --> 988 response = requests.get(url) 989 response.raise_for_status()
File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\requests\api.py:73, in get(url, params, kwargs)
63 r"""Sends a GET request.
64
65 :param url: URL for the new :class:Request
object.
(...)
70 :rtype: requests.Response
71 """
---> 73 return request("get", url, params=params, kwargs)
File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\requests\api.py:59, in request(method, url, kwargs) 58 with sessions.Session() as session: ---> 59 return session.request(method=method, url=url, kwargs)
File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\requests\sessions.py:575, in Session.request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json) 563 req = Request( 564 method=method.upper(), 565 url=url, (...) 573 hooks=hooks, 574 ) --> 575 prep = self.prepare_request(req) 577 proxies = proxies or {}
File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\requests\sessions.py:486, in Session.prepare_request(self, request) 485 p = PreparedRequest() --> 486 p.prepare( 487 method=request.method.upper(), 488 url=request.url, 489 files=request.files, 490 data=request.data, 491 json=request.json, 492 headers=merge_setting( 493 request.headers, self.headers, dict_class=CaseInsensitiveDict 494 ), 495 params=merge_setting(request.params, self.params), 496 auth=merge_setting(auth, self.auth), 497 cookies=merged_cookies, 498 hooks=merge_hooks(request.hooks, self.hooks), 499 ) 500 return p
File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\requests\models.py:368, in PreparedRequest.prepare(self, method, url, headers, files, data, params, auth, cookies, hooks, json) 367 self.prepare_method(method) --> 368 self.prepare_url(url, params) 369 self.prepare_headers(headers)
File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\requests\models.py:445, in PreparedRequest.prepare_url(self, url, params) 444 if not host: --> 445 raise InvalidURL(f"Invalid URL {url!r}: No host supplied") 447 # In general, we want to try IDNA encoding the hostname if the string contains 448 # non-ASCII characters. This allows users to automatically get the correct IDNA 449 # behaviour. For strings containing only ASCII characters, we need to also verify 450 # it doesn't start with a wildcard (*), before allowing the unencoded hostname.
InvalidURL: Invalid URL 'https:\raw.githubusercontent.com\premise-community-scenarios\energy-perspective-2050-switzerland\main\inventories\lci-EP2050.csv': No host supplied
The above exception was the direct cause of the following exception:
ConnectionError Traceback (most recent call last) Cell In[7], line 1 ----> 1 ndb.update()
File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\premise\new_database.py:963, in NewDatabase.update(self, sectors) 961 update_func = sector_update_methods[sector]["func"] 962 fixed_args = sector_update_methods[sector]["args"] --> 963 scenario = update_func(scenario, *fixed_args) 965 if "applied functions" not in scenario: 966 scenario["applied functions"] = []
File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\premise\external.py:75, in _update_external_scenarios(scenario, version, system_model) 73 if "inventories" in [r.name for r in data_package.resources]: 74 if data_package.get_resource("inventories"): ---> 75 additional = AdditionalInventory( 76 database=scenario["database"], 77 version_in=data_package.descriptor["ecoinvent"]["version"], 78 version_out=version, 79 path=data_package.get_resource("inventories").source, 80 system_model=system_model, 81 ) 82 inventories.extend(additional.merge_inventory()) 84 resource = data_package.get_resource("config")
File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\premise\inventory_imports.py:984, in AdditionalInventory.init(self, database, version_in, version_out, path, system_model) 983 def init(self, database, version_in, version_out, path, system_model): --> 984 super().init(database, version_in, version_out, path, system_model)
File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\premise\inventory_imports.py:378, in BaseInventoryImport.init(self, database, version_in, version_out, path, system_model, keep_uncertainty_data) 373 raise FileNotFoundError( 374 f"The inventory file {path} could not be found." 375 ) 377 self.path = Path(path) if isinstance(path, str) else path --> 378 self.import_db = self.load_inventory() 380 # register migration maps 381 # as imported inventories link 382 # to different ecoinvent versions 383 ei_versions = ["35", "36", "37", "38", "39", "310"]
File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\premise\inventory_imports.py:1009, in AdditionalInventory.load_inventory(self) 1007 if ":/" in path_str and "://" not in path_str: 1008 path_str = path_str.replace(":/", "://") -> 1009 self.download_file(path_str, TEMP_CSV_FILE) 1010 file_path = TEMP_CSV_FILE 1011 else:
File ~\AppData\Local\anaconda3\envs\prem\Lib\site-packages\premise\inventory_imports.py:1001, in AdditionalInventory.download_file(self, url, local_path) 999 writer.writerow(line.decode("utf-8").split(",")) 1000 except requests.RequestException as e: -> 1001 raise ConnectionError(f"Error downloading the file: {e}") from e
ConnectionError: Error downloading the file: Invalid URL 'https:\raw.githubusercontent.com\premise-community-scenarios\energy-perspective-2050-switzerland\main\inventories\lci-EP2050.csv': No host supplied
Hi @juliana-steinbach, I don't know. Maybe you have some issues with your Internet connection. The online resource is accessible. Alternatively, you can download the data package and point to the datapackage.json
file.
Yeah, that is odd. I downloaded the data package and it worked, only had a small error but I guess it is because I am using Ecoinvent 3.9.1:
ValueError: Dataset treatment of municipal solid waste, municipal incineration and electricity, for reuse in municipal waste incineration only is not found in the original database.
Yes. That's ei 3.10. You can modify the inventories and config files in the data package to make it ei 3.9-compliant.
Hello, I am using the newest version of Premise to extract the scenarios from Switzerland but I am getting an error when updating the external scenario:
fp = r"https://raw.githubusercontent.com/premise-community-scenarios/energy-perspective-2050-switzerland/main/datapackage.json" ep2050 = Package(fp) bw.projects.set_current("BE_CH")
ndb = NewDatabase( scenarios = [ {"model":"image", "pathway":"SSP2-Base", "year":2050,} ], key= "**", source_type="ecospold", # <--- this is NEW do not type ecospold02! source_file_path=r"C:\Users\jsteinbach\OneDrive - mines-paristech.fr\Git HySPI\ecoinvent 3.9.1_cutoff_ecoSpold02\datasets", # <-- this is NEW source_version="3.9.1", external_scenarios=[ ep2050, # <-- list datapackages here ] ) ndb.update_external_scenario()
premise_gwp
provides characterization factors for such flows.+------------------------------------------------------------------+ +--------------------------------+----------------------------------+ | Utils functions | Description | +--------------------------------+----------------------------------+ | clear_cache() | Clears the cache folder. Useful | | | when updating
premise
or | | | encountering issues with | | | inventories. | +--------------------------------+----------------------------------+ | get_regions_definition(model) | Retrieves the list of countries | | | for each region of the model. | +--------------------------------+----------------------------------+ | ndb.NewDatabase(...) | Generates a summary of the most | | ndb.generate_scenario_report() | important scenarios' variables. | +--------------------------------+----------------------------------+ Keep uncertainty data? NewDatabase(..., keep_uncertainty_data=True)Disable multiprocessing? NewDatabase(..., use_multiprocessing=False)
Hide these messages? NewDatabase(..., quiet=True) External scenarios should now be given as part of the scenarios list. E.g., {'external scenarios': ['scenario': 'A', 'data': datapackage]}
Fetching IAM data Done!
AttributeError Traceback (most recent call last) Cell In[14], line 13 1 ndb = NewDatabase( 2 scenarios = [ 3 {"model":"image", "pathway":"SSP2-Base", "year":2050,} (...) 11 ] 12 ) ---> 13 ndb.update_external_scenario()
AttributeError: 'NewDatabase' object has no attribute 'update_external_scenario'