Skip to content

Instantly share code, notes, and snippets.

View cmutel's full-sized avatar

Chris Mutel cmutel

View GitHub Profile
from flowmapper.transformation_mapping import prepare_transformations
from flowmapper import Flow
from flowmapper.utils import load_standard_transformations
our_transform = [{
"update": [{
"source":
{
"name": "2,4-D",
"context": "Emissions to air/"
{
"source": {
"name": "Zinc",
"context": "water/groundwater, long-term"
},
"target": {
"name": "Zinc, ion",
"context": ["water", "ground-, long-term"]
}
}
import bw2data as bd
db = bd.Database("Transport routes")
# Compatible with legacy - "new_node" is 2.5 only
my_route = bd.new_activity(
name="Transport from CN to EU",
unit="trip",
code="surf-practice"
)
my_route.save()
import csv
import bw2data as bd
bio_db_name = 'ecoinvent-3.10-biosphere'
new_method = ("Something", "Something", "Danger zone")
# Writing
with open(f'{bio_db_name}.csv', 'w', encoding='utf-8') as csvfile:
fieldnames = ['id', 'name', 'categories', 'unit', 'cf']
@cmutel
cmutel / bw3_schema.py
Last active November 1, 2023 21:09
In-progress schema for Brightway 3 basic database
from typing import List, Optional
from enum import IntEnum
from datetime import datetime
from pydantic import BaseModel, Json, HttpUrl, Field
class BrightwayBase(BaseModel):
id: int
name: str
@cmutel
cmutel / fix_minor_version_ecospold.py
Created October 19, 2023 12:41
Fix the `minorRelease` in ecospold files
import os
import sys
from pathlib import Path
from typing import Union
from lxml import etree, objectify
def fix_version(
filepath: Union[str, Path],
import bw2data as bd
import bw2calc as bc
bd.projects.set_current("ghg-scores")
db = bd.Database("eg")
db.register()
a = db.new_node(code="a", name="a")
a.save()

The aim of bw_projects is relatively simple - we want to store data in a SQLite file and in a few subdirectories, and we want to be able to do switch this SQLite file and associated subdirectories whenever we want.

To do this we use peewee as an ORM to SQLite, and its ability to bind ORM tables to a database object after initial import:

    def _create_database(self):
        db = SqliteDatabase(self._filepath)
        for model in self._tables:
            model.bind(db, bind_refs=False, bind_backrefs=False)
 db.connect()
#!/usr/bin/env python
from collections import defaultdict
from pathlib import Path
import bw2data as bd
import bw2io as bi
import functools
import pandas as pd
import requests
import tempfile
# Reset min/max
for ds in bd.Database("swiss consumption 1.0"):
for exc in ds.technosphere():
if exc.get('uncertainty type') == 0 or exc.get('uncertainty_type') == 0:
exc['minimum'] = exc['maximum'] = np.NaN
exc.save()
# Get functional unit
ave_hh = bd.get_node(name='ch hh average consumption disaggregated, years 151617')