Skip to content

Commit

Permalink
Adding tests for the UnboundLocalError
Browse files Browse the repository at this point in the history
  • Loading branch information
saroele committed May 15, 2018
1 parent 996134e commit 8dd0c28
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
12 changes: 6 additions & 6 deletions opengrid/library/regression.py
Expand Up @@ -30,8 +30,8 @@ class MultiVarLinReg(Analysis):
The analysis is based on a forward-selection approach: starting from a simple model, the model is iteratively
refined and verified until no statistical relevant improvements can be obtained. Each model in the iteration loop
is stored in the attribute self._list_of_fits. The selected model is self._fit (=pointer to the last element of
self._list_of_fits).
is stored in the attribute self.list_of_fits. The selected model is self.fit (=pointer to the last element of
self.list_of_fits).
The dataframe can contain daily, weekly, monthly, yearly ... values. Each row is an instance.
Expand Down Expand Up @@ -105,7 +105,7 @@ def list_of_fits(self):

def do_analysis(self):
"""
Find the best model (fit) and create self._list_of_fits and self._fit
Find the best model (fit) and create self.list_of_fits and self.fit
"""
if self.cross_validation:
Expand All @@ -115,7 +115,7 @@ def do_analysis(self):

def _do_analysis_no_cross_validation(self):
"""
Find the best model (fit) and create self._list_of_fits and self._fit
Find the best model (fit) and create self.list_of_fits and self.fit
"""

# first model is just the mean
Expand Down Expand Up @@ -356,7 +356,7 @@ def add_prediction(self):
-------
Nothing, adds columns to self.df
"""
self.df = self._predict(fit=self._fit, df=self.df)
self.df = self._predict(fit=self.fit, df=self.df)

def plot(self, model=True, bar_chart=True, **kwargs):
"""
Expand Down Expand Up @@ -388,7 +388,7 @@ def plot(self, model=True, bar_chart=True, **kwargs):
"""
plot_style()
figures = []
fit = kwargs.get('fit', self._fit)
fit = kwargs.get('fit', self.fit)
df = kwargs.get('df', self.df)

if not 'predicted' in df.columns:
Expand Down
17 changes: 11 additions & 6 deletions opengrid/tests/test_regression.py
Expand Up @@ -29,12 +29,17 @@ def test_init(self):
mvlr.do_analysis()
self.assertTrue(hasattr(mvlr, 'list_of_fits'))

# def test_raises(self):
# df = datasets.get('gas_2016_hour')
# df_month = df.resample('MS').sum()
# mvlr = og.MultiVarLinReg(df_month, '313b', p_max=0.04)
# self.assertRaises(UnboundLocalError, mvlr.fit)
# self.assertRaises(UnboundLocalError, mvlr.list_of_fits)
def test_raises(self):
df = datasets.get('gas_2016_hour')
df_month = df.resample('MS').sum()
mvlr = og.MultiVarLinReg(df_month, '313b', p_max=0.04)
self.assertRaises(UnboundLocalError, mvlr.add_prediction)
try:
x = mvlr.list_of_fits
self.assertTrue(False)
except UnboundLocalError:
self.assertTrue(True)


def test_strange_names(self):
df = datasets.get('gas_2016_hour')
Expand Down

0 comments on commit 8dd0c28

Please sign in to comment.