def ceil(*args, **kw): arg0 = args[0] if isinstance(arg0, (int, float, long)): return _math.ceil(*args,**kw) elif isinstance(arg0, complex): return _cmath.ceil(*args,**kw) elif isinstance(arg0, _sympy.Basic): return _sympy.ceil(*args,**kw) else: return _numpy.ceiling(*args,**kw)
def closure(vis, tel, lastv=-1): # Find target source id target_id = vis.split('/')[-1].split('_')[0] print "target_id", target_id print "Antennas for closure phase", tel # Find which number is which antenna (amend if you are a taql superstar and can get this less clanky) command = 'taql \'select NAME from %s/ANTENNA\' >closure_which'%vis os.system(command) idx_tels, iline = [-1,-1,-1], 0 f = open('closure_which') for line in f: if not 'select' in line: for i in range(3): if tel[i]==line[:len(tel[i])]: idx_tels[i] = iline iline += 1 f.close() if -1 in idx_tels: print 'Did not find one or more of the telescopes' exit() # get the bandwidth command = 'taql \'select TOTAL_BANDWIDTH from %s/SPECTRAL_WINDOW\' >total_bandwidth'%vis os.system(command) with open('total_bandwidth') as f: lines = f.readlines() f.close() os.system('rm total_bandwidth') bandwidth = np.float(lines[-1].rstrip('\n')) if bandwidth > 2e6: # get channel width command = 'taql \'select CHAN_WIDTH from %s/SPECTRAL_WINDOW\' >chan_width'%vis os.system(command) with open('chan_width') as f: lines = f.readlines() f.close() os.system('rm chan_width') chan_width = np.float(lines[-1].split(',')[0].strip('[')) if chan_width < 195312: nchans = np.ceiling(195312/chan_width) else: nchans = 1 # Make three reference MSs with pointers os.system('rm -fr closure_temp*.ms') command = 'taql \'select from %s where ANTENNA1==%d and ANTENNA2==%d giving closure_temp1.ms\'' %(vis,idx_tels[0],idx_tels[1]) os.system(command) command = 'taql \'select from %s where ANTENNA1==%d and ANTENNA2==%d giving closure_temp2.ms\'' %(vis,idx_tels[1],idx_tels[2]) os.system(command) command = 'taql \'select from %s where ANTENNA1==%d and ANTENNA2==%d giving closure_temp3.ms\'' %(vis,idx_tels[0],idx_tels[2]) os.system(command) # Load data arrays (NB assumes all three have the same structure - needs changing) t1 = pt.table('closure_temp1.ms') t2 = pt.table('closure_temp2.ms') t3 = pt.table('closure_temp3.ms') ut = t1.select('TIME') spw = t1.select('DATA_DESC_ID') d1,d2,d3 = t1.select('DATA'), t2.select('DATA'), t3.select('DATA') cp,a1,a2,a3 = get_amp_clph(d1[:lastv],d2[:lastv],d3[:lastv],spw[:lastv]) # mask wrapped values # unwrap phases: it will correct an array of phases modulo 2pi such that all jumps are less than or equal to pi cp = np.unwrap(cp) # return a statistic - is 1.64 for random closure phase, less for coherent os.system('rm -fr closure_temp*ms') os.system('rm closure_which') os.system('rm closure_out_%s.png' % target_id) plt.clf() plt.ylabel(r'$\phi$ $[rad]$') ; plt.plot(cp,'k+') #; plt.grid(True) plt.title('Scatter for target %s is %s' % (target_id,np.mean(np.gradient(cp)**2))) plt.savefig('closure_out_%s.png' % target_id) return np.mean(np.gradient(cp)**2)
def generate_simple_scanning_plan(self,dx,dy,scan_depth = 3,scan_width = 50): """ scans are defined in terms of a line on the ebeam pattern. Distances (dx,dy) are defined in um. The current position is assumed to be the start (minus nulling of the mos). The system acknowledges the sample tilt and compensates for it. Outputs a list of instructions that can then be fed into the function execute_scanning_plan. Instructions are basically dictionaries containing a single key (the action to be performed) and a list of parameters or more dictionaries with further parameters """ self._todo_list = [] dx_real,dy_real = self.coord_rel_ebeam_to_coarse_piezos(dx,dy) ### need to figure out effective movement of the steppers ##### cut the scan plan up into dx,dy per individual scan. We make this sample tilt dependent. No more than 1 um depth deviation per individual scan tilt_x,tilt_y = self.get_sample_tilt_x(),self.get_sample_tilt_y() segments_x,segments_y = np.floor(tilt_x*scan_width),np.floor(tilt_y*scan_width) ### round down. so effectively allow a tilt of 2 um. if segments_x == 0: segments_x = 1 ### no scans because of no tilt is not an option if segments_y == 0: segments_y = 1 ### no scans because of no tilt is not an option no_of_segments_per_scan_location = segments_x*segments_y total_no_of_scan_locations = np.ceiling(np.sqrt(dx**2+dy**2)/float(scan_width)) ### ceiling because more is better than too little. # self._todo_list = self._todo_list + [{'turn_lights_off':[]}] #XXXX for scan in range(total_no_of_scan_locations): self._todo_list = self._todo_list + [{'mos_reset_fine_XY':[]}] ### go to centre of scan range x0_fine = scan_width*(len(segments_x)-1)/2. y0_fine = scan_width*(len(segments_y)-1)/2. self._todo_list = self._todo_list + [{'mos_move_to_relative_pos':[x0_fine,y0_fine,0]}] ### move to centre of very first segment for s_x in range(segments_x): self._todo_list = self._todo_list + [{'mos_move_to_relative_pos':[(s_x != 0)*scan_width/(len(segments_x),0,0]}] ### move to centre of the current segment_x for s_y in range(segments_y): self._todo_list = self._todo_list + [{'mos_move_to_relative_pos':[0,(s_y!=0)*scan_width/(len(segments_y),0]}] ### move to centre of the current segment_y self._todo_list = self._todo_list + [{'cam_find_surface': []}] ### find z. self._todo_list = self._todo_list + [{'mos_move_to_relative_pos':[0,0,-0.5]}]### want to only check what is under the surface ;) ### generate scan instructions cur_x = self._mos.get_x() cur_y = self._mos.get_y() x0 , x1 = cur_x - scan_width/(2*len(segments_x)), cur_x + scan_width/(2*len(segments_x)) y0 , y1 = cur_y - scan_width/(2*len(segments_y)), cur_y + scan_width/(2*len(segments_y)) self._todo_list = self._todo_list + [{'do_2D_scans': ['scan_'+str(scan)+'segX_'+str(s_x)+'segY_'+str(s_y),x0,x1,y0,y1,range(scan_depth)]}] ### finally move on with the corase steppers total_no_of_scan_locations = float(total_no_of_scan_locations) self._todo_list = self._todo_list + [{'mos_coarse_move_rel': [dx_real/total_no_of_scan_locations,dy_real/total_no_of_scan_locations]}] return def do_2D_scans(self,name,x0,y0,x1,y1,dzs,pixeltime = 10.): """ programs the setup to perform a series of green scans at a certain focal position (relative to the starting position) """ cent_x,ceny = self._mos.get_x(),self._mos.get_y() z0 = self._mos.get_z() xpts = int(np.abs(x0-x1)/self._scan_resolution)+1 ypts = int(np.abs(y0-y1)/self._scan_resolution)+1 aom0 = self._aom.get_power() self._aom.set_power(self._scan_aom_power) self._scan2d.set_xstart(x0) self._scan2d.set_xstop(x1) self._scan2d.set_ystart(y0) self._scan2d.set_ystop(y1) self._scan2d.set_pixeltime(pixeltime) stop_scan = False for dz in dzs: self._mos.set_z(z0+dz) setup_controller.set_keyword(name+'dz_%s'%np.round(dz,2)) self._scan2d.set_is_running(True) while(self._scan2d.get_is_running()): qt.msleep(0.1) if (msvcrt.kbhit() and (msvcrt.getch() == 'q')): stop_scan=True if stop_scan: self._scan2d.set_is_running(False); break qt.msleep(5) self._aom.set_power(aom0) self._mos.set_x(cent_x),self._mos.set_y(cent_y) return True def execute_scanning_plan(self): """ goes through self._todo_list and executes one instruction after the other. instructions are dictionaries with a single key that is assigned to a list of values. {'function_name' : [val1,val2,val3...]} we then execute the following command for each instruction self.function_name(val1,val2,val3...) Resets the todo_list when done. Inputs: None Outputs: None """ for instruction in self._todo_list: execstring = instruction.keys()[0] args = instruction[execstring] execstring = 'self.' + execstring args_string = '(' for arg in args[:-1]: args_string = args_string+',' args_string = args_string + args[-1]+')' exec(execstring+args_string) self._todo_list = [] def load_cfg(self): params_from_cfg = self._ins_cfg.get_all() for p in params_from_cfg: val = self._ins_cfg.get(p) if type(val) == unicode: val = str(val) try: self.set(p, value=val) except: pass def save_cfg(self): parlist = self.get_parameters() for param in parlist: value = self.get(param) self._ins_cfg[param] = value ########################### """ Public Methods TODO """ ########################### def cam_take_image(self): """ Currently no driver for the MatrixVision acquisition card. We trigger the labview program by setting a parameter in the adwin. TODO: Implement remote power plug driver. """ # if self._rem_plug.get_is_on('light_source'): # self._phys_adwin.Set_Par(80,1) # else: # self._rem_plug.turn_on('light_source') # qt.msleep(2) self._phys_adwin.Set_Par(80,1) def mos_go_to_bitmarker(self,bit_x,bit_y,iteration = 0): """ TODO tries to find out where you are and then move towards the right bitmarker """ ### TODOreset the fine piezos in XY x,y,suc = self.cam_where_am_i(return_current_pos = True) if not suc: print 'TODO: do something smart if no bitmarker is recognized in the image' ### calculate distance ### rotate into physical movement ### move in steps towards goal ### check are we there yet? if yes: done; if no: redo! def compare_latest_and_current_pos(self): """ This function evaluates whether the distance travelled makes sense TODO and what are the consequences if it does not. """ x0,y0,x1,y1,dx,dy=[self.get_x_pos(),self.get_y_pos(),self.get_latest_x_pos(),self.get_latest_y_pos(),self.get_logged_distance_x(),self.get_logged_distance_y()] x0-x1 = dx2 y0-y1 = dy2 if abs(dx-dx2)/dx2*dx > 0.2 or abs(dy-dy2)/dy2*dy > 0.2: print "suspiciously high deviation in travelled distance! more local search recommended!" else: self.set_logged_distance_x(0) self.set_logged_distance_y(0) def generate_scanning_plan_bitm(self,list_of_bitm,x_rel_to_bitm,y_rel_to_bitm,scan_depth = 3,scan_width = 50): """ very similar to generate_scanning_plan. However locations are defined in terms of numbers of bitmarkers. Will heavily rely on the function mos_go_to_bitmarker. TODO """ self._todo_list = [] return def find_NVs(self,timestamp): """analysis of past scans for nv finding""" pass def save_NV_position_to_msmt_folder(self,timestamp): """creates msmt folder and stores NV information in said msmt folder""" pass def coord_rel_ebeam_to_camera(self,dx,dy): """ input are relative coordinates from current positions TODO bugcheck! """ if self._last_where_am_i_succeeded: angle = self.get_sample_rotation() x_cam = np.cos(angle)*dx-np.sin(angle)*dy y_cam = np.sin(angle)*dx+np.cos(angle)*dy y_cam = -y_cam ## camera image is flipped along the vertical axis return x_cam,y_cam else: print "Location is unknown, can't conduct relative movements based on ebeam pattern" return 0,0 ### 0,0 indicates no relative movement def coord_rel_camera_to_fine_piezos(self,dx,dy): """ physically knows what is up or down. heavy lifting is done by coord_ebeam_to_camera """ pass def coord_rel_camera_to_coarse_piezos(self,dx,dy): """ physically knows what is up or down. heavy lifting is done by coord_ebeam_to_camera """ pass def ebeam_pattern_load(self,timestamp = None): """TODO loads_ebeam_patterns from file/timestamps""" pass def ebeam_pattern_save(self,timestamp = None): """TODO: stores np array along side other data in the latest msmt file""" pass ####################### """ Private Methods """ ####################### def _cam_apply_laplace_filter(self,img): """ besides applying a filter based on derivatives this function also applies a phenomenological threhsold""" laplacian = cv2.Laplacian(img,cv2.CV_64F,ksize=21) laplacian = (laplacian-(np.amax(laplacian)+np.amin(laplacian))/2.) ## centre around 0 laplacian = 2*laplacian/(np.amax(laplacian)-np.amin(laplacian)) ## normalize to interval of -1 to 1 return ct.apply_brightness_filter(laplacian,-1.0,threshold_max= 0.2)
def _reduce(A,B): if isinstance(A, np.ndarray): return A.reduce(B) else: return reduce(A,B) MONADS = { u'+': lambda A: A, u'-': lambda A: -A, u'×': lambda A: 0 if A==0 else (-1 if A<0 else 1), u'÷': lambda A: 1.0/A, u'*': lambda A: np.exp(A), u'⍟': lambda A: np.log(A), u'○': lambda A: np.pi * A, u'|': lambda A: abs(A), u'⌈': lambda A: int(np.ceiling(A)), u'⌊': lambda A: int(np.floor(A)), u'⍋': lambda A: np.sort(A), u'⍒': lambda A: reversed(np.sort(A)), u'?': lambda A: random.randint(0,A), u'⍳': lambda A: np.arange(A), u'⍴': lambda A: A.shape(), } DYADS = { u'+': lambda A,B: A+B, u'-': lambda A,B: A-B, u'×': lambda A,B: A*B, u'÷': lambda A,B: A/B, u'*': lambda A,B: A**B, u'⍟': lambda A,B: np.log(B,A),
def lazyselect(arr, length, k, universe): if arr == None: assert type(length) is int and type(k) is int and type(universe) is int arr = np.random.randint(0,universe,length) # 1 # pick n^(3/4) elements from S, chosen independently and uniformly # at random with replacement; call this multiset of elements R draw = int(length**(3/4)) P = [None] S_k = None while(len(P) > 4 * n**(3/4) + 2 or S_k not in P): P = [] R = # take with replacement 'draw' elements # 2 # sort R in O(n^(3/4)log(n)) steps using timsort R = sorted(R) # 3 # x = kn^(-1/4) # l = max(floor(x - root(n)), 1) # h = min(floor(x + root(n)), n^(3/4)) # a = R_l # b = R_h # compare a and b to every element of S and determine # r_s(a) and r_s(b) x = (int) k * (length ** (-1/4)) l = max(np.floor(x - np.sqrt(length)), 1) h = min(np.ceiling(x + np.sqrt(length)), (int) n ** (3/4)) a = R[l + 1] b = R[h + 1] a_rank = 0 b_rank = 0 for elem in arr: if elem < a: a_rank += 1 if elem < b: b_rank += 1 # 4 small_n = n ** 1/4 if k < small_n: for elem in arr: if elem <= b: P.append(elem) elif k > n - small_n: for elem in arr: if a <= elem: P.append(elem) elif ((n - small_n) < k) and (k < small_n): for elem in arr: if a <= elem <= b: P.append(elem) # 5 # by sorting P in O(P log P) steps, identify P_(k-r_s(a) + 1) # which is S_k P = sorted(P) S_k = P[k - a_rank + 1] return S_k