Skip to content

Commit

Permalink
New cheat sheet content (#163)
Browse files Browse the repository at this point in the history
* New cheat sheet content

* Split cheat sheet into multiple pages
  • Loading branch information
cmutel authored Sep 15, 2024
1 parent 7cd2d90 commit 2a0a0e9
Show file tree
Hide file tree
Showing 22 changed files with 929 additions and 1,065 deletions.
89 changes: 89 additions & 0 deletions source/content/cheatsheet/databases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
## Databases

**Q:** How do I list all databases?

```python
sorted(bd.databases)
```

**Q:** How do I test if a given database is installed?

```python
'<my database label>' in bd.databases
```

**Q:** How do I instantiate a `Database` object?

```python
my_db = bd.Database('<database_name>')
```

**Q:** How do I copy a `Database`?

```python
copied_database = bd.Database('<database_name>').copy('<new_name>')
```

**Q:** How do I rename a `Database`?

```python
new_database = bd.Database('<database_name>').rename('<new_name>')
```

**Q:** How do I delete a `Database`?

```python
del bd.databases['<database_name>']
```

### Metadata

**Q:** How do I see the `Database` metadata?

```python
bd.Database('<database_name>').metadata
```

**Q:** How do I change the `Database` metadata?

```python
bd.Database('<database_name>').metadata['<some_key>'] = '<some_value>'
```

**Q:** How do I see which other databases this `Database` refers to?

```python
bd.Database('<database_name>').metadata['depends']
```

**Q:** How can I see what kind of modelling paradigm and storage engine a `Database` uses?

This information is given to a limited degree by the database backend:

```python
bd.Database('<database_name>').metadata['backend']
```

There are three backends in a normal Brightway installation:

* `sqlite`: The default backend. Uses SQLite like a graph database.
* `iotable`: Uses the SQLite database for nodes, but stores edges only in datapackages. Limits edges to a single numerical value without uncertainty, but gives better performance for large IO data.
* `multifunctional`: Stores `multifunctional` processes as a custom node type, and automatically allocates following the given database or process preferences when creating datapackages.

### Searching

**Q:** How do I search a `Database`?

```python
bd.Database('<database_name>').search('<my_query_string>')
```

See {py:obj}`bw2data.backends.base.SQLiteBackend.search` for documentation and function options.

### Datapackages

**Q:** How do I get the `bw_processing` datapackage for this `Database`?

```python
bd.Database('<database_name>').datapackage()
```
86 changes: 86 additions & 0 deletions source/content/cheatsheet/ia.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
## Impact Assessment

### Impact Categories (`Method`)

**Q:** How do I list the installed impact categories?

```python
sorted(bd.methods)
```

**Q:** How do I test if a given impact category is installed?

```python
('<impact>', '<category>') in bd.methods
```

**Q:** How can I get a random impact category?

```python
bd.methods.random()
```

**Q:** How do I search for an impact category using list comprehensions?

```python
[
method for method in bd.methods
if 'ilcd 2.0' in method[0].lower()
and 'LT' not in method[2]
]
```

**Q:** How do I see the data in a given impact category?

```python
my_method_object = bd.Method(('<impact>', '<category>'))
list(my_method_object)
```

**Q:** How is the data in impact categories structured?

Iterating over a `Method` object yields tuples.

* The first element in the tuple will be the biosphere `Node`
* The second element will be the characterization factor, either as a number, or as a dictionary which includes uncertainty information
* There could be a third element, which gives the *location* for the characterization factor. This third element is not required.

**Q:** How do I interpret the uncertainty dictionary?

See the [`stats_arrays` documentation](https://stats-arrays.readthedocs.io/en/latest/#mapping-parameter-array-columns-to-uncertainty-distributions).

**Q:** How do I create a new impact category?

Start by defining characterization data following the tuple format defined in `How is the data in impact categories structured?`:

```python
import stats_arrays as sa
my_cf_data = [
(biosphere_node_1, 42),
(biosphere_node_1, 23, 'BR'),
(biosphere_node_2, {
'uncertainty_type': sa.TriangularUncertainty.id,
'amount': 7,
'loc': 7,
'maximum': 21
})
]
```

Then write the characterization factor to the `Method`:

```python
bd.Method(('<impact>', '<category>')).write(my_cf_data)
```

**Q:** How do I see the impact category metadata?

```python
bd.Method(('<impact>', '<category>')).metadata
```

**Q:** How do I change the impact category metadata?

```python
bd.Method(('<impact>', '<category>')).metadata['<some_key>'] = '<some_value>'
```
219 changes: 219 additions & 0 deletions source/content/cheatsheet/importing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
## Importing data

The philosophy and workflow for importing data is explained in the "Overview".

```{admonition} Before starting an import
:class: important
Make sure you have the basic data, such as background data and biosphere flows available. It will probably be needed to create links with your imported data.
```

### Creating Importers

**Q:** How do I start an import from the Brightway excel template?

```python
importer = bi.ExcelImporter('<file_path>')
```

See {py:obj}`bw2io.importers.excel.ExcelImporter` for additional information.

**Q:** How do I start an import from SimaPro CSV?

```python
from pathlib import Path
importer = bi.SimaProBlockCSVImporter(Path('<file_path>'))
```

See {py:obj}`bw2io.importers.simapro_block_csv.SimaProBlockCSVImporter` for additional information.

**Q:** How do I start an import from Ecospold 1?

```python
importer = bi.SingleOutputEcospold1Importer('<directory_path>', '<database_name>')
```

See {py:obj}`bw2io.importers.ecospold1.SingleOutputEcospold1Importer` for additional information.

**Q:** How do I start an import from Ecospold 1?

```python
importer = bi.SingleOutputEcospold2Importer('<directory_path>', '<database_name>')
```

See {py:obj}`bw2io.importers.ecospold2.SingleOutputEcospold2Importer` for additional information.

### Ecoinvent

**Q:** How can I import an ecoinvent release using the online portal?

```python
bi.import_ecoinvent_release(
version='<version_number>',
system_model='<system_model>',
ecoinvent_user='<ecoinvent_user_name>',
ecoinvent_password='<ecoinvent_password>'
)
```

The `import_ecoinvent_release` function will create a namespaced set of impact categories and a separate namespaced biosphere `Database`.

```{warning}
The setup function `bw2setup()` is deprecated and should no longer be used.
```

See {py:obj}`bw2io.ecoinvent.import_ecoinvent_release` for customization options, and the [`ecoinvent_interface` documentation](https://github.com/brightway-lca/ecoinvent_interface/?tab=readme-ov-file#authentication-via-settings-object) for instructions on how to download additional ecoinvent data and save your credentials.

**Q:** How can I import a local copy of an ecoinvent release?

Start with a basic project which has the correct set of elementary flows for your given release - see [Creating initial project data](initial-project-data). You can then use the standard ecospold2 importer:

```python
importer = bi.SingleOutputEcospold2Importer(
dirpath='<ecoinvent_database_zip_file>',
dbname='<database_name>'
)
importer.apply_strategies()
importer.write_database()
```

We strongly recommend that you follow the `ecoinvent-<version>-<system model>` database naming pattern.

### Applying Transformations

**Q:** How do I apply the default transformation strategies?

```python
importer.apply_strategies()
```

**Q:** How do I apply a custom transformation strategy?

```python
importer.apply_strategy(<my_function>)
```

**Q:** How do I write a custom transformation strategy?

Transformation functions must take the entire import data as the single input argument, and return the modified entire import data. They should follow this general pattern:

```python
from typing import List

def my_custom_strategy(data: List[dict]) -> List[dict]:
"""Add very import information to each dataset"""
for dataset in data:
dataset["this is a dataset"] = True
return data
```

If you need to provide additional information to a transformation strategy, you can [curry](https://en.wikipedia.org/wiki/Currying) the transformation function:

```python
from typing import List
from functools import partial

def my_custom_strategy(data: List[dict], favorite_color: str) -> List[dict]:
"""Add very import color information"""
for dataset in data:
dataset["my favorite color"] = favorite_color
return data

importer.apply_strategy(partial(my_custom_strategy, favorite_color="blue"))
```

**Q:** How do I change the default strategies?

`importer.strategies` is a list, and the normal Python list methods can be used to modify the list before `.apply_strategies()` is called. You can also replace `importer.strategies` completely:

```python
importer.strategies = [<some functions>]
```

**Q:** How can I apply static transformations from [randonneur_data](https://github.com/brightway-lca/randonneur_data)?

```python
importer.randonneur('<transformation_label')
```

See {py:obj}`bw2io.importers.base_lci.LCIImporter.randonneur` for customization of this function's behavior.

### Creating Links

**Q:** How do I realize internal links among the nodes in my imported data?

```python
importer.match_database()
```

See {py:obj}`bw2io.importers.base_lci.LCIImporter.match_database` for customization of this function's behavior.

**Q:** How do I realize external links from my imported data to other `Databases`?

```python
importer.match_database('<database_label>')
```

See {py:obj}`bw2io.importers.base_lci.LCIImporter.match_database` for customization of this function's behavior.

### Linking Status

**Q:** How can I quickly check if all edges are linked?

```python
importer.all_linked
```

**Q:** How can I see how many edges have been linked so far?

```python
importer.statistics()
```

**Q:** How can I export an Excel file of the unlinked edges?

```python
filepath = importer.write_excel(only_unlinked=True)
```

**Q:** How can I export a Randonneur template for matching unlinked edges?

```python
filepath = importer.create_randonneur_excel_template_for_unlinked()
```

See {py:obj}`bw2io.importers.base_lci.LCIImporter.create_randonneur_excel_template_for_unlinked` for customization of this function's behavior.

### Handling Unlinked Edges

**Q:** How can I iterate over all unique unlinked edges?

```python
for edge in importer.unlinked:
do_something(edge)
```

**Q:** How can I delete unlinked edges?

```python
importer.drop_unlinked(i_am_reckless=True)
```

**Q:** How can I add biosphere nodes from my imported data?

You can leave such biosphere nodes in the `Database` you are going to create, but you can separate them out into a new `Database`:

```python
importer.create_new_biosphere('<new_database_name>')
```

You can also add them to an existing database:

```python
importer.add_unlinked_flows_to_biosphere_database('<existing_database_name>')
```

**Q:** How can I create placeholder process or product nodes referenced in my imported data but without any producers?

```python
importer.add_unlinked_activities()
```
Loading

0 comments on commit 2a0a0e9

Please sign in to comment.