Ashar086 commited on
Commit
6d689b7
·
verified ·
1 Parent(s): 0595dd0

Update components/animation_engineer.py

Browse files
Files changed (1) hide show
  1. components/animation_engineer.py +80 -99
components/animation_engineer.py CHANGED
@@ -1,8 +1,7 @@
1
  import streamlit as st
2
- import plotly.graph_objects as go
3
- from plotly.subplots import make_subplots
4
- import pandas as pd
5
- import numpy as np
6
 
7
  class AnimationEngineer:
8
  def __init__(self):
@@ -14,105 +13,87 @@ class AnimationEngineer:
14
  'physiotherapy': '#FFD700', # Gold
15
  'cognitive therapy': '#DDA0DD' # Plum
16
  }
 
 
17
 
18
- def display_care_plan_animation(self, final_plan, patient_data):
19
  st.write("Visualizing Care Plan...")
20
  steps = final_plan.split(", ")
21
 
22
- # Create a DataFrame for the care plan steps
23
- df = pd.DataFrame({
24
- 'Step': range(1, len(steps) + 1),
25
- 'Description': steps,
26
- 'Value': np.random.randint(50, 100, len(steps)) # Random values for visualization
27
- })
28
-
29
- # Create the main figure with subplots
30
- fig = make_subplots(
31
- rows=2, cols=2,
32
- subplot_titles=("Care Plan Progress", "Patient Vitals", "Treatment Effectiveness", "Overall Health Score"),
33
- specs=[[{"type": "indicator"}, {"type": "scatter"}],
34
- [{"type": "bar"}, {"type": "indicator"}]]
35
- )
36
-
37
- # Care Plan Progress Indicator
38
- fig.add_trace(go.Indicator(
39
- mode="gauge+number",
40
- value=0,
41
- title={'text': "Care Plan Progress"},
42
- gauge={'axis': {'range': [None, len(steps)]},
43
- 'steps': [{'range': [0, len(steps)], 'color': "lightgray"}],
44
- 'threshold': {'line': {'color': "red", 'width': 4}, 'thickness': 0.75, 'value': len(steps)}},
45
- domain={'row': 0, 'column': 0}
46
- ))
47
-
48
- # Patient Vitals Line Chart
49
- vitals = np.random.rand(10) * 10 + 90 # Simulated vital signs data
50
- fig.add_trace(go.Scatter(
51
- x=list(range(10)), y=vitals, mode='lines+markers', name='Vitals',
52
- line=dict(color='royalblue', width=2)
53
- ), row=1, col=2)
54
-
55
- # Treatment Effectiveness Bar Chart
56
- fig.add_trace(go.Bar(
57
- x=df['Step'], y=df['Value'], name='Effectiveness',
58
- marker_color=[self.colors.get(step.split()[0], '#FFFFFF') for step in df['Description']]
59
- ), row=2, col=1)
60
-
61
- # Overall Health Score Indicator
62
- fig.add_trace(go.Indicator(
63
- mode="gauge+number",
64
- value=75, # Starting value
65
- title={'text': "Overall Health Score"},
66
- gauge={'axis': {'range': [None, 100]},
67
- 'steps': [{'range': [0, 50], 'color': "lightgray"},
68
- {'range': [50, 75], 'color': "gray"}],
69
- 'threshold': {'line': {'color': "red", 'width': 4}, 'thickness': 0.75, 'value': 90}},
70
- domain={'row': 1, 'column': 1}
71
- ))
72
-
73
- # Update layout
74
- fig.update_layout(height=800, showlegend=False)
75
-
76
- # Display the initial plot
77
- plot_placeholder = st.empty()
78
- plot_placeholder.plotly_chart(fig, use_container_width=True)
79
-
80
- # Animate the care plan steps
81
- for i in range(len(steps)):
82
- # Update Care Plan Progress
83
- fig.data[0].value = i + 1
84
-
85
- # Update Patient Vitals
86
- new_vital = np.random.rand() * 10 + 90
87
- fig.data[1].y = list(fig.data[1].y) + [new_vital]
88
- fig.data[1].x = list(range(len(fig.data[1].y)))
89
-
90
- # Update Treatment Effectiveness
91
- fig.data[2].marker.color = [self.colors.get(step.split()[0], '#FFFFFF') if idx <= i else 'lightgray'
92
- for idx, step in enumerate(df['Description'])]
93
-
94
- # Update Overall Health Score
95
- fig.data[3].value = min(100, fig.data[3].value + np.random.randint(-5, 10))
96
-
97
- # Update the plot
98
- plot_placeholder.plotly_chart(fig, use_container_width=True)
99
-
100
- # Add a short pause between updates
101
- st.empty().text(f"Step {i+1}: {steps[i]}")
102
- st.empty().text(f"Processing...")
103
-
104
  st.success("Care Plan Visualization Complete!")
105
  st.balloons()
106
 
107
- def parse_patient_data(self, patient_data):
108
- # This is a placeholder. In a real scenario, you would parse the actual patient data.
109
- return {
110
- 'condition': patient_data.get('condition', 'Unknown'),
111
- 'age': patient_data.get('age', 50),
112
- 'gender': patient_data.get('gender', 'Unknown'),
113
- 'vitals': {
114
- 'heart_rate': np.random.randint(60, 100),
115
- 'blood_pressure': f"{np.random.randint(100, 140)}/{np.random.randint(60, 90)}",
116
- 'temperature': round(np.random.uniform(36.1, 37.2), 1)
117
- }
118
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import time
3
+ from PIL import Image, ImageDraw, ImageFont
4
+ import io
 
5
 
6
  class AnimationEngineer:
7
  def __init__(self):
 
13
  'physiotherapy': '#FFD700', # Gold
14
  'cognitive therapy': '#DDA0DD' # Plum
15
  }
16
+ self.frame_width = 600
17
+ self.frame_height = 400
18
 
19
+ def display_care_plan_animation(self, final_plan):
20
  st.write("Visualizing Care Plan...")
21
  steps = final_plan.split(", ")
22
 
23
+ # Create all frames
24
+ frames = self.create_all_frames(steps)
25
+
26
+ # Create a placeholder for our animation
27
+ animation_placeholder = st.empty()
28
+
29
+ # Display frames
30
+ for i, frame in enumerate(frames):
31
+ animation_placeholder.image(frame)
32
+ progress = int((i + 1) / len(frames) * 100)
33
+ st.progress(progress)
34
+ time.sleep(0.5) # Adjust speed of animation
35
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  st.success("Care Plan Visualization Complete!")
37
  st.balloons()
38
 
39
+ def create_all_frames(self, steps):
40
+ frames = []
41
+
42
+ # Create intro frame
43
+ intro_frame = self.create_intro_frame()
44
+ frames.append(intro_frame)
45
+
46
+ # Create step frames
47
+ for i, step in enumerate(steps):
48
+ step_frame = self.create_step_frame(step, i+1, len(steps))
49
+ frames.append(step_frame)
50
+
51
+ # Create transition frames
52
+ transition_frames = self.create_transition_frames()
53
+ frames.extend(transition_frames)
54
+
55
+ # Create final frame
56
+ final_frame = self.create_final_frame()
57
+ frames.append(final_frame)
58
+
59
+ return frames
60
+
61
+ def create_intro_frame(self):
62
+ img = Image.new('RGB', (self.frame_width, self.frame_height), color='white')
63
+ d = ImageDraw.Draw(img)
64
+ font = ImageFont.load_default()
65
+ d.text((self.frame_width//2, self.frame_height//2), "Care Plan Animation", fill='black', font=font, anchor="mm")
66
+ return img
67
+
68
+ def create_step_frame(self, step, step_num, total_steps):
69
+ img = Image.new('RGB', (self.frame_width, self.frame_height), color='white')
70
+ d = ImageDraw.Draw(img)
71
+
72
+ color = next((self.colors[key] for key in self.colors if key.lower() in step.lower()), '#FFFFFF')
73
+
74
+ d.rectangle([50, 50, self.frame_width-50, self.frame_height-50], fill=color, outline='black')
75
+
76
+ font = ImageFont.load_default()
77
+ d.text((self.frame_width//2, 70), f"Step {step_num}/{total_steps}", fill='black', font=font, anchor="mm")
78
+ d.text((self.frame_width//2, self.frame_height//2), step, fill='black', font=font, anchor="mm")
79
+
80
+ return img
81
+
82
+ def create_transition_frames(self):
83
+ frames = []
84
+ for i in range(10): # 10 transition frames
85
+ img = Image.new('RGB', (self.frame_width, self.frame_height), color='white')
86
+ d = ImageDraw.Draw(img)
87
+ size = 20 + i * 5 # Growing circle
88
+ d.ellipse([self.frame_width//2 - size, self.frame_height//2 - size,
89
+ self.frame_width//2 + size, self.frame_height//2 + size],
90
+ fill='red')
91
+ frames.append(img)
92
+ return frames
93
+
94
+ def create_final_frame(self):
95
+ img = Image.new('RGB', (self.frame_width, self.frame_height), color='white')
96
+ d = ImageDraw.Draw(img)
97
+ font = ImageFont.load_default()
98
+ d.text((self.frame_width//2, self.frame_height//2), "Care Plan Complete!", fill='black', font=font, anchor="mm")
99
+ return img