Skip to content

Commit

Permalink
Merge pull request #55 from RSGInc/eschew-at-sign
Browse files Browse the repository at this point in the history
at sign considered harmful
  • Loading branch information
toliwaga committed Mar 19, 2016
2 parents 92566c5 + 436e0bf commit cd4c507
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 149 deletions.
34 changes: 14 additions & 20 deletions bca4abm/bca4abm.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,23 @@ def assign_variables(assignment_expressions, df, locals_d):
Evaluate a set of variable expressions from a spec in the context
of a given data table.
There are two kinds of supported expressions: "simple" expressions are
evaluated in the context of the DataFrame using DataFrame.eval.
This is the default type of expression.
Expressions are evaluated using Python's eval function.
Python expressions have access to variables in locals_d (and df being
accessible as variable df.) They also have access to previously assigned
targets as the assigned target name.
Python expressions are evaluated in the context of this function using
Python's eval function. Because we use Python's eval this type of
expression supports more complex operations than a simple expression.
Python expressions are denoted by beginning with the @ character.
Users should take care that these expressions must result in
a Pandas Series.
Users should take care that expressions should result in
a Pandas Series (scalars will be automatically promoted to series.)
Parameters
----------
exprs : sequence of str
assignment_expressions : pandas.DataFrame of target assignment expressions
target: target column names
expression: pandas or python expression to evaluate
df : pandas.DataFrame
locals_d : Dict
This is a dictionary of local variables that will be the environment
for an evaluation of an expression that begins with @
for an evaluation of "python" expression.
Returns
-------
Expand All @@ -131,9 +130,10 @@ def assign_variables(assignment_expressions, df, locals_d):
"""

def to_series(x):
def to_series(x, target=None):
if np.isscalar(x):
# assert False
if target:
print "WARNING: assign_variables promoting scalar %s to series" % target
return pd.Series([x] * len(df), index=df.index)
return x

Expand All @@ -148,9 +148,7 @@ def to_series(x):
target = e[0]
expression = e[1]
try:
values = to_series(eval(expression[1:], globals(), locals_d)) \
if expression.startswith('@') \
else df.eval(expression)
values = to_series(eval(expression, globals(), locals_d), target=target)
l.append((target, values))

# FIXME - do we want to update locals to allows us to ref previously assigned targets?
Expand All @@ -159,10 +157,6 @@ def to_series(x):
print "Variable %s expression failed for: %s" % (str(target), str(expression))
raise err

# FIXME - should we add_assigned_columns rather than creating a dataframe and returning it?
# FIXME - we could pass in the target df to optionally assign directly from items
# FIXME - (though desired target might not be the eval df if eval df is a merged table...)

# since we allow targets to be recycled, we want to only keep the last usage
keepers = []
for statement in reversed(l):
Expand Down
6 changes: 3 additions & 3 deletions bca4abm/tests/configs/auto_ownership.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Description,Target,Expression
base scenario auto ownership cost,base_auto_ownership_cost, @persons.hh_expansion_factor * persons.base_vehicles * ANNUAL_COST_PER_VEHICLE / persons.hh_size
build scenario auto ownership cost,build_auto_ownership_cost, @persons.hh_expansion_factor * persons.build_vehicles * ANNUAL_COST_PER_VEHICLE / persons.hh_size
auto ownership cost benefit,diff_auto_ownership_cost, @build_auto_ownership_cost - base_auto_ownership_cost
base scenario auto ownership cost,base_auto_ownership_cost, persons.hh_expansion_factor * persons.base_vehicles * ANNUAL_COST_PER_VEHICLE / persons.hh_size
build scenario auto ownership cost,build_auto_ownership_cost, persons.hh_expansion_factor * persons.build_vehicles * ANNUAL_COST_PER_VEHICLE / persons.hh_size
auto ownership cost benefit,diff_auto_ownership_cost, build_auto_ownership_cost - base_auto_ownership_cost
6 changes: 3 additions & 3 deletions bca4abm/tests/configs/demographics.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Target,Expression
# compare hh_income to federal poverty threshold based on hh_size
_poverty_threshold, @(persons.hh_size==1)*POVERTY_1 + (persons.hh_size>1)*POVERTY_2 + (persons.hh_size - 2).clip(lower=0)*POVERTY_N
coc_poverty,@persons.hh_income <= _poverty_threshold
coc_age, person_age > 65
_poverty_threshold, (persons.hh_size==1)*POVERTY_1 + (persons.hh_size>1)*POVERTY_2 + (persons.hh_size - 2).clip(lower=0)*POVERTY_N
coc_poverty, persons.hh_income <= _poverty_threshold
coc_age, persons.person_age > 65
32 changes: 16 additions & 16 deletions bca4abm/tests/configs/link.csv
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
Description,Target,Expression
,_auto_volume, total_volume - truck_volume
,_vmt_auto, @ _auto_volume * links.distance
,_vmt_truck, @ links.truck_volume * links.distance
,_auto_volume, links.total_volume - links.truck_volume
,_vmt_auto, _auto_volume * links.distance
,_vmt_truck, links.truck_volume * links.distance
# operating costs SANDAG version
auto operating cost,cost_op_auto, @ _vmt_auto * OPERATING_COST_PER_MILE_AUTO * DISCOUNT_RATE * ANNUALIZATION_FACTOR
truck operating cost,cost_op_truck, @ _vmt_truck * OPERATING_COST_PER_MILE_TRUCK * DISCOUNT_RATE * ANNUALIZATION_FACTOR
total auto and truck operating cost,cost_op_total, @ cost_op_auto + cost_op_truck
auto operating cost,cost_op_auto, _vmt_auto * OPERATING_COST_PER_MILE_AUTO * DISCOUNT_RATE * ANNUALIZATION_FACTOR
truck operating cost,cost_op_truck, _vmt_truck * OPERATING_COST_PER_MILE_TRUCK * DISCOUNT_RATE * ANNUALIZATION_FACTOR
total auto and truck operating cost,cost_op_total, cost_op_auto + cost_op_truck
# unreliability costs SANDAG version
,_time_ratio, congested_time / time
,_ttim2, @ (TIME_RATIO_FACTOR * (_time_ratio ** TIME_RATIO_POWER)).clip(upper=TIME_RATIO_UPPER_LIMIT)
,_tti50, @ _ttim2 ** TTI50_POWER
,_tti80, @ 1 + log(_ttim2) * TTI80_LOG_MULTIPLIER
,_dly_per_mile, @ _tti50 + (_tti80 - _tti50) * RELIABILITY_RATIO
,_equiv_delay_auto, @ _dly_per_mile * links.distance * _auto_volume/60
,_equiv_delay_truck, @ _dly_per_mile * links.distance * links.truck_volume/60
auto delay cost,cost_delay_auto, @ _equiv_delay_auto * VALUE_OF_RELIABILITY_AUTO * DISCOUNT_RATE * ANNUALIZATION_FACTOR
truck delay cost,cost_delay_truck, @ _equiv_delay_truck * VALUE_OF_RELIABILITY_TRUCK * DISCOUNT_RATE * ANNUALIZATION_FACTOR
total auto and truck cost,cost_delay_total, @ cost_delay_auto + cost_delay_truck
,_time_ratio, links.congested_time / links.time
,_ttim2, (TIME_RATIO_FACTOR * (_time_ratio ** TIME_RATIO_POWER)).clip(upper=TIME_RATIO_UPPER_LIMIT)
,_tti50, _ttim2 ** TTI50_POWER
,_tti80, 1 + log(_ttim2) * TTI80_LOG_MULTIPLIER
,_dly_per_mile, _tti50 + (_tti80 - _tti50) * RELIABILITY_RATIO
,_equiv_delay_auto, _dly_per_mile * links.distance * _auto_volume/60
,_equiv_delay_truck, _dly_per_mile * links.distance * links.truck_volume/60
auto delay cost,cost_delay_auto, _equiv_delay_auto * VALUE_OF_RELIABILITY_AUTO * DISCOUNT_RATE * ANNUALIZATION_FACTOR
truck delay cost,cost_delay_truck, _equiv_delay_truck * VALUE_OF_RELIABILITY_TRUCK * DISCOUNT_RATE * ANNUALIZATION_FACTOR
total auto and truck cost,cost_delay_total, cost_delay_auto + cost_delay_truck

10 changes: 5 additions & 5 deletions bca4abm/tests/configs/link_daily.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Description,Target,Expression
total vehicle miles travelled,vmt_total, total_volume * distance
total vehicle miles travelled,vmt_total, links.total_volume * links.distance
# accident costs SANDAG version based on daily volumes
annualized crash cost property damage,crash_cost_pdo, @ vmt_total * CRASH_RATE_PDO * CRASH_COST_PDO * DISCOUNT_RATE * ANNUALIZATION_FACTOR
annualized crash cost injury,crash_cost_injury, @ vmt_total * CRASH_RATE_INJURY * CRASH_COST_INJURY * DISCOUNT_RATE * ANNUALIZATION_FACTOR
annualized crash cost fatalities,crash_cost_fatal, @ vmt_total * CRASH_RATE_FATAL * CRASH_COST_FATAL * DISCOUNT_RATE * ANNUALIZATION_FACTOR
total annualized crash cost,crash_cost_total, @ crash_cost_pdo + crash_cost_injury + crash_cost_fatal
annualized crash cost property damage,crash_cost_pdo, vmt_total * CRASH_RATE_PDO * CRASH_COST_PDO * DISCOUNT_RATE * ANNUALIZATION_FACTOR
annualized crash cost injury,crash_cost_injury, vmt_total * CRASH_RATE_INJURY * CRASH_COST_INJURY * DISCOUNT_RATE * ANNUALIZATION_FACTOR
annualized crash cost fatalities,crash_cost_fatal, vmt_total * CRASH_RATE_FATAL * CRASH_COST_FATAL * DISCOUNT_RATE * ANNUALIZATION_FACTOR
total annualized crash cost,crash_cost_total, crash_cost_pdo + crash_cost_injury + crash_cost_fatal
28 changes: 14 additions & 14 deletions bca4abm/tests/configs/person_trips.csv
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Description,Target,Expression
,_work, @trips.tour_purpose.map(TOUR_PURPOSE_WORK_MAP)
,_work, trips.tour_purpose.map(TOUR_PURPOSE_WORK_MAP)
#
monetized auto ivt,auto_time, @-0.5 * trips.hh_expansion_factor * (trips.build_auto_time - trips.base_auto_time) * _work.map(WORK_IVT_VOT_MAP)/60.0 * DISCOUNT_RATE * ANNUALIZATION_FACTOR
monetized transit ivt,transit_time, @-0.5 * trips.hh_expansion_factor * (trips.build_transit_time - trips.base_transit_time) * _work.map(WORK_IVT_VOT_MAP)/60.0 * DISCOUNT_RATE * ANNUALIZATION_FACTOR
monetized transit wait time,transit_wait_time, @-0.5 * trips.hh_expansion_factor * (trips.build_transit_wait - trips.base_transit_wait) * _work.map(WORK_WAIT_VOT_MAP)/60.0 * DISCOUNT_RATE * ANNUALIZATION_FACTOR
monetized transit walk time,transit_walk_time, @-0.5 * trips.hh_expansion_factor * (trips.build_transit_walk - trips.base_transit_walk) * _work.map(WORK_WALK_VOT_MAP)/60.0 * DISCOUNT_RATE * ANNUALIZATION_FACTOR
monetized bike time,bike_time, @-0.5 * trips.hh_expansion_factor * (trips.build_bike_time - trips.base_bike_time) * _work.map(WORK_IVT_VOT_MAP)/60.0 * DISCOUNT_RATE * ANNUALIZATION_FACTOR
monetized walk time,walk_time, @-0.5 * trips.hh_expansion_factor * (trips.build_walk_time - trips.base_walk_time) * _work.map(WORK_WALK_VOT_MAP)/60.0 * DISCOUNT_RATE * ANNUALIZATION_FACTOR
toll cost,toll, @-0.5 * trips.hh_expansion_factor * (trips.build_toll_cost - trips.base_toll_cost) * TOLL_INCLUSION_FACTOR * DISCOUNT_RATE * ANNUALIZATION_FACTOR
fuel cost,fuel, @-0.5 * trips.hh_expansion_factor * (trips.build_fuel_cost - trips.base_fuel_cost) * FUEL_INCLUSION_FACTOR * DISCOUNT_RATE * ANNUALIZATION_FACTOR
park cost,park, @-0.5 * trips.hh_expansion_factor * (trips.build_park_cost - trips.base_park_cost) * PARK_INCLUSION_FACTOR * DISCOUNT_RATE * ANNUALIZATION_FACTOR
fare cost,fare, @-0.5 * trips.hh_expansion_factor * (trips.build_fare_cost - trips.base_fare_cost) * FARE_INCLUSION_FACTOR * DISCOUNT_RATE * ANNUALIZATION_FACTOR
monetized auto ivt,auto_time, -0.5 * trips.hh_expansion_factor * (trips.build_auto_time - trips.base_auto_time) * _work.map(WORK_IVT_VOT_MAP)/60.0 * DISCOUNT_RATE * ANNUALIZATION_FACTOR
monetized transit ivt,transit_time, -0.5 * trips.hh_expansion_factor * (trips.build_transit_time - trips.base_transit_time) * _work.map(WORK_IVT_VOT_MAP)/60.0 * DISCOUNT_RATE * ANNUALIZATION_FACTOR
monetized transit wait time,transit_wait_time, -0.5 * trips.hh_expansion_factor * (trips.build_transit_wait - trips.base_transit_wait) * _work.map(WORK_WAIT_VOT_MAP)/60.0 * DISCOUNT_RATE * ANNUALIZATION_FACTOR
monetized transit walk time,transit_walk_time, -0.5 * trips.hh_expansion_factor * (trips.build_transit_walk - trips.base_transit_walk) * _work.map(WORK_WALK_VOT_MAP)/60.0 * DISCOUNT_RATE * ANNUALIZATION_FACTOR
monetized bike time,bike_time, -0.5 * trips.hh_expansion_factor * (trips.build_bike_time - trips.base_bike_time) * _work.map(WORK_IVT_VOT_MAP)/60.0 * DISCOUNT_RATE * ANNUALIZATION_FACTOR
monetized walk time,walk_time, -0.5 * trips.hh_expansion_factor * (trips.build_walk_time - trips.base_walk_time) * _work.map(WORK_WALK_VOT_MAP)/60.0 * DISCOUNT_RATE * ANNUALIZATION_FACTOR
toll cost,toll, -0.5 * trips.hh_expansion_factor * (trips.build_toll_cost - trips.base_toll_cost) * TOLL_INCLUSION_FACTOR * DISCOUNT_RATE * ANNUALIZATION_FACTOR
fuel cost,fuel, -0.5 * trips.hh_expansion_factor * (trips.build_fuel_cost - trips.base_fuel_cost) * FUEL_INCLUSION_FACTOR * DISCOUNT_RATE * ANNUALIZATION_FACTOR
park cost,park, -0.5 * trips.hh_expansion_factor * (trips.build_park_cost - trips.base_park_cost) * PARK_INCLUSION_FACTOR * DISCOUNT_RATE * ANNUALIZATION_FACTOR
fare cost,fare, -0.5 * trips.hh_expansion_factor * (trips.build_fare_cost - trips.base_fare_cost) * FARE_INCLUSION_FACTOR * DISCOUNT_RATE * ANNUALIZATION_FACTOR
#
,_monetized_time, @auto_time+transit_time+transit_wait_time+transit_walk_time+bike_time+walk_time
,_cost, @toll+fuel+park+fare
total benefit,total, @(_monetized_time + _cost)
,_monetized_time, auto_time+transit_time+transit_wait_time+transit_walk_time+bike_time+walk_time
,_cost, toll+fuel+park+fare
total benefit,total, _monetized_time + _cost
28 changes: 14 additions & 14 deletions bca4abm/tests/configs/physical_activity_person.csv
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Description,Target,Expression
,_under_20, person_age < 20
,_over_65, person_age >= 65
,_over_75, person_age >= 75
,_walk_risk_factor, @WALK_FACTOR_AGE_UNDER_20 * _under_20 + WALK_FACTOR_AGE_20_TO_74 * ( ~ (_under_20 | _over_75)) + WALK_FACTOR_AGE_75_PLUS * _over_75
,_bike_risk_factor, @BIKE_FACTOR_AGE_UNDER_20 * _under_20 + BIKE_FACTOR_AGE_20_TO_64 * ( ~ (_under_20 | _over_65)) + BIKE_FACTOR_AGE_65_PLUS * _over_65
,_base_walk_risk_reduction, @(_walk_risk_factor * persons.base_walk / WALK_DIVISOR_MINUTES_PER_DAY).clip(upper=WALK_MAX_RISK_REDUCTION)
,_base_bike_risk_reduction, @(_bike_risk_factor * persons.base_bike / BIKE_DIVISOR_MINUTES_PER_DAY).clip(upper=BIKE_MAX_RISK_REDUCTION)
,_base_total_risk_reduction, @(_base_walk_risk_reduction+_base_bike_risk_reduction).clip(upper=TOTAL_MAX_RISK_REDUCTION)
base scenario value of risk reduction,base_value_of_risk_reduction, @_base_total_risk_reduction * VALUE_OF_MORTALITY_RISK_PER_YEAR * persons.hh_expansion_factor
,_build_walk_risk_reduction, @(_walk_risk_factor * persons.build_walk / WALK_DIVISOR_MINUTES_PER_DAY).clip(upper=WALK_MAX_RISK_REDUCTION)
,_build_bike_risk_reduction, @(_bike_risk_factor * persons.build_bike / BIKE_DIVISOR_MINUTES_PER_DAY).clip(upper=BIKE_MAX_RISK_REDUCTION)
,_build_total_risk_reduction, @(_build_walk_risk_reduction+_build_bike_risk_reduction).clip(upper=TOTAL_MAX_RISK_REDUCTION)
build scenario value of risk reduction,build_value_of_risk_reduction, @_build_total_risk_reduction * VALUE_OF_MORTALITY_RISK_PER_YEAR * persons.hh_expansion_factor
benefit value of risk reduction,benefit_risk_reduction, @build_value_of_risk_reduction - base_value_of_risk_reduction
,_under_20, persons.person_age < 20
,_over_65, persons.person_age >= 65
,_over_75, persons.person_age >= 75
,_walk_risk_factor, WALK_FACTOR_AGE_UNDER_20 * _under_20 + WALK_FACTOR_AGE_20_TO_74 * ( ~ (_under_20 | _over_75)) + WALK_FACTOR_AGE_75_PLUS * _over_75
,_bike_risk_factor, BIKE_FACTOR_AGE_UNDER_20 * _under_20 + BIKE_FACTOR_AGE_20_TO_64 * ( ~ (_under_20 | _over_65)) + BIKE_FACTOR_AGE_65_PLUS * _over_65
,_base_walk_risk_reduction, (_walk_risk_factor * persons.base_walk / WALK_DIVISOR_MINUTES_PER_DAY).clip(upper=WALK_MAX_RISK_REDUCTION)
,_base_bike_risk_reduction, (_bike_risk_factor * persons.base_bike / BIKE_DIVISOR_MINUTES_PER_DAY).clip(upper=BIKE_MAX_RISK_REDUCTION)
,_base_total_risk_reduction, (_base_walk_risk_reduction+_base_bike_risk_reduction).clip(upper=TOTAL_MAX_RISK_REDUCTION)
base scenario value of risk reduction,base_value_of_risk_reduction, _base_total_risk_reduction * VALUE_OF_MORTALITY_RISK_PER_YEAR * persons.hh_expansion_factor
,_build_walk_risk_reduction, (_walk_risk_factor * persons.build_walk / WALK_DIVISOR_MINUTES_PER_DAY).clip(upper=WALK_MAX_RISK_REDUCTION)
,_build_bike_risk_reduction, (_bike_risk_factor * persons.build_bike / BIKE_DIVISOR_MINUTES_PER_DAY).clip(upper=BIKE_MAX_RISK_REDUCTION)
,_build_total_risk_reduction, (_build_walk_risk_reduction+_build_bike_risk_reduction).clip(upper=TOTAL_MAX_RISK_REDUCTION)
build scenario value of risk reduction,build_value_of_risk_reduction, _build_total_risk_reduction * VALUE_OF_MORTALITY_RISK_PER_YEAR * persons.hh_expansion_factor
benefit value of risk reduction,benefit_risk_reduction, build_value_of_risk_reduction - base_value_of_risk_reduction
8 changes: 4 additions & 4 deletions bca4abm/tests/configs/physical_activity_trip.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Target,Expression
base_walk, (base == 1) * (base_transit_walk + base_walk_time)
base_bike, (base == 1) * base_bike_time
build_walk, (build == 1) * (build_transit_walk + build_walk_time)
build_bike, (build == 1) * build_bike_time
base_walk, (trips.base == 1) * (trips.base_transit_walk + trips.base_walk_time)
base_bike, (trips.base == 1) * trips.base_bike_time
build_walk, (trips.build == 1) * (trips.build_transit_walk + trips.build_walk_time)
build_bike, (trips.build == 1) * trips.build_bike_time
6 changes: 3 additions & 3 deletions example/configs/auto_ownership.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Description,Target,Expression
base scenario auto ownership cost,base_auto_ownership_cost, @persons.hh_expansion_factor * persons.base_vehicles * ANNUAL_COST_PER_VEHICLE / persons.hh_size
build scenario auto ownership cost,build_auto_ownership_cost, @persons.hh_expansion_factor * persons.build_vehicles * ANNUAL_COST_PER_VEHICLE / persons.hh_size
auto ownership cost benefit,diff_auto_ownership_cost, @build_auto_ownership_cost - base_auto_ownership_cost
base scenario auto ownership cost,base_auto_ownership_cost, persons.hh_expansion_factor * persons.base_vehicles * ANNUAL_COST_PER_VEHICLE / persons.hh_size
build scenario auto ownership cost,build_auto_ownership_cost, persons.hh_expansion_factor * persons.build_vehicles * ANNUAL_COST_PER_VEHICLE / persons.hh_size
auto ownership cost benefit,diff_auto_ownership_cost, build_auto_ownership_cost - base_auto_ownership_cost
6 changes: 3 additions & 3 deletions example/configs/demographics.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Target,Expression
# compare hh_income to federal poverty threshold based on hh_size
_poverty_threshold, @(persons.hh_size==1)*POVERTY_1 + (persons.hh_size>1)*POVERTY_2 + (persons.hh_size - 2).clip(lower=0)*POVERTY_N
coc_poverty,@persons.hh_income <= _poverty_threshold
coc_age, person_age > 65
_poverty_threshold, (persons.hh_size==1)*POVERTY_1 + (persons.hh_size>1)*POVERTY_2 + (persons.hh_size - 2).clip(lower=0)*POVERTY_N
coc_poverty, persons.hh_income <= _poverty_threshold
coc_age, persons.person_age > 65

0 comments on commit cd4c507

Please sign in to comment.