def phase_parm(tdeg, fdeg): """helper function to create a t/f parm for phase, including constraints. Placeholder until Maaijke implements periodic constraints. """ polc = meq.polc(Timba.array.zeros((tdeg + 1, fdeg + 1)) * 0.0, scale=array([3600.0, 8e8, 0, 0, 0, 0, 0, 0])) shape = [tdeg + 1, fdeg + 1] # work out constraints on coefficients # maximum excursion in freq is pi/2 # max excursion in time is pi/2 dt = 0.2 df = 0.5 cmin = [] cmax = [] for it in range(tdeg + 1): for jf in range(fdeg + 1): mm = math.pi / (dt ** it * df ** jf) cmin.append(-mm) cmax.append(mm) cmin[0] = -1e9 cmax[0] = 1e9 return Meq.Parm( polc, shape=shape, real_polc=polc, node_groups="Parm", constrain_min=cmin, constrain_max=cmax, table_name=get_mep_table(), )
def phase_parm(tdeg, fdeg): """helper function to create a t/f parm for phase, including constraints. Placeholder until Maaijke implements periodic constraints. """ polc = meq.polc(Timba.array.zeros((tdeg + 1, fdeg + 1)) * 0.0, scale=array([3600., 8e+8, 0, 0, 0, 0, 0, 0])) shape = [tdeg + 1, fdeg + 1] # work out constraints on coefficients # maximum excursion in freq is pi/2 # max excursion in time is pi/2 dt = .2 df = .5 cmin = [] cmax = [] for it in range(tdeg + 1): for jf in range(fdeg + 1): mm = math.pi / (dt**it * df**jf) cmin.append(-mm) cmax.append(mm) cmin[0] = -1e+9 cmax[0] = 1e+9 return Meq.Parm(polc, shape=shape, real_polc=polc, node_groups='Parm', constrain_min=cmin, constrain_max=cmax, table_name=get_mep_table())
def _resolve_grid(axisname, dom, num, grid, cellsize): # first, figure out grid # (a) grid specified explicitly if grid is not None and len(grid): if grid.ndim != 1: raise TypeError, '%s_grid must be a vector' % axisname if num is not None and num != len(grid): raise ValueError, 'both num_%s and %s_grid specified but do not match' % ( axisname, axisname) num = len(grid) # figure out segments if num < 3: # <=2 points: always regular segs = record(start_index=0, end_index=num - 1) else: # >2 grid points: check for regularity segs = record(start_index=[0], end_index=[1]) cur_step = grid[1] - grid[0] for i in range(2, num - 1): dx = grid[i] - grid[i - 1] if cur_step is not None and dx != cur_step: # start new segment if new step cur_step = None segs.start_index.append(i) segs.end_index.append(i) else: # else extend current segment cur_step = dx segs.end_index[-1] = i # update end of current segment segs.start_index = asarray(segs.start_index, arr_int32) segs.end_index = asarray(segs.end_index, arr_int32) # (b) num is specified elif num is not None: if num <= 0: raise ValueError, 'illegal num_%s value' % axisname if dom is None: raise ValueError, 'domain must be specified to use num_%s' % axisname step = (dom[1] - dom[0]) / num grid = dom[0] + (Timba.array.arange(num) + 0.5) * step # set cell size if not specified if cellsize is None or not len(cellsize): cellsize = Timba.array.zeros([num], arr_double) + step segs = record(start_index=0, end_index=num - 1) else: raise ValueError, 'either num_%s or %s_grid must be specified' % ( axisname, axisname) # resolve cell size if not specified # use distance to nearest grid point as the cell size if cellsize is None or not len(cellsize): cellsize = Timba.array.zeros([num], arr_double) x = array([2 * grid[0] - grid[1]] + grid.ravel + [2 * grid[-1] - grid[-2]]) d1 = grid - x[:-2] d2 = x[2:] - grid for i in range(num): cellsize[i] = min(d1[i], d2[i]) elif len(cellsize) == 1: cellsize = Timba.array.zeros([num], arr_double) + cellsize[0] elif len(cellsize) != num: raise ValueError, 'length of %s_cell_size does not conform to grid shape' % axisname return (grid, cellsize, segs)
def _resolve_grid (axisname,dom,num,grid,cellsize): # first, figure out grid # (a) grid specified explicitly if grid is not None and len(grid): if grid.ndim != 1: raise TypeError,'%s_grid must be a vector'%axisname; if num is not None and num != len(grid): raise ValueError,'both num_%s and %s_grid specified but do not match'%(axisname,axisname); num = len(grid); # figure out segments if num<3: # <=2 points: always regular segs = record(start_index=0,end_index=num-1); else: # >2 grid points: check for regularity segs = record(start_index=[0],end_index=[1]); cur_step = grid[1]-grid[0]; for i in range(2,num-1): dx = grid[i] - grid[i-1]; if cur_step is not None and dx != cur_step: # start new segment if new step cur_step = None; segs.start_index.append(i); segs.end_index.append(i); else: # else extend current segment cur_step = dx; segs.end_index[-1] = i; # update end of current segment segs.start_index = asarray(segs.start_index,arr_int32); segs.end_index = asarray(segs.end_index,arr_int32); # (b) num is specified elif num is not None: if num <= 0: raise ValueError,'illegal num_%s value'%axisname; if dom is None: raise ValueError,'domain must be specified to use num_%s'%axisname; step = (dom[1]-dom[0])/num; grid = dom[0] + (Timba.array.arange(num)+0.5)*step; # set cell size if not specified if cellsize is None or not len(cellsize): cellsize = Timba.array.zeros([num],arr_double) + step; segs = record(start_index=0,end_index=num-1); else: raise ValueError,'either num_%s or %s_grid must be specified'%(axisname,axisname); # resolve cell size if not specified # use distance to nearest grid point as the cell size if cellsize is None or not len(cellsize): cellsize = Timba.array.zeros([num],arr_double); x = array([ 2*grid[0]-grid[1]] + grid.ravel + [2*grid[-1]-grid[-2]]); d1 = grid - x[:-2]; d2 = x[2:] - grid; for i in range(num): cellsize[i] = min(d1[i],d2[i]); elif len(cellsize) == 1: cellsize = Timba.array.zeros([num],arr_double) + cellsize[0]; elif len(cellsize) != num: raise ValueError,'length of %s_cell_size does not conform to grid shape'%axisname; return (grid,cellsize,segs);
def polc (coeff,shape=None,offset=None,scale=None,domain=None, weight=None,dbid=None,axis_index=None,pert=1e-6,subclass=_polc_type): """creates a polc record"""; rec = subclass(); # process coeff argument -- if a list, then force into a 2D array if isinstance(coeff,(tuple,list)): if shape and len(shape)>2: raise ValueError,'coeff array must be one- or two-dimensional'; # if filter(lambda x:isinstance(x,complex),coeff): # coeff = array_complex(coeff,shape=shape); # else: coeff = array_double(coeff,shape=shape); if is_scalar(coeff): # if not isinstance(coeff,complex): # force float or complex coeff = float(coeff); rec.coeff = array(coeff); elif is_array(coeff): if len(coeff.shape) > 2: raise TypeError,'coeff array must be one- or two-dimensional'; ## if coeff.type() not in (arr_double,arr_dcomplex): ## raise TypeError,'coeff array must be float (Float64) or dcomplex (Complex64)'; ## if not coeff.type() == arr_double: ## raise TypeError,'coeff array must be float (Float64)'; rec.coeff = array_double(coeff); else: raise TypeError,'illegal coeff argument'; # process domain argument if domain is not None: if isinstance(domain,_domain_type): rec.domain = domain; else: raise TypeError,'domain argument must be a MeqDomain object'; # other optional arguments if offset is not None: if isinstance(offset,(tuple,list,int,float)): rec.offset = array_double(offset); elif is_array(offset): if len(offset.shape) > 1: raise TypeError,'offset array must be one-dimensional'; rec.offset = array_double(offset); else: raise TypeError,"invalid 'offset' argument of type %s"%type(offset); if scale is not None: if isinstance(scale,(tuple,list,int,float)): rec.scale = array_double(scale); elif is_array(scale): if len(scale.shape) > 1: raise TypeError,'scale array must be one-dimensional'; rec.scale = array_double(scale); else: raise TypeError,"invalid 'scale' argument of type %s"%type(scale); if axis_index is not None: if isinstance(axis_index,(tuple,list,int)): rec.axis_index = array_int(axis_index); elif is_array(axis_index): if len(scale.shape) > 1: raise TypeError,'axis_index array must be one-dimensional'; rec.axis_index = array_int(axis_index); else: raise TypeError,"invalid 'axis_index' argument of type %s"%type(scale); if weight is not None: rec.weight = float(weight); if dbid is not None: rec.dbid = int(dbid); if pert is not None: rec.pert = float(pert); return rec;
def array_int (value,shape=None): arr = array(value,dtype=arr_int32); if shape: arr.shape = shape; return arr;
def array_complex (value,shape=None): arr = array(value,dtype=arr_dcomplex); if shape: arr.shape = shape; return arr;
def array_double (value,shape=None): arr = array(value,dtype=arr_double); if shape: arr.shape = shape; return arr;
def polc(coeff, shape=None, offset=None, scale=None, domain=None, weight=None, dbid=None, axis_index=None, pert=1e-6, subclass=_polc_type): """creates a polc record""" rec = subclass() # process coeff argument -- if a list, then force into a 2D array if isinstance(coeff, (tuple, list)): if shape and len(shape) > 2: raise ValueError('coeff array must be one- or two-dimensional') # if filter(lambda x:isinstance(x,complex),coeff): # coeff = array_complex(coeff,shape=shape); # else: coeff = array_double(coeff, shape=shape) if is_scalar(coeff): # if not isinstance(coeff,complex): # force float or complex coeff = float(coeff) rec.coeff = array(coeff) elif is_array(coeff): if len(coeff.shape) > 2: raise TypeError('coeff array must be one- or two-dimensional') ## if coeff.type() not in (arr_double,arr_dcomplex): ## raise TypeError,'coeff array must be float (Float64) or dcomplex (Complex64)'; ## if not coeff.type() == arr_double: ## raise TypeError,'coeff array must be float (Float64)'; rec.coeff = array_double(coeff) else: raise TypeError('illegal coeff argument') # process domain argument if domain is not None: if isinstance(domain, _domain_type): rec.domain = domain else: raise TypeError('domain argument must be a MeqDomain object') # other optional arguments if offset is not None: if isinstance(offset, (tuple, list, int, float)): rec.offset = array_double(offset) elif is_array(offset): if len(offset.shape) > 1: raise TypeError('offset array must be one-dimensional') rec.offset = array_double(offset) else: raise TypeError("invalid 'offset' argument of type %s" % type(offset)) if scale is not None: if isinstance(scale, (tuple, list, int, float)): rec.scale = array_double(scale) elif is_array(scale): if len(scale.shape) > 1: raise TypeError('scale array must be one-dimensional') rec.scale = array_double(scale) else: raise TypeError("invalid 'scale' argument of type %s" % type(scale)) if axis_index is not None: if isinstance(axis_index, (tuple, list, int)): rec.axis_index = array_int(axis_index) elif is_array(axis_index): if len(scale.shape) > 1: raise TypeError('axis_index array must be one-dimensional') rec.axis_index = array_int(axis_index) else: raise TypeError("invalid 'axis_index' argument of type %s" % type(scale)) if weight is not None: rec.weight = float(weight) if dbid is not None: rec.dbid = int(dbid) if pert is not None: rec.pert = float(pert) return rec
def array_int(value, shape=None): arr = array(value, dtype=arr_int32) if shape: arr.shape = shape return arr
def array_complex(value, shape=None): arr = array(value, dtype=arr_dcomplex) if shape: arr.shape = shape return arr
def array_double(value, shape=None): arr = array(value, dtype=arr_double) if shape: arr.shape = shape return arr