forked from p7ayfu77/astro-csbdeep
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathastrodenoisepygui.py
729 lines (582 loc) · 24.7 KB
/
astrodenoisepygui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
import kivy
kivy.require('2.1.0')
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.actionbar import ActionToggleButton
from kivy.uix.slider import Slider
from kivy.uix.popup import Popup
from kivy.factory import Factory
from kivy.clock import mainthread
from kivy.core.window import Window
from kivy.properties import *
from kivy.graphics.texture import Texture
from kivy.logger import Logger
from kivy.clock import Clock
import os
import sys
import signal
import time
import traceback
import threading
from pathlib import Path
import numpy as np
import tqdm
from tifffile import imread, imsave
from astropy.io import fits
import tensorflow as tf
from csbdeep.data import NoNormalizer, STFNormalizer, PadAndCropResizer
from csbdeep.models import CARE
from astrodeep.utils.fits import read_fits, write_fits
from operator import itemgetter
import astrodenoisepyguiversion
supported_save_formats_fits = ['.fit','.fits','.fts']
supported_save_formats_tiff = ['.tif','.tiff']
file_dialog_ext = [
("Supported Image", "*.fit;*.fits;*.fts;*.FITS;*.FIT;*.FTS;*.tiff;*.tif;*.TIF;*.TIFF"),
("TIF Image", "*.tiff;*.tif;*.TIF;*.TIFF"),
("FITS Image", "*.fit;*.fits;*.fts;*.FITS;*.FIT;*.FTS")
]
def isWindows():
return sys.platform == 'win32'
class LoadDialog(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
def sort_by_date(self, files, filesystem):
result = sorted(
zip(
files,
list(map(os.path.isdir, files)),
list(map(os.path.getmtime, files))
),
key=itemgetter(1,2),
reverse=True)
return list(map(itemgetter(0),result))
class SaveDialog(FloatLayout):
save = ObjectProperty(None)
text_input = ObjectProperty(None)
cancel = ObjectProperty(None)
def sort_by_date(self, files, filesystem):
result = sorted(
zip(
files,
list(map(os.path.isdir, files)),
list(map(os.path.getmtime, files))
),
key=itemgetter(1,2),
reverse=True)
return list(map(itemgetter(0),result))
class MySlider(Slider):
def __init__(self, **kwargs):
self.register_event_type('on_slider_release')
self.register_event_type('on_slider_move')
super(MySlider, self).__init__(**kwargs)
def on_slider_release(self):
pass
def on_slider_move(self):
pass
def on_touch_up(self, touch):
super(MySlider, self).on_touch_up(touch)
if touch.grab_current == self:
self.dispatch('on_slider_release')
return True
def on_touch_move(self, touch):
super(MySlider, self).on_touch_move(touch)
if self.collide_point(*touch.pos):
self.dispatch('on_slider_move')
return True
class MyFloatLayout(FloatLayout):
def __init__(self, **kwargs):
super(MyFloatLayout, self).__init__(**kwargs)
self.rawimagedata = None
self.processedimagedata = None
self.preprocessedimagedata = None
self.models_basedir = 'models'
self.models = os.listdir('models')
self.selected_model = 'default'
self.processed = False
self.preprocessed = False
self.fits_headers = None
self.process_trigger = Clock.create_trigger(self.process_now)
self.bind(stfC=self.process_trigger, stfB=self.process_trigger,tilling=self.process_trigger,expand_low=self.process_trigger)
self.stop_event = threading.Event()
self.processthread = None
imageout = ObjectProperty(None, allownone=True)
filetoload = StringProperty()
stfC = NumericProperty(-2.8)
stfB = NumericProperty(0.25)
tilling = NumericProperty(3)
sizing = NumericProperty(1)
expand_low = NumericProperty(0.9)
denoise_enabled = BooleanProperty(False)
normalize_enabled = BooleanProperty(True)
autoupdate_enabled = BooleanProperty(True)
models = ObjectProperty(None)
selected_model = StringProperty()
progress = NumericProperty(0)
selected_device = StringProperty('GPU')
snapshotinfo = StringProperty()
def dismiss_popup(self):
self._popup.dismiss()
def show_load(self):
if isWindows():
from astrodeep.filedialogs import open_file_dialog
path = open_file_dialog(
"Select FITS or TIFF image",
App.get_running_app().lastpath,
ext=file_dialog_ext)
if path is None:
return
self.load(path)
else:
content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
self._popup = Popup(title="Load file", content=content,
size_hint=(0.9, 0.9))
self._popup.open()
def load(self, path):
if (isinstance(path,list) and len(path) == 0) or path is None:
return
if isinstance(path,list):
path = path[0]
App.get_running_app().lastpath = str(Path(path).parent)
try:
exists = Path(path).exists()
if not exists:
raise Exception(f"Path does not exists: {path}")
self.filetoload = path
except Exception as e:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
print (message)
return
finally:
if not isWindows():
self.dismiss_popup()
self.sizing = 1
self.load_now()
def load_now(self):
if self.filetoload == '' or self.filetoload is None:
return
Window.set_system_cursor('wait')
loadthread = threading.Thread(target=self.load_callback)
threading.excepthook = self.load_exception_callback
loadthread.start()
def load_exception_callback(self,args):
e = args[1]
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
print (message)
print ("".join(traceback.format_exception(args[0], args[1], args[2])))
Window.set_system_cursor('arrow')
def load_callback(self):
result, headers = self.load_file_data(self.filetoload)
if result is None:
return
self.load_result(result, headers)
@mainthread
def load_result(self, result, headers):
Window.set_system_cursor('arrow')
self.rawimagedata = result
self.fits_headers = headers
self.imageout = None
self.processed = False
self.preprocessed = False
self.denoise_enabled = False
self.process_trigger()
def load_file_data(self, path):
path = Path(path)
extension = path.suffix.lower()
if extension in supported_save_formats_fits:
data, headers = read_fits(path)
elif extension in supported_save_formats_tiff:
data, headers = np.moveaxis(imread(path),-1,0), None
else:
print("Skipping unsupported format. Allowed formats: .tiff/.tif/.fits/.fit/.fts")
return None, None
if not np.issubdtype(data.dtype, np.float32):
data = (data / np.iinfo(data.dtype).max).astype(np.float32)
if data.ndim == 2:
data = data[np.newaxis,...]
if self.sizing != 1:
return self.resize_image(data, self.sizing), headers
return data, headers
def resize_image(self,data,subsample):
zoom_order = 1
from scipy.ndimage.interpolation import zoom
factor = np.ones(data.ndim)
factor[1:3] = subsample
return zoom(data, [1, 1/subsample, 1/subsample], mode='nearest')
def show_save(self):
if isWindows():
from astrodeep.filedialogs import save_file_dialog
path = save_file_dialog(
"Save FITS or TIFF image",
App.get_running_app().lastpath,
ext=file_dialog_ext)
if path is None:
return
self.save(path)
else:
content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
self._popup = Popup(title="Save file", content=content,
size_hint=(0.9, 0.9))
self._popup.open()
def save(self, path):
if not isWindows():
self.dismiss_popup()
filepath = Path(path)
App.get_running_app().lastpath = str(filepath.parent)
if self.fits_headers is None:
self.fits_headers = fits.Header()
self.fits_headers['HISTORY'] = 'AstroDenoisePyGUI_STF_C {}'.format(self.stfC)
self.fits_headers['HISTORY'] = 'AstroDenoisePyGUI_STF_B {}'.format(self.stfB)
self.fits_headers['HISTORY'] = 'AstroDenoisePyGUI_STF_Low {}'.format(self.expand_low)
self.fits_headers['HISTORY'] = 'AstroDenoisePyGUI_Model {}'.format(self.selected_model)
self.fits_headers['HISTORY'] = 'AstroDenoisePyGUI_Scale {}'.format(self.sizing)
self.fits_headers['HISTORY'] = 'AstroDenoisePyGUI_Processed {}'.format(self.processed)
self.fits_headers['HISTORY'] = 'AstroDenoisePyGUI_AutoUpdate {}'.format(self.autoupdate_enabled)
self.fits_headers['HISTORY'] = 'AstroDenoisePyGUI_Denoise {}'.format(self.denoise_enabled)
if self.denoise_enabled:
result_tosave = self.processedimagedata
self.processed = True
else:
result_tosave = self.preprocessedimagedata
try:
extension = filepath.suffix.lower()
if extension in supported_save_formats_fits:
result_forsave = np.moveaxis(np.transpose(result_tosave),1,2)
write_fits(filepath,result_forsave,headers=self.fits_headers)
elif extension in supported_save_formats_tiff:
result_tosave = (result_tosave - np.min(result_tosave)) / (np.max(result_tosave) - np.min(result_tosave))
result_tosave = (result_tosave * np.iinfo(np.uint16).max).astype(np.uint16)
imsave(filepath,data=result_tosave)
else:
print("Skipping unsupported format. Allowed formats: .tiff/.tif/.fits/.fit/.fts")
except Exception as e:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
print (message)
def on_stfB(self, instance, value):
self.stfB = value
self.processed = False
self.preprocessed = False
def on_stfC(self, instance, value):
self.stfC = value
self.processed = False
self.preprocessed = False
def on_expand_low(self, instance, value):
self.expand_low = value
self.processed = False
#self.preprocessed = False
def on_tilling(self, instance, value):
self.tilling = value
self.processed = False
#self.preprocessed = False
def reset_sliders(self):
self.processed = False
#self.preprocessed = False
self.stfC = -2.8
self.stfB = 0.25
#self.expand_low = 0.9
def denoise_check(self, instance, value):
self.denoise_enabled = value
self.process_now()
def normalize_check(self, instance, value):
self.normalize_enabled = value
self.processed = False
self.preprocessed = False
self.process_now()
def autoupdate_check(self, instance, value):
self.autoupdate_enabled = value
self.process_now()
def update_model(self,value):
self.selected_model = value
self.processed = False
self.preprocessed = False
self.process_now()
def update_device(self,value):
self.selected_device = value
self.processed = False
self.preprocessed = False
self.process_now()
def on_sizing(self, instance, value):
self.sizing = value
self.load_now()
def process_now(self, *largs):
if not self.autoupdate_enabled:
return
self.start_process()
def start_process(self):
if self.rawimagedata is None:
return
if self.denoise_enabled and not self.processed:
pass
elif self.denoise_enabled and self.processedimagedata is not None:
self.process_result(self.processedimagedata)
return
elif self.denoise_enabled and self.processedimagedata is None:
pass
elif not self.preprocessed:
pass
elif self.preprocessedimagedata is not None:
self.process_result(self.preprocessedimagedata)
return
if self.processthread is not None and self.processthread.is_alive():
self.stop_event.set()
with tqdm.tqdm(desc='Waiting for thread to stop...') as waiter_loger:
wait_count = 0
while self.processthread.is_alive():
wait_count += 1
time.sleep(0.1)
waiter_loger.update(wait_count)
self.stop_event.clear()
Window.set_system_cursor('wait')
self.processthread = threading.Thread(target=self.process_callback)
threading.excepthook = self.process_exception_callback
self.processthread.start()
def process_exception_callback(self,args):
e = args[1]
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
print (message)
print ("".join(traceback.format_exception(args[0], args[1], args[2])))
Window.set_system_cursor('arrow')
def process_callback(self):
result = self.process(self.rawimagedata,self.stfC,self.stfB)
if result is not None:
self.process_result(result)
@mainthread
def process_result(self,result):
Window.set_system_cursor('arrow')
if self.denoise_enabled:
self.processedimagedata = result
self.processed = True
else:
self.preprocessedimagedata = result
self.preprocessed = True
image = (result - np.min(result)) / (np.max(result) - np.min(result))
image = (image * 255).astype('uint8')
colorfmt='rgb'
if image.shape[2] == 1:
colorfmt='luminance'
texture = Texture.create(size=(image.shape[1], image.shape[0]), colorfmt=colorfmt)
texture.blit_buffer(image.tobytes(), colorfmt=colorfmt)
texture.flip_vertical()
texture.mag_filter = 'nearest'
self.imageout = texture
@mainthread
def update_progress(self,progress):
self.progress = progress
def process(self, data, C=-2.8,B=0.25):
self.update_progress(0)
expand_low_actual = 0.5 - (self.expand_low/2)
normalizer = STFNormalizer(C=C,B=B,expand_low=expand_low_actual,do_after=False) if self.normalize_enabled else NoNormalizer(expand_low=expand_low_actual)
if self.denoise_enabled:
with tf.device(f"/{self.selected_device}:0"):
axes = 'YX'
model = CARE(config=None, name=self.selected_model, basedir=self.models_basedir)
output_denoised = []
for i, c in enumerate(data):
if self.stop_event.is_set():
return None
output_denoised.append(
model.predict(c, axes, normalizer=normalizer,resizer=PadAndCropResizer(), n_tiles = (self.tilling,self.tilling))
)
self.update_progress((i+1)/len(data))
result = np.moveaxis(np.transpose(output_denoised),0,1)
else:
result = normalizer.before(np.moveaxis(np.transpose(data),0,1),'YX')
self.update_progress(1)
return result
def show_compare(self,instance):
if instance.state != 'down' and instance.last_touch.button == 'right':
snapshots = self.ids.av
snapshots.remove_widget(instance)
return
elif instance.state != 'down' and instance.last_touch.button == 'left':
self.process_trigger()
return
import json
snapshotinfo = json.dumps(
instance.parameters,
ensure_ascii=True,
sort_keys=False,
indent=4)
self.snapshotinfo = snapshotinfo.replace('"','').replace(',','').replace('{','').replace('}','')
result = instance.imagedata
image = (result - np.min(result)) / (np.max(result) - np.min(result))
image = (image * 255).astype('uint8')
colorfmt='rgb'
if image.shape[2] == 1:
colorfmt='luminance'
texture = Texture.create(size=(image.shape[1], image.shape[0]), colorfmt=colorfmt)
texture.blit_buffer(image.tobytes(), colorfmt=colorfmt)
texture.flip_vertical()
texture.mag_filter = 'nearest'
self.imageout = texture
def take_snapshot(self):
snapshots = self.ids.av
if self.denoise_enabled:
data = self.processedimagedata
self.processed = True
else:
data = self.preprocessedimagedata
if data is None:
return
compare = CompareExample(
icon='astrodenoisepy/data/color-palette-outline.png',
group='compare',
imagedata=data,
parameters={
"Denoised": self.denoise_enabled,
"STF.C": self.stfC,
"STF.B": self.stfB,
"ExpandLow": self.expand_low,
"Model": self.selected_model
})
compare.bind(on_release=self.show_compare)
snapshots.add_widget(compare,3)
class MyZoomFloatLayout(AnchorLayout):
region_x = NumericProperty(0)
region_y = NumericProperty(0)
region_w = NumericProperty(0)
region_h = NumericProperty(0)
scale = NumericProperty(1)
imageoutzoom = ObjectProperty(None, allownone=True)
imageout = ObjectProperty(None, allownone=True)
def on_imageout(self, instance, value):
if value:
self.region_w = value.width / self.scale
self.region_h = value.height / self.scale
self.imageoutzoom.texture = value.get_region(self.region_x, self.region_y, self.region_w, self.region_h)
else:
self.region_w = 0
self.region_h = 0
self.scale = 1
self.region_x = 0
self.region_y = 0
return True
def on_touch_down(self, touch):
if not self.collide_point(*touch.pos):
return super().on_touch_down(touch)
if self.imageout is None:
return True
if touch.is_mouse_scrolling:
if touch.button == 'scrolldown':
if self.scale < 10:
prev_w = self.imageout.width / self.scale
prev_h = self.imageout.height / self.scale
self.scale *= 1.1
self.region_w = self.imageout.width / self.scale
self.region_h = self.imageout.height / self.scale
self.region_x += (prev_w-self.region_w) // 2
self.region_y += (prev_h-self.region_h) // 2
elif touch.button == 'scrollup':
if self.scale > 1:
prev_w = self.imageout.width / self.scale
prev_h = self.imageout.height / self.scale
self.scale /= 1.1
self.region_w = self.imageout.width / self.scale
self.region_h = self.imageout.height / self.scale
if (self.region_w > self.imageout.width) or (self.region_h > self.imageout.height):
self.region_w = self.imageout.width
self.region_h = self.imageout.height
new_x = self.region_x + (prev_w-self.region_w) // 2
new_y = self.region_y + (prev_h-self.region_h) // 2
if (new_x + self.region_w) > self.imageout.width:
self.region_x = self.imageout.width - self.region_w
elif new_x < 0:
self.region_x = 0
else:
self.region_x += (prev_w-self.region_w) // 2
if (new_y + self.region_h) > self.imageout.height:
self.region_y = self.imageout.height - self.region_h
elif new_y < 0:
self.region_y = 0
else:
self.region_y += (prev_h-self.region_h) // 2
else:
self.scale = 1
else:
touch.grab(self)
self.imageoutzoom.texture = self.imageout.get_region(self.region_x, self.region_y, self.region_w, self.region_h)
return True
def on_touch_move(self, touch):
if not self.collide_point(*touch.pos):
return super().on_touch_move(touch)
if self.imageout is None:
return True
if touch.grab_current is self:
imx, imy = self.children[0].get_norm_image_size()
deltax = -touch.dx * (self.region_w/self.right) * (self.right/imx)
deltay = -touch.dy * (self.region_h/self.top) * (self.top/imy)
new_x = self.region_x + deltax
new_y = self.region_y + deltay
if (new_x >= 0) and (new_x + self.region_w <= self.imageout.width):
self.region_x += deltax
if (new_y >= 0) and (new_y + self.region_h <= self.imageout.height):
self.region_y += deltay
self.imageoutzoom.texture = self.imageout.get_region(self.region_x, self.region_y, self.region_w, self.region_h)
return True
def on_touch_up(self, touch):
super().on_touch_move(touch)
if touch.grab_current is self:
touch.ungrab(self)
return True
class CompareExample(ActionToggleButton):
def __init__(self, imagedata=None, parameters=None, **kwargs):
super(CompareExample, self).__init__(**kwargs)
self.imagedata = imagedata
self.parameters = parameters
class AstroDenoisePyGUIApp(App):
lastpath = StringProperty()
def build(self):
self.title = f"AstroDenoisePy - v{astrodenoisepyguiversion.version}"
Window.bind(on_request_close=self.window_request_close)
Window.left = self.config.getint('graphics', 'left')
Window.top = self.config.getint('graphics', 'top')
Window.size = (self.config.getint('graphics', 'width'),self.config.getint('graphics', 'height'))
self.lastpath = self.config.get('astrodenoisepy', 'lastpath')
from kivy.config import Config
Config.set('input', 'mouse', 'mouse,disable_multitouch')
def build_config(self, config):
config.setdefaults('graphics', {
'width': 1024,
'height': 768,
'top': 100,
'left': 100
})
config.setdefaults('astrodenoisepy', {
'lastpath': ''
})
def window_request_close(self, win):
self.config.set('graphics', 'left', Window.left)
self.config.set('graphics', 'top', Window.top)
self.config.set('graphics', 'width', Window.size[0])
self.config.set('graphics', 'height', Window.size[1])
self.config.set('astrodenoisepy', 'lastpath', Path(self.lastpath).as_posix())
self.config.write()
return False
Factory.register('Root', cls=MyFloatLayout)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)
def handle_exception(exc_type, exc_value, exc_traceback):
""" handle all exceptions """
# KeyboardInterrupt is a special case.
# We don't raise the error dialog when it occurs.
if issubclass(exc_type, KeyboardInterrupt):
return
Logger.error("Unhandled Exception:")
Logger.error("".join(traceback.format_exception(exc_type, exc_value, exc_traceback)))
try:
App.get_running_app().stop()
except Exception:
pass
# we want to handle TERM signal cleanly (sent by sv down)
def handleSigTERM(a, b):
App.get_running_app().stop()
signal.signal(signal.SIGTERM, handleSigTERM)
# install handler for exceptions
sys.excepthook = handle_exception
if __name__ == '__main__':
AstroDenoisePyGUIApp().run()