From 325a1f0d3bfe476c7010ebab9684175ae27f99cd Mon Sep 17 00:00:00 2001 From: Ben Young Date: Thu, 7 Oct 2021 10:22:29 -0400 Subject: [PATCH 1/2] linting --- examples/fedefl_inventory_methods.py | 8 +++++--- examples/impact_world.py | 1 + examples/recipe.py | 16 +++++++++------- examples/traci.py | 16 +++++++++------- examples/valuation.py | 12 +++++++----- lciafmt/df.py | 6 +++--- lciafmt/iw.py | 2 +- lciafmt/jsonld.py | 2 +- lciafmt/recipe.py | 10 +++++----- lciafmt/traci.py | 2 +- tests/test_mapped_method.py | 7 ++++--- 11 files changed, 46 insertions(+), 36 deletions(-) diff --git a/examples/fedefl_inventory_methods.py b/examples/fedefl_inventory_methods.py index db154b6..5b48989 100644 --- a/examples/fedefl_inventory_methods.py +++ b/examples/fedefl_inventory_methods.py @@ -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() diff --git a/examples/impact_world.py b/examples/impact_world.py index 0c6fce9..c47e26b 100644 --- a/examples/impact_world.py +++ b/examples/impact_world.py @@ -3,6 +3,7 @@ method = lciafmt.Method.ImpactWorld + def main(): data = lciafmt.get_method(method) diff --git a/examples/recipe.py b/examples/recipe.py index d122aa8..ed8e343 100644 --- a/examples/recipe.py +++ b/examples/recipe.py @@ -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() diff --git a/examples/traci.py b/examples/traci.py index dc93f4d..42ad033 100644 --- a/examples/traci.py +++ b/examples/traci.py @@ -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 @@ -28,5 +29,6 @@ def main(): store_method(mapped_data, method) save_json(method, mapped_data) + if __name__ == "__main__": main() diff --git a/examples/valuation.py b/examples/valuation.py index 266fb7a..856499a 100644 --- a/examples/valuation.py +++ b/examples/valuation.py @@ -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() diff --git a/lciafmt/df.py b/lciafmt/df.py index 165c1aa..27bcb15 100644 --- a/lciafmt/df.py +++ b/lciafmt/df.py @@ -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: @@ -72,4 +73,3 @@ def record(records: list, location_uuid, factor]) return records - diff --git a/lciafmt/iw.py b/lciafmt/iw.py index f426798..d15a82b 100644 --- a/lciafmt/iw.py +++ b/lciafmt/iw.py @@ -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"), diff --git a/lciafmt/jsonld.py b/lciafmt/jsonld.py index 8ead46c..21ba9ec 100644 --- a/lciafmt/jsonld.py +++ b/lciafmt/jsonld.py @@ -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 diff --git a/lciafmt/recipe.py b/lciafmt/recipe.py index 38456b3..f76e0ec 100644 --- a/lciafmt/recipe.py +++ b/lciafmt/recipe.py @@ -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, @@ -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'] @@ -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: @@ -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 diff --git a/lciafmt/traci.py b/lciafmt/traci.py index 314bded..36b4af4 100644 --- a/lciafmt/traci.py +++ b/lciafmt/traci.py @@ -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 diff --git a/tests/test_mapped_method.py b/tests/test_mapped_method.py index 589b4a3..375f6a8 100644 --- a/tests/test_mapped_method.py +++ b/tests/test_mapped_method.py @@ -3,6 +3,7 @@ import lciafmt from lciafmt.util import log, read_method + class TestInputFiles(unittest.TestCase): def test_duplicate_flows(self): @@ -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__": From 1bbd7f5f4ed85c57e6c74b70640239095fa781d3 Mon Sep 17 00:00:00 2001 From: Ben Young <44471635+bl-young@users.noreply.github.com> Date: Thu, 7 Oct 2021 10:57:57 -0400 Subject: [PATCH 2/2] add status badge to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c2f3d5d..92f06a5 100644 --- a/README.md +++ b/README.md @@ -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).