phyjaafar commited on
Commit
08fa91e
·
verified ·
1 Parent(s): ac5bab0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -12
app.py CHANGED
@@ -1,25 +1,40 @@
1
  import gradio as gr
2
  import numpy as np
 
3
 
4
- def calculate_tia(t1, a1, t2, a2):
5
  try:
6
- # Calculate washout rate constant (k)
7
  k = np.log(a1 / a2) / (t2 - t1)
8
- # Estimate initial activity (A₀)
9
  a0 = a1 / np.exp(-k * t1)
10
- # Calculate Time-Integrated Activity (TIA)
11
  tia = a0 / k
12
 
13
  result = f"""
14
  ### ✅ Results
15
  - **A₀ (initial activity):** {a0:.2f} MBq
16
  - **k (washout rate):** {k:.5f} / hour
17
- - **TIA (time-integrated activity):** {tia:.2f} MBq·h
18
  """
19
- except Exception:
20
- result = "⚠️ Error: Please check your inputs. Values must be positive and non-zero."
21
 
22
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  with gr.Blocks() as demo:
25
  gr.Markdown("# ⚛️ Lu-177 Dosimetry Tool\nEstimate Time-Integrated Activity (TIA) from Two SPECT Time Points")
@@ -32,9 +47,11 @@ with gr.Blocks() as demo:
32
  t2 = gr.Number(label="Time Point 2 (hours)", value=60)
33
  a2 = gr.Number(label="Activity at Time 2 (MBq)", value=4)
34
 
35
- output = gr.Markdown()
36
- button = gr.Button("Calculate TIA")
37
- button.click(fn=calculate_tia, inputs=[t1, a1, t2, a2], outputs=output)
 
 
38
 
39
  gr.Markdown("""
40
  ---
@@ -66,5 +83,5 @@ with gr.Blocks() as demo:
66
 
67
  ---
68
  """)
69
-
70
  demo.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
+ import matplotlib.pyplot as plt
4
 
5
+ def calculate_and_plot(t1, a1, t2, a2):
6
  try:
7
+ # الحسابات الأساسية
8
  k = np.log(a1 / a2) / (t2 - t1)
 
9
  a0 = a1 / np.exp(-k * t1)
 
10
  tia = a0 / k
11
 
12
  result = f"""
13
  ### ✅ Results
14
  - **A₀ (initial activity):** {a0:.2f} MBq
15
  - **k (washout rate):** {k:.5f} / hour
16
+ - **TIA:** {tia:.2f} MBq·h
17
  """
 
 
18
 
19
+ # إنشاء نقاط الرسم
20
+ t_points = np.linspace(0, max(t2 + 40, 200), 100)
21
+ a_points = a0 * np.exp(-k * t_points)
22
+
23
+ # رسم المنحنى باستخدام matplotlib
24
+ fig, ax = plt.subplots(figsize=(6, 4))
25
+ ax.plot(t_points, a_points, label="Fitted Curve", color='blue')
26
+ ax.scatter([t1, t2], [a1, a2], color='red', label="Input Points")
27
+ ax.set_xlabel("Time (hours)")
28
+ ax.set_ylabel("Activity (MBq)")
29
+ ax.set_title("Mono-Exponential Washout Curve")
30
+ ax.grid(True)
31
+ ax.legend()
32
+ plt.tight_layout()
33
+
34
+ return result, fig
35
+
36
+ except Exception:
37
+ return "⚠️ Error: Please check your inputs.", None
38
 
39
  with gr.Blocks() as demo:
40
  gr.Markdown("# ⚛️ Lu-177 Dosimetry Tool\nEstimate Time-Integrated Activity (TIA) from Two SPECT Time Points")
 
47
  t2 = gr.Number(label="Time Point 2 (hours)", value=60)
48
  a2 = gr.Number(label="Activity at Time 2 (MBq)", value=4)
49
 
50
+ output_text = gr.Markdown()
51
+ output_plot = gr.Plot()
52
+ button = gr.Button("Calculate & Plot TIA")
53
+
54
+ button.click(fn=calculate_and_plot, inputs=[t1, a1, t2, a2], outputs=[output_text, output_plot])
55
 
56
  gr.Markdown("""
57
  ---
 
83
 
84
  ---
85
  """)
86
+
87
  demo.launch()