<jupyter_start><jupyter_text>Hasnah Said A4: Common Analysis November 4, 2021<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Step 0: Data acquisition In the first part of this project, we are trying to answer the following question for the county that we were assigned to: **How did masking policies change the progression of confirmed COVID-19 cases from February 1, 2020 through October 15, 2021?****My county:** hasna024 | Said	Hasnah | 7| 54 | Fairfield | Connecticut | 1,618.46|624.89 | 957,419 | 40,590 | Bridgeport<jupyter_code>mask_mandate = pd.read_csv('raw_data/mask_mandates_by_county.csv')
mask_use = pd.read_csv('raw_data/mask_use_by_county.csv')
us_confirmed_cases = pd.read_csv('raw_data/RAW_us_confirmed_cases.csv')<jupyter_output><empty_output><jupyter_text>Step 1: Analyze The common question that you are to answer is:**How did masking policies change the progression of confirmed COVID-19 cases from February 1, 2020 through October 15, 2021?**To answer the question, the first thing I will do is extract the information relevent to my county from the three datasets. I was assigned Fairfield, Connecticut. I did the following to process the data and prepare them to plot the time series: * Filter out Fairfield data* Drop unneeded columns in the confirmed cases* Transpose the cases dataframe* Rename columns and add a FIPS column* Merge the cases dataframe with the mask use data frame on FIPS* Drop unneded columns in mandate mask dataframe* Convert date columns to datetime type* Merge the mandage mask with other two data frame on date* Calculate the number of daily new cases* Add a column for the population* Calculate the the rate of infection using the formula I found in the link shared on Slack(https://towardsdatascience.com/wide-to-long-data-how-and-when-to-use-pandas-melt-stack-and-wide-to-long-7c1e0f462a98):  Rate of Infection = $\frac{Number of Infections}{Population at Risk}$ I will define Population at Risk = county population - confirmed cases<jupyter_code># Filter out Fairfield data 
fairfield_cases_df = us_confirmed_cases.loc[(us_confirmed_cases['Admin2'] == 'Fairfield') & (us_confirmed_cases['Province_State'] == 'Connecticut')]
fairfield_fips = fairfield_cases_df['FIPS'].item()
fairfield_mask_df = mask_use.loc[mask_use['COUNTYFP'] == fairfield_fips]
fairfield_mandate_df = mask_mandate[(mask_mandate['State_Tribe_Territory'] == 'CT') & (mask_mandate['County_Name'] == 'Fairfield County')]
# Drop columns that are not needed
fairfield_cases_df = fairfield_cases_df.drop(['Province_State', 'Admin2', 'UID', 'iso2', 'iso3', 'code3', 'FIPS',
       'Country_Region', 'Lat', 'Long_', 'Combined_Key'], axis=1)
# Transpose the dates and number of cases
fairfield_cases_df['id'] = fairfield_cases_df.index
fairfield_cases_df.set_index('id',inplace=True)
fairfield_cases_df = fairfield_cases_df.T
fairfield_cases_df = fairfield_cases_df.rename(columns={323: 'cases'})
fairfield_cases_df['date'] = fairfield_cases_df.index
fairfield_cases_df['date'] = pd.to_datetime(fairfield_cases_df['date'])
fairfield_cases_df
# Merge confirmed cases and mask use dataframes
merged_df = fairfield_cases_df.merge(fairfield_mask_df, how='left', left_on='FIPS', right_on='COUNTYFP')
# Convert date column to datetime type to merge
fairfield_mandate_df['date'] = pd.to_datetime(fairfield_mandate_df['date'])
# Drop columns not needed for visualization
fairfield_mandate_df = fairfield_mandate_df.drop(['State_Tribe_Territory', 'County_Name', 'FIPS_State', 'FIPS_County'], axis=1)
# Merge mask mandate with other two dataframes
merged_df = merged_df.merge(fairfield_mandate_df, how='left', on='date').drop(['COUNTYFP'], axis =1)
merged_df
merged_df['date'] = pd.to_datetime(merged_df['date'])
# Filter out dates outsided the range 
mask = (merged_df['date'] >= '2020-02-01') & (merged_df['date'] <= '2021-10-15')
merged_df = merged_df.loc[mask]
merged_df
# Calculate the daily new covid cases
merged_df['daily_new_cases'] = merged_df['cases'].diff()
# Calculate the rate of infection
merged_df['Population'] = 957419
merged_df['Population_at_Risk'] = merged_df['Population'] - merged_df['cases'] 
merged_df['Rate_of_infection'] = merged_df['cases'] / merged_df['Population_at_Risk']
merged_df.columns
merged_df.to_csv('clean_data/Fairfield_Cases_Masks.csv')<jupyter_output><empty_output><jupyter_text>Step 2: Visualize The visualization I created shows the number of new cases daily. I marked the period of time where masking mandate policies were in effect in orange. The second visualization shows the rate of infection over the same period of time.<jupyter_code>fig = plt.figure(figsize = (25, 15))

plt.plot(merged_df['date'], merged_df['daily_new_cases'], 
        label='Daily new confirmed COVID caes - No Mask Mandate')

plt.plot(merged_df.loc[merged_df['Face_Masks_Required_in_Public'] == 'Yes', 'date'], 
         merged_df.loc[merged_df['Face_Masks_Required_in_Public'] == 'Yes', 'daily_new_cases'], 
         label='Daily new confirmed COVID caes - Mask Mandate')
plt.grid()


plt.ylabel('Number of new daily cases', fontsize=15)
plt.xlabel('Date', fontsize=15)

plt.title('The Number of Daily New COVID Cases in Fairfield, Connecticut from February 1, 2020 to October 15, 2021', fontsize=30)
plt.legend(fontsize=15)

txt="The plot above shows the number of new confirmed COVID cases daily. The blue part shows the new cases when masks were not mandated while the orange section shows the number of new cases when masks were mandated."
plt.figtext(0.5, 0.01, txt, wrap=True, horizontalalignment='center', fontsize=15)
plt.savefig('fairfield_daily_case_increase.png')
fig = plt.figure(figsize = (25, 15))


plt.plot(merged_df.loc[merged_df['Face_Masks_Required_in_Public'] == 'Yes', 'date'], 
         merged_df.loc[merged_df['Face_Masks_Required_in_Public'] == 'Yes', 'cases'])

plt.bar(merged_df.loc[merged_df['Face_Masks_Required_in_Public'] == 'Yes', 'date'], 
         merged_df.loc[merged_df['Face_Masks_Required_in_Public'] == 'Yes', 'cases'], 
         color= 'lightyellow', width = 1.5)

plt.grid()
plt.ylabel('COVID Confirmed Cases', fontsize = 15)
plt.xlabel('Date', fontsize = 15)
plt.title('The Rate of COVID-19 Infection in Fairfield, Connecticut From February 1, 2020 to October 15, 2021', fontsize=20)
plt.savefig('fairfield_rate_of_infection.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11093774379778437, 'fraction_numerical': 0.024808862537057264, 'mean_word_length': 6.558962264150943, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '534880', 'license': 'MIT', 'n_tokens_mistral': 2241, 'n_tokens_neox': 1998, 'n_words': 593, 'path': 'A4/A4.ipynb', 'repo_name': 'HasnahSaid/data512_final_project', 'size': '6409'}