def process(self): """rearranges the ping data into a matrix of max amplitude of dimensions corrisponding to the power, gain and beam sections.""" MINSAMPLES = 5 datadim = self.pingdata.shape self.pingmax = pl.zeros((len(self.settings['power']), len(self.settings['gain']), datadim[2])) for i, power in enumerate(self.settings['power']): for j, gain in enumerate(self.settings['gain']): for k in xrange(datadim[2]): sampleindx = pl.find((self.pingdata[:, 1, k] == power) & (self.pingdata[:, 2, k] == gain)) if len(sampleindx) > MINSAMPLES: temp = self.pingdata[sampleindx[-MINSAMPLES:], 0, k] tempmax = temp.max() if tempmax == 0: self.pingmax[i, j, k] = pl.NaN else: self.pingmax[i, j, k] = temp.max() else: self.pingmax[i, j, k] = pl.NaN #The following section removes settings that were collected erroniously. #gain settings first null = pl.zeros((len(self.settings['gain']), datadim[2])) powershortlist = [] self.havedata = True # this is an ugly workaround... for i, power in enumerate(self.settings['power']): test = pl.isnan(self.pingmax[i, :, :] ) if test.all(): powershortlist.append(i) print 'removing ' + str(power) + ' power setting.' for i in powershortlist: try: self.settings['power'].pop(i) except IndexError: self.havedata = False if self.havedata: self.pingmax = pl.delete(self.pingmax, powershortlist, 0) #then power settings null = pl.zeros((len(self.settings['power']), datadim[2])) gainshortlist = [] for i, gain in enumerate(self.settings['gain']): test = pl.isnan(self.pingmax[:, i, :]) if test.all(): gainshortlist.append(i) print 'removing ' + str(gain) + ' gain setting.' for i in gainshortlist: try: self.settings['gain'].pop(i) except IndexError: self.havedata = False if self.havedata: self.pingmax = pl.delete(self.pingmax, gainshortlist, 1) #remove the power and gain to normalize self.pingmax = 20*pl.log10(self.pingmax) for i, power in enumerate(self.settings['power']): for j, gain in enumerate(self.settings['gain']): self.pingmax[i, j, :] = self.pingmax[i, j, :] - power - gain
def _create_cropped_cmap(n_segments = 12, cmap='hot'): new_cmap = plt.cm.get_cmap(cmap, n_segments)(range(n_segments)) new_cmap = plt.delete(new_cmap, (0, -1), 0) palette = matplotlib.colors.LinearSegmentedColormap.from_list("palette", new_cmap) return palette
def delta_E(pos): t0 = time.time() neighbors = (pos + neighbor_kernel) % N tpos = tuple(pos) dE = n.zeros(6) # calc. dE for each neighbor exchange, return array for k, neigh in enumerate(neighbors): tneigh = tuple(neigh) if lattice[tneigh] != lattice[tpos]: n_center = [neighbors[j] for j in p.delete(p.arange(6), k)] n_neigh = ((pos + neighbor_neighbors[k]) % N) # n_neigh = [ne for j, ne in enumerate(n_neigh) if j != reverse_neigh_indices[k]] for temp in n_center: temp = tuple(temp) dE[k] -= lattice[temp] * lattice[tpos] * u11 + u00 * ( (not lattice[temp]) * (not lattice[tpos]) ) #+ u10*(lattice[temp] + lattice[tpos])*(not lattice[temp]*lattice[tpos]) dE[k] += lattice[temp] * lattice[tneigh] * u11 + u00 * ( (not lattice[temp]) * (not lattice[tneigh]) ) #+ u10*(lattice[temp] + lattice[tneigh])*(not lattice[temp]*lattice[tneigh]) for temp in n_neigh: temp = tuple(temp) dE[k] += lattice[temp] * lattice[tpos] * u11 + u00 * ( (not lattice[temp]) * (not lattice[tpos]) ) #+ u10*(lattice[temp] + lattice[tpos])*(not lattice[temp]*lattice[tpos]) dE[k] -= lattice[temp] * lattice[tneigh] * u11 + u00 * ( (not lattice[temp]) * (not lattice[tneigh]) ) #+ u10*(lattice[temp] + lattice[tneigh])*(not lattice[temp]*lattice[tneigh]) t1 = time.time() return dE, t1 - t0
def calculate_activity_histogram(spikes, total_neurons, bin=0.1): """Calculates histogram and bins specifically for neurons. Bins are provided in seconds instead of milliseconds.""" hist, bin_edges = pylab.histogram( [spike[0] for spike in spikes], bins=pylab.ceil(max([spike[0] for spike in spikes])/bin)) bin_edges = pylab.delete(bin_edges, len(bin_edges)-1) / 1000. return [[float(i)/total_neurons for i in hist], bin_edges]
def findAbsorptionLines(self): #no interpolation here, just return the measured data peak index tresh=6 #db treshhold to detect a line #filter first #bring to logarithmic scale movav=self.getmovingAveragedData() hlog=20*py.log10(movav[:,2]) #smooth data #remove etalon minima ixlines_prob=signal.argrelmin(hlog)[0] ixlines=[] ixmax=signal.argrelmax(hlog)[0] #check depth of minima compared two the two adjacent maxima if ixmax[0]>ixlines_prob[0]: #true if starts with minimum, treat this case separatedly if hlog[ixmax[0]]-hlog[ixlines_prob[0]]>tresh: ixlines.append(ixlines_prob[0]) #and remove first minimum from ixlines_prob ixlines_prob=py.delete(ixlines_prob,0) if ixmax[-1]<ixlines_prob[-1]: #treat this case separatedly if hlog[ixmax[-1]]-hlog[ixlines_prob[-1]]>tresh: ixlines.append(ixlines_prob[0]) ixlines_prob=py.delete(ixlines_prob,-1) #now the remaining elements of ixlines, ixmax should be in the order of max min max ... min max leftdist=hlog[ixmax[:-1]]-hlog[ixlines_prob] rightdist=hlog[ixmax[1:]]-hlog[ixlines_prob] for i in range(len(ixlines_prob)): #check if distance is higher than treshhold if (leftdist[i]>tresh or rightdist[i]>tresh): ixlines.append(ixlines_prob[i]) #remove inappropriate lines (by distance so far, at least 5 datapoints! ixlines=py.asarray(ixlines) ixlines=py.concatenate(([ixlines[0]],ixlines[py.diff(ixlines)>5])) return ixlines
def variability(r_times): """ Get HRV from RR occurance times array. In: r_times : ndarray, Relative time in seconds Out: time : ndarray, Relative time vector in seconds hrv : ndarray, HRV vector in milliseconds """ time = pl.delete(r_times,-1) hrv = pl.zeros(len(r_times)-1, dtype='float') for i in range(0, len(r_times)-1): hrv[i] = (r_times[i+1]-r_times[i])* 1000 assert pl.shape(time) == pl.shape(hrv) return time, hrv
def data_remove_constant_cols(array): (nrows, ncols) = array.shape delete_cols = [] non_delete_cols = [] for c in range(ncols): same = True for r in range(1, nrows): # Non constant if array[r][c] != array[r - 1][c]: same = False break # If constant, save column number to be deleted if same: delete_cols.append(c) else: non_delete_cols.append(c) # Delete columns ndeleted_cols = 0 for c in delete_cols: array = pylab.delete(array, c - ndeleted_cols, axis=1) # Reduce future column numbers now that array shape has changed ndeleted_cols += 1 return array
filters.low_pass_filter(data_ecog[i, :], Fsampling=f_sampling, Fcutoff=f_lp_cutoff), int(f_sampling / f_subsample)) data_ecog_lp_ss.flush() print(i) pl.save(os.path.join(memap_folder, 'data_ecog_lp_ss.npy'), data_ecog_lp_ss) spike_samples = tf.spikedetect(data_probe_hp, threshold_multiplier=6.5, bad_channels=probe_bad_channels) pl.save(os.path.join(memap_folder, 'spike_samples.npy'), spike_samples) spike_samples_clean = spike_samples for i in pl.arange(pl.size(spike_samples_clean)-1,-1,-1): data = data_probe_hp[:, spike_samples[i]-60:spike_samples[i]+60] stdevs = sp.std(data,1) if np.max(data) > 3000 or pl.any(stdevs>600): spike_samples_clean = pl.delete(spike_samples_clean, i) if i%100==0: print(i) spike_samples_clean = pl.delete(spike_samples_clean, 0) pl.save(os.path.join(memap_folder, 'spike_samples_clean.npy'), spike_samples_clean) channels = np.empty(0) for i in pl.arange(0, pl.size(spike_samples_clean)): data = np.array(data_probe_hp[:, spike_samples_clean[i]].tolist()) channels = np.append(channels, np.argmax(data)) if i%100==0: print(i) channels_spikes_df = pd.DataFrame([(channels, spike_samples_clean)], columns=['Channels', 'Samples']) spike_times_shaftA = channels_spikes_df.Samples[0][channels_spikes_df.Channels[0]>7][channels_spikes_df.Channels[0]<16] spike_times_shaftB = channels_spikes_df.Samples[0][channels_spikes_df.Channels[0]>23]
def crunchData(estimTypes, colNums, observables): ''' Takes a list of pimc file types ['estimator', 'super', etc..] and a list of list of column numbers (in order of file types) as [[0,1], [1,4,6], etc...] and will combine data from jobs that were the same but had different random number seeds all into one file. ''' args = parseCMD() reduceType = args.reduceType numToDelete = args.deleteNum deleteList = [] for n in range(numToDelete): deleteList.append(n) numLinesToSkip = 2+numToDelete print 'deleting elements indexed by: ',deleteList print 'and skipping',numLinesToSkip,'lines of data file.' # make list of data file names estimFiles = glob.glob('*%s*' % estimTypes[0]) # check which ensemble canonical = ensembleCheck(estimFiles[0][0]) # make list of all reduced variable, in order, based on ensemble indList = indepVarList(estimFiles, canonical, reduceType) for nType, estType in enumerate(estimTypes): # dict to hold all estType data for all independent variables (T,..) allTemps = {} # dict to hold numBins, average, stdErr for each data file statTemps = {} # loop over independent variable for given estType for numTemp, temp in enumerate(indList): # Get filenames. The chemical potential always has a sign # in front and the temperature always comes after the # file type and a dash. if reduceType == 'T': bipFiles = glob.glob('*%s-%s*' % (estimTypes[nType], temp)) elif reduceType == 'u': bipFiles = glob.glob('*%s*%s*' % (estimTypes[nType], temp)) arrs = {} arrs2 = {} for nf, f in enumerate(bipFiles): if checkIfEmpty(f,numLinesToSkip): print f,' is empty.' pass else: dat = pl.genfromtxt(f, unpack=True, usecols=colNums[nType]) # skip a few for d in dat: d = pl.delete(d, deleteList) # define key that can be sorted properly if numTemp < 10: arrName = 'arr00'+str(numTemp) elif (numTemp >= 10 and numTemp < 100): arrName = 'arr0'+str(numTemp) else: arrName = 'arr'+str(numTemp) # treat data from one column if int(len(colNums[nType])) == 1: if nf == 0: arrs[arrName] = dat arrs2[arrName] = [[pl.average(dat), pl.std(dat)/pl.sqrt(1.0*int(len(dat))), int(len(dat))]] else: arrs[arrName] = pl.append(arrs[arrName], dat) arrs2[arrName] = pl.append( arrs2[arrName], [[pl.average(dat), pl.std(dat)/pl.sqrt(1.0*int(len(dat))), int(len(dat))]]) # treat data from multiple columns else: if nf == 0: for n, arr in enumerate(dat): arrs[arrName+'_'+str(n)] = arr arrs2[arrName+'_'+str(n)] = [[pl.average(arr), pl.std(arr)/pl.sqrt(1.0*int(len(arr))), int(len(arr))]] else: for n, arr in enumerate(dat): arrs[arrName+'_'+str(n)] = pl.append( arrs[arrName+'_'+str(n)], arr) arrs2[arrName+'_'+str(n)] = pl.append( arrs2[arrName+'_'+str(n)], [[pl.average(arr), pl.std(arr)/pl.sqrt(1.0*int(len(arr))), int(len(arr))]]) # construct key name. This assumes <1000 temperatures. if numTemp < 10: allArrName = 'allTemps00'+str(numTemp) elif (10 <= numTemp and numTemp < 100): allArrName = 'allTemps0'+str(numTemp) else: allArrName = 'allTemps'+str(numTemp) if nType < 10: allArrName += '00'+str(nType) elif (10 <= nType and nType < 100): allArrName += '0'+str(nType) else: allArrName += str(nType) allTemps[allArrName] = arrs statTemps[allArrName] = arrs2 # ---- Write all data to disk ---- # length of max. sized array for all data maxLen = 0 for t in allTemps: for g in allTemps[t]: arrayLen = len(allTemps[t][g]) if arrayLen > maxLen: maxLen = arrayLen # open file to hold all of estType data newName = 'Reduced'+str(estType.capitalize())+'Data.dat' fout = open(newName, 'w') # write independent variable as top header line fout.write('#%15s\t' % indList[0]) for n in range(len(colNums[nType])-1): fout.write('%16s\t'% '') for temp in indList[1:]: fout.write('%16s\t'% temp) for n in range(len(colNums[nType])-1): fout.write('%16s\t'% '') fout.write('\n') # write observable names as second header line fout.write('#%15s\t' % observables[nType][0]) for n in range(len(observables[nType])-1): fout.write('%16s\t' % observables[nType][n+1]) for temp in indList[1:]: for obs in observables[nType]: fout.write('%16s\t' % obs) fout.write('\n') # write data arrays to disk for line in range(maxLen): for a in sorted(allTemps.iterkeys()): for aa in sorted(allTemps[a]): try: fout.write('%16.8E,\t' % ( float(allTemps[a][aa][line]) )) except: fout.write('%16s,\t' % '') fout.write('\n') fout.close() # ---- Average individual files and write to disk ---- # get length of max. sized array for averages maxLen2 = 0 for t in statTemps: for g in statTemps[t]: arrayLen = len(statTemps[t][g]) if arrayLen > maxLen2: maxLen2 = arrayLen maxLen2 /= 3 # open file for standard error newName = 'zAveraged'+str(estType.capitalize())+'Data.dat' fout = open(newName, 'w') # write independent variable as top header line fout.write('#%15s\t%16s\t%16s\t' % ( indList[0], '','')) for n in range(len(colNums[nType])-1): fout.write('%16s\t%16s\t%16s\t'% ('','','')) for temp in indList[1:]: fout.write('%16s\t%16s\t%16s\t'% (temp,'','')) for n in range(len(colNums[nType])-1): fout.write('%16s\t%16s\t%16s\t'% ('','','')) fout.write('\n') # write observable names as second header line fout.write('#%15s\t%16s\t%16s\t' % ( observables[nType][0], 'std Err', 'bins')) for n in range(len(observables[nType])-1): fout.write('%16s\t%16s\t%16s\t' % ( observables[nType][n+1], 'std Err', 'bins')) for temp in indList[1:]: for obs in observables[nType]: fout.write('%16s\t%16s\t%16s\t' % ( obs, 'std Err', 'bins')) fout.write('\n') # write data arrays to disk en = 0 for line in range(maxLen2): for a in sorted(statTemps.iterkeys()): for aa in sorted(statTemps[a]): try: fout.write('%15.8E,\t%15.8E,\t%15.8E,\t' % ( float(statTemps[a][aa][en]), float(statTemps[a][aa][en+1]), float(statTemps[a][aa][en+2]))) except: fout.write('%15s,\t%15s,\t%15s,\t' % ('','','')) fout.write('\n') en += 3 fout.close()
ll = 1.5 ul = 4 bins_x = pyl.arange(ll, ul, 0.5) grid = [] for i in range(bins_x.size - 1): xmin = bins_x[i] xmax = bins_x[i + 1] cond = [cond1 and cond2 for cond1, cond2 in zip(x >= xmin, x < xmax)] grid.append(galaxies2.compress(cond)) icd = [] for i in range(len(grid)): icd.append([galaxy.ICD_IH * 100 for galaxy in grid[i]]) from boxplot_percentile import percentile_box_plot as pbp pbp(f1s1, icd, indexer=list(pyl.delete(bins_x, -1) + 0.25)) pbp(f1s2, icd, indexer=list(pyl.delete(bins_x, -1) + 0.25)) # Add the box and whiskers galaxies = pickle.load(open('galaxies.pickle', 'rb')) galaxies2 = filter(lambda galaxy: galaxy.ston_V > 30., galaxies) galaxies2 = pyl.asarray(galaxies2) x = [galaxy.z for galaxy in galaxies2] ll = 1.5 ul = 4 bins_x = pyl.arange(ll, ul, 0.5) grid = [] for i in range(bins_x.size - 1): xmin = bins_x[i] xmax = bins_x[i + 1]
def plot_icd_vs_mass(): galaxies = pickle.load(open('galaxies.pickle','rb')) # galaxies = filter(lambda galaxy: 0.06 * galaxy.halflight *\ # astCalc.da(galaxy.z)*1000/206265. > 2, galaxies) # Make figure f1 = pyl.figure(1, figsize=(6,4)) f1s1 = f1.add_subplot(121) f1s2 = f1.add_subplot(122) # f1s3 = f1.add_subplot(223) # f1s4 = f1.add_subplot(224) #Upper and Lower limit arrow verts arrowup_verts = [[0.,0.], [-1., -1], [0.,0.], [0.,-2.], [0.,0.], [1,-1], [0,0]] #arrowdown_verts = [[0.,0.], [-1., 1], [0.,0.], # [0.,2.], [0.,0.], [1, 1]] for galaxy in galaxies: if galaxy.ston_I > 10. and galaxy.ICD_IH != None: # Add arrows first if galaxy.ICD_IH > 0.5: f1s1.scatter(galaxy.Mass, 0.5*100, s=100, marker=None, verts=arrowup_verts) else: f1s1.scatter(galaxy.Mass, galaxy.ICD_IH * 100, c='0.8', marker='o', s=25, edgecolor='0.8') f1s2.scatter(galaxy.Mass, galaxy.ICD_IH * 100, c='0.8', marker='o', s=25, edgecolor='0.8') # Add the box and whiskers galaxies2 = filter(lambda galaxy: galaxy.ston_I > 10., galaxies) galaxies2 = pyl.asarray(galaxies2) x = [galaxy.Mass for galaxy in galaxies2] ll = 8.5 ul= 12 bins_x =pyl.array([8.5, 9., 9.5, 10., 10.5, 11., 12.]) grid = [] for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1] cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)] grid.append(galaxies2.compress(cond)) icd = [] for i in range(len(grid)): icd.append([galaxy.ICD_IH*100 for galaxy in grid[i]]) from boxplot_percentile_width import percentile_box_plot as pbp #bp1 = f1s1.boxplot(icd, positions=pyl.delete(bins_x,-1)+0.25, sym='') width = pyl.diff(bins_x) index = pyl.delete(bins_x,-1) + 0.25 index[-1] = index[-1] + 0.25 pbp(f1s1, icd, indexer=list(index), width=width) pbp(f1s2, icd, indexer=list(index), width=width) f1s1.set_xlim(8,12) f1s2.set_xlim(8,12) f1s1.set_ylim(-10,50) f1s2.set_ylim(0,15) f1s1.set_xticks([8,9,10,11,12]) f1s2.set_xticks([8,9,10,11,12]) f1s1.set_ylabel(r"$\xi[i_{775},H_{160}]$ (%)") f1s1.set_xlabel(r"Log Mass ($M_{\odot})$") f1s2.set_xlabel(r"Log Mass ($M_{\odot})$") import matplotlib.font_manager line1 = pyl.Line2D([], [], marker='o', mfc='0.8', mec='0.8', markersize=8, linewidth=0) line2 = pyl.Line2D([], [], marker='s', mec='#348ABD', mfc='None', markersize=10, linewidth=0, markeredgewidth=2) line3 = pyl.Line2D([], [], color='#A60628', linewidth=2) prop = matplotlib.font_manager.FontProperties(size='small') pyl.figlegend((line1, line2, line3), ('Data', 'Quartiles', 'Medians'), 'lower center', prop=prop, ncol=3) from matplotlib.patches import ConnectionPatch xy = (12, 15) xy2 = (8, 15) con = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data', axesA=f1s1, axesB=f1s2) xy = (12, 0) xy2 = (8, 0) con2 = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data', axesA=f1s1, axesB=f1s2) f1s1.add_artist(con) f1s1.add_artist(con2) xy = (12, 3) xy2 = (8, 3) xy = (12, -1) xy2 = (8, -1) pyl.draw() pyl.show()
import pylab as pl from scipy.spatial import ConvexHull # On the 2-Sphere, the Voronoi tesselation is equivalent to the convex hull projected on the sphere # (Sugihara, Journal for Geometry and Graphics Volume 6 (2002), No. 1, 69-81.) # I assume that the same is true in 4D.... [This has to be checked!] R = 1.6180339887498949 #magic number by straley for 120 particles import sys polar = pl.load(sys.argv[1]) from spheretools import * cartesian = convert(polar, R) CHull = ConvexHull(cartesian) with open("bonds.txt", 'w') as fw: for p in range(cartesian.shape[0]): # print p which_simplex, position = pl.where(CHull.simplices == p) # print which_simplex all_neighs = pl.unique(CHull.simplices[which_simplex].flatten()) # print "all_neighs",all_neighs index_of_p = pl.where(all_neighs == p) # print "p is at",index_of_p neighs = pl.delete(all_neighs, index_of_p) # print"neighs after ", neighs fw.write(str(len(neighs)) + " " + " ".join(map(str, neighs)) + "\n")
def read_bunch_data(file_list, format='ascii'): A, R = [], [] if format == 'ascii': data_list = [f.replace('.cfg', '_prt.dat') for f in file_list] for i, d in enumerate(data_list): try: A.append(plt.loadtxt(d).T) except IOError: print '*** WARNING: no file ', d # R.append((i, file_list[i])) A.append(A[-1]) except ValueError: print i, d raise ValueError elif format == 'hdf5': data_list = [f.replace('.cfg', '.prt.h5') for f in file_list] for i, d in enumerate(data_list): try: hf = h5py.File(d, 'r') A.append( plt.insert(hf['Fields']['Data'], 0, hf['Time']['time'], axis=1).T) except IOError: print '*** WARNING: no file ', d R.append((i, file_list[i])) elif format == 'h5': data_list = file_list A = None for i, d in enumerate(data_list): try: hf = h5py.File(d, 'r')['Bunch'] if A == None: keys = hf.keys() a = hf[keys[0]] A = plt.zeros((len(data_list), len(keys) + 1, len(a))) A[i, 0, :] = plt.arange(len(hf['mean_x'])) A[i, 1, :] = hf['mean_x'] A[i, 2, :] = hf['mean_xp'] A[i, 3, :] = hf['mean_y'] A[i, 4, :] = hf['mean_yp'] A[i, 5, :] = hf['mean_z'] A[i, 6, :] = hf['mean_dp'] A[i, 7, :] = hf['sigma_x'] A[i, 8, :] = hf['sigma_y'] A[i, 9, :] = hf['sigma_z'] A[i, 10, :] = hf['sigma_dp'] A[i, 11, :] = hf['epsn_x'] A[i, 12, :] = hf['epsn_y'] A[i, 13, :] = hf['epsn_z'] A[i, 14, :] = hf['n_macroparticles'] # A.append(plt.array( # [, # hf['mean_x'], hf['mean_xp'], hf['mean_y'], hf['mean_yp'], hf['mean_dz'], hf['mean_dp'], # hf['sigma_x'], hf['sigma_y'], hf['sigma_dz'], hf['sigma_dp'], # hf['epsn_x'], hf['epsn_y'], hf['epsn_z'], hf['n_macroparticles']]).T) except IOError: print '*** WARNING: no file ', d R.append((i, file_list[i])) except KeyError: print '*** WARNING: damaged file ', d R.append((i, file_list[i])) else: raise(ValueError('*** Unknown format: ', format)) for r in R: file_list.remove(r[1]) delete_from_data_array = [r[0] for r in R] A = plt.delete(A, delete_from_data_array, axis=0) A = plt.array(A) A = plt.rollaxis(A, 0, 3) # [n_cols, n_turns, n_files] = A.shape return A, file_list
def sim(vx0,y0,L01,a0,k1,dE,m,ygrd,t,fs,steps): # function for using fmin # def optfunc_dE(par): # return abs(-.5*k1*c**2+.5*(k1+par)*(c-(c*par/(k1+par)))**2-dE) # function for using fsolve def optfunc_dE(par, spring_par): k1 = spring_par['k'] c = spring_par['c'] return abs(-.5*k1*c**2+.5*(k1+par)*(c-(c*par/(k1+par)))**2-dE) g = -9.81 # gravitational force betaTO_sim = zeros(steps) xF=0 print 'x' x_sim = [0] y_sim = [0] vx_sim = [0] vy_sim = [0] t_sim = [0] k2 = 0 L02 = 0 skip_all = False st_lo_idx = -1 # leg orientation xL = L01 * cos(a0 * pi/180.) yL = L01 * sin(a0 * pi/180.) # first ground contact t_gc = sqrt(2.*(L01*sin(a0*pi/180.)-y0)/g) if isnan(t_gc) == True: skip_all = True if skip_all == False: x_gc = vx0*t_gc y_gc = .5*g*t_gc**2 + y0 # system parameter for first step s = zeros(4) s[0] = 0 s[1] = y0 s[2] = vx0 s[3] = 0 # definitions for calculation end_sim = False res = zeros([1,4]) res_app = [] t_v = [0] skip_stance = False # start loop while end_sim == False: st_lo_idx += 1 if st_lo_idx != 0: # if not first step t_gc = (-res[-1,3]/g) + \ sqrt( ((res[-1,3]**2)/(g**2)) - \ ((2*res[-1,1])/g-(2*L01*sin(a0*pi/180)/g)) ) if isnan(t_gc) == False and isinf(t_gc) == False: y_gc = 0.5*g*t_gc**2 + res[-1,3]*t_gc + res[-1,1] t_vec = linspace(0,t_gc,ceil(t_gc*fs)+1) #print len(t_vec) s[0] = res[-1,2]*t_gc + res[-1,0] s[1] = y_gc s[2] = res[-1,2] s[3] = g*t_gc +res[-1,3] else: t_gc = 1 t_vec = linspace(0,t_gc,ceil(t_gc*fs)+1) end_sim = True skip_stance = True #break vx = zeros_like(t_vec) vy = zeros_like(t_vec) x = zeros_like(t_vec) y = zeros_like(t_vec) vx[0] = res[-1,2] vy[0] = res[-1,3] x[0] = res[-1,0] y[0] = res[-1,1] else: # if first step t_vec = linspace(0,t_gc,ceil(t_gc*fs)+1) #print t_vec vx = zeros_like(t_vec) vy = zeros_like(t_vec) x = zeros_like(t_vec) y = zeros_like(t_vec) #print vx vx[0] = s[2] vy[0] = s[3] x[0] = s[0] y[0] = s[1] ### flight phase x[1:] = vx[0]*t_vec[1:] + x[0] y[1:] = .5*g*(t_vec[1:]**2)+vy[0]*t_vec[1:] + y[0] vy[1:] = g*t_vec[1:] + vy[0] vx[1:] = vx[0]*ones_like(t_vec[1:]) # end at apex if st_lo_idx == steps or skip_stance == True: tapex = vy[0]/-g #print 'y0=',y0,'vy0=',vy[0],' tapex=',tapex tvec_apex = linspace(0,tapex,ceil(tapex*fs)+1) vyapex = g*tvec_apex+vy[0] #print 'vyapex=',vyapex yapex = .5*g*(tvec_apex**2) + vyapex[0]*tvec_apex + y[0] xapex = vx[0] * tvec_apex + x[0] vxapex = vx[0] * ones_like(tvec_apex) #print 'lvy:',len(vyapex),' lvx:',len(vxapex) x_sim = append(x_sim,xapex[1:]) y_sim = append(y_sim,yapex[1:]) vx_sim = append(vx_sim,vxapex[1:]) vy_sim = append(vy_sim,vyapex[1:]) t_sim = append(t_sim,tvec_apex[1:]+t_sim[-1]) skip_stance = True """ for kk in arange(2,len(y)): if y[kk-2]<y[kk-1] and y[kk-1]>y[kk]: end_sim = True x_sim = append(x_sim,x[1:kk]) y_sim = append(y_sim,y[1:kk]) vx_sim = append(vx_sim,vx[1:kk]) vy_sim = append(vy_sim,vy[1:kk]) t_sim = append(t_sim,t_vec[1:kk]+t_sim[-1]) """ ### stance phase if skip_stance == False: # system parameter if st_lo_idx == 0: # if first step s[0] = x_gc # x s[1] = y_gc # y s[2] = vx0 # vx s[3] = g*t_gc + 0 # vy xF = s[0]+xL yF = ygrd parameter = {'k':k1,'L0':L01,'m':m,'g':g,'xF':xF,'yF':yF} calc_length = 0.1*fs find_pos = False round_prec = 11 # precision of rounding #print 'round_prec = ', round_prec step_size = 1./fs res = zeros([1,4]) t_v = [0] res_app = False skip = False L0 = L01 FinMin = False # start integration of stance phase while find_pos == False: t_vint = (step_size) * arange(calc_length) res_int = odeint(s_dot,s,t_vint, args=(parameter,), rtol=1e-11) # stop condition vx or y < 0 if res_int[-1,1] < 0 or res_int[-1,2] < 0: find_pos = True end_sim = True skip = True # if ymin: calculation of new k and L for energy changes for idx in arange(1,len(res_int[:,1])-1): if res_int[idx,1]<res_int[idx-1,1] and res_int[idx,1]<res_int[idx+1,1]: res_int=delete(res_int,s_[idx+1::],0) t_vint = delete(t_vint,s_[idx+1::],0) c= L0-(sqrt(res_int[-1,1]**2 + (xF-res_int[-1,0])**2)) #dk = fmin(optfunc_dE,0,full_output=1,disp=0,xtol=1e-12) spring_par = {'k':k1,'c':c} dk = fsolve(optfunc_dE,x0=[0],args=(spring_par,)) #print '###### dk ####',dk # if not find a minimum stop loop --> no step #if dk[-1] <> 0: # find_pos = True # end_sim = True # skip = True # raise ValueError, 'no energy management possible' # break k2 = k1+dk[0] L02 = L01 - c*dk[0]/(k1+dk[0]) #print 'delta L = ',L0 - L02, ' delta k = ', dk[0] L0 = L02 parameter = {'k':k2,'L0':L02,'m':m,'g':g,'xF':xF,'yF':yF} FinMin = True break # if not ymin: start one before to avoid ignoring while change if FinMin == False: res_int = res_int[:-1] t_vint = t_vint[:-1] # virtual leg after integration L_virt = sqrt(res_int[-1,1]**2 + (xF-res_int[-1,0])**2) if L_virt < L0 and skip == False: s[0] = res_int[-1,0] s[1] = res_int[-1,1] s[2] = res_int[-1,2] s[3] = res_int[-1,3] elif L_virt >= L0 and skip == False: for jj in arange(1,len(res_int)): L_virt_1 = sqrt(res_int[jj,1]**2 + abs(xF-res_int[jj,0])**2) L_virt_2 = sqrt(res_int[jj-1,1]**2 + abs(xF-res_int[jj-1,0])**2) if round(L_virt_2,round_prec) == round(L0,round_prec): find_pos = True res_int = res_int[0:jj,:] res_app = True t_vint = t_vint[0:jj] if res_int[-1,3] <=0: end_sim = True break elif L_virt_2 < L0 and L_virt_1 > L0: s[0] = res_int[jj-1,0] s[1] = res_int[jj-1,1] s[2] = res_int[jj-1,2] s[3] = res_int[jj-1,3] res_int = res_int[0:jj,:] res_app = True t_vint = t_vint[0:jj] step_size = step_size / 10. break # append arrays if res_app == False: res = res_int t_v = t_vint res_app = True else: res = append(res,res_int[1::],0) t_v = append(t_v,t_vint[1::]+t_v[-1]) # take of angle #print st_lo_idx betaTO_sim[st_lo_idx] = arctan(res[-1,1]/(res[-1,0]-xF))*180./pi # append arrays x_sim = append(x_sim,append(x,res[:,0])) y_sim = append(y_sim,append(y,res[:,1])) vx_sim = append(vx_sim,append(vx,res[:,2])) vy_sim = append(vy_sim,append(vy,res[:,3])) t_sim = append(t_sim,append(t_vec,t_v+t_gc)+t_sim[-1]) else: #skip_stance == True because of ending at apex end_sim = True # cut array x_sim = x_sim[1::] y_sim = y_sim[1::] vx_sim = vx_sim[1::] vy_sim = vy_sim[1::] t_sim = t_sim[1::] #print 'vor return' return x_sim,y_sim,vx_sim,vy_sim,t_sim,st_lo_idx,betaTO_sim,xF,k2,L02
def plot_icd_vs_mass(): galaxies = pickle.load(open('galaxies.pickle','rb')) arrow_up = [[0,0], [-1,-1], [0,0], [0,-2], [0,0], [1,-1], [0,0]] # Make figure f1 = pyl.figure(1, figsize=(4,6)) f1s1 = f1.add_subplot(211) f1s2 = f1.add_subplot(212) for galaxy in galaxies: if galaxy.ston_I > 30. and galaxy.ICD_IH != None: f1s1.scatter(galaxy.z, galaxy.ICD_IH * 100, c='0.8', marker='o', s=25, edgecolor='0.8') if galaxy.ston_V > 30. and galaxy.ICD_VJ != None: f1s2.scatter(galaxy.z, galaxy.ICD_VJ * 100, c='0.8', marker='o', s=25, edgecolor='0.8') # Add the box and whiskers galaxies2 = filter(lambda galaxy: galaxy.ston_I > 30., galaxies) galaxies2 = pyl.asarray(galaxies2) x = [galaxy.z for galaxy in galaxies2] ll = 1.5 ul= 4 bins_x =pyl.arange(ll, ul, 0.5) grid = [] for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1] cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)] grid.append(galaxies2.compress(cond)) icd = [] for i in range(len(grid)): icd.append([galaxy.ICD_IH*100 for galaxy in grid[i]]) from boxplot_percentile import percentile_box_plot as pbp pbp(f1s1, icd, indexer=list(pyl.delete(bins_x,-1)+0.25), whisker_top=None, whisker_bottom=None) # Add the box and whiskers galaxies = pickle.load(open('galaxies.pickle','rb')) galaxies2 = filter(lambda galaxy: galaxy.ston_V > 30., galaxies) galaxies2 = pyl.asarray(galaxies2) x = [galaxy.z for galaxy in galaxies2] ll = 1.5 ul= 4 bins_x =pyl.arange(ll, ul, 0.5) grid = [] for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1] cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)] grid.append(galaxies2.compress(cond)) icd = [] for i in range(len(grid)): icd.append([galaxy.ICD_VJ*100 for galaxy in grid[i]]) pbp(f1s2, icd, indexer=list(pyl.delete(bins_x,-1)+0.25), whisker_top=None, whisker_bottom=None) f1s1.set_xlim(1.5, 3.5) f1s2.set_xlim(1.5, 3.5) f1s1.set_ylim(-1, 10) f1s2.set_ylim(-1, 10) f1s1.set_xticklabels([]) f1s2.set_xticks([1.5,2,2.5,3,3.5]) f1s1.set_ylabel(r"$\xi[i_{775},H_{160}]$ (%)") f1s2.set_xlabel("Redshift") f1s2.set_ylabel(r"$\xi[V_{606},J_{125}]$ (%)") import matplotlib.font_manager line1 = pyl.Line2D([], [], marker='o', mfc='0.8', mec='0.8', markersize=8, linewidth=0) line2 = pyl.Line2D([], [], marker='s', mec='#348ABD', mfc='None', markersize=10, linewidth=0, markeredgewidth=2) line3 = pyl.Line2D([], [], color='#A60628', linewidth=2) prop = matplotlib.font_manager.FontProperties(size='small') pyl.figlegend((line1, line2, line3), ('Data', 'Quartiles', 'Medians'), 'lower center', prop=prop, ncol=3) pyl.show()
def calculate_activity_histogram_one_neu(spikes, bins): """Calculates histogram of one neuron""" hist, bin_edges = pylab.histogram(spikes, bins=bins) bin_edges = pylab.delete(bin_edges, len(bin_edges)-1) / 1000. return [[float(i) for i in hist], bin_edges]
def plot_uvj_vs_icd(): galaxies = pickle.load(open('galaxies.pickle','rb')) galaxies = filter(lambda galaxy: galaxy.ICD_IH != None, galaxies) galaxies = filter(lambda galaxy: galaxy.sersic != None and \ galaxy.ston_I > 30, galaxies) #Upper and Lower limit arrow verts arrowup_verts = [[0.,0.], [-1., -1], [0.,0.], [0.,-2.], [0.,0.], [1,-1]] #arrowdown_verts = [[0.,0.], [-1., 1], [0.,0.], # [0.,2.], [0.,0.], [1, 1]] F = pyl.figure(1,figsize=(8,3)) grid = AxesGrid(F, 111, nrows_ncols=(1,4), axes_pad = 0.1, add_all=True, aspect=False, share_all = True) ax1 = grid[0] ax2 = grid[1] ax3 = grid[2] ax4 = grid[3] for galaxy in galaxies: if galaxy.sersic < 1.: col1 =ax1.scatter(galaxy.Mass, galaxy.ICD_IH * 100., s=25, c='0.8', edgecolor='0.8') if 1. < galaxy.sersic < 2.: col2 =ax2.scatter(galaxy.Mass, galaxy.ICD_IH * 100., s=25, c='0.8', edgecolor='0.8') if 2. < galaxy.sersic < 3.: col3 =ax3.scatter(galaxy.Mass, galaxy.ICD_IH * 100., s=25, c='0.8', edgecolor='0.8') if 3. < galaxy.sersic: if galaxy.ICD_IH*100 < 50: col4 =ax4.scatter(galaxy.Mass, galaxy.ICD_IH * 100., s=25, c='0.8', edgecolor='0.8') else: col4 = ax4.scatter(galaxy.Mass, 50, marker=None, s=100, verts=arrowup_verts) # Add the box and whiskers galaxies1 = filter(lambda galaxy: galaxy.ston_I > 30. and \ galaxy.sersic < 1, galaxies) galaxies1 = pyl.asarray(galaxies1) galaxies2 = filter(lambda galaxy: galaxy.ston_I > 30. and \ 1 < galaxy.sersic < 2, galaxies) galaxies2 = pyl.asarray(galaxies2) galaxies3 = filter(lambda galaxy: galaxy.ston_I > 30. and \ 2 < galaxy.sersic < 3, galaxies) galaxies3 = pyl.asarray(galaxies3) galaxies4 = filter(lambda galaxy: galaxy.ston_I > 30. and \ 3 < galaxy.sersic, galaxies) galaxies4 = pyl.asarray(galaxies4) x1 = [galaxy.Mass for galaxy in galaxies1] x2 = [galaxy.Mass for galaxy in galaxies2] x3 = [galaxy.Mass for galaxy in galaxies3] x4 = [galaxy.Mass for galaxy in galaxies4] grid1 = [] grid2 = [] grid3 = [] grid4 = [] from boxplot_percentile_width import percentile_box_plot as pbp bins_x =pyl.array([8.5, 9., 9.5, 10., 11]) for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1] cond=[cond1 and cond2 for cond1, cond2 in zip(x1>=xmin, x1<xmax)] grid1.append(galaxies1.compress(cond)) icd1 = [] for i in range(len(grid1)): icd1.append([galaxy.ICD_IH*100 for galaxy in grid1[i]]) width = pyl.diff(bins_x) index = pyl.delete(bins_x,-1) + 0.25 index[-1] = index[-1] + 0.25 bp1 = pbp(ax1, icd1, indexer=list(index), width=width) bins_x =pyl.array([8.5, 9., 9.5, 10., 10.5 ,11.5]) for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1] cond=[cond1 and cond2 for cond1, cond2 in zip(x2>=xmin, x2<xmax)] grid2.append(galaxies2.compress(cond)) icd2 = [] for i in range(len(grid2)): icd2.append([galaxy.ICD_IH*100 for galaxy in grid2[i]]) width = pyl.diff(bins_x) index = pyl.delete(bins_x,-1) + 0.25 index[-1] = index[-1] + 0.25 bp2 = pbp(ax2, icd2, indexer=list(index), width=width) bins_x =pyl.array([8.5, 9.5, 10., 11.]) for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1] cond=[cond1 and cond2 for cond1, cond2 in zip(x3>=xmin, x3<xmax)] grid3.append(galaxies3.compress(cond)) icd3 = [] for i in range(len(grid3)): icd3.append([galaxy.ICD_IH*100 for galaxy in grid3[i]]) width = pyl.diff(bins_x) index = pyl.delete(bins_x,-1) + 0.25 index[-1] = index[-1] + 0.25 index[0] = index[0] + 0.25 bp3 = pbp(ax3, icd3, indexer=list(index), width=width) bins_x =pyl.array([8.5, 9., 9.5, 10., 10.5, 11., 12.]) for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1] cond=[cond1 and cond2 for cond1, cond2 in zip(x4>=xmin, x4<xmax)] grid4.append(galaxies4.compress(cond)) icd4 = [] for i in range(len(grid4)): icd4.append([galaxy.ICD_IH*100 for galaxy in grid4[i]]) width = pyl.diff(bins_x) index = pyl.delete(bins_x,-1) + 0.25 index[-1] = index[-1] + 0.25 print 'ajsdf' bp4 = pbp(ax4, icd4, indexer=list(index), width=width) ax1.set_xticks([8, 9, 10, 11]) ax2.set_xticks([8, 9, 10, 11]) ax3.set_xticks([8, 9, 10, 11]) ax4.set_xticks([8, 9, 10, 11]) ax1.set_ylim(0, 50) ax2.set_ylim(0, 50) ax3.set_ylim(0, 50) ax4.set_ylim(0, 50) ax1.set_ylabel(r'$\xi[i_{775},H_{160}]$ (%)') ax1.set_title('n < 1') ax2.set_title('1 < n < 2') ax3.set_title('2 < n < 3') ax4.set_title('3 < n') pyl.figtext(.5, .05, r'Log Mass $(M_{\odot})$',fontsize=18, horizontalalignment='center') ax1.axhline(0, lw=2, zorder=0) ax2.axhline(0, lw=2, zorder=0) ax3.axhline(0, lw=2, zorder=0) ax4.axhline(0, lw=2, zorder=0) import matplotlib.font_manager line1 = pyl.Line2D([], [], marker='o', mfc='0.8', mec='0.8', markersize=8, linewidth=0) line2 = pyl.Line2D([], [], marker='s', mec='#348ABD', mfc='None', markersize=10, linewidth=0, markeredgewidth=2) line3 = pyl.Line2D([], [], color='#A60628', linewidth=2) prop = matplotlib.font_manager.FontProperties(size='small') ax3.legend((line1, line2, line3), ('Data', 'Quartiles', 'Medians'), loc='upper center', prop=prop, ncol=1) pyl.tight_layout() pyl.subplots_adjust(bottom=0.21, left=0.11) pyl.show()
def crunchData(estimTypes, colNums, observables): ''' Takes a list of pimc file types ['estimator', 'super', etc..] and a list of list of column numbers (in order of file types) as [[0,1], [1,4,6], etc...] and will combine data from jobs that were the same but had different random number seeds all into one file. ''' args = parseCMD() reduceType = args.reduceType numToDelete = args.deleteNum deleteList = [] for n in range(numToDelete): deleteList.append(n) numLinesToSkip = 2 + numToDelete print 'deleting elements indexed by: ', deleteList print 'and skipping', numLinesToSkip, 'lines of data file.' # make list of data file names estimFiles = glob.glob('*%s*' % estimTypes[0]) # check which ensemble canonical = ensembleCheck(estimFiles[0][0]) # make list of all reduced variable, in order, based on ensemble indList = indepVarList(estimFiles, canonical, reduceType) for nType, estType in enumerate(estimTypes): # dict to hold all estType data for all independent variables (T,..) allTemps = {} # dict to hold numBins, average, stdErr for each data file statTemps = {} # loop over independent variable for given estType for numTemp, temp in enumerate(indList): # Get filenames. The chemical potential always has a sign # in front and the temperature always comes after the # file type and a dash. if reduceType == 'T': bipFiles = glob.glob('*%s-%s*' % (estimTypes[nType], temp)) elif reduceType == 'u': bipFiles = glob.glob('*%s*%s*' % (estimTypes[nType], temp)) arrs = {} arrs2 = {} for nf, f in enumerate(bipFiles): if checkIfEmpty(f, numLinesToSkip): print f, ' is empty.' pass else: dat = pl.genfromtxt(f, unpack=True, usecols=colNums[nType]) # skip a few for d in dat: d = pl.delete(d, deleteList) # define key that can be sorted properly if numTemp < 10: arrName = 'arr00' + str(numTemp) elif (numTemp >= 10 and numTemp < 100): arrName = 'arr0' + str(numTemp) else: arrName = 'arr' + str(numTemp) # treat data from one column if int(len(colNums[nType])) == 1: if nf == 0: arrs[arrName] = dat arrs2[arrName] = [[ pl.average(dat), pl.std(dat) / pl.sqrt(1.0 * int(len(dat))), int(len(dat)) ]] else: arrs[arrName] = pl.append(arrs[arrName], dat) arrs2[arrName] = pl.append(arrs2[arrName], [[ pl.average(dat), pl.std(dat) / pl.sqrt(1.0 * int(len(dat))), int(len(dat)) ]]) # treat data from multiple columns else: if nf == 0: for n, arr in enumerate(dat): arrs[arrName + '_' + str(n)] = arr arrs2[arrName + '_' + str(n)] = [[ pl.average(arr), pl.std(arr) / pl.sqrt(1.0 * int(len(arr))), int(len(arr)) ]] else: for n, arr in enumerate(dat): arrs[arrName + '_' + str(n)] = pl.append( arrs[arrName + '_' + str(n)], arr) arrs2[arrName + '_' + str(n)] = pl.append( arrs2[arrName + '_' + str(n)], [[ pl.average(arr), pl.std(arr) / pl.sqrt(1.0 * int(len(arr))), int(len(arr)) ]]) # construct key name. This assumes <1000 temperatures. if numTemp < 10: allArrName = 'allTemps00' + str(numTemp) elif (10 <= numTemp and numTemp < 100): allArrName = 'allTemps0' + str(numTemp) else: allArrName = 'allTemps' + str(numTemp) if nType < 10: allArrName += '00' + str(nType) elif (10 <= nType and nType < 100): allArrName += '0' + str(nType) else: allArrName += str(nType) allTemps[allArrName] = arrs statTemps[allArrName] = arrs2 # ---- Write all data to disk ---- # length of max. sized array for all data maxLen = 0 for t in allTemps: for g in allTemps[t]: arrayLen = len(allTemps[t][g]) if arrayLen > maxLen: maxLen = arrayLen # open file to hold all of estType data newName = 'Reduced' + str(estType.capitalize()) + 'Data.dat' fout = open(newName, 'w') # write independent variable as top header line fout.write('#%15s\t' % indList[0]) for n in range(len(colNums[nType]) - 1): fout.write('%16s\t' % '') for temp in indList[1:]: fout.write('%16s\t' % temp) for n in range(len(colNums[nType]) - 1): fout.write('%16s\t' % '') fout.write('\n') # write observable names as second header line fout.write('#%15s\t' % observables[nType][0]) for n in range(len(observables[nType]) - 1): fout.write('%16s\t' % observables[nType][n + 1]) for temp in indList[1:]: for obs in observables[nType]: fout.write('%16s\t' % obs) fout.write('\n') # write data arrays to disk for line in range(maxLen): for a in sorted(allTemps.iterkeys()): for aa in sorted(allTemps[a]): try: fout.write('%16.8E,\t' % (float(allTemps[a][aa][line]))) except: fout.write('%16s,\t' % '') fout.write('\n') fout.close() # ---- Average individual files and write to disk ---- # get length of max. sized array for averages maxLen2 = 0 for t in statTemps: for g in statTemps[t]: arrayLen = len(statTemps[t][g]) if arrayLen > maxLen2: maxLen2 = arrayLen maxLen2 /= 3 # open file for standard error newName = 'zAveraged' + str(estType.capitalize()) + 'Data.dat' fout = open(newName, 'w') # write independent variable as top header line fout.write('#%15s\t%16s\t%16s\t' % (indList[0], '', '')) for n in range(len(colNums[nType]) - 1): fout.write('%16s\t%16s\t%16s\t' % ('', '', '')) for temp in indList[1:]: fout.write('%16s\t%16s\t%16s\t' % (temp, '', '')) for n in range(len(colNums[nType]) - 1): fout.write('%16s\t%16s\t%16s\t' % ('', '', '')) fout.write('\n') # write observable names as second header line fout.write('#%15s\t%16s\t%16s\t' % (observables[nType][0], 'std Err', 'bins')) for n in range(len(observables[nType]) - 1): fout.write('%16s\t%16s\t%16s\t' % (observables[nType][n + 1], 'std Err', 'bins')) for temp in indList[1:]: for obs in observables[nType]: fout.write('%16s\t%16s\t%16s\t' % (obs, 'std Err', 'bins')) fout.write('\n') # write data arrays to disk en = 0 for line in range(maxLen2): for a in sorted(statTemps.iterkeys()): for aa in sorted(statTemps[a]): try: fout.write('%15.8E,\t%15.8E,\t%15.8E,\t' % (float(statTemps[a][aa][en]), float(statTemps[a][aa][en + 1]), float(statTemps[a][aa][en + 2]))) except: fout.write('%15s,\t%15s,\t%15s,\t' % ('', '', '')) fout.write('\n') en += 3 fout.close()
def plot_uvj_vs_icd(): galaxies = pickle.load(open("galaxies.pickle", "rb")) galaxies = filter(lambda galaxy: galaxy.ICD_IH != None, galaxies) galaxies = filter(lambda galaxy: galaxy.sersic != None and galaxy.ston_I > 30, galaxies) # Upper and Lower limit arrow verts arrowup_verts = [[0.0, 0.0], [-1.0, -1], [0.0, 0.0], [0.0, -2.0], [0.0, 0.0], [1, -1]] # arrowdown_verts = [[0.,0.], [-1., 1], [0.,0.], # [0.,2.], [0.,0.], [1, 1]] F = pyl.figure(1, figsize=(8, 3)) grid = AxesGrid(F, 111, nrows_ncols=(1, 4), axes_pad=0.1, add_all=True, aspect=False, share_all=True) ax1 = grid[0] ax2 = grid[1] ax3 = grid[2] ax4 = grid[3] for galaxy in galaxies: if galaxy.sersic < 1.0: col1 = ax1.scatter(galaxy.Mass, galaxy.Color_grad, s=25, c="0.8", edgecolor="0.8") if 1.0 < galaxy.sersic < 2.0: col2 = ax2.scatter(galaxy.Mass, galaxy.Color_grad, s=25, c="0.8", edgecolor="0.8") if 2.0 < galaxy.sersic < 3.0: col3 = ax3.scatter(galaxy.Mass, galaxy.Color_grad, s=25, c="0.8", edgecolor="0.8") if 3.0 < galaxy.sersic: if galaxy.Color_grad < 50: col4 = ax4.scatter(galaxy.Mass, galaxy.Color_grad, s=25, c="0.8", edgecolor="0.8") else: col4 = ax4.scatter(galaxy.Mass, 50, marker=None, s=100, verts=arrowup_verts) # Add the box and whiskers galaxies1 = filter(lambda galaxy: galaxy.ston_I > 30.0 and galaxy.sersic < 1, galaxies) galaxies1 = pyl.asarray(galaxies1) galaxies2 = filter(lambda galaxy: galaxy.ston_I > 30.0 and 1 < galaxy.sersic < 2, galaxies) galaxies2 = pyl.asarray(galaxies2) galaxies3 = filter(lambda galaxy: galaxy.ston_I > 30.0 and 2 < galaxy.sersic < 3, galaxies) galaxies3 = pyl.asarray(galaxies3) galaxies4 = filter(lambda galaxy: galaxy.ston_I > 30.0 and 3 < galaxy.sersic, galaxies) galaxies4 = pyl.asarray(galaxies4) x1 = [galaxy.Mass for galaxy in galaxies1] x2 = [galaxy.Mass for galaxy in galaxies2] x3 = [galaxy.Mass for galaxy in galaxies3] x4 = [galaxy.Mass for galaxy in galaxies4] ll = 8.5 ul = 12 bins_x = pyl.arange(8.5, 12.5, 0.5) grid1 = [] grid2 = [] grid3 = [] grid4 = [] for i in range(bins_x.size - 1): xmin = bins_x[i] xmax = bins_x[i + 1] cond = [cond1 and cond2 for cond1, cond2 in zip(x1 >= xmin, x1 < xmax)] grid1.append(galaxies1.compress(cond)) cond = [cond1 and cond2 for cond1, cond2 in zip(x2 >= xmin, x2 < xmax)] grid2.append(galaxies2.compress(cond)) cond = [cond1 and cond2 for cond1, cond2 in zip(x3 >= xmin, x3 < xmax)] grid3.append(galaxies3.compress(cond)) cond = [cond1 and cond2 for cond1, cond2 in zip(x4 >= xmin, x4 < xmax)] grid4.append(galaxies4.compress(cond)) icd1 = [] icd2 = [] icd3 = [] icd4 = [] for i in range(len(grid1)): icd1.append([galaxy.Color_grad for galaxy in grid1[i]]) icd2.append([galaxy.Color_grad for galaxy in grid2[i]]) icd3.append([galaxy.Color_grad for galaxy in grid3[i]]) icd4.append([galaxy.Color_grad for galaxy in grid4[i]]) from boxplot_percentile import percentile_box_plot as pbp bp1 = pbp(ax1, icd1, indexer=list(pyl.delete(bins_x, -1) + 0.25)) bp2 = pbp(ax2, icd2, indexer=list(pyl.delete(bins_x, -1) + 0.25)) bp3 = pbp(ax3, icd3, indexer=list(pyl.delete(bins_x, -1) + 0.25)) bp4 = pbp(ax4, icd4, indexer=list(pyl.delete(bins_x, -1) + 0.25)) ax1.set_xticks([8, 9, 10, 11]) ax2.set_xticks([8, 9, 10, 11]) ax3.set_xticks([8, 9, 10, 11]) ax4.set_xticks([8, 9, 10, 11]) # ax1.set_xlim(8, 12) # ax2.set_xlim(8, 12) # ax3.set_xlim(8, 12) # ax4.set_xlim(8, 12) # ax1.set_ylim(0, 50) # ax2.set_ylim(0, 50) # ax3.set_ylim(0, 50) # ax4.set_ylim(0, 50) ax1.set_ylabel(r"$\xi[i_{775},H_{160}]$ (%)") ax1.set_title("n < 1") ax2.set_title("1 < n < 2") ax3.set_title("2 < n < 3") ax4.set_title("3 < n") pyl.figtext(0.5, 0.05, r"Log Mass $(M_{\odot})$", fontsize=18, horizontalalignment="center") ax1.axhline(0, lw=2, zorder=0) ax2.axhline(0, lw=2, zorder=0) ax3.axhline(0, lw=2, zorder=0) ax4.axhline(0, lw=2, zorder=0) import matplotlib.font_manager line1 = pyl.Line2D([], [], marker="o", mfc="0.8", mec="0.8", markersize=8, linewidth=0) line2 = pyl.Line2D([], [], marker="s", mec="blue", mfc="None", markersize=10, linewidth=0, markeredgewidth=2) line3 = pyl.Line2D([], [], color="r", linewidth=2) prop = matplotlib.font_manager.FontProperties(size="small") ax3.legend((line1, line2, line3), ("Data", "Quartiles", "Medians"), "upper center", prop=prop, ncol=1) pyl.tight_layout() pyl.subplots_adjust(bottom=0.21, left=0.11) pyl.show()
data_ecog_lp_ss.flush() print(i) pl.save(os.path.join(memap_folder, 'data_ecog_lp_ss.npy'), data_ecog_lp_ss) spike_samples = tf.spikedetect(data_probe_hp, threshold_multiplier=6.5, bad_channels=probe_bad_channels) pl.save(os.path.join(memap_folder, 'spike_samples.npy'), spike_samples) spike_samples_clean = spike_samples for i in pl.arange(pl.size(spike_samples_clean)-1,-1,-1): data = data_probe_hp[:, spike_samples[i]-60:spike_samples[i]+60] stdevs = sp.std(data,1) if np.max(data) > 3000 or pl.any(stdevs>600): spike_samples_clean = pl.delete(spike_samples_clean, i) if i%100==0: print(i) spike_samples_clean = pl.delete(spike_samples_clean, 0) pl.save(os.path.join(memap_folder, 'spike_samples_clean.npy'), spike_samples_clean) channels = np.empty(0) for i in pl.arange(0, pl.size(spike_samples_clean)): data = np.array(data_probe_hp[:, spike_samples_clean[i]].tolist()) channels = np.append(channels, np.argmax(data)) if i%100==0: print(i) channels_spikes_df = pd.DataFrame([(channels, spike_samples_clean)], columns=['Channels', 'Samples']) spike_times_shaftA = channels_spikes_df.Samples[0][channels_spikes_df.Channels[0]>7][channels_spikes_df.Channels[0]<16] spike_times_shaftB = channels_spikes_df.Samples[0][channels_spikes_df.Channels[0]>23]
def plot_uvj_vs_icd(): galaxies = pickle.load(open('galaxies.pickle', 'rb')) galaxies = filter(lambda galaxy: galaxy.ICD_IH != None, galaxies) galaxies = filter(lambda galaxy: galaxy.sersic != None and \ galaxy.ston_I > 10, galaxies) #Upper and Lower limit arrow verts arrowup_verts = [[0., 0.], [-1., -1], [0., 0.], [0., -2.], [0., 0.], [1, -1]] #arrowdown_verts = [[0.,0.], [-1., 1], [0.,0.], # [0.,2.], [0.,0.], [1, 1]] F = pyl.figure(1, figsize=(8, 3)) grid = AxesGrid(F, 111, nrows_ncols=(1, 4), axes_pad=0.1, add_all=True, aspect=False, share_all=False) ax1 = grid[0] ax2 = grid[1] ax3 = grid[2] ax4 = grid[3] for galaxy in galaxies: if galaxy.sersic < 1.: if galaxy.ICD_IH * 100 < 50: col1 = ax1.scatter(galaxy.Mass, galaxy.ICD_IH * 100., s=25, c='0.8', edgecolor='0.8') else: col1 = ax1.scatter(galaxy.Mass, 50, marker=None, s=100, verts=arrowup_verts) if 1. < galaxy.sersic < 2.: if galaxy.ICD_IH * 100 < 50: col2 = ax2.scatter(galaxy.Mass, galaxy.ICD_IH * 100., s=25, c='0.8', edgecolor='0.8') else: col2 = ax2.scatter(galaxy.Mass, 50, marker=None, s=100, verts=arrowup_verts) if 2. < galaxy.sersic < 3.: if galaxy.ICD_IH * 100 < 50: col3 = ax3.scatter(galaxy.Mass, galaxy.ICD_IH * 100., s=25, c='0.8', edgecolor='0.8') else: col3 = ax3.scatter(galaxy.Mass, 50, marker=None, s=100, verts=arrowup_verts) if 3. < galaxy.sersic: if galaxy.ICD_IH * 100 < 50: col4 = ax4.scatter(galaxy.Mass, galaxy.ICD_IH * 100., s=25, c='0.8', edgecolor='0.8') else: col4 = ax4.scatter(galaxy.Mass, 50, marker=None, s=100, verts=arrowup_verts) # Add the box and whiskers galaxies1 = filter(lambda galaxy: galaxy.ston_I > 10. and \ galaxy.sersic < 1, galaxies) galaxies1 = pyl.asarray(galaxies1) galaxies2 = filter(lambda galaxy: galaxy.ston_I > 10. and \ 1 < galaxy.sersic < 2, galaxies) galaxies2 = pyl.asarray(galaxies2) galaxies3 = filter(lambda galaxy: galaxy.ston_I > 10. and \ 2 < galaxy.sersic < 3, galaxies) galaxies3 = pyl.asarray(galaxies3) galaxies4 = filter(lambda galaxy: galaxy.ston_I > 10. and \ 3 < galaxy.sersic, galaxies) galaxies4 = pyl.asarray(galaxies4) x1 = [galaxy.Mass for galaxy in galaxies1] x2 = [galaxy.Mass for galaxy in galaxies2] x3 = [galaxy.Mass for galaxy in galaxies3] x4 = [galaxy.Mass for galaxy in galaxies4] grid1 = [] grid2 = [] grid3 = [] grid4 = [] from boxplot_percentile_width import percentile_box_plot as pbp bins_x = pyl.array([8.5, 9., 9.5, 10., 11]) for i in range(bins_x.size - 1): xmin = bins_x[i] xmax = bins_x[i + 1] cond = [cond1 and cond2 for cond1, cond2 in zip(x1 >= xmin, x1 < xmax)] grid1.append(galaxies1.compress(cond)) icd1 = [] for i in range(len(grid1)): icd1.append([galaxy.ICD_IH * 100 for galaxy in grid1[i]]) width = pyl.diff(bins_x) index = pyl.delete(bins_x, -1) + 0.25 index[-1] = index[-1] + 0.25 bp1 = pbp(ax1, icd1, indexer=list(index), width=width) bins_x = pyl.array([8.5, 9., 9.5, 10., 10.5, 11.5]) for i in range(bins_x.size - 1): xmin = bins_x[i] xmax = bins_x[i + 1] cond = [cond1 and cond2 for cond1, cond2 in zip(x2 >= xmin, x2 < xmax)] grid2.append(galaxies2.compress(cond)) icd2 = [] for i in range(len(grid2)): icd2.append([galaxy.ICD_IH * 100 for galaxy in grid2[i]]) width = pyl.diff(bins_x) index = pyl.delete(bins_x, -1) + 0.25 index[-1] = index[-1] + 0.25 bp2 = pbp(ax2, icd2, indexer=list(index), width=width) bins_x = pyl.array([8.5, 9.5, 10., 11.]) for i in range(bins_x.size - 1): xmin = bins_x[i] xmax = bins_x[i + 1] cond = [cond1 and cond2 for cond1, cond2 in zip(x3 >= xmin, x3 < xmax)] grid3.append(galaxies3.compress(cond)) icd3 = [] for i in range(len(grid3)): icd3.append([galaxy.ICD_IH * 100 for galaxy in grid3[i]]) width = pyl.diff(bins_x) index = pyl.delete(bins_x, -1) + 0.25 index[-1] = index[-1] + 0.25 index[0] = index[0] + 0.25 bp3 = pbp(ax3, icd3, indexer=list(index), width=width) bins_x = pyl.array([8.5, 9., 9.5, 10., 10.5, 11., 12.]) for i in range(bins_x.size - 1): xmin = bins_x[i] xmax = bins_x[i + 1] cond = [cond1 and cond2 for cond1, cond2 in zip(x4 >= xmin, x4 < xmax)] grid4.append(galaxies4.compress(cond)) icd4 = [] for i in range(len(grid4)): icd4.append([galaxy.ICD_IH * 100 for galaxy in grid4[i]]) width = pyl.diff(bins_x) index = pyl.delete(bins_x, -1) + 0.25 index[-1] = index[-1] + 0.25 print 'ajsdf' bp4 = pbp(ax4, icd4, indexer=list(index), width=width) ax1.set_xticks([8, 9, 10, 11]) ax2.set_xticks([8, 9, 10, 11, 12]) ax3.set_xticks([8, 9, 10, 11]) ax4.set_xticks([8, 9, 10, 11, 12]) ax1.set_ylim(0, 50) ax2.set_ylim(0, 50) ax3.set_ylim(0, 50) ax4.set_ylim(0, 50) ax1.set_xlim(8, 12.5) ax2.set_xlim(8, 12.5) ax3.set_xlim(8, 12.5) ax4.set_xlim(8, 12.5) ax1.set_ylabel(r'$\xi[i_{775},H_{160}]$ (%)') ax1.set_title('n < 1') ax2.set_title('1 < n < 2') ax3.set_title('2 < n < 3') ax4.set_title('3 < n') pyl.figtext(.5, .05, r'Log Mass $(M_{\odot})$', fontsize=18, horizontalalignment='center') ax1.axhline(0, lw=2, zorder=0) ax2.axhline(0, lw=2, zorder=0) ax3.axhline(0, lw=2, zorder=0) ax4.axhline(0, lw=2, zorder=0) import matplotlib.font_manager line1 = pyl.Line2D([], [], marker='o', mfc='0.8', mec='0.8', markersize=8, linewidth=0) line2 = pyl.Line2D([], [], marker='s', mec='#348ABD', mfc='None', markersize=10, linewidth=0, markeredgewidth=2) line3 = pyl.Line2D([], [], color='#A60628', linewidth=2) prop = matplotlib.font_manager.FontProperties(size='small') ax3.legend((line1, line2, line3), ('Data', 'Quartiles', 'Medians'), loc='upper center', prop=prop, ncol=1) pyl.tight_layout() pyl.subplots_adjust(bottom=0.21, left=0.11) pyl.show()
def plot_icd_vs_mass(): galaxies = pickle.load(open('galaxies.pickle','rb')) arrow_up = [[0,0], [-1,-1], [0,0], [0,-2], [0,0], [1,-1], [0,0]] # Make figure f1 = pyl.figure(1, figsize=(6,4)) f1s1 = f1.add_subplot(221) f1s2 = f1.add_subplot(222) f1s3 = f1.add_subplot(223) f1s4 = f1.add_subplot(224) for galaxy in galaxies: if galaxy.ston_I > 30. and galaxy.ICD_IH != None: if galaxy.ICD_IH*100 < 50: f1s1.scatter(galaxy.z, galaxy.ICD_IH * 100, c='0.8', marker='o', s=25, edgecolor='0.8') else: f1s1.scatter(galaxy.z, 50, s=100, marker=None, verts=arrow_up) f1s2.scatter(galaxy.z, galaxy.ICD_IH * 100, c='0.8', marker='o', s=25, edgecolor='0.8') if galaxy.ston_V > 30. and galaxy.ICD_VJ != None: f1s3.scatter(galaxy.z, galaxy.ICD_VJ * 100, c='0.8', marker='o', s=25, edgecolor='0.8') f1s4.scatter(galaxy.z, galaxy.ICD_VJ * 100, c='0.8', marker='o', s=25, edgecolor='0.8') # Add the box and whiskers galaxies2 = filter(lambda galaxy: galaxy.ston_I > 30., galaxies) galaxies2 = pyl.asarray(galaxies2) x = [galaxy.z for galaxy in galaxies2] ll = 1.5 ul= 4 bins_x =pyl.arange(ll, ul, 0.5) grid = [] for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1] cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)] grid.append(galaxies2.compress(cond)) icd = [] for i in range(len(grid)): icd.append([galaxy.ICD_IH*100 for galaxy in grid[i]]) from boxplot_percentile import percentile_box_plot as pbp pbp(f1s1, icd, indexer=list(pyl.delete(bins_x,-1)+0.25)) pbp(f1s2, icd, indexer=list(pyl.delete(bins_x,-1)+0.25)) # Add the box and whiskers galaxies = pickle.load(open('galaxies.pickle','rb')) galaxies2 = filter(lambda galaxy: galaxy.ston_V > 30., galaxies) galaxies2 = pyl.asarray(galaxies2) x = [galaxy.z for galaxy in galaxies2] ll = 1.5 ul= 4 bins_x =pyl.arange(ll, ul, 0.5) grid = [] for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1] cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)] grid.append(galaxies2.compress(cond)) icd = [] for i in range(len(grid)): icd.append([galaxy.ICD_VJ*100 for galaxy in grid[i]]) pbp(f1s3, icd, indexer=list(pyl.delete(bins_x,-1)+0.25)) pbp(f1s4, icd, indexer=list(pyl.delete(bins_x,-1)+0.25)) f1s1.set_xlim(1.5, 3.5) f1s2.set_xlim(1.5, 3.5) f1s3.set_xlim(1.5, 3.5) f1s4.set_xlim(1.5, 3.5) f1s1.set_ylim(-5, 50) f1s2.set_ylim(-1, 10) f1s3.set_ylim(-5, 50) f1s4.set_ylim(-1, 10) f1s1.set_xticks([1.5,2,2.5,3,3.5]) f1s2.set_xticks([1.5,2,2.5,3,3.5]) f1s1.set_xticklabels([]) f1s2.set_xticklabels([]) f1s3.set_xticks([1.5,2,2.5,3,3.5]) f1s4.set_xticks([1.5,2,2.5,3,3.5]) f1s1.axhline(0.0, lw=2, c='b', zorder=0) f1s2.axhline(0.0, lw=2, c='b', zorder=0) f1s3.axhline(0.0, lw=2, c='b', zorder=0) f1s4.axhline(0.0, lw=2, c='b', zorder=0) f1s3.set_xlabel("Redshift") f1s1.set_ylabel(r"$\xi[i_{775},H_{160}]$ (%)") f1s4.set_xlabel("Redshift") f1s3.set_ylabel(r"$\xi[V_{606},J_{125}]$ (%)") import matplotlib.font_manager line1 = pyl.Line2D([], [], marker='o', mfc='0.8', mec='0.8', markersize=8, linewidth=0) line2 = pyl.Line2D([], [], marker='s', mec='blue', mfc='None', markersize=10, linewidth=0, markeredgewidth=2) line3 = pyl.Line2D([], [], color='r', linewidth=2) prop = matplotlib.font_manager.FontProperties(size='small') pyl.figlegend((line1, line2, line3), ('Data', 'Quartiles', 'Medians'), 'lower center', prop=prop, ncol=3) from matplotlib.patches import ConnectionPatch xy = (3.5, 10) xy2 = (1.5, 10) con = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data', axesA=f1s1, axesB=f1s2) xy = (3.5, -1) xy2 = (1.5, -1) con2 = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data', axesA=f1s1, axesB=f1s2) f1s1.add_artist(con) f1s1.add_artist(con2) xy = (3.5, 10) xy2 = (1.5, 10) con = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data', axesA=f1s3, axesB=f1s4) xy = (3.5, -1) xy2 = (1.5, -1) con2 = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data', axesA=f1s3, axesB=f1s4) f1s3.add_artist(con) f1s3.add_artist(con2) pyl.draw() pyl.show()
def plot_icd_vs_mass(): galaxies = pickle.load(open('galaxies.pickle', 'rb')) # galaxies = filter(lambda galaxy: 0.06 * galaxy.halflight *\ # astCalc.da(galaxy.z)*1000/206265. > 2, galaxies) # Make figure f1 = pyl.figure(1, figsize=(6, 4)) f1s1 = f1.add_subplot(121) f1s2 = f1.add_subplot(122) # f1s3 = f1.add_subplot(223) # f1s4 = f1.add_subplot(224) #Upper and Lower limit arrow verts arrowup_verts = [[0., 0.], [-1., -1], [0., 0.], [0., -2.], [0., 0.], [1, -1], [0, 0]] #arrowdown_verts = [[0.,0.], [-1., 1], [0.,0.], # [0.,2.], [0.,0.], [1, 1]] for galaxy in galaxies: if galaxy.ston_I > 10. and galaxy.ICD_IH != None: # Add arrows first if galaxy.ICD_IH > 0.5: f1s1.scatter(galaxy.Mass, 0.5 * 100, s=100, marker=None, verts=arrowup_verts) else: f1s1.scatter(galaxy.Mass, galaxy.ICD_IH * 100, c='0.8', marker='o', s=25, edgecolor='0.8') f1s2.scatter(galaxy.Mass, galaxy.ICD_IH * 100, c='0.8', marker='o', s=25, edgecolor='0.8') # Add the box and whiskers galaxies2 = filter(lambda galaxy: galaxy.ston_I > 10., galaxies) galaxies2 = pyl.asarray(galaxies2) x = [galaxy.Mass for galaxy in galaxies2] ll = 8.5 ul = 12 bins_x = pyl.array([8.5, 9., 9.5, 10., 10.5, 11., 12.]) grid = [] for i in range(bins_x.size - 1): xmin = bins_x[i] xmax = bins_x[i + 1] cond = [cond1 and cond2 for cond1, cond2 in zip(x >= xmin, x < xmax)] grid.append(galaxies2.compress(cond)) icd = [] for i in range(len(grid)): icd.append([galaxy.ICD_IH * 100 for galaxy in grid[i]]) from boxplot_percentile_width import percentile_box_plot as pbp #bp1 = f1s1.boxplot(icd, positions=pyl.delete(bins_x,-1)+0.25, sym='') width = pyl.diff(bins_x) index = pyl.delete(bins_x, -1) + 0.25 index[-1] = index[-1] + 0.25 pbp(f1s1, icd, indexer=list(index), width=width) pbp(f1s2, icd, indexer=list(index), width=width) f1s1.set_xlim(8, 12) f1s2.set_xlim(8, 12) f1s1.set_ylim(-10, 50) f1s2.set_ylim(0, 15) f1s1.set_xticks([8, 9, 10, 11, 12]) f1s2.set_xticks([8, 9, 10, 11, 12]) f1s1.set_ylabel(r"$\xi[i_{775},H_{160}]$ (%)") f1s1.set_xlabel(r"Log Mass ($M_{\odot})$") f1s2.set_xlabel(r"Log Mass ($M_{\odot})$") import matplotlib.font_manager line1 = pyl.Line2D([], [], marker='o', mfc='0.8', mec='0.8', markersize=8, linewidth=0) line2 = pyl.Line2D([], [], marker='s', mec='#348ABD', mfc='None', markersize=10, linewidth=0, markeredgewidth=2) line3 = pyl.Line2D([], [], color='#A60628', linewidth=2) prop = matplotlib.font_manager.FontProperties(size='small') pyl.figlegend((line1, line2, line3), ('Data', 'Quartiles', 'Medians'), 'lower center', prop=prop, ncol=3) from matplotlib.patches import ConnectionPatch xy = (12, 15) xy2 = (8, 15) con = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data', axesA=f1s1, axesB=f1s2) xy = (12, 0) xy2 = (8, 0) con2 = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data', axesA=f1s1, axesB=f1s2) f1s1.add_artist(con) f1s1.add_artist(con2) xy = (12, 3) xy2 = (8, 3) xy = (12, -1) xy2 = (8, -1) pyl.draw() pyl.show()
def plot_icd_vs_mass(): galaxies = pickle.load(open('galaxies.pickle','rb')) # Make figure f1 = pyl.figure(1, figsize=(6,4)) f1s1 = f1.add_subplot(221) f1s2 = f1.add_subplot(222) f1s3 = f1.add_subplot(223) f1s4 = f1.add_subplot(224) #Upper and Lower limit arrow verts arrowup_verts = [[0.,0.], [-1., -1], [0.,0.], [0.,-2.], [0.,0.], [1,-1], [0,0]] #arrowdown_verts = [[0.,0.], [-1., 1], [0.,0.], # [0.,2.], [0.,0.], [1, 1]] for galaxy in galaxies: if galaxy.ston_I > 30. and galaxy.ICD_IH != None: # Add arrows first if galaxy.ICD_IH > 0.5: f1s1.scatter(galaxy.Mass, 0.5*100, s=100, marker=None, verts=arrowup_verts) else: f1s1.scatter(galaxy.Mass, galaxy.ICD_IH * 100, c='0.8', marker='o', s=25, edgecolor='0.8') f1s2.scatter(galaxy.Mass, galaxy.ICD_IH * 100, c='0.8', marker='o', s=25, edgecolor='0.8') if galaxy.ston_J > 30. and galaxy.ICD_JH != None: # Add arrows first if galaxy.ICD_JH > 0.12: f1s3.scatter(galaxy.Mass, 12, s=100, marker=None, verts=arrowup_verts) else: f1s3.scatter(galaxy.Mass, galaxy.ICD_JH * 100, c='0.8', marker='o', s=25, edgecolor='0.8') f1s4.scatter(galaxy.Mass, galaxy.ICD_JH * 100, c='0.8', marker='o', s=25, edgecolor='0.8') # Add the box and whiskers galaxies2 = filter(lambda galaxy: galaxy.ston_I > 30., galaxies) galaxies2 = pyl.asarray(galaxies2) x = [galaxy.Mass for galaxy in galaxies2] ll = 8.5 ul= 12 #bins_x =pyl.arange(8.5, 12.5, 0.5) bins_x =pyl.array([8.5, 9., 9.5, 10., 10.5, 11., 12.]) grid = [] for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1] cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)] grid.append(galaxies2.compress(cond)) icd = [] for i in range(len(grid)): icd.append([galaxy.ICD_IH*100 for galaxy in grid[i]]) from boxplot_percentile import percentile_box_plot as pbp #bp1 = f1s1.boxplot(icd, positions=pyl.delete(bins_x,-1)+0.25, sym='') pbp(f1s1, icd, indexer=list(pyl.delete(bins_x,-1)+0.25)) pbp(f1s2, icd, indexer=list(pyl.delete(bins_x,-1)+0.25)) # Add the box and whiskers galaxies2 = filter(lambda galaxy: galaxy.ston_J > 30., galaxies) galaxies2 = pyl.asarray(galaxies2) x = [galaxy.Mass for galaxy in galaxies2] ll = 8.5 ul= 12 #bins_x =pyl.linspace(ll, ul, 7) #bins_x =pyl.arange(8.5, 12.5, 0.5) bins_x =pyl.array([8.5, 9., 9.5, 10., 10.5, 11., 12.]) grid = [] for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1] cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)] grid.append(galaxies2.compress(cond)) icd = [] for i in range(len(grid)): icd.append([galaxy.ICD_JH*100 for galaxy in grid[i]]) #bp2 = f1s2.boxplot(icd, positions=pyl.delete(bins_x,-1)+0.25, sym='') pbp(f1s3, icd, indexer=list(pyl.delete(bins_x,-1)+0.25)) pbp(f1s4, icd, indexer=list(pyl.delete(bins_x,-1)+0.25)) # Finish Plot # Tweak colors on the boxplot #pyl.setp(bp1['boxes'], lw=2) #pyl.setp(bp1['whiskers'], lw=2) #pyl.setp(bp1['medians'], lw=2) #pyl.setp(bp2['boxes'], lw=2) #pyl.setp(bp2['whiskers'], lw=2) #pyl.setp(bp2['medians'], lw=2) #pyl.setp(bp['fliers'], color='#8CFF6F', marker='+') #f1s1.axvspan(7.477, 9, facecolor='#FFFDD0', ec='None', zorder=0) #f1s1.axvspan(11, 12, facecolor='#FFFDD0', ec='None', zorder=0) #f1s2.axvspan(7.477, 9, facecolor='#FFFDD0', ec='None', zorder=0) #f1s2.axvspan(11, 12, facecolor='#FFFDD0', ec='None', zorder=0) f1s1.set_xlim(8,12) f1s2.set_xlim(8,12) f1s3.set_xlim(8,12) f1s4.set_xlim(8,12) f1s1.set_ylim(-10,50) f1s2.set_ylim(0,15) f1s3.set_ylim(-5,12) f1s4.set_ylim(-1,3) f1s1.set_xticks([8,9,10,11,12]) f1s1.set_xticklabels([]) f1s2.set_xticks([8,9,10,11,12]) f1s2.set_xticklabels([]) f1s3.set_xticks([8,9,10,11,12]) f1s4.set_xticks([8,9,10,11,12]) f1s4.set_yticks([-1, 0, 1, 2, 3]) f1s3.set_xlabel(r"Log Mass ($M_{\odot})$") f1s1.set_ylabel(r"$\xi[i_{775},H_{160}]$ (%)") f1s4.set_xlabel(r"Log Mass ($M_{\odot})$") f1s3.set_ylabel(r"$\xi[J_{125},H_{160}]$ (%)") import matplotlib.font_manager line1 = pyl.Line2D([], [], marker='o', mfc='0.8', mec='0.8', markersize=8, linewidth=0) line2 = pyl.Line2D([], [], marker='s', mec='blue', mfc='None', markersize=10, linewidth=0, markeredgewidth=2) line3 = pyl.Line2D([], [], color='r', linewidth=2) prop = matplotlib.font_manager.FontProperties(size='small') pyl.figlegend((line1, line2, line3), ('Data', 'Quartiles', 'Medians'), 'lower center', prop=prop, ncol=3) from matplotlib.patches import ConnectionPatch xy = (12, 15) xy2 = (8, 15) con = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data', axesA=f1s1, axesB=f1s2) xy = (12, 0) xy2 = (8, 0) con2 = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data', axesA=f1s1, axesB=f1s2) f1s1.add_artist(con) f1s1.add_artist(con2) xy = (12, 3) xy2 = (8, 3) con = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data', axesA=f1s3, axesB=f1s4) xy = (12, -1) xy2 = (8, -1) con2 = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data', axesA=f1s3, axesB=f1s4) f1s3.add_artist(con) f1s3.add_artist(con2) pyl.draw() pyl.show()
from scipy.spatial import ConvexHull # On the 2-Sphere, the Voronoi tesselation is equivalent to the convex hull projected on the sphere # (Sugihara, Journal for Geometry and Graphics Volume 6 (2002), No. 1, 69-81.) # I assume that the same is true in 4D.... [This has to be checked!] R= 1.6180339887498949 #magic number by Straley for 120 particles import sys if (sys.argv[1][-3:]=="npy"): polar=pl.load(sys.argv[1]) else: polar=pl.loadtxt(sys.argv[1]) from spheretools import * cartesian=convert(polar, R) CHull=ConvexHull(cartesian) with open("bonds.txt",'w') as fw: for p in range(cartesian.shape[0]): # print p which_simplex,position=pl.where(CHull.simplices==p) # print which_simplex all_neighs=pl.unique(CHull.simplices[which_simplex].flatten()) # print "all_neighs",all_neighs index_of_p=pl.where(all_neighs==p) # print "p is at",index_of_p neighs=pl.delete(all_neighs,index_of_p) # print"neighs after ", neighs fw.write(str(len(neighs))+" "+" ".join(map(str, neighs))+"\n" )
def plot_icd_vs_mass(): galaxies = pickle.load(open('galaxies.pickle', 'rb')) arrow_up = [[0, 0], [-1, -1], [0, 0], [0, -2], [0, 0], [1, -1], [0, 0]] # Make figure f1 = pyl.figure(1, figsize=(4, 6)) f1s1 = f1.add_subplot(211) f1s2 = f1.add_subplot(212) for galaxy in galaxies: if galaxy.ston_I > 30. and galaxy.ICD_IH != None: f1s1.scatter(galaxy.z, galaxy.ICD_IH * 100, c='0.8', marker='o', s=25, edgecolor='0.8') if galaxy.ston_V > 30. and galaxy.ICD_VJ != None: f1s2.scatter(galaxy.z, galaxy.ICD_VJ * 100, c='0.8', marker='o', s=25, edgecolor='0.8') # Add the box and whiskers galaxies2 = filter(lambda galaxy: galaxy.ston_I > 30., galaxies) galaxies2 = pyl.asarray(galaxies2) x = [galaxy.z for galaxy in galaxies2] ll = 1.5 ul = 4 bins_x = pyl.arange(ll, ul, 0.5) grid = [] for i in range(bins_x.size - 1): xmin = bins_x[i] xmax = bins_x[i + 1] cond = [cond1 and cond2 for cond1, cond2 in zip(x >= xmin, x < xmax)] grid.append(galaxies2.compress(cond)) icd = [] for i in range(len(grid)): icd.append([galaxy.ICD_IH * 100 for galaxy in grid[i]]) from boxplot_percentile import percentile_box_plot as pbp pbp(f1s1, icd, indexer=list(pyl.delete(bins_x, -1) + 0.25), whisker_top=None, whisker_bottom=None) # Add the box and whiskers galaxies = pickle.load(open('galaxies.pickle', 'rb')) galaxies2 = filter(lambda galaxy: galaxy.ston_V > 30., galaxies) galaxies2 = pyl.asarray(galaxies2) x = [galaxy.z for galaxy in galaxies2] ll = 1.5 ul = 4 bins_x = pyl.arange(ll, ul, 0.5) grid = [] for i in range(bins_x.size - 1): xmin = bins_x[i] xmax = bins_x[i + 1] cond = [cond1 and cond2 for cond1, cond2 in zip(x >= xmin, x < xmax)] grid.append(galaxies2.compress(cond)) icd = [] for i in range(len(grid)): icd.append([galaxy.ICD_VJ * 100 for galaxy in grid[i]]) pbp(f1s2, icd, indexer=list(pyl.delete(bins_x, -1) + 0.25), whisker_top=None, whisker_bottom=None) f1s1.set_xlim(1.5, 3.5) f1s2.set_xlim(1.5, 3.5) f1s1.set_ylim(-1, 10) f1s2.set_ylim(-1, 10) f1s1.set_xticklabels([]) f1s2.set_xticks([1.5, 2, 2.5, 3, 3.5]) f1s1.set_ylabel(r"$\xi[i_{775},H_{160}]$ (%)") f1s2.set_xlabel("Redshift") f1s2.set_ylabel(r"$\xi[V_{606},J_{125}]$ (%)") import matplotlib.font_manager line1 = pyl.Line2D([], [], marker='o', mfc='0.8', mec='0.8', markersize=8, linewidth=0) line2 = pyl.Line2D([], [], marker='s', mec='#348ABD', mfc='None', markersize=10, linewidth=0, markeredgewidth=2) line3 = pyl.Line2D([], [], color='#A60628', linewidth=2) prop = matplotlib.font_manager.FontProperties(size='small') pyl.figlegend((line1, line2, line3), ('Data', 'Quartiles', 'Medians'), 'lower center', prop=prop, ncol=3) pyl.show()
ll = 1.5 ul= 4 bins_x =pyl.arange(ll, ul, 0.5) grid = [] for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1] cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)] grid.append(galaxies2.compress(cond)) icd = [] for i in range(len(grid)): icd.append([galaxy.ICD_IH*100 for galaxy in grid[i]]) from boxplot_percentile import percentile_box_plot as pbp pbp(f1s1, icd, indexer=list(pyl.delete(bins_x,-1)+0.25)) pbp(f1s2, icd, indexer=list(pyl.delete(bins_x,-1)+0.25)) # Add the box and whiskers galaxies = pickle.load(open('galaxies.pickle','rb')) galaxies2 = filter(lambda galaxy: galaxy.ston_V > 30., galaxies) galaxies2 = pyl.asarray(galaxies2) x = [galaxy.z for galaxy in galaxies2] ll = 1.5 ul= 4 bins_x =pyl.arange(ll, ul, 0.5) grid = [] for i in range(bins_x.size-1): xmin = bins_x[i] xmax = bins_x[i+1]