<jupyter_start><jupyter_text>UrbanSim Templates demo<NAME>, Feb 2021 Background[UrbanSim](https://github.com/udst/urbansim) is a platform for modeling land use in cities. It runs in Python and uses the [Orca](https://github.com/udst/orca) task orchestration system. Orca breaks a model into "steps", Python functions that can be assembled on the fly into linear or cyclical pipelines. (Typically each step is a statistical model capturing one aspect of the dynamics being studied.) Orca is designed for workflows like city simulation where the data representing a model's state is so large that it needs to be managed outside the task graph. Steps refer to tables and columns of data by name rather than passing the data directly.UrbanSim [Templates](https://github.com/udst/urbansim_templates) is a library that provides automated building blocks for Orca-based models. The templates were developed to reduce the need for custom code and improve the portability of model components.Currently we have templates for (a) regression, (b) binary logit, (c) multinomial logit estimated with [PyLogit](https://github.com/timothyb0912/pylogit) (best choice for flexible utility expressions), and (d) multinomial logit estimated with [ChoiceModels](https://github.com/udst/choicemodels) (best choice for sampling of interchangeable alternatives). DocumentationFull UrbanSim Templates documentation: https://udst.github.io/urbansim_templates/ InstallationYou can install `orca` and `urbansim_templates` with Pip or from Conda Forge.<jupyter_code>import pandas as pd

print(pd.__version__)
import orca

print(orca.__version__)
import urbansim_templates

print(urbansim_templates.__version__)
# This makes the notebook output clearer
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)<jupyter_output><empty_output><jupyter_text>Setting up ModelManager[ModelManager](https://udst.github.io/urbansim_templates/modelmanager.html) is part of the Templates library. It's an extension to Orca for saving and loading template-based model steps. By default it will look for a folder named `configs` in your current working directory, where it will read and save yaml representations of model steps. If there are already model steps there, the corresponding template classes need to be loaded before initializing ModelManager.<jupyter_code>from urbansim_templates.models import OLSRegressionStep
from urbansim_templates import modelmanager

modelmanager.initialize()<jupyter_output>No yaml files found in path 'configs'<jupyter_text>Setting up dataWe'll load a DataFrame and register it with Orca, so that our statistical models can refer to it.<jupyter_code>df = pd.read_csv('data/buildings-demo.csv').dropna()
len(df)
orca.add_table('buildings', df)
orca.get_table('buildings').to_frame().head()<jupyter_output><empty_output><jupyter_text>Fitting a modelNow we can choose a [template](https://udst.github.io/urbansim_templates/model-steps.html) and use it to fit a model.<jupyter_code>from urbansim_templates.models import OLSRegressionStep

m = OLSRegressionStep()
m.name = 'price-prediction'
m.tables = 'buildings'
m.model_expression = 'np.log1p(res_price_per_sqft) ~ non_residential_sqft>0 + year_built<1960'
m.fit()<jupyter_output>OLS Regression Results                                 
========================================================================================
Dep. Variable:     np.log1p(res_price_per_sqft)   R-squared:                       0.398
Model:                                      OLS   Adj. R-squared:                  0.395
Method:                           Least Squares   F-statistic:                     158.1
Date:                          Tue, 09 Feb 2021   Prob (F-statistic):           1.93e-53
Time:                                  12:02:09   Log-Likelihood:                -598.98
No. Observations:                           482   AIC:                             1204.
Df Residuals:                               479   BIC:                             1216.
Df Model:                                     2                                         
Covariance Type:                      nonrobust                                         
=====================[...]<jupyter_text>Registering the stepWhen we're happy with the specification, we can "register" the step with ModelManager. This saves a copy to disk and also passes it to Orca so it can be run as part of a sequence of other steps for validation or simulation.<jupyter_code>modelmanager.register(m)<jupyter_output>Saving 'price-prediction.yaml': /Users/maurer/Dropbox/Git-imac/udst/urbansim_templates/examples/configs
Registering model step 'price-prediction'<jupyter_text>Making changesPreviously registered steps can be retrieved, modified, and re-registered as needed.<jupyter_code>modelmanager.list_steps()
m2 = modelmanager.get_step('price-prediction')
m2.name = 'better-price-prediction'
# here you can edit the specification and re-fit, etc.

modelmanager.register(m2)
modelmanager.remove_step('better-price-prediction')<jupyter_output>Removing 'better-price-prediction' and 'better-price-prediction.yaml'
{'fraction_non_alphanumeric': 0.0944206008583691, 'fraction_numerical': 0.013070620366757706, 'mean_word_length': 3.54924578527063, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '351578', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1451, 'n_tokens_neox': 1358, 'n_words': 461, 'path': 'examples/UrbanSim-Templates-demo.ipynb', 'repo_name': 'AZMAG/urbansim_templates', 'size': '5130'}