# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.9.2 64-bit (''pff-factfinder-Gy5E4KFG-py3.9'': poetry)'
#     name: python3
# ---

# +
#Initialize
from factfinder.calculate import Calculate
from factfinder.median import Median
from dotenv import load_dotenv
import os
import pandas as pd
import logging
# pd.options.display.float_format = "{:,.18f}".format

try:
    env_path = "../.env"
    load_dotenv(dotenv_path=env_path)
except:
    print(".env file is missing ...")

logger = logging.getLogger()

calculate = Calculate(api_key=os.environ["API_KEY"], year=2010, source="acs", geography='2010_to_2020')
# -

##INPUTS -- change here
pff_variable = 'mdvl'
geotype = 'CT20'
census_geoid_list = ['36005020100']

# See all digits of ratio
ratio = calculate.geo.ratio
ratio.loc[ratio.geoid_ct2020.isin(census_geoid_list), :]


# Get ranges and design factor from metadata
ranges = calculate.meta.median_ranges(pff_variable)
design_factor = calculate.meta.median_design_factor(pff_variable)
top_coding = calculate.meta.median_top_coding(pff_variable)
bottom_coding = calculate.meta.median_bottom_coding(pff_variable)

# Calculate inputs in 2020 geogs
logger.disabled = True
df = calculate.calculate_e_m_multiprocessing(list(ranges.keys()), geotype)
logger.disabled = False

# 3. create a pivot table with census_geoid as the index, and
# pff_variable as column names. df_pivoted.e -> the estimation dataframe
df_pivoted = df.loc[df.census_geoid.isin(census_geoid_list), ["census_geoid", "pff_variable", "e"]].pivot(
    index="census_geoid", columns="pff_variable", values=["e"]
)
df_pivoted = df_pivoted.round(16)
df_pivoted


def get_median_and_median_moe(ranges, row, DF, top_coding, bottom_coding):
    md = Median(ranges, row, DF, top_coding, bottom_coding)
    e = md.median
    m = md.median_moe
    return pd.Series({'e': e, 'm': m})


results = df_pivoted.e.apply(lambda x: get_median_and_median_moe(
            ranges, x, design_factor, top_coding, bottom_coding), axis=1)
results["census_geoid"] = df_pivoted.index
results = results.reset_index(drop=True)
results["pff_variable"] = pff_variable
results["geotype"] = geotype

StarcoderdataJupyterScriptsDedupFiltered