-
Notifications
You must be signed in to change notification settings - Fork 2
/
visualisation_mosaic_3band_npyV.py
211 lines (173 loc) · 8.23 KB
/
visualisation_mosaic_3band_npyV.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
import matplotlib
matplotlib.use("Agg")
from visualisation_mosaic_1band import BoxLayoutMosaic, CustomButton
import math
import numpy as np
import sys
import os
import glob
import urllib.request
import pandas as pd
from astropy.wcs import WCS
from astropy.visualization import make_lupton_rgb
import astropy.io.fits as pyfits
from kivy.app import App
from kivy.properties import NumericProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
from kivy.uix.label import Label
from kivy.uix.slider import Slider
from kivy.uix.textinput import TextInput
import matplotlib.pyplot as plt
from functools import partial
from kivy.core.window import Window
from PIL import Image
import random
class BoxLayoutMosaicColor(BoxLayoutMosaic):
def prepare_numpy_array(self):
self.clean_scratch(self.pathtoscratch_numpy)
for i in np.arange(len(self.listimage)):
image_B, image_G, image_R = [pyfits.open(self.pathtofile + self.listimage[i])[0].data,
pyfits.open(self.pathtofile + self.listimage[i])[1].data,
pyfits.open(self.pathtofile + self.listimage[i])[2].data]
image = np.array([image_B, image_G, image_R])
np.save(self.pathtoscratch_numpy + str(self.listimage[i]), image)
def scale_val(self, image_array):
if len(np.shape(image_array)) == 2:
image_array = [image_array]
vmin = np.min([self.background_rms_image(5, image_array[i]) for i in range(len(image_array))])
xl, yl = np.shape(image_array[0])
box_size = 14 # in pixel
xmin = int((xl) / 2 - (box_size / 2))
xmax = int((xl) / 2 + (box_size / 2))
vmax = np.max([image_array[i][xmin:xmax, xmin:xmax] for i in range(len(image_array))])
return vmin, vmax
def showplot_rgb(self, rimage, gimage, bimage):
vmin, vmax = self.scale_val([rimage, gimage, bimage])
img = np.zeros((rimage.shape[0], rimage.shape[1], 3), dtype=float)
img[:, :, 0] = self.sqrt_sc(rimage, scale_min=vmin, scale_max=vmax)
img[:, :, 1] = self.sqrt_sc(gimage, scale_min=vmin, scale_max=vmax)
img[:, :, 2] = self.sqrt_sc(bimage, scale_min=vmin, scale_max=vmax)
return img
def sqrt_sc(self, inputArray, scale_min=None, scale_max=None):
#
imageData = np.array(inputArray, copy=True)
if scale_min is None:
scale_min = imageData.min()
if scale_max is None:
scale_max = imageData.max()
imageData = imageData.clip(min=scale_min, max=scale_max)
imageData = imageData - scale_min
indices = np.where(imageData < 0)
imageData[indices] = 0.00001
imageData = np.sqrt(imageData)
imageData = imageData / np.sqrt(scale_max - scale_min)
return imageData
def draw_image(self, name, scale_state, defaultvalue=True, max=1, min=0):
try:
image_B = np.load(self.pathtoscratch_numpy + name)[0]
image_G = np.load(self.pathtoscratch_numpy + name)[1]
image_R = np.load(self.pathtoscratch_numpy + name)[2]
image = self.showplot_rgb(image_R, image_G, image_B)
except FileNotFoundError:
image = np.ones((44, 44, 3)) * 0.0000001
return image
def prepare_png(self, number):
start = self.counter
for i in np.arange(start, start + number + 1):
try:
img = self.draw_image(self.listimage[i], self.scale_state)
# print('prepare png',self.listimage[i])
except IndexError:
img = self.draw_image('not_existing.fits', self.scale_state)
image = Image.fromarray(np.uint8(img * 255), 'RGB')
image = image.resize((150, 150), Image.ANTIALIAS)
image.save(self.pathtoscratch + str(i + 1) + self.scale_state + str(start) + '.png', 'PNG')
self.counter = self.counter + 1
def build(self):
self.pathds9 = 'C:\\SAOImageDS9\\ds9.exe'
# self.pathtofile = './files_to_visualize/'
self.pathtoscratch = './scratch_png/'
self.pathtoscratch_numpy = './scratch_numpy/'
self.path_background = 'green.png'
# self.listimage = sorted([os.path.basename(x) for x in glob.glob(self.pathtofile + '*.fits')])
self.listimage = sorted([os.path.basename(x) for x in glob.glob(self.pathtoscratch_numpy + '*.npy')])
# print(self.listimage[0])
if len(sys.argv) > 1:
self.random_seed = sys.argv[1]
else:
print("Random seed set to default value 42")
self.random_seed = 42
if len(sys.argv) > 2:
self.fraction = float(sys.argv[2])
else:
print("No repeated objects")
self.fraction = 0
if len(sys.argv) > 3:
self.numpy_computing = sys.argv[3]
self.repeat_random_objects(self.fraction)
random.Random(self.random_seed).shuffle(self.listimage)
self.clean_scratch(self.pathtoscratch)
self.start_image_number = 0
self.counter = 0
self.scale_min = 0
self.scale_max = 1
self.limit_max = 1
self.limit_min = 0
self.step = (self.scale_max - self.scale_min) / 10.
self.scale_state = 'linear'
self.number_per_frame = 100
self.total_n_frame = int(len(self.listimage) / 100.)
self.forward_backward_state = 0
self.dataframe = self.create_df()
self.prepare_png(self.number_per_frame)
allbox = BoxLayout(orientation='vertical')
buttonbox = BoxLayout(orientation='horizontal', size_hint_y=0.1)
superbox = GridLayout(cols=10, size_hint_y=0.9)
self.list_of_buttons = []
for i in np.arange(self.number_per_frame):
try:
if self.dataframe['classification'][i] == 0:
self.list_of_buttons.append(
CustomButton(0, source=self.pathtoscratch + str(i + 1) + self.scale_state + str(0) + '.png'))
else:
self.list_of_buttons.append(
CustomButton(1, source=self.path_background))
self.dataframe['Grid_pos'].iloc[100 * self.forward_backward_state + i] = i + 1
except KeyError:
self.list_of_buttons.append(
CustomButton(1, source=self.pathtoscratch + str(i + 1) + self.scale_state + str(0) + '.png'))
self.list_of_buttons[i].bind(on_press=partial(self.on_click, i))
for button in self.list_of_buttons:
superbox.add_widget(button)
allbox.add_widget(superbox)
buttonscale1 = Button(text="Linear")
buttonscale2 = Button(text="Sqrt")
buttonscale3 = Button(text="Log")
buttonscale4 = Button(text="Asinh")
buttonscale1.bind(on_press=partial(self.change_scale, 'linear'))
buttonscale2.bind(on_press=partial(self.change_scale, 'sqrt'))
buttonscale3.bind(on_press=partial(self.change_scale, 'log'))
buttonscale4.bind(on_press=partial(self.change_scale, 'asinh'))
bforward = Button(text=" --> ")
bbackward = Button(text=" <-- ")
bforward.bind(on_press=self.forward)
bbackward.bind(on_press=self.backward)
self.textnumber = TextInput(text=str(self.forward_backward_state), multiline=False, font_size=25)
self.textnumber.bind(on_text_validate=self.change_number)
tnumber = Label(text=str(' / ' + str(self.total_n_frame)), font_size=25)
buttonbox.add_widget(buttonscale1)
buttonbox.add_widget(buttonscale2)
buttonbox.add_widget(buttonscale3)
buttonbox.add_widget(buttonscale4)
buttonbox.add_widget(bbackward)
buttonbox.add_widget(bforward)
buttonbox.add_widget(self.textnumber)
buttonbox.add_widget(tnumber)
allbox.add_widget(buttonbox)
return allbox
if __name__ == '__main__':
BoxLayoutMosaicColor().run()