def compare(zid,user_name,verbose=True): their_answer = consensus.one_answer(zid,user_name) expert_answer = consensus.checksum(zid) nt = len(their_answer) nx = len(expert_answer) if nt == nx: match_temp = True for tk in their_answer.iterkeys(): if expert_answer.has_key(tk): match_temp = True and expert_answer.has_key(tk) if verbose: if match_temp: print "%s's answer for %s (%i sources) matches that of the experts." % (user_name,zid,nt) else: print "%s's answer for %s (%i sources) does not match that of the experts." % (user_name,zid,nt) match_expert = match_temp else: if verbose: print "%s's number of sources for %s does not match (%i vs %i)" % (user_name,zid,nt,nx) match_expert = False return match_expert
def compare(zid, user_name, verbose=True): their_answer = consensus.one_answer(zid, user_name) expert_answer = consensus.checksum(zid) nt = len(their_answer) nx = len(expert_answer) if nt == nx: match_temp = True for tk in their_answer.iterkeys(): if expert_answer.has_key(tk): match_temp = True and expert_answer.has_key(tk) if verbose: if match_temp: print "%s's answer for %s (%i sources) matches that of the experts." % ( user_name, zid, nt) else: print "%s's answer for %s (%i sources) does not match that of the experts." % ( user_name, zid, nt) match_expert = match_temp else: if verbose: print "%s's number of sources for %s does not match (%i vs %i)" % ( user_name, zid, nt, nx) match_expert = False return match_expert
def mps_bending_angle(consensus_level = 0.50): # Compute the bending and position angles for double-peaked, single-contour sources with optical counterparts tstart = time.time() # Load data df = pd.read_csv('%s/bending_angles/multipeaked_singles_cc.csv' % rgz_dir,delimiter=',') df2 = df[(df['ntotal'] == 2)] with open('%s/bending_angles/angles_multipeaked_singles.csv' % rgz_dir,'w') as f: print >> f,'zooniverse_id,bending_angle,position_angle' # get optical counterpart for zooniverse_id (loop over eventually) df2_pair1 = df2[::2] df2_pair2 = df2[1::2] _zid = np.array(df2_pair1['zooniverse_id']) _x1 = np.array(df2_pair1['xc']) _y1 = np.array(df2_pair1['yc']) _x2 = np.array(df2_pair2['xc']) _y2 = np.array(df2_pair2['yc']) for zooniverse_id,x1,y1,x2,y2 in zip(_zid,_x1,_y1,_x2,_y2): c = consensus.checksum(zooniverse_id) try: if (len(c['answer']) == 1) and (c['n_users']/float(c['n_total']) >= consensus_level): if c['answer'][c['answer'].keys()[0]].has_key('ir_peak'): peak_x,peak_y = c['answer'][c['answer'].keys()[0]]['ir_peak'] ir_x = peak_x * first_ir_scale_x ir_y = peak_y * first_ir_scale_y alpha = bending_angle(ir_x,ir_y,x1,y1,x2,y2) alpha_deg = alpha * 180./np.pi phi = position_angle(ir_x,ir_y,x1,y1,x2,y2) phi_deg = phi * 180./np.pi print >> f,'{0:s},{1:.4f},{2:.4f}'.format(zooniverse_id,alpha_deg,phi_deg) else: print "Had more than 1 IR sources and/or less than {0:.2f} percent consensus for {1:s}".format(consensus_level,zooniverse_id) except TypeError: print "No 'answer' key for %s" % zooniverse_id # Timing the process tend = time.time() n = len(df2)/2 print '%.2f minutes for %i subjects' % ((tend - tstart)/60.,n) print '%.2f subjects per second' % (n/(tend - tstart)) return None
def plot_gs_volunteers(): # Plot the results of the gold standard classifications by the users, removing the expert classifications gs_gals = get_galaxies() for gal in gs_gals: print gal c0 = consensus.checksum(gal,experts_only=False,excluded=experts) consensus.plot_consensus(c0,savefig = True) return None
def plot_gs_volunteers(): # Plot the results of the gold standard classifications by the users, removing the expert classifications gs_gals = get_galaxies() for gal in gs_gals: print gal c0 = consensus.checksum(gal, experts_only=False, excluded=experts) consensus.plot_consensus(c0, savefig=True) return None
def compare(zid, user_name, verbose=True, exclude_user=False): # Compare the classification of a single galaxy by a single user to that of the expert science team their_answer = consensus.one_answer(zid, user_name) if exclude_user: expert_answer = consensus.checksum(zid, experts_only=True, excluded=user_name) else: expert_answer = consensus.checksum(zid, experts_only=True) # Total number of galaxies identified in each answer nt = len(their_answer['answer']) nx = len(expert_answer['answer']) suff_x = '' if nx == 1 else 's' suff_t = '' if nt == 1 else 's' if nt == nx: match_temp = True for tk in their_answer['answer'].iterkeys(): match_temp = True and expert_answer['answer'].has_key(tk) if verbose: if match_temp: print "Yes - %s's answer for %s (%i source%s) matches that of the experts." % ( user_name, zid, nt, suff_t) else: print "No - %s's answer for %s has the same number of source%s (%i) as the experts, but not the same arrangement." % ( user_name, zid, suff_t, nt) match_expert = match_temp else: if verbose: print "No - %s had %i source%s in %s, compared to %i source%s by the experts" % ( user_name, nt, suff_t, zid, nx, suff_x) match_expert = False return match_expert
import consensus zids = ("hymor01_ARG0003o4o", "hymor01_ARG0003o4w", "hymor01_ARG0003o4t", "hymor02_ARG0003hkm", "hymor02_ARG0003hlo", "hymor02_ARG0003hk3", "hymor03_ARG0000gng", "hymor04_ARG0003czn", "hymor04_ARG0003cyx", "hymor04_ARG0003cyr", "hymor04_ARG0003cyc", "hymor05_ARG0001arg", "hymor05_ARG0001ark", "hymor05_ARG0001arv", "hymor05_ARG0001aqn", "hymor06_ARG00021rf", "hymor07_ARG00024bn", "hymor07_ARG00024d2", "hymor07_ARG0002495", "hymor07_ARG000248y", "hymor08_ARG0003j0n", "hymor08_ARG0003j0a", "hymor08_ARG0003j0z", "hymor08_ARG0003j1d", "hymor09_ARG00027o7", "hymor09_ARG00027p0", "hymor09_ARG00027mr", "hymor10_ARG0003ph9", "hymor10_ARG0003pgv", "hymor10_ARG0003pgw", "hymor10_ARG0003pht", "hymor11_ARG0002p17", "hymor11_ARG0002p1h", "hymor11_ARG0002p1x", "hymor11_ARG0002ozg", "hymor11_ARG0002oza" "hymor12_ARG00007vb", "hymor12_ARG00007us", "hymor12_ARG00007uw", "hymor12_ARG00007up", "hymor12_ARG00007vp", "hymor13_ARG0001hz4", "hymor13_ARG0001hyx", "hymor13_ARG0001hzl", "hymor13_ARG0001hys") for zid in zids: c = consensus.checksum(zid.split('_')[1]) if c != None: consensus.plot_consensus(c, savefig=zid) else: print "Couldn't do %s" % zid
import consensus zids=("hymor01_ARG0003o4o", "hymor01_ARG0003o4w", "hymor01_ARG0003o4t", "hymor02_ARG0003hkm", "hymor02_ARG0003hlo", "hymor02_ARG0003hk3", "hymor03_ARG0000gng", "hymor04_ARG0003czn", "hymor04_ARG0003cyx", "hymor04_ARG0003cyr", "hymor04_ARG0003cyc", "hymor05_ARG0001arg", "hymor05_ARG0001ark", "hymor05_ARG0001arv", "hymor05_ARG0001aqn", "hymor06_ARG00021rf", "hymor07_ARG00024bn", "hymor07_ARG00024d2", "hymor07_ARG0002495", "hymor07_ARG000248y", "hymor08_ARG0003j0n", "hymor08_ARG0003j0a", "hymor08_ARG0003j0z", "hymor08_ARG0003j1d", "hymor09_ARG00027o7", "hymor09_ARG00027p0", "hymor09_ARG00027mr", "hymor10_ARG0003ph9", "hymor10_ARG0003pgv", "hymor10_ARG0003pgw", "hymor10_ARG0003pht", "hymor11_ARG0002p17", "hymor11_ARG0002p1h", "hymor11_ARG0002p1x", "hymor11_ARG0002ozg", "hymor11_ARG0002oza" "hymor12_ARG00007vb", "hymor12_ARG00007us", "hymor12_ARG00007uw", "hymor12_ARG00007up", "hymor12_ARG00007vp", "hymor13_ARG0001hz4", "hymor13_ARG0001hyx", "hymor13_ARG0001hzl", "hymor13_ARG0001hys") for zid in zids: c = consensus.checksum(zid.split('_')[1]) if c != None: consensus.plot_consensus(c,savefig=zid) else: print "Couldn't do %s" % zid
def plot_one_triple(zooniverse_id,pathdict,figno=1,savefig=False,anglepath=''): # Make a four-panel plot of the consensus identification with marked bending angle and position angle for a triple source cons = consensus.checksum(zooniverse_id) subject = subjects.find_one({'zooniverse_id':zooniverse_id}) contours = get_contours(subject,pathdict) radio_components = contours['contours'] # Plot image answer = cons['answer'] # Download contour data sf_x = 500./contours['width'] sf_y = 500./contours['height'] verts_all = [] codes_all = [] components = contours['contours'] for comp in components: # Order of bounding box components is (xmax,ymax,xmin,ymin) comp_xmax,comp_ymax,comp_xmin,comp_ymin = comp[0]['bbox'] # Only plot radio components identified by the users as the consensus; # check on the xmax value to make sure for v in answer.itervalues(): if comp_xmax in v['xmax']: for idx,level in enumerate(comp): verts = [((p['x'])*sf_x,(p['y']-1)*sf_y) for p in level['arr']] codes = np.ones(len(verts),int) * Path.LINETO codes[0] = Path.MOVETO verts_all.extend(verts) codes_all.extend(codes) try: path = Path(verts_all, codes_all) patch_black = patches.PathPatch(path, facecolor = 'none', edgecolor='black', lw=1) except AssertionError: print 'Users found no components for consensus match of %s' % zooniverse_id # Plot the infrared results fig = plt.figure(figno,(15,4)) fig.clf() ax3 = fig.add_subplot(143) ax4 = fig.add_subplot(144) colormaparr = [cm.hot_r,cm.Blues,cm.RdPu,cm.Greens,cm.PuBu,cm.YlGn,cm.Greys][::-1] colorarr = ['r','b','m','g','c','y','k'][::-1] if len(answer) > 0: # At least one galaxy was identified for idx,ans in enumerate(answer.itervalues()): if ans.has_key('peak_data'): # Plot the KDE map colormap = colormaparr.pop() ax3.imshow(np.rot90(ans['peak_data']['Z']), cmap=colormap,extent=[xmin, xmax, ymin, ymax]) # Plot individual sources color = colorarr.pop() x_plot,y_plot = ans['ir_x'],ans['ir_y'] ax3.scatter(x_plot, y_plot, c=color, marker='o', s=10, alpha=1./len(x_plot)) ax4.plot([ans['ir_peak'][0]],[ans['ir_peak'][1]],color=color,marker='*',markersize=12) elif ans.has_key('ir'): color = colorarr.pop() x_plot,y_plot = ans['ir'] ax3.plot([x_plot],[y_plot],color=color,marker='o',markersize=2) ax4.plot([x_plot],[y_plot],color=color,marker='*',markersize=12) else: ax4.text(550,idx*25,'#%i - no IR host' % idx,fontsize=11) ax3.set_xlim([0, 500]) ax3.set_ylim([500, 0]) ax3.set_title(zooniverse_id) ax3.set_aspect('equal') ax4.set_xlim([0, 500]) ax4.set_ylim([500, 0]) ax4.set_title('Consensus (%i/%i users)' % (cons['n_users'],cons['n_total'])) ax4.set_aspect('equal') # Display IR and radio images url_standard = subject['location']['standard'] im_standard = Image.open(cStringIO.StringIO(urllib.urlopen(url_standard).read())) ax1 = fig.add_subplot(141) ax1.imshow(im_standard,origin='upper') ax1.set_title('WISE') url_radio = subject['location']['radio'] im_radio = Image.open(cStringIO.StringIO(urllib.urlopen(url_radio).read())) ax2 = fig.add_subplot(142) ax2.imshow(im_radio,origin='upper') ax2.set_title(subject['metadata']['source']) ax2.get_yaxis().set_ticklabels([]) ax3.get_yaxis().set_ticklabels([]) # Plot contours identified as the consensus if len(answer) > 0: ax4.add_patch(patch_black) # Add centers of bounding boxes for comp in components: bbox_radio = comp[0]['bbox'] bbox_ir = bbox_radio_to_ir(bbox_radio) xrad = np.median((bbox_ir[0],bbox_ir[2])) yrad = np.median((bbox_ir[1],bbox_ir[3])) ax4.scatter(xrad,yrad, c='g', marker='s', s=15, alpha=1) dbx = [bbox_ir[i] for i in (2,2,0,0,2)] dby = [bbox_ir[i] for i in (3,1,1,3,3)] ax4.plot(dbx,dby,color='g') radiobeamsize = 5. # arcsec imagesize = 3. # arcmin imagescale = IMG_HEIGHT_NEW/imagesize / 60. # pixel / arcsec radio_tol = radiobeamsize * imagescale for ans in answer: if answer[ans].has_key('ir_peak'): # Optical counterpart position xc,yc = answer[ans]['ir_peak'] # Measure all positions in radio pixel coordinates radio_centroids = pix_radio(components) maxdist = 0 for centroid in radio_centroids: d = pix_dist(xc,yc,centroid[0],centroid[1]) maxdist = d if d > maxdist else maxdist if d <= radio_tol: middle_radio = centroid radio_centroids.remove(middle_radio) x1 = radio_centroids[0][0] y1 = radio_centroids[0][1] x2 = radio_centroids[1][0] y2 = radio_centroids[1][1] if len(radio_centroids) == 2: m1 = (y1 - yc) / (x1 - xc) b1 = yc - m1*xc m2 = (y2 - yc) / (x2 - xc) b2 = yc - m2*xc xedge1 = 0 if x1 < xc else 500 yedge1 = y1 - (x1-xedge1)*(yc-y1)/(xc-x1) xedge2 = 0 if x2 < xc else 500 yedge2 = y2 - (x2-xedge2)*(yc-y2)/(xc-x2) # Draw and annotate the the bending angle ax4.plot([xedge1,xc],[yedge1,yc],color='orange',linestyle='--') ax4.plot([xedge2,xc],[yedge2,yc],color='orange',linestyle='--') alpha_deg = bending_angle(xc,yc,x1,y1,x2,y2) * 180/np.pi ax4.text(550,0,r'$\alpha$ = %.1f deg' % alpha_deg,fontsize=11) else: print "\tDidn't find match to optical ID for triple radio source %s" % zooniverse_id else: print "\tNo IR peak for %s" % zooniverse_id ax4.yaxis.tick_right() ax1.get_xaxis().set_ticks([0,100,200,300,400]) ax2.get_xaxis().set_ticks([0,100,200,300,400]) ax3.get_xaxis().set_ticks([0,100,200,300,400]) ax4.get_xaxis().set_ticks([0,100,200,300,400,500]) plt.subplots_adjust(wspace=0.02) # Save hard copy of the figure if savefig: fig.savefig('{0:}/bending_angles/plots/individual/triples/{1:}ba_{2:}.pdf'.format(rgz_dir,anglepath,zooniverse_id)) plt.close() else: plt.show() # Close figure after it's done; otherwise mpl complains about having thousands of stuff open return None
def plot_one_double(zooniverse_id,pathdict,figno=1,savefig=False,anglepath='',dbltype='radio'): # Make a four-panel plot of the consensus identification with marked bending angle and position angle for a double source cons = consensus.checksum(zooniverse_id) subject = subjects.find_one({'zooniverse_id':zooniverse_id}) contours = get_contours(subject,pathdict) radio_components = contours['contours'] # Plot image answer = cons['answer'] # Download contour data sf_x = 500./contours['width'] sf_y = 500./contours['height'] verts_all = [] codes_all = [] components = contours['contours'] for comp in components: # Order of bounding box components is (xmax,ymax,xmin,ymin) comp_xmax,comp_ymax,comp_xmin,comp_ymin = comp[0]['bbox'] # Only plot radio components identified by the users as the consensus; # check on the xmax value to make sure for v in answer.itervalues(): if comp_xmax in v['xmax']: for idx,level in enumerate(comp): verts = [((p['x'])*sf_x,(p['y']-1)*sf_y) for p in level['arr']] codes = np.ones(len(verts),int) * Path.LINETO codes[0] = Path.MOVETO verts_all.extend(verts) codes_all.extend(codes) try: path = Path(verts_all, codes_all) patch_black = patches.PathPatch(path, facecolor = 'none', edgecolor='black', lw=1) except AssertionError: print 'Users found no components for consensus match of %s' % zooniverse_id # Plot the infrared results fig = plt.figure(figno,(15,4)) fig.clf() ax3 = fig.add_subplot(143) ax4 = fig.add_subplot(144) colormaparr = [cm.hot_r,cm.Blues,cm.RdPu,cm.Greens,cm.PuBu,cm.YlGn,cm.Greys][::-1] colorarr = ['r','b','m','g','c','y','k'][::-1] if len(answer) > 0: # At least one galaxy was identified for idx,ans in enumerate(answer.itervalues()): if ans.has_key('peak_data'): # Plot the KDE map colormap = colormaparr.pop() ax3.imshow(np.rot90(ans['peak_data']['Z']), cmap=colormap,extent=[xmin, xmax, ymin, ymax]) # Plot individual sources color = colorarr.pop() ''' x_plot = [xt * 500./424 for xt in ans['ir_x'] if xt != -99.0] y_plot = [yt * 500./424 for yt in ans['ir_y'] if yt != -99.0] ''' x_plot,y_plot = ans['ir_x'],ans['ir_y'] ax3.scatter(x_plot, y_plot, c=color, marker='o', s=10, alpha=1./len(x_plot)) ax4.plot([ans['ir_peak'][0]],[ans['ir_peak'][1]],color=color,marker='*',markersize=12) elif ans.has_key('ir'): color = colorarr.pop() x_plot,y_plot = ans['ir'] ax3.plot([x_plot],[y_plot],color=color,marker='o',markersize=2) ax4.plot([x_plot],[y_plot],color=color,marker='*',markersize=12) else: ax4.text(550,idx*25,'#%i - no IR host' % idx,fontsize=11) ax3.set_xlim([0, 500]) ax3.set_ylim([500, 0]) ax3.set_title(zooniverse_id) ax3.set_aspect('equal') ax4.set_xlim([0, 500]) ax4.set_ylim([500, 0]) ax4.set_title('Consensus (%i/%i users)' % (cons['n_users'],cons['n_total'])) ax4.set_aspect('equal') # Display IR and radio images url_standard = subject['location']['standard'] im_standard = Image.open(cStringIO.StringIO(urllib.urlopen(url_standard).read())) ax1 = fig.add_subplot(141) ax1.imshow(im_standard,origin='upper') ax1.set_title('WISE') url_radio = subject['location']['radio'] im_radio = Image.open(cStringIO.StringIO(urllib.urlopen(url_radio).read())) ax2 = fig.add_subplot(142) ax2.imshow(im_radio,origin='upper') ax2.set_title(subject['metadata']['source']) ax2.get_yaxis().set_ticklabels([]) ax3.get_yaxis().set_ticklabels([]) # Plot contours identified as the consensus if len(answer) > 0: ax4.add_patch(patch_black) radio_centers = [] for component in components: bbox = component[0]['bbox'] # Draw centers of bounding boxes xradiocen = np.median((bbox[0],bbox[2])) / first_ir_scale_x yradiocen = np.median((bbox[1],bbox[3])) / first_ir_scale_y radio_centers.append((xradiocen,yradiocen)) ax4.scatter(xradiocen,yradiocen, c='g', marker='s', s=15, alpha=1) # Draw edge of bounding box xradiomin = bbox[2] / first_ir_scale_x xradiomax = bbox[0] / first_ir_scale_x yradiomin = bbox[3] / first_ir_scale_y yradiomax = bbox[1] / first_ir_scale_y ax4.plot([xradiomin,xradiomin,xradiomax,xradiomax,xradiomin],[yradiomin,yradiomax,yradiomax,yradiomin,yradiomin],color='g') for ans in answer: if answer[ans].has_key('ir_peak'): # Optical counterpart position xc,yc = answer[ans]['ir_peak'] # Position of radio sources for multi-peaked, single-component subjects if dbltype == "mps": local_maxima = mps_cc(subject,pathdict,plot=False,verbose=False) suffix = '' if len(local_maxima) == 1 else 's' assert len(local_maxima) >= 2, \ "%i peak%s in first radio component of %s; must have exactly 2 peaks to plot bending angle using mps method." % (len(local_maxima),suffix,zooniverse_id) x1 = local_maxima[0][1][0] / first_ir_scale_x y1 = local_maxima[0][1][1] / first_ir_scale_y x2 = local_maxima[1][1][0] / first_ir_scale_x y2 = local_maxima[1][1][1] / first_ir_scale_y ax4.scatter(x1,y1, color='darkorange', marker='s', s=15, alpha=1) ax4.scatter(x2,y2, color='darkorange', marker='s', s=15, alpha=1) # Position of radio sources for double-lobed, two-component subjects elif len(radio_centers) == 2: x1,y1 = radio_centers[0] x2,y2 = radio_centers[1] else: raise ValueError("Centers of radio boxes not defined.") m1 = (y1 - yc) / (x1 - xc) b1 = yc - m1*xc m2 = (y2 - yc) / (x2 - xc) b2 = yc - m2*xc xedge1 = 0 if x1 < xc else 500 yedge1 = y1 - (x1-xedge1)*(yc-y1)/(xc-x1) xedge2 = 0 if x2 < xc else 500 yedge2 = y2 - (x2-xedge2)*(yc-y2)/(xc-x2) # Draw and annotate the the bending angle ax4.plot([xedge1,xc],[yedge1,yc],color='orange',linestyle='--') ax4.plot([xedge2,xc],[yedge2,yc],color='orange',linestyle='--') alpha_deg = bending_angle(xc,yc,x1,y1,x2,y2) * 180/np.pi ax4.text(550,0,r'$\alpha$ = %.1f deg' % alpha_deg,fontsize=11) # Draw vector pointing north # Draw the bisector vector ''' yd = y_bisect(xc,yc,xedge1,yedge1,xedge2,yedge2) ax4.arrow(xc,yc,-xc,yd-yc,head_width=20, head_length=40, fc='blue', ec='blue') ''' # Compute the position angle with respect to north phi_deg = position_angle(xc,500-yc,x1,500-y1,x2,500-y2) * 180/np.pi ax4.text(550,50,r'$\phi$ = %.1f deg' % phi_deg,fontsize=11) ax4.arrow(xc,yc,0,-yc,head_width=20, head_length=40, fc='grey', ec='grey',ls='dotted') else: print "No peak for %s" % zooniverse_id ax4.yaxis.tick_right() ax1.get_xaxis().set_ticks([0,100,200,300,400]) ax2.get_xaxis().set_ticks([0,100,200,300,400]) ax3.get_xaxis().set_ticks([0,100,200,300,400]) ax4.get_xaxis().set_ticks([0,100,200,300,400,500]) plt.subplots_adjust(wspace=0.02) # Save hard copy of the figure if savefig: fig.savefig('%s/bending_angles/plots/individual/%sba_%s.pdf' % (rgz_dir,anglepath,zooniverse_id)) plt.close() else: plt.show() # Close figure after it's done; otherwise mpl complains about having thousands of stuff open return None
import consensus # FIRST first = consensus.checksum('ARG00029ls') consensus.plot_consensus(first) # ATLAS atlas = consensus.checksum('ARG0003r18') consensus.plot_consensus(atlas)
def plot_one_triple(zooniverse_id, pathdict, figno=1, savefig=False, anglepath=''): # Make a four-panel plot of the consensus identification with marked bending angle and position angle for a triple source cons = consensus.checksum(zooniverse_id) subject = subjects.find_one({'zooniverse_id': zooniverse_id}) contours = get_contours(subject, pathdict) radio_components = contours['contours'] # Plot image answer = cons['answer'] # Download contour data sf_x = 500. / contours['width'] sf_y = 500. / contours['height'] verts_all = [] codes_all = [] components = contours['contours'] for comp in components: # Order of bounding box components is (xmax,ymax,xmin,ymin) comp_xmax, comp_ymax, comp_xmin, comp_ymin = comp[0]['bbox'] # Only plot radio components identified by the users as the consensus; # check on the xmax value to make sure for v in answer.itervalues(): if comp_xmax in v['xmax']: for idx, level in enumerate(comp): verts = [((p['x']) * sf_x, (p['y'] - 1) * sf_y) for p in level['arr']] codes = np.ones(len(verts), int) * Path.LINETO codes[0] = Path.MOVETO verts_all.extend(verts) codes_all.extend(codes) try: path = Path(verts_all, codes_all) patch_black = patches.PathPatch(path, facecolor='none', edgecolor='black', lw=1) except AssertionError: print 'Users found no components for consensus match of %s' % zooniverse_id # Plot the infrared results fig = plt.figure(figno, (15, 4)) fig.clf() ax3 = fig.add_subplot(143) ax4 = fig.add_subplot(144) colormaparr = [ cm.hot_r, cm.Blues, cm.RdPu, cm.Greens, cm.PuBu, cm.YlGn, cm.Greys ][::-1] colorarr = ['r', 'b', 'm', 'g', 'c', 'y', 'k'][::-1] if len(answer) > 0: # At least one galaxy was identified for idx, ans in enumerate(answer.itervalues()): if ans.has_key('peak_data'): # Plot the KDE map colormap = colormaparr.pop() ax3.imshow(np.rot90(ans['peak_data']['Z']), cmap=colormap, extent=[xmin, xmax, ymin, ymax]) # Plot individual sources color = colorarr.pop() x_plot, y_plot = ans['ir_x'], ans['ir_y'] ax3.scatter(x_plot, y_plot, c=color, marker='o', s=10, alpha=1. / len(x_plot)) ax4.plot([ans['ir_peak'][0]], [ans['ir_peak'][1]], color=color, marker='*', markersize=12) elif ans.has_key('ir'): color = colorarr.pop() x_plot, y_plot = ans['ir'] ax3.plot([x_plot], [y_plot], color=color, marker='o', markersize=2) ax4.plot([x_plot], [y_plot], color=color, marker='*', markersize=12) else: ax4.text(550, idx * 25, '#%i - no IR host' % idx, fontsize=11) ax3.set_xlim([0, 500]) ax3.set_ylim([500, 0]) ax3.set_title(zooniverse_id) ax3.set_aspect('equal') ax4.set_xlim([0, 500]) ax4.set_ylim([500, 0]) ax4.set_title('Consensus (%i/%i users)' % (cons['n_users'], cons['n_total'])) ax4.set_aspect('equal') # Display IR and radio images url_standard = subject['location']['standard'] im_standard = Image.open( cStringIO.StringIO(urllib.urlopen(url_standard).read())) ax1 = fig.add_subplot(141) ax1.imshow(im_standard, origin='upper') ax1.set_title('WISE') url_radio = subject['location']['radio'] im_radio = Image.open(cStringIO.StringIO(urllib.urlopen(url_radio).read())) ax2 = fig.add_subplot(142) ax2.imshow(im_radio, origin='upper') ax2.set_title(subject['metadata']['source']) ax2.get_yaxis().set_ticklabels([]) ax3.get_yaxis().set_ticklabels([]) # Plot contours identified as the consensus if len(answer) > 0: ax4.add_patch(patch_black) # Add centers of bounding boxes for comp in components: bbox_radio = comp[0]['bbox'] bbox_ir = bbox_radio_to_ir(bbox_radio) xrad = np.median((bbox_ir[0], bbox_ir[2])) yrad = np.median((bbox_ir[1], bbox_ir[3])) ax4.scatter(xrad, yrad, c='g', marker='s', s=15, alpha=1) dbx = [bbox_ir[i] for i in (2, 2, 0, 0, 2)] dby = [bbox_ir[i] for i in (3, 1, 1, 3, 3)] ax4.plot(dbx, dby, color='g') radiobeamsize = 5. # arcsec imagesize = 3. # arcmin imagescale = IMG_HEIGHT_NEW / imagesize / 60. # pixel / arcsec radio_tol = radiobeamsize * imagescale for ans in answer: if answer[ans].has_key('ir_peak'): # Optical counterpart position xc, yc = answer[ans]['ir_peak'] # Measure all positions in radio pixel coordinates radio_centroids = pix_radio(components) maxdist = 0 for centroid in radio_centroids: d = pix_dist(xc, yc, centroid[0], centroid[1]) maxdist = d if d > maxdist else maxdist if d <= radio_tol: middle_radio = centroid radio_centroids.remove(middle_radio) x1 = radio_centroids[0][0] y1 = radio_centroids[0][1] x2 = radio_centroids[1][0] y2 = radio_centroids[1][1] if len(radio_centroids) == 2: m1 = (y1 - yc) / (x1 - xc) b1 = yc - m1 * xc m2 = (y2 - yc) / (x2 - xc) b2 = yc - m2 * xc xedge1 = 0 if x1 < xc else 500 yedge1 = y1 - (x1 - xedge1) * (yc - y1) / (xc - x1) xedge2 = 0 if x2 < xc else 500 yedge2 = y2 - (x2 - xedge2) * (yc - y2) / (xc - x2) # Draw and annotate the the bending angle ax4.plot([xedge1, xc], [yedge1, yc], color='orange', linestyle='--') ax4.plot([xedge2, xc], [yedge2, yc], color='orange', linestyle='--') alpha_deg = bending_angle(xc, yc, x1, y1, x2, y2) * 180 / np.pi ax4.text(550, 0, r'$\alpha$ = %.1f deg' % alpha_deg, fontsize=11) else: print "\tDidn't find match to optical ID for triple radio source %s" % zooniverse_id else: print "\tNo IR peak for %s" % zooniverse_id ax4.yaxis.tick_right() ax1.get_xaxis().set_ticks([0, 100, 200, 300, 400]) ax2.get_xaxis().set_ticks([0, 100, 200, 300, 400]) ax3.get_xaxis().set_ticks([0, 100, 200, 300, 400]) ax4.get_xaxis().set_ticks([0, 100, 200, 300, 400, 500]) plt.subplots_adjust(wspace=0.02) # Save hard copy of the figure if savefig: fig.savefig( '{0:}/bending_angles/plots/individual/triples/{1:}ba_{2:}.pdf'. format(rgz_dir, anglepath, zooniverse_id)) plt.close() else: plt.show() # Close figure after it's done; otherwise mpl complains about having thousands of stuff open return None
def plot_one_double(zooniverse_id, pathdict, figno=1, savefig=False, anglepath='', dbltype='radio'): # Make a four-panel plot of the consensus identification with marked bending angle and position angle for a double source cons = consensus.checksum(zooniverse_id) subject = subjects.find_one({'zooniverse_id': zooniverse_id}) contours = get_contours(subject, pathdict) radio_components = contours['contours'] # Plot image answer = cons['answer'] # Download contour data sf_x = 500. / contours['width'] sf_y = 500. / contours['height'] verts_all = [] codes_all = [] components = contours['contours'] for comp in components: # Order of bounding box components is (xmax,ymax,xmin,ymin) comp_xmax, comp_ymax, comp_xmin, comp_ymin = comp[0]['bbox'] # Only plot radio components identified by the users as the consensus; # check on the xmax value to make sure for v in answer.itervalues(): if comp_xmax in v['xmax']: for idx, level in enumerate(comp): verts = [((p['x']) * sf_x, (p['y'] - 1) * sf_y) for p in level['arr']] codes = np.ones(len(verts), int) * Path.LINETO codes[0] = Path.MOVETO verts_all.extend(verts) codes_all.extend(codes) try: path = Path(verts_all, codes_all) patch_black = patches.PathPatch(path, facecolor='none', edgecolor='black', lw=1) except AssertionError: print 'Users found no components for consensus match of %s' % zooniverse_id # Plot the infrared results fig = plt.figure(figno, (15, 4)) fig.clf() ax3 = fig.add_subplot(143) ax4 = fig.add_subplot(144) colormaparr = [ cm.hot_r, cm.Blues, cm.RdPu, cm.Greens, cm.PuBu, cm.YlGn, cm.Greys ][::-1] colorarr = ['r', 'b', 'm', 'g', 'c', 'y', 'k'][::-1] if len(answer) > 0: # At least one galaxy was identified for idx, ans in enumerate(answer.itervalues()): if ans.has_key('peak_data'): # Plot the KDE map colormap = colormaparr.pop() ax3.imshow(np.rot90(ans['peak_data']['Z']), cmap=colormap, extent=[xmin, xmax, ymin, ymax]) # Plot individual sources color = colorarr.pop() ''' x_plot = [xt * 500./424 for xt in ans['ir_x'] if xt != -99.0] y_plot = [yt * 500./424 for yt in ans['ir_y'] if yt != -99.0] ''' x_plot, y_plot = ans['ir_x'], ans['ir_y'] ax3.scatter(x_plot, y_plot, c=color, marker='o', s=10, alpha=1. / len(x_plot)) ax4.plot([ans['ir_peak'][0]], [ans['ir_peak'][1]], color=color, marker='*', markersize=12) elif ans.has_key('ir'): color = colorarr.pop() x_plot, y_plot = ans['ir'] ax3.plot([x_plot], [y_plot], color=color, marker='o', markersize=2) ax4.plot([x_plot], [y_plot], color=color, marker='*', markersize=12) else: ax4.text(550, idx * 25, '#%i - no IR host' % idx, fontsize=11) ax3.set_xlim([0, 500]) ax3.set_ylim([500, 0]) ax3.set_title(zooniverse_id) ax3.set_aspect('equal') ax4.set_xlim([0, 500]) ax4.set_ylim([500, 0]) ax4.set_title('Consensus (%i/%i users)' % (cons['n_users'], cons['n_total'])) ax4.set_aspect('equal') # Display IR and radio images url_standard = subject['location']['standard'] im_standard = Image.open( cStringIO.StringIO(urllib.urlopen(url_standard).read())) ax1 = fig.add_subplot(141) ax1.imshow(im_standard, origin='upper') ax1.set_title('WISE') url_radio = subject['location']['radio'] im_radio = Image.open(cStringIO.StringIO(urllib.urlopen(url_radio).read())) ax2 = fig.add_subplot(142) ax2.imshow(im_radio, origin='upper') ax2.set_title(subject['metadata']['source']) ax2.get_yaxis().set_ticklabels([]) ax3.get_yaxis().set_ticklabels([]) # Plot contours identified as the consensus if len(answer) > 0: ax4.add_patch(patch_black) radio_centers = [] for component in components: bbox = component[0]['bbox'] # Draw centers of bounding boxes xradiocen = np.median((bbox[0], bbox[2])) / first_ir_scale_x yradiocen = np.median((bbox[1], bbox[3])) / first_ir_scale_y radio_centers.append((xradiocen, yradiocen)) ax4.scatter(xradiocen, yradiocen, c='g', marker='s', s=15, alpha=1) # Draw edge of bounding box xradiomin = bbox[2] / first_ir_scale_x xradiomax = bbox[0] / first_ir_scale_x yradiomin = bbox[3] / first_ir_scale_y yradiomax = bbox[1] / first_ir_scale_y ax4.plot([xradiomin, xradiomin, xradiomax, xradiomax, xradiomin], [yradiomin, yradiomax, yradiomax, yradiomin, yradiomin], color='g') for ans in answer: if answer[ans].has_key('ir_peak'): # Optical counterpart position xc, yc = answer[ans]['ir_peak'] # Position of radio sources for multi-peaked, single-component subjects if dbltype == "mps": local_maxima = mps_cc(subject, pathdict, plot=False, verbose=False) suffix = '' if len(local_maxima) == 1 else 's' assert len(local_maxima) >= 2, \ "%i peak%s in first radio component of %s; must have exactly 2 peaks to plot bending angle using mps method." % (len(local_maxima),suffix,zooniverse_id) x1 = local_maxima[0][1][0] / first_ir_scale_x y1 = local_maxima[0][1][1] / first_ir_scale_y x2 = local_maxima[1][1][0] / first_ir_scale_x y2 = local_maxima[1][1][1] / first_ir_scale_y ax4.scatter(x1, y1, color='darkorange', marker='s', s=15, alpha=1) ax4.scatter(x2, y2, color='darkorange', marker='s', s=15, alpha=1) # Position of radio sources for double-lobed, two-component subjects elif len(radio_centers) == 2: x1, y1 = radio_centers[0] x2, y2 = radio_centers[1] else: raise ValueError("Centers of radio boxes not defined.") m1 = (y1 - yc) / (x1 - xc) b1 = yc - m1 * xc m2 = (y2 - yc) / (x2 - xc) b2 = yc - m2 * xc xedge1 = 0 if x1 < xc else 500 yedge1 = y1 - (x1 - xedge1) * (yc - y1) / (xc - x1) xedge2 = 0 if x2 < xc else 500 yedge2 = y2 - (x2 - xedge2) * (yc - y2) / (xc - x2) # Draw and annotate the the bending angle ax4.plot([xedge1, xc], [yedge1, yc], color='orange', linestyle='--') ax4.plot([xedge2, xc], [yedge2, yc], color='orange', linestyle='--') alpha_deg = bending_angle(xc, yc, x1, y1, x2, y2) * 180 / np.pi ax4.text(550, 0, r'$\alpha$ = %.1f deg' % alpha_deg, fontsize=11) # Draw vector pointing north # Draw the bisector vector ''' yd = y_bisect(xc,yc,xedge1,yedge1,xedge2,yedge2) ax4.arrow(xc,yc,-xc,yd-yc,head_width=20, head_length=40, fc='blue', ec='blue') ''' # Compute the position angle with respect to north phi_deg = position_angle(xc, 500 - yc, x1, 500 - y1, x2, 500 - y2) * 180 / np.pi ax4.text(550, 50, r'$\phi$ = %.1f deg' % phi_deg, fontsize=11) ax4.arrow(xc, yc, 0, -yc, head_width=20, head_length=40, fc='grey', ec='grey', ls='dotted') else: print "No peak for %s" % zooniverse_id ax4.yaxis.tick_right() ax1.get_xaxis().set_ticks([0, 100, 200, 300, 400]) ax2.get_xaxis().set_ticks([0, 100, 200, 300, 400]) ax3.get_xaxis().set_ticks([0, 100, 200, 300, 400]) ax4.get_xaxis().set_ticks([0, 100, 200, 300, 400, 500]) plt.subplots_adjust(wspace=0.02) # Save hard copy of the figure if savefig: fig.savefig('%s/bending_angles/plots/individual/%sba_%s.pdf' % (rgz_dir, anglepath, zooniverse_id)) plt.close() else: plt.show() # Close figure after it's done; otherwise mpl complains about having thousands of stuff open return None
def mps_bending_angle(consensus_level=0.50): # Compute the bending and position angles for double-peaked, single-contour sources with optical counterparts tstart = time.time() # Load data df = pd.read_csv('%s/bending_angles/multipeaked_singles_cc.csv' % rgz_dir, delimiter=',') df2 = df[(df['ntotal'] == 2)] with open('%s/bending_angles/angles_multipeaked_singles.csv' % rgz_dir, 'w') as f: print >> f, 'zooniverse_id,bending_angle,position_angle' # get optical counterpart for zooniverse_id (loop over eventually) df2_pair1 = df2[::2] df2_pair2 = df2[1::2] _zid = np.array(df2_pair1['zooniverse_id']) _x1 = np.array(df2_pair1['xc']) _y1 = np.array(df2_pair1['yc']) _x2 = np.array(df2_pair2['xc']) _y2 = np.array(df2_pair2['yc']) for zooniverse_id, x1, y1, x2, y2 in zip(_zid, _x1, _y1, _x2, _y2): c = consensus.checksum(zooniverse_id) try: if (len(c['answer']) == 1) and ( c['n_users'] / float(c['n_total']) >= consensus_level): if c['answer'][c['answer'].keys()[0]].has_key('ir_peak'): peak_x, peak_y = c['answer'][c['answer'].keys() [0]]['ir_peak'] ir_x = peak_x * first_ir_scale_x ir_y = peak_y * first_ir_scale_y alpha = bending_angle(ir_x, ir_y, x1, y1, x2, y2) alpha_deg = alpha * 180. / np.pi phi = position_angle(ir_x, ir_y, x1, y1, x2, y2) phi_deg = phi * 180. / np.pi print >> f, '{0:s},{1:.4f},{2:.4f}'.format( zooniverse_id, alpha_deg, phi_deg) else: print "Had more than 1 IR sources and/or less than {0:.2f} percent consensus for {1:s}".format( consensus_level, zooniverse_id) except TypeError: print "No 'answer' key for %s" % zooniverse_id # Timing the process tend = time.time() n = len(df2) / 2 print '%.2f minutes for %i subjects' % ((tend - tstart) / 60., n) print '%.2f subjects per second' % (n / (tend - tstart)) return None