Skip to content

Commit

Permalink
LODES adrio2 refactor. (#148)
Browse files Browse the repository at this point in the history
* LODES adrio2 refactor.

* Minor issues addressed in LODES.
  • Loading branch information
meaghan66 authored Aug 20, 2024
1 parent e4bdd89 commit d8de7df
Show file tree
Hide file tree
Showing 4 changed files with 415 additions and 80 deletions.
169 changes: 90 additions & 79 deletions doc/devlog/2024-06-05.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,55 +44,70 @@
"source": [
"### **Basic Queries**\n",
"\n",
"The 'label' and 'commuters' are two simple yet imperative queries that show the basic functionality of the LODES ADRIO maker. The 'label' query represents the GEOIDs that are involved with the commuter matrices. The input given by the user for the scope, in this case being states, is translated into a list of GEOIDs. The 'commuters' query shows the total number of workers moving from a home GEOID to a work GEOID as a matrix. The matrix is read so that the rows represent the residence GEOID and the columns are the work location GEOID."
"The Commuters and Geoid calls are two simple yet imperative queries that show the basic functionality of the LODES ADRIO maker. The Geoid query represents the GEOIDs that are involved with the commuter matrices. The input given by the user for the scope, in this case being states, is translated into a list of GEOIDs. The Commuters query shows the total number of workers moving from a home GEOID to a work GEOID as a matrix. The matrix is read so that the rows represent the residence GEOID and the columns are the work location GEOID."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from unittest.mock import Mock\n",
"\n",
"import numpy as np\n",
"\n",
"from epymorph.data_shape import SimDimensions\n",
"from epymorph.geography.us_census import StateScope\n",
"from epymorph.simulation import NamespacedAttributeResolver\n",
"\n",
"state_scope = StateScope.in_states_by_code([\"AZ\", \"CO\", \"NV\", \"NM\"])\n",
"\n",
"data = Mock(spec=NamespacedAttributeResolver)\n",
"dim = Mock(spec=SimDimensions)\n",
"rng = Mock(spec=np.random.Generator)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from epymorph.geo.adrio.lodes import Commuters, Geoid\n",
"\n",
"time_period = 2015\n",
"commuters = Commuters(time_period)\n",
"geoids = Geoid()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Home/Work GEOIDs: ['04' '08' '32' '35']\n",
"Home/Work GEOIDs:\n",
" ['04' '08' '32' '35']\n",
"\n",
"Commuters Matrix:\n",
" [[2550132 2582 13263 8100]\n",
" [ 1202 2405258 382 5557]\n",
" [ 3552 535 1179411 361]\n",
" [ 6813 4824 409 764244]]\n"
" [ 6813 4824 409 764244]]\n",
"\n"
]
}
],
"source": [
"from epymorph.data_shape import Shapes\n",
"from epymorph.geo.adrio import adrio_maker_library\n",
"from epymorph.geo.dynamic import DynamicGeo\n",
"from epymorph.geo.spec import DynamicGeoSpec, Year\n",
"from epymorph.geography.us_census import StateScope\n",
"from epymorph.simulation import AttributeDef\n",
"\n",
"spec = DynamicGeoSpec(\n",
" attributes=[\n",
" AttributeDef('label', str, Shapes.N),\n",
" AttributeDef('commuters', int, Shapes.NxN),\n",
" ],\n",
" time_period=Year(2015),\n",
" scope=StateScope.in_states_by_code([\"AZ\", \"CO\", \"NV\", \"NM\"]),\n",
" source={\n",
" 'label': 'LODES:geoid',\n",
" 'commuters': 'LODES'\n",
" }\n",
")\n",
"\n",
"geo = DynamicGeo.from_library(spec, adrio_maker_library)\n",
"\n",
"print(\n",
" f\"Home/Work GEOIDs:\\n {geoids.evaluate_in_context(data, dim, state_scope, rng)}\\n\")\n",
"\n",
"print(f\"Home/Work GEOIDs: {geo['label']}\\n\")\n",
"\n",
"print(f\"Commuters Matrix:\\n {geo['commuters']}\")"
"print(\n",
" f\"Commuters Matrix:\\n {commuters.evaluate_in_context(data, dim, state_scope, rng)}\\n\")"
]
},
{
Expand All @@ -101,30 +116,30 @@
"source": [
"# Attributes\n",
"\n",
"The 'commuters' attribute outputs the total number of workers commuting, but LODES provides three categories for attributes specifying the type of workers: Age, Monthly Income, and Industry Sectors. Within each category, there are three ranges within them, and the sum of the ranges equals the total number of workers. All of these categories and the total commuters are displayed as NxN matrices of integers, excluding the label query.\n",
"The Commuters class outputs the total number of workers commuting, but LODES provides three categories for attributes specifying the type of workers: Age, Monthly Income, and Industry Sectors. Within each category, there are three ranges within them, and the sum of the ranges equals the total number of workers. All of these categories and the total commuters are displayed as NxN matrices of integers, excluding the Geoid query.\n",
"\n",
"## Age\n",
"- 'commuters_29_under'\n",
"- '29 and Under'\n",
" - Commuters that are ages 29 and under.\n",
"- 'commuters_30_to_54\n",
"- ''30_54'\n",
" - Commuters that are between the ages of 30 and 54.\n",
"- 'commuters_55_over'\n",
"- '55 and Over'\n",
" - Commuters that are ages 55 and over.\n",
"\n",
"## Monthly Income\n",
"- 'commuters_1250_under_earnings'\n",
"- '$1250 and Under'\n",
" - Commuters that earn $1250 and under per month.\n",
"- 'commuters_1251_to_3333_earnings'\n",
"- '$1251_$3333'\n",
" - Commuters that earn between $1251 and $3333 per month.\n",
"- 'commuters_3333_over_earnings'\n",
"- '$3333 and Over'\n",
" - Commuters that earn over $3333 per month.\n",
"\n",
"## Industry Sector\n",
"- 'commuters_goods_producing_industry'\n",
"- 'Goods Producing'\n",
" - Commuters that work in Goods Producing industry sectors.\n",
"- 'commuters_trade_transport_utility_industry'\n",
"- 'Trade Transport Utility'\n",
" - Commuters that work in Trade, Transportation, and Utility industry sectors.\n",
"- 'commuters_other_industry'\n",
"- 'Other'\n",
" - Commuters that work under all other service industry sectors other than the above claimed industries.\n"
]
},
Expand All @@ -133,32 +148,53 @@
"metadata": {},
"source": [
"## Job Type\n",
"Along with the above categories, LODES provides files detailing different job types and the total number of jobs under that type. However, unlike the attributes, these matrices do not sum to be the total number of workers. \n",
"Along with the above categories, LODES provides files detailing different job types and the total number of jobs under that type. However, unlike the attributes, these matrices do not sum to be the total number of workers.\n",
"\n",
"- 'all_jobs'\n",
"- 'All Jobs'\n",
" - All jobs regardless of job type. Allows for multiple jobs per person and is the default when calling the above attributes.\n",
"- 'primary_jobs'\n",
"- 'Primary Jobs'\n",
" - Primary jobs, which a primary job is the highest paying job for an individual worker for the year. Limits to one job per worker.\n",
"- 'all_private_jobs'\n",
"- 'All Private Jobs'\n",
" - All private jobs, which are privately owned businesses and organizations excluding federal government jobs.\n",
"- 'private_primary_jobs'\n",
"- 'Private Primary Jobs'\n",
" - Primary jobs within the private sector.\n",
"- 'all_federal_jobs'\n",
"- 'All Federal Jobs'\n",
" - All jobs within the federal government sector.\n",
"- 'federal_primary_jobs\n",
" - Jobs under the federal government sector that are defined as primary jobs."
"- 'Federal Primary Jobs\n",
" - Jobs under the federal government sector that are defined as primary jobs.\n",
"\n",
"Job type is an additional variable that can be combined with the previously explained attributes. For example, a user may retrieve a matrix of commuters work for the Goods Producing industry sector for only Primary jobs. This variable is not required for all calls and the default value will be 'All Jobs'."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Age Range Attribute Example\n",
"\n",
"Below is an example of calling the three different age ranges provided by LODES in a geo spec. The example here loads four counties into the matrices rather than the four states that was used previously."
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from epymorph.geo.adrio.lodes import CommutersByAge\n",
"from epymorph.geography.us_census import CountyScope\n",
"\n",
"time_period = 2015\n",
"county_scope = CountyScope.in_counties([\"04013\", \"08041\", \"32003\", \"35001\"])\n",
"\n",
"commuters_29_under = CommutersByAge(time_period, '29 and Under')\n",
"commuters_30_54 = CommutersByAge(time_period, '30_54')\n",
"commuters_55_over = CommutersByAge(time_period, '55 and Over')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -187,37 +223,12 @@
}
],
"source": [
"from epymorph.data_shape import Shapes\n",
"from epymorph.geo.adrio import adrio_maker_library\n",
"from epymorph.geo.dynamic import DynamicGeo\n",
"from epymorph.geo.spec import DynamicGeoSpec, Year\n",
"from epymorph.geography.us_census import CountyScope\n",
"from epymorph.simulation import AttributeDef\n",
"\n",
"spec = DynamicGeoSpec(\n",
" attributes=[\n",
" AttributeDef('label', str, Shapes.N),\n",
" AttributeDef('commuters_29_under', int, Shapes.NxN),\n",
" AttributeDef('commuters_30_to_54', int, Shapes.NxN),\n",
" AttributeDef('commuters_55_over', int, Shapes.NxN),\n",
" ],\n",
" time_period=Year(2015),\n",
" scope=CountyScope.in_counties([\"04013\", \"08041\", \"32003\", \"35001\"]),\n",
" source={\n",
" 'label': 'LODES:geoid',\n",
" 'commuters_29_under': 'LODES',\n",
" 'commuters_30_to_54': 'LODES',\n",
" 'commuters_55_over': 'LODES',\n",
" }\n",
")\n",
"\n",
"geo = DynamicGeo.from_library(spec, adrio_maker_library)\n",
"\n",
"print(f\"Commuters ages 29 and under:\\n {geo['commuters_29_under']}\\n\")\n",
"\n",
"print(f\"Commuters between ages 30 and 54:\\n {geo['commuters_30_to_54']}\\n\")\n",
"\n",
"print(f\"Commuters ages 55 and over:\\n {geo['commuters_55_over']}\\n\")"
"print(\n",
" f\"Commuters ages 29 and under:\\n {commuters_29_under.evaluate_in_context(data, dim, county_scope, rng)}\\n\")\n",
"print(\n",
" f\"Commuters between ages 30 and 54:\\n {commuters_30_54.evaluate_in_context(data, dim, county_scope, rng)}\\n\")\n",
"print(\n",
" f\"Commuters ages 55 and over:\\n {commuters_55_over.evaluate_in_context(data, dim, county_scope, rng)}\\n\")"
]
}
],
Expand All @@ -237,7 +248,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.11.0rc1"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion epymorph/geo/adrio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""AdrioMaker library."""
from epymorph.geo.adrio.census.adrio_census import ADRIOMakerCensus
from epymorph.geo.adrio.census.lodes import ADRIOMakerLODES
from epymorph.geo.adrio.census.adrio_lodes import ADRIOMakerLODES
from epymorph.geo.adrio.disease.adrio_cdc import ADRIOMakerCDC
from epymorph.geo.adrio.file.adrio_csv import ADRIOMakerCSV
from epymorph.geo.dynamic import ADRIOMaker
Expand Down
File renamed without changes.
Loading

0 comments on commit d8de7df

Please sign in to comment.