Commit bad181a2 authored by Jakob Knollmueller's avatar Jakob Knollmueller

working prototype

parent 240a3623
import matplotlib
matplotlib.use('agg')
from kivy.app import App
from kivy.uix.widget import Widget
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
from kivy.clock import Clock
from kivy.uix.textinput import TextInput
import re
import threading
from point_separation import build_problem, problem_iteration,load_data
# from matplotlib import pyplot as plt
import nifty2go as ift
import matplotlib.pyplot as plt
class FloatInput(TextInput):
pat = re.compile('[^0-9]')
def insert_text(self, substring, from_undo=False):
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 MyImage(BoxLayout):
source = StringProperty('')
text = StringProperty('')
img = ObjectProperty(None)
def reload(self):
self.img.reload()
class MyAlphaWidget(BoxLayout):
alpha = NumericProperty(None)
pass
class StoreResultsWidget(BoxLayout):
pass
class LoadDataWidget(BoxLayout):
pass
class ImageWidget(BoxLayout):
data_image = ObjectProperty(None)
diffuse_image = ObjectProperty(None)
point_image = ObjectProperty(None)
power_image = ObjectProperty(None)
def reload_images(self):
self.diffuse_image.reload()
self.point_image.reload()
self.power_image.reload()
class MenuWidget(BoxLayout):
pass
class MyWidget(BoxLayout):
image_widget = ObjectProperty(None)
menu_widget = ObjectProperty(None)
data_path = StringProperty(None)
result_path = StringProperty(None)
alpha = NumericProperty(None)
def reload_images(self):
self.image_widget.reload_images()
def set_result_path(self, path):
self.result_path = path
print path
def set_data_path(self, path):
self.data_path = path
data = load_data(self.data_path)
self.myEnergy = build_problem(data, self.alpha)
self.plotting()
print path
def set_alpha(self, alpha):
self.alpha = alpha
print alpha
def plotting(self):
plt.viridis()
plt.imsave(self.result_path+'points0.png', self.myEnergy.u.val)
plt.imsave(self.result_path+'maps0.png', (self.myEnergy.s).val)
plt.imsave(self.result_path+'data0.png', ift.log(self.myEnergy.d).val)
self.reload_images()
def run_separation(self, ini):
if ini:
self.set_data_path(self.data_path)
self.myEnergy = problem_iteration(self.myEnergy)
self.plotting()
class SeparatorApp(App):
def build(self):
self.trigger = 0
self.root = MyWidget()
Clock.schedule_interval(self.update, 1)
return self.root
def update(self,*args):
self.trigger += 1
if self.trigger < 0:
self.root.run_separation(self.trigger==-10)
if __name__ == '__main__':
MyApp().run()
\ No newline at end of file
SeparatorApp().run()
This diff is collapsed.
from nifty2go import Energy, Field, log, exp, DiagonalOperator
from nifty2go.library import WienerFilterCurvature
class SeparationEnergy(Energy):
def __init__(self, position, parameters):
x = position.val.clip(-9, 9)
position = Field(position.domain, val=x)
super(SeparationEnergy, self).__init__(position=position)
self.parameters = parameters
self.inverter = parameters['inverter']
self.d = parameters['data']
self.FFT = parameters['FFT']
self.correlation = parameters['correlation']
self.alpha = parameters['alpha']
self.q = parameters['q']
pos_tanh = parameters['pos_tanh']
self.S = self.FFT.adjoint * self.correlation * self.FFT
self.a = pos_tanh(self.position)
self.a_p = pos_tanh.derivative(self.position)
self.u = log(self.d * self.a)
self.u_p = self.a_p/self.a
one_m_a = 1 - self.a
self.s = log(self.d * one_m_a)
self.s_p = - self.a_p / one_m_a
self.var_x = 9.
def at(self, position):
return self.__class__(position, parameters=self.parameters)
@property
def value(self):
diffuse = 0.5 * self.s.vdot(self.S.inverse(self.s))
point = (self.alpha-1).vdot(self.u) + self.q.vdot(exp(-self.u))
det = self.s.integrate()
det += 0.5 / self.var_x * self.position.vdot(self.position)
return diffuse + point + det
@property
def gradient(self):
diffuse = self.S.inverse(self.s) * self.s_p
point = (self.alpha - 1) * self.u_p - self.q * exp(-self.u) * self.u_p
det = self.position / self.var_x
det += self.s_p
return diffuse + point + det
@property
def curvature(self):
point = self.q * exp(-self.u) * self.u_p ** 2
R = self.FFT * self.s_p
N = self.correlation
S = DiagonalOperator(1/(point + 1/self.var_x))
return WienerFilterCurvature(R=R, N=N, S=S, inverter=self.inverter)
#:kivy 1.0.9
TextInput:
on_parent:self.focus = True
<MyWidget>:
image_widget: image_widget
menu_widget: menu_widget
data_path: 'hst_05195_01_wfpc2_f702w_pc_sci.fits'
result_path: ''
alpha: 1.5
spacing: 10
orientation: 'horizontal'
MenuWidget:
id: menu_widget
size_hint: 0.5,1
ImageWidget:
id: image_widget
size_hint: 1,1
<MyImage>:
img: img
source: self.source
text: self.text
orientation: 'vertical'
spacing: 10
Label:
text: self.parent.text
size_hint: 1,0.1
Image:
id: img
source: self.parent.source
size_hint: 1,1
<MyAlphaWidget>:
orientation: 'horizontal'
Label:
text: unichr(945)
FloatInput
text: '1.5'
on_text_validate: app.root.set_alpha(float(self.text))
<StoreResultsWidget>:
orientation:'horizontal'
Label:
text:'path to results'
TextInput:
text: ''
multiline: False
on_text_validate: app.root.set_result_path(self.text)
<LoadDataWidget>:
orientation:'horizontal'
Label:
text: 'location of the data'
TextInput:
text: 'hst_05195_01_wfpc2_f702w_pc_sci.fits'
multiline: False
on_text_validate: app.root.set_data_path(self.text)
<FloatInput>:
multiline: False
<ImageWidget>:
data_image: data
diffuse_image: diffuse
point_image: point
power_image: power
orientation: 'vertical'
spacing: 10
BoxLayout
spacing: 10
orientation: 'horizontal'
MyImage:
id: data
source: 'data0.png'
text: 'data'
MyImage:
id: diffuse
source: 'maps0.png'
text: 'diffuse'
BoxLayout:
spacing: 10
orientation: 'horizontal'
MyImage:
id: point
source: 'points0.png'
text: 'points'
MyImage:
id: power
source: 'power.png'
text: 'power'
<MenuWidget>:
orientation: 'vertical'
MyAlphaWidget:
size_hint: 1,0.1
LoadDataWidget:
size_hint: 1,0.1
text: 'location of the data'
StoreResultsWidget:
size_hint: 1,0.1
text: 'path to results'
Button
text: 'run separation'
on_press: app.trigger = -11
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