Skip to content
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

general-updates #2

Merged
merged 2 commits into from
Jun 13, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions variable_generators/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def make_agg_var(agent, geog, geog_id, var_to_aggregate, agg_function):
"""
var_name = agg_function + '_' + var_to_aggregate

@orca.column(geog, var_name, cache=False)
@orca.column(geog, var_name, cache=True, cache_scope='iteration')
def func():
agents = orca.get_table(agent)
print('Calculating {} of {} for {}'
Expand Down Expand Up @@ -54,13 +54,16 @@ def func():


def make_disagg_var(from_geog_name, to_geog_name, var_to_disaggregate,
from_geog_id_name):
from_geog_id_name, name_based_on_geography=True):
"""
Generator function for disaggregating variables. Registers with orca.
"""
var_name = from_geog_name + '_' + var_to_disaggregate
if name_based_on_geography:
var_name = from_geog_name + '_' + var_to_disaggregate
else:
var_name = var_to_disaggregate

@orca.column(to_geog_name, var_name, cache=False)
@orca.column(to_geog_name, var_name, cache=True, cache_scope='iteration')
def func():
print('Disaggregating {} to {} from {}'
.format(var_to_disaggregate, to_geog_name, from_geog_name))
Expand All @@ -79,7 +82,7 @@ def make_size_var(agent, geog, geog_id):
"""
var_name = 'total_' + agent

@orca.column(geog, var_name, cache=False)
@orca.column(geog, var_name, cache=True, cache_scope='iteration')
def func():
agents = orca.get_table(agent)
print('Calculating number of {} for {}'.format(agent, geog))
Expand All @@ -101,7 +104,7 @@ def make_proportion_var(agent, geog, geog_id, target_variable, target_value):
"""
var_name = 'prop_%s_%s' % (target_variable, int(target_value))

@orca.column(geog, var_name, cache=False)
@orca.column(geog, var_name, cache=True, cache_scope='iteration')
def func():
agents = orca.get_table(agent).to_frame(
columns=[target_variable, geog_id])
Expand All @@ -123,9 +126,13 @@ def make_dummy_variable(agent, geog_var, geog_id):
"""
Generator function for spatial dummy. Registers with orca.
"""
var_name = '{}_is_{}'.format(geog_var, str(int(geog_id)))
# cache_scope
try:
var_name = geog_var + '_is_' + str(geog_id)
except:
var_name = geog_var + '_is_' + str(int(geog_id))

@orca.column(agent, var_name, cache=True)
@orca.column(agent, var_name, cache=True, cache_scope='iteration')
def func():
agents = orca.get_table(agent)
return (agents[geog_var] == geog_id).astype('int32')
Expand All @@ -139,7 +146,7 @@ def make_ratio_var(agent1, agent2, geog):
"""
var_name = 'ratio_%s_to_%s' % (agent1, agent2)

@orca.column(geog, var_name, cache=False)
@orca.column(geog, var_name, cache=True, cache_scope='iteration')
def func():
locations = orca.get_table(geog)
print('Calculating ratio of {} to {} for {}'
Expand All @@ -154,6 +161,28 @@ def func():
return func


def make_density_var(agent, geog):
"""
Generator function for density variables. Registers with orca.
"""
var_name = 'density_%s' % (agent)

@orca.column(geog, var_name, cache=True, cache_scope='iteration')
def func():
locations = orca.get_table(geog)
print('Calculating density of {} for {}'.format(agent, geog))
if geog != 'blocks':
series = locations['total_' + agent] * 1.0 / (
locations['sum_acres'] + 1.0)
else:
series = locations['total_' + agent] * 1.0 / (
locations['acres'] + 1.0)
series = series.fillna(0)
return series

return func


def make_access_var(name, agent, target_variable=False, target_value=False,
radius=1000, agg_function='sum', decay='flat', log=True,
filters=False):
Expand Down