Commit 108c2be1 authored by Jakob Knollmueller's avatar Jakob Knollmueller

added filebrowser

parent 8c0c97b7
import matplotlib
# matplotlib.use('agg')
matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
matplotlib.use('agg')
# matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
from point_separation import build_problem, problem_iteration,load_data
......@@ -12,16 +12,20 @@ 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 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
# from matplotlib import pyplot as plt
from matplotlib import pyplot as plt
import nifty2go as ift
import matplotlib.pyplot as plt
import time
from kivy.uix.progressbar import ProgressBar
class FloatInput(TextInput):
......@@ -52,10 +56,13 @@ class ResultsPathWidget(BoxLayout):
class DataPathWidget(BoxLayout):
pass
class ImageWidget(ScreenManager):
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
......@@ -80,7 +87,17 @@ class ActionWidget(BoxLayout):
pass
class DisplayChoiceWidget(BoxLayout):
pass
class DisplayOptionWidget(BoxLayout):
class GlobalScreenManager(ScreenManager):
pass
class MainScreen(Screen):
pass
class StartScreen(Screen):
pass
class FileScreen(Screen):
pass
class PathScreen(Screen):
pass
class MyWidget(BoxLayout):
......@@ -91,6 +108,16 @@ class MyWidget(BoxLayout):
result_path = StringProperty(None)
alpha = NumericProperty(None)
class StartWidget(BoxLayout):
pass
class MyPathBrowser(FileBrowser):
pass
class MyFileBrowser(FileBrowser):
pass
class SeparatorApp(App):
stop = threading.Event()
......@@ -104,16 +131,20 @@ class SeparatorApp(App):
vmin = None
vmax = None
iterations = 5
user_path = ''
reconstructing = False
def build(self):
self.set_default()
self.trigger = 0
self.root = MyWidget()
self.root.image_widget.transition = NoTransition()
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 = 'hst_05195_01_wfpc2_f702w_pc_sci.fits'
self.data_path = ''
self.result_path = ''
self.alpha = 1.5
self.path = ''
......@@ -121,10 +152,16 @@ class SeparatorApp(App):
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):
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)
......@@ -136,9 +173,11 @@ class SeparatorApp(App):
self.update_plots()
def run_separation(self):
threading.Thread(target= self.run_separation_thread).start()
if not self.reconstructing:
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')
......@@ -150,27 +189,32 @@ class SeparatorApp(App):
self.plot_array(self.myEnergy.u.val, 'points.png')
self.plot_array(self.myEnergy.s.val, 'diffuse.png')
self.update_plots()
self.set_reconstructing(False)
def save_results(self):
pass
def save_results(self, path):
self.result_path = path[0]
self.root.current = 'main'
def set_result_path(self, path):
self.result_path = path
print path
@mainthread
def update_plots(self):
self.root.image_widget.reload()
self.image_widget.reload()
@mainthread
def set_reconstructing(self, reconstructing):
self.reconstructing = reconstructing
def set_data_path(self, path):
self.data_path = path
@mainthread
def set_data_image(self):
self.data_image = self.result_path + 'data.png'
self.data_image = self.path + 'data.png'
@mainthread
def set_image_paths(self):
self.points_image = self.result_path + 'points.png'
self.diffuse_image = self.result_path + 'diffuse.png'
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)
......@@ -183,6 +227,7 @@ class SeparatorApp(App):
def on_stop(self):
self.stop.set()
if __name__ == '__main__':
plt.viridis()
plt.gray()
SeparatorApp().run()
......@@ -6,6 +6,9 @@ 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
np.random.seed(42)
......@@ -19,31 +22,112 @@ if __name__ == '__main__':
myEnergy = build_problem(data, alpha=alpha)
for i in range(10):
myEnergy = problem_iteration(myEnergy)
A = FFTSmoothingOperator(myEnergy.s.domain, sigma=2.)
plt.magma()
size = 15
vmin = data.min()+0.01
vmax = 0.1*data.max()
vmax = 0.01*data.max()
plt.figure()
plt.imshow(np.exp(myEnergy.s.val),norm=LogNorm(vmin=vmin, vmax=vmax))
cbar = plt.colorbar()
cbar.set_label('flux', size=size)
plt.title('diffuse emission', size=size)
plt.axis('off')
ax = plt.gca()
im = ax.imshow(np.exp(myEnergy.s.val),norm=LogNorm(vmin=vmin, vmax=vmax))
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
cbar = plt.colorbar(im, cax=cax)
cbar.set_label('flux', size=size)
plt.tight_layout()
plt.savefig('hubble_diffuse.pdf')
plt.figure()
plt.imshow(np.exp(myEnergy.u.val), norm=LogNorm(vmin=vmin, vmax=vmax))
cbar = plt.colorbar()
cbar.set_label('flux', size=size)
plt.title('point-like emission', size=size)
plt.axis('off')
ax = plt.gca()
im = ax.imshow(A(exp(myEnergy.u)).val, norm=LogNorm(vmin=vmin, vmax=vmax))
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
cbar = plt.colorbar(im, cax=cax)
cbar.set_label('flux', size=size)
plt.tight_layout()
plt.savefig('hubble_point.pdf')
plt.figure()
plt.imshow(data, norm=LogNorm(vmin=vmin, vmax=vmax))
cbar = plt.colorbar()
cbar.set_label('flux', size=size)
plt.title('data', size=size)
plt.axis('off')
ax = plt.gca()
dat = Field(myEnergy.s.domain,val=data)
im = ax.imshow((dat).val, norm=LogNorm(vmin=vmin, vmax=vmax))
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
cbar = plt.colorbar(im, cax=cax)
cbar.set_label('flux', size=size)
plt.tight_layout()
plt.savefig('hubble_data.pdf')
plt.close()
plt.figure()
fig, ax = plt.subplots(1, 3, figsize=(6, 3))
plt.suptitle('zoomed in section', size=size)
# fig.tight_layout()
vmin = data.min() + 0.0001
vmax = 0.001 * data.max()
im = ax[0].imshow(data[600:700, 650:750], norm=LogNorm(vmin=vmin, vmax=vmax))
ax[0].set_title('data', size=15)
ax[0].axis('off')
ax[1].imshow(exp(myEnergy.s).val[600:700, 650:750], norm=LogNorm(vmin=vmin, vmax=vmax))
ax[1].set_title('diffuse', size=15)
ax[1].axis('off')
ax[2].imshow(exp(myEnergy.u).val[600:700, 650:750], norm=LogNorm(vmin=vmin, vmax=vmax))
ax[2].set_title('point-like', size=15)
ax[2].axis('off')
# cax = fig.add_axes([0., 0.9, 0.03, 0.8])
cb = fig.colorbar(im, ax=ax.ravel().tolist(), orientation='horizontal', pad=0.01)
cb.set_label('flux', size=15)
fig.subplots_adjust(left=None, bottom=0.25, right=None, top=None,
wspace=0.01, hspace=None)
plt.savefig('hubble_zoom.pdf')
fig = plt.figure()
grid = AxesGrid(fig, 111, # similar to subplot(142)
nrows_ncols=(1, 3),
axes_pad=0.1,
share_all=True,
label_mode="1",
cbar_location="bottom",
cbar_mode="single",
cbar_pad=0.1
)
im = grid[0].imshow(data[600:700, 650:750],
norm=LogNorm(vmin=vmin, vmax=vmax)) # , extent=extent, interpolation="not")
im = grid[1].imshow(exp(myEnergy.s).val[600:700, 650:750],
norm=LogNorm(vmin=vmin, vmax=vmax)) # , extent=extent, interpolation="not")
im = grid[2].imshow(exp(myEnergy.u).val[600:700, 650:750],
norm=LogNorm(vmin=vmin, vmax=vmax)) # , extent=extent, interpolation="not")
grid[0].axis('off')
grid[1].axis('off')
grid[2].axis('off')
grid[0].set_label('data')
# plt.colorbar(im, cax = grid.cbar_axes[0])
cb = grid.cbar_axes[0].colorbar(im)
for cax in grid.cbar_axes:
cax.toggle_label(True)
plt.close()
plt.figure()
power = power_analyze(exp(myEnergy.s))
k_lengths = power.domain.k_lenghts
plt.plot(power.val, k_lengths, 'k-')
plt.yscale('log')
plt.xscale('log')
plt.title('diffuse power')
\ No newline at end of file
import nifty4 as ift
import numpy as np
# from matplotlib import pyplot as plt
from matplotlib import pyplot as plt
from astropy.io import fits
from separation_energy import SeparationEnergy
from nifty4.library.nonlinearities import PositiveTanh
def load_data(path):
# if path[-5:] == '.fits':
data = fits.open(path)[1].data
# else:
# data = plt.imread(path)
if path[-5:] == '.fits':
data = fits.open(path)[1].data
else:
data = plt.imread(path)[:,:,0]
data = data.clip(min=0.001)
data = np.ndarray.astype(data, float)
......
......@@ -29,26 +29,6 @@ TextInput:
text: '1.5'
on_text: app.set_alpha(float(self.text))
<ResultsPathWidget>:
orientation:'horizontal'
Label:
text:'path to results'
TextInput:
text: ''
multiline: False
on_text: app.set_result_path(self.text)
<DataPathWidget>:
orientation:'horizontal'
Label:
text: 'location of the data'
TextInput:
text: 'hst_05195_01_wfpc2_f702w_pc_sci.fits'
multiline: False
on_text: app.set_data_path(self.text)
<FloatInput>:
multiline: False
......@@ -94,7 +74,7 @@ TextInput:
id: power
source: app.power_image
text: 'power'
<ImageWidget>
<DisplayWidget>
all: all
points: points
diffuse: diffuse
......@@ -154,13 +134,7 @@ TextInput:
orientation: 'vertical'
MyAlphaWidget:
size_hint: 1,0.1
DataPathWidget:
size_hint: 1,0.1
text: 'location of the data'
ResultsPathWidget:
size_hint: 1,0.1
text: 'path to results'
DisplayOptionWidget
ActionWidget
......@@ -168,39 +142,88 @@ TextInput:
orientation: 'horizontal'
Button:
text: 'load data'
on_press: app.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()
on_press: app.root.current = 'path'
<DisplayChoiceWidget>:
orientation: 'horizontal'
Button:
text: 'all'
on_press: app.root.image_widget.current = 'all'
on_press: app.image_widget.current = 'all'
Button:
text: 'data'
on_press: app.root.image_widget.current = 'data'
on_press: app.image_widget.current = 'data'
Button:
text: 'diffuse'
on_press: app.root.image_widget.current = 'diffuse'
on_press: app.image_widget.current = 'diffuse'
Button:
text: 'points'
on_press: app.root.image_widget.current = 'points'
on_press: app.image_widget.current = 'points'
Button:
text: 'power'
on_press: app.root.image_widget.current = 'power'
<DisplayOptionWidget>:
on_press: app.image_widget.current = 'power'
<ImageWidget>:
orientation: 'vertical'
Label:
text: 'display'
size_hint: 1,0.1
image_widget : image_widget
DisplayChoiceWidget:
size_hint: 1, 0.1
DisplayWidget:
id: image_widget
<StartWidget>:
Button:
text: 'load data'
on_press: app.root.current = 'file'
<MyPathBrowser>:
id: _filebrowser
dirselect: True
<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'
PathScreen:
id: path
name: 'path'
<StartScreen>:
StartWidget
<FileScreen>:
MyFileBrowser:
on_success: app.load_data(self.selection)
<MainScreen>:
image_widget: image_widget
MyWidget:
id:image_widget
<PathScreen>:
MyPathBrowser
on_success: app.save_results(self.selection)
DisplayChoiceWidget
......
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