1+ import os
12from pathlib import Path
23
34import matplotlib .pyplot as plt
45import numpy as np
56from magicgui import magicgui
6- from matplotlib .backends .backend_qt5agg import (
7- FigureCanvasQTAgg as FigureCanvas ,
8- )
7+ from matplotlib .backends .backend_qt5agg import \
8+ FigureCanvasQTAgg as FigureCanvas
99from matplotlib .figure import Figure
10+ from monai .transforms import Zoom
1011from qtpy .QtWidgets import QSizePolicy
1112from scipy import ndimage
12- from monai .transforms import Zoom
1313from tifffile import imwrite
1414
1515from napari_cellseg3d import utils
@@ -147,6 +147,7 @@ def launch_review(
147147
148148 layer = view1 .layers [0 ]
149149 layer1 = view1 .layers [1 ]
150+
150151 # if not as_folder:
151152 # r_path = os.path.dirname(r_path)
152153
@@ -164,20 +165,19 @@ def file_widget(
164165 dirname = Path (r_path )
165166 # def saver():
166167 out_dir = file_widget .dirname .value
168+
167169 # print("The directory is:", out_dir)
168170
169171 def quicksave ():
170172 if not as_folder :
171173 if viewer .layers ["labels" ] is not None :
172- time = utils .get_date_time ()
173- name = str (out_dir ) + "/labels_reviewed_" + time + ".tif"
174+ name = os .path .join (str (out_dir ), "labels_reviewed.tif" )
174175 dat = viewer .layers ["labels" ].data
175176 imwrite (name , data = dat )
176177
177178 else :
178179 if viewer .layers ["labels" ] is not None :
179- time = utils .get_date_time ()
180- dir_name = str (out_dir ) + "/labels_reviewed_" + time
180+ dir_name = os .path .join (str (out_dir ), "labels_reviewed" )
181181 dat = viewer .layers ["labels" ].data
182182 utils .save_stack (dat , dir_name , filetype = filetype )
183183
@@ -206,17 +206,17 @@ def quicksave():
206206 xy_axes = canvas .figure .add_subplot (3 , 1 , 1 )
207207 canvas .figure .suptitle ("Shift-click on image for plot \n " , fontsize = 8 )
208208 xy_axes .imshow (np .zeros ((100 , 100 ), np .int16 ))
209- xy_axes .scatter (50 , 50 , s = 10 , c = "red " , alpha = 0.25 )
209+ xy_axes .scatter (50 , 50 , s = 10 , c = "green " , alpha = 0.25 )
210210 xy_axes .set_xlabel ("x axis" )
211211 xy_axes .set_ylabel ("y axis" )
212212 yz_axes = canvas .figure .add_subplot (3 , 1 , 2 )
213213 yz_axes .imshow (np .zeros ((100 , 100 ), np .int16 ))
214- yz_axes .scatter (50 , 50 , s = 10 , c = "red " , alpha = 0.25 )
214+ yz_axes .scatter (50 , 50 , s = 10 , c = "green " , alpha = 0.25 )
215215 yz_axes .set_xlabel ("y axis" )
216216 yz_axes .set_ylabel ("z axis" )
217217 zx_axes = canvas .figure .add_subplot (3 , 1 , 3 )
218218 zx_axes .imshow (np .zeros ((100 , 100 ), np .int16 ))
219- zx_axes .scatter (50 , 50 , s = 10 , c = "red " , alpha = 0.25 )
219+ zx_axes .scatter (50 , 50 , s = 10 , c = "green " , alpha = 0.25 )
220220 zx_axes .set_xlabel ("x axis" )
221221 zx_axes .set_ylabel ("z axis" )
222222
@@ -234,17 +234,33 @@ def update_canvas_canvas(viewer, event):
234234
235235 if "shift" in event .modifiers :
236236 try :
237- m_point = np .round (viewer .cursor .position ).astype (int )
238- print (m_point )
239-
240- crop_big = crop_img (
241- [m_point [0 ], m_point [1 ], m_point [2 ]],
237+ cursor_position = np .round (viewer .cursor .position ).astype (int )
238+ print (cursor_position )
239+
240+ cropped_volume = crop_volume_around_point (
241+ [
242+ cursor_position [0 ],
243+ cursor_position [1 ],
244+ cursor_position [2 ],
245+ ],
242246 viewer .layers ["volume" ],
243247 )
244248
245- xy_axes .imshow (crop_big [50 ], cmap = "inferno" , vmin = 200 , vmax = 2000 )
246- yz_axes .imshow (crop_big .transpose (1 , 0 , 2 )[50 ], cmap = "inferno" , vmin = 200 , vmax = 2000 )
247- zx_axes .imshow (crop_big .transpose (2 , 0 , 1 )[50 ], cmap = "inferno" , vmin = 200 , vmax = 2000 )
249+ xy_axes .imshow (
250+ cropped_volume [50 ], cmap = "inferno" , vmin = 200 , vmax = 2000
251+ )
252+ yz_axes .imshow (
253+ cropped_volume .transpose (1 , 0 , 2 )[50 ],
254+ cmap = "inferno" ,
255+ vmin = 200 ,
256+ vmax = 2000 ,
257+ )
258+ zx_axes .imshow (
259+ cropped_volume .transpose (2 , 0 , 1 )[50 ],
260+ cmap = "inferno" ,
261+ vmin = 200 ,
262+ vmax = 2000 ,
263+ )
248264 canvas .draw_idle ()
249265 except Exception as e :
250266 print (e )
@@ -262,40 +278,49 @@ def update_button(axis_event):
262278
263279 view1 .dims .events .current_step .connect (update_button )
264280
265- def crop_img (points , layer ):
281+ def crop_volume_around_point (points , layer ):
266282
267- if zoom_factor != [1 ,1 , 1 ]:
268- im = np .array (layer .data , dtype = np .int16 )
269- image = Zoom (
283+ if zoom_factor != [1 , 1 , 1 ]:
284+ vol = np .array (layer .data , dtype = np .int16 )
285+ volume = Zoom (
270286 zoom_factor ,
271287 keep_size = False ,
272288 padding_mode = "empty" ,
273- )(np .expand_dims (im , axis = 0 ))
274- image = image [0 ]
289+ )(np .expand_dims (vol , axis = 0 ))
290+ volume = volume [0 ]
275291 # image = ndimage.zoom(layer.data, zoom_factor, mode="nearest") # cleaner but much slower...
276- else :
277- image = layer .data
278-
279- min_vals = [x - 50 for x in points ]
280- max_vals = [x + 50 for x in points ]
281- yohaku_minus = [n if n < 0 else 0 for n in min_vals ]
282- yohaku_plus = [
283- x - image .shape [i ] if image .shape [i ] < x else 0
284- for i , x in enumerate (max_vals )
292+ else :
293+ volume = layer .data
294+
295+ min_coordinates = [point - 50 for point in points ]
296+ max_coordinates = [point + 50 for point in points ]
297+ inferior_bound = [
298+ min_coordinate if min_coordinate < 0 else 0
299+ for min_coordinate in min_coordinates
300+ ]
301+ superior_bound = [
302+ max_coordinate - volume .shape [i ]
303+ if volume .shape [i ] < max_coordinate
304+ else 0
305+ for i , max_coordinate in enumerate (max_coordinates )
285306 ]
307+
286308 crop_slice = tuple (
287- slice (np .maximum (0 , n ), x ) for n , x in zip (min_vals , max_vals )
309+ slice (np .maximum (0 , min_coordinate ), max_coordinate )
310+ for min_coordinate , max_coordinate in zip (
311+ min_coordinates , max_coordinates
312+ )
288313 )
289314
290315 if as_folder :
291- crop_temp = image [crop_slice ].persist ().compute ()
316+ crop_temp = volume [crop_slice ].persist ().compute ()
292317 else :
293-
294318 crop_temp = layer .data [crop_slice ]
295- cropped_img = np .zeros ((100 , 100 , 100 ), np .int16 )
296- cropped_img [
297- - yohaku_minus [0 ] : 100 - yohaku_plus [0 ],
298- - yohaku_minus [1 ] : 100 - yohaku_plus [1 ],
299- - yohaku_minus [2 ] : 100 - yohaku_plus [2 ],
319+
320+ cropped_volume = np .zeros ((100 , 100 , 100 ), np .int16 )
321+ cropped_volume [
322+ - inferior_bound [0 ] : 100 - superior_bound [0 ],
323+ - inferior_bound [1 ] : 100 - superior_bound [1 ],
324+ - inferior_bound [2 ] : 100 - superior_bound [2 ],
300325 ] = crop_temp
301- return cropped_img
326+ return cropped_volume
0 commit comments