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
4 |
+
from sklearn.linear_model import LinearRegression
|
5 |
+
from sklearn.ensemble import RandomForestRegressor
|
6 |
+
from sklearn.svm import SVR
|
7 |
+
from sklearn.tree import DecisionTreeRegressor
|
8 |
+
from sklearn.ensemble import GradientBoostingRegressor
|
9 |
+
from sklearn.model_selection import train_test_split
|
10 |
+
import time
|
11 |
+
|
12 |
+
# Wide mode
|
13 |
+
st.set_page_config(layout="wide")
|
14 |
+
|
15 |
+
# Initialize parameters
|
16 |
+
params = {
|
17 |
+
'Beds': 100,
|
18 |
+
'Doctors': 50,
|
19 |
+
'Nurses': 100,
|
20 |
+
'Ventilators': 20,
|
21 |
+
'ICU_Beds': 20,
|
22 |
+
'Surgical_Suites': 5,
|
23 |
+
'Emergency_Room_Beds': 10,
|
24 |
+
'Pharmacy_Staff': 5,
|
25 |
+
'Lab_Staff': 5,
|
26 |
+
'Radiology_Staff': 5,
|
27 |
+
'Patient_Admissions': 50,
|
28 |
+
'Patient_Discharges': 40,
|
29 |
+
'Average_Stay': 5,
|
30 |
+
'ICU_Admissions': 10,
|
31 |
+
'Surgical_Cases': 20,
|
32 |
+
'Emergency_Room_Visits': 30,
|
33 |
+
'Pharmacy_Requests': 50,
|
34 |
+
'Lab_Requests': 40,
|
35 |
+
'Radiology_Requests': 30
|
36 |
+
}
|
37 |
+
|
38 |
+
# Create a Streamlit app
|
39 |
+
st.title("Hospital Resource Allocation Simulator")
|
40 |
+
|
41 |
+
# Add a sidebar for input parameters
|
42 |
+
st.sidebar.header("Input Parameters")
|
43 |
+
for param, value in params.items():
|
44 |
+
params[param] = st.sidebar.slider(param, 0, 200, value)
|
45 |
+
|
46 |
+
# Create a button to start the simulation
|
47 |
+
if st.sidebar.button("Start Live Simulation"):
|
48 |
+
st.header("Live Simulation")
|
49 |
+
st.write("Simulation started. Graphs will update every second to represent hourly changes in resource allocation.")
|
50 |
+
|
51 |
+
# Initialize dataframes to store simulation data
|
52 |
+
beds_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Occupied'])
|
53 |
+
doctors_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Busy'])
|
54 |
+
nurses_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Busy'])
|
55 |
+
ventilators_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'In_Use'])
|
56 |
+
icu_beds_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Occupied'])
|
57 |
+
surgical_suites_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'In_Use'])
|
58 |
+
emergency_room_beds_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Occupied'])
|
59 |
+
pharmacy_staff_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Busy'])
|
60 |
+
lab_staff_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Busy'])
|
61 |
+
radiology_staff_df = pd.DataFrame(index=pd.date_range(start='2022-01-01', periods=24, freq='H'), columns=['Available', 'Busy'])
|
62 |
+
|
63 |
+
# Initialize log for resource usage
|
64 |
+
log = []
|
65 |
+
|
66 |
+
# Generate synthetic data for training
|
67 |
+
np.random.seed(42)
|
68 |
+
X = pd.DataFrame(np.random.randint(0, 100, size=(100, len(params))), columns=params.keys())
|
69 |
+
y = np.random.randint(0, 100, size=100) # Dummy target for demonstration
|
70 |
+
|
71 |
+
# Split data for training and testing
|
72 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
73 |
+
|
74 |
+
# Initialize regression models
|
75 |
+
models = {
|
76 |
+
'Linear Regression': LinearRegression(),
|
77 |
+
'Random Forest Regression': RandomForestRegressor(),
|
78 |
+
'SVR': SVR(),
|
79 |
+
'Decision Tree Regression': DecisionTreeRegressor(),
|
80 |
+
'Gradient Boosting Regression': GradientBoostingRegressor()
|
81 |
+
}
|
82 |
+
|
83 |
+
# Predict values for each parameter using each model
|
84 |
+
predicted_values = {}
|
85 |
+
for name, model in models.items():
|
86 |
+
predictions = model.fit(X_train, y_train).predict(X_test)
|
87 |
+
predicted_values[name] = predictions
|
88 |
+
|
89 |
+
# Display predicted values
|
90 |
+
# st.subheader("Predicted Parameter Values")
|
91 |
+
# predicted_df = pd.DataFrame(predicted_values, index=X_test.index)
|
92 |
+
# st.write(predicted_df)
|
93 |
+
|
94 |
+
# Create a grid layout for the graphs
|
95 |
+
col1, col2, col3 = st.columns(3)
|
96 |
+
|
97 |
+
with col1:
|
98 |
+
st.subheader("Beds Availability")
|
99 |
+
beds_chart = st.line_chart(beds_df)
|
100 |
+
|
101 |
+
with col2:
|
102 |
+
st.subheader("Doctors Availability")
|
103 |
+
doctors_chart = st.line_chart(doctors_df)
|
104 |
+
|
105 |
+
with col3:
|
106 |
+
st.subheader("Nurses Availability")
|
107 |
+
nurses_chart = st.line_chart(nurses_df)
|
108 |
+
|
109 |
+
col4, col5, col6 = st.columns(3)
|
110 |
+
|
111 |
+
with col4:
|
112 |
+
st.subheader("Ventilators Availability")
|
113 |
+
ventilators_chart = st.line_chart(ventilators_df)
|
114 |
+
|
115 |
+
with col5:
|
116 |
+
st.subheader("ICU Beds Availability")
|
117 |
+
icu_beds_chart = st.line_chart(icu_beds_df)
|
118 |
+
|
119 |
+
with col6:
|
120 |
+
st.subheader("Surgical Suites Availability")
|
121 |
+
surgical_suites_chart = st.line_chart(surgical_suites_df)
|
122 |
+
|
123 |
+
col7, col8, col9 = st.columns(3)
|
124 |
+
|
125 |
+
with col7:
|
126 |
+
st.subheader("Emergency Room Beds Availability")
|
127 |
+
emergency_room_beds_chart = st.line_chart(emergency_room_beds_df)
|
128 |
+
|
129 |
+
with col8:
|
130 |
+
st.subheader("Pharmacy Staff Availability")
|
131 |
+
pharmacy_staff_chart = st.line_chart(pharmacy_staff_df)
|
132 |
+
|
133 |
+
with col9:
|
134 |
+
st.subheader("Lab Staff Availability")
|
135 |
+
lab_staff_chart = st.line_chart(lab_staff_df)
|
136 |
+
|
137 |
+
col10, col11, col12 = st.columns(3)
|
138 |
+
|
139 |
+
with col10:
|
140 |
+
st.subheader("Radiology Staff Availability")
|
141 |
+
radiology_staff_chart = st.line_chart(radiology_staff_df)
|
142 |
+
|
143 |
+
# Start the simulation
|
144 |
+
for i in range(24):
|
145 |
+
# Simulate patient admissions and discharges
|
146 |
+
admissions = np.random.poisson(params['Patient_Admissions'])
|
147 |
+
discharges = np.random.poisson(params['Patient_Discharges'])
|
148 |
+
|
149 |
+
# Update bed availability
|
150 |
+
beds_df.iloc[i, 0] = params['Beds'] - admissions + discharges
|
151 |
+
beds_df.iloc[i, 1] = admissions
|
152 |
+
|
153 |
+
# Simulate doctor and nurse availability
|
154 |
+
doctors_available = params['Doctors'] - (admissions * 0.6 + params['ICU_Admissions'] * 0.4)
|
155 |
+
nurses_available = params['Nurses'] - (admissions * 1.4 + params['ICU_Admissions'] * 0.6)
|
156 |
+
doctors_df.iloc[i, 0] = doctors_available
|
157 |
+
doctors_df.iloc[i, 1] = admissions * 0.25
|
158 |
+
nurses_df.iloc[i, 0] = nurses_available
|
159 |
+
nurses_df.iloc[i, 1] = admissions * 0.8
|
160 |
+
|
161 |
+
# Simulate ventilator availability
|
162 |
+
ventilators_available = params['Ventilators'] - (admissions * 0.1 + params['ICU_Admissions'] * 0.2)
|
163 |
+
ventilators_df.iloc[i, 0] = ventilators_available
|
164 |
+
ventilators_df.iloc[i, 1] = admissions * 0.05
|
165 |
+
|
166 |
+
# Simulate ICU bed availability
|
167 |
+
icu_beds_available = params['ICU_Beds'] - (admissions * 0.1 + params['ICU_Admissions'] * 0.5)
|
168 |
+
icu_beds_df.iloc[i, 0] = icu_beds_available
|
169 |
+
icu_beds_df.iloc[i, 1] = admissions * 0.08
|
170 |
+
|
171 |
+
# Simulate surgical suite availability
|
172 |
+
surgical_suites_available = params['Surgical_Suites'] - (admissions * 0.1 + params['Surgical_Cases'] * 0.4)
|
173 |
+
surgical_suites_df.iloc[i, 0] = surgical_suites_available
|
174 |
+
surgical_suites_df.iloc[i, 1] = admissions * 0.05
|
175 |
+
|
176 |
+
# Simulate emergency room bed availability
|
177 |
+
emergency_room_beds_available = params['Emergency_Room_Beds'] - (admissions * 0.1 + params['Emergency_Room_Visits'] * 0.5)
|
178 |
+
emergency_room_beds_df.iloc[i, 0] = emergency_room_beds_available
|
179 |
+
emergency_room_beds_df.iloc[i, 1] = admissions * 0.1
|
180 |
+
|
181 |
+
# Simulate pharmacy, lab, and radiology staff availability
|
182 |
+
pharmacy_requests = params['Pharmacy_Requests'] + params['ICU_Admissions'] * 0.1 + admissions * 0.1
|
183 |
+
lab_requests = params['Lab_Requests'] + params['ICU_Admissions'] * 0.1 + admissions * 0.1
|
184 |
+
radiology_requests = params['Radiology_Requests'] + params['ICU_Admissions'] * 0.1 + admissions * 0.1
|
185 |
+
|
186 |
+
pharmacy_staff_available = params['Pharmacy_Staff'] - pharmacy_requests * 0.1
|
187 |
+
lab_staff_available = params['Lab_Staff'] - lab_requests * 0.1
|
188 |
+
radiology_staff_available = params['Radiology_Staff'] - radiology_requests * 0.1
|
189 |
+
|
190 |
+
pharmacy_staff_df.iloc[i, 0] = pharmacy_staff_available
|
191 |
+
pharmacy_staff_df.iloc[i, 1] = pharmacy_requests * 0.1
|
192 |
+
lab_staff_df.iloc[i, 0] = lab_staff_available
|
193 |
+
lab_staff_df.iloc[i, 1] = lab_requests * 0.1
|
194 |
+
radiology_staff_df.iloc[i, 0] = radiology_staff_available
|
195 |
+
radiology_staff_df.iloc[i, 1] = radiology_requests * 0.1
|
196 |
+
|
197 |
+
# Update graphs with new data
|
198 |
+
beds_chart.line_chart(beds_df)
|
199 |
+
doctors_chart.line_chart(doctors_df)
|
200 |
+
nurses_chart.line_chart(nurses_df)
|
201 |
+
ventilators_chart.line_chart(ventilators_df)
|
202 |
+
icu_beds_chart.line_chart(icu_beds_df)
|
203 |
+
surgical_suites_chart.line_chart(surgical_suites_df)
|
204 |
+
emergency_room_beds_chart.line_chart(emergency_room_beds_df)
|
205 |
+
pharmacy_staff_chart.line_chart(pharmacy_staff_df)
|
206 |
+
lab_staff_chart.line_chart(lab_staff_df)
|
207 |
+
radiology_staff_chart.line_chart(radiology_staff_df)
|
208 |
+
|
209 |
+
# Log resource usage for each patient
|
210 |
+
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]}")
|
211 |
+
|
212 |
+
# Wait for 1 second before updating the simulation
|
213 |
+
time.sleep(1)
|
214 |
+
|
215 |
+
# Display the resource usage log
|
216 |
+
st.subheader("Resource Usage Log")
|
217 |
+
for entry in log:
|
218 |
+
st.write(entry)
|
219 |
+
|