andreped commited on
Commit
c4a7c37
·
unverified ·
2 Parent(s): 8ece154 44bc519

Merge pull request #47 from andreped/liver-support

Browse files
Files changed (3) hide show
  1. demo/src/compute.py +7 -1
  2. demo/src/gui.py +24 -18
  3. demo/src/logger.py +30 -0
demo/src/compute.py CHANGED
@@ -1,11 +1,17 @@
1
  import subprocess as sp
2
 
 
 
 
 
 
3
 
4
  def run_model(fixed_path=None, moving_path=None, fixed_seg_path=None, moving_seg_path=None, output_path=None, task="B"):
5
  if (fixed_seg_path is None) or (moving_seg_path is None):
6
- print("The fixed or moving segmentation were not provided and are thus ignored for inference.")
7
  sp.check_call(["ddmr", "-f", fixed_path, "-m", moving_path, \
8
  "-o", output_path, "-a", task, "--model", "BL-NS", "--original-resolution"])
9
  else:
 
10
  sp.check_call(["ddmr", "-f", fixed_path, "-m", moving_path, "-fs", fixed_seg_path, "-ms", moving_seg_path,
11
  "-o", output_path, "-a", task, "--model", "BL-NS", "--original-resolution"])
 
1
  import subprocess as sp
2
 
3
+ from .logger import get_logger
4
+
5
+
6
+ LOGGER = get_logger()
7
+
8
 
9
  def run_model(fixed_path=None, moving_path=None, fixed_seg_path=None, moving_seg_path=None, output_path=None, task="B"):
10
  if (fixed_seg_path is None) or (moving_seg_path is None):
11
+ LOGGER.info("The fixed or moving segmentation were not provided and are thus ignored for inference.")
12
  sp.check_call(["ddmr", "-f", fixed_path, "-m", moving_path, \
13
  "-o", output_path, "-a", task, "--model", "BL-NS", "--original-resolution"])
14
  else:
15
+ LOGGER.info("All four inputs were successfully provided.")
16
  sp.check_call(["ddmr", "-f", fixed_path, "-m", moving_path, "-fs", fixed_seg_path, "-ms", moving_seg_path,
17
  "-o", output_path, "-a", task, "--model", "BL-NS", "--original-resolution"])
demo/src/gui.py CHANGED
@@ -1,9 +1,14 @@
1
  import gradio as gr
2
 
3
  from .compute import run_model
 
4
  from .utils import load_ct_to_numpy
5
 
6
 
 
 
 
 
7
  class WebUI:
8
  def __init__(
9
  self,
@@ -27,6 +32,7 @@ class WebUI:
27
  "Brain": "B",
28
  "Liver": "L"
29
  }
 
30
 
31
  self.fixed_image_path = None
32
  self.moving_image_path = None
@@ -42,12 +48,12 @@ class WebUI:
42
  label="Which 2D slice to show",
43
  )
44
 
45
- self.run_btn = gr.Button("Run analysis").style(
46
  full_width=False, size="lg"
47
  )
48
 
49
  def set_class_name(self, value):
50
- print("Changed task to:", value)
51
  self.class_name = value
52
 
53
  def upload_file(self, files):
@@ -85,7 +91,7 @@ class WebUI:
85
  self.moving_images = load_ct_to_numpy(self.moving_image_path)
86
  self.pred_images = load_ct_to_numpy(output_path + "pred_image.nii.gz")
87
 
88
- return None
89
 
90
  def get_fixed_image(self, k):
91
  k = int(k) - 1
@@ -121,7 +127,13 @@ class WebUI:
121
  margin: auto;
122
  }
123
  #upload {
124
- height: 80px;
 
 
 
 
 
 
125
  }
126
  """
127
  with gr.Blocks(css=css) as demo:
@@ -149,6 +161,9 @@ class WebUI:
149
  info="Which task to perform image-to-registration on",
150
  multiselect=False,
151
  size="sm",
 
 
 
152
  )
153
  model_selector.input(
154
  fn=lambda x: self.set_class_name(x),
@@ -158,19 +173,8 @@ class WebUI:
158
 
159
  self.run_btn.render()
160
 
161
- """
162
- with gr.Row():
163
- gr.Examples(
164
- examples=[
165
- os.path.join(self.cwd, "ixi_image.nii.gz"),
166
- os.path.join(self.cwd, "ixi_image2.nii.gz"),
167
- ],
168
- inputs=file_output,
169
- outputs=file_output,
170
- fn=self.upload_file,
171
- cache_examples=True,
172
- )
173
- """
174
 
175
  with gr.Row():
176
  with gr.Box():
@@ -201,6 +205,8 @@ class WebUI:
201
 
202
  pred_images = []
203
  for i in range(self.nb_slider_items):
 
 
204
  visibility = True if i == 1 else False
205
  t = gr.Image(
206
  visible=visibility, elem_id="model-2d", label="predicted fixed image", show_label=True,
@@ -213,7 +219,7 @@ class WebUI:
213
  self.run_btn.click(
214
  fn=self.process,
215
  inputs=None,
216
- outputs=None,
217
  )
218
 
219
  self.slider.input(
 
1
  import gradio as gr
2
 
3
  from .compute import run_model
4
+ from .logger import setup_logger, read_logs
5
  from .utils import load_ct_to_numpy
6
 
7
 
8
+ # setup logging
9
+ LOGGER = setup_logger()
10
+
11
+
12
  class WebUI:
13
  def __init__(
14
  self,
 
32
  "Brain": "B",
33
  "Liver": "L"
34
  }
35
+ self.class_name = "Brain"
36
 
37
  self.fixed_image_path = None
38
  self.moving_image_path = None
 
48
  label="Which 2D slice to show",
49
  )
50
 
51
+ self.run_btn = gr.Button("Run analysis", show_progress="full", elem_id="button").style(
52
  full_width=False, size="lg"
53
  )
54
 
55
  def set_class_name(self, value):
56
+ LOGGER.info(f"Changed task to: {value}")
57
  self.class_name = value
58
 
59
  def upload_file(self, files):
 
91
  self.moving_images = load_ct_to_numpy(self.moving_image_path)
92
  self.pred_images = load_ct_to_numpy(output_path + "pred_image.nii.gz")
93
 
94
+ return self.pred_images[0]
95
 
96
  def get_fixed_image(self, k):
97
  k = int(k) - 1
 
127
  margin: auto;
128
  }
129
  #upload {
130
+ height: 120px;
131
+ }
132
+ #button {
133
+ height: 120px;
134
+ }
135
+ #dropdown {
136
+ height: 120px;
137
  }
138
  """
139
  with gr.Blocks(css=css) as demo:
 
161
  info="Which task to perform image-to-registration on",
162
  multiselect=False,
163
  size="sm",
164
+ default="Brain",
165
+ elem_id="dropdown",
166
+
167
  )
168
  model_selector.input(
169
  fn=lambda x: self.set_class_name(x),
 
173
 
174
  self.run_btn.render()
175
 
176
+ logs = gr.Textbox(label="Logs", info="Verbose from inference will be displayed below.", max_lines=8, autoscroll=True)
177
+ demo.load(read_logs, None, logs, every=1)
 
 
 
 
 
 
 
 
 
 
 
178
 
179
  with gr.Row():
180
  with gr.Box():
 
205
 
206
  pred_images = []
207
  for i in range(self.nb_slider_items):
208
+ if i == 0:
209
+ first_pred_component = t
210
  visibility = True if i == 1 else False
211
  t = gr.Image(
212
  visible=visibility, elem_id="model-2d", label="predicted fixed image", show_label=True,
 
219
  self.run_btn.click(
220
  fn=self.process,
221
  inputs=None,
222
+ outputs=first_pred_component,
223
  )
224
 
225
  self.slider.input(
demo/src/logger.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import sys
3
+
4
+
5
+ def get_logger():
6
+ logging.getLogger(__name__)
7
+
8
+
9
+ def setup_logger():
10
+ # clear log
11
+ file_to_delete = open("log.log",'w')
12
+ file_to_delete.close()
13
+
14
+ file_handler = logging.FileHandler(filename='log.log')
15
+ stdout_handler = logging.StreamHandler(stream=sys.stdout)
16
+ handlers = [file_handler, stdout_handler]
17
+
18
+ logging.basicConfig(
19
+ level=logging.INFO,
20
+ format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
21
+ handlers=handlers,
22
+ )
23
+
24
+ return get_logger()
25
+
26
+
27
+ def read_logs():
28
+ sys.stdout.flush()
29
+ with open("log.log", "r") as f:
30
+ return f.read()