def check_model(model, im_h, obj_h): obj = siddon.simu.object_from_header(obj_h) obj = siddon.fa.InfoArray(data=obj, header=dict(obj.header)) obj[:] = 1. im_h['n_images'] = 100 im_h['max_lon'] = 3 * np.pi data = siddon.simu.circular_trajectory_data(**im_h) if obj.dtype == data.dtype: P, D, obj_mask, data_mask = model(data, obj, obj_rmin=1., decimate=True) Mo = lo.decimate(obj_mask) w = (Mo.T * (P.T * np.ones(data.size))).reshape(obj_mask.shape) is_seen = (w != 0) new_obj = fa.FitsArray(obj_mask.shape) new_obj = 1. new_obj *= (1 - obj_mask) data[:] = (P * Mo * new_obj.ravel()).reshape(data.shape) hypers = new_obj.ndim * (1e-10, ) sol = lo.acg(P, data.ravel(), D, hypers=hypers, tol=1e-20) sol = fa.asfitsarray((Mo.T * sol).reshape(obj_mask.shape), header=obj.header) assert_almost_equal(sol[is_seen], new_obj[is_seen], decimal=1)
# make sure it is 64 bits data data.header[i]['BITPIX'] = -64 # cube cube = tomograpy.centered_cubic_map(3, 64, fill=0.) # model kwargs = {'obj_rmin':1., 'obj_rmax':1.5, 'data_rmin':1., 'data_rmax':1.3, 'mask_negative':False, 'mask_nan':True} P, D, obj_mask, data_mask = tomograpy.models.srt(data, cube, **kwargs) # apply mask to data data *= (1 - data_mask) data[np.isnan(data)] = 0. # hyperparameters hypers = cube.ndim * (1e0, ) # inversion # expected time b = data.ravel() b[np.isnan(b)] = 0. t = time.time() bpj = P.T * b print((time.time() - t) * 4 * 100 ) # real time t = time.time() sol = lo.acg(P, b, D, hypers, maxiter=100) print(time.time() - t) # reshape result fsol = tomograpy.fa.asfitsarray(sol.reshape(cube.shape), header=cube.header) tomograpy.path = os.path.join(os.getenv('HOME'), 'data', 'tomograpy.) fsol.tofits(os.path.join(tomograpy.path, "output", "test_tomograpy.secchi_mask.fits"))
# data path = os.path.join(os.getenv('HOME'), 'data', '171dec08') obsrvtry = 'STEREO_A' time_window = ['2008-12-01T00:00:00.000', '2008-12-03T00:00:00.000'] # one image every time_step seconds time_step = 4 * 3600. data = siddon.secchi.read_data(path, bin_factor=4, obsrvtry=obsrvtry, time_window=time_window, time_step=time_step) # cube shape = 3 * (128,) header = {'CRPIX1':64., 'CRPIX2':64., 'CRPIX3':64., 'CDELT1':0.0234375, 'CDELT2':0.0234375, 'CDELT3':0.0234375, 'CRVAL1':0., 'CRVAL2':0., 'CRVAL3':0.,} cube = fa.zeros(shape, header=header) # model P = siddon.siddon_lo(data.header, cube.header) D = [lo.diff(cube.shape, axis=i) for i in xrange(cube.ndim)] hypers = cube.ndim * (1e0, ) # inversion t = time.time() A = P.T * P + np.sum([h * d.T * d for h, d in zip(hypers, D)]) b = P.T * data.flatten() #callback = lo.iterative.CallbackFactory(verbose=True) #x, info = spl.bicgstab(A, b, maxiter=100, callback=callback) x, info = lo.acg(P, data.flatten(), D, hypers, maxiter=100,) sol = cube.copy() sol[:] = x.reshape(cube.shape) print(time.time() - t)
obj = tomograpy.centered_cubic_map(3, 32) obj[:] = tomograpy.phantom.shepp_logan(obj.shape) # data radius = 200. a = tomograpy.fov(obj.header, radius) data = tomograpy.centered_stack(a, 128, n_images=60, radius=radius, max_lon=np.pi) # projector P = tomograpy.lo(data.header, obj.header) # projection t = time.time() data = tomograpy.projector(data, obj) print("projection time : " + str(time.time() - t)) # data y = data.flatten() # backprojection t = time.time() x0 = P.T * y bpj = x0.reshape(obj.shape) print("projection time : " + str(time.time() - t)) # priors Ds = [lo.diff(obj.shape, axis=i) for i in xrange(3)] # inversion using scipy.sparse.linalg t = time.time() sol = lo.acg(P, y, Ds, 1e-2 * np.ones(3), maxiter=100, tol=1e-20) sol = sol.reshape(bpj.shape) print("inversion time : " + str(time.time() - t))
print("projection time : " + str(time.time() - t)) # backprojection x0 = obj4.copy() x0[:] = 0. t = time.time() x0 = tomograpy.backprojector4d(data, x0, obstacle="sun") print("backprojection time : " + str(time.time() - t)) # model kwargs = { 'obj_rmin': 1., #'obj_rmax':1.3, 'mask_negative': False, 'dt_min': 100 } P, D, obj_mask, data_mask = tomograpy.models.stsrt(data, obj, **kwargs) # hyperparameters hypers = (1e-1, 1e-1, 1e-1, 1e3) # test time for one projection b = data.ravel() t = time.time() u = P.T * b print("time with index grouping : %f" % ((time.time() - t))) # inversion t = time.time() sol = lo.acg(P, b, D, hypers, tol=1e-10, maxiter=100) # reshape result fsol = tomograpy.fa.asfitsarray(sol.reshape(obj_mask.shape), header=obj4.header) print(time.time() - t) #fsol.tofits('stsrt_test.fits')
projection = tm.Projection(obs, npixels_per_sample=4) P = lo.aslinearoperator(projection) # simulate data x0 = tm.gaussian(projection.shapein, 3) # map with gaussian source tod0 = projection(x0) # data n = np.random.randn(*tod0.shape) # noise nsr = 1e-2 tod = tod0 + nsr * n # noisy data y = tod.ravel() # as 1d array # load compression matrix filename = os.path.join(os.getenv("HOME"), "data", "pacs", "mmc_cam_angle_0_scan_angle_0_speed60.fits") c = fa.FitsArray(file=filename).astype(np.float64) cmm = csh.compression.AnyOperator(c) C = cmm((projection.shapeout[0], projection.shapeout[1][0]), ) # compress z = C * y # inversion H = C * P Ds = [lo.diff(x0.shape, axis=i) for i in (0, 1)] x_inv = lo.acg(H, z, Ds, 1e-1 * np.ones(2), tol=1e-10, maxiter=100) x_inv.resize(x0.shape) # condition number #M = H.T * H #Md = M.todense() #print np.linalg.cond(Md) #print lo.iterative.utils.cond(H.T * H)
#!/usr/bin/env python import time import numpy as np import tomograpy import lo # object obj = tomograpy.centered_cubic_map(10, 64) obj[:] = tomograpy.phantom.shepp_logan(obj.shape) # data radius = 200 a = tomograpy.fov(obj, radius) data = tomograpy.centered_stack(a, 128, n_images=60, radius=radius, max_lon=np.pi) # model kwargs = {"pb":"pb", "obj_rmin":1.5, "data_rmin":1.5} P, D, obj_mask, data_mask = tomograpy.models.thomson(data, obj, u=.5, **kwargs) # projection t = time.time() data[:] = (P * obj.ravel()).reshape(data.shape) print("projection time : " + str(time.time() - t)) # data # backprojection t = time.time() x0 = P.T * data.ravel() bpj = x0.reshape(obj.shape) print("backprojection time : " + str(time.time() - t)) # inversion using scipy.sparse.linalg t = time.time() sol = lo.acg(P, data.ravel(), D, 1e-3 * np.ones(3), maxiter=100, tol=1e-8) sol = sol.reshape(obj.shape) print("inversion time : " + str(time.time() - t))
'CDELT1':cdelt[0], 'CDELT2':cdelt[1], 'CDELT3':cdelt[2], 'CRVAL1':0., 'CRVAL2':0., 'CRVAL3':0.,} cube = fa.zeros(shape, header=header) t = time.time() cube = tomograpy.backprojector(data, cube, obstacle="sun") print("backprojection time : " + str(time.time() - t)) # inversion t = time.time() u = .5 kwargs={ "obj_rmin":1.5, "obj_rmax":3., "data_rmin":1.5, "data_rmax":2.5, "mask_negative":True } P, D, obj_mask, data_mask = tomograpy.models.thomson(data, cube, u, **kwargs) # bpj b = data.flatten() bpj = (P.T * b).reshape(cube.shape) hypers = 1e3 * np.ones(3) sol = lo.acg(P, b, D, hypers, maxiter=100, tol=1e-6) print("inversion time : %f" % (time.time() - t)) # reshape solution sol.resize(cube.shape) sol = fa.asfitsarray(sol, header=cube.header) # reproject solution reproj = P * sol.ravel() reproj.resize(data.shape)
# The model is the masking of the sky map then the projection # This is basically matrix multiplication model = projection * mask # Performing inversion # --------------------- # with TAMASIS x_tm = mapper_rls(tod, model, hyper=1e-1, tol=1e-10, maxiter=100) # with lo routines # transform to lo H = lo.aslinearoperator(model * mask) # smoothness priors Ds = [lo.diff(backmap.shape, axis=axis) for axis in (0, 1)] # inversion y = tod.ravel() # requires 1d input x_lo = lo.acg(H, y, Ds, 1e-1 * np.ones(3), tol=1e-10, maxiter=100) x_lo.resize(backmap.shape) # output is 1d so need reshaping # with sparsity assumptions (using Huber algorithm) x_h = lo.hacg(H, y, Ds, 1e1 * np.ones(3), np.asarray((None, 1e-6, 1e-6, 1e-6)), x0=x_lo.flatten(), tol=1e-7, maxiter=200) x_h.resize(backmap.shape)
import lo # object obj = tomograpy.centered_cubic_map(10, 64) obj[:] = tomograpy.phantom.shepp_logan(obj.shape) # data radius = 200 a = tomograpy.fov(obj, radius) data = tomograpy.centered_stack(a, 128, n_images=60, radius=radius, max_lon=np.pi) # model kwargs = {"pb": "pb", "obj_rmin": 1.5, "data_rmin": 1.5} P, D, obj_mask, data_mask = tomograpy.models.thomson(data, obj, u=.5, **kwargs) # projection t = time.time() data[:] = (P * obj.ravel()).reshape(data.shape) print("projection time : " + str(time.time() - t)) # data # backprojection t = time.time() x0 = P.T * data.ravel() bpj = x0.reshape(obj.shape) print("backprojection time : " + str(time.time() - t)) # inversion using scipy.sparse.linalg t = time.time() sol = lo.acg(P, data.ravel(), D, 1e-3 * np.ones(3), maxiter=100, tol=1e-8) sol = sol.reshape(obj.shape) print("inversion time : " + str(time.time() - t))
coverage = projection.T(np.ones(tod.shape)) # naive map naive = backmap / coverage # mask according to coverage (everything that is covered by less than 10.) mask = Masking(coverage < 10.) # The model is the masking of the sky map then the projection # This is basically matrix multiplication model = projection * mask # Performing inversion # --------------------- # with TAMASIS x_tm = mapper_rls(tod, model, hyper=1e-1, tol=1e-10, maxiter=100) # with lo routines # transform to lo H = lo.aslinearoperator(model * mask) # smoothness priors Ds = [lo.diff(backmap.shape, axis=axis) for axis in (0, 1)] # inversion y = tod.ravel() # requires 1d input x_lo = lo.acg(H, y, Ds, 1e-1 * np.ones(3), tol=1e-10, maxiter=100) x_lo.resize(backmap.shape) # output is 1d so need reshaping # with sparsity assumptions (using Huber algorithm) x_h = lo.hacg(H, y, Ds, 1e1 * np.ones(3), np.asarray((None, 1e-6, 1e-6, 1e-6)), x0=x_lo.flatten(), tol=1e-7, maxiter=200) x_h.resize(backmap.shape)
# create projector projection = tm.Projection(obs, npixels_per_sample=4) P = lo.aslinearoperator(projection) # simulate data x0 = tm.gaussian(projection.shapein, 3) # map with gaussian source tod0 = projection(x0) # data n = np.random.randn(*tod0.shape) # noise nsr = 1e-2 tod = tod0 + nsr * n # noisy data y = tod.ravel() # as 1d array # load compression matrix filename = os.path.join(os.getenv("HOME"), "data", "pacs", "mmc_cam_angle_0_scan_angle_0_speed60.fits") c = fa.FitsArray(file=filename).astype(np.float64) cmm = csh.compression.AnyOperator(c) C = cmm((projection.shapeout[0], projection.shapeout[1][0])) # compress z = C * y # inversion H = C * P Ds = [lo.diff(x0.shape, axis=i) for i in (0, 1)] x_inv = lo.acg(H, z, Ds, 1e-1 * np.ones(2), tol=1e-10, maxiter=100) x_inv.resize(x0.shape) # condition number # M = H.T * H # Md = M.todense() # print np.linalg.cond(Md) # print lo.iterative.utils.cond(H.T * H)
# data path = os.path.join(os.getenv('HOME'), 'data', '171dec08') obsrvtry = 'STEREO_A' time_window = ['2008-12-01T00:00:00.000', '2008-12-03T00:00:00.000'] # one image every time_step seconds time_step = 4 * 3600. data = tomograpy.secchi.read_data(path, bin_factor=4, obsrvtry=obsrvtry, time_window=time_window, time_step=time_step) # cube shape = 3 * (128,) header = {'CRPIX1':64., 'CRPIX2':64., 'CRPIX3':64., 'CDELT1':0.0234375, 'CDELT2':0.0234375, 'CDELT3':0.0234375, 'CRVAL1':0., 'CRVAL2':0., 'CRVAL3':0.,} cube = fa.zeros(shape, header=header) # model P = tomograpy.lo(data.header, cube.header) D = [lo.diff(cube.shape, axis=i) for i in xrange(cube.ndim)] hypers = cube.ndim * (1e0, ) # inversion t = time.time() A = P.T * P + np.sum([h * d.T * d for h, d in zip(hypers, D)]) b = P.T * data.flatten() #callback = lo.iterative.CallbackFactory(verbose=True) #x, info = spl.bicgstab(A, b, maxiter=100, callback=callback) x, info = lo.acg(P, data.flatten(), D, hypers, maxiter=100,) sol = cube.copy() sol[:] = x.reshape(cube.shape) print(time.time() - t)
# projection t = time.time() data = tomograpy.projector4d(data, obj4, obstacle="sun") print("projection time : " + str(time.time() - t)) # backprojection x0 = obj4.copy() x0[:] = 0. t = time.time() x0 = tomograpy.backprojector4d(data, x0, obstacle="sun") print("backprojection time : " + str(time.time() - t)) # model kwargs = {'obj_rmin':1., #'obj_rmax':1.3, 'mask_negative':False, 'dt_min':100} P, D, obj_mask, data_mask = tomograpy.models.stsrt(data, obj, **kwargs) # hyperparameters hypers = (1e-1, 1e-1, 1e-1, 1e3) # test time for one projection b = data.ravel() t = time.time() u = P.T * b print("time with index grouping : %f" % ((time.time() - t))) # inversion t = time.time() sol = lo.acg(P, b, D, hypers, tol=1e-10, maxiter=100) # reshape result fsol = tomograpy.fa.asfitsarray(sol.reshape(obj_mask.shape), header=obj4.header) print(time.time() - t) #fsol.tofits('stsrt_test.fits')
import tomograpy import lo # object obj = tomograpy.centered_cubic_map(3, 32) obj[:] = tomograpy.phantom.shepp_logan(obj.shape) # data radius = 200. a = tomograpy.fov(obj.header, radius) data = tomograpy.centered_stack(a, 128, n_images=60, radius=radius, max_lon=np.pi) # projector P = tomograpy.lo(data.header, obj.header) # projection t = time.time() data = tomograpy.projector(data, obj) print("projection time : " + str(time.time() - t)) # data y = data.flatten() # backprojection t = time.time() x0 = P.T * y bpj = x0.reshape(obj.shape) print("projection time : " + str(time.time() - t)) # priors Ds = [lo.diff(obj.shape, axis=i) for i in xrange(3)] # inversion using scipy.sparse.linalg t = time.time() sol = lo.acg(P, y, Ds, 1e-2 * np.ones(3), maxiter=100, tol=1e-20) sol = sol.reshape(bpj.shape) print("inversion time : " + str(time.time() - t))