From 890299b4b7ca66d733b65326a5de30e5ccb3d12f Mon Sep 17 00:00:00 2001 From: jpfleischer Date: Mon, 21 Oct 2024 17:50:10 -0400 Subject: [PATCH 1/3] Fix egrid data if name not in properties --- src/emcommon/metrics/footprint/util.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/emcommon/metrics/footprint/util.py b/src/emcommon/metrics/footprint/util.py index 183dc6b..049068d 100644 --- a/src/emcommon/metrics/footprint/util.py +++ b/src/emcommon/metrics/footprint/util.py @@ -101,7 +101,10 @@ async def get_egrid_region(coords: list[float, float], year: int) -> str | None: return None region_feature = get_feature_containing_point(coords, geojson) if region_feature is not None: - return region_feature['properties']['name'] + properties = region_feature['properties'] + region_name = properties.get('name') or properties.get('SUBRGN') + if region_name: + return region_name Log.warn(f"An eGRID region was not found for coords {coords} in year {year}.") return None From 0fd4dd0932b01f54fee061b8d385949a7c3a43c2 Mon Sep 17 00:00:00 2001 From: jpfleischer Date: Wed, 23 Oct 2024 13:20:26 -0400 Subject: [PATCH 2/3] Add 2021 NYC ebike test --- test/metrics/test_footprint_calculations.py | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/metrics/test_footprint_calculations.py b/test/metrics/test_footprint_calculations.py index e36594e..5d53bc3 100644 --- a/test/metrics/test_footprint_calculations.py +++ b/test/metrics/test_footprint_calculations.py @@ -125,6 +125,38 @@ async def test_nyc_bus_footprint(): expectEqual(metadata[key], expected_metadata[key]) +@jest_test +async def test_2021_nyc_ebike_footprint(): + """ + Test kWh and kg CO2e for a 2021 ebike in NYC. + """ + fake_trip = { + "_id": "fake_trip_id", + "distance": 1000, + "start_fmt_time": "2021-01-01", + "start_loc": {"coordinates": [-74.006, 40.7128]}, + "user_input": {"mode_confirm": "ebike"} + } + fake_label_options = { + "MODE": [ + {"value": "ebike", "base_mode": "E_BIKE"} + ] + } + + (footprint, metadata) = await emcmff.calc_footprint_for_trip(fake_trip, fake_label_options) + + expected_footprint = {'kwh': 0.01367, 'kg_co2': 0.005071} + expected_metadata = { + "data_sources": ["egrid2021"], + "is_provisional": False, + "requested_year": 2021, + } + for key in expected_footprint.keys(): + expectAlmostEqual(footprint[key], expected_footprint[key], places=2) + for key in expected_metadata.keys(): + expectEqual(metadata[key], expected_metadata[key]) + + @jest_test async def test_impact_of_ebike_replacing_car(): """ From 5cee2d4676a63a20a2379a21c88892923747e967 Mon Sep 17 00:00:00 2001 From: Jack Greenlee Date: Wed, 23 Oct 2024 17:46:41 -0400 Subject: [PATCH 3/3] use `'key' in dict` rather than dict.get('key') for JS compatibility This fails in JS because `properties` is a JS object, not a dict, and JS objects don't have 'get'. We get around this by using `in`. It's not as elegant but it's foolproof --- src/emcommon/metrics/footprint/util.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/emcommon/metrics/footprint/util.py b/src/emcommon/metrics/footprint/util.py index 049068d..29571e8 100644 --- a/src/emcommon/metrics/footprint/util.py +++ b/src/emcommon/metrics/footprint/util.py @@ -101,10 +101,10 @@ async def get_egrid_region(coords: list[float, float], year: int) -> str | None: return None region_feature = get_feature_containing_point(coords, geojson) if region_feature is not None: - properties = region_feature['properties'] - region_name = properties.get('name') or properties.get('SUBRGN') - if region_name: - return region_name + if 'name' in region_feature['properties']: + return region_feature['properties']['name'] + if 'SUBRGN' in region_feature['properties']: + return region_feature['properties']['SUBRGN'] Log.warn(f"An eGRID region was not found for coords {coords} in year {year}.") return None