-
Notifications
You must be signed in to change notification settings - Fork 100
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
Adding defaults #4
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
99dde26
addings some basic data sources
fscottfoti 09cfe10
all basic variables implemented for auto ownership
fscottfoti c113280
auto ownership is simulating
fscottfoti 6692d0d
performance improvements
fscottfoti a8a9c7e
adding columns (now 88) to the auto ownership model
fscottfoti 109cd47
just figured out that the variables like AUTOPEAKRETAIL are in access…
fscottfoti d72c2c1
add the activitysim.py module too
fscottfoti 4e87889
pep8
fscottfoti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
example/data/* | ||
|
||
.ipynb_checkpoints | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import urbansim.sim.simulation as sim | ||
from urbansim.urbanchoice import interaction, mnl | ||
import pandas as pd | ||
import numpy as np | ||
import os | ||
|
||
|
||
def random_rows(df, n): | ||
return df.take(np.random.choice(len(df), size=n, replace=False)) | ||
|
||
|
||
def read_model_spec(fname, | ||
description_name="Description", | ||
expression_name="Expression"): | ||
""" | ||
Read in the excel file and reformat for machines | ||
""" | ||
cfg = pd.read_csv(fname) | ||
# don't need description and set the expression to the index | ||
cfg = cfg.drop(description_name, axis=1).set_index(expression_name).stack() | ||
return cfg | ||
|
||
|
||
def identity_matrix(alt_names): | ||
return pd.DataFrame(np.identity(len(alt_names)), | ||
columns=alt_names, | ||
index=alt_names) | ||
|
||
|
||
def simple_simulate(choosers, alternatives, spec): | ||
exprs = spec.index | ||
coeffs = spec.values | ||
|
||
# merge choosers and alternatives | ||
_, df, _ = interaction.mnl_interaction_dataset( | ||
choosers, alternatives, len(alternatives)) | ||
|
||
# evaluate the expressions to build the final matrix | ||
vars, names = [], [] | ||
for expr in exprs: | ||
if expr[0][0] == "@": | ||
expr = "({}) * df.{}".format(expr[0][1:], expr[1]) | ||
try: | ||
s = eval(expr) | ||
except Exception as e: | ||
print "Failed with Python eval:\n%s" % expr | ||
raise e | ||
else: | ||
expr = "({}) * {}".format(*expr) | ||
try: | ||
s = df.eval(expr) | ||
except Exception as e: | ||
print "Failed with DataFrame eval:\n%s" % expr | ||
raise e | ||
names.append(expr) | ||
vars.append(s) | ||
model_design = pd.concat(vars, axis=1) | ||
model_design.columns = names | ||
|
||
df = random_rows(model_design, 100000).describe().transpose() | ||
df = df[df["std"] == 0] | ||
if len(df): | ||
print "WARNING: Describe of columns with no variability:\n", df | ||
|
||
choices = mnl.mnl_simulate( | ||
model_design.as_matrix(), | ||
coeffs, | ||
numalts=len(alternatives), | ||
returnprobs=False) | ||
|
||
return pd.Series(choices, index=choosers.index), model_design |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import os | ||
import uuid | ||
import yaml | ||
from urbansim.utils import misc | ||
import urbansim.sim.simulation as sim | ||
from .. import activitysim as asim | ||
|
||
import warnings | ||
|
||
warnings.filterwarnings('ignore', category=pd.io.pytables.PerformanceWarning) | ||
pd.options.mode.chained_assignment = None | ||
|
||
|
||
@sim.injectable('settings', cache=True) | ||
def settings(): | ||
with open(os.path.join(misc.configs_dir(), "settings.yaml")) as f: | ||
settings = yaml.load(f) | ||
# monkey patch on the settings object since it's pretty global | ||
# but will also be available as injectable | ||
sim.settings = settings | ||
return settings | ||
|
||
|
||
@sim.injectable('run_number') | ||
def run_number(): | ||
return misc.get_run_number() | ||
|
||
|
||
@sim.injectable('uuid', cache=True) | ||
def uuid_hex(): | ||
return uuid.uuid4().hex | ||
|
||
|
||
@sim.injectable('store', cache=True) | ||
def hdfstore(settings): | ||
return pd.HDFStore( | ||
os.path.join(misc.data_dir(), settings["store"]), | ||
mode='r') | ||
|
||
|
||
@sim.injectable("scenario") | ||
def scenario(settings): | ||
return settings["scenario"] | ||
|
||
|
||
@sim.table(cache=True) | ||
def land_use(store): | ||
return store["land_use/taz_data"] | ||
|
||
|
||
@sim.table(cache=True) | ||
def accessibility(store): | ||
df = store["skims/accessibility"] | ||
df.columns = [c.upper() for c in df.columns] | ||
return df | ||
|
||
|
||
@sim.table(cache=True) | ||
def households(store, settings): | ||
if "households_sample_size" in settings: | ||
return asim.random_rows(store["households"], | ||
settings["households_sample_size"]) | ||
return store["households"] | ||
|
||
|
||
@sim.table(cache=True) | ||
def persons(store): | ||
return store["persons"] | ||
|
||
|
||
sim.broadcast('land_use', 'households', cast_index=True, onto_on='TAZ') | ||
sim.broadcast('accessibility', 'households', cast_index=True, onto_on='TAZ') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import urbansim.sim.simulation as sim | ||
from activitysim.defaults import datasources | ||
|
||
|
||
@sim.column("households") | ||
def income_in_thousands(households): | ||
return households.income / 1000 | ||
|
||
|
||
@sim.column("households") | ||
def drivers(households, persons): | ||
# we assume that everyone 16 and older is a potential driver | ||
return persons.local.query("16 <= age").\ | ||
groupby("household_id").size().\ | ||
reindex(households.index).fillna(0) | ||
|
||
|
||
@sim.column("households") | ||
def num_young_children(households, persons): | ||
return persons.local.query("age <= 4").\ | ||
groupby("household_id").size().\ | ||
reindex(households.index).fillna(0) | ||
|
||
|
||
@sim.column("households") | ||
def num_children(households, persons): | ||
return persons.local.query("5 <= age <= 15").\ | ||
groupby("household_id").size().\ | ||
reindex(households.index).fillna(0) | ||
|
||
|
||
@sim.column("households") | ||
def num_adolescents(households, persons): | ||
return persons.local.query("16 <= age <= 17").\ | ||
groupby("household_id").size().\ | ||
reindex(households.index).fillna(0) | ||
|
||
|
||
@sim.column("households") | ||
def num_college_age(households, persons): | ||
return persons.local.query("18 <= age <= 24").\ | ||
groupby("household_id").size().\ | ||
reindex(households.index).fillna(0) | ||
|
||
|
||
@sim.column("households") | ||
def num_young_adults(households, persons): | ||
return persons.local.query("25 <= age <= 34").\ | ||
groupby("household_id").size().\ | ||
reindex(households.index).fillna(0) | ||
|
||
|
||
@sim.column("land_use") | ||
def household_density(land_use): | ||
return land_use.total_households / land_use.total_acres | ||
|
||
|
||
@sim.column("land_use") | ||
def employment_density(land_use): | ||
return land_use.total_employment / land_use.total_acres | ||
|
||
|
||
@sim.column("land_use") | ||
def density_index(land_use): | ||
return (land_use.household_density * land_use.employment_density) / \ | ||
(land_use.household_density + land_use.employment_density) | ||
|
||
|
||
@sim.column("land_use") | ||
def county_name(land_use, settings): | ||
assert "county_map" in settings | ||
inv_map = {v: k for k, v in settings["county_map"].items()} | ||
return land_use.county_id.map(inv_map) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
store: mtc_asim.h5 | ||
|
||
households_sample_size: 100000 | ||
|
||
county_map: | ||
San Francisco: 1 | ||
San Mateo: 2 | ||
Santa Clara: 3 | ||
Alameda: 4 | ||
Contra Costa: 5 | ||
Solano: 6 | ||
Napa: 7 | ||
Sonoma: 8 | ||
Marin: 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Keep data here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import urbansim.sim.simulation as sim | ||
import os | ||
from activitysim import activitysim as asim | ||
|
||
|
||
@sim.table() | ||
def auto_alts(): | ||
return asim.identity_matrix(["cars%d" % i for i in range(5)]) | ||
|
||
|
||
@sim.injectable() | ||
def auto_ownership_spec(): | ||
f = os.path.join('configs', "auto_ownership_coeffs.csv") | ||
return asim.read_model_spec(f).head(4*26) | ||
|
||
|
||
@sim.model() | ||
def auto_ownership_simulate(households, | ||
auto_alts, | ||
auto_ownership_spec, | ||
land_use, | ||
accessibility): | ||
|
||
choosers = sim.merge_tables(households.name, tables=[households, | ||
land_use, | ||
accessibility]) | ||
alternatives = auto_alts.to_frame() | ||
|
||
choices, model_design = \ | ||
asim.simple_simulate(choosers, alternatives, auto_ownership_spec) | ||
|
||
print "Choices:\n", choices.value_counts() | ||
sim.add_column("households", "auto_ownership", choices) | ||
|
||
return model_design |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
excel!?! No thanks!