Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic label options for dev-emulator configs #30

Merged
merged 5 commits into from
Aug 30, 2023

Conversation

JGreenlee
Copy link
Collaborator

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

This is being implemented in e-mission/e-mission-phone#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".

These include the mode "moped" and will be used to test the dynamically configurable label options.
@shankari
Copy link
Contributor

@JGreenlee now that e-mission/e-mission-phone#1014 is merged, maybe we should merge this too?

@JGreenlee JGreenlee marked this pull request as ready for review August 26, 2023 14:30
@JGreenlee
Copy link
Collaborator Author

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
Copy link
Contributor

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
Copy link
Collaborator Author

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

I did eventually find this issue (e-mission/e-mission-docs#696 (comment)) 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
Copy link
Collaborator Author

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
Copy link
Contributor

I found this in the history...

commit 2cbcb8198e45ef6d426a3a3003d92fe2b065d262
Author: Shankari <[email protected]>
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
Copy link
Collaborator Author

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
Copy link
Contributor

you would use a similar method, (also outlined in e-mission/e-mission-docs#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
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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
Copy link
Collaborator Author

JGreenlee commented Aug 28, 2023

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
Copy link
Contributor

shankari commented Aug 28, 2023

My calculations are in e-mission/e-mission-docs#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
Copy link
Collaborator Author

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: e-mission/e-mission-phone@1a1dcbd

@shankari
Copy link
Contributor

e-mission/e-mission-docs#139 It's linked from e-mission/e-mission-docs#688 but a little further down.

@JGreenlee
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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
Copy link
Contributor

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 e-mission/e-mission-docs#139 the original calculations are in a HTML file linked in there.

@JGreenlee
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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 e-mission/e-mission-docs#688 (comment) for math or units errors

@JGreenlee
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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
Copy link
Contributor

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
Copy link
Collaborator Author

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.

An investigation in e-mission#30 revealed that this field was mislabeled - it is actually kg per kilometer, not per meter.
Fortunately, all the numbers are correct, so we can just give it a new name that is accurate and descriptive.
Used 100 mpg as an estimated average for mopeds, which yields 0.05555 kg CO2 / km:
e-mission#30
JGreenlee added a commit to JGreenlee/e-mission-phone that referenced this pull request Aug 29, 2023
The investigation in e-mission/nrel-openpath-deploy-configs#30 revealed that this field was mislabeled - it is actually kg per kilometer, not per meter.
Fortunately, all the numbers are correct, so we can just give it a new name that is accurate and descriptive.
@shankari shankari merged commit 2386a99 into e-mission:main Aug 30, 2023
JGreenlee added a commit to JGreenlee/nrel-openpath-deploy-configs that referenced this pull request Aug 30, 2023
e-mission#30 was merged, but there are a few things missing to get dynamic label options working fully as expected:

- change key -> value -- the phone expects each option to have a field by the name of 'value'. The old trip_confirm_options used 'value'. I think I was using 'key' in the new format because it is more concise, but we still need to support both this format and the trip_confirm_options format, and it is not worth going back and changing all the places in the phone code where 'value' is used.

-add baseMode for each of the MODE options (to work with e-mission/e-mission-phone#1023)

-add missing modes: "bikeshare" and "not_a_trip". Evidently, these were left off in the first PR. I think it may have been based on an older version so I checked all the options against the newest trip_confirm_options.

-capitalize English text for 'E-bike' and make it appear before 'bike' - this is what the latest trip_confirm_options has
JGreenlee added a commit to JGreenlee/e-mission-phone that referenced this pull request Aug 31, 2023
Since MOPED was included in e-mission/nrel-openpath-deploy-configs#30, the config will be looking for an icon + color for MOPED (at least on dev-emulator configs for now). The MDI library actually has an icon for 'moped', and we'll use green since a moped is a) a derivative of a bicycle and b) fairly emission-friendly

Note that this doesn't make MOPED show up on all configurations (or even the default configurations) - only when the labelOptions points to MOPED as a baseMode for one of its mode options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants