Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
from sklearn.linear_model import LinearRegression | |
from sklearn.ensemble import RandomForestRegressor | |
from sklearn.svm import SVR | |
from sklearn.tree import DecisionTreeRegressor | |
from sklearn.ensemble import GradientBoostingRegressor | |
from sklearn.model_selection import train_test_split | |
import time | |
# Wide mode | |
st.set_page_config(layout="wide") | |
# Initialize parameters | |
params = { | |
'Beds': 100, | |
'Doctors': 50, | |
'Nurses': 100, | |
'Ventilators': 20, | |
'ICU_Beds': 20, | |
'Surgical_Suites': 5, | |
'Emergency_Room_Beds': 10, | |
'Pharmacy_Staff': 5, | |
'Lab_Staff': 5, | |
'Radiology_Staff': 5, | |
'Patient_Admissions': 50, | |
'Patient_Discharges': 40, | |
'Average_Stay': 5, | |
'ICU_Admissions': 10, | |
'Surgical_Cases': 20, | |
'Emergency_Room_Visits': 30, | |
'Pharmacy_Requests': 50, | |
'Lab_Requests': 40, | |
'Radiology_Requests': 30 | |
} | |
# Create a Streamlit app | |
st.title("Hospital Resource Allocation Simulator") | |
# Add a sidebar for input parameters | |
st.sidebar.header("Input Parameters") | |
for param, value in params.items(): | |
params[param] = st.sidebar.slider(param, 0, 200, value) | |
# Create a button to start the simulation | |
if st.sidebar.button("Start Live Simulation"): | |
st.header("Live Simulation") | |
st.write("Simulation started. Graphs will update every second to represent hourly changes in resource allocation.") | |
# Initialize dataframes to store simulation data | |
beds_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Occupied']) | |
doctors_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Busy']) | |
nurses_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Busy']) | |
ventilators_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'In_Use']) | |
icu_beds_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Occupied']) | |
surgical_suites_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'In_Use']) | |
emergency_room_beds_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Occupied']) | |
pharmacy_staff_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Busy']) | |
lab_staff_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Busy']) | |
radiology_staff_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Busy']) | |
# Initialize log for resource usage | |
log = [] | |
# Generate synthetic data for training | |
np.random.seed(42) | |
X = pd.DataFrame(np.random.randint(0, 100, size=(100, len(params))), columns=params.keys()) | |
y = np.random.randint(0, 100, size=100) # Dummy target for demonstration | |
# Split data for training and testing | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
# Initialize regression models | |
models = { | |
'Linear Regression': LinearRegression(), | |
'Random Forest Regression': RandomForestRegressor(), | |
'SVR': SVR(), | |
'Decision Tree Regression': DecisionTreeRegressor(), | |
'Gradient Boosting Regression': GradientBoostingRegressor() | |
} | |
# Predict values for each parameter using each model | |
predicted_values = {} | |
for name, model in models.items(): | |
predictions = model.fit(X_train, y_train).predict(X_test) | |
predicted_values[name] = predictions | |
# Display predicted values | |
# st.subheader("Predicted Parameter Values") | |
# predicted_df = pd.DataFrame(predicted_values, index=X_test.index) | |
# st.write(predicted_df) | |
# Create a grid layout for the graphs | |
col1, col2, col3 = st.columns(3) | |
with col1: | |
st.subheader("Beds Availability") | |
beds_chart = st.line_chart(beds_df) | |
with col2: | |
st.subheader("Doctors Availability") | |
doctors_chart = st.line_chart(doctors_df) | |
with col3: | |
st.subheader("Nurses Availability") | |
nurses_chart = st.line_chart(nurses_df) | |
col4, col5, col6 = st.columns(3) | |
with col4: | |
st.subheader("Ventilators Availability") | |
ventilators_chart = st.line_chart(ventilators_df) | |
with col5: | |
st.subheader("ICU Beds Availability") | |
icu_beds_chart = st.line_chart(icu_beds_df) | |
with col6: | |
st.subheader("Surgical Suites Availability") | |
surgical_suites_chart = st.line_chart(surgical_suites_df) | |
col7, col8, col9 = st.columns(3) | |
with col7: | |
st.subheader("Emergency Room Beds Availability") | |
emergency_room_beds_chart = st.line_chart(emergency_room_beds_df) | |
with col8: | |
st.subheader("Pharmacy Staff Availability") | |
pharmacy_staff_chart = st.line_chart(pharmacy_staff_df) | |
with col9: | |
st.subheader("Lab Staff Availability") | |
lab_staff_chart = st.line_chart(lab_staff_df) | |
col10, col11, col12 = st.columns(3) | |
with col10: | |
st.subheader("Radiology Staff Availability") | |
radiology_staff_chart = st.line_chart(radiology_staff_df) | |
# Start the simulation | |
for i in range(24): | |
# Simulate patient admissions and discharges | |
admissions = np.random.poisson(params['Patient_Admissions']) | |
discharges = np.random.poisson(params['Patient_Discharges']) | |
# Update bed availability | |
beds_df.iloc[i, 0] = params['Beds'] - admissions + discharges | |
beds_df.iloc[i, 1] = admissions | |
# Simulate doctor and nurse availability | |
doctors_available = params['Doctors'] - (admissions * 0.6 + params['ICU_Admissions'] * 0.4) | |
nurses_available = params['Nurses'] - (admissions * 1.4 + params['ICU_Admissions'] * 0.6) | |
doctors_df.iloc[i, 0] = doctors_available | |
doctors_df.iloc[i, 1] = admissions * 0.25 | |
nurses_df.iloc[i, 0] = nurses_available | |
nurses_df.iloc[i, 1] = admissions * 0.8 | |
# Simulate ventilator availability | |
ventilators_available = params['Ventilators'] - (admissions * 0.1 + params['ICU_Admissions'] * 0.2) | |
ventilators_df.iloc[i, 0] = ventilators_available | |
ventilators_df.iloc[i, 1] = admissions * 0.05 | |
# Simulate ICU bed availability | |
icu_beds_available = params['ICU_Beds'] - (admissions * 0.1 + params['ICU_Admissions'] * 0.5) | |
icu_beds_df.iloc[i, 0] = icu_beds_available | |
icu_beds_df.iloc[i, 1] = admissions * 0.08 | |
# Simulate surgical suite availability | |
surgical_suites_available = params['Surgical_Suites'] - (admissions * 0.1 + params['Surgical_Cases'] * 0.4) | |
surgical_suites_df.iloc[i, 0] = surgical_suites_available | |
surgical_suites_df.iloc[i, 1] = admissions * 0.05 | |
# Simulate emergency room bed availability | |
emergency_room_beds_available = params['Emergency_Room_Beds'] - (admissions * 0.1 + params['Emergency_Room_Visits'] * 0.5) | |
emergency_room_beds_df.iloc[i, 0] = emergency_room_beds_available | |
emergency_room_beds_df.iloc[i, 1] = admissions * 0.1 | |
# Simulate pharmacy, lab, and radiology staff availability | |
pharmacy_requests = params['Pharmacy_Requests'] + params['ICU_Admissions'] * 0.1 + admissions * 0.1 | |
lab_requests = params['Lab_Requests'] + params['ICU_Admissions'] * 0.1 + admissions * 0.1 | |
radiology_requests = params['Radiology_Requests'] + params['ICU_Admissions'] * 0.1 + admissions * 0.1 | |
pharmacy_staff_available = params['Pharmacy_Staff'] - pharmacy_requests * 0.1 | |
lab_staff_available = params['Lab_Staff'] - lab_requests * 0.1 | |
radiology_staff_available = params['Radiology_Staff'] - radiology_requests * 0.1 | |
pharmacy_staff_df.iloc[i, 0] = pharmacy_staff_available | |
pharmacy_staff_df.iloc[i, 1] = pharmacy_requests * 0.1 | |
lab_staff_df.iloc[i, 0] = lab_staff_available | |
lab_staff_df.iloc[i, 1] = lab_requests * 0.1 | |
radiology_staff_df.iloc[i, 0] = radiology_staff_available | |
radiology_staff_df.iloc[i, 1] = radiology_requests * 0.1 | |
# Update graphs with new data | |
beds_chart.line_chart(beds_df) | |
doctors_chart.line_chart(doctors_df) | |
nurses_chart.line_chart(nurses_df) | |
ventilators_chart.line_chart(ventilators_df) | |
icu_beds_chart.line_chart(icu_beds_df) | |
surgical_suites_chart.line_chart(surgical_suites_df) | |
emergency_room_beds_chart.line_chart(emergency_room_beds_df) | |
pharmacy_staff_chart.line_chart(pharmacy_staff_df) | |
lab_staff_chart.line_chart(lab_staff_df) | |
radiology_staff_chart.line_chart(radiology_staff_df) | |
# Log resource usage for each patient | |
log.append(f"Hour {i}: Beds Available - {beds_df.iloc[i, 0]}, Beds Occupied - {beds_df.iloc[i, 1]}, Doctors Available - {doctors_df.iloc[i, 0]}, Doctors Busy - {doctors_df.iloc[i, 1]}, Nurses Available - {nurses_df.iloc[i, 0]}, Nurses Busy - {nurses_df.iloc[i, 1]}, Ventilators Available - {ventilators_df.iloc[i, 0]}, Ventilators In Use - {ventilators_df.iloc[i, 1]}, ICU Beds Available - {icu_beds_df.iloc[i, 0]}, ICU Beds Occupied - {icu_beds_df.iloc[i, 1]}, Surgical Suites Available - {surgical_suites_df.iloc[i, 0]}, Surgical Suites In Use - {surgical_suites_df.iloc[i, 1]}, Emergency Room Beds Available - {emergency_room_beds_df.iloc[i, 0]}, Emergency Room Beds Occupied - {emergency_room_beds_df.iloc[i, 1]}, Pharmacy Staff Available - {pharmacy_staff_df.iloc[i, 0]}, Pharmacy Staff Busy - {pharmacy_staff_df.iloc[i, 1]}, Lab Staff Available - {lab_staff_df.iloc[i, 0]}, Lab Staff Busy - {lab_staff_df.iloc[i, 1]}, Radiology Staff Available - {radiology_staff_df.iloc[i, 0]}, Radiology Staff Busy - {radiology_staff_df.iloc[i, 1]}") | |
# Wait for 1 second before updating the simulation | |
time.sleep(1) | |
# Display the resource usage log | |
st.subheader("Resource Usage Log") | |
for entry in log: | |
st.write(entry) | |