Skip to content

Commit

Permalink
Merge pull request #656 from hdoupe/elastic_submit_bugfix
Browse files Browse the repository at this point in the history
Elastic submit bugfix
  • Loading branch information
brittainhard authored Sep 25, 2017
2 parents f534f2e + bf1736c commit 6f37800
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 84 deletions.
102 changes: 28 additions & 74 deletions webapp/apps/dynamic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
DynamicElasticitySaveInputs, DynamicElasticityOutputUrl)
from ..taxbrain.models import TaxSaveInputs, OutputUrl, ErrorMessageTaxCalculator
from ..taxbrain.views import (growth_fixup, benefit_switch_fixup, make_bool, dropq_compute,
JOB_PROC_TIME_IN_SECONDS, get_reform_from_gui)
JOB_PROC_TIME_IN_SECONDS, get_reform_from_gui,
parse_fields)
from ..taxbrain.helpers import (default_policy, taxcalc_results_to_tables, default_behavior,
convert_val, to_json_reform)

Expand Down Expand Up @@ -188,6 +189,7 @@ def dynamic_behavioral(request, pk):
This view handles the dynamic behavioral input page and calls the function that
handles the calculation on the inputs.
"""
start_year = request.REQUEST.get('start_year')
if request.method == 'POST':
# Client is attempting to send inputs, validate as form data
fields = dict(request.REQUEST)
Expand All @@ -198,25 +200,13 @@ def dynamic_behavioral(request, pk):

if dyn_mod_form.is_valid():
model = dyn_mod_form.save()

curr_dict = dict(model.__dict__)
for key, value in curr_dict.items():
print "got this ", key, value

for key, value in curr_dict.items():
if type(value) == type(unicode()):
curr_dict[key] = [convert_val(x) for x in value.split(',') if x]
else:
print "missing this: ", key

# get macrosim data from form
worker_data = {k:v for k, v in curr_dict.items() if v not in (u'', None, [])}
worker_data = parse_fields(curr_dict)

#get microsim data
outputsurl = OutputUrl.objects.get(pk=pk)
model.micro_sim = outputsurl
taxbrain_model = outputsurl.unique_inputs
is_file = False
# necessary for simulations before PR 641
if not taxbrain_model.json_text:
(reform_dict, assumptions_dict, _, _,
Expand All @@ -226,7 +216,6 @@ def dynamic_behavioral(request, pk):
behavior_model=model
)
else:
is_file = True
reform_dict = json.loads(taxbrain_model.json_text.reform_text)
(_, assumptions_dict, _, _,
errors_warnings) = get_reform_from_gui(
Expand All @@ -238,9 +227,9 @@ def dynamic_behavioral(request, pk):
submitted_ids, max_q_length = dropq_compute.submit_dropq_calculation(
reform_dict,
int(start_year),
is_file=is_file,
is_file=False,
additional_data=assumptions_dict,
package_up_user_mods=False
pack_up_user_mods=False
)

if not submitted_ids:
Expand Down Expand Up @@ -280,11 +269,10 @@ def dynamic_behavioral(request, pk):
form_personal_exemp = dyn_mod_form

else:

# Probably a GET request, load a default form
start_year = request.REQUEST.get('start_year')
form_personal_exemp = DynamicBehavioralInputsModelForm(first_year=start_year)


behavior_default_params = default_behavior(int(start_year))

init_context = {
Expand All @@ -307,11 +295,7 @@ def dynamic_elasticities(request, pk):
This view handles the dynamic macro elasticities input page and
calls the function that handles the calculation on the inputs.
"""

# Probably a GET request, load a default form
start_year = request.REQUEST.get('start_year')
elasticity_default_params = default_elasticity_parameters(int(start_year))

if request.method=='POST':
# Client is attempting to send inputs, validate as form data
fields = dict(request.REQUEST)
Expand All @@ -323,64 +307,33 @@ def dynamic_elasticities(request, pk):
model = dyn_mod_form.save()

curr_dict = dict(model.__dict__)
for key, value in curr_dict.items():
print "got this ", key, value

#Replace empty elasticity field with defaults
for k,v in elasticity_default_params.items():
if k in curr_dict and not curr_dict[k]:
curr_dict[k] = elasticity_default_params[k].col_fields[0].values

for key, value in curr_dict.items():
if type(value) == type(unicode()):
try:
curr_dict[key] = [float(x) for x in value.split(',') if x]
except ValueError:
curr_dict[key] = [make_bool(x) for x in value.split(',') if x]
else:
print "missing this: ", key


# get macrosim data from form
worker_data = {k:v for k, v in curr_dict.items() if v not in (u'', None, [])}
worker_data = parse_fields(curr_dict)

#get microsim data
outputsurl = OutputUrl.objects.get(pk=pk)
model.micro_sim = outputsurl
taxbrain_model = outputsurl.unique_inputs
# necessary for simulations before PR 641
if not taxbrain_model.json_text:
taxbrain_dict = dict(taxbrain_model.__dict__)
growth_fixup(taxbrain_dict)
for key, value in taxbrain_dict.items():
if type(value) == type(unicode()):
taxbrain_dict[key] = [convert_val(x) for x in value.split(',') if x]
else:
print "missing this: ", key

microsim_data = {k:v for k, v in taxbrain_dict.items() if not (v == [] or v == None)}

#Don't need to pass around the microsim results
if 'tax_result' in microsim_data:
del microsim_data['tax_result']

benefit_switch_fixup(request.REQUEST, microsim_data, taxbrain_model)
microsim_data.update(worker_data)
# start calc job
submitted_ids, max_q_length = dropq_compute.submit_elastic_calculation(microsim_data,
int(start_year))

else:
el_keys = ('first_year', 'elastic_gdp')
elasticity_params = { k:v for k, v in worker_data.items() if k in el_keys}
additional_data = {'elasticity_params': elasticity_params}
reform_text = json.loads(taxbrain_model.json_text.reform_text)
reform_text[reform_text.keys()[0]]["elastic_gdp"] = elasticity_params["elastic_gdp"]
submitted_ids, max_q_length = dropq_compute.submit_elastic_calculation(
reform_text,
int(start_year),
is_file=True
(reform_dict, _, _, _,
errors_warnings) = get_reform_from_gui(
request,
taxbrain_model=taxbrain_model,
behavior_model=None
)
else:
reform_dict = json.loads(taxbrain_model.json_text.reform_text)

min_year = min(reform_dict.keys(), key=float)
reform_dict[min_year]['elastic_gdp'] = worker_data['elastic_gdp']

submitted_ids, max_q_length = dropq_compute.submit_elastic_calculation(
reform_dict,
int(start_year),
is_file=False,
additional_data=None,
pack_up_user_mods=False
)

if not submitted_ids:
no_inputs = True
Expand Down Expand Up @@ -421,9 +374,10 @@ def dynamic_elasticities(request, pk):
form_personal_exemp = dyn_mod_form

else:

# Probably a GET request, load a default form
form_personal_exemp = DynamicElasticityInputsModelForm(first_year=start_year)

elasticity_default_params = default_elasticity_parameters(int(start_year))

init_context = {
'form': form_personal_exemp,
Expand Down
13 changes: 7 additions & 6 deletions webapp/apps/taxbrain/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ def submit_json_dropq_calculation(self, user_mods, first_budget_year, additional
additional_data=additional_data)

def submit_dropq_calculation(self, user_mods, first_budget_year, additional_data={}, is_file=False,
package_up_user_mods=True):
pack_up_user_mods=True):
url_template = "http://{hn}" + DROPQ_URL
return self.submit_calculation(user_mods, first_budget_year, url_template,
num_years=NUM_BUDGET_YEARS,
additional_data=additional_data,
pack_up_user_mods=package_up_user_mods)
pack_up_user_mods=pack_up_user_mods)

def submit_json_dropq_small_calculation(self, user_mods, first_budget_year):
url_template = "http://{hn}" + DROPQ_SMALL_URL
Expand All @@ -73,20 +73,21 @@ def submit_json_dropq_small_calculation(self, user_mods, first_budget_year):
pack_up_user_mods=False)

def submit_dropq_small_calculation(self, user_mods, first_budget_year, additional_data={}, is_file=False,
package_up_user_mods=True):
pack_up_user_mods=True):
url_template = "http://{hn}" + DROPQ_SMALL_URL
return self.submit_calculation(user_mods, first_budget_year, url_template,
num_years=NUM_BUDGET_YEARS_QUICK,
additional_data=additional_data,
increment_counter=False,
pack_up_user_mods=package_up_user_mods)
pack_up_user_mods=pack_up_user_mods)

def submit_elastic_calculation(self, user_mods, first_budget_year, is_file=False, additional_data={}):
def submit_elastic_calculation(self, user_mods, first_budget_year, is_file=False, additional_data={},
pack_up_user_mods=True):
url_template = "http://{hn}/elastic_gdp_start_job"
return self.submit_calculation(user_mods, first_budget_year, url_template,
start_budget_year=1,
additional_data=additional_data,
pack_up_user_mods=not is_file)
pack_up_user_mods=pack_up_user_mods)


def submit_calculation(self, user_mods, first_budget_year, url_template,
Expand Down
2 changes: 1 addition & 1 deletion webapp/apps/taxbrain/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def convert_val(x):

def parse_fields(param_dict):
for k, v in param_dict.copy().items():
if v == u'' or v is None:
if v == u'' or v is None or v == []:
del param_dict[k]
continue
if type(v) == type(unicode()): #TODO: isinstance(value, unicode)
Expand Down
6 changes: 3 additions & 3 deletions webapp/apps/taxbrain/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,22 +408,22 @@ def submit_reform(request, user=None):
assert not taxcalc_errors
# try: # TODO: is try-catch necessary here?
log_ip(request)
# TODO: drop is_file and package_up_user_mods keywords
# TODO: drop is_file and pack_up_user_mods keywords
if do_full_calc:
submitted_ids, max_q_length = dropq_compute.submit_dropq_calculation(
reform_dict,
int(start_year),
is_file=is_file,
additional_data=assumptions_dict,
package_up_user_mods=False
pack_up_user_mods=False
)
else:
submitted_ids, max_q_length = dropq_compute.submit_dropq_small_calculation(
reform_dict,
int(start_year),
is_file=is_file,
additional_data=assumptions_dict,
package_up_user_mods=False
pack_up_user_mods=False
)
job_ids = denormalize(submitted_ids)
json_reform = JSONReformTaxCalculator()
Expand Down

0 comments on commit 6f37800

Please sign in to comment.