Skip to content

Commit

Permalink
Merge pull request #13 from NREL/ndr/phev-multiple-target
Browse files Browse the repository at this point in the history
Multi-energy target for PHEV
  • Loading branch information
nreinicke authored Nov 17, 2023
2 parents 7c3e86c + 084031b commit 765d836
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
pip install ".[dev]"
- name: Run mypy
run: mypy .
run: mypy .

- name: Run black
run: |
Expand Down
7 changes: 0 additions & 7 deletions mypy.ini

This file was deleted.

2 changes: 2 additions & 0 deletions nrel/routee/powertrain/core/powertrain_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class PowertrainType(Enum):
ICE = 1
HEV = 2
BEV = 3
PHEV_EV_MODE = 4
PHEV_HEV_MODE = 5

@classmethod
def from_string(cls, s: Optional[str]) -> PowertrainType:
Expand Down
2 changes: 2 additions & 0 deletions nrel/routee/powertrain/core/real_world_adjustments.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
PowertrainType.ICE: 1.166,
PowertrainType.HEV: 1.1252,
PowertrainType.BEV: 1.3958,
PowertrainType.PHEV_EV_MODE: 1.3958,
PowertrainType.PHEV_HEV_MODE: 1.1252,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
"2016_AUDI_A3_4cyl_2WD": "https://app.box.com/shared/static/msdkke3xtdpknmu6a26j08ol8vxiw0hu.json",
"2016_BMW_328d_4cyl_2WD": "https://app.box.com/shared/static/1710cj655y0t5rtclh1ogo0eqt8subr7.json",
"2016_BMW_i3_REx_PHEV_Charge_Depleting": "https://app.box.com/shared/static/oe6tbw2mlx35ak3m39ms6anxaeulheph.json",
"2016_BMW_i3_REx_PHEV_Charge_Sustaining": "https://app.box.com/shared/static/wjrmdj9ec6vb9mhtovsqe5bhi67ot4h9.json",
"2016_BMW_i3_REx_PHEV_Charge_Sustaining": "https://app.box.com/shared/static/tzhyb99ch98s3wofwld6vhu4ylbfuq8j.json",
"2016_CHEVROLET_Malibu_4cyl_2WD": "https://app.box.com/shared/static/hi0k3dsnliru8bf3ho4tygnz7o2bbc39.json",
"2016_CHEVROLET_Spark_EV": "https://app.box.com/shared/static/p3w6sifsaf924hm4xr0fcnyvq5f735sb.json",
"2016_CHEVROLET_Volt_Charge_Depleting": "https://app.box.com/shared/static/refg47ekep5anyq41cvd5e347w5bglv4.json",
"2016_CHEVROLET_Volt_Charge_Sustaining": "https://app.box.com/shared/static/gtduyd1vhfm55lk3je18zsjb5e75dxcb.json",
"2016_CHEVROLET_Volt_Charge_Sustaining": "https://app.box.com/shared/static/v90zippsq2gbenzevk6tktjh20skm5rb.json",
"2016_FORD_C-MAX_(PHEV)_Charge_Depleting": "https://app.box.com/shared/static/8xzw1joi4z75146wosob55z0v4t56s0t.json",
"2016_FORD_C-MAX_(PHEV)_Charge_Sustaining": "https://app.box.com/shared/static/5e2tbg1lxhbb8xpgg9xpjee8po0vuile.json",
"2016_FORD_C-MAX_(PHEV)_Charge_Sustaining": "https://app.box.com/shared/static/ejgyl2ye0j0d88p99t8tjs7xqpmfute9.json",
"2016_FORD_C-MAX_HEV": "https://app.box.com/shared/static/1g0ut3e8dux98wupucrqnlhcrqb4pg2q.json",
"2016_FORD_Escape_4cyl_2WD": "https://app.box.com/shared/static/co9prrqzfqk4lj8860zwpah1n6lmethz.json",
"2016_FORD_Explorer_4cyl_2WD": "https://app.box.com/shared/static/odlr0di4avsyk09sgt98oifuekemkluw.json",
"2016_HYUNDAI_Elantra_4cyl_2WD": "https://app.box.com/shared/static/gou1vtt6ii5npr65o834iz1xsn1wxx1h.json",
"2016_HYUNDAI_Sonata_PHEV_Charge_Depleting": "https://app.box.com/shared/static/s802i81gce7zgue9bqksx7qhupbuq30i.json",
"2016_HYUNDAI_Sonata_PHEV_Charge_Sustaining": "https://app.box.com/shared/static/4clk59eyyxebg4jcrok5xhyfbykzjee4.json",
"2016_HYUNDAI_Sonata_PHEV_Charge_Sustaining": "https://app.box.com/shared/static/m7cskbeppcug6ekwx0dpt7btvza01d8m.json",
"2016_KIA_Optima_Hybrid": "https://app.box.com/shared/static/iavjn7x93umljvtmuwtbplsbxd1nrjbm.json",
"2016_Leaf_24_kWh": "https://app.box.com/shared/static/gl7ap1czp6cihsil29wwaktindqyfzk4.json",
"2016_MITSUBISHI_i-MiEV": "https://app.box.com/shared/static/93khsx238l3b3emq36ui8yj0rnmf737w.json",
Expand All @@ -29,7 +29,7 @@
"2017_CHEVROLET_Bolt": "https://app.box.com/shared/static/6yg9ed4gixrp7q5irze2w4ud5h3nthme.json",
"2017_Maruti_Dzire_VDI": "https://app.box.com/shared/static/luyzs74knk7yowjbuer5zb88getfstjx.json",
"2017_Prius_Prime_Charge_Depleting": "https://app.box.com/shared/static/5m6mnxtg0bpb8to19570nikcxa7mryn3.json",
"2017_Prius_Prime_Charge_Sustaining": "https://app.box.com/shared/static/84yx66loytq79of7w528m68ehibs16ze.json",
"2017_Prius_Prime_Charge_Sustaining": "https://app.box.com/shared/static/cxtts7pj9hi2n8om2xcx2d1i0rpsa6pw.json",
"2017_Toyota_Highlander_3": "https://app.box.com/shared/static/ihmhqsy2myfceh25xhhld8jhv995np2d.json",
"2020_Chevrolet_Colorado_2WD_Diesel": "https://app.box.com/shared/static/i969w1bc6p1uvvubtxtiugvnh56ib1dq.json",
"2020_VW_Golf_1": "https://app.box.com/shared/static/u3ya0kc5ehol7qcv7ecov6992839cn9g.json",
Expand Down
13 changes: 11 additions & 2 deletions nrel/routee/powertrain/trainers/sklearn_random_forest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from enum import Enum
import pandas as pd
from skl2onnx import to_onnx
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
from sklearn.ensemble import RandomForestRegressor

Expand Down Expand Up @@ -55,10 +55,19 @@ def inner_train(
if self.output_type == RandomForestTrainerOutput.ONNX:
# convert to ONNX
n_features = len(features.columns)
n_targets = len(target.columns)

# explicity specify the output shape since skl2onnx was not able to infer it
def custom_transform_shape_calculator(operator):
operator.outputs[0].type = FloatTensorType([None, n_targets])

initial_type = [(ONNX_INPUT_NAME, FloatTensorType([None, n_features]))]
onnx_model = to_onnx(
onnx_model = convert_sklearn(
rf,
initial_types=initial_type,
custom_shape_calculators={
rf.__class__: custom_transform_shape_calculator
},
)

estimator = ONNXEstimator(onnx_model)
Expand Down
12 changes: 4 additions & 8 deletions nrel/routee/powertrain/validation/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,18 @@
}


def mean_squared_error(
A: np.ndarray, B: np.ndarray, axis: Optional[int] = None
) -> float:
def mean_squared_error(A, B, axis: Optional[int] = None) -> float:
return np.square(A - B).mean(axis=axis)


def net_energy_error(target: np.ndarray, target_pred: np.ndarray) -> float:
def net_energy_error(target, target_pred) -> float:
net_e = np.sum(target)
net_e_pred = np.sum(target_pred)
net_error = (net_e_pred - net_e) / net_e
return net_error


def weighted_relative_percent_difference(
target: np.ndarray, target_pred: np.ndarray
) -> float:
def weighted_relative_percent_difference(target, target_pred) -> float:
epsilon = np.finfo(np.float64).eps

w = np.array(np.abs(target) / np.sum(np.abs(target)))
Expand All @@ -61,7 +57,7 @@ def weighted_relative_percent_difference(
return mean_error


def relative_percent_difference(target: np.ndarray, target_pred: np.ndarray) -> float:
def relative_percent_difference(target, target_pred) -> float:
epsilon = np.finfo(np.float64).eps

error_norm = np.abs(
Expand Down
11 changes: 9 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "nrel.routee.powertrain"
version = "1.0.2"
version = "1.0.3"
description = "RouteE-Powertrain is a tool for predicting energy usage over a set of road links."
readme = "README.md"
authors = [{ name = "National Renewable Energy Laboratory" }]
Expand Down Expand Up @@ -39,7 +39,8 @@ dev = [
"types-protobuf",
"types-python-dateutil",
"types-redis",
"types-requests"
"types-requests",
"pandas-stubs",
]

[project.urls]
Expand All @@ -56,3 +57,9 @@ include = [

[tool.ruff.per-file-ignores]
"__init__.py" = ["F401"]

[tool.mypy]
ignore_missing_imports = true
namespace_packages = true
explicit_package_bases = true
exclude = ["docs/", "build/", "dist/"]
8 changes: 4 additions & 4 deletions scripts/developers/train_model_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,25 +312,25 @@ def load_all_files(files, file_limit=FILE_LIMIT):
pt.DataColumn(name="grade_dec", units="decimal"),
pt.DataColumn(name="entry_angle", units="degrees"),
]
features = [
features = [
[pt.DataColumn(name="speed_mph", units="mph")],
[
pt.DataColumn(name="speed_mph", units="mph"),
pt.DataColumn(name="grade_dec", units="decimal")
pt.DataColumn(name="grade_dec", units="decimal"),
],
[
pt.DataColumn(name="speed_mph", units="mph"),
pt.DataColumn(name="previous_speed_mph", units="mph"),
pt.DataColumn(name="grade_dec", units="decimal"),
pt.DataColumn(name="previous_grade_dec", units="decimal")
pt.DataColumn(name="previous_grade_dec", units="decimal"),
],
[
pt.DataColumn(name="previous_speed_mph", units="mph"),
pt.DataColumn(name="speed_mph", units="mph"),
pt.DataColumn(name="previous_grade_dec", units="decimal"),
pt.DataColumn(name="grade_dec", units="decimal"),
pt.DataColumn(name="entry_angle", units="degrees"),
pt.DataColumn(name="exit_angle", units="degrees")
pt.DataColumn(name="exit_angle", units="degrees"),
],
]

Expand Down
3 changes: 0 additions & 3 deletions setup.py

This file was deleted.

0 comments on commit 765d836

Please sign in to comment.