Commit 8054b9ce authored by Jakob Knollmueller's avatar Jakob Knollmueller

everything not essential thrown out

parent a7371557
from sugar import build_problem, problem_iteration
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
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 = build_problem(data,1.25)
energy2 = build_problem(data,1.5)
energy3 = build_problem(data,1.75)
for i in range(20):
energy1 = problem_iteration(energy1)
energy2 = problem_iteration(energy2)
energy3 = problem_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')
import matplotlib
matplotlib.use('agg')
# matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
from sugar import build_multi_problem, multi_problem_iteration,load_data
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
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):
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 SeparatorApp(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()
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 = 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 = multi_problem_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()
SeparatorApp().run()
from sugar import build_problem, problem_iteration, load_data
from nifty4 import *
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
np.random.seed(42)
if __name__ == '__main__':
path = 'hst_05195_01_wfpc2_f702w_pc_sci.fits'
data = fits.open(path)[1].data
data = data.clip(min=0.001)
data = np.ndarray.astype(data, float)
alpha = 1.25
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.01*data.max()
plt.figure()
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.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.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')
#: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'