Commit a2eabd51 authored by Jakob Knollmueller's avatar Jakob Knollmueller
Browse files

avoid errors in gui

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