e-mission / nrel-openpath-deploy-configs

Configurations for current OpenPATH deployments, published for transparency
BSD 3-Clause "New" or "Revised" License
2 stars 9 forks source link

Add dynamic label options for dev-emulator configs #30

Closed JGreenlee closed 10 months ago

JGreenlee commented 11 months ago

As discussed in https://github.com/e-mission/e-mission-docs/issues/945, we will be supporting the dynamic configuration of users' choices for MODE, PURPOSE, and/or REPLACED_MODE labels.

This is being implemented in https://github.com/e-mission/e-mission-phone/pull/1014.

To test this, I've switched both dev-emulator-study and dev-emulator-program to use this mechanism, including all the old modes and purposes, plus a new mode "Moped".

shankari commented 10 months ago

@JGreenlee now that https://github.com/e-mission/e-mission-phone/pull/1014 is merged, maybe we should merge this too?

JGreenlee commented 10 months ago

Does the URL need to be changed to:

"label_options": "https://raw.githubusercontent.com/e-mission/nrel-openpath-deploy-configs/main/label_options/example-program-label-options.json",

instead of my fork and branch?

Also, for the purpose of testing and making sure this works, I added a new mode "Moped". But we don't have co2 and/or MET data for it. What should we do about that?

shankari commented 10 months ago

Also, for the purpose of testing and making sure this works, I added a new mode "Moped". But we don't have co2 and/or MET data for it. What should we do about that?

MET is 0 (car-like) for CO2, we have to look it up. eyeballing, average is ~ 100 mpg. At least in the US, typically one passenger. If you look at the blame for the default trip_confirm_options, you can find the issue with the calculations for existing modes

JGreenlee commented 10 months ago

The commit history for trip_confirm_options.json.sample does not reveal much.

I did eventually find this issue (https://github.com/e-mission/e-mission-docs/issues/696#issuecomment-1017001601) which explained a few things, but not the information I was looking for.

Is there a systematic breakdown somewhere explaining where each of these co2PerMeter values come from?

JGreenlee commented 10 months ago

for CO2, we have to look it up. eyeballing, average is ~ 100 mpg

Also eyeballing, I am seeing similar estimates (they are a lot more fuel efficient than I thought!)

However, I haven't been able to find any specific stats for overall efficiency. I think it's tough to say because nobody wants to definitively say what is and isn't a moped, vs. a scooter vs. a motorcycle. I am guessing that they aren't regulated differently so there aren't distinct classes between different types of two-wheelers.

So I am still not sure what to put down.

shankari commented 10 months ago

I found this in the history...

commit 2cbcb8198e45ef6d426a3a3003d92fe2b065d262
Author: Shankari <shankari@eecs.berkeley.edu>
Date:   Fri Aug 12 15:27:10 2022 -0700

    Add support for e-car to give people a better sense of their carbon footprint

    Also duplicated at: https://github.com/e-mission/e-mission-docs/issues/688#issuecomment-1212776169

    Since we are NREL, we want to also support the difference in energy intensity between car vs. e-car.

    Estimates for the average e-car:
    - From Andrew Kotz: 3 - 4 miles/kWH = 333 - 250 WH/mile
    - From Andy's Leaf: 244 WH/mile
    - From my Leaf (4.2 miles/kWH average, probably because I am in the SF Bay Area): 238 WH/mile

...
JGreenlee commented 10 months ago

I see it now (I missed that commit because it isn't in master - I was expecting it to be an old commit)

But that's only for e-car. How does that help us with MPG -> CO2 for mopeds? And what MPG should we actually use?

shankari commented 10 months ago

you would use a similar method, (also outlined in https://github.com/e-mission/e-mission-docs/issues/688) for the other modes, for mopeds. I think that 100mpg is a fine eyeball figure. Note that it is not clear that anybody wants to use "moped" just yet - this is just an example showing the method to add new modes. So you can pick whatever you had in mind when you created that placeholder mode.

Presumably if a partner wants to add a new mode, they have a clear picture of what they have in mind - e.g. scooter or motorcycle or ... If they don't, this will help focus their mind a bit 😄

JGreenlee commented 10 months ago

According to https://www.epa.gov/greenvehicles/greenhouse-gas-emissions-typical-passenger-vehicle, a gallon of gas creates about 8887 grams of CO2 when burned, so 8.887 kg CO2 per gallon.

100 miles per gallon -> 160934 meters per gallon. 8.887 kg CO2 per gallon / 160934 meters per gallon = 0.00005522 kg CO2 per meter.

That can't be right. We're expecting something around 0.05. "Gas car drove alone" is around 0.22 and the average moped is roughly 4 times as efficient as the average car (100mpg vs 25mpg).

So we are off by an multiple of 1000. Why?

We are looking for kg CO2 per meter, right? Not per kilometer?

JGreenlee commented 10 months ago

What we currently have for drove_alone, as an example, is 0.22031.

Does a gas car really emit 0.22 kilograms of CO2 per meter? That would mean 1 kilogram every 5 meters. It would only take 44 meters to account for what the EPA says should come from an entire gallon of gas.

By that logic, we're basing our estimates on a car has 0.0273403 mpg.

JGreenlee commented 10 months ago

Either our units are mislabeled, or there is something wrong with using 8887 grams per gallon as a measure of the CO2 emitted by a gas vehicle.

shankari commented 10 months ago

My calculations are in https://github.com/e-mission/e-mission-docs/issues/688 if you want to compare I don't remember the details any more, which is why it is good to write them down

JGreenlee commented 10 months ago

But where are the calculations for gas vehicles? I only see electric vehicles.

I specifically want to know where the numbers came from that were added in this commit: https://github.com/e-mission/e-mission-phone/commit/1a1dcbdc6277a56ece8b44a37f9b5eb21ce4bd29

shankari commented 10 months ago

https://github.com/e-mission/e-mission-docs/issues/139 It's linked from https://github.com/e-mission/e-mission-docs/issues/688 but a little further down.

JGreenlee commented 10 months ago

Although I still have not seen where the co2PerMeter values originate from, I can see that the server code does an mpg -> CO2 conversion in this function:

https://github.com/e-mission/e-mission-server/blob/9e5717e75d03c817f78c9699fd143416bfe67215/emission/core/wrapper/user.py#L81-L100

It uses 8.91 kg per gallon (pretty consistent with the 8.887 kg used above). And it deals in kilograms of CO2 per kilometer, not per meter.

Based on the defaultMpg defined on the server (which is ~32.04), the car_footprint in that function ends up being ~0.17278 kg CO2 per kilometer

JGreenlee commented 10 months ago

So although I don't know where the original co2PerMeter values come from, I am fairly certain that something is deeply wrong with them. They are either off by several magnitudes, or they are mislabeled as the wrong unit.

shankari commented 10 months ago

I bet it is the wrong unit. The variable name should probably be co2PerKm. For the EV calculations, do we end up with km or meters? Because I remember checking the the EV calculations made sense wrt gas car so you should be able to follow along that as well.

And what are we multiplying with in the footprint calculations?

If you look through https://github.com/e-mission/e-mission-docs/issues/139 the original calculations are in a HTML file linked in there.

JGreenlee commented 10 months ago

And what are we multiplying with in the footprint calculations?

Working backwards to trace this through the code, I can see that the co2PerMeter fields are read in while mapping modeOptions to modeCO2PerMeter.

https://github.com/e-mission/e-mission-phone/blob/ca2da7184080218f2a9b000d9280eacba636fc45/www/js/metrics-mappings.js#L357-L372:

    this.populateCustomFootprints = function() {
        let modeOptions = this.inputParams["MODE"].options;
        let modeCO2PerMeter = modeOptions.map((opt) => {
            if (opt.range_limit_km) {
                if (this.range_limited_motorized) {
                    Logger.displayError("Found two range limited motorized options", {
                        first: this.range_limited_motorized, second: opt});
                }
                this.range_limited_motorized = opt;
                console.log("Found range limited motorized mode", this.range_limited_motorized);
            }
            if (angular.isDefined(opt.co2PerMeter)) {
                return [opt.value, opt.co2PerMeter];
            } else {
                return undefined;
            }
        }).filter((modeCO2) => angular.isDefined(modeCO2));;
        this.customPerMeterFootprint = Object.fromEntries(modeCO2PerMeter);
        console.log("After populating, custom perMeterFootprint", this.customPerMeterFootprint);
    }

This mapping is stored in customPerMeterFootprint, which is returned by cdh.getCustomFootprint(), which is returned by fh.getFootprint() (if useCustom=true, which it does).

fh.getFootprint() is invoked on line 32 of https://github.com/e-mission/e-mission-phone/blob/b6da3bbaa1b14f19937c079bef975dc6752c427e/www/js/metrics-factory.js#L34-L42:

fh.getFootprintForMetrics = function(userMetrics, defaultIfMissing=0) {
    var footprint = fh.getFootprint();
    var result = 0;
    for (var i in userMetrics) {
      var mode = userMetrics[i].key;
      if (mode == 'ON_FOOT') {
        mode = 'WALKING';
      }
      if (mode in footprint) {
        result += footprint[mode] * mtokm(userMetrics[i].values);
      }
      else if (mode == 'IN_VEHICLE') {
        result += ((footprint['CAR'] + footprint['BUS'] + footprint["LIGHT_RAIL"] + footprint['TRAIN'] + footprint['TRAM'] + footprint['SUBWAY']) / 6) * mtokm(userMetrics[i].values);
      }
      else {
        console.warn('WARNING FootprintHelper.getFootprintFromMetrics() was requested for an unknown mode: ' + mode + " metrics JSON: " + JSON.stringify(userMetrics));
        result += defaultIfMissing * mtokm(userMetrics[i].values);
      }
    }
    return result;
  }

and on line 42, the values that came from co2PerMeter are finally multiplied, by values in km ❗❗

So it definitely should be called co2PerKm (maybe ideally, kgCo2PerKm)

JGreenlee commented 10 months ago

For the EV calculations, do we end up with km or meters? Because I remember checking the the EV calculations made sense wrt gas car so you should be able to follow along that as well.

If these calculations were double-checked against and seemed congruent with figures that were off by a scale 1000, there's probably something off with them too. Double checking https://github.com/e-mission/e-mission-docs/issues/688#issuecomment-1212776169 for math or units errors

JGreenlee commented 10 months ago

Yes, these calculations also result in kg CO2 per km. I double checked everything up to:

e_car_drove_alone: 0.2915 lb/mile e_car_shared_ride: 0.1458 lb/mile

And this seems correct. This is then converted to scientific units.

We want the values in kg/PkmT

0.2915 lb = 1 mile 0.1322 kg = 1.609 km (0.1322 / 1.609) = 0.08216 kg/PkmT (versus 0.00728 for e-bike, so the magnitude seems right)

0.1458 lb = 1 mile 0.0661 kg = 1.609 meters (0.0661 / 1.609) = 0.04108 kg/PkmT (half of drove_alone) so the magnitude seems right

e_car_drove_alone: 0.08216 kg/meter e_car_shared_ride: 0.04108 kg/meter

The only problem is last line, which reports the values as "kg/meter", when they are actually of "kg/km". So the math is good but the units are mislabeled (just as the units were off for gas modes).


This is good news. All the numbers are right - we can simply rename co2PerMeter to co2PerKm or kgCo2PerKm and avoid this confusion in the future.

And for moped, we can try that again and get kg / km this time:

100 miles per gallon = 160.394 km per gallon We'll use 8.91 kg CO2 per gallon this time to be consistent with the server (as opposed to the 8.887 I used before) 8.91 kg CO2 per gallon / 160.394 km per gallon = 0.05555 kg CO2 per km

JGreenlee commented 10 months ago

I'll update this PR with the rename and the new calculations - we'll also need phone changes because we are renaming a property.

I am glad we caught this before we have people start making their own labelOptions JSON files

shankari commented 10 months ago

This is good news. All the numbers are right - we can simply rename co2PerMeter to co2PerKm or kgCo2PerKm and avoid this confusion in the future.

This was probably just me tweaking the calculations and running out of time/priority to change the variable name. I'm glad we figured it all now.

We should really have a single unified source across the phone and the public dashboard, which is the issue that I plan to have @ananta-nrel work on as his first task

JGreenlee commented 10 months ago

As a side note, I find it interesting that because mopeds are so efficient on gas, they actually result in fewer emissions than e-car drove alone (0.05555 vs 0.08216).

I think that illustrates really well how electrification is not the whole solution - mode shift is equally essential.