Skip to content

Commit

Permalink
Merge pull request #253 from USEPA/v1.2.3-release
Browse files Browse the repository at this point in the history
V1.2.3 release
  • Loading branch information
catherinebirney authored Jul 15, 2022
2 parents 97fdab9 + 9963c90 commit aaf5403
Show file tree
Hide file tree
Showing 25 changed files with 311 additions and 149 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
py-version: ['3.7', '3.8', '3.9', '3.10']
py-version: ['3.8', '3.9', '3.10']

steps:
- uses: actions/checkout@v2
Expand Down
25 changes: 23 additions & 2 deletions .github/workflows/test_methods.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
workflow_dispatch: # also allow manual trigger, for testing purposes

jobs:
build:
FBA_testing:
runs-on: ubuntu-latest

steps:
Expand All @@ -36,6 +36,27 @@ jobs:
run: |
python flowsa/test_methods.py
FBS_testing:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.10"

- name: Update pip & install testing pkgs
run: |
python -VV
python -m pip install --upgrade pip setuptools wheel
pip install pytest pytest-cov flake8
# install package & dependencies
- name: Install package and dependencies
run: |
pip install .
- name: Compare FBS with remote
id: FBS
if: always() # Proceed even if Test FBA fails
Expand All @@ -51,7 +72,7 @@ jobs:
name: FBS diff files
# A file, directory or wildcard patter that describes what to upload
path: |
${{ env.LD_LIBRARY_PATH }}/python3.10/site-packages/flowsa/data/fbs_diff/*_diff.csv
~/.local/share/flowsa/FBSComparisons/*
~/.local/share/flowsa/FlowBySector/*
~/.local/share/flowsa/Log/*
if-no-files-found: warn # 'warn' or 'ignore' are also available, defaults to `warn`
Expand Down
10 changes: 9 additions & 1 deletion flowsa/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
flow_by_activity_wsec_fields, flow_by_activity_mapped_wsec_fields, \
activity_fields
from flowsa.settings import datapath, MODULEPATH, logoutputpath, \
sourceconfigpath, log, flowbysectormethodpath
sourceconfigpath, log, flowbysectormethodpath, methodpath


# Sets default Sector Source Name
Expand Down Expand Up @@ -366,3 +366,11 @@ def str2bool(v):
return True
else:
return False


def check_method_status():
"""Read the current method status"""
yaml_path = methodpath + 'method_status.yaml'
with open(yaml_path, 'r') as f:
method_status = yaml.safe_load(f)
return method_status
79 changes: 0 additions & 79 deletions flowsa/data/FIPS_Crosswalk.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3194,82 +3194,3 @@ Wyoming,56039,Teton,56039,Teton,56039,Teton
Wyoming,56041,Uinta,56041,Uinta,56041,Uinta
Wyoming,56043,Washakie,56043,Washakie,56043,Washakie
Wyoming,56045,Weston,56045,Weston,56045,Weston
Puerto rico,72000,,72000,,72000,
Puerto rico,72001,,72001,Adjuntas municipio,72001,Adjuntas municipio
Puerto rico,72003,,72003,Aguada municipio,72003,Aguada municipio
Puerto rico,72005,,72005,Aguadilla municipio,72005,Aguadilla municipio
Puerto rico,72007,,72007,Aguas buenas municipio,72007,Aguas buenas municipio
Puerto rico,72009,,72009,Aibonito municipio,72009,Aibonito municipio
Puerto rico,72011,,72011,Añasco municipio,72011,Añasco municipio
Puerto rico,72013,,72013,Arecibo municipio,72013,Arecibo municipio
Puerto rico,72015,,72015,Arroyo municipio,72015,Arroyo municipio
Puerto rico,72017,,72017,Barceloneta municipio,72017,Barceloneta municipio
Puerto rico,72019,,72019,Barranquitas municipio,72019,Barranquitas municipio
Puerto rico,72021,,72021,Bayamón municipio,72021,Bayamón municipio
Puerto rico,72023,,72023,Cabo rojo municipio,72023,Cabo rojo municipio
Puerto rico,72025,,72025,Caguas municipio,72025,Caguas municipio
Puerto rico,72027,,72027,Camuy municipio,72027,Camuy municipio
Puerto rico,72029,,72029,Canóvanas municipio,72029,Canóvanas municipio
Puerto rico,72031,,72031,Carolina municipio,72031,Carolina municipio
Puerto rico,72033,,72033,Cataño municipio,72033,Cataño municipio
Puerto rico,72035,,72035,Cayey municipio,72035,Cayey municipio
Puerto rico,72037,,72037,Ceiba municipio,72037,Ceiba municipio
Puerto rico,72039,,72039,Ciales municipio,72039,Ciales municipio
Puerto rico,72041,,72041,Cidra municipio,72041,Cidra municipio
Puerto rico,72043,,72043,Coamo municipio,72043,Coamo municipio
Puerto rico,72045,,72045,Comerío municipio,72045,Comerío municipio
Puerto rico,72047,,72047,Corozal municipio,72047,Corozal municipio
Puerto rico,72049,,72049,Culebra municipio,72049,Culebra municipio
Puerto rico,72051,,72051,Dorado municipio,72051,Dorado municipio
Puerto rico,72053,,72053,Fajardo municipio,72053,Fajardo municipio
Puerto rico,72054,,72054,Florida municipio,72054,Florida municipio
Puerto rico,72055,,72055,Guánica municipio,72055,Guánica municipio
Puerto rico,72057,,72057,Guayama municipio,72057,Guayama municipio
Puerto rico,72059,,72059,Guayanilla municipio,72059,Guayanilla municipio
Puerto rico,72061,,72061,Guaynabo municipio,72061,Guaynabo municipio
Puerto rico,72063,,72063,Gurabo municipio,72063,Gurabo municipio
Puerto rico,72065,,72065,Hatillo municipio,72065,Hatillo municipio
Puerto rico,72067,,72067,Hormigueros municipio,72067,Hormigueros municipio
Puerto rico,72069,,72069,Humacao municipio,72069,Humacao municipio
Puerto rico,72071,,72071,Isabela municipio,72071,Isabela municipio
Puerto rico,72073,,72073,Jayuya municipio,72073,Jayuya municipio
Puerto rico,72075,,72075,Juana díaz municipio,72075,Juana díaz municipio
Puerto rico,72077,,72077,Juncos municipio,72077,Juncos municipio
Puerto rico,72079,,72079,Lajas municipio,72079,Lajas municipio
Puerto rico,72081,,72081,Lares municipio,72081,Lares municipio
Puerto rico,72083,,72083,Las marías municipio,72083,Las marías municipio
Puerto rico,72085,,72085,Las piedras municipio,72085,Las piedras municipio
Puerto rico,72087,,72087,Loíza municipio,72087,Loíza municipio
Puerto rico,72089,,72089,Luquillo municipio,72089,Luquillo municipio
Puerto rico,72091,,72091,Manatí municipio,72091,Manatí municipio
Puerto rico,72093,,72093,Maricao municipio,72093,Maricao municipio
Puerto rico,72095,,72095,Maunabo municipio,72095,Maunabo municipio
Puerto rico,72097,,72097,Mayagüez municipio,72097,Mayagüez municipio
Puerto rico,72099,,72099,Moca municipio,72099,Moca municipio
Puerto rico,72101,,72101,Morovis municipio,72101,Morovis municipio
Puerto rico,72103,,72103,Naguabo municipio,72103,Naguabo municipio
Puerto rico,72105,,72105,Naranjito municipio,72105,Naranjito municipio
Puerto rico,72107,,72107,Orocovis municipio,72107,Orocovis municipio
Puerto rico,72109,,72109,Patillas municipio,72109,Patillas municipio
Puerto rico,72111,,72111,Peñuelas municipio,72111,Peñuelas municipio
Puerto rico,72113,,72113,Ponce municipio,72113,Ponce municipio
Puerto rico,72115,,72115,Quebradillas municipio,72115,Quebradillas municipio
Puerto rico,72117,,72117,Rincón municipio,72117,Rincón municipio
Puerto rico,72119,,72119,Río grande municipio,72119,Río grande municipio
Puerto rico,72121,,72121,Sabana grande municipio,72121,Sabana grande municipio
Puerto rico,72123,,72123,Salinas municipio,72123,Salinas municipio
Puerto rico,72125,,72125,San germán municipio,72125,San germán municipio
Puerto rico,72127,,72127,San juan municipio,72127,San juan municipio
Puerto rico,72129,,72129,San lorenzo municipio,72129,San lorenzo municipio
Puerto rico,72131,,72131,San sebastián municipio,72131,San sebastián municipio
Puerto rico,72133,,72133,Santa isabel municipio,72133,Santa isabel municipio
Puerto rico,72135,,72135,Toa alta municipio,72135,Toa alta municipio
Puerto rico,72137,,72137,Toa baja municipio,72137,Toa baja municipio
Puerto rico,72139,,72139,Trujillo alto municipio,72139,Trujillo alto municipio
Puerto rico,72141,,72141,Utuado municipio,72141,Utuado municipio
Puerto rico,72143,,72143,Vega alta municipio,72143,Vega alta municipio
Puerto rico,72145,,72145,Vega baja municipio,72145,Vega baja municipio
Puerto rico,72147,,72147,Vieques municipio,72147,Vieques municipio
Puerto rico,72149,,72149,Villalba municipio,72149,Villalba municipio
Puerto rico,72151,,72151,Yabucoa municipio,72151,Yabucoa municipio
Puerto rico,72153,,72153,Yauco municipio,72153,Yauco municipio
6 changes: 6 additions & 0 deletions flowsa/data/source_catalog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ Census_PEP_Population:
sector-like_activities: False
activity_schema:
sector_aggregation_level: ""
Census_QWI:
class:
- Employment
sector-like_activities: True
activity_schema: NAICS_2012_Code
sector_aggregation_level: "disaggregated"
Census_VIP:
class: Money
sector-like_activities: False
Expand Down
126 changes: 126 additions & 0 deletions flowsa/data_source_scripts/Census_QWI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# BLS_QCEW.py (flowsa)
# !/usr/bin/env python3
# coding=utf-8
"""
Pulls Quarterly Census of Employment and Wages data in NAICS from Bureau
of Labor Statistics. Writes out to various FlowBySector class files for
these data items
EMP = Number of employees, Class = Employment
PAYANN = Annual payroll ($1,000), Class = Money
ESTAB = Number of establishments, Class = Other
This script is designed to run with a configuration parameter
--year = 'year' e.g. 2015
"""

import json
import pandas as pd
import numpy as np
from flowsa.location import get_all_state_FIPS_2, get_county_FIPS, US_FIPS
from flowsa.common import fba_default_grouping_fields, load_api_key
from flowsa.schema import flow_by_activity_wsec_fields, \
flow_by_activity_mapped_wsec_fields
from flowsa.flowbyfunctions import assign_fips_location_system, \
aggregator
from flowsa.dataclean import add_missing_flow_by_fields, \
replace_strings_with_NoneType


def census_qwi_url_helper(*, build_url, year, config, **_):
"""
This helper function uses the "build_url" input from flowbyactivity.py,
which is a base url for data imports that requires parts of the url text
string to be replaced with info specific to the data year. This function
does not parse the data, only modifies the urls from which data is
obtained.
:param build_url: string, base url
:param config: dictionary, items in FBA method yaml
:param args: dictionary, arguments specified when running flowbyactivity.py
flowbyactivity.py ('year' and 'source')
:return: list, urls to call, concat, parse, format into Flow-By-Activity
format
"""
quarters = [1, 2, 3, 4]
urls = []
if int(year) >= 2015:
fips_year = str(2015)
elif int(year) >= 2013:
fips_year = str(2013)
else:
fips_year = str(2010)
county_fips_df = get_county_FIPS(fips_year)
county_fips = county_fips_df.FIPS
for q in quarters:
for d in county_fips:
url = build_url
url = url.replace('__year__', str(year))
userAPIKey = load_api_key(config['api_name'])
url = url.replace("__apiKey__", userAPIKey)
state_digit = str(d[0]) + str(d[1])
county_digit = str(d[2]) + str(d[3]) + str(d[4])
url = url.replace("__state__", state_digit)
url = url.replace("__county__", county_digit)
url = url.replace("__quarter__", str(q))
urls.append(url)

return urls


def census_qwi_call(*, resp, **_):
"""
Convert response for calling url to pandas dataframe,
begin parsing df into FBA format
:param resp: df, response from url call
:return: pandas dataframe of original source data
"""
try:
json_load = json.loads(resp.text)
# convert response to dataframe
df = pd.DataFrame(data=json_load[1:len(json_load)],
columns=json_load[0])
except:
print(resp)
df = pd.DataFrame()
finally:
return df


def census_qwi_parse(*, df_list, year, **_):
"""
Combine, parse, and format the provided dataframes
:param df_list: list of dataframes to concat and format
:param args: dictionary, used to run flowbyactivity.py
('year' and 'source')
:return: df, parsed and partially formatted to flowbyactivity
specifications
"""
# Concat dataframes
df = pd.concat(df_list, ignore_index=True)
# drop rows don't need
# get rid of None values in EmpTotal
df = df[df.EmpTotal.notnull()]
df.loc[df['ownercode'] == 'A00', 'Owner'] = 'State and local government ' \
'plus private ownership'
df.loc[df['ownercode'] == 'A01', 'Owner'] = 'Federal government'
df.loc[df['ownercode'] == 'A05', 'Owner'] = 'All Private'
df = df.reindex()

# Combine the State and County into the location.
df['Location'] = df['state'] + df['county']

# industry needs to be renamed Activity Produced by.
# add the Quarter and ownership codes to flowname.
df['FlowName'] = df.apply(
lambda x: f'Number of employees, {x["Owner"]}, Quarter {x["quarter"]}',
axis=1)
df = df.rename(columns={'EmpTotal': 'FlowAmount',
'year': 'Year',
'industry': "ActivityProducedBy"})

df = df.drop(columns=['state', 'county', 'Owner', 'ownercode'])
# add location system based on year of data
df = assign_fips_location_system(df, year)
# add hard code data
df['SourceName'] = 'Census_QWI'
df['FlowType'] = "ELEMENTARY_FLOW"
df['Class'] = "Employment"
return df
10 changes: 10 additions & 0 deletions flowsa/methods/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Current Status

Known issues with exisiting FBA and FBS methods are shown in [method_status.yaml](method_status.yaml)

```
<Method_Name>
Active: True or False indicates whether the method can still be run.
Status: Description of the current status.
Type: Type of error generated by the method, e.g. 'HTTPError'
```
1 change: 1 addition & 0 deletions flowsa/methods/flowbyactivitymethods/BLS_QCEW.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ years:
- 2016
- 2017
- 2018
- 2019
25 changes: 25 additions & 0 deletions flowsa/methods/flowbyactivitymethods/Census_QWI.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
author: US Census Bureau
source_name: Quarterly Workforce Indicators
source_url: https://www.census.gov/data/developers/data-sets/qwi.html#ownership
bib_id: Census_QWI
api_name: Census
api_key_required: True
format: csv # comma delineated data
url:
base_url: https://api.census.gov/data/timeseries/qwi/se?get=industry,EmpTotal,ownercode&for=county:__county__&in=state:__state__&year=__year__&quarter=__quarter__&key=__apiKey__
url_replace_fxn: !script_function:Census_QWI census_qwi_url_helper
call_response_fxn: !script_function:Census_QWI census_qwi_call
parse_response_fxn: !script_function:Census_QWI census_qwi_parse
years:
- 2002
- 2010
- 2011
- 2012
- 2013
- 2014
- 2015
- 2016
- 2017
- 2018
- 2019
- 2020
2 changes: 1 addition & 1 deletion flowsa/methods/flowbyactivitymethods/USGS_MYB_Copper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ date_generated: '2021-03-19'
years_available: 2011-2015
format: xlsx
url:
base_url: https://s3-us-west-2.amazonaws.com/prd-wret/assets/palladium/production/mineral-pubs/copper/myb1-2015-coppe.xlsx
base_url: https://d9-wret.s3.us-west-2.amazonaws.com/assets/palladium/production/mineral-pubs/copper/myb1-2015-coppe.xlsx
url_replace_fxn: !script_function:USGS_MYB usgs_myb_url_helper
call_response_fxn: !script_function:USGS_MYB usgs_copper_call
parse_response_fxn: !script_function:USGS_MYB usgs_copper_parse
Expand Down
5 changes: 0 additions & 5 deletions flowsa/methods/flowbysectormethods/Employment_state_2012.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,4 @@ source_names:
qcew:
names: !from_index:BLS_QCEW_asets.csv qcew
allocation_method: direct
allocation_source: None
allocation_source_class: None
allocation_source_year: None
allocation_flow: None
allocation_compartment: None
allocation_from_scale: state
6 changes: 0 additions & 6 deletions flowsa/methods/flowbysectormethods/Employment_state_2013.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ source_names:
year: 2013
clean_fba_df_fxn: !script_function:BLS_QCEW clean_bls_qcew_fba_for_employment_sat_table
clean_fba_w_sec_df_fxn: !script_function:BLS_QCEW bls_clean_allocation_fba_w_sec
activity_set_file: 'BLS_QCEW_asets.csv'
activity_sets:
qcew:
names: !from_index:BLS_QCEW_asets.csv qcew
allocation_method: direct
allocation_source: None
allocation_source_class: None
allocation_source_year: None
allocation_flow: None
allocation_compartment: None
allocation_from_scale: state
6 changes: 0 additions & 6 deletions flowsa/methods/flowbysectormethods/Employment_state_2014.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ source_names:
year: 2014
clean_fba_df_fxn: !script_function:BLS_QCEW clean_bls_qcew_fba_for_employment_sat_table
clean_fba_w_sec_df_fxn: !script_function:BLS_QCEW bls_clean_allocation_fba_w_sec
activity_set_file: 'BLS_QCEW_asets.csv'
activity_sets:
qcew:
names: !from_index:BLS_QCEW_asets.csv qcew
allocation_method: direct
allocation_source: None
allocation_source_class: None
allocation_source_year: None
allocation_flow: None
allocation_compartment: None
allocation_from_scale: state
Loading

0 comments on commit aaf5403

Please sign in to comment.