### Started implementing operator tests.

```Removed nrow, ncol, ndim from operator class.
Fixed hashing of spaces and fft cache.
Fixed two bugs in the los_response line_integrator.```
parent e5f8538c
 ... ... @@ -32,6 +32,13 @@ """ from __future__ import division import matplotlib as mpl mpl.use('Agg') import imp #nifty = imp.load_module('nifty', None, # '/home/steininger/Downloads/nifty', ('','',5)) from nifty import * ... ... @@ -39,7 +46,7 @@ from nifty import * class problem(object): def __init__(self, x_space, s2n=2, **kwargs): def __init__(self, x_space, s2n=0.5, **kwargs): """ Sets up a Wiener filter problem. ... ... @@ -51,14 +58,16 @@ class problem(object): Signal-to-noise ratio (default: 2). """ self.store = [] ## set signal space self.z = x_space ## set conjugate space self.k = self.z.get_codomain() #self.k.power_indices.set_default() #self.k.set_power_indices(**kwargs) ## set some power spectrum self.power = (lambda k: 42 / (k + 1) ** 3) self.power = (lambda k: 42 / (k + 1) ** 5) ## define signal covariance self.S = power_operator(self.k, spec=self.power, bare=True) ... ... @@ -151,7 +160,10 @@ class problem(object): ## pre-compute denominator denominator = self.k.power_indices["rho"] + 2 * (alpha - 1 + abs(epsilon)) self.save_signal_and_data() ## iterate i = 0 iterating = True while(iterating): ... ... @@ -159,7 +171,7 @@ class problem(object): self.m = self.D(self.j, W=self.S, tol=1E-3, note=True) if(self.m is None): break print 'Reconstructed m' #print'Reconstructed m' ## reconstruct power spectrum tr_B1 = self.Sk.pseudo_tr(self.m) ## == Sk(m).pseudo_dot(m) print 'Calculated trace B1' ... ... @@ -173,17 +185,49 @@ class problem(object): print ('denominator', denominator) print ('power', power) print 'Calculated power' power = np.clip(power, 0.1, np.max(power)) power = np.clip(power, 0.00000001, np.max(power)) self.store += [{'tr_B1': tr_B1, 'tr_B2': tr_B2, 'num': numerator, 'denom': denominator}] ## check convergence dtau = log(power / self.S.get_power(), base=self.S.get_power()) print ('dtau', np.max(np.abs(dtau))) iterating = (np.max(np.abs(dtau)) > 2E-2) print max(np.abs(dtau)) #printmax(np.abs(dtau)) self.save_map(i) i += 1 ## update signal covariance self.S.set_power(power, bare=False) ## auto-updates D ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ def save_signal_and_data(self): self.s.plot(title="signal", save="img/signal.png") try: d_ = field(self.z, val=self.d.val, target=self.k) d_.plot(title="data", vmin=self.s.min(), vmax=self.s.max(), save="img/data.png") except: pass def save_map(self, index=0): # save map if(self.m is None): pass else: self.m.plot(title="reconstructed map", vmin=self.s.min(), vmax=self.s.max(), save="img/map_"+str(index)+".png") self.m.plot(power=True, mono=False, other=(self.power, self.S.get_power()), nbin=None, binbounds=None, log=False, save='img/map_power_'+str(index)+".png") def plot(self): """ Produces plots. ... ... @@ -209,7 +253,7 @@ class problem(object): ##----------------------------------------------------------------------------- # if(__name__=="__main__"): x = rg_space((128,)) x = rg_space((128,128), zerocenter=True) p = problem(x, log = False) about.warnings.off() ## pl.close("all") ... ...
 # -*- coding: utf-8 -*- from nifty import * about.warnings.off() shape = (256, 256) x_space = rg_space(shape) k_space = x_space.get_codomain() power = lambda k: 42/((1+k*shape)**2) S = power_operator(k_space, codomain=x_space, spec=power) s = S.get_random_field(domain=x_space) def make_los(n=10, angle=0, d=1): starts_list = [] ends_list = [] for i in xrange(n): starts_list += [[(-0.2)*d, (-0.2 + 1.2*i/n)*d]] ends_list += [[(1.2)*d, (-0.2 + 1.2*i/n)*d]] starts_list = np.array(starts_list) ends_list = np.array(ends_list) rot_matrix = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]) starts_list = rot_matrix.dot(starts_list.T-0.5*d).T+0.5*d ends_list = rot_matrix.dot(ends_list.T-0.5*d).T+0.5*d return (starts_list, ends_list) temp_coords = (np.empty((0,2)), np.empty((0,2))) n = 256 m = 256 for alpha in [np.pi/n*j for j in xrange(n)]: temp = make_los(n=m, angle=alpha) temp_coords = np.concatenate([temp_coords, temp], axis=1) starts = list(temp_coords.T) ends = list(temp_coords.T) #n_points = 360. #starts = [[(np.cos(i/n_points*np.pi)+1)*shape/2., # (np.sin(i/n_points*np.pi)+1)*shape/2.] for i in xrange(int(n_points))] #starts = list(np.array(starts).T) # #ends = [[(np.cos(i/n_points*np.pi + np.pi)+1)*shape/2., # (np.sin(i/n_points*np.pi + np.pi)+1)*shape/2.] for i in xrange(int(n_points))] #ends = list(np.array(ends).T) R = los_response(x_space, starts=starts, ends=ends, sigmas_up=0.1, sigmas_low=0.1) d_space = R.target N = diagonal_operator(d_space, diag=s.var(), bare=True) n = N.get_random_field(domain=d_space) d = R(s) + n j = R.adjoint_times(N.inverse_times(d)) D = propagator_operator(S=S, N=N, R=R) m = D(j, W=S, tol=1E-14, limii=50, note=True) s.plot(title="signal", save='1_plot_s.png') s.plot(save='plot_s_power.png', power=True, other=power) j.plot(save='plot_j.png') #d_ = field(x_space, val=d.val, target=k_space) #d_.plot(title="data", vmin=s.min(), vmax=s.max(), save='plot_d.png') m.plot(title="reconstructed map", vmin=s.min(), vmax=s.max(), save='1_plot_m.png') m.plot(title="reconstructed map", save='2_plot_m.png') m.plot(save='plot_m_power.png', power=True, other=power) \ No newline at end of file