Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
REFACT: f_downscale-->f_dsc
Browse files Browse the repository at this point in the history
  • Loading branch information
ankostis committed May 12, 2020
1 parent 3d94c38 commit 4590527
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 84 deletions.
4 changes: 2 additions & 2 deletions Notebooks/CarsDB-compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def load_accdb_and_python_datasets(veh_nums=None):
("kerb_mass", None),
("vehicle_class", "wltc_class"),
# ("pmr_km", "pmr"),
("f_dsc_req", "f_downscale"),
("f_dsc_req", "f_dsc"),
("v_max", "v_max"),
("n_max1", "n95_high"),
# ("???", "is_n_lim_vmax")
Expand Down Expand Up @@ -191,7 +191,7 @@ def load_accdb_and_python_datasets(veh_nums=None):
("vehicle_class", "wltc_class"),
("pmr_km", None),
("no_of_gears", None),
("f_dsc_req", "f_downscale"),
("f_dsc_req", "f_dsc"),
("v_max", "v_max"),
("n_vmax", "n_vmax"),
("gear_v_max", "g_vmax"),
Expand Down
2 changes: 1 addition & 1 deletion Notebooks/RunVehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"n_max": mdl["n_max"],
"wltc_class": mdl["wltc_class"],
"f_dscl_orig": mdl["f_dscl_orig"],
"f_downscale": mdl["f_downscale"],
"f_dsc": mdl["f_dsc"],
}

pprint(oprops)
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ before running the experiment:

>>> mdl = processor.model ## Returns the validated model with filled-in defaults.
>>> sorted(mdl) ## The "defaulted" model now includes the `params` branch.
['driver_mass', 'f0', 'f1', 'f2', 'f_downscale_decimals', 'f_downscale_threshold', 'f_inertial',
['driver_mass', 'f0', 'f1', 'f2', 'f_dsc_decimals', 'f_dsc_threshold', 'f_inertial',
'f_n_clutch_gear2', 'f_n_min', 'f_n_min_gear2', 'f_safety_margin', 'gear_ratios', 'n_idle',
'n_min_drive1', 'n_min_drive2', 'n_min_drive2_stopdecel', 'n_min_drive2_up', 'n_min_drive_down',
'n_min_drive_down_start', 'n_min_drive_set', 'n_min_drive_up', 'n_min_drive_up_start', 'n_rated',
Expand All @@ -331,7 +331,7 @@ Now you can run the experiment:

>>> mdl = processor.run() ## Runs experiment and augments the model with results.
>>> sorted(mdl) ## Print the top-branches of the "augmented" model.
[`cycle`, 'driver_mass', 'f0', 'f1', 'f2', `f_downscale`, 'f_downscale_decimals', 'f_downscale_threshold',
[`cycle`, 'driver_mass', 'f0', 'f1', 'f2', `f_dsc`, 'f_dsc_decimals', 'f_dsc_threshold',
`f_dscl_orig`, 'f_inertial', 'f_n_clutch_gear2', 'f_n_min', 'f_n_min_gear2', 'f_safety_margin', `g_vmax`,
'gear_ratios', `is_n_lim_vmax`, `n95_high`, `n95_low`, 'n_idle', `n_max`, `n_max1`, `n_max2`, `n_max3`,
'n_min_drive1', 'n_min_drive2', 'n_min_drive2_stopdecel', 'n_min_drive2_up', 'n_min_drive_down',
Expand Down
3 changes: 3 additions & 0 deletions datatree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
/f_downscale
/f_downscale_decimals
/f_downscale_threshold
/f_dsc
/f_dsc_decimals
/f_dsc_threshold
/f_dscl_orig
/f_inertial
/f_safety_margin
Expand Down
10 changes: 5 additions & 5 deletions docs/code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,14 @@ The :term:`JSON-schema` of the data for this project:
maxItems: 2
items:
type: number
f_downscale_threshold:
f_dsc_threshold:
title: Downscale-factor threshold
description: The limit for the calculated `f_downscale` below which no downscaling
description: The limit for the calculated `f_dsc` below which no downscaling
happens.
type:
- number
default: 0.01
f_downscale_decimals:
f_dsc_decimals:
title: Downscale-factor rounding decimals
type:
- number
Expand Down Expand Up @@ -420,7 +420,7 @@ The :term:`JSON-schema` of the data for this project:
default:
- 1.15
- 0.03
f_downscale:
f_dsc:
description: |
The downscaling-factor as calculated by the experiment (Annex 1-8.3).
<BLANKLINE>
Expand All @@ -430,7 +430,7 @@ The :term:`JSON-schema` of the data for this project:
$ref: /wltc
cycle:
description: |
A dataframe matrix with 2-level columns(item, gear),
An inp/out dataframe matrix with 2-level columns(item, gear),
and items, in addition to those of `grid_wots`:
- `v_cycle`: reduced by safety-margin, but not by ASM
- `v_dsc`: (optional)
Expand Down
42 changes: 23 additions & 19 deletions tests/test_downscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

from wltp import datamodel
from wltp.downscale import (
calc_downscale_factor,
calc_f_dsc_orig,
calc_f_dsc,
calc_v_dsc,
decide_wltc_class,
downscale_by_recursing,
Expand All @@ -42,48 +43,51 @@ def test_smoke1():
class_data = wltc["classes"][wltc_class]
V = pd.Series(class_data["v_cycle"])

f_downscale_threshold = 0.01 # TODO: get it from schema-default
f_downscale_decimals = 3 # TODO: get it from schema-default
f_dsc_threshold = 0.01 # TODO: get it from schema-default
f_dsc_decimals = 3 # TODO: get it from schema-default
dsc_data = class_data["downscale"]
phases = dsc_data["phases"]
p_max_values = dsc_data["p_max_values"]
downsc_coeffs = dsc_data["factor_coeffs"]
f_downscale, _orig_f = calc_downscale_factor(
f_dsc_orig = calc_f_dsc_orig(
p_max_values,
downsc_coeffs,
p_rated,
f_downscale_threshold,
f_downscale_decimals,
test_mass,
f0,
f1,
f2,
f_inertial,
)
if f_downscale > 0:
V = calc_v_dsc(V, f_downscale, phases)
f_dsc = calc_f_dsc(
f_dsc_orig,
f_dsc_threshold,
f_dsc_decimals,
)
if f_dsc > 0:
V = calc_v_dsc(V, f_dsc, phases)
# print(
# "Class(%s), f_dnscl(%s), DIFFs:\n%s" % (wclass, f_downscale, diffs[bad_ix])
# "Class(%s), f_dnscl(%s), DIFFs:\n%s" % (wclass, f_dsc, diffs[bad_ix])
# )
# plt.plot(V, "r")
# plt.plot(V1, "b")
# plt.plot(V2, "g")
# plt.show()
# raise AssertionError(
# "Class(%s), f_dnscl(%s)" % (wclass, f_downscale)
# "Class(%s), f_dnscl(%s)" % (wclass, f_dsc)
# )


def test_smoke2():
wclasses = datamodel.get_wltc_data()["classes"]
test_data = [
(pd.Series(wclass["v_cycle"]), wclass["downscale"]["phases"], f_downscale)
(pd.Series(wclass["v_cycle"]), wclass["downscale"]["phases"], f_dsc)
for wclass in wclasses.values()
for f_downscale in np.linspace(0.1, 1, 10)
for f_dsc in np.linspace(0.1, 1, 10)
]

for (V, phases, f_downscale) in test_data:
calc_v_dsc(V, f_downscale, phases)
for (V, phases, f_dsc) in test_data:
calc_v_dsc(V, f_dsc, phases)


_wltc = datamodel.get_wltc_data()
Expand Down Expand Up @@ -116,23 +120,23 @@ def double_round(n, decimals):
phases = class_data["downscale"]["phases"]

bad_accuracies, bad_rounds = {}, {}
for f_downscale in np.arange(0, 4, 0.1):
V1 = downscale_by_recursing(V, f_downscale, phases)
V2 = downscale_by_scaling(V, f_downscale, phases)
for f_dsc in np.arange(0, 4, 0.1):
V1 = downscale_by_recursing(V, f_dsc, phases)
V2 = downscale_by_scaling(V, f_dsc, phases)

bad_ix = ~np.isclose(V1, V2)
if bad_ix.any():
errs = pd.concat(
(V1, V2, V1 - V2), axis=1, keys=["recurse", "rescale", "diff"]
)[bad_ix]
bad_accuracies[f_downscale] = errs
bad_accuracies[f_dsc] = errs

bad_ix = (
double_round(V1, v_decimals).to_numpy()
!= double_round(V2, v_decimals).to_numpy()
)
if bad_ix.any():
bad_rounds[f_downscale] = pd.concat(
bad_rounds[f_dsc] = pd.concat(
(V1, V2, (V1 - V2).abs()), axis=1, keys=["recurse", "rescale", "diff"]
)[bad_ix]

Expand Down
4 changes: 2 additions & 2 deletions tests/vehdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def compare(
:param equiv_colnames:
a matrix of M x N column-names, like:
[("p_downscale", "f_downscale"), ("cycle", "wltc_class"), ...]
[("p_downscale", "f_dsc"), ("cycle", "wltc_class"), ...]
All columns in the N dimension must exist in the respective dataframe in `datasets` list.
The 1st columns in the M-dimension are used a axis-1, level-0 labels
Expand Down Expand Up @@ -619,7 +619,7 @@ def run_pyalgo_on_accdb_vehicle(
"n_max": mdl["n_max"],
"wltc_class": mdl["wltc_class"],
"f_dscl_orig": mdl["f_dscl_orig"],
"f_downscale": mdl["f_downscale"],
"f_dsc": mdl["f_dsc"],
}

cycle = mdl["cycle"]
Expand Down
10 changes: 5 additions & 5 deletions wltp/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,15 @@ def _get_model_schema(additional_properties=False) -> dict:
maxItems: 2
items:
type: number
f_downscale_threshold:
f_dsc_threshold:
title: Downscale-factor threshold
description:
The limit for the calculated `f_downscale` below which no downscaling
The limit for the calculated `f_dsc` below which no downscaling
happens.
type:
- number
default: 0.01
f_downscale_decimals:
f_dsc_decimals:
title: Downscale-factor rounding decimals
type:
- number
Expand Down Expand Up @@ -466,7 +466,7 @@ def _get_model_schema(additional_properties=False) -> dict:
default:
- 1.15
- 0.03
f_downscale:
f_dsc:
description: |2
The downscaling-factor as calculated by the experiment (Annex 1-8.3).
Expand All @@ -476,7 +476,7 @@ def _get_model_schema(additional_properties=False) -> dict:
$ref: {_wltc_url}
cycle:
description: |2
A dataframe matrix with 2-level columns(item, gear),
An inp/out dataframe matrix with 2-level columns(item, gear),
and items, in addition to those of `grid_wots`:
- `v_cycle`: reduced by safety-margin, but not by ASM
- `v_dsc`: (optional)
Expand Down
68 changes: 31 additions & 37 deletions wltp/downscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

import pandas as pd

from . import io as wio
from . import invariants as inv

from . import io as wio

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -42,19 +41,12 @@ def decide_wltc_class(wltc_data, p_m_ratio, v_max):
return wltc_class


def calc_downscale_factor(
p_max_values,
downsc_coeffs,
p_rated,
f_downscale_threshold,
f_downscale_decimals, # TODO: DROP f_downscale_decimals, in invariants
test_mass,
f0,
f1,
f2,
f_inertial,
def calc_f_dsc_orig(
wltc_dsc_p_max_values, wltc_dsc_coeffs, p_rated, test_mass, f0, f1, f2, f_inertial,
):
"""Check if downscaling required, and apply it.
"""
Check if downscaling required, and apply it.
:return: (float) the factor
@see: Annex 1-7, p 68
Expand All @@ -63,40 +55,42 @@ def calc_downscale_factor(

## Max required power at critical point.
#
pmv = p_max_values[c.v]
pma = p_max_values[c.a]
pmv = wltc_dsc_p_max_values[c.v]
pma = wltc_dsc_p_max_values[c.a]
p_req_max = (
f0 * pmv + f1 * pmv ** 2 + f2 * pmv ** 3 + f_inertial * pma * pmv * test_mass
) / 3600.0
r_max = p_req_max / p_rated

(r0, a1, b1) = downsc_coeffs
(r0, a1, b1) = wltc_dsc_coeffs

if r_max >= r0:
f_downscale = a1 * r_max + b1
f_downscale = f_downscale_orig = inv.round1(f_downscale, f_downscale_decimals)

## ATTENTION:
# By the spec, f_downscale MUST be > 0.01 to apply,
# but in F new vehicle.form.txt:(3537, 3563, 3589) (see CHANGES.rst)
# a +0.5 is ADDED!
if f_downscale <= f_downscale_threshold:
f_downscale = 0
f_dsc = a1 * r_max + b1
else:
f_downscale = f_downscale_orig = 0
f_dsc = 0

return f_downscale, f_downscale_orig
return f_dsc


def calc_f_dsc(f_dsc_orig: float, f_dsc_threshold: float, f_dsc_decimals,) -> float:
"""
ATTENTION: by the spec, f_dsc MUST be > 0.01 to apply, but
in :file:`F_new_vehicle.form.txt:(3537, 3563, 3589)` a +0.5 is ADDED!
(see CHANGES.rst)
"""
f_dsc = inv.round1(f_dsc_orig, f_dsc_decimals)
return 0 if f_dsc <= f_dsc_threshold else f_dsc


def calc_v_dsc(V: pd.Series, f_downscale, phases) -> pd.Series:
def calc_v_dsc(v: pd.Series, f_dsc, dsc_phases) -> pd.Series:
"""
Downscale velocity profile by `f_downscale`.
Downscale velocity profile by `f_dsc`.
:return:
the downscaled velocity profile, not-rounded
(by the Spec should have 1 decimal only)
- The Spec demarks 2 UP/DOWN phases with 3 time-points in `phases`,
- The Spec demarks 2 UP/DOWN phases with 3 time-points in `dsc_phases`,
eg. for class3:
- 1533: the "start" of downscaling
Expand All @@ -120,11 +114,11 @@ def calc_v_dsc(V: pd.Series, f_downscale, phases) -> pd.Series:
@see: Annex 1-8, p 64-68
"""
# return downscale_by_recursing(V, f_downscale, phases)
return downscale_by_scaling(V, f_downscale, phases)
# return downscale_by_recursing(V, f_dsc, phases)
return downscale_by_scaling(v, f_dsc, dsc_phases)


def downscale_by_recursing(V, f_downscale, phases):
def downscale_by_recursing(V, f_dsc, phases):
"""Downscale by recursing (according to the Spec). """
V_DSC = V.copy()
(t0, t1, t2) = phases
Expand All @@ -133,7 +127,7 @@ def downscale_by_recursing(V, f_downscale, phases):
#
for t in range(t0, t1):
a = V[t + 1] - V[t]
V_DSC[t + 1] = V_DSC[t] + a * (1 - f_downscale)
V_DSC[t + 1] = V_DSC[t] + a * (1 - f_dsc)

## Deceleration phase
#
Expand All @@ -145,7 +139,7 @@ def downscale_by_recursing(V, f_downscale, phases):
return V_DSC


def downscale_by_scaling(V: pd.Series, f_downscale, phases) -> pd.Series:
def downscale_by_scaling(V: pd.Series, f_dsc, phases) -> pd.Series:
"""
Multiply the UP/DOWN phases with 2 factors (no recurse, against the Spec).
Expand All @@ -160,7 +154,7 @@ def downscale_by_scaling(V: pd.Series, f_downscale, phases) -> pd.Series:
"""
V_DSC = V.copy()
(t0, t1, t2) = phases
f_scale = 1 - f_downscale
f_scale = 1 - f_dsc

## UP-phase
#
Expand Down
Loading

0 comments on commit 4590527

Please sign in to comment.