Skip to content

Instantly share code, notes, and snippets.

@tngTUDOR
tngTUDOR / copy_missing_flows.py
Created July 9, 2024 10:24
copy missing flows from one biosphere to another
#!/usr/bin/env python
# coding: utf-8
# # Copy missing biosphere3 flows
# From an ecoinvent 3.10 install to a 3.8 to create a temporary migration environment to avoid to re-build complete project from scratch.
#
# In[1]:
@tngTUDOR
tngTUDOR / recursive_df_obj_to_graph.py
Last active April 22, 2024 13:04
bwanalyzer recursive calculation object to graph.
import bw2data as bd
import bw2analyzer as bwa
import networkx as nx
# Make sure to set the right project.
A_PROJECT = "STeamPunk"
bd.projects.set_current(A_PROJECT)
# Where does the LCA fu come from?
FG_DB = "vent_eco"
a = bd.Database(FG_DB).random()
@tngTUDOR
tngTUDOR / MultiLCA_as_df.py
Last active July 4, 2024 06:16
Format MultiLCA results as a pandas DataFrame
# for a MultiLCA with i inventories (functional units) and j impact methods
results = []
for i, res in enumerate(m_lca.results.tolist()):
demand = m_lca.func_units[i]
a = list(demand)[0]
for j, score in enumerate(res):
method = m_lca.methods[j]
results.append(
{
"fu": a["name"],
@tngTUDOR
tngTUDOR / regex_match_sp_name.py
Last active January 25, 2024 18:43
python regex to capture SP names
# A regex to describe a (1)first group, (2) a space, (3)a location enclosed by curly braces
# (4)a bar and a space, (5)a second group, (6)a space, (7) a bar and a space, a (8)last group.
# Spaces are groups by themselves to ease further treatment
pattern = "([^|]+)( )(\\{[\\W\\w\\s]+\\})(\\| )([\\W\\w\\s]+)( )(\\| )([\\W\\w\\s]+)"
@tngTUDOR
tngTUDOR / install_ecoinvent_in_2_lines.py
Last active November 24, 2023 12:38
install_ecoinvent_in_2_lines
import bw2data as bd
import bw2io as bi
from packaging import version
# We need bw2io version 0.9.dev24 at least
assert version.parse(".".join([str(i) for i in bi.__version__])) >= version.parse("0.9.DEV24")
EI_USERNAME = "my_user"
EI_PASSWORD = "somepassword"
# Let's make the credentials be stored somewhere so that we don't need to provide them every time
@tngTUDOR
tngTUDOR / bc23_bw_prio.md
Last active September 18, 2023 14:21
Ideas on brightway development priorities

Ideas on priorities for Brightway development

Workshop of Brightcon 2023 to prioritize the mid- and long-term features to develop within the brightway ecosystem

In no particular order:

  • support semantic web technologies (importing / exporting databases or data-packages)
  • improve the existing means to share date (backup and restore projects is the most suitable today)
  • full back and forth support with openlca
  • full back and forth support with simapro
@tngTUDOR
tngTUDOR / bike_inv.py
Created August 17, 2023 08:00
create a simple bw25 inventory with one emission from biosphere3
#!/usr/bin/env python
# coding: utf-8
import bw2data as bd
import bw2io as bi
import bw2calc as bc
bd.projects.set_current("custom")
# restart the database for quick and easy re-use
@tngTUDOR
tngTUDOR / ttyrec.md
Created November 4, 2022 12:50
recording a terminal session inside linux container

Recording a terminal session with ttyrec + ttygif

ttyrec is easy to install in many platforms

Use it inside the container, to produce the output file in a volume, and then use ttyrec to produce a GIF from the host.

  • ttyrec - It's one apt install ttyrec away (or dnf)
  • ttygif idem
@tngTUDOR
tngTUDOR / add_conda_env_tljh.md
Created October 22, 2022 08:31
Adding a new conda environment to tljh

Adding a new conda/mamba environment to a tljh ( The littlest Jupyter Hub ) that can be recognized by the tljh is done by adding ipykernel to the packages of the new environment.

  • create the environment
  • make sure ipykernel is added
sudo -E conda create -n new_env python=3 numpy pandas ipykernel

In theory, this can also be done in the jupyter lab terminal, as an admin user.