Commit fd9e15f6 authored by Jakob Knollmueller's avatar Jakob Knollmueller

restructuring

parent c2e4c7b9
......@@ -19,7 +19,6 @@
import numpy as np
from matplotlib import pyplot as plt
import starblade as sb
import nifty4 as ift
if __name__ == '__main__':
......
#: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
<MyAlphaWidget>:
orientation: 'horizontal'
Label:
text: unichr(945)
FloatInput
text: '1.5'
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
source: self.source
img: img
orientation: 'vertical'
spacing: 10
Label:
text: self.parent.text
size_hint: 1,0.1
Image:
id: img
source: self.parent.source
<AllImageWidget>:
data: data
points: points
diffuse: diffuse
power:power
orientation: 'vertical'
spacing: 10
BoxLayout
spacing: 10
orientation: 'horizontal'
MyImage:
id: data
text: 'data'
source: app.data_image
MyImage:
id: diffuse
source: app.diffuse_image
text: 'diffuse'
BoxLayout:
spacing: 10
orientation: 'horizontal'
MyImage:
id:points
source: app.points_image
text: 'points'
MyImage:
id: power
source: app.power_image
text: 'power'
<DisplayWidget>
all: all
points: points
diffuse: diffuse
data: data
power: power
AllImageScreen:
id:all
name: 'all'
on_pre_enter: self.reload()
SingleImageScreen:
id:data
name: 'data'
text: 'data'
source: app.data_image
on_pre_enter: self.reload()
SingleImageScreen:
id: diffuse
name: 'diffuse'
text: 'diffuse'
source: app.diffuse_image
on_pre_enter: self.reload()
SingleImageScreen:
id: points
name: 'points'
text: 'points'
source: app.points_image
on_pre_enter: self.reload()
SingleImageScreen:
id:power
name: 'power'
text: 'power'
source: app.power_image
on_pre_enter: self.reload()
<SingleImageScreen>
img: img
name: ''
source: self.source
text: ''
MyImage
id: img
text: self.parent.text
source: self.parent.source
<AllImageScreen>
name: ''
all : all
AllImageWidget
id:all
<MenuWidget>:
orientation: 'vertical'
MyAlphaWidget:
size_hint: 1,0.1
IterationWidget:
size_hint: 1,0.1
ActionWidget
<ActionWidget>:
orientation: 'horizontal'
Button:
text: 'load data'
on_press: app.root.current = 'file'
Button:
text: 'run separation'
on_press: app.run_separation()
Button:
text: 'save results'
on_press: app.save_results()
<DisplayChoiceWidget>:
orientation: 'horizontal'
Button:
text: 'all'
on_press: app.image_widget.current = 'all'
Button:
text: 'data'
on_press: app.image_widget.current = 'data'
Button:
text: 'diffuse'
on_press: app.image_widget.current = 'diffuse'
Button:
text: 'points'
on_press: app.image_widget.current = 'points'
Button:
text: 'power'
on_press: app.image_widget.current = 'power'
<ImageWidget>:
orientation: 'vertical'
image_widget : image_widget
DisplayChoiceWidget:
size_hint: 1, 0.1
DisplayWidget:
id: image_widget
<MyPathBrowser>:
id: _filebrowser
dirselect: True
<MyFileBrowser>:
<GlobalScreenManager>:
file: file
main: main
path: path
MainScreen:
id: main
name: 'main'
FileScreen:
id:file
name: 'file'
PathScreen:
id: path
name: 'path'
<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
filters: [root.is_dir]
on_success: app.select_path(self.selection)
on_canceled: app.root.current = 'main'
import matplotlib
matplotlib.use('agg')
# matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
from astropy.io import fits
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, mainthread
from os.path import sep, expanduser, isdir, dirname, join
from kivy.garden.filebrowser import FileBrowser
from kivy.utils import platform
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
import numpy as np
import re
import threading
import matplotlib.pyplot as plt
from kivy.uix.progressbar import ProgressBar
import starblade as sb
def load_data(path):
if path[-5:] == '.fits':
data = fits.open(path)[1].data
else:
data = plt.imread(path)
data = data.clip(min=0.001)
data = np.ndarray.astype(data, float)
return data
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):
text = StringProperty('')
source = StringProperty('')
def reload(self):
self.img.reload()
class MyAlphaWidget(BoxLayout):
alpha = NumericProperty(None)
pass
class IterationWidget(BoxLayout):
iteration = NumericProperty(None)
pass
class ResultsPathWidget(BoxLayout):
pass
class DataPathWidget(BoxLayout):
pass
class DisplayWidget(ScreenManager):
def reload(self):
for child in self.children:
child.reload()
class ImageWidget(BoxLayout):
def reload(self):
self.image_widget.reload()
class MenuWidget(BoxLayout):
pass
class SingleImageScreen(Screen):
source = StringProperty('')
def reload(self):
self.img.reload()
pass
class AllImageScreen(Screen):
def reload(self):
self.all.reload()
pass
class AllImageWidget(BoxLayout):
def reload(self):
self.data.reload()
self.points.reload()
self.diffuse.reload()
self.power.reload()
pass
class ActionWidget(BoxLayout):
pass
class DisplayChoiceWidget(BoxLayout):
pass
class GlobalScreenManager(ScreenManager):
pass
class MainScreen(Screen):
pass
class FileScreen(Screen):
pass
class PathScreen(Screen):
def is_dir(self, directory, filename):
return isdir(join(directory, filename))
class MyWidget(BoxLayout):
image_widget = ObjectProperty(None)
menu_widget = ObjectProperty(None)
data_path = StringProperty(None)
result_path = StringProperty(None)
alpha = NumericProperty(None)
class MyPathBrowser(FileBrowser):
pass
class MyFileBrowser(FileBrowser):
filters = ['*.fits', '*.png', '*.jpg']
pass
class StarbladeApp(App):
stop = threading.Event()
data_path = StringProperty(None)
result_path = StringProperty(None)
alpha = NumericProperty(None)
data_image = StringProperty(None)
diffuse_image = StringProperty(None)
points_image = StringProperty(None)
power_image = StringProperty(None)
vmin = None
vmax = None
myEnergy = None
iterations = 3
user_path = ''
reconstructing = False
data_loaded = False
def build(self):
self.set_default()
##TODO create placeholder image here
self.trigger = 0
self.root = GlobalScreenManager()
self.image_widget = self.root.main.image_widget.image_widget.image_widget
self.root.transition = NoTransition()
self.image_widget.transition = NoTransition()
return self.root
def set_default(self):
self.data_path = ''
self.result_path = ''
self.alpha = 1.5
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'
if platform == 'win':
self.user_path = dirname(expanduser('~')) + sep + 'Documents'
else:
self.user_path = expanduser('~') + sep + 'Documents'
def load_data(self, selection):
print selection
self.data_path = selection[0]
threading.Thread(target=self.load_data_thread).start()
self.root.current = 'main'
def load_data_thread(self):
self.data = load_data(self.data_path)
self.vmin = np.log(self.data.min())
self.max = np.log(self.data.max())
self.plot_data()
self.set_data_loaded(True)
self.set_data_image()
self.update_plots()
def run_separation(self):
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 = sb.build_multi_starblade(self.data, self.alpha)
self.plot_components(self.path)
self.set_image_paths()
self.update_plots()
for i in range(self.iterations):
self.myEnergy = sb.multi_starblade_iteration(self.myEnergy)
self.plot_components(self.path)
self.update_plots()
self.set_reconstructing(False)
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
@mainthread
def update_plots(self):
self.image_widget.reload()
@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
@mainthread
def set_data_image(self):
self.data_image = self.path + 'data.png'
@mainthread
def set_image_paths(self):
self.points_image = self.path + 'points.png'
self.diffuse_image = self.path + 'diffuse.png'
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):
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()
if __name__ == '__main__':
plt.gray()
StarbladeApp().run()
import nifty4 as ift
import numpy as np
from matplotlib import rc
rc('font',**{'family':'serif','serif':['Palatino']})
rc('text', usetex=True)
from matplotlib import pyplot as plt
import starblade as sb
np.random.seed(42)
if __name__ == '__main__':
s_space = ift.RGSpace([1024])
h_space = s_space.get_default_codomain()
FFT = ift.FFTOperator(h_space)
p_spec = lambda k: (1./(1+k)**2.5)
S = ift.create_power_operator(h_space, power_spectrum=p_spec)
sh = S.draw_sample()
s = FFT(sh)
u = ift.Field(s_space, val = -12)
u.val[200] = 1
u.val[300] = 3
u.val[500] = 4
u.val[700] = 5
u.val[900] = 2
u.val[154] = 0.5
u.val[421] = 0.25
u.val[652] = 1
u.val[1002] = 2.5
d = ift.exp(s) + ift.exp(u)
data = d.val
energy1 = sb.build_starblade(data,1.25)
energy2 = sb.build_starblade(data,1.5)
energy3 = sb.build_starblade(data,1.75)
for i in range(20):
energy1 = sb.starblade_iteration(energy1)
energy2 = sb.starblade_iteration(energy2)
energy3 = sb.starblade_iteration(energy3)
size = 15
plt.figure()
# plt.plot(data, 'k-')
f, (ax0, ax1,ax2) = plt.subplots(3, sharex=True, sharey=True)
plt.suptitle('diffuse components', size=size)
ax0.plot(ift.exp(energy1.s).val, 'k-')
ax0.yaxis.set_label_position("right")
ax0.set_ylabel(r'$\alpha = 1.25$', size=size)
ax0.set_ylim(1e-1,1e3)
ax0.set_yscale("log")
ax1.plot(ift.exp(energy2.s).val, 'k-')
ax1.yaxis.set_label_position("right")
ax1.set_ylabel(r'$\alpha = 1.5$', size=size)
ax2.plot(ift.exp(energy3.s).val, 'k-')
ax2.yaxis.set_label_position("right")
ax2.set_ylabel(r'$\alpha = 1.75$', size=size)
plt.savefig('1d_diffuse.pdf')
plt.figure()
f, (ax0, ax1,ax2) = plt.subplots(3, sharex=True, sharey=True)
plt.suptitle('point-like components', size=size)
ax0.plot(ift.exp(energy1.u).val, 'k-')
ax0.yaxis.set_label_position("right")
ax0.set_ylabel(r'$\alpha = 1.25$', size=size)
ax0.set_ylim(1e-1,1e3)
ax0.set_yscale("log")
ax1.plot(ift.exp(energy2.u).val, 'k-')
ax1.yaxis.set_label_position("right")
ax1.set_ylabel(r'$\alpha = 1.5$', size=size)
ax2.plot(ift.exp(energy3.u).val, 'k-')
ax2.yaxis.set_label_position("right")
ax2.set_ylabel(r'$\alpha = 1.75$', size=size)
ax0.set_yscale("log")
ax0.set_ylim(1e-1,1e3)
# plt.ylim(1e-0)
plt.savefig('1d_points.pdf')
plt.figure()
f, (ax0, ax1,ax2) = plt.subplots(3, sharex=True, sharey=True)
plt.suptitle('data and true components', size=size)
ax0.plot(data, 'k-')
ax0.set_yscale("log")
ax0.set_ylim(1e-1,1e3)
ax0.yaxis.set_label_position("right")
ax0.set_ylabel(r'data', size=size)
ax1.plot(ift.exp(s).val, 'k-')
ax1.yaxis.set_label_position("right")
ax1.set_ylabel(r'diffuse', size=size)
ax2.plot(ift.exp(u).val, 'k-')
ax2.yaxis.set_label_position("right")
ax2.set_ylabel(r'point-like', size=size)
# plt.ylim(1e-0)
plt.savefig('1d_data.pdf')
plt.figure()
binbounds = ift.PowerSpace.useful_binbounds(energy2.FFT.domain[0],logarithmic=False, nbin=100)
power1 = ift.power_analyze(energy2.FFT.inverse((energy1.s)),binbounds=binbounds)
power2 = ift.power_analyze(energy2.FFT.inverse((energy2.s)),binbounds=binbounds)
power3 = ift.power_analyze(energy2.FFT.inverse((energy3.s)),binbounds=binbounds)
real_power = ift.power_analyze(sh)
power_d = ift.power_analyze(energy2.FFT.inverse(ift.log(energy2.d)),binbounds=binbounds)
# power_u = ift.power_analyze(energy2.FFT.inverse(ift.exp(energy2.u)),binbounds=binbounds)
k_lengths = power1.domain[0].k_lengths
plt.plot(k_lengths, p_spec(k_lengths*1024.)*1024**2, 'k-', label='theoretical')
plt.plot(k_lengths, power_d.val, 'k:', label='data')
plt.plot(k_lengths, power1.val, 'k-', label=(r'$\alpha = 1.25$'), alpha=0.6)
plt.plot(k_lengths, power2.val, 'k-', label=(r'$\alpha = 1.5$'),alpha=0.3)
plt.plot(k_lengths, power3.val, 'k-', label=(r'$\alpha = 1.75$'), alpha=0.15)
# plt.plot(k_lengths, power_u.val, 'k:',label='point-like')
plt.legend()
plt.yscale('log')
plt.xscale('log')
plt.title('power spectra',size=15)
plt.ylabel('power',size=15)
plt.xlabel('harmonic mode',size=15)
plt.savefig('1d_power.pdf')
from nifty4 import *
import nifty4 as ift
import numpy as np
from matplotlib import rc
rc('font',**{'family':'serif','serif':['Palatino']})
rc('text', usetex=True)
from matplotlib import pyplot as plt
from matplotlib.colors import LogNorm
from mpl_toolkits.axes_grid1 import make_axes_locatable
from mpl_toolkits.axes_grid1 import AxesGrid
from astropy.io import fits