-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from UDST/col-from-expression
[0.2.dev5] Template for column from expression
- Loading branch information
Showing
8 changed files
with
405 additions
and
9 deletions.
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
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
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
|
||
setup( | ||
name='urbansim_templates', | ||
version='0.2.dev4', | ||
version='0.2.dev5', | ||
description='UrbanSim extension for managing model steps', | ||
author='UrbanSim Inc.', | ||
author_email='[email protected]', | ||
|
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,191 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
import orca | ||
|
||
from urbansim_templates import modelmanager | ||
from urbansim_templates.data import ColumnFromExpression | ||
from urbansim_templates.utils import validate_template | ||
|
||
|
||
@pytest.fixture | ||
def orca_session(): | ||
""" | ||
Set up a clean Orca and ModelManager session, with a data table. | ||
""" | ||
orca.clear_all() | ||
modelmanager.initialize() | ||
|
||
d1 = {'id': np.arange(5), | ||
'a': np.random.random(5), | ||
'b': np.random.choice(np.arange(20), size=5)} | ||
|
||
df = pd.DataFrame(d1).set_index('id') | ||
orca.add_table('obs', df) | ||
|
||
|
||
def test_template_validity(): | ||
""" | ||
Check template conforms to basic spec. | ||
""" | ||
assert validate_template(ColumnFromExpression) | ||
|
||
|
||
def test_missing_colname(orca_session): | ||
""" | ||
Missing column_name should raise a ValueError. | ||
""" | ||
c = ColumnFromExpression() | ||
c.table = 'tab' | ||
c.expression = 'a' | ||
|
||
try: | ||
c.run() | ||
except ValueError as e: | ||
print(e) | ||
return | ||
|
||
pytest.fail() | ||
|
||
|
||
def test_missing_table(orca_session): | ||
""" | ||
Missing table should raise a ValueError. | ||
""" | ||
c = ColumnFromExpression() | ||
c.column_name = 'col' | ||
c.expression = 'a' | ||
|
||
try: | ||
c.run() | ||
except ValueError as e: | ||
print(e) | ||
return | ||
|
||
pytest.fail() | ||
|
||
|
||
def test_missing_expression(orca_session): | ||
""" | ||
Missing expression should raise a ValueError. | ||
""" | ||
c = ColumnFromExpression() | ||
c.column_name = 'col' | ||
c.table = 'tab' | ||
|
||
try: | ||
c.run() | ||
except ValueError as e: | ||
print(e) | ||
return | ||
|
||
pytest.fail() | ||
|
||
|
||
def test_expression(orca_session): | ||
""" | ||
Check that column is created and expression evaluated correctly. | ||
""" | ||
c = ColumnFromExpression() | ||
c.column_name = 'c' | ||
c.table = 'obs' | ||
c.expression = 'a * 5 + sqrt(b)' | ||
|
||
c.run() | ||
|
||
val1 = orca.get_table('obs').get_column('c') | ||
df = orca.get_table('obs').to_frame() | ||
val2 = df.a * 5 + np.sqrt(df.b) | ||
assert(val1.equals(val2)) | ||
|
||
|
||
def test_data_type(orca_session): | ||
""" | ||
Check that casting data type works. | ||
""" | ||
orca.add_table('tab', pd.DataFrame({'a': [0.1, 1.33, 2.4]})) | ||
|
||
c = ColumnFromExpression() | ||
c.column_name = 'b' | ||
c.table = 'tab' | ||
c.expression = 'a' | ||
c.run() | ||
|
||
v1 = orca.get_table('tab').get_column('b').values | ||
np.testing.assert_equal(v1, [0.1, 1.33, 2.4]) | ||
|
||
c.data_type = 'int' | ||
c.run() | ||
|
||
v1 = orca.get_table('tab').get_column('b').values | ||
np.testing.assert_equal(v1, [0, 1, 2]) | ||
|
||
|
||
def test_missing_values(orca_session): | ||
""" | ||
Check that filling in missing values works. | ||
""" | ||
orca.add_table('tab', pd.DataFrame({'a': [0.1, np.nan, 2.4]})) | ||
|
||
c = ColumnFromExpression() | ||
c.column_name = 'b' | ||
c.table = 'tab' | ||
c.expression = 'a' | ||
c.run() | ||
|
||
v1 = orca.get_table('tab').get_column('b').values | ||
np.testing.assert_equal(v1, [0.1, np.nan, 2.4]) | ||
|
||
c.missing_values = 5 | ||
c.run() | ||
|
||
v1 = orca.get_table('tab').get_column('b').values | ||
np.testing.assert_equal(v1, [0.1, 5.0, 2.4]) | ||
|
||
|
||
def test_modelmanager_registration(orca_session): | ||
""" | ||
Check that modelmanager registration and auto-run work as expected. | ||
""" | ||
c = ColumnFromExpression() | ||
c.column_name = 'c' | ||
c.table = 'obs' | ||
c.expression = 'a + b' | ||
|
||
modelmanager.register(c) | ||
modelmanager.remove_step(c.name) | ||
assert('c' in orca.get_table('obs').columns) | ||
|
||
|
||
def test_expression_with_standalone_columns(orca_session): | ||
""" | ||
Check that expression can assemble data from stand-alone columns that are not part | ||
of the core DataFrame wrapped by a table. | ||
""" | ||
c = ColumnFromExpression() | ||
c.column_name = 'c' | ||
c.table = 'obs' | ||
c.expression = 'a + b' | ||
|
||
modelmanager.register(c) | ||
modelmanager.remove_step(c.name) | ||
|
||
d = ColumnFromExpression() | ||
d.column_name = 'd' | ||
d.table = 'obs' | ||
d.expression = 'a + c' | ||
|
||
d.run() | ||
assert('d' in orca.get_table('obs').columns) | ||
|
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 +1 @@ | ||
version = __version__ = '0.2.dev4' | ||
version = __version__ = '0.2.dev5' |
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,2 +1,3 @@ | ||
from .column_from_expression import ColumnFromExpression | ||
from .load_table import LoadTable | ||
from .save_table import SaveTable |
Oops, something went wrong.