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

Allow non-binary incidence #123

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion populationsim/balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def np_balancer(
yy + relaxed_constraint / float(importance))

# update HH weights
weights_final[incidence[c] > 0] *= gamma[c]
weights_final *= pow(gamma[c], incidence[c])

# clip weights to upper and lower bounds
weights_final = np.clip(weights_final, weights_lower_bound, weights_upper_bound)
Expand Down
2 changes: 1 addition & 1 deletion populationsim/simul_balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def np_simul_balancer(
yy + (relaxed_constraint / float(importance)))

# update HH weights
sub_weights[z][incidence[c] > 0] *= gamma[z, c]
sub_weights[z] *= pow(gamma[z, c], incidence[c])

# clip weights to upper and lower bounds
sub_weights[z] = np.clip(sub_weights[z], weights_lower_bound, weights_upper_bound)
Expand Down
7 changes: 5 additions & 2 deletions populationsim/steps/sub_balancing.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,12 @@ def sub_balancing(settings, crosswalk, control_spec, incidence_table):
# only want ones for which there are (non-zero) controls
parent_ids = parent_controls_df.index.intersection(parent_ids)

for parent_id in parent_ids:
num_parent_ids = len(parent_ids)
for idx, parent_id in enumerate(parent_ids, start=1):

logger.info("balancing seed %s, %s %s" % (seed_id, parent_geography, parent_id))
log_msg = "balancing {}/{} seed {}, {} {}"
log_msg = log_msg.format(idx, num_parent_ids, seed_id, parent_geography, parent_id)
logger.info(log_msg)

initial_weights = weights_df[weights_df[parent_geography] == parent_id]
initial_weights = initial_weights.set_index(settings.get('household_id_col'))
Expand Down
4 changes: 2 additions & 2 deletions populationsim/tests/test_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def teardown_function(func):


TAZ_COUNT = 36
TAZ_100_HH_COUNT = 25
TAZ_100_HH_COUNT = 33
TAZ_100_HH_REPOP_COUNT = 26


Expand All @@ -51,7 +51,7 @@ def test_full_run1():
'meta_control_factoring',
'final_seed_balancing',
'integerize_final_seed_weights',
'sub_balancing.geography = TRACT',
'sub_balancing.geography=TRACT',
'sub_balancing.geography=TAZ',
'expand_households',
'summarize',
Expand Down
12 changes: 6 additions & 6 deletions scripts/validation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
" packages = 'pyyaml pandas numpy matplotlib'\n",
" ret = os.system(f'conda install {packages}')\n",
" if ret != 0:\n",
" os.system(f'pip install {packages}')"
" os.system(f'{sys.executable} -m pip install {packages}')"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't call pip or python directly in case the user path picks a different python than the one running the notebook.

]
},
{
Expand Down Expand Up @@ -840,7 +840,7 @@
}
],
"source": [
"def meta_geog_df(meta_geog):\n",
"def meta_geog_df(summary_df, meta_geog):\n",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modify the methods to not use global variables and instead pass requisite data as arguments.

" geography_df = pd.read_csv(os.path.join(popsim_dir, geography_file))\n",
" geog = use_geographies[use_geographies.index(meta_geog) + 1] # next geography in list\n",
" meta_df = geography_df[[meta_geog, geog]].drop_duplicates(ignore_index=True)\n",
Expand All @@ -852,7 +852,7 @@
" \n",
"for geog in use_geographies:\n",
" if not geog in summary_df.geography.unique():\n",
" summary_df = summary_df.append(meta_geog_df(geog))\n",
" summary_df = summary_df.append(summary_df, meta_geog_df(geog))\n",
"\n",
"summary_df.tail()"
]
Expand All @@ -870,7 +870,7 @@
"metadata": {},
"outputs": [],
"source": [
"def process_control(name, geography, control, result):\n",
"def process_control(summary_df, name, geography, control, result):\n",
" \"\"\"\n",
" Global\n",
" ------\n",
Expand Down Expand Up @@ -948,12 +948,12 @@
"\n",
"stats = []\n",
"for params, ax in zip(aggregate_list, axes.ravel()):\n",
" s, f = process_control(**params)\n",
" s, f = process_control(summary_df, **params)\n",
" stats.append(s)\n",
" \n",
" ax.set_title(f\"{params['geography']} - {params['name']}\")\n",
" ax.set_ylabel('Frequency'); ax.set_xlabel('Difference: control vs. result')\n",
" ax.scatter(f.index, f)\n",
" ax.hist(diff, bins=10, range=(-5,5), alpha=0.5)\n",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a histogram plot, rather than a scatter plot, to show the histogram.

"\n",
"summary_fig.savefig(os.path.join(validation_dir, 'frequencies.pdf'))\n",
"stats_df = pd.DataFrame(stats)\n",
Expand Down