<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Interactive Web application with Dash
Authors
Step1:  Data collection (2/6)
Query d_labitems table (Dictionary table for mapping)
Query labevents table (History of the labitem order)
Join two tables
Query prescriptions table (History of the prscription order)
Step2:  Data preparation for labevents table (3/6)
Convert data type to datetime and extract only year value
Step3:  Data preparation for prescriptions table (4/6)
Filter conditions
Step4:  Create a structure and presentation of your web with HTML and CSS (5/6)
Step5:  Define the reactive behavior with Python (6/6)
<ASSISTANT_TASK:>
Python Code:
# # Dash packages installation
# !conda install -c conda-forge dash-renderer -y
# !conda install -c conda-forge dash -y
# !conda install -c conda-forge dash-html-components -y
# !conda install -c conda-forge dash-core-components -y
# !conda install -c conda-forge plotly -y
import dash
import dash_core_components as dcc
import dash_html_components as html
import flask
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import numpy as np
import pandas as pd
pd.set_option('display.max_columns', 999)
import pandas.io.sql as psql
Explanation: Interactive Web application with Dash
Authors: Prof. med. Thomas Ganslandt &#84;&#104;&#111;&#109;&#97;&#115;&#46;&#71;&#97;&#110;&#115;&#108;&#97;&#110;&#100;&#116;&#64;&#109;&#101;&#100;&#109;&#97;&#46;&#117;&#110;&#105;&#45;&#104;&#101;&#105;&#100;&#101;&#108;&#98;&#101;&#114;&#103;&#46;&#100;&#101; <br>
and Kim Hee &#72;&#101;&#101;&#69;&#117;&#110;&#46;&#75;&#105;&#109;&#64;&#109;&#101;&#100;&#109;&#97;&#46;&#117;&#110;&#105;&#45;&#104;&#101;&#105;&#100;&#101;&#108;&#98;&#101;&#114;&#103;&#46;&#100;&#101; <br>
Heinrich-Lanz-Center for Digital Health (HLZ) of the Medical Faculty Mannheim <br>
Heidelberg University
This tutorial is prepared for TMF summer school on 03.07.2019
Prerequisite: MIMIC-III files locally
You should place the following MIMIC-III data files in the data/ subfolder:
D_LABITEMS.csv
LABEVENTS.csv
PRESCRIPTIONS.csv
Dash
https://dash.plot.ly/gallery <br>
Dash is a Python framework for creating data-driven web applications <br>
Dash apps are written on top of Flask, Plotly, and React
Flask is a Python web framework
Plotly is specifically a charting library built on top of D3.js
React is a JavaScript library for building user interfaces maintained by Facebook and a community 
Case Study 2: Labitems Trend Visualization
Trend analysis is the widespread practice of collecting information and attempting to spot a pattern. This case study will illustrate a drug reaction of a sepsis patient. This case study tracks the biomarker and prescription history of patient 41976. It visualizes the relation between two key biomarkers of sepsis (White Blood Cells and Neutrophils) and 
'41976' patient is choosen for this case study because this patient contains most and interesting records among other sepsis patients '10006', '10013', '10036', '10056', '40601'
Import Python pakages (1/6)
End of explanation
d_lab = pd.read_csv("data/D_LABITEMS.csv")
d_lab.columns = map(str.lower, d_lab.columns)
d_lab.drop(columns = ['row_id'], inplace = True)
lab = pd.read_csv("data/LABEVENTS.csv")
lab.columns = map(str.lower, lab.columns)
lab = lab[lab['subject_id'] == 41976]
lab.drop(columns = ['row_id'], inplace = True)
lab = pd.merge(d_lab, lab, on = 'itemid', how = 'inner')
print(lab.columns)
lab[['subject_id', 'hadm_id', 'itemid', 'label', 'value']].head()
presc = pd.read_csv("data/PRESCRIPTIONS.csv")
presc.columns = map(str.lower, presc.columns)
presc = presc[presc['subject_id'] == 41976]
presc.drop(columns = ['row_id'], inplace = True)
print(presc.columns)
presc[['subject_id', 'hadm_id', 'icustay_id', 'drug']].head()
Explanation: Data collection (2/6)
Query d_labitems table (Dictionary table for mapping)
Query labevents table (History of the labitem order)
Join two tables
Query prescriptions table (History of the prscription order)
End of explanation
lab['charttime'] = pd.to_datetime(lab['charttime'], errors = 'coerce')
lab.sort_values(by='charttime', inplace=True)
lab.set_index('charttime', inplace = True)
lab.head(1)
Explanation: Data preparation for labevents table (3/6)
Convert data type to datetime and extract only year value
End of explanation
presc['dose_val_rx'] = pd.to_numeric(presc['dose_val_rx'], errors = 'coerce')
presc = presc[presc['dose_unit_rx']=='mg']
presc = presc[presc['drug'].isin(['Vancomycin','Meropenem','Levofloxacin'])]
temp_df = pd.DataFrame()
for item in presc.drug.unique():
    temp = presc[presc['drug'].str.contains(item)]
    temp['norm_size'] = temp['dose_val_rx'] / temp['dose_val_rx'].max()
    temp_df = temp_df.append(temp)
presc = pd.merge(presc, temp_df, on=list(presc.columns))
presc['startdate'] = pd.to_datetime(presc['startdate'], errors = 'coerce')
presc.sort_values(by='startdate', inplace=True)
presc.set_index('startdate', inplace = True)
presc.head(1)
Explanation: Data preparation for prescriptions table (4/6)
Filter conditions:
unit: 'mg'
antibiotics medicines: ('Vancomycin','Meropenem','Levofloxacin')
Contruct a normalized dose column
Convert data type to datetime and extract only year value
End of explanation
list_patient = ['41976']
list_biomarker = ['White Blood Cells', 'Neutrophils']
list_drug = ['Vancomycin','Meropenem','Levofloxacin']
# stylesheets = ['./resources/bWLwgP.css']
app = dash.Dash()
app.layout = html.Div([
    dcc.Dropdown(
        id = 'patient',
        value = '41976',
        multi = False,
        options = [{'label': i, 'value': i} for i in list_patient],
    ),
    dcc.Dropdown(
        id = 'biomarker',
        value = 'White Blood Cells',
        multi = False,
        options = [{'label': i, 'value': i} for i in list_biomarker],
    ),
    dcc.Dropdown(
        id = 'drug',
        value = ['Vancomycin'],
        multi = True,
        options = [{'label': i, 'value': i} for i in list_drug],
    ),
    dcc.Graph(id = 'graph'),
])
Explanation: Create a structure and presentation of your web with HTML and CSS (5/6)
End of explanation
@app.callback(Output('graph', 'figure'), 
              [Input('patient', 'value'),
               Input('biomarker', 'value'),
               Input('drug', 'value')])
def update_graph(patient, biomarker, drug):
    traces = []
    temp_l = lab[lab['subject_id'].astype(str) == patient]
    temp_p = presc[presc['subject_id'].astype(str) == patient]
    temp_min = 0
    
    item = biomarker
    temp = temp_l[temp_l['label'] == item]
    temp_min = float(temp.value.astype(float).min())
    trace = go.Scatter(
                x = temp.index,
                y = temp.value,
                name = item,
                mode = 'lines+markers',
            )
    traces.append(trace)
        
    for i, item in enumerate(drug):
        temp = temp_p[ temp_p['drug'] == item]
        trace = go.Scatter(
                    x = temp.index,
                    y = np.ones((1, len(temp)))[0] * temp_min - i - 1,
                    name = item,
                    mode = 'markers',
                    marker = {
                        'size': temp.norm_size * 10
                    }
                )
        traces.append(trace)
    
    layout = go.Layout(
        legend = {'x': 0.5, 'y': -0.1, 'orientation': 'h', 'xanchor': 'center'},
        margin = {'l': 300, 'b': 10, 't': 10, 'r': 300},
        hovermode = 'closest',
    )
    return {'data': traces, 'layout': layout}
app.run_server(port = 8050)
Explanation: Define the reactive behavior with Python (6/6)
End of explanation
<END_TASK>