VAERS DATA POINT enhancement of Mortality Reports with Plotly and Hover Text Wrap
Channel: VHFILM
Date: 2021-12-24

Highlights to focus on...Is Panda's text wrap, Replace Python new lines with HTML breaks, The Garbage Collect Module, and the Speed enhancement of Panda's Memory_Map. 

Spaghetti Code Included ;-) If you refine the code, please link....

#VAERS #DATA #PLOTLY

AB == Angle Brackets, so replace those (AB)'s

import pandas as pd
import numpy as np
pd.set_option('display.max_columns', None)
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px
import warnings
import gc
warnings.filterwarnings("ignore")
date = pd.Timestamp.now().strftime('%m/%d/%Y')
import re
gc.enable()
vdata=pd.read_csv('2021VAERSData.csv', encoding='latin1', dtype={'BIRTH_DEFECT':object},low_memory=False)
vvax = pd.read_csv('2021VAERSVAX.csv',encoding='latin1',low_memory=False)
vsymp = pd.read_csv('2021VAERSSYMPTOMS.csv',encoding='latin1',low_memory=False)
vsymp.info()
dp =[2,4,6,8,10]
vsymp.drop(vsymp.columns[dp],axis=1, inplace=True)
c = [7,12,15,23]
vdata.drop(vdata.columns[c],axis=1, inplace=True)
v1 = vdata.merge(vvax, on='VAERS_ID', how='outer')
v1.head(1)
VAERSM = v1.merge(vsymp, on ="VAERS_ID", how='outer')
VAERSM.head(1)
VAERSM = VAERSM.query('VAX_TYPE== "COVID19"')
VAERSM.to_csv("VDUP.csv", index=False)
del vdata
del vvax
del vsymp
del VAERSM
VDUP = pd.read_csv('VDUP.csv',encoding='latin1',memory_map=True, low_memory=False)
test = VDUP.duplicated(subset=["VAERS_ID"])
print(test.value_counts())
del test
VDUP.query('VAX_TYPE=="COVID19"',inplace=True) 
gc.collect()
fullframe = VDUP.copy(deep=True) 
nodupids =  VDUP.drop_duplicates(subset=['VAERS_ID'], keep='first')
del VDUP
print(len(fullframe),len(nodupids))
pd.options.display.max_colwidth = 5000
nodupids[['VAERS_ID','SYMPTOM_TEXT']].head(3)
D_nodupids = nodupids.query('DIED=="Y"')

zero_d = D_nodupids.query('NUMDAYS AB= 1',)
zero_d.replace({np.nan:'N' },regex=True,inplace=True)
d_count =len(zero_d)
zero_d["SYMPTOM_TEXT" ]=zero_d['SYMPTOM_TEXT'].str.wrap(100)
zero_d['SYMPTOM_TEXT']= zero_d['SYMPTOM_TEXT'].str.replace('\\n','AB br /AB')
zbar= zero_d.groupby('AGE_YRS')['DIED'].count().to_frame().reset_index()
zbar
fig = make_subplots(rows=1, cols=2,shared_yaxes=False)

fig.add_trace(
    go.Box( y=zero_d['AGE_YRS'], boxpoints='all',name='Median Mortality', text=zero_d[['AGE_YRS','SYMPTOM1','SYMPTOM2','SYMPTOM3','SYMPTOM4','SYMPTOM5','SYMPTOM_TEXT']],hovertemplate = "AGE %{text[0]} ABbrAB %{text[1]}  ABbrAB %{text[2]} ABbrAB %{text[3]} ABbrAB %{text[4]} ABbrAB %{text[5]} ABbrAB %{text[6]}"),
    row=1, col=1
)

fig.add_trace(
    go.Bar(x=zbar['AGE_YRS'],y=zbar["DIED"],name='Total by Age',width=1, text=zbar[['AGE_YRS','DIED']],hovertemplate = "AGE %{text[0]} ABbrAB Deaths Reported: %{text[1]}") ,
    row=1, col=2
)

fig.update_layout(legend_traceorder="normal",template='plotly_dark',legend=dict(
        itemclick="toggleothers",
        itemdoubleclick="toggle"),hoverlabel=dict(
        bgcolor="red",
        font_size=30,
        font_family="Rockwell"))

fig.update_layout(font=dict(family="Droid Sans Mono",size=30))
fig.update_yaxes(tickfont_size=30, ticks="outside", ticklen=20, tickwidth=10,showspikes=True)
fig.update_xaxes(tickfont_size=30, ticks="outside", ticklen=20, tickwidth=10,showspikes=True)
fig.update_layout(height=1000, width=1900, title_text=f"DIED or DIED WITHIN 1 DAY OF SHOT -REPORTS TO VAERS #{d_count}")
fig.show()
dlong = nodupids.query('NUMDAYS AB=11 and NUMDAYS AB500')
dlong.replace(np.nan,"N", inplace=True)
dlong["SYMPTOM_TEXT" ]=dlong['SYMPTOM_TEXT'].str.wrap(100)
dlong['SYMPTOM_TEXT']= dlong['SYMPTOM_TEXT'].str.replace('\\n','ABbr /AB')
fig = px.scatter(dlong, x='NUMDAYS',y='VAERS_ID',color="DIED",hover_data=['SYMPTOM_TEXT'])
fig.update_layout(height=900, width=1900, title_text=f"LONG RANGE -REPORTS TO VAERS 11 Days or More as of {date}")
fig.update_layout(legend_traceorder="normal",template='plotly_dark',legend=dict(
        itemclick="toggleothers",
        itemdoubleclick="toggle"),hoverlabel=dict(
        bgcolor="black",
        font_size=30,
        font_family="Rockwell"
    )   )
fig.update_layout(font=dict(family="Droid Sans Mono",size=30))
fig.update_xaxes(tickfont_size=30, ticks="outside", ticklen=20, tickwidth=10)
fig.update_yaxes(tickfont_size=30, ticks="outside", ticklen=20, tickwidth=10)
fig.show()
del dlong
gc.collect()
dshort = D_nodupids.query('NUMDAYS AB=1 and NUMDAYS AB 11 ')
dshort.replace(np.nan,"N", inplace=True)
dshort["SYMPTOM_TEXT" ]=dshort['SYMPTOM_TEXT'].str.wrap(100)
dshort['SYMPTOM_TEXT']= dshort['SYMPTOM_TEXT'].str.replace('\\n','ABbr /AB')
zdbar= dshort.groupby('AGE_YRS')['DIED'].count().to_frame().reset_index()
d_count= zdbar['DIED'].sum()
----last graph CODE in comments--- Out of room