def inversion(path, obj_params, data_params, opt_params, mask_params): """ Perform an inversion using given parameters. """ import numpy as np import lo, siddon import fitsarray as fa import solar, models # data data = solar.read_data(path, **data_params) if data is None: return data = solar.sort_data_array(data) # create object obj = make_object(obj_params) # configuration persistency out_header = persistency_header(obj.header, data_params, mask_params, opt_params) # pop optimization parameters model = opt_params.pop("model") optimizer = opt_params.pop("optimizer") hypers = opt_params.pop("hypers") if opt_params.has_key("input"): opt_params["x0"] = fa.FitsArray(file=opt_params["input"]) # model P, D, obj_mask, data_mask = model(data, obj, **mask_params) # apply masking to data data *= (1 - data_mask) data[np.isnan(data)] = 0. # inversion b = data.ravel() exec("sol = lo." + optimizer + "(P, b, D, hypers, **opt_params)") # reshape result sol = fa.asfitsarray(sol.reshape(obj_mask.shape), header=out_header) return sol
def _radius_map(my_map): from fitsarray import asfitsarray x, y, z = asfitsarray(my_map).axes() x2 = x ** 2 y2 = y ** 2 z2 = z ** 2 X2, Y2 = np.meshgrid(x2, y2) R = np.dstack([X2 + Y2 + z2i for z2i in z2]) return R
def define_map_mask(cube, obj_rmin=None, obj_rmax=None, obj_cylinder=None, remove_nan=False, **kwargs): """ Output a mask of shape cube.shape. """ if remove_nan: obj_mask = np.isnan(cube) else: obj_mask = np.zeros(cube.shape, dtype=bool) if obj_rmin is not None or obj_rmax is not None or obj_cylinder is not None: R = map_radius(fa.asfitsarray(cube)) if obj_rmin is not None: obj_mask[R < obj_rmin] = 1 if obj_rmax is not None: obj_mask[R > obj_rmax] = 1 if obj_cylinder is not None: map_ = cylinder(fa.asfitsarray(cube, obj_cylinder)) obj_mask[map_ == 1] = 1 return obj_mask
def _pb_data_coef(data): """Returns pb coefficients for a data array.""" from fitsarray import asfitsarray coefs = np.zeros(data.shape) # loop on images assuming images are on last axis for i in xrange(data.shape[-1]): # get phyiscal coordinates of pixels im = asfitsarray(solar.slice_data(data, i)) alpha, beta = im.axes() Alpha, Beta = np.meshgrid(alpha, beta) # define coefficients as square of impact parameter coefs[..., i] = _impact_parameter(Alpha, Beta, im.header['D']) ** 2 return coefs
def _pb_map_coef(my_map, u): """Returns pb map coefficients corresponding to a map array.""" from fitsarray import asfitsarray coefs = np.zeros(my_map.shape) x, y, z = asfitsarray(my_map).axes() x2 = x ** 2 y2 = y ** 2 z2 = z ** 2 X2, Y2 = np.meshgrid(x2, y2) # loop on z to avoid memory explosion ! for i, z2i in enumerate(z2): R2 = X2 + Y2 + z2i R = np.sqrt(R2) O = _r2omega(R) C1, C2 = _pb_thomson_coef(O) coefs[..., i] = ((1 - u) * C1 + u * C2) / (R2 ** 2) # set infinite values due to divide by zero to 0. coefs[1 - np.isfinite(coefs)] = 0. return coefs * np.pi * sigma / 2.
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)
'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)