Skip to content

Commit

Permalink
Merge pull request #87 from USEPA/release_v1.0.3
Browse files Browse the repository at this point in the history
Release v1.0.3
  • Loading branch information
bl-young authored Dec 2, 2022
2 parents 70049aa + 7ba1da9 commit 50877c8
Show file tree
Hide file tree
Showing 19 changed files with 1,855 additions and 73 deletions.
27 changes: 13 additions & 14 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ on:
- '.gitignore'
pull_request:
branches: [master, develop]
types: [opened, reopened] # excludes syncronize to avoid redundant trigger from commits on PRs
types: [opened, reopened, ready_for_review] # excludes syncronize to avoid redundant trigger from commits on PRs
paths-ignore:
- '**.md'
- '**.bib'
Expand Down Expand Up @@ -49,22 +49,21 @@ jobs:
pip install pytest pytest-cov flake8
# install testing
- name: Install package and dependencies (non-Windows)
if: matrix.os != 'windows-latest'
- name: Install package and dependencies
run: |
pip install .
- name: Install package and dependencies (Windows)
if: matrix.os == 'windows-latest'
run: |
pip install .[ImpactWorld]
# - name: Install package and dependencies (Windows)
# if: matrix.os == 'windows-latest'
# run: |
# pip install .[ImpactWorld]

# MS Access install (for Windows) needed for IW+
- name: Choco install msaccess2010
if: matrix.os == 'windows-latest'
uses: crazy-max/ghaction-chocolatey@v1
with:
args: install msaccess2010-redist
# # MS Access install (for Windows) needed for IW+
# - name: Choco install msaccess2010
# if: matrix.os == 'windows-latest'
# uses: crazy-max/ghaction-chocolatey@v1
# with:
# args: install msaccess2010-redist

# linting & pytest
- name: Lint with flake8
Expand All @@ -76,4 +75,4 @@ jobs:
- name: Test with pytest
run: |
pytest
pytest -m "not generate_methods"
48 changes: 48 additions & 0 deletions .github/workflows/test_methods.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This workflow tests the generation of impact methods

name: Generate and store impact methods

on:
pull_request:
branches: [master, develop]
types: [opened, reopened, ready_for_review] # excludes syncronize to avoid redundant trigger from commits on PRs
workflow_dispatch: # also allow manual trigger, for testing purposes

jobs:
generate_methods:
runs-on: macos-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.10"

- name: Update pip & install testing pkgs
run: |
python -VV
python -m pip install --upgrade pip setuptools wheel
pip install pytest pytest-cov flake8
# install package & dependencies
- name: Install package and dependencies
run: |
pip install .
- name: Generate and store methods
run: |
pytest -m generate_methods
- name: Upload files
if: always()
uses: actions/upload-artifact@v3.1.1
with:
# Artifact name
name: lciafmt_methods
# A file, directory or wildcard patter that describes what to upload
path: |
# ~/Library/Application Support/lciafmt/*
~/Library/Application Support/lciafmt/diff/*
if-no-files-found: warn # 'warn' or 'ignore' are also available, defaults to `warn`
# retention-days: 5 # cannot exceed the retention limit set by the repository, organization, or enterprise.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The LCIA Formatter v1 was peer-reviewed internally at USEPA and externally throu
|ReCiPe 2016 Endpoint|National Institute for Public Health and the Environment (The Netherlands)|[LCIA: the ReCiPe Model](https://www.rivm.nl/en/life-cycle-assessment-lca/recipe)|
|ImpactWorld+ Midpoint*|International Reference Center for Life Cycle of Products, Services and Systems (CIRAIG)|[ImpactWorld+](http://www.impactworldplus.org/en/team.php)|
|ImpactWorld+ Endpoint*|International Reference Center for Life Cycle of Products, Services and Systems (CIRAIG)|[ImpactWorld+](http://www.impactworldplus.org/en/team.php)|
|IPCC GWP|Intergovernmental Panel on Climate Change (IPCC)| |
|FEDEFL Inventory Methods|US Environmental Protection Agency|[FEDEFL Inventory Methods](https://github.com/USEPA/LCIAformatter/wiki/Inventory-Methods)|

\* only works on Windows installations
Expand All @@ -24,9 +25,9 @@ The LCIA Formatter v1 was peer-reviewed internally at USEPA and externally throu
`lciafmt` requires Python 3.7 or greater.

Install a release directly from github using pip. From a command line interface, run:
> pip install git+https://github.com/USEPA/LCIAformatter.git@v1.0.1#egg=lciafmt
> pip install git+https://github.com/USEPA/LCIAformatter.git@v1.0.3#egg=lciafmt
where you can replace 'v1.0.1' with the version you wish to use under [Releases](https://github.com/USEPA/LCIAformatter/releases).
where you can replace 'v1.0.3' with the version you wish to use under [Releases](https://github.com/USEPA/LCIAformatter/releases).

Alternatively, to install from the most current point on the repository:
```
Expand Down
37 changes: 35 additions & 2 deletions lciafmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import lciafmt.jsonld as jsonld
import lciafmt.traci as traci
import lciafmt.recipe as recipe
import lciafmt.ipcc as ipcc
import lciafmt.fedefl_inventory as fedefl_inventory
import lciafmt.util as util
import lciafmt.endpoint as ep
import lciafmt.custom as custom


from enum import Enum
Expand All @@ -32,6 +34,7 @@ class Method(Enum):
RECIPE_2016 = "ReCiPe 2016"
FEDEFL_INV = "FEDEFL Inventory"
ImpactWorld = "ImpactWorld"
IPCC = "IPCC"

def get_metadata(cls):
"""Return the stored metadata."""
Expand Down Expand Up @@ -101,7 +104,10 @@ def get_method(method_id, add_factors_for_missing_contexts=True,
:param url: str, alternate url for method, defaults to url in method config
:return: DataFrame of method in standard format
"""
method_id = util.check_as_class(method_id)
if not method_id:
return custom.get_custom_method(file=file)
else:
method_id = util.check_as_class(method_id)
if method_id == Method.TRACI:
return traci.get(add_factors_for_missing_contexts, file=file, url=None)
if method_id == Method.RECIPE_2016:
Expand All @@ -110,6 +116,8 @@ def get_method(method_id, add_factors_for_missing_contexts=True,
if method_id == Method.ImpactWorld:
import lciafmt.iw as impactworld
return impactworld.get(file=file, url=url)
if method_id == Method.IPCC:
return ipcc.get()
if method_id == Method.FEDEFL_INV:
return fedefl_inventory.get(subset)

Expand Down Expand Up @@ -172,9 +180,9 @@ def get_mapped_method(method_id, indicators=None, methods=None) -> pd.DataFrame:
mapped_method = map_flows(method, system=mapping_system,
case_insensitive=case_insensitive)
mapped_method = util.collapse_indicators(mapped_method)
util.store_method(mapped_method, method_id)
else:
mapped_method = method
util.store_method(mapped_method, method_id)
if indicators is not None:
mapped_method = mapped_method[mapped_method['Indicator'].isin(indicators)]
if len(mapped_method) == 0:
Expand Down Expand Up @@ -216,3 +224,28 @@ def supported_indicators(method_id) -> list:
return list(indicators)
else:
return None


def apply_lcia_method(df, method_id) -> pd.DataFrame:
"""Applies characterization factors to a dataframe.
:param df: dataframe containing 'FlowAmount' and 'FlowUUID' columns
:param method_id: class Method or str, based on id field of
supported_methods
:return: DataFrame with impact method applied as 'Impact' column
"""
if 'FlowUUID' not in df.columns or 'FlowAmount' not in df.columns:
raise TypeError ('DataFrame must containt "FlowUUID" and '
'"FlowAmount" columns')
impact_method = (
get_mapped_method(method_id)
.drop(columns=['Flowable', 'Context', 'Location', 'Location UUID',
'CAS No', 'Method UUID','Indicator UUID','Unit'])
.rename(columns={'Flow UUID':'FlowUUID'}))

impacts = df.merge(impact_method, how = 'inner',
on = ['FlowUUID'])
impacts['Impact'] = (impacts['FlowAmount'] *
impacts['Characterization Factor'])

return impacts
25 changes: 25 additions & 0 deletions lciafmt/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# custom.py (lciafmt)
# !/usr/bin/env python3
# coding=utf-8
"""
Functions to create custom manual methods
"""

import pandas as pd

from lciafmt.df import lciafmt_cols

def get_custom_method(file: str=None, input_df=None):
"""Converts a dataframe or a csv filepath to a dataframe suitable for
lciafmt. If `file` is passed, input_df is ignored.
"""
if file:
input_df = pd.read_csv(file)
if (pd.Series(['Characterization Factor', 'Flowable', 'Context'])
.isin(input_df.columns).all()):
df = input_df.reindex(columns=lciafmt_cols)
df = df.fillna('')
return df
else:
raise Exception

Loading

0 comments on commit 50877c8

Please sign in to comment.