Skip to content
Merged
447 changes: 421 additions & 26 deletions python/hh_characteristics.py

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions python/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"structure_type": 4,
"income_category": 10,
"household_size": 7,
"household_workers": 4,
"tract": {
2010: 627,
2020: 736,
Expand All @@ -39,7 +40,7 @@
# The industry_code is a variable to group employment data into. Almost all
# codes are 2-digit naics codes. The 2-digit naics code 72 was split into 721
# and 722 3-digit naics code. Self employment and military active duty data do
# not natively have a naics code so it is therefore assigned to 'SE' and 'MIL'
# not natively have a naics code so it is therefore assigned to 'SE' and 'MIL'
# respectively, as the naics codes are being treated as strings.
"industry_code": 23,
},
Expand All @@ -52,7 +53,9 @@

# For ease of access, combine the constants part of the dictionary with the current
# MGRA series
_DISTINCT_COUNTS = _DISTINCT_COUNTS["constants"] | _DISTINCT_COUNTS["series"][utils.SERIES]
_DISTINCT_COUNTS = (
_DISTINCT_COUNTS["constants"] | _DISTINCT_COUNTS["series"][utils.SERIES]
)


#########
Expand Down
7 changes: 4 additions & 3 deletions python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import python.parsers as parsers


#########
# PATHS #
#########
Expand Down Expand Up @@ -72,8 +71,8 @@
+ _secrets["sql"]["gis"]["server"]
+ "/"
+ _secrets["sql"]["gis"]["database"]
+ "?driver=ODBC Driver 18 for SQL Server" +
"&TrustServerCertificate=yes",
+ "?driver=ODBC Driver 18 for SQL Server"
+ "&TrustServerCertificate=yes",
fast_executemany=True,
)

Expand Down Expand Up @@ -127,6 +126,8 @@

HOUSEHOLD_SIZES = list(range(1, 8))

HOUSEHOLD_WORKERS = list(range(0, 4))

ASE = ["age_group", "sex", "ethnicity"]

INCOME_CATEGORIES = [
Expand Down
39 changes: 39 additions & 0 deletions reporting/check_hhworkers_hhsize.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- SQL script to check that households by workers do not exceed households by size
DECLARE @run_id INTEGER = :run_id;


-- Check hard constraint of household size is not violated
WITH [Household Size] AS (
SELECT
[year],
[mgra],
SUM(CASE WHEN [metric] != 'Household Size - 1' THEN [value] ELSE 0 END) AS [2+ size], -- 2+ household size
SUM(CASE WHEN [metric] NOT IN ('Household Size - 1','Household Size - 2') THEN [value] ELSE 0 END) AS [3+ size] -- 3+ household size
FROM [outputs].[hh_characteristics]
WHERE [run_id] = @run_id AND [metric] LIKE 'Household Size%'
GROUP BY [year], [mgra]
),
[Household Workers] AS (
SELECT
[year],
[mgra],
SUM(CASE WHEN [metric] = 'Household Workers - 2' THEN [value] ELSE 0 END) AS [2 workers],
SUM(CASE WHEN [metric] = 'Household Workers - 3+' THEN [value] ELSE 0 END) AS [3+ workers]
FROM [outputs].[hh_characteristics]
WHERE [run_id] = @run_id AND [metric] LIKE 'Household Workers%'
GROUP BY [year], [mgra]
)
SELECT
[Household Size].[year],
[Household Size].[mgra],
[Household Size].[2+ size],
[Household Size].[3+ size],
[Household Workers].[2 workers],
[Household Workers].[3+ workers]
FROM [Household Size]
INNER JOIN [Household Workers]
ON [Household Size].[year] = [Household Workers].[year]
AND [Household Size].[mgra] = [Household Workers].[mgra]
WHERE
[Household Size].[2+ size] < [Household Workers].[2 workers]
OR [Household Size].[3+ size] < [Household Workers].[3+ workers]
47 changes: 47 additions & 0 deletions reporting/check_implied_workers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- SQL script to check that implied workers do not exceed persons aged 18+
DECLARE @run_id INTEGER = :run_id;


-- Check soft constraint of persons aged 18+
WITH [Household Size] AS (
SELECT
[year],
[mgra],
SUM(
CASE
WHEN [metric] = 'Household Workers - 1' THEN [value]
WHEN [metric] = 'Household Workers - 2' THEN [value]*2
WHEN [metric] = 'Household Workers - 3+' THEN [value]*3
ELSE 0
END
) AS [min_workers]
FROM [outputs].[hh_characteristics]
WHERE [run_id] = @run_id AND [metric] LIKE 'Household Workers%'
GROUP BY [year], [mgra]
),
[Population Aged 18+] AS (
SELECT
[year],
[mgra],
SUM([value]) AS [persons_18plus]
FROM [outputs].[ase]
WHERE
[run_id] = @run_id
AND [age_group] NOT IN (
'Under 5',
'5 to 9',
'10 to 14',
'15 to 17'
)
GROUP BY [year], [mgra]
)
SELECT
[Household Size].[year],
[Household Size].[mgra],
[Household Size].[min_workers],
ISNULL([Population Aged 18+].[persons_18plus], 0) AS [persons_18plus]
FROM [Household Size]
LEFT OUTER JOIN [Population Aged 18+]
ON [Household Size].[year] = [Population Aged 18+].[year]
AND [Household Size].[mgra] = [Population Aged 18+].[mgra]
WHERE [min_workers] > ISNULL([Population Aged 18+].[persons_18plus], 0);
4 changes: 3 additions & 1 deletion reporting/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# https://github.com/SANDAG/Series-15-Urban-Development-Model/blob/main/Other/Significant%20Change.xlsx

# Main configuration, what run_id to operate on
RUN_ID = 245
RUN_ID = 229

# We cannot import python.utils, as just importing will cause a new [run_id]` value and
# new log file to be created. Instead, copy what we need for now :(
Expand Down Expand Up @@ -46,6 +46,8 @@
"Check every HHP has HH": "check_every_hhp_has_hh.sql",
"Check implied HHP vs actual HHP": "check_implied_hhp_vs_actual_hhp.sql",
"Check householders vs households": "check_householders_vs_households.sql",
"Check implied workers vs persons 18+": "check_implied_workers.sql",
"Check households by workers vs households by size": "check_hhworkers_hhsize.sql",
}

# Run each script, printing out status messages if necessary:
Expand Down
7 changes: 2 additions & 5 deletions sql/hh_characteristics/get_mgra_controls_hh_by_size.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ Get MGRA controls for household population used in households by household size.
This data is pulled from previously run modules.

Two input parameters are used
run_id - the run identifier is used to get the list of census tracts in tandem with
the year parameter that determines their vintage
year - the year parameter is used to identify the 5-year ACS Detailed Tables release
to use (ex. 2023 maps to 2019-2023) and the census tract geography vintage
(2010-2019 uses 2010, 2020-2029 uses 2020)
run_id - the run identifier for this run
year - the year parameter grabs the year of data to use
*/

-- Initialize parameters
Expand Down
47 changes: 47 additions & 0 deletions sql/hh_characteristics/get_mgra_controls_hh_by_workers_18plus.sql
Comment thread
GregorSchroeder marked this conversation as resolved.
Comment thread
GregorSchroeder marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Get MGRA controls for persons aged 18+ used in households by number of workers.
This data is pulled from previously run modules.

It would be preferable to use 16+ as that is the age of eligibility for labor
force participation in the ACS but the Estimates Program uses 15 to 17 as an
age category so there is not an easy way to select only 16+.

Two input parameters are used
run_id - the run identifier for this run
year - the year parameter grabs the year of data to use
*/

-- Initialize parameters
DECLARE @run_id integer = :run_id;
DECLARE @year integer = :year;

-- Get population aged 18+
WITH [ase_data] AS (
SELECT
[mgra],
SUM([value]) AS [value]
FROM [outputs].[ase]
WHERE
[run_id] = @run_id
AND [year] = @year
AND [pop_type] = 'Household Population'
AND [age_group] NOT IN (
'Under 5',
'5 to 9',
'10 to 14',
'15 to 17'
)
GROUP BY [mgra]
)
-- Ensure all MGRAs are included
SELECT
@run_id AS [run_id],
@year AS [year],
[mgra].[mgra],
ISNULL([value], 0) AS [persons_18plus]
FROM [inputs].[mgra]
LEFT OUTER JOIN [ase_data]
ON [mgra].[mgra] = [ase_data].[mgra]
WHERE
[mgra].[run_id] = @run_id
ORDER BY [mgra].[mgra]
46 changes: 46 additions & 0 deletions sql/hh_characteristics/get_mgra_controls_hh_by_workers_hhs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Get MGRA controls for households by size used in households by number of workers.
This data is pulled from previously run portions of this module.

Since households by workers only contains (0,1,2,3+) categories, the household
size categories are collapsed to (2+,3+) to match the minimum implied household
sizes from the households by workers categories (e.g. 0 and 1 worker households
can be any size 1+, a 2 worker household must be at least size 2, and a three
worker household must be at least size 3).

Two input parameters are used
run_id - the run identifier for this run
year - the year parameter grabs the year of data to use
*/

-- Initialize parameters
DECLARE @run_id integer = :run_id;
DECLARE @year integer = :year;

-- Get households by size
SELECT
@run_id AS [run_id],
@year AS [year],
[mgra],
SUM(
CASE
WHEN [metric] != 'Household Size - 1' THEN [value]
ELSE 0
END
) AS [2], -- 2+ household size
SUM(
CASE
WHEN [metric] NOT IN (
'Household Size - 1',
'Household Size - 2'
) THEN [value]
ELSE 0
END
) AS [3] -- 3+ household size
FROM [outputs].[hh_characteristics]
WHERE
[run_id] = @run_id
AND [year] = @year
AND [metric] LIKE 'Household Size%'
GROUP BY [mgra]
ORDER BY [mgra]
Loading