Skip to content

Commit

Permalink
Merge pull request #78 from USEPA/develop
Browse files Browse the repository at this point in the history
final linting for JOSS release
  • Loading branch information
bl-young authored Oct 8, 2021
2 parents 6478ee2 + 1bbd7f5 commit 75a2535
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 36 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# LCIA formatter
[![build](https://github.com/USEPA/LCIAformatter/actions/workflows/python-package.yml/badge.svg)](https://github.com/USEPA/LCIAformatter/actions/workflows/python-package.yml)

The LCIA formatter, or `lciafmt`, is a Python tool for standardizing the format and flows of life cycle impact assessment (LCIA) data. The tool acquires LCIA data transparently from its original
source, cleans the data, shapes them into a standard format using the [LCIAmethod format](./format%20specs/LCIAmethod.md), and optionally applies flow mappings as defined in the [Federal LCA Commons Elementary Flow List](https://github.com/USEPA/Federal-LCA-Commons-Elementary-Flow-List). The result can be exported to all formats supported by the
`pandas` package (e.g. Excel, CSV) or the [openLCA JSON-LD format](https://github.com/GreenDelta/olca-schema).
Expand Down
8 changes: 5 additions & 3 deletions examples/fedefl_inventory_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@

method = lciafmt.Method.FEDEFL_INV


def main():

log.debug('Subsets available: ' + ", ".join(map(str, fedefl.subset_list.get_subsets())))

subsets = None

inventory_methods = lciafmt.get_method(method_id='FEDEFL Inventory',subset=subsets)
inventory_methods = lciafmt.get_method(method_id='FEDEFL Inventory', subset=subsets)

store_method(inventory_methods, method)
save_json(method, inventory_methods)



if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions examples/impact_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

method = lciafmt.Method.ImpactWorld


def main():

data = lciafmt.get_method(method)
Expand Down
16 changes: 9 additions & 7 deletions examples/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,31 @@

method = lciafmt.Method.RECIPE_2016

#To obtain summary LCIA endpoint categories (damage assessment) set to True
# To obtain summary LCIA endpoint categories (damage assessment) set to True
apply_summary = False


def main():

data = lciafmt.get_method(method, endpoint = True,
summary = apply_summary)
data = lciafmt.get_method(method, endpoint=True,
summary=apply_summary)

# make flowables case insensitive to handle lack of consistent structure in source file
data['Flowable'] = data['Flowable'].str.lower()

# map the flows to the Fed.LCA commons flows
# set preserve_unmapped=True if you want to keep unmapped
# flows in the resulting data frame
mapping = method.get_metadata()['mapping']
mapped_data = lciafmt.map_flows(data, system=mapping, case_insensitive=True)

mapped_data = collapse_indicators(mapped_data)

# write the result to parquet and JSON-LD
store_method(mapped_data, method)
for m in mapped_data['Method'].unique():
save_json(method, mapped_data, m)


if __name__ == "__main__":
main()
16 changes: 9 additions & 7 deletions examples/traci.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@

method = lciafmt.Method.TRACI


def main():

data = lciafmt.get_method(method)

if mod is not None:
log.info("getting modified CFs")
modified_cfs=get_modification(mod,"TRACI2.1")
data = data.merge(modified_cfs,how='left',on=['Flowable','Context','Indicator'])
data.loc[data['Updated CF'].notnull(),'Characterization Factor']=data['Updated CF']
data = data.drop(columns=['Updated CF','Note'])
data['Method']="TRACI 2.1 ("+mod+" mod)"
modified_cfs = get_modification(mod, "TRACI2.1")
data = data.merge(modified_cfs, how='left', on=['Flowable', 'Context', 'Indicator'])
data.loc[data['Updated CF'].notnull(), 'Characterization Factor'] = data['Updated CF']
data = data.drop(columns=['Updated CF', 'Note'])
data['Method'] = "TRACI 2.1 (" + mod + " mod)"

# map the flows to the Fed.LCA commons flows
# set preserve_unmapped=True if you want to keep unmapped
# flows in the resulting data frame
Expand All @@ -28,5 +29,6 @@ def main():
store_method(mapped_data, method)
save_json(method, mapped_data)


if __name__ == "__main__":
main()
12 changes: 7 additions & 5 deletions examples/valuation.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import lciafmt
from lciafmt.util import store_method


def main():

methods = lciafmt.generate_endpoints('Weidema_valuation', name = 'Weidema Valuation',
matching_fields=['Indicator unit'])
# matching_fields = ['Indicator unit'] is used to avoid listing all
methods = lciafmt.generate_endpoints('Weidema_valuation', name='Weidema Valuation',
matching_fields=['Indicator unit'])
# matching_fields = ['Indicator unit'] is used to avoid listing all
# indicators separately in source data file

store_method(methods, method_id = None)

store_method(methods, method_id=None)


if __name__ == "__main__":
main()
6 changes: 3 additions & 3 deletions lciafmt/df.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ def data_frame(records: list) -> pandas.DataFrame:

def as_list(df: pandas.DataFrame, row=-1) -> list:
"""Converts the given data frame into a list of lists. When the `row`
paremeter is given with a value >= 0, only that row is extracted as
list from the data frame."""
paremeter is given with a value >= 0, only that row is extracted as
list from the data frame.
"""
if df is None:
return []
if row >= 0:
Expand Down Expand Up @@ -72,4 +73,3 @@ def record(records: list,
location_uuid,
factor])
return records

2 changes: 1 addition & 1 deletion lciafmt/iw.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _read(access_file: str) -> pd.DataFrame:
("CF - regionalized - LandTrans - aggregated", "Elementary Flow"),
("CF - regionalized - LandOcc - aggregated", "Elementary Flow"),
("CF - regionalized - EutroMar - aggregated", "Compartment"),
("CF - regionalized - PartMatterForm - aggregated","Compartment"),
("CF - regionalized - PartMatterForm - aggregated", "Compartment"),
("CF - regionalized - AcidFW - aggregated", "Compartment"),
("CF - regionalized - AcidTerr - aggregated", "Compartment"),
("CF - regionalized - EutroFW - aggregated", "Compartment"),
Expand Down
2 changes: 1 addition & 1 deletion lciafmt/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __method(self, row) -> olca.ImpactMethod:
m.name = row[0]
m.version = pkg_version_number
m.impact_categories = []
m.description=description
m.description = description
self.__methods[uid] = m
return m

Expand Down
10 changes: 5 additions & 5 deletions lciafmt/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'sea water': 'water/sea water',
'Sea water': 'water/sea water',
'marine water': 'water/sea water'}
flowables_split = pd.read_csv(datapath+'ReCiPe2016_split.csv')
flowables_split = pd.read_csv(datapath + 'ReCiPe2016_split.csv')


def get(add_factors_for_missing_contexts=True, endpoint=True,
Expand Down Expand Up @@ -77,7 +77,7 @@ def get(add_factors_for_missing_contexts=True, endpoint=True,
df2 = df.merge(endpoint_df, how="inner", on=["Method", "Indicator"])
df2 = df2.append(flowdf, ignore_index=True, sort=False)
# reformat dataframe and apply conversion
df2['Characterization Factor'] = df2['Characterization Factor']*df2['EndpointConversion']
df2['Characterization Factor'] = df2['Characterization Factor'] * df2['EndpointConversion']
df2['Method'] = df2['EndpointMethod']
df2['Indicator'] = df2['EndpointIndicator']
df2['Indicator unit'] = df2['EndpointUnit']
Expand All @@ -97,7 +97,7 @@ def get(add_factors_for_missing_contexts=True, endpoint=True,

length = len(df)
df.drop_duplicates(keep='first', inplace=True)
length = length-len(df)
length = length - len(df)
log.info(f"{length} duplicate entries removed")

if summary:
Expand Down Expand Up @@ -179,11 +179,11 @@ def _read_endpoints(file: str) -> pd.DataFrame:
log.debug("extracted %i endpoint factors", endpoint_factor_count)

log.info("processing endpoint factors")
endpoint.loc[endpoint['EndpointUnit'].str.contains('daly', case=False),'EndpointUnit'] = 'DALY'
endpoint.loc[endpoint['EndpointUnit'].str.contains('daly', case=False), 'EndpointUnit'] = 'DALY'
endpoint.loc[endpoint['EndpointUnit'].str.contains('species', case=False), 'EndpointUnit'] = 'species-year'
endpoint.loc[endpoint['EndpointUnit'].str.contains('USD', case=False), 'EndpointUnit'] = 'USD2013'

endpoint_map = pd.read_csv(datapath+'ReCiPe2016_endpoint_to_midpoint.csv')
endpoint_map = pd.read_csv(datapath + 'ReCiPe2016_endpoint_to_midpoint.csv')
endpoint = endpoint.merge(endpoint_map, how="left", on='EndpointIndicator')

# split into two dataframes
Expand Down
2 changes: 1 addition & 1 deletion lciafmt/traci.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get(add_factors_for_missing_contexts=True, file=None,

length = len(df)
df.drop_duplicates(keep='first', inplace=True)
length = length-len(df)
length = length - len(df)
log.info(f"{length} duplicate entries removed")

return df
Expand Down
7 changes: 4 additions & 3 deletions tests/test_mapped_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lciafmt
from lciafmt.util import log, read_method


class TestInputFiles(unittest.TestCase):

def test_duplicate_flows(self):
Expand All @@ -13,12 +14,12 @@ def test_duplicate_flows(self):
for m in method_list:
method = read_method(lciafmt.Method.get_class(m['id']))
if method is not None:
dup_flowables = method[method[['Method','Indicator','Flow UUID']].duplicated(keep=False)]
dup_flowables = method[method[['Method', 'Indicator', 'Flow UUID']].duplicated(keep=False)]
duplicates = len(dup_flowables)
if duplicates > 0:
log.warning('duplicate factors in method '+ m['name'])
log.warning('duplicate factors in method ' + m['name'])
total_duplicates += duplicates
self.assertTrue(total_duplicates==0,'Duplicate factors in one or more methods')
self.assertTrue(total_duplicates == 0, 'Duplicate factors in one or more methods')


if __name__ == "__main__":
Expand Down

0 comments on commit 75a2535

Please sign in to comment.