Skip to content

Commit

Permalink
Use RegressionModel.fit for consistency with LCM class.
Browse files Browse the repository at this point in the history
  • Loading branch information
jiffyclub committed Apr 29, 2014
1 parent f8d60fe commit 48860e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
27 changes: 21 additions & 6 deletions urbansim/models/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import statsmodels.formula.api as smf

from . import util
from .. exceptions import ModelEvaluationError
from ..exceptions import ModelEvaluationError


def fit_model(df, filters, model_expression):
Expand Down Expand Up @@ -106,7 +106,7 @@ def __init__(self, fit_filters, predict_filters, model_expression,
self.name = name or 'RegressionModel'
self.model_fit = None

def fit_model(self, data):
def fit(self, data):
"""
Fit the model to data and store/return the results.
Expand All @@ -127,6 +127,22 @@ class instance for use during prediction.
self.model_fit = fit
return fit

@property
def fitted(self):
"""
True if the model is ready for prediction.
"""
return bool(self.model_fit)

def assert_fitted(self):
"""
Raises a RuntimeError if the model is not ready for prediction.
"""
if not self.fitted:
raise RuntimeError('Model has not been fit.')

def predict(self, data):
"""
Predict a new data set based on an estimated model.
Expand All @@ -144,8 +160,7 @@ def predict(self, data):
after applying filters.
"""
if not self.model_fit:
raise RuntimeError('Model has not been fit.')
self.assert_fitted()
return predict(
data, self.predict_filters, self.model_fit, self.ytransform)

Expand Down Expand Up @@ -229,7 +244,7 @@ def _iter_groups(self, data):
for name in self.models:
yield name, groups.get_group(name)

def fit_models(self, data):
def fit(self, data):
"""
Fit each of the models in the group.
Expand All @@ -244,7 +259,7 @@ def fit_models(self, data):
Keys are the segment names.
"""
return {name: self.models[name].fit_model(df)
return {name: self.models[name].fit(df)
for name, df in self._iter_groups(data)}

def predict(self, data):
Expand Down
4 changes: 2 additions & 2 deletions urbansim/models/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_RegressionModel(test_df):
with pytest.raises(RuntimeError):
model.predict(test_df)

fit = model.fit_model(test_df)
fit = model.fit(test_df)
assert isinstance(fit, RegressionResultsWrapper)
assert isinstance(model.model_fit, RegressionResultsWrapper)

Expand All @@ -104,7 +104,7 @@ def test_RegressionModelGroup(groupby_df):
assert isinstance(hmg.models['y'], regression.RegressionModel)
assert hmg.models['y'].name == 'y'

fits = hmg.fit_models(groupby_df)
fits = hmg.fit(groupby_df)
assert isinstance(fits['x'], RegressionResultsWrapper)
assert isinstance(fits['y'], RegressionResultsWrapper)

Expand Down

0 comments on commit 48860e2

Please sign in to comment.