Nicha1234 commited on
Commit
8a73f1c
·
verified ·
1 Parent(s): 0ec7f7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -23
app.py CHANGED
@@ -14,8 +14,12 @@ st.set_page_config(layout="wide", page_title="K-Space to MRI")
14
 
15
  st.markdown("""
16
  <style>
17
- /* CSS แก้ปัญหาการสั่นของหน้าจอ */
 
 
 
18
  .stApp { transition: none !important; }
 
19
  html, body, [class*="st-"] {
20
  font-size: 18px;
21
  }
@@ -92,7 +96,7 @@ kspace_bg_image = format_kspace_display(kspace_raw)
92
 
93
  def get_image_from_plot(fig):
94
  buf = io.BytesIO()
95
- # ลบ bbox_inches='tight' ออกเพื่อไม่ให้รูปภาพเปลี่ยนขนาดไปมา (แก้อาการสั่น)
96
  plt.savefig(buf, format='png', dpi=100)
97
  plt.close(fig)
98
  buf.seek(0)
@@ -105,27 +109,27 @@ def draw_kspace_diagram():
105
  rect = plt.Rectangle((-1, -1), 2, 2, fill=False, edgecolor='black', lw=3)
106
  ax.add_patch(rect)
107
 
108
- # วาดแกนลูกศรตัดกันตรงกลาง (อยู่ภายในกรอบ)
109
- ax.annotate('', xy=(0.9, 0), xytext=(-0.9, 0), arrowprops=dict(arrowstyle='<|-|>', color='black', lw=2))
110
- ax.annotate('', xy=(0, 0.9), xytext=(0, -0.9), arrowprops=dict(arrowstyle='<|-|>', color='black', lw=2))
111
 
112
- # ตัวอักษรบอกทิศทาง +kx, -kx, +ky, -ky (อยู่นอกกรอบ)
113
  ax.text(1.1, 0, '+kx', fontsize=20, fontweight='bold', va='center')
114
- ax.text(-1.2, 0, '-kx', fontsize=20, fontweight='bold', va='center', ha='right')
115
  ax.text(0, 1.1, '+ky', fontsize=20, fontweight='bold', ha='center')
116
  ax.text(0, -1.1, '-ky', fontsize=20, fontweight='bold', ha='center', va='top')
117
 
118
- # Label บอกชื่อแกนพร้อมลู
119
  # Frequency axis ด้านล่าง
120
- ax.annotate('', xy=(0.9, -1.4), xytext=(-0.9, -1.4), arrowprops=dict(arrowstyle='<|-|>', color='black', lw=4))
121
- ax.text(0, -1.5, 'kx (Frequency)', ha='center', va='top', fontsize=16, fontweight='bold')
122
 
123
  # Phase axis ด้านซ้าย
124
- ax.annotate('', xy=(-1.5, 0.9), xytext=(-1.5, -0.9), arrowprops=dict(arrowstyle='<|-|>', color='black', lw=4))
125
- ax.text(-1.6, 0, 'ky (Phase)', ha='right', va='center', rotation=90, fontsize=16, fontweight='bold')
126
 
127
- ax.set_xlim(-2.0, 1.5)
128
- ax.set_ylim(-2.0, 1.5)
129
  ax.axis('off')
130
 
131
  plt.tight_layout()
@@ -173,13 +177,16 @@ def draw_filtered_kspace(filtered_k):
173
  ax.axis('off')
174
  return get_image_from_plot(fig)
175
 
 
176
  def draw_mri(mri_result):
177
  fig, ax = plt.subplots(figsize=(4, 4))
178
  if np.max(mri_result) > 0:
179
- vmax = np.percentile(mri_result, 99.5) # ตัด noise สว่างจ้าออก เพื่อให้เนื้อสมองสว่างขึ้
 
180
  else:
181
- vmax = 1
182
- ax.imshow(np.flipud(mri_result), cmap='gray', vmin=0, vmax=vmax)
 
183
  ax.axis('off')
184
  return get_image_from_plot(fig)
185
 
@@ -327,15 +334,12 @@ with col_main:
327
 
328
  st.progress((st.session_state.fill_step + 1) / total_anim_steps)
329
 
 
330
  col_anim1, col_anim2 = st.columns([1, 1])
331
  with col_anim1:
332
- img1_ph = st.empty()
333
- # นำ use_container_width=True ออกเพื่อล็อกขนาดภาพไม่ให้สั่น
334
- img1_ph.image(draw_pulse_sequence(st.session_state.fill_step, total_anim_steps))
335
  with col_anim2:
336
- img2_ph = st.empty()
337
- # นำ use_container_width=True ออกเพื่อล็อกขนาดภาพไม่ให้สั่น
338
- img2_ph.image(draw_kspace_filling(st.session_state.fill_step, total_anim_steps))
339
 
340
  st.markdown("""
341
  ---
 
14
 
15
  st.markdown("""
16
  <style>
17
+ /* CSS แก้ปัญหาการสั่นของหน้าจอ (บังคับให้มี Scrollbar ตลอดเวลา หน้าเว็บจะได้ไม่หดขยาย) */
18
+ html, body {
19
+ overflow-y: scroll !important;
20
+ }
21
  .stApp { transition: none !important; }
22
+
23
  html, body, [class*="st-"] {
24
  font-size: 18px;
25
  }
 
96
 
97
  def get_image_from_plot(fig):
98
  buf = io.BytesIO()
99
+ # ลบ bbox_inches='tight' ออกเพื่อไม่ให้รูปภาพเปลี่ยนขนาดพิกเซลไปมา (แก้อาการสั่น)
100
  plt.savefig(buf, format='png', dpi=100)
101
  plt.close(fig)
102
  buf.seek(0)
 
109
  rect = plt.Rectangle((-1, -1), 2, 2, fill=False, edgecolor='black', lw=3)
110
  ax.add_patch(rect)
111
 
112
+ # วาดแกนลูกศรตัดกันตรงกลาง
113
+ ax.annotate('', xy=(0.95, 0), xytext=(-0.95, 0), arrowprops=dict(arrowstyle='<|-|>', color='black', lw=2))
114
+ ax.annotate('', xy=(0, 0.95), xytext=(0, -0.95), arrowprops=dict(arrowstyle='<|-|>', color='black', lw=2))
115
 
116
+ # ตัวอักษรบอกทิศทาง +kx, -kx, +ky, -ky
117
  ax.text(1.1, 0, '+kx', fontsize=20, fontweight='bold', va='center')
118
+ ax.text(-1.1, 0, '-kx', fontsize=20, fontweight='bold', va='center', ha='right')
119
  ax.text(0, 1.1, '+ky', fontsize=20, fontweight='bold', ha='center')
120
  ax.text(0, -1.1, '-ky', fontsize=20, fontweight='bold', ha='center', va='top')
121
 
122
+ # Label บอกชื่อแกน (เอาไวข้างนอกอบแบบในรูป)
123
  # Frequency axis ด้านล่าง
124
+ ax.annotate('', xy=(1, -1.3), xytext=(-1, -1.3), arrowprops=dict(arrowstyle='<|-|>', color='black', lw=4))
125
+ ax.text(0, -1.4, 'kx (Frequency)', ha='center', va='top', fontsize=16, fontweight='bold')
126
 
127
  # Phase axis ด้านซ้าย
128
+ ax.annotate('', xy=(-1.3, 1), xytext=(-1.3, -1), arrowprops=dict(arrowstyle='<|-|>', color='black', lw=4))
129
+ ax.text(-1.4, 0, 'ky (Phase)', ha='right', va='center', rotation=90, fontsize=16, fontweight='bold')
130
 
131
+ ax.set_xlim(-1.8, 1.4)
132
+ ax.set_ylim(-1.8, 1.4)
133
  ax.axis('off')
134
 
135
  plt.tight_layout()
 
177
  ax.axis('off')
178
  return get_image_from_plot(fig)
179
 
180
+ # ฟังก์ชัน MRI แก้ให้ภาพสว่างใส โดยตัด Noise พิกเซลทิ้ง
181
  def draw_mri(mri_result):
182
  fig, ax = plt.subplots(figsize=(4, 4))
183
  if np.max(mri_result) > 0:
184
+ # ตัดพิกเซลที่สว่างหรืมืดเกินไป (Noise) ไป 1% เพื่อให้เนื้อสมองสว่างชัดเจ
185
+ vmin, vmax = np.percentile(mri_result, (1, 99))
186
  else:
187
+ vmin, vmax = 0, 1
188
+
189
+ ax.imshow(np.flipud(mri_result), cmap='gray', vmin=vmin, vmax=vmax)
190
  ax.axis('off')
191
  return get_image_from_plot(fig)
192
 
 
334
 
335
  st.progress((st.session_state.fill_step + 1) / total_anim_steps)
336
 
337
+ # นำ st.empty() ออก และแสดงภาพโดยตรงเพื่อลดการเคลียร์หน้าจอที่จะทำให้เว็บกระตุก
338
  col_anim1, col_anim2 = st.columns([1, 1])
339
  with col_anim1:
340
+ st.image(draw_pulse_sequence(st.session_state.fill_step, total_anim_steps))
 
 
341
  with col_anim2:
342
+ st.image(draw_kspace_filling(st.session_state.fill_step, total_anim_steps))
 
 
343
 
344
  st.markdown("""
345
  ---