diff --git a/gui_app.py b/gui_app.py index a70717740603b8833259565b8bf88f4431d8c1ce..697299094bc29ea910ca14305fabbc3e1fff792a 100644 --- a/gui_app.py +++ b/gui_app.py @@ -2,7 +2,7 @@ import matplotlib matplotlib.use('agg') # matplotlib.use('module://kivy.garden.matplotlib.backend_kivy') -from point_separation import build_problem, problem_iteration,load_data +from point_separation import build_multi_problem, multi_problem_iteration,load_data from kivy.app import App from kivy.uix.widget import Widget @@ -12,7 +12,7 @@ from kivy.uix.image import Image from kivy.properties import ObjectProperty, StringProperty, NumericProperty from kivy.uix.boxlayout import BoxLayout from kivy.clock import Clock, mainthread -from os.path import sep, expanduser, isdir, dirname +from os.path import sep, expanduser, isdir, dirname, join from kivy.garden.filebrowser import FileBrowser from kivy.utils import platform @@ -31,12 +31,22 @@ class FloatInput(TextInput): pat = re.compile('[^0-9]') def insert_text(self, substring, from_undo=False): + print substring 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) +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) + class MyImage(BoxLayout): @@ -49,6 +59,9 @@ class MyImage(BoxLayout): class MyAlphaWidget(BoxLayout): alpha = NumericProperty(None) pass +class IterationWidget(BoxLayout): + iteration = NumericProperty(None) + pass class ResultsPathWidget(BoxLayout): pass @@ -93,12 +106,13 @@ class GlobalScreenManager(ScreenManager): class MainScreen(Screen): pass -class StartScreen(Screen): - pass + class FileScreen(Screen): pass class PathScreen(Screen): - pass + def is_dir(self, directory, filename): + return isdir(join(directory, filename)) + class MyWidget(BoxLayout): image_widget = ObjectProperty(None) @@ -108,13 +122,12 @@ class MyWidget(BoxLayout): result_path = StringProperty(None) alpha = NumericProperty(None) -class StartWidget(BoxLayout): - pass class MyPathBrowser(FileBrowser): pass class MyFileBrowser(FileBrowser): + filters = ['*.fits', '*.png', '*.jpg'] pass @@ -130,10 +143,12 @@ class SeparatorApp(App): power_image = StringProperty(None) vmin = None vmax = None - iterations = 5 + myEnergy = None + iterations = 3 user_path = '' reconstructing = False - + data_loaded = False + def build(self): self.set_default() self.trigger = 0 @@ -167,34 +182,41 @@ class SeparatorApp(App): self.data = load_data(self.data_path) self.vmin = np.log(self.data.min()) self.max = np.log(self.data.max()) - self.plot_array(np.log(self.data), 'data.png') - + self.plot_data() + self.set_data_loaded(True) self.set_data_image() self.update_plots() def run_separation(self): - if not self.reconstructing: + if not self.reconstructing and self.data_loaded: threading.Thread(target= self.run_separation_thread).start() def run_separation_thread(self): self.set_reconstructing(True) - self.myEnergy = build_problem(self.data, self.alpha) - self.plot_array(self.myEnergy.u.val, 'points.png') - self.plot_array(self.myEnergy.s.val, 'diffuse.png') + self.myEnergy = build_multi_problem(self.data, self.alpha) + self.plot_components(self.path) self.set_image_paths() self.update_plots() for i in range(self.iterations): - self.myEnergy = problem_iteration(self.myEnergy) - self.plot_array(self.myEnergy.u.val, 'points.png') - self.plot_array(self.myEnergy.s.val, 'diffuse.png') + self.myEnergy = multi_problem_iteration(self.myEnergy) + self.plot_components(self.path) self.update_plots() self.set_reconstructing(False) - def save_results(self, path): + def save_results(self): + if self.myEnergy is not None: + self.root.current = 'path' + + def select_path(self, path): self.result_path = path[0] + threading.Thread(target=self.save_data_thread).start() self.root.current = 'main' + 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) def set_result_path(self, path): self.result_path = path @@ -204,6 +226,9 @@ class SeparatorApp(App): @mainthread def set_reconstructing(self, reconstructing): self.reconstructing = reconstructing + @mainthread + def set_data_loaded(self, loaded): + self.data_loaded = loaded def set_data_path(self, path): self.data_path = path @@ -216,13 +241,36 @@ class SeparatorApp(App): self.points_image = self.path + 'points.png' self.diffuse_image = self.path + 'diffuse.png' - def plot_array(self, array, path): - plt.imsave(path, array, vmin=self.vmin, vmax=self.vmax) + 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.) def set_alpha(self, alpha): - self.alpha = alpha - print alpha + if alpha == '': + pass + else: + self.alpha = alpha + def set_iterations(self,iterations): + if iterations == '': + pass + else: + self.iterations = int(iterations) def on_stop(self): self.stop.set() diff --git a/separator.kv b/separator.kv index 0833aec0519b92dd91c75bf735ec721df544076e..cb5589aa3ceeeb670f16d94ae5c0ddcdde1c9f50 100644 --- a/separator.kv +++ b/separator.kv @@ -27,10 +27,19 @@ TextInput: text: unichr(945) FloatInput text: '1.5' - on_text: app.set_alpha(float(self.text)) + on_text: app.set_alpha(self.text) +<IterationWidget>: + orientation: 'horizontal' + Label: + text: 'iterations' + IntInput + text: '3' + on_text: app.set_iterations(self.text) <FloatInput>: multiline: False +<IntInput>: + multiline: False <MyImage>: text: self.text @@ -134,6 +143,8 @@ TextInput: orientation: 'vertical' MyAlphaWidget: size_hint: 1,0.1 + IterationWidget: + size_hint: 1,0.1 ActionWidget @@ -148,7 +159,7 @@ TextInput: on_press: app.run_separation() Button: text: 'save results' - on_press: app.root.current = 'path' + on_press: app.save_results() <DisplayChoiceWidget>: orientation: 'horizontal' @@ -176,10 +187,6 @@ TextInput: DisplayWidget: id: image_widget -<StartWidget>: - Button: - text: 'load data' - on_press: app.root.current = 'file' <MyPathBrowser>: id: _filebrowser @@ -188,37 +195,37 @@ TextInput: <MyFileBrowser>: <GlobalScreenManager>: - start: start file: file main: main path: path - StartScreen: - id: start - name: 'start' - FileScreen: - id:file - name: 'file' MainScreen: id: main name: 'main' + FileScreen: + id:file + name: 'file' + PathScreen: id: path name: 'path' -<StartScreen>: - StartWidget + <FileScreen>: MyFileBrowser: on_success: app.load_data(self.selection) + on_canceled: app.root.current = 'main' + <MainScreen>: image_widget: image_widget MyWidget: id:image_widget <PathScreen>: MyPathBrowser - on_success: app.save_results(self.selection) + filters: [root.is_dir] + on_success: app.select_path(self.selection) + on_canceled: app.root.current = 'main'