gui_app.py 8 KB
Newer Older
Jakob Knollmueller's avatar
Jakob Knollmueller committed
1
import matplotlib
Jakob Knollmueller's avatar
Jakob Knollmueller committed
2
3
matplotlib.use('agg')
# matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
Jakob Knollmueller's avatar
Jakob Knollmueller committed
4

Jakob Knollmueller's avatar
Jakob Knollmueller committed
5
from point_separation import  build_multi_problem, multi_problem_iteration,load_data
Jakob Knollmueller's avatar
Jakob Knollmueller committed
6

Jakob Knollmueller's avatar
Jakob Knollmueller committed
7
8
from kivy.app import App
from kivy.uix.widget import Widget
Jakob Knollmueller's avatar
Jakob Knollmueller committed
9
10
11
12
13
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.properties import ObjectProperty, StringProperty, NumericProperty
from kivy.uix.boxlayout import BoxLayout
14
from kivy.clock import Clock, mainthread
Jakob Knollmueller's avatar
Jakob Knollmueller committed
15
from os.path import sep, expanduser, isdir, dirname, join
Jakob Knollmueller's avatar
Jakob Knollmueller committed
16
17
from kivy.garden.filebrowser import FileBrowser
from kivy.utils import platform
Jakob Knollmueller's avatar
Jakob Knollmueller committed
18

19
20
21
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
import numpy as np
Jakob Knollmueller's avatar
Jakob Knollmueller committed
22
23
import re
import threading
Jakob Knollmueller's avatar
Jakob Knollmueller committed
24
from matplotlib import pyplot as plt
Jakob Knollmueller's avatar
Jakob Knollmueller committed
25
26
import nifty2go as ift
import matplotlib.pyplot as plt
27
import time
Jakob Knollmueller's avatar
Jakob Knollmueller committed
28
from kivy.uix.progressbar import ProgressBar
Jakob Knollmueller's avatar
Jakob Knollmueller committed
29
30
31
32
33

class FloatInput(TextInput):

    pat = re.compile('[^0-9]')
    def insert_text(self, substring, from_undo=False):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
34
        print substring
Jakob Knollmueller's avatar
Jakob Knollmueller committed
35
36
37
38
39
40
        pat = self.pat
        if '.' in self.text:
            s = re.sub(pat, '', substring)
        else:
            s = '.'.join([re.sub(pat, '', s) for s in substring.split('.', 1)])
        return super(FloatInput, self).insert_text(s, from_undo=from_undo)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
41
42
43
44
45
46
47
48
49
class IntInput(TextInput):

    pat = re.compile('[^0-9]')
    def insert_text(self, substring, from_undo=False):
        pat = self.pat
        s = re.sub(pat, '', substring)

        return super(IntInput, self).insert_text(s, from_undo=from_undo)

Jakob Knollmueller's avatar
Jakob Knollmueller committed
50
51
52
53


class MyImage(BoxLayout):
    text = StringProperty('')
54
    source = StringProperty('')
Jakob Knollmueller's avatar
Jakob Knollmueller committed
55

56
57
    def reload(self):
        self.img.reload()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
58
59
60
61

class MyAlphaWidget(BoxLayout):
    alpha = NumericProperty(None)
    pass
Jakob Knollmueller's avatar
Jakob Knollmueller committed
62
63
64
class IterationWidget(BoxLayout):
    iteration = NumericProperty(None)
    pass
Jakob Knollmueller's avatar
Jakob Knollmueller committed
65
class ResultsPathWidget(BoxLayout):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
66
67
68

    pass

Jakob Knollmueller's avatar
Jakob Knollmueller committed
69
class DataPathWidget(BoxLayout):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
70
71
    pass

Jakob Knollmueller's avatar
Jakob Knollmueller committed
72
class DisplayWidget(ScreenManager):
73
    def reload(self):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
74
        for child in self.children:
75
            child.reload()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
76
77
78
class ImageWidget(BoxLayout):
    def reload(self):
        self.image_widget.reload()
79

Jakob Knollmueller's avatar
Jakob Knollmueller committed
80
81
class MenuWidget(BoxLayout):
    pass
Jakob Knollmueller's avatar
Jakob Knollmueller committed
82
class SingleImageScreen(Screen):
83
84
85
    source = StringProperty('')
    def reload(self):
        self.img.reload()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
86
87
    pass
class AllImageScreen(Screen):
88
89
    def reload(self):
        self.all.reload()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
90
91
    pass
class AllImageWidget(BoxLayout):
92
93
94
95
96
    def reload(self):
        self.data.reload()
        self.points.reload()
        self.diffuse.reload()
        self.power.reload()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
97
    pass
Jakob Knollmueller's avatar
Jakob Knollmueller committed
98

Jakob Knollmueller's avatar
Jakob Knollmueller committed
99
100
101
102
class ActionWidget(BoxLayout):
    pass
class DisplayChoiceWidget(BoxLayout):
    pass
Jakob Knollmueller's avatar
Jakob Knollmueller committed
103
104
105
106
107
108

class GlobalScreenManager(ScreenManager):
    pass

class MainScreen(Screen):
    pass
Jakob Knollmueller's avatar
Jakob Knollmueller committed
109

Jakob Knollmueller's avatar
Jakob Knollmueller committed
110
111
112
class FileScreen(Screen):
    pass
class PathScreen(Screen):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
113
114
115
    def is_dir(self, directory, filename):
        return isdir(join(directory, filename))

Jakob Knollmueller's avatar
Jakob Knollmueller committed
116

117
118
119
120
121
122
123
124
class MyWidget(BoxLayout):
    image_widget = ObjectProperty(None)
    menu_widget = ObjectProperty(None)

    data_path = StringProperty(None)
    result_path = StringProperty(None)
    alpha = NumericProperty(None)

Jakob Knollmueller's avatar
Jakob Knollmueller committed
125
126
127
128
129

class MyPathBrowser(FileBrowser):
    pass

class MyFileBrowser(FileBrowser):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
130
    filters = ['*.fits', '*.png', '*.jpg']
Jakob Knollmueller's avatar
Jakob Knollmueller committed
131
132
133
    pass


Jakob Knollmueller's avatar
Jakob Knollmueller committed
134
class SeparatorApp(App):
135
136
    stop = threading.Event()

Jakob Knollmueller's avatar
Jakob Knollmueller committed
137
138
139
    data_path = StringProperty(None)
    result_path = StringProperty(None)
    alpha = NumericProperty(None)
140
141
142
143
144
145
    data_image = StringProperty(None)
    diffuse_image = StringProperty(None)
    points_image = StringProperty(None)
    power_image = StringProperty(None)
    vmin = None
    vmax = None
Jakob Knollmueller's avatar
Jakob Knollmueller committed
146
147
    myEnergy = None
    iterations = 3
Jakob Knollmueller's avatar
Jakob Knollmueller committed
148
149
    user_path = ''
    reconstructing = False
Jakob Knollmueller's avatar
Jakob Knollmueller committed
150
151
    data_loaded = False

Jakob Knollmueller's avatar
Jakob Knollmueller committed
152
153
154
    def build(self):
        self.set_default()
        self.trigger = 0
Jakob Knollmueller's avatar
Jakob Knollmueller committed
155
156
157
158
        self.root = GlobalScreenManager()
        self.image_widget = self.root.main.image_widget.image_widget.image_widget
        self.root.transition = NoTransition()
        self.image_widget.transition = NoTransition()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
159
        return self.root
160

Jakob Knollmueller's avatar
Jakob Knollmueller committed
161
    def set_default(self):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
162
        self.data_path = ''
Jakob Knollmueller's avatar
Jakob Knollmueller committed
163
164
        self.result_path = ''
        self.alpha = 1.5
165
166
167
168
169
        self.path = ''
        self.data_image = self.path + 'placeholder.png'
        self.diffuse_image = self.path + 'placeholder.png'
        self.points_image = self.path + 'placeholder.png'
        self.power_image = self.path + 'placeholder.png'
Jakob Knollmueller's avatar
Jakob Knollmueller committed
170
171
172
173
        if platform == 'win':
            self.user_path = dirname(expanduser('~')) + sep + 'Documents'
        else:
            self.user_path = expanduser('~') + sep + 'Documents'
Jakob Knollmueller's avatar
Jakob Knollmueller committed
174

Jakob Knollmueller's avatar
Jakob Knollmueller committed
175
176
177
    def load_data(self, selection):
        print selection
        self.data_path = selection[0]
178
        threading.Thread(target=self.load_data_thread).start()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
179
        self.root.current = 'main'
180
181

    def load_data_thread(self):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
182
        self.data = load_data(self.data_path)
183
184
        self.vmin = np.log(self.data.min())
        self.max = np.log(self.data.max())
Jakob Knollmueller's avatar
Jakob Knollmueller committed
185
186
        self.plot_data()
        self.set_data_loaded(True)
187
188
189
        self.set_data_image()
        self.update_plots()

Jakob Knollmueller's avatar
Jakob Knollmueller committed
190
    def run_separation(self):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
191
        if not self.reconstructing and self.data_loaded:
Jakob Knollmueller's avatar
Jakob Knollmueller committed
192
            threading.Thread(target= self.run_separation_thread).start()
193
194

    def run_separation_thread(self):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
195
        self.set_reconstructing(True)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
196
197
        self.myEnergy = build_multi_problem(self.data, self.alpha)
        self.plot_components(self.path)
198
199
200
201
        self.set_image_paths()
        self.update_plots()

        for i in range(self.iterations):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
202
203
            self.myEnergy = multi_problem_iteration(self.myEnergy)
            self.plot_components(self.path)
204
            self.update_plots()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
205
        self.set_reconstructing(False)
206

Jakob Knollmueller's avatar
Jakob Knollmueller committed
207
208
209
210
211
    def save_results(self):
        if self.myEnergy is not None:
            self.root.current = 'path'

    def select_path(self, path):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
212
        self.result_path = path[0]
Jakob Knollmueller's avatar
Jakob Knollmueller committed
213
        threading.Thread(target=self.save_data_thread).start()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
214
        self.root.current = 'main'
Jakob Knollmueller's avatar
Jakob Knollmueller committed
215

Jakob Knollmueller's avatar
Jakob Knollmueller committed
216
217
218
219
    def save_data_thread(self):
        np.savetxt(self.result_path + '/points.csv', np.exp(self.myEnergy.u.val))
        np.savetxt(self.result_path + '/diffus.csv', np.exp(self.myEnergy.s.val))
        self.plot_components(self.result_path)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
220
221
    def set_result_path(self, path):
        self.result_path = path
Jakob Knollmueller's avatar
Jakob Knollmueller committed
222

223
    @mainthread
Jakob Knollmueller's avatar
Jakob Knollmueller committed
224
    def update_plots(self):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
225
226
227
228
        self.image_widget.reload()
    @mainthread
    def set_reconstructing(self, reconstructing):
        self.reconstructing = reconstructing
Jakob Knollmueller's avatar
Jakob Knollmueller committed
229
230
231
    @mainthread
    def set_data_loaded(self, loaded):
        self.data_loaded = loaded
Jakob Knollmueller's avatar
Jakob Knollmueller committed
232

Jakob Knollmueller's avatar
Jakob Knollmueller committed
233
234
    def set_data_path(self, path):
        self.data_path = path
235
236
    @mainthread
    def set_data_image(self):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
237

Jakob Knollmueller's avatar
Jakob Knollmueller committed
238
        self.data_image = self.path + 'data.png'
239
240
    @mainthread
    def set_image_paths(self):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
241
242
        self.points_image = self.path + 'points.png'
        self.diffuse_image = self.path + 'diffuse.png'
243

Jakob Knollmueller's avatar
Jakob Knollmueller committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
    def plot_data(self):
        if self.data.shape[0] == 1:
            plt.imsave(self.path+'data.png', self.data[0], vmin=self.vmin, vmax=self.vmax)
        else:
            plt.imsave(self.path+ 'data.png', self.data/255.)

    def plot_components(self, path):
        diffuse = np.empty_like(self.data)
        points = np.empty_like(self.data)
        for i  in range(len(self.myEnergy)):
            diffuse[...,i] = np.exp(self.myEnergy[i].s.val)
            points[...,i] = np.exp(self.myEnergy[i].u.val)
        if len(self.myEnergy) == 1:
            plt.imsave(path+'diffuse.png', diffuse[...,0], vmin=self.vmin, vmax=self.vmax)
            plt.imsave(path+'points.png', points[...,0], vmin=self.vmin, vmax=self.vmax)
        else:
            plt.imsave(self.path+ 'diffuse.png', diffuse/255.)
            plt.imsave(self.path+ 'points.png', points/255.)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
262
263


Jakob Knollmueller's avatar
Jakob Knollmueller committed
264
    def set_alpha(self, alpha):
Jakob Knollmueller's avatar
Jakob Knollmueller committed
265
266
267
268
269
270
271
272
273
        if alpha == '':
            pass
        else:
            self.alpha = alpha
    def set_iterations(self,iterations):
        if iterations == '':
            pass
        else:
            self.iterations = int(iterations)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
274

275
276
    def on_stop(self):
        self.stop.set()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
277

Jakob Knollmueller's avatar
Jakob Knollmueller committed
278

Jakob Knollmueller's avatar
Jakob Knollmueller committed
279
if __name__ == '__main__':
Jakob Knollmueller's avatar
Jakob Knollmueller committed
280
    plt.gray()
Jakob Knollmueller's avatar
Jakob Knollmueller committed
281
    SeparatorApp().run()