def sample_histogram(self,caumass,truemass,bins=20,ax=None): if ax == None: mp.hist(caumass,bins=bins,color='b',alpha=.6) p1 = mp.axvline(truemass,ymin=.8,color='k',lw=1.5) p2 = mp.axvline(np.median(caumass*1),ymin=.8,color='g',lw=1.5) p3 = mp.axvline(np.mean(caumass*1),ymin=.8,color='c',lw=1.5) p4 = mp.axvline(astStats.biweightLocation(caumass*1,6.0),ymin=.8,color='r',lw=1.5) mp.legend([p1,p2,p3,p4],["Table Mass","Median","Mean","biweightLocation"]) else: ax.hist(caumass,bins=bins,color='b',alpha=.6) p1 = ax.axvline(truemass,ymin=.8,color='k',lw=1.5) p2 = ax.axvline(np.median(caumass*1),ymin=.8,color='g',lw=1.5) p3 = ax.axvline(np.mean(caumass*1),ymin=.8,color='c',lw=1.5) p4 = ax.axvline(astStats.biweightLocation(caumass*1,6.0),ymin=.8,color='r',lw=1.5) ax.legend([p1,p2,p3,p4],["Table Mass","Median","Mean","biweightLocation"],fontsize=8)
def set_sample(self,r,v,mags,rlimit,vlimit=3500,H0=72.0,gal_mem=None): ''' The gal_mem argument allows for members to be returned so you have an extra returning value''' rvalues = np.array(r)[np.where((r < rlimit) & (v < vlimit) & (v > -vlimit))] vvalues = np.array(v)[np.where((r < rlimit) & (v < vlimit) & (v > -vlimit))] magvalues = np.array(mags)[np.where((r < rlimit) & (v < vlimit) & (v > -vlimit))] try: #This fixes the 0 velocity location based on the N galaxies inside our limits vfix = astStats.biweightLocation(vvalues[np.where((rvalues<0.5) & (vvalues>-vlimit) & (vvalues<vlimit))],6.0) vvalues = vvalues - vfix except: #Exception is caught because astStats needs a certain number of galaxies to work vfix = np.average(vvalues[np.where((rvalues<0.5) & (vvalues>-3500) & (vvalues<3500))]) vvalues = vvalues - vfix rvalues1 = np.array(rvalues)[np.where((rvalues < rlimit) & (vvalues < vlimit) & (vvalues > -vlimit))] vvalues1 = np.array(vvalues)[np.where((rvalues < rlimit) & (vvalues < vlimit) & (vvalues > -vlimit))] magvalues1 = np.array(magvalues)[np.where((rvalues < rlimit) & (vvalues < vlimit) & (vvalues > -vlimit))] if gal_mem is not None: #If gal_mem is called, it will return an extra value memvalues = np.array(gal_mem)[np.where((r < rlimit) & (v < vlimit) & (v > -vlimit))] memvalues1 = np.array(memvalues)[np.where((rvalues < rlimit) & (vvalues < vlimit) & (vvalues > -vlimit))] return (rvalues1,vvalues1,magvalues1,memvalues1) else: return (rvalues1,vvalues1,magvalues1)
def ss_recover(): # Preliminary data file upload global h, gal_num, line_num, halo_num, r_limit, vlimit, beta h, gal_num, line_num, halo_num, r_limit, vlimit, beta = loadtxt( "" + root + "/nkern/Documents/MDB_milliMil_halodata/Caustic/stack_data/" + str(run_loc) + "/program_constants.tab", unpack=True, ) halo_num = int(halo_num) line_num, gal_num = int(line_num), int(gal_num) # Second preliminary data file upload global HaloID, M_crit200, R_crit200, SRAD, ESARD, HVD HaloID, M_crit200, R_crit200, SRAD, ESRAD, HVD = loadtxt( "" + root + "/nkern/Documents/MDB_milliMil_halodata/Caustic/stack_data/" + str(run_loc) + "/simdata.tab", unpack=True, ) HaloID = str(HaloID) HaloID, M_crit200, R_crit200, SRAD, ESRAD, HVD = ( HaloID[:halo_num], M_crit200[:halo_num], R_crit200[:halo_num], SRAD[:halo_num], ESRAD[:halo_num], HVD[:halo_num], ) # First Data file upload global ENC_CAUMASS, ENC_INFMASS, ENC_VDISP j = 0 for m in range(halo_num): if j == 0: # Initialization of arrays ENC_CAUMASS, ENC_INFMASS, ENC_VDISP = loadtxt( "" + root + "/nkern/Documents/MDB_milliMil_halodata/Caustic/stack_data/" + str(run_loc) + "/halo_" + str(m) + "_constants.tab", usecols=(0, 1, 2), unpack=True, ) else: ENC_CAUMASSt, ENC_INFMASSt, ENC_VDISPt = loadtxt( "" + root + "/nkern/Documents/MDB_milliMil_halodata/Caustic/stack_data/" + str(run_loc) + "/halo_" + str(m) + "_constants.tab", usecols=(0, 1, 2), unpack=True, ) ENC_CAUMASS = hstack([ENC_CAUMASS, ENC_CAUMASSt]) ENC_INFMASS = hstack([ENC_INFMASS, ENC_INFMASSt]) ENC_VDISP = hstack([ENC_VDISP, ENC_VDISPt]) j += 1 # Second data file upload global LINE_CAUMASS, LINE_INFMASS, LINE_VDISP j = 0 for m in range(halo_num): if j == 0: # Initialization of arrays LINE_CAUMASS, LINE_INFMASS, LINE_VDISP = loadtxt( "" + root + "/nkern/Documents/MDB_milliMil_halodata/Caustic/stack_data/" + str(run_loc) + "/halo_" + str(m) + "_linenum.tab", unpack=True, ) else: line_caumass, line_infmass, line_vdisp = loadtxt( "" + root + "/nkern/Documents/MDB_milliMil_halodata/Caustic/stack_data/" + str(run_loc) + "/halo_" + str(m) + "_linenum.tab", unpack=True, ) LINE_CAUMASS = vstack([LINE_CAUMASS, line_caumass]) LINE_INFMASS = vstack([LINE_INFMASS, line_infmass]) LINE_VDISP = vstack([LINE_VDISP, line_vdisp]) j += 1 # Third data file upload global ENC_CAUSURF, ENC_INFSURF, ENC_INFNFW, x_range j = 0 for m in range(halo_num): if j == 0: # Initialization of arrays ENC_CAUSURF, ENC_INFSURF, ENC_INFNFW, x_range = loadtxt( "" + root + "/nkern/Documents/MDB_milliMil_halodata/Caustic/stack_data/" + str(run_loc) + "/halo_" + str(m) + "_profiles.tab", unpack=True, ) else: enc_causurf, enc_infsurf, enc_infnfw, x_range = loadtxt( "" + root + "/nkern/Documents/MDB_milliMil_halodata/Caustic/stack_data/" + str(run_loc) + "/halo_" + str(m) + "_profiles.tab", unpack=True, ) ENC_CAUSURF = vstack([ENC_CAUSURF, enc_causurf]) ENC_INFSURF = vstack([ENC_INFSURF, enc_infsurf]) ENC_INFNFW = vstack([ENC_INFNFW, enc_infnfw]) j += 1 # Fourth data file upload global ENC_R, ENC_V, ENC_MAG, ENC_GPX3D, ENC_GPY3D, ENC_GPZ3D, ENC_GVX3D, ENC_GVY3D, ENC_GVZ3D ENC_R, ENC_V, ENC_MAG, ENC_GPX3D, ENC_GPY3D, ENC_GPZ3D, ENC_GVX3D, ENC_GVY3D, ENC_GVZ3D = ( [], [], [], [], [], [], [], [], [], ) j = 0 for m in range(halo_num): enc_r, enc_v, enc_mag, enc_gpx3d, enc_gpy3d, enc_gpz3d, enc_gvx3d, enc_gvy3d, enc_gvz3d = loadtxt( "" + root + "/nkern/Documents/MDB_milliMil_halodata/Caustic/stack_data/" + str(run_loc) + "/halo_" + str(m) + "_RVdata.tab", unpack=True, ) ENC_R.append(enc_r) ENC_V.append(enc_v) ENC_MAG.append(enc_mag) ENC_GPX3D.append(enc_gpx3d) ENC_GPY3D.append(enc_gpy3d) ENC_GPZ3D.append(enc_gpz3d) ENC_GVX3D.append(enc_gvx3d) ENC_GVY3D.append(enc_gvy3d) ENC_GVZ3D.append(enc_gvz3d) j += 1 ENC_R, ENC_V, ENC_MAG, ENC_GPX3D, ENC_GPY3D, ENC_GPZ3D, ENC_GVX3D, ENC_GVY3D, ENC_GVZ3D = ( array(ENC_R), array(ENC_V), array(ENC_MAG), array(ENC_GPX3D), array(ENC_GPY3D), array(ENC_GPZ3D), array(ENC_GVX3D), array(ENC_GVY3D), array(ENC_GVZ3D), ) # Fifth data file to upload global LINE_CAUSURF j = 0 for m in range(halo_num): if j == 0: line_prof = loadtxt( "" + root + "/nkern/Documents/MDB_milliMil_halodata/Caustic/stack_data/" + str(run_loc) + "/halo_" + str(m) + "_losprofile.tab", unpack=True, ) LINE_CAUSURF = array([line_prof[0:line_num]]) else: line_prof = loadtxt( "" + root + "/nkern/Documents/MDB_milliMil_halodata/Caustic/stack_data/" + str(run_loc) + "/halo_" + str(m) + "_losprofile.tab", unpack=True, ) line_causurf = array([line_prof[0:line_num]]) LINE_CAUSURF = vstack([LINE_CAUSURF, line_causurf]) j += 1 # Sixth data set upload (los rv data) if get_los == True: global LINE_R, LINE_V, LINE_MAG LINE_R, LINE_V, LINE_MAG = [], [], [] j = 0 for m in range(halo_num): line_r, line_v, line_mag = [], [], [] for l in range(line_num): r, v, mag = loadtxt( "" + root + "/nkern/Documents/MDB_milliMil_halodata/Caustic/stack_data/" + str(run_loc) + "/LOS_RV/halo_" + str(m) + "_los_" + str(l) + "_rv.tab", unpack=True, ) line_r.append(r) line_v.append(v) line_mag.append(mag) LINE_R.append(line_r) LINE_V.append(line_v) LINE_MAG.append(line_mag) LINE_R, LINE_V, LINE_MAG = array(LINE_R), array(LINE_V), array(LINE_MAG) # Other data arrays to use: global avg_mfrac, avg_hvdfrac, stack_mfrac, stack_hvdfrac, maLINE_CAUMASS, maLINE_VDISP global stack_mbias, stack_mscat, stack_vbias, stack_vscat, avg_mbias, avg_mscat, avg_vbias, avg_vscat maLINE_CAUMASS = ma.masked_array(LINE_CAUMASS, mask=LINE_CAUMASS == 0) # Mask 0 Values maLINE_VDISP = ma.masked_array(LINE_VDISP, mask=LINE_VDISP == 0) # Mask 0 Values ### Mass Fractions ### # Note: I was using map() as an iterator, but for N = 5, sometimes there are less than 3 non-masked values per los # Note: and biweight###() does not take less than 4 unique values. I don't yet know how to incorporate a "try:" # Note: statement into an iterator function like map(), so I resort to a "for" loop ## Ensemble fractions stack_mfrac = ma.log(ENC_CAUMASS / M_crit200) stack_hvdfrac = ma.log(ENC_VDISP / HVD) ## Averaged fractions a_size = halo_num # This becomes line_num if doing vertical average first!! avg_mfrac, avg_hvdfrac = zeros(a_size), zeros(a_size) for a in range(a_size): try: avg_mfrac[a] = astStats.biweightLocation(ma.copy(ma.log(maLINE_CAUMASS[a] / M_crit200[a])), 6.0) avg_hvdfrac[a] = astStats.biweightLocation(ma.copy(ma.log(maLINE_VDISP[a] / HVD[a])), 6.0) except: avg_mfrac[a] = ma.mean(ma.log(maLINE_CAUMASS[a] / M_crit200[a])) avg_hvdfrac[a] = ma.mean(ma.log(maLINE_VDISP[a] / M_crit200[a])) # Bias and Scatter for Ensemble and LOS Average Systems stack_mbias, stack_mscat = ( astStats.biweightLocation(ma.copy(stack_mfrac), 6.0), astStats.biweightScale(ma.copy(stack_mfrac), 9.0), ) avg_mbias, avg_mscat = ( astStats.biweightLocation(ma.copy(avg_mfrac), 6.0), astStats.biweightScale(ma.copy(avg_mfrac), 9.0), ) stack_vbias, stack_vscat = ( astStats.biweightLocation(ma.copy(stack_hvdfrac), 6.0), astStats.biweightScale(ma.copy(stack_hvdfrac), 9.0), ) avg_vbias, avg_vscat = ( astStats.biweightLocation(ma.copy(avg_hvdfrac), 6.0), astStats.biweightScale(ma.copy(avg_hvdfrac), 9.0), )
def stat_calc(self,MASS_EST,MASS_TRUE,HVD_EST,HVD_TRUE,data_set=None,ens=True): ''' Does bias and scatter calculations ''' # Cut data set if necessary if data_set == 'cut_low_mass': '''Cutting all 'true' mass estimates below 1e14 off''' cut = np.where(MASS_TRUE>1e14)[0] MASS_EST = MASS_EST[cut] MASS_TRUE = MASS_TRUE[cut] HVD_EST = HVD_EST[cut] HVD_TRUE = HVD_TRUE[cut] # Define a Masked array for sometimes zero terms epsilon = 10.0 use_est = False # Use MassCalc estimated r200 mass values if true maMASS_EST = ma.masked_array(MASS_EST,mask=MASS_EST<epsilon) # Mask essentially zero values maHVD_EST = ma.masked_array(HVD_EST,mask=HVD_EST<epsilon) # Mass / HVD Fractions if ens == True: # Ensemble Arrays MFRAC = np.log(maMASS_EST/MASS_TRUE) VFRAC = np.log(maHVD_EST/HVD_TRUE) else: # LOS Mass Fraction Arrays: 0th axis is halo number, 1st axis is line of sight number MFRAC,VFRAC = [],[] for a in range(len(MASS_EST)): MFRAC.append( ma.log( maMASS_EST[a]/MASS_TRUE[a] ) ) VFRAC.append( ma.log( maHVD_EST[a]/HVD_TRUE[a] ) ) MFRAC,VFRAC = np.array(MFRAC),np.array(VFRAC) if ens == True: mbias,mscat = astStats.biweightLocation(MFRAC,6.0),astStats.biweightScale(MFRAC,9.0) vbias,vscat = astStats.biweightLocation(VFRAC,6.0),astStats.biweightScale(VFRAC,9.0) return MFRAC,mbias,mscat,VFRAC,vbias,vscat else: if self.ss: # Create vertically averaged (by halo averaged) arrays, with line_num elements # biweightLocation takes only arrays with 4 or more elements HORZ_MFRAC,HORZ_VFRAC = [],[] VERT_MFRAC,VERT_VFRAC = [],[] for a in range(self.line_num): if len(ma.compressed(MFRAC[:,a])) > 4: VERT_MFRAC.append( astStats.biweightLocation( ma.compressed( MFRAC[:,a] ), 6.0 ) ) VERT_VFRAC.append( astStats.biweightLocation( ma.compressed( VFRAC[:,a] ), 6.0 ) ) else: VERT_MFRAC.append( np.median( ma.compressed( MFRAC[:,a] ) ) ) VERT_VFRAC.append( np.median( ma.compressed( VFRAC[:,a] ) ) ) VERT_MFRAC,VERT_VFRAC = np.array(VERT_MFRAC),np.array(VERT_VFRAC) # Create horizontally averaged (by line of sight) arrays, with halo_num elements for a in self.halo_range: if len(ma.compressed(MFRAC[a])) > 4: HORZ_MFRAC.append( astStats.biweightLocation( ma.compressed( MFRAC[a] ), 6.0 ) ) HORZ_VFRAC.append( astStats.biweightLocation( ma.compressed( VFRAC[a] ), 6.0 ) ) else: HORZ_MFRAC.append( np.median( ma.compressed( MFRAC[a] ) ) ) HORZ_VFRAC.append( np.median( ma.compressed( VFRAC[a] ) ) ) HORZ_MFRAC,HORZ_VFRAC = np.array(HORZ_MFRAC),np.array(HORZ_VFRAC) # Bias and Scatter Calculations mbias,mscat = astStats.biweightLocation(VERT_MFRAC,6.0),astStats.biweightScale(VERT_MFRAC,9.0) vbias,vscat = astStats.biweightLocation(VERT_VFRAC,6.0),astStats.biweightScale(VERT_VFRAC,9.0) else: # Bin stack LOS systems need only one average mbias,mscat = astStats.biweightLocation(MFRAC,6.0),astStats.biweightScale(MFRAC,9.0) vbias,vscat = astStats.biweightLocation(VFRAC,6.0),astStats.biweightScale(VFRAC,9.0) return MFRAC,mbias,mscat,VFRAC,vbias,vscat
} gal_vlos(u) = gal_pos_vect(0,u)*gal_v(0,u)+gal_pos_vect(1,u)*gal_v(1,u)+gal_pos_vect(2,u)*gal_v(2,u); } """ fast = weave.inline(code,['gal_pos_vect','n','gal_dist','gal_vlos','gal_v','new_pos','gal_p'],type_converters=converters.blitz,compiler='gcc') r = (halo_dist**2-np.dot(halo_pos_vect*halo_dist,gal_pos_vect)**2)**0.5 v = gal_vlos-halo_vlos*np.dot(halo_pos_vect,gal_pos_vect) #dot product is cosine of the angle between the halo center and galaxy ################################### #End of line of sight calculations# ################################### #limit our particles based on set limits in projected radius and los velocity (aka: what we would observe) rvalues = np.array(r)[np.where((r < rlimit) & (v < vlimit) & (v > -vlimit))] vvalues = np.array(v)[np.where((r < rlimit) & (v < vlimit) & (v > -vlimit))] try: vfix = astStats.biweightLocation(vvalues[np.where((rvalues<0.5) & (vvalues>-vlimit) & (vvalues<vlimit))],6.0) vvalues = vvalues - vfix except: vfix = np.average(vvalues[np.where((rvalues<0.5) & (vvalues>-3500) & (vvalues<3500))]) vvalues = vvalues - vfix rvalues1 = np.array(rvalues)[np.where((rvalues < rlimit) & (vvalues < vlimit) & (vvalues > -vlimit))] vvalues1 = np.array(vvalues)[np.where((rvalues < rlimit) & (vvalues < vlimit) & (vvalues > -vlimit))] rvalues = rvalues1 vvalues = vvalues1 #limit our sample based on a richness limit rand_int = np.random.randint(0,rvalues.size,rich_lim) rvalues = rvalues[rand_int] vvalues = vvalues[rand_int]