mathpluscode commited on
Commit
5e04242
·
1 Parent(s): 0ee1a64

Close matplotlib figures

Browse files
Files changed (2) hide show
  1. app.py +18 -8
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,11 +1,16 @@
1
  from pathlib import Path
2
 
3
  import gradio as gr
 
4
  import numpy as np
5
  import requests
6
  import SimpleITK as sitk # noqa: N813
7
  import spaces
8
  import torch
 
 
 
 
9
  from cinema import CineMA, ConvUNetR, ConvViT, heatmap_soft_argmax
10
  from cinema.examples.cine_cmr import plot_cmr_views
11
  from cinema.examples.inference.landmark_heatmap import (
@@ -29,9 +34,6 @@ from cinema.examples.inference.segmentation_sax import (
29
  from cinema.examples.inference.segmentation_sax import (
30
  plot_volume_changes as plot_volume_changes_sax,
31
  )
32
- from huggingface_hub import hf_hub_download
33
- from monai.transforms import Compose, ScaleIntensityd, SpatialPadd
34
- from tqdm import tqdm
35
 
36
  # cache directories
37
  cache_dir = Path("/tmp/.cinema")
@@ -220,6 +222,7 @@ def mae(image_id, mask_ratio, progress=gr.Progress()):
220
  reconstructed_dict,
221
  masks_dict,
222
  )
 
223
  return fig
224
 
225
 
@@ -331,8 +334,10 @@ def segmentation_sax(trained_dataset, seed, image_id, t_step, progress=gr.Progre
331
  progress(1, desc="Plotting results...")
332
  fig1 = plot_segmentations_sax(images, labels, t_step)
333
  fig2 = plot_volume_changes_sax(labels, t_step)
334
-
335
- return fig1, fig2
 
 
336
 
337
 
338
  def segmentation_sax_tab():
@@ -471,8 +476,10 @@ def segmentation_lax(seed, image_id, progress=gr.Progress()):
471
  progress(1, desc="Plotting results...")
472
  fig1 = plot_segmentations_lax(images, labels)
473
  fig2 = plot_volume_changes_lax(labels)
474
-
475
- return fig1, fig2
 
 
476
 
477
 
478
  def segmentation_lax_tab():
@@ -642,7 +649,10 @@ def landmark(image_id, view, method, seed, progress=gr.Progress()):
642
 
643
  landmark_fig = plot_landmarks(images, coords)
644
  lv_fig = plot_lv(coords)
645
- return landmark_fig, lv_fig
 
 
 
646
 
647
 
648
  def landmark_tab():
 
1
  from pathlib import Path
2
 
3
  import gradio as gr
4
+ import matplotlib.pyplot as plt
5
  import numpy as np
6
  import requests
7
  import SimpleITK as sitk # noqa: N813
8
  import spaces
9
  import torch
10
+ from huggingface_hub import hf_hub_download
11
+ from monai.transforms import Compose, ScaleIntensityd, SpatialPadd
12
+ from tqdm import tqdm
13
+
14
  from cinema import CineMA, ConvUNetR, ConvViT, heatmap_soft_argmax
15
  from cinema.examples.cine_cmr import plot_cmr_views
16
  from cinema.examples.inference.landmark_heatmap import (
 
34
  from cinema.examples.inference.segmentation_sax import (
35
  plot_volume_changes as plot_volume_changes_sax,
36
  )
 
 
 
37
 
38
  # cache directories
39
  cache_dir = Path("/tmp/.cinema")
 
222
  reconstructed_dict,
223
  masks_dict,
224
  )
225
+ plt.close(fig)
226
  return fig
227
 
228
 
 
334
  progress(1, desc="Plotting results...")
335
  fig1 = plot_segmentations_sax(images, labels, t_step)
336
  fig2 = plot_volume_changes_sax(labels, t_step)
337
+ result = (fig1, fig2)
338
+ plt.close(fig1)
339
+ plt.close(fig2)
340
+ return result
341
 
342
 
343
  def segmentation_sax_tab():
 
476
  progress(1, desc="Plotting results...")
477
  fig1 = plot_segmentations_lax(images, labels)
478
  fig2 = plot_volume_changes_lax(labels)
479
+ result = (fig1, fig2)
480
+ plt.close(fig1)
481
+ plt.close(fig2)
482
+ return result
483
 
484
 
485
  def segmentation_lax_tab():
 
649
 
650
  landmark_fig = plot_landmarks(images, coords)
651
  lv_fig = plot_lv(coords)
652
+ result = (landmark_fig, lv_fig)
653
+ plt.close(landmark_fig)
654
+ plt.close(lv_fig)
655
+ return result
656
 
657
 
658
  def landmark_tab():
requirements.txt CHANGED
@@ -17,6 +17,6 @@ scikit-learn==1.6.1
17
  scipy==1.15.2
18
  spaces==0.36.0
19
  timm==1.0.15
20
- git+https://github.com/mathpluscode/CineMA@edc0774baed3c2429a2a4ffaa36a3910f2780b2b#egg=cinema
21
  --extra-index-url https://download.pytorch.org/whl/cu113
22
  torch==2.5.1
 
17
  scipy==1.15.2
18
  spaces==0.36.0
19
  timm==1.0.15
20
+ git+https://github.com/mathpluscode/CineMA@460cf087e2a42f44ac80d6b0a722be0dcfbb755a#egg=cinema
21
  --extra-index-url https://download.pytorch.org/whl/cu113
22
  torch==2.5.1