def distance_to_points(self, points): xs, ys = array(points).T xds = array([self._min.x - xs, xs - self._max.x]) yds = array([self._min.y - ys, ys - self._max.y]) r = arange(len(xs)) xds = xds[argmin(npabs(xds),axis=0),r] yds = yds[argmin(npabs(yds),axis=0),r] xds[xds<0] = 0 yds[yds<0] = 0 return hypot(xds,yds)
def identify(frequency): closest = npabs(NoteIdentifier._freqs - frequency).argmin() if npabs(frequency - NoteIdentifier._freqs[closest]) > \ npabs(frequency - NoteIdentifier._freqs[closest + 1]): other = NoteIdentifier._freqs[closest + 1] else: other = NoteIdentifier._freqs[closest - 1] uncertainty = npabs(frequency - NoteIdentifier._freqs[closest]) / \ npabs(other - NoteIdentifier._freqs[closest]) return (NoteIdentifier._freqs[closest], NoteIdentifier._notes[closest]), uncertainty
def decorrelation_witer(W): """ Iterative MDUM decorrelation that avoids matrix inversion. """ lim = 1.0 tol = 1.0e-05 W = W/(W**2).sum() while lim > tol: W1 = (3.0/2.0)*W - 0.5*dot(dot(W,W.T),W) lim = npmax(npabs(npabs(diag(dot(W1,W.T))) - 1.0)) W = W1 return W
def conv_der(im): """ calculate derivative of the image with convolution we learn in class :param im: image :return: the derived image """ x_conv = array([[1, 0, -1]]) x_der = convolve2d(im, x_conv, mode='same') y_der = convolve2d(im, x_conv.T, mode='same') magnitude = sqrt((npabs(x_der)**2) + (npabs(y_der)**2)) del x_conv, x_der, y_der return magnitude
def find_nearest(a, a0): "Element in nd array `a` closest to the scalar value `a0`" a0 = array(a0) s = a0.size idx = zeros(s) if s == 1: idx = npabs(a - a0).argmin() return idx elif s > 1: for i in range(s): idx[i] = npabs(a - a0[i]).argmin() return idx
def undistortedCoordinates(map1, map2, x, y, maxDistance = 1.): '''Returns the coordinates of a point in undistorted image map1 and map2 are the mapping functions from undistorted image to distorted (original image) map1(x,y) = originalx, originaly''' distx = npabs(map1-x) disty = npabs(map2-y) indices = logical_and(distx<maxDistance, disty<maxDistance) closeCoordinates = unravel_index(find(indices), distx.shape) # returns i,j, ie y,x xWeights = 1-distx[indices] yWeights = 1-disty[indices] return dot(xWeights, closeCoordinates[1])/npsum(xWeights), dot(yWeights, closeCoordinates[0])/npsum(yWeights)
def decorrelation_witer(W): """ Iterative MDUM decorrelation that avoids matrix inversion. """ lim = 1.0 tol = 1.0e-05 W = W / (W**2).sum() while lim > tol: W1 = (3.0 / 2.0) * W - 0.5 * dot(dot(W, W.T), W) lim = npmax(npabs(npabs(diag(dot(W1, W.T))) - 1.0)) W = W1 return W
def _get_still(mag_acc_f, dt, window, gravity, thresholds): """ Stillness determination of acceleration magnitude data Parameters ---------- mag_acc_f : numpy.ndarray (N, 3) array of filtered acceleration data. dt : float Sampling time difference, in seconds. window : float Moving statistics window length, in seconds. gravity : float Gravitational acceleration, as measured by the sensor during static sitting or standing. thresholds : dict Dictionary of the 4 thresholds to be used - acceleration and jerk moving averages and standard deviations. Returns ------- acc_still : numpy.ndarray (N, ) boolean array indicating stillness starts : numpy.ndarray (Q, ) array of where stillness ends, where by necessity has to follow: Q < N / 2 stops : numpy.ndarray (P, ) array of where stillness ends, where by necessity has to follow: P < N / 2 """ # calculate the sample window from the time window n_window = int(around(window / dt)) # compute the acceleration moving standard deviation am_avg, am_std, _ = mov_stats(mag_acc_f, n_window) # compute the jerk jerk = gradient(mag_acc_f, dt, edge_order=2) # compute the jerk moving average and standard deviation jm_avg, jm_std, _ = mov_stats(jerk, n_window) # create masks from the moving statistics of acceleration and jerk am_avg_mask = npabs(am_avg - gravity) < thresholds['accel moving avg'] am_std_mask = am_std < thresholds['accel moving std'] jm_avg_mask = npabs(jm_avg) < thresholds['jerk moving avg'] jm_std_mask = jm_std < thresholds['jerk moving std'] acc_still = am_avg_mask & am_std_mask & jm_avg_mask & jm_std_mask starts = where(diff(acc_still.astype(int)) == 1)[0] stops = where(diff(acc_still.astype(int)) == -1)[0] if acc_still[0]: starts = append(0, starts) if acc_still[-1]: stops = append(stops, len(acc_still) - 1) return acc_still, starts, stops
def ica_par_fp(X, tolerance, g, gprime, orthog, alpha, maxIterations, Winit): n, p = X.shape W = orthog(Winit) lim = tolerance + 1 it = 1 while ((lim > tolerance) and (it < maxIterations)): wtx = dot(W, X) gwtx = g(wtx, alpha) g_wtx = gprime(wtx, alpha) W1 = dot(gwtx, X.T) / p - dot(diag(g_wtx.mean(axis=1)), W) W1 = orthog(W1) lim = npmax(npabs(npabs(diag(dot(W1, W.T))) - 1.0)) W = W1 it = it + 1 return W
def _minimize(self, verbose=True, factr=FACTR, pgtol=PGTOL): from numpy import abs as npabs, max as npmax from numpy import empty, atleast_1d self.__verbose = verbose varnames = self.__varnames() grad = self.gradient() sign_grad = { name: self.__sign * atleast_1d(grad[name]) for name in varnames } self.__flat_gradient = empty(sum(s.size for s in sign_grad.values())) self.__flat_solution = empty( sum(self._variables[n].size for n in varnames)) _set_flat_arr(sign_grad, varnames, self.__flat_gradient) if npmax(npabs(self.__flat_gradient)) <= pgtol: if verbose: print("Gradient near zero before the first iteration. " "Returning the current value.") return r = self.__try_minimize(5, factr=factr, pgtol=pgtol) if r[2]["warnflag"] == 1: msg = "L-BFGS-B: too many function evaluations or too many iterations" raise OptimixError(msg) if r[2]["warnflag"] == 2: raise OptimixError("L-BFGS-B: {}".format(r[2]["task"])) _set_var_arr(r[0], self.__varnames(), self._variables)
def Weiner(f,s,n,cut,p): w=zeros(f.shape[0]) #print(w.shape) p = int(4) inds = [i for i,nu in enumerate(f) if npabs(nu)<cut] w[inds] = s*nppower(cos(pi/2. * f[inds] / cut) , p) return w/(w+n)
def bins_and_hist(a, b, w, size_hist, sigs, bin_size=11): bins = logspace(log10(a[w].min()), log10(a[w].max()), num=bin_size) a_bin = [0.5 * (bins[i - 1] + bins[i]) for i in range(1, len(bins))] b_bin = [ median((b[w])[(a[w] <= bins[i]) & (a[w] > bins[i - 1])]) for i in range(1, len(bins)) ] e_b_bin = [ std((b[w])[(a[w] <= bins[i]) & (a[w] > bins[i - 1])]) for i in range(1, len(bins)) ] x_bin, y_bin = linspace(log10(a[w]).min(), log10(a[w]).max(), num=size_hist),\ linspace(log10(b[w]).min(), log10(b[w]).max(), num=size_hist) H, xe, ye = histogram2d(log10(a[w]), log10(b[w]), bins=(x_bin, y_bin)) H_sorted, lev = sort(H.flatten())[::-1], [] for i in range(len(sigs)): lev.append(H_sorted[argmin( npabs(cumsum(H_sorted) - H.sum() / 100. * sigs[i]))]) lev = array(lev) lev[lev < 1.5] = 1. x, y = linspace(log10(a[w]).min(), log10(a[w]).max(), num=H.shape[0]), \ linspace(log10(b[w]).min(), log10(b[w]).max(), num=H.shape[1]) return x, y, H, log10(lev[::-1]), a_bin, b_bin, e_b_bin
def stetson_kindex(fmags, ferrs): ''' This calculates the Stetson K index (robust measure of the kurtosis). Requires finite mags and errs. ''' # use a fill in value for the errors if they're none if ferrs is None: ferrs = npfull_like(mags, 0.005) ndet = len(fmags) if ndet > 9: # get the median and ndet medmag = npmedian(fmags) # get the stetson index elements delta_prefactor = (ndet / (ndet - 1)) sigma_i = delta_prefactor * (fmags - medmag) / ferrs stetsonk = (npsum(npabs(sigma_i)) / (npsqrt(npsum(sigma_i * sigma_i))) * (ndet**(-0.5))) return stetsonk else: LOGERROR('not enough detections in this magseries ' 'to calculate stetson K index') return npnan
def remove_dependent_cols(X, tol=1e-6, verbose=False): r"""Remove dependent columns. Return a matrix with dependent columns removed. Parameters ---------- X : array_like Matrix to might have dependent columns. Returns ------- array_like Full column rank matrix. """ from scipy.linalg import qr from numpy import abs as npabs from numpy import any as npany from numpy import where R = qr(X, mode="r")[0][: X.shape[1], :] I = npabs(R.diagonal()) > tol if npany(~I) and verbose: msg = "Columns " + str(where(~I)[0]) print(msg + " have been removed because linear dependence") R = X[:, I] else: R = X.copy() return R
def dcpf(Ybus, Sbus, Ibus, V0, ref, pvpq, pq, pv): """ Solves a DC power flow. :param Ybus: Normal circuit admittance matrix :param Sbus: Complex power injections at all the nodes :param Ibus: Complex current injections at all the nodes :param V0: Array of complex seed voltage (it contains the ref voltages) :param ref: array of the indices of the slack nodes :param pvpq: array of the indices of the non-slack nodes :param pq: array of the indices of the pq nodes :param pv: array of the indices of the pv nodes :return: Complex voltage solution Converged: Always true Solution error Computed power injections given the found solution """ start = time.time() # Decompose the voltage in angle and magnitude Va_ref = angle(V0[ref]) # we only need the angles at the slack nodes Vm = npabs(V0) # initialize result vector Va = empty(len(V0)) # reconvert the pqpv vector to a matrix so that we can call numpy directly with it pvpq_ = matrix(pvpq) # Compile the reduced imaginary impedance matrix Bpqpv = Ybus.imag[pvpq_.T, pvpq_] Bref = Ybus.imag[pvpq_.T, ref] # compose the reduced power injections # Since we have removed the slack nodes, we must account their influence as injections Bref * Va_ref Pinj = Sbus[pvpq].real + (-Bref * Va_ref + Ibus[pvpq].real) * Vm[pvpq] # update angles for non-reference buses Va[pvpq] = spsolve(Bpqpv, Pinj) Va[ref] = Va_ref # re assemble the voltage V = Vm * exp(1j * Va) # compute the calculated power injection and the error of the voltage solution Scalc = V * conj(Ybus * V - Ibus) # compute the power mismatch between the specified power Sbus and the calculated power Scalc mis = Scalc - Sbus # complex power mismatch F = r_[mis[pv].real, mis[pq].real, mis[pq].imag] # concatenate again # check for convergence normF = linalg.norm(F, Inf) end = time.time() elapsed = end - start return V, True, normF, Scalc, 1, elapsed
def compute_seq_corr_matrix(X, N): '''computes sequnece correlation matrix''' seq_mat_prod = dot(X, tp(X))/N seq_avg_prod = \ dot(tp(matrix(mean(tp(X), 0))), matrix(mean(tp(X), 0))) seq_corr = npabs(seq_mat_prod - seq_avg_prod) return seq_corr
def compute_seq_corr_matrix(X, N): '''computes sequnece correlation matrix''' seq_mat_prod = dot(X, tp(X)) / N seq_avg_prod = \ dot(tp(matrix(mean(tp(X), 0))), matrix(mean(tp(X), 0))) seq_corr = npabs(seq_mat_prod - seq_avg_prod) return seq_corr
def ica_par_fp(X, tolerance, g, gprime, orthog, alpha, maxIterations, Winit): """Parallel FastICA; orthog sets the method of unmixing vector decorrelation. This fucntion is not meant to be directly called; it is wrapped by fastica().""" n,p = X.shape W = orthog(Winit) lim = tolerance + 1 it = 1 while ((lim > tolerance) and (it < maxIterations)): wtx = dot(W,X) gwtx = g(wtx,alpha) g_wtx = gprime(wtx,alpha) W1 = dot(gwtx,X.T)/p - dot(diag(g_wtx.mean(axis=1)),W) W1 = orthog(W1) lim = npmax(npabs(npabs(diag(dot(W1,W.T))) - 1.0)) W = W1 it = it + 1 return W
def ica_par_fp(X, tolerance, g, gprime, orthog, alpha, maxIterations, Winit): """Parallel FastICA; orthog sets the method of unmixing vector decorrelation. This fucntion is not meant to be directly called; it is wrapped by fastica().""" n, p = X.shape W = orthog(Winit) lim = tolerance + 1 it = 1 while ((lim > tolerance) and (it < maxIterations)): wtx = dot(W, X) gwtx = g(wtx, alpha) g_wtx = gprime(wtx, alpha) W1 = dot(gwtx, X.T) / p - dot(diag(g_wtx.mean(axis=1)), W) W1 = orthog(W1) lim = npmax(npabs(npabs(diag(dot(W1, W.T))) - 1.0)) W = W1 it = it + 1 return W
def lightcurve_moments(ftimes, fmags, ferrs): '''This calculates the weighted mean, stdev, median, MAD, percentiles, skew, kurtosis, fraction of LC beyond 1-stdev, and IQR. Parameters ---------- ftimes,fmags,ferrs : np.array The input mag/flux time-series with all non-finite elements removed. Returns ------- dict A dict with all of the light curve moments calculated. ''' ndet = len(fmags) if ndet > 9: # now calculate the various things we need series_median = npmedian(fmags) series_wmean = (npsum(fmags * (1.0 / (ferrs * ferrs))) / npsum(1.0 / (ferrs * ferrs))) series_mad = npmedian(npabs(fmags - series_median)) series_stdev = 1.483 * series_mad series_skew = spskew(fmags) series_kurtosis = spkurtosis(fmags) # get the beyond1std fraction series_above1std = len(fmags[fmags > (series_median + series_stdev)]) series_below1std = len(fmags[fmags < (series_median - series_stdev)]) # this is the fraction beyond 1 stdev series_beyond1std = (series_above1std + series_below1std) / float(ndet) # get the magnitude percentiles series_mag_percentiles = nppercentile( fmags, [5.0, 10, 17.5, 25, 32.5, 40, 60, 67.5, 75, 82.5, 90, 95]) return { 'median': series_median, 'wmean': series_wmean, 'mad': series_mad, 'stdev': series_stdev, 'skew': series_skew, 'kurtosis': series_kurtosis, 'beyond1std': series_beyond1std, 'mag_percentiles': series_mag_percentiles, 'mag_iqr': series_mag_percentiles[8] - series_mag_percentiles[3], } else: LOGERROR('not enough detections in this magseries ' 'to calculate light curve moments') return None
def draw_meas(self, ax_Meas, meas): measmax = npabs(meas).max() if self.max_meas is None or measmax > self.max_meas: self.max_meas = measmax return ax_Meas.pcolormesh(self.actions_grid, self.states_grid, meas, cmap=self.colors.cmap_meas, vmin=-self.max_meas, vmax=self.max_meas)
def ica_def(X, tolerance, g, gprime, orthog, alpha, maxIterations, Winit): n, p = X.shape W = Winit for j in xrange(n): w = Winit[j, :] it = 1 lim = tolerance + 1 while ((lim > tolerance) & (it < maxIterations)): wtx = dot(w, X) gwtx = g(wtx, alpha) g_wtx = gprime(wtx, alpha) w1 = (X * gwtx).mean(axis=1) - g_wtx.mean() * w w1 = decorrelation_gs(w1, W, j) lim = npabs(npabs((w1 * w).sum()) - 1.0) w = w1 it = it + 1 W[j, :] = w return W
def roundplot(traj, ax, linestyle, label=''): for j in range(rounds): index = (len(s)-1)*j x = traj[i*2, index]*1e3 y = traj[i*2+1, index]*1e3 if j == 0: ax.plot(x, y, linestyle, label=label) else: ax.plot(x, y, linestyle) axmax.append(max(npabs([x, y])))
def map_deflection(self, A, d1, d2, d3, d4, i, aeroNode, dType='x'): """ see mapDeflections... based on the posiition matrix, finds the ith deflection component Then we ratios the new deflection diMax and compares it to the nearby points to 'test'. """ di = array([d1[i], d2[i], d3[i], d4[i]]) diMax = max(npabs(di)) # L1 norm log.info("d%s = %s" % (dType, ListPrint(di))) abcd = solve(A, di) #ui = abcd*aeroNode # element-wise multiplication...faster, cant make it work; tried dot & transpose too... (a, b, c, d) = abcd ui = a + b*aeroNode[0] + c*aeroNode[1] + d*aeroNode[2] isRanged = is_list_ranged(0., abcd, 1.) log.info('isRanged=%s u%sRatio=%g - a=%g b=%g c=%g d=%g' %(isRanged, dType, npabs(ui/diMax), a, b, c, d)) return ui
def ica_def(X, tolerance, g, gprime, orthog, alpha, maxIterations, Winit): """Deflationary FastICA using Gram-Schmidt decorrelation at each step. This function is not meant to be directly called; it is wrapped by fastica().""" n,p = X.shape W = Winit # j is the index of the extracted component for j in xrange(n): w = Winit[j, :] it = 1 lim = tolerance + 1 while ((lim > tolerance) & (it < maxIterations)): wtx = dot(w, X) gwtx = g(wtx, alpha) g_wtx = gprime(wtx, alpha) w1 = (X * gwtx).mean(axis=1) - g_wtx.mean() * w w1 = decorrelation_gs(w1, W, j) lim = npabs(npabs((w1 * w).sum()) - 1.0) w = w1 it = it + 1 W[j, :] = w return W
def ica_def(X, tolerance, g, gprime, orthog, alpha, maxIterations, Winit): """Deflationary FastICA using Gram-Schmidt decorrelation at each step. This function is not meant to be directly called; it is wrapped by fastica().""" n, p = X.shape W = Winit # j is the index of the extracted component for j in xrange(n): w = Winit[j, :] it = 1 lim = tolerance + 1 while ((lim > tolerance) & (it < maxIterations)): wtx = dot(w, X) gwtx = g(wtx, alpha) g_wtx = gprime(wtx, alpha) w1 = (X * gwtx).mean(axis=1) - g_wtx.mean() * w w1 = decorrelation_gs(w1, W, j) lim = npabs(npabs((w1 * w).sum()) - 1.0) w = w1 it = it + 1 W[j, :] = w return W
def _hist_diffs(batch, quantile=50, bins=16): """Compute differences between HSV histograms across a batch. """ bsize = batch.bsize msize = bsize + 1 assert msize <= batch.get_frames().shape[0] hist_vals = _hsv_hist(batch, msize, bins=bins) norm = hist_vals[slice(0, bsize), :] - hist_vals[slice(1, bsize + 1), :] norm = norm / prod(batch.get_frames().shape[1:4]) * 100 return percentile(npabs(norm), q=quantile, axis=(1))
def map_deflection(self, A, d1, d2, d3, d4, i, aero_node, dType='x'): """ see mapDeflections... based on the posiition matrix, finds the ith deflection component Then we ratios the new deflection diMax and compares it to the nearby points to 'test'. """ di = array([d1[i], d2[i], d3[i], d4[i]]) diMax = max(npabs(di)) # L1 norm log.info("d%s = %s" % (dType, list_print(di))) abcd = solve(A, di) #ui = abcd*aero_node # element-wise multiplication...faster, # cant make it work; tried dot & transpose too... (a, b, c, d) = abcd ui = a + b*aero_node[0] + c*aero_node[1] + d*aero_node[2] is_ranged = is_list_ranged(0., abcd, 1.) log.info('is_ranged=%s u%sRatio=%g - a=%g b=%g c=%g d=%g' %( is_ranged, dType, npabs(ui/diMax), a, b, c, d)) return ui
def read_wav(filename, channel=0): a = read_wav0(filename) samplerate, data = a try: data = map(lambda x: x[channel], data) except: pass m = npmax(npabs(data), axis=0) print '# absolute sample maximum in percent' print 'SAMPLE_MAXIMUM=%s' % int((m * 100 + 32768 / 2) / 32768.0) if m > 0: data /= m return samplerate, data
def stetson_jindex(ftimes, fmags, ferrs, weightbytimediff=False): '''This calculates the Stetson index for the magseries, based on consecutive pairs of observations. Based on Nicole Loncke's work for her Planets and Life certificate at Princeton. This requires finite times, mags, and errs. If weightbytimediff is True, the Stetson index for any pair of mags will be reweighted by the difference in times between them using the scheme in Fruth+ 2012 and Zhange+ 2003 (as seen in Sokolovsky+ 2017). w_i = exp(- (t_i+1 - t_i)/ delta_t ) ''' ndet = len(fmags) if ndet > 9: # get the median and ndet medmag = npmedian(fmags) # get the stetson index elements delta_prefactor = (ndet / (ndet - 1)) sigma_i = delta_prefactor * (fmags - medmag) / ferrs sigma_j = nproll(sigma_i, 1) # Nicole's clever trick to advance indices # by 1 and do x_i*x_(i+1) if weightbytimediff: time_i = ftimes time_j = nproll(ftimes, 1) difft = npdiff(ftimes) deltat = npmedian(difft) weights_i = npexp(-difft / deltat) products = (weights_i * sigma_i[1:] * sigma_j[1:]) else: # ignore first elem since it's actually x_0*x_n products = (sigma_i * sigma_j)[1:] stetsonj = (npsum(npsign(products) * npsqrt(npabs(products)))) / ndet return stetsonj else: LOGERROR('not enough detections in this magseries ' 'to calculate stetson J index') return npnan
def _l1_quantile(batch, quantile=50, size=32): """Compute differences between subsequent frames in a batch. """ bsize = batch.bsize msize = bsize + 1 assert msize <= batch.get_frames().shape[0] simg = zeros((msize, size, size, 3)) for iran in range(msize): fsmall = resize(batch.get_frames()[iran, :, :, :], (size, size)) fsmall_hsv = cvtColor(fsmall, COLOR_RGB2HSV) simg[iran, :, :, :] = fsmall_hsv norm = simg[slice(0, bsize), :, :, :] - simg[slice(1, bsize + 1), :, :, :] return percentile(npabs(norm), q=quantile, axis=(1, 2, 3))
def stetson_kindex(fmags, ferrs): '''This calculates the Stetson K index (a robust measure of the kurtosis). Parameters ---------- fmags,ferrs : np.array The input mag/flux time-series to process. Must have no non-finite elems. Returns ------- float The Stetson K variability index. ''' # use a fill in value for the errors if they're none if ferrs is None: ferrs = npfull_like(fmags, 0.005) ndet = len(fmags) if ndet > 9: # get the median and ndet medmag = npmedian(fmags) # get the stetson index elements delta_prefactor = (ndet/(ndet - 1)) sigma_i = delta_prefactor*(fmags - medmag)/ferrs stetsonk = ( npsum(npabs(sigma_i))/(npsqrt(npsum(sigma_i*sigma_i))) * (ndet**(-0.5)) ) return stetsonk else: LOGERROR('not enough detections in this magseries ' 'to calculate stetson K index') return npnan
def find_lines(self, bsax, num_bins=1, thresh=5): #self.flfig,self.flfig = plt.subplots(nrows=1,ncols=1) if len(self.flat_signal) > 15: wl = 15 else: wl = len(self.flat_signal) - 1 smoothed = savgol_filter(self.flat_signal, window_length=wl, polyorder=1, axis=0) with catch_warnings(): simplefilter("ignore", category=RuntimeWarning) noise_levels = std(self.flat_signal - smoothed, axis=0) abs_scatter = npabs(-log(self.flat_signal)) self.mass_channel = arange(abs_scatter.shape[1]) + 1 self.mass_channel_name = (arange(abs_scatter.shape[1]) + 1).astype('|S10') if self.bin_set: for i in range(self.num_bins): self.mass_channel_name[-(self.num_bins - i)] = "Bin %i" % (i + 1) self.peak_counts = zeros(abs_scatter.shape[1]) mean_level = mean(self.raw_signal, axis=0) for i in range(abs_scatter.shape[1]): if mean_level[i] > 50: ww = where(abs_scatter[:, i] > (noise_levels[i] * thresh))[0] self.peak_counts[i] = len(ww) bsax.ax.bar(self.mass_channel, self.peak_counts, width=1) #self.flfig2.plot(self.wls,self.flat_signal[:,peak-1],label=peak) #self.flfig2.plot(self.wls,self.flat_signal[:,peak-1]-smoothed[:,peak-1],label="Noise") #self.flfig2.set_xlabel("Wavelength (nm)") #self.flfig2.set_ylabel("Ion Yield") #self.flfig2.legend() bsax.ax.set_xlabel("Mass", fontsize=font) bsax.ax.set_ylabel("Channels", fontsize=font) bsax.ax.minorticks_on()
def laser_corr(wls, signal, signal_err, pow_table="Power_Master_Full.txt"): wls_temp = [] i_temp = [] for i, w in enumerate(wls): if (w not in wls_temp): wls_temp.append(w) i_temp.append(int(i)) wls_pow = wls[i_temp] signal_pow = signal[i_temp] signal_err_pow = signal_err[i_temp] a = Power(array(wls_pow), signal_pow, signal_err_pow, pow_table) signal_pow = exp(-a.depletion) signal_err_pow = npabs(exp(-a.depletion) * a.depletion_error) return wls_pow, signal_pow, signal_err_pow
def random_walk(self, d=None, plot_h=None) -> None: '''Let all population walk randomly''' walk_left = self.move_per_day.copy() # Every day, people start from home pos = self.home.copy() # Some travel less, some more while npany(walk_left): walk_left -= 1 # All randomly move an edge-length pos += nparray(npround( (nprandom.random(size=pos.shape) * 2 - 1) * self.rms_v.T[:, None]) * (walk_left != 0).T[:, None], dtype=pos.dtype) # Can't jump beyond boundary # So, reflect exploration # pos = pos.clip(min=0, max=self.p_max-1) pos = nparray(npnot(pos > (self.p_max-1)) * npabs(pos), dtype=pos.dtype)\ + nparray((pos > (self.p_max-1)) * (2 * (self.p_max - 1) - pos), dtype=pos.dtype) for indiv in range(self.pop_size): # TODO: A ufunc or async map would have been faster if walk_left[indiv]: self.calc_exposure(indiv, pos) if plot_h.contam_dots: strain_persist = self.strain_types[-1].persistence host_types = [] host_types.append( (pos * (npnot(self.active[:, None]) * self.susceptible[:, None] > self.resist_def)).tolist()) host_types.append((pos * self.active[:, None]).tolist()) host_types.append(( pos * (npnot(self.active[:, None]) * (self.susceptible[:, None] <= self.resist_def))).tolist()) pathn_pers = [] for pers in range(int(strain_persist))[::-1]: pathn_pers.append( npnonzero(self.space_contam == (pers + 1))) plot_h.update_contam(host_types, pathn_pers) return
def uniaxial_stress_limit(X): """ Evaluate stress limit states for uniaxial tension. Dimensionality of problem is inferred from size of X. Usage g_stress = uniaxial_stress_limit(X) Arguments X = array of composite laminate properties and loading = [E1, E2, nu12, G12, theta, t, ... sig_11_t, sig_22_t, sig_11_c, sig_22_c, sig_12_s, # for i = 1 . . . E1, E2, nu12, G12, theta, t, ... sig_11_t, sig_22_t, sig_11_c, sig_22_c, sig_12_s, # for i = k Nx] Returns g_stress = array of limit state values = [g_11_t_1 g_22_t_1 g_11_c_1 g_22_c_1 g_s_12_1, . . . g_11_t_k g_22_t_k g_11_c_k g_22_c_k g_s_12_k] @pre ((len(X) - 1) % 11) == 0 """ ## Pre-process inputs k = int((len(X) - 1) / 11) Y = reshape(array(X[:-1]), (k, 11)) ## Unpack inputs Nx = X[-1] Param = Y[:, 0:4] # [E1, E2, nu12, G12] Theta = Y[:, 4] T = Y[:, 5] Sigma_max = Y[:, 6:11] # [sig_11_t, sig_22_t, sig_11_c, sig_22_c, sig_12_s] ## Evaluate stress [\sigma_11, \sigma_22, \sigma_12]_i Stresses = Nx * uniaxial_stresses(Param, Theta, T) ## Construct limit state g_limit = zeros((k, 5)) g_limit[:, (0,1)] = +1 - Stresses[:, (0,1)] / Sigma_max[:, (0,1)] g_limit[:, (2,3)] = +1 + Stresses[:, (0,1)] / Sigma_max[:, (2,3)] g_limit[:, 4] = +1 - npabs(Stresses[:, 2]) / Sigma_max[:, 4] return g_limit.flatten()
def _integrate_acc(acc, dt, still_at_end): """ Double integrate acceleration along 1 axis (ie 1D) to get velocity and position Parameters ---------- acc : numpy.ndarray (N, ) array of acceleration values to integrate dt : float Time difference between samples of acceleration in seconds. still_at_end : bool Whether or not the acceleration ends with a still period. Determines how drift is mitigated. Returns ------- vel : numpy.ndarray (N, ) array of velocities pos : numpy.ndarray (N, ) array of positions """ x = arange(acc.size) # integrate and drift mitigate if not still_at_end: # fc = butter(1, [2 * 0.1 * dt, 2 * 5 * dt], btype='band') # vel = cumtrapz(filtfilt(fc[0], fc[1], acc), dx=dt, initial=0) vel = detrend(cumtrapz(acc, dx=dt, initial=0)) if npabs(vel[0]) > 0.05: # if too far away from zero vel -= vel[ 0] # reset the beginning back to 0, the integration always starts with stillness else: vel_dr = cumtrapz(acc, dx=dt, initial=0) vel = vel_dr - ( ((vel_dr[-1] - vel_dr[0]) / (x[-1] - x[0])) * x) # no intercept # integrate the velocity to get position pos = cumtrapz(vel, dx=dt, initial=0) return vel, pos
def bins_and_hist(a, b, w, size_hist, sigs, bin_size=11): bins = logspace(log10(a[w].min()), log10(a[w].max()), num=bin_size) a_bin = [0.5*(bins[i-1]+bins[i]) for i in range(1, len(bins))] b_bin = [median((b[w])[(a[w] <= bins[i]) & (a[w] > bins[i-1])]) for i in range(1, len(bins))] e_b_bin = [std((b[w])[(a[w] <= bins[i]) & (a[w] > bins[i-1])]) for i in range(1, len(bins))] x_bin, y_bin = linspace(log10(a[w]).min(), log10(a[w]).max(), num=size_hist),\ linspace(log10(b[w]).min(), log10(b[w]).max(), num=size_hist) H, xe, ye = histogram2d(log10(a[w]), log10(b[w]), bins=(x_bin, y_bin)) H_sorted, lev = sort(H.flatten())[::-1], [] for i in range(len(sigs)): lev.append(H_sorted[argmin(npabs(cumsum(H_sorted)-H.sum()/100.*sigs[i]))]) lev = array(lev) lev[lev < 1.5] = 1. x, y = linspace(log10(a[w]).min(), log10(a[w]).max(), num=H.shape[0]), \ linspace(log10(b[w]).min(), log10(b[w]).max(), num=H.shape[1]) return x, y, H, log10(lev[::-1]), a_bin, b_bin, e_b_bin
def I(t): i = (npabs(times - t)).argmin() return rates[i]
def plot_angular_momentum_size_velocity(): size = 50000 # Planck parameters h, Om, OL = 0.677, 0.31, 0.69 ''' # uniform halo mass function mhalo = array(10. ** uniform(10.5, 13.5, size), dtype=float32) # read halo mass-function # mhf = genfromtxt('mVector_PLANCK-SMT_11-13.txt') # mhf = genfromtxt('mVector_PLANCK-SMT_10.5-13.txt') # mhf = genfromtxt('mVector_PLANCK-SMT_11-14.5.txt') # mhalo = array(choice(mhf[:, 0], p=mhf[:, 5]/sum(mhf[:, 5]), size=size), dtype=float32) # mhalo, _ = sample_mf(size, 11., Mmax=15.5, hmf_model=Tinker10) Ms = Mstar(0., mode='high', model='L12') mstar = [] print("generating mstar...") for m in mhalo: mstar.append(Ms(m)) mstar = array(mstar) # mstar = Mstar_vU(mhalo, mode='def') ''' mstar = array(10. ** uniform(9.35, 11., size), dtype=float32) # mstar = array(10. ** uniform(9., 11., size), dtype=float32) mhalo = Mstar_D10_ltg(mstar) # ms_lowlim, ms_highlim = 7., 12.# 11.75 # mhalo = mhalo[(log10(array(mstar)) > ms_lowlim) & (log10(array(mstar)) < ms_highlim)] # mstar = mstar[(log10(array(mstar)) > ms_lowlim) & (log10(array(mstar)) < ms_highlim)] # DM halo spin parameter distribution # from Maccio', Dutton & van den Bosch (2008) lambda_halo = 10. ** normal(-1.466, 0.253, len(mhalo)) bt = get_bt_distribution_none(mstar) # bt = get_bt_distribution_from_SDSS(mstar) # bt = get_bt_distribution_from_meert14(mstar) # bt, mstar, mhalo, lambda_halo = get_bt_distribution_from_lambda(mstar, mhalo, lambda_halo) # Dutton & Maccio' (2014) # b = -0.101 + 0.026 * array([0., 1.]) # a = 0.52 + (0.905 - 0.52) * exp(-0.617 * array([0., 1.]) ** 1.21) b = -0.097 + 0.024 * array([0., 1.]) a = 0.537 + (1.025 - 0.537) * exp(-0.718 * array([0., 1.]) ** 1.08) # scatter 0.11dex cvir = [] print ("generating concentrations...") for m in mhalo: cvir.append(10. ** normal(a[0] + b[0] * log10(m / (1e12 / h)), 0.11, 1)[0]) cvir = array(cvir) # Circular velocity for NFW haloes G = 4.302e-3 * 1e-6 # Mpc Mo^-1 (km/s)^2 f = lambda x: log(1.+asarray(x)) - asarray(x)/(1.+asarray(x)) rho_c = 3. * (h * 100.)**2 / (8. * pi * G) # Bryan & Norman (1998) Oz = lambda z: Om * (1+z)**3 / (Om * (1.+z)**3+OL) Delta_c = lambda z: 18. * pi**2 + 82. * (Oz(z) - 1.) - 39. * (Oz(z) - 1.)**2 rho_hat = 4. / 3. * pi * Delta_c(0.) * rho_c # 0.6 factor to account for different normalization in SHMR vc = sqrt(G * f(2.15) / f(cvir) * cvir / 2.15 * pow(rho_hat, 1./3.)) * power(mhalo / h, 1./3.) for i in range(len(vc)): vc[i] *= .6*vrot_vc_P12(vc[i]) # print "%e %e" % (f(10.) * 2.15 / (f(2.15) * 10.), sqrt(2. * f(10.) * 2.15 / (f(2.15) * 10.))) # Kravtsov 2013 # rho_200 = 4. / 3. * pi * 200. * rho_c r200 = 1e3 * power(mhalo / rho_hat, 1./3.) rs = [] for r in r200: rs.append(10. ** normal(log10(0.015 * r), 0.25, 1)[0]) rs = array(rs) js = rs * vc # jhalo - mhalo fe = cvir / 2. * (1.-1./power(1.+cvir, 2) - 2. * log(1.+cvir)/(1.+cvir)) / power(cvir/(1.+cvir)-log(1.+cvir), 2) # fe = f(2.15) / f(cvir) * cvir / 2.15 j_halo = sqrt(2. / fe) * lambda_halo * sqrt(G * 1e3 * mhalo / r200) * r200 # j_halo da Romanowsky & Fall (2012) eq. (14) # j_halo = 4.23e4 * lambda_halo * power(mhalo / 1e12, 2./3.) """ ------------- Definition of B/T cuts """ # bt_discs, bt_spirals, bt_ltg, bt_lents, bt_ells = 0.1, 0.2, 0.5, 0.8, 0.95 bt_discs, bt_spirals, bt_ltg, bt_lents, bt_ells = 0.1, 0.25, 0.5, 0.65, 0.8 # fj(Mhalo) def jstar_FR(mass, bf): if (bf >= 0.) & (bf <= bt_discs): # Sc j0, alpha, s = 3.29, 0.55, 0.18 elif (bf > bt_discs) & (bf <= bt_spirals): # Sb j0, alpha, s = 3.21, 0.68, 0.15 elif (bf > bt_spirals) & (bf <= bt_ltg): # Sa j0, alpha, s = 3.02, 0.64, 0.12 elif (bf > bt_ltg) & (bf <= bt_lents): # S0 j0, alpha, s = 3.05, 0.8, 0.22 elif (bf > bt_lents) & (bf <= bt_ells): # fE j0, alpha, s = 2.875, 0.6, 0.2 elif (bf > bt_ells) & (bf <= 1.): # sE j0, alpha, s = 2.73, 0.6, 0.2 elif bf == -1: j0, alpha, s = 3. , 0.67, 0.22 else: print (bf) raise ValueError("Problem in bt not in ]0,1]") return 10. ** normal(j0+alpha*(mass-11.), s, 1)[0] # fitting function to jb/jd estimated from Romanowsky & Fall (2012) data # jb/jd = 0.025 + B/T^2 # jd = j_star / (1 + (jb/jd-1)B/T) # --> here jdisc = jstar / fb_func jb_over_jd_fit = lambda fb: 0.025 + fb**2 fb_func = lambda fb: 1. + (jb_over_jd_fit(fb) - 1.) * fb jstar, jdisc, jbulge = [], [], [] print ("generating jstar...") for i in range(len(mstar)): jstar.append(jstar_FR(log10(mstar[i]), bt[i])) jdisc.append(jstar[i] / fb_func(bt[i])) jbulge.append(jdisc[i] * jb_over_jd_fit(bt[i])) jstar, jdisc, jbulge = array(jstar), array(jdisc), array(jbulge) fj = jstar / j_halo # jstar_FR(log10(0.158 * mhalo)) / j_halo # Tully Fisher Mstar_TF = lambda t: 10.**(-0.61+4.93*asarray(t)) # McGaugh & Schombert (2015), sTF @ 3.6um vstar_TF = lambda t: 10.**(0.61/4.93 + 1./4.93*asarray(t)) # Mstar_TF = lambda t: 10.**(1.49+4.09*asarray(t)) # McGaugh & Schombert (2015), Baryonic-TF @ 3.6um # vstar_TF = lambda t: 10.**(-1.49/4.09 + 1./4.09*asarray(t)) sstar_FJ = lambda t: 10.**(2.054 + 0.286 * asarray(t-10.)) Mstar_FJ = lambda t: 10.**(-2.054/0.286 + 10. + 1./0.286 * asarray(t)) # Shen et al. (2003) mass_size_etg = lambda mass: -5.54061 + 0.56*log10(mass) mass_size_ltg = lambda mass: -1. + 0.14*log10(mass) + 0.25 * log10(1. + mass/3.98e10) # Lange et al. (2015) (Tables 2-3, g-i colour cut, r-band Re) # mass_size_etg = lambda mass: log10(8.25e-5) + 0.44*log10(mass) # mass_size_ltg = lambda mass: log10(13.98e-3) + 0.25*log10(mass) # mass_size_ltg = lambda mass: -1. + 0.16*log10(mass) + 0.65 * log10(1. + mass/17.1e10) # Cappellari et al. 13, sigma from mass plane sigma_MP = lambda mass: 10.**(0.5*(log10(mass)-10.6-log10(10.**mass_size_etg(mass)/2.))+log10(130.)) factor = 1.1 # V_circ(R_50) := factor * sigma(R_50) ''' plt.figure() js[bt<bt_spirals] = 10.**mass_size_ltg(mstar[bt<bt_spirals]) * vstar_TF(log10(mstar[bt<bt_spirals])) js[bt>bt_ells] = 10.**mass_size_etg(mstar[bt>bt_ells]) * 0.33 * factor * sstar_FJ(log10(mstar[bt>bt_ells])) # js[bt>bt_ells] = 10.**mass_size_etg(mstar[bt>bt_ells]) * 0.33 * factor * sigma_MP(mstar[bt>bt_ells]) plt.plot(log10(mstar[bt<bt_spirals]), log10(js[bt<bt_spirals]), 'b.', alpha=0.1) plt.plot(log10(mstar[bt>bt_ells]), log10(js[bt>bt_ells]), 'r.', alpha=0.1) ms_rf12_discs_str = "11.06 9.84 9.23 10.46 11.31 11.50 11.34 10.13 11.42 10.62 10.61 10.74 11.03 11.31 10.93 11.32 10.01 11.34 10.67 11.33 10.81 10.83 10.65 11.31 11.23 11.10 10.32 10.74 11.13 10.95 11.12 10.61 8.62 10.49 11.37 10.39 9.86 11.07 10.22 9.43 11.08 11.42 10.43 10.03 11.56 9.58 10.73 10.96 10.76 10.54 11.10 11.31 11.11 11.50 10.84 11.07 11.34 10.47 11.23 11.34 10.86 10.44 11.51 9.14 11.74 10.82 11.26" jt_rf12_discs_str = "2230 2290 770 790 190 260 480 550 1480 1930 4270 4280 2070 2230 340 360 1810 2900 630 820 1360 1450 990 1090 1210 1720 2580 3370 2230 2280 3150 3380 470 500 2220 2300 580 760 1110 1300 1250 1430 1520 1620 1030 1210 1400 2020 2070 2190 1070 1180 460 480 900 1000 2100 2380 920 1070 1680 2190 550 600 120 120 990 1040 4250 4470 1320 1350 160 180 930 1070 550 610 580 610 2240 2410 1060 1850 370 400 300 320 2030 2380 130 140 1220 1460 790 850 180 360 260 330 1650 1740 2440 2620 1280 1590 1430 1560 1300 1650 590 1030 1450 1580 570 580 2050 2150 2540 2780 710 830 960 1040 2360 2890 70 190 7560 8380 2010 2090 3140 3350" ms_rf12_ell_str = "10.97 11.94 10.52 11.13 9.79 10.68 11.34 10.75 11.05 11.26 11.66 10.18 9.97 10.35 10.79 10.76 10.55 11.33 10.26 11.04 10.24 10.95 10.50 10.96 10.09" jt_rf12_ell_str = "1270 3640 400 240 25 70 2330 1040 3100 630 680 7 130 680 1000 610 240 3360 270 1150 240 1700 160 210 110" ms_dvdb12_str = "9.484 9.631 9.784 9.941 10.103 10.270 10.442 10.618 10.796 10.979 11.164 11.352" js_dvdb12_str = "2.417 2.482 2.550 2.621 2.697 2.779 2.868 2.960 3.050 3.154 3.261 3.391" ms_rf12_discs = [float(s) for s in ms_rf12_discs_str.split()] jt_rf12_discs = [float(s) for s in jt_rf12_discs_str.split()] ms_rf12_ell = [float(s) for s in ms_rf12_ell_str.split()] jt_rf12_ell = [float(s) for s in jt_rf12_ell_str.split()] ms_dvdb12 = [float(s) for s in ms_dvdb12_str.split()] js_dvdb12 = [float(s) for s in js_dvdb12_str.split()] plt.plot(ms_rf12_discs, log10(jt_rf12_discs[::2]), 'bs') plt.plot(ms_rf12_ell, log10(jt_rf12_ell), 'rs') plt.plot(ms_dvdb12, js_dvdb12, 'k^', markersize=20) plt.plot(linspace(9,12), 3.18+0.52*(linspace(9,12)-11.),'k--') plt.xlabel(r"$\log\rm\,M_\ast/M_\odot$", fontsize=16) plt.ylabel(r"$\log\rm\,j_\ast/km\,s^{-1}\,kpc$", fontsize=16) # plt.savefig('invEx_Mstar-jstar_wRF12.png', bbox_inches='tight') fig,ax = plt.subplots() mhalo_bin, fj_bin, e_fj_bin = median_bins(log10(mhalo), log10(js / j_halo), bt<bt_spirals, 19) plt.errorbar(mhalo_bin, fj_bin, yerr=e_fj_bin-fj_bin, fmt='o-', color='b') # plt.plot(log10(mhalo[bt<bt_spirals]), log10(js/j_halo)[bt<bt_spirals], 'b.', alpha=0.1) mhalo_bin, fj_bin, e_fj_bin = median_bins(log10(mhalo), log10(js / j_halo), bt>bt_ells, 17) plt.errorbar(mhalo_bin, fj_bin, yerr=e_fj_bin-fj_bin, fmt='o-', color='r') # plt.plot(log10(mhalo[bt>bt_ells]), log10(js/j_halo)[bt>bt_ells], 'r.', alpha=0.1) # mhalo_bin, fj_bin, e_fj_bin = median_bins(log10(mhalo), log10(js / j_halo), bt>0, 19) # plt.errorbar(mhalo_bin, fj_bin, yerr=e_fj_bin-fj_bin, fmt='--', color='k') ax.set_xlabel(r"$\log\rm\,M_h/M_\odot$", fontsize=16) ax.set_ylabel(r"$\log\rm\,f_j(M_h)\equiv j_\ast / j_h$", fontsize=16) # plt.savefig('invEx_Mhalo-fj.png', bbox_inches='tight') plt.show() print (j[2]) ''' # Re = jdisc / (vstar_TF(log10(mstar))) # Re[bt > 0.8] = jbulge[bt > 0.8] / (0.7 * vstar_TF(log10(mstar[bt > 0.8]))) # Re[bt > 0.8] = jbulge[bt > 0.8] / (10.**(-1.06 + 0.32*log10(mstar[bt > 0.8]))) Re = jstar / vc # vstar_TF(log10(mstar)) Re[bt > bt_ells] = jstar[bt > bt_ells] / (vc[bt > bt_ells] / factor) # sstar_FJ(log10(mstar[bt > bt_ltg]))) # Mo, Mao & White (1998) # lambda_halo_prime = lambda_halo * jdisc / j_halo # fj lambda_halo_prime = lambda_halo * fj # fj cvir = array(cvir) fac_sis_nfw = 1. / sqrt(cvir / 2. * (1.-1./power(1.+cvir, 2) - 2. * log(1. + cvir) / (1. + cvir)) / power(cvir / (1. + cvir) - log(1. + cvir), 2)) # md = mstar / mhalo # fac_sis_nfw *= pow(lambda_halo_prime / 0.1, -0.0 + 2.71 * md + 0.0047 / lambda_halo_prime) * \ # (1-3.*md+5.2*md*md) * (1-0.019*cvir * 0.00025*cvir*cvir + 0.52 / cvir) Rd = 1. / sqrt(2.) * lambda_halo_prime * r200 * fac_sis_nfw # Compute dark matter fractions assuming NFW profile and that Re=stellar half mass radius rs = r200 / cvir rho_0 = mhalo / rs**3 / 4. / pi / (log(1.+cvir)-cvir/(1.+cvir)) mh_re = 4. * pi * rho_0 * rs**3 * (log((rs+Re)/rs) - Re / (Re+rs)) fDM = mh_re / (mstar / 2. + mh_re) w = bt < bt_spirals Ms_nos = Mstar(0., mode='high', model='L12', no_scatter=True) ms_scatter = (log10(mstar) - log10([Ms_nos(mh) for mh in mhalo])) # / log10([Ms_nos(mh) for mh in mhalo]) x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(mhalo, fj, w, 20, [68., 90.], bin_size=30) fj_scatter = (log10(array(fj)) - interp(log10(mhalo), log10(mhalo_bin), log10(fj_bin))) # / \ # interp(log10(mhalo), log10(mhalo_bin), log10(fj_bin)) ''' corner_data = vstack([log10(mstar)[w], log10(mstar/mhalo)[w], log10(fj)[w], log10(fDM)[w], log10(Re)[w], log10(vc)[w], log10(jstar)[w], log10(j_halo)[w], ms_scatter[w], fj_scatter[w]]).T print(corner_data.shape) corner.corner(corner_data, labels=[r"$\rm\log\,M_\ast/M_\odot$", r"$\rm\log\,M_\ast/M_h$", r"$\rm\log\,f_j$", r"$\rm \log\,f_{DM}$", r"$\rm\log\,R_e/kpc$", r"$\rm\log\,v_c/km/s$", r"$\rm \log\,j_\ast$", r"$\rm \log\,j_h$", r"$\sigma_{\log M_\ast-\log M_h}$", r"$\sigma_{\log f_j-\log M_h}$"], quantiles=[0.16, 0.5, 0.84], show_titles=True, title_kwargs={"fontsize": 12}, range=[.99, .99, .99, .99, .99, .99, .99, .99, .95, .95], color='b') plt.savefig("corner_plot.pdf", bbox_inches='tight') print (j[2]) ''' """ HDF5 file output """ f = h5py.File("jstar-jhalo.hdf5", 'w') group = f.create_group("columns") dset_lambda = group.create_dataset("Lambda_Halo", data=lambda_halo) dset_mhalo = group.create_dataset("Halo_Mass", data=mhalo) dset_mstar = group.create_dataset("Galaxy_Mass", data=mstar) dset_jhalo = group.create_dataset("Halo_Specific_Angular_Momentum", data=j_halo) dset_jstar = group.create_dataset("Galaxy_Specific_Angular_Momentum", data=jstar) dset_cvir = group.create_dataset("Halo_Concentration", data=cvir) dset_fj = group.create_dataset("Retained_fraction_of_j", data=fj) dset_bt = group.create_dataset("Bulge_Fraction", data=bt) dset_vc = group.create_dataset("Circular_Velocity", data=vc) dset_vc = group.create_dataset("Velocity_Dispersion", data=vc/1.1) dset_Re = group.create_dataset("Effective_Radius", data=Re) dset_Rd = group.create_dataset("Disc_Scale_Radius", data=Rd) """ Plots """ # plt.plot(log10(mstar * bt), log10(jbulge), 'r.', alpha=0.005) # plt.plot(log10(mstar * bt * (1./bt - 1.)), log10(jdisc), 'b.', alpha=0.005) # plt.plot(linspace(9, 12), 3.28+0.67*(linspace(9, 12)-11.), 'k-', lw=3) # plt.plot(linspace(9, 12), 2.75+0.67*(linspace(9, 12)-11.), 'k--', lw=3) # plt.show() size_hist = 20 sigs = [68., 90.] save_plots = False ''' ----------- Mstar-Mhalo ''' # Dutton et al. 2010 relations d10_msmh_ltg = lambda x: 10.**1.61 * (asarray(x) / 10.**10.4)**-0.5 * (0.5 + 0.5 * (asarray(x) / 10.**10.4))**0.5 d10_msmh_etg = lambda x: 10.**1.97 * (asarray(x) / 10.**10.8)**-0.15 * (0.5 + 0.5 * (asarray(x) / 10.**10.8)**2)**0.5 d10_ms_ltg = logspace(9.35, 11) d10_ms_etg = logspace(9.85, 11.4) fig = plt.figure() ax = fig.add_subplot(211) ax.set_ylabel(r"$\log\rm\,M_\ast/M_\odot$", fontsize=16) # ax.xlabel(r"$\log\rm\,M_h/M_\odot$", fontsize=16) ax.axes.get_xaxis().set_visible(False) ax.set_ylim([8, 11.75]) ax.set_xlim([10.75, 13.5]) if len(bt[bt<0])>0: x, y, H, lev, _, _, _ = bins_and_hist(mhalo/h, mstar/h**2, bt<0, 30, sigs) else: x, y, H, lev, _, _, _ = bins_and_hist(mhalo/h, mstar/h**2, bt > 0, 30, sigs) # plt.plot(log10(d10_msmh_ltg(d10_ms_ltg) * d10_ms_ltg), log10(d10_ms_ltg), 'b-') # plt.plot(log10(d10_msmh_etg(d10_ms_etg) * d10_ms_etg), log10(d10_ms_etg), 'r-') plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Greys'), alpha=0.75) plt.xlim() ax2 = fig.add_subplot(212, sharex=ax) ax2.set_ylabel(r"$\log\rm\,M_\ast/M_\odot$", fontsize=16) ax2.set_xlabel(r"$\log\rm\,M_h/M_\odot$", fontsize=16) ax2.set_xlim([10.75, 13.5]) fig.subplots_adjust(hspace=0) if len(bt[bt<0])>0: x, y, H, lev, _, _, _ = bins_and_hist(mhalo, mstar/mhalo, bt<0, 30, sigs) else: x, y, H, lev, _, _, _ = bins_and_hist(mhalo, mstar/mhalo, bt > 0, 30, sigs) plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Greys'), alpha=0.75) if save_plots: plt.savefig('SHMR_all_ref.pdf', bbox_inches='tight') ''' ----------- lambda-Mhalo ''' fig = plt.figure() plt.xlabel(r"$\log\rm\,M_h/M_\odot$", fontsize=16) plt.ylabel(r"$\log\rm\lambda_{halo}$", fontsize=16) plt.plot(log10(mhalo), log10(lambda_halo), 'b.', alpha=0.0025) if len(bt[bt<0])>0: x, y, H, lev, _, _, _ = bins_and_hist(mhalo, lambda_halo, bt<0, size_hist, sigs) else: x, y, H, lev, _, _, _ = bins_and_hist(mhalo, lambda_halo, bt > 0, size_hist, sigs) plt.contour(x, y, log10(H).T, levels=lev, colors='#151AB0') ''' ----------- j_halo - M_halo ''' fig = plt.figure() ax = fig.add_subplot(111) plt.xlabel(r"$\log\rm\,M_h/M_\odot$", fontsize=16) plt.ylabel(r"$\log\rm\,j_h/km\,s^{-1}\,kpc$", fontsize=16) if len(bt[bt<0])>0: x, y, H, lev, _, _, _ = bins_and_hist(mhalo, j_halo, bt<0, size_hist, sigs) else: x, y, H, lev, _, _, _ = bins_and_hist(mhalo, j_halo, bt > 0, size_hist, sigs) # ax.contour(x, y, log10(H).T, levels=lev, colors='#151AB0') ax.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Greys'), alpha=0.75) ax.plot(linspace(10.75, 13), 3.28+0.67*(linspace(10.75, 13)-11.), 'k--', lw=3) # plt.plot([12.5, log10(0.158 * 10.**12.5)], [3.505, 3.7481], 'm-', lw=3) # ax.arrow(12.5, 3.505, -12.5+log10(0.08 * 10.**12.5), -3.505+3.28+0.67*(log10(0.08 * 10.**12.5)-11.)-0.045, # head_width=0.075, head_length=0.05, fc='m', ec='m', lw=3) ax.arrow(12., 3.1704, -12.+log10(0.08 * 10.**12.), -3.1704+3.28+0.67*(log10(0.08 * 10.**12.)-11.)-0.045, head_width=0.075, head_length=0.05, fc='m', ec='m', lw=3) ax.text(11.15, 4., r"$f_{\rm baryon}\simeq 0.16$"+"\n"+r"$M_\ast=M_{\rm bar}/2$", color='m', fontsize=18) if save_plots: plt.savefig('jh-Mh_all_ref.pdf', bbox_inches='tight') ''' ----------- fj(M_halo) ''' fig = plt.figure() # ax = fig.add_subplot(111) ax = host_subplot(111) ax2 = ax.twiny() ax.set_xlabel(r"$\log\rm\,M_h/M_\odot$", fontsize=16) ax.set_ylabel(r"$\log\rm\,f_j(M_h)\equiv j_\ast / j_h$", fontsize=16) ax2.set_xlabel(r"$\log\,M_\ast/M_\odot$", fontsize=16) if len(bt[bt<0])>0: x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(mhalo, fj, bt<0, size_hist, sigs) plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Greys'), alpha=0.75) # ------ Median relation x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(mhalo, fj, bt<0, size_hist, sigs, bin_size=15) plt.errorbar(log10(mhalo_bin), log10(fj_bin), yerr=abs(log10(fj_bin)-log10(e_fj_bin)), c='k', fmt='o') ax.set_xlim(10.5,13.5) ax2.set_xlim(10.5,13.5) mhx = logspace(10.5, 13.5, num=100) # Ax2 tick location in stellar mass mstar_tick_loc = arange(8.0, 11.5, 0.5) else: ax.text(0.1, 0.9, r"$\rm late-types$", fontsize=18, color='#151AB0', transform=ax.transAxes) # ax.text(0.1, 0.85, r"$\rm Sa-S0$", fontsize=18, color='#009603', transform=ax.transAxes) ax.text(0.1, 0.825, r"$\rm early-types$", fontsize=18, color='#BD000D', transform=ax.transAxes) # spirals w = bt < bt_spirals # plt.plot(log10(mhalo[w]), log10(fj[w]), 'b.', alpha=0.0025) x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(mhalo, fj, w, size_hist, sigs) # plt.errorbar(log10(mhalo_bin), log10(fj_bin), yerr=abs(log10(fj_bin)-log10(e_fj_bin)), c='b', fmt='o') # plt.contour(x, y, log10(H).T, levels=lev, colors='#151AB0') ax.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Blues'), alpha=0.75) ''' # Sa and lenticulars w = (bt >= bt_spirals) & (bt < bt_ells) # plt.plot(log10(mhalo[w]), log10(fj[w]), 'b.', alpha=0.0025) x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(mhalo, fj, w, size_hist, sigs) # plt.errorbar(log10(mhalo_bin), log10(fj_bin), yerr=abs(log10(fj_bin)-log10(e_fj_bin)), c='g', fmt='o') # plt.contour(x, y, log10(H).T, levels=lev, colors='#AB9700') plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Greens'), alpha=0.75) ''' # ellipticals w = bt > bt_ells # plt.plot(log10(mhalo[w]), log10(fj[w]), 'r.', alpha=0.0025) x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(mhalo, fj, w, size_hist, sigs) # plt.errorbar(log10(mhalo_bin), log10(fj_bin), yerr=abs(log10(fj_bin)-log10(e_fj_bin)), c='r', fmt='o') # plt.contour(x, y, log10(H).T, levels=lev, colors='#BD000D') ax.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Reds'), alpha=0.75) # ------ Median relation # x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(mhalo, fj, bt > 0, size_hist, sigs, bin_size=30) x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(mhalo, fj, (bt < bt_spirals), size_hist, sigs, bin_size=30) # plt.errorbar(log10(mhalo_bin), log10(fj_bin), yerr=abs(log10(fj_bin)-log10(e_fj_bin)), c='b', fmt='o') ax.plot(log10(mhalo_bin)[:8], log10(fj_bin)[:8], 'bo-') x, y, H, lev, mhalo_bin, fj_bin, e_fj_bin = bins_and_hist(mhalo, fj, (bt > bt_ells), size_hist, sigs, bin_size=30) # plt.errorbar(log10(mhalo_bin), log10(fj_bin), yerr=abs(log10(fj_bin)-log10(e_fj_bin)), c='r', fmt='s') ax.plot(log10(mhalo_bin)[2:], log10(fj_bin)[2:], 'rs-') # second x-axis with Mstar ax.set_xlim(11,13.5) ax2.set_xlim(11,13.5) mhx = logspace(11, 13.5, num=100) # Ax2 tick location in stellar mass mstar_tick_loc = arange(9, 11.5, 0.5) Ms = Mstar(0., mode='high', model='L12', no_scatter=True) ms_mhx = array([Ms(m) for m in mhx]) # Ax2 string tick definition ax2_tick_labels = ["%2.1f" % (m) for m in mstar_tick_loc] ax2_tick_loc = [] for msx in mstar_tick_loc: ax2_tick_loc.append(log10(mhx[argmin(npabs( log10(ms_mhx) - msx))])) ax2.set_xticks(ax2_tick_loc) ax2.set_xticklabels(ax2_tick_labels) if save_plots: plt.savefig('fj-Mh_BT_ref.pdf', bbox_inches='tight') # plt.savefig('fj-Mh_BT_ref.pdf', bbox_inches='tight') ''' ----------- Kravtsov ''' fig = plt.figure() plt.ylabel(r"$\log\rm\,R_e/kpc$", fontsize=16) plt.xlabel(r"$\log\rm\,r_{\rm vir}/kpc$", fontsize=16) ax = fig.add_subplot(111) ax.text(0.1, 0.9, r"$\rm late-types$", fontsize=18, color='#151AB0', transform=ax.transAxes) # ax.text(0.1, 0.85, r"$\rm Sa-S0$", fontsize=18, color='#009603', transform=ax.transAxes) ax.text(0.1, 0.825, r"$\rm early-types$", fontsize=18, color='#BD000D', transform=ax.transAxes) # ax.text(0.4, 0.15, r"$\rm Kravtsov Plot$", fontsize=20, transform=ax.transAxes) if len(bt[bt<0])>0: x, y, H, lev, _, _, _ = bins_and_hist(r200, Re, bt<0, size_hist, sigs) plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Greys'), alpha=0.75) # ------ Median relation x, y, H, lev, r200_bin, Rd_bin, e_Rd_bin = bins_and_hist(r200, Re, bt<0, size_hist, sigs, bin_size=30) plt.errorbar(log10(r200_bin), log10(Rd_bin), yerr=abs(log10(Rd_bin)-log10(e_Rd_bin)), c='k', fmt='o') else: # spirals w = bt < bt_spirals # plt.plot(log10(r200)[w], log10(Re)[w], 'b.', alpha=0.0075) x, y, H, lev, _, _, _ = bins_and_hist(r200, Re, w, size_hist, sigs) # plt.contour(x, y, log10(H).T, levels=lev, colors='#151AB0') plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Blues'), alpha=0.75) ''' # Sa and lenticulars w = (bt >= bt_spirals) & (bt < bt_ells) # plt.plot(log10(r200)[w], log10(Re)[w], 'b.', alpha=0.0075) x, y, H, lev, _, _, _ = bins_and_hist(r200, Re, w, size_hist, sigs) # plt.contour(x, y, log10(H).T, levels=lev, colors='#AB9700') plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Greens'), alpha=0.75) ''' # ellipticals w = bt > bt_ells # plt.plot(log10(r200)[w], log10(Re)[w], 'r.', alpha=0.0075) x, y, H, lev, _, _, _ = bins_and_hist(r200, Re, w, size_hist, sigs) # plt.contour(x, y, log10(H).T, levels=lev, colors='#BD000D') plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Reds'), alpha=0.75) # ------ Median relation x, y, H, lev, r200_bin, Rd_bin, e_Rd_bin = bins_and_hist(r200, Re, (bt < bt_spirals), size_hist, sigs, bin_size=30) # plt.errorbar(log10(mhalo_bin), log10(fj_bin), yerr=abs(log10(fj_bin)-log10(e_fj_bin)), c='b', fmt='o') ax.plot(log10(r200_bin)[:8], log10(Rd_bin)[:8], 'bo-') x, y, H, lev, r200_bin, Rd_bin, e_Rd_bin = bins_and_hist(r200, Re, (bt > bt_ells), size_hist, sigs, bin_size=30) # plt.errorbar(log10(mhalo_bin), log10(fj_bin), yerr=abs(log10(fj_bin)-log10(e_fj_bin)), c='r', fmt='s') ax.plot(log10(r200_bin)[2:], log10(Rd_bin)[2:], 'rs-') plt.plot(log10(linspace(125, 800)), log10(0.015 * linspace(125, 800)), 'k-', lw=2) plt.plot(log10(linspace(125, 800)), log10(0.015 * linspace(125, 800))+0.5, 'k--', lw=2) plt.plot(log10(linspace(125, 800)), log10(0.015 * linspace(125, 800))-0.5, 'k--', lw=2) plt.xlim([log10(125.), log10(800.)]) if save_plots: plt.savefig('Re-r200_BT_ref.pdf', bbox_inches='tight') ''' ----------- Mo, Mao & White Rd-r200 ''' fig = plt.figure() plt.ylabel(r"$\log\rm\,R_d/kpc$", fontsize=16) plt.xlabel(r"$\log\rm\,r_{\rm vir}/kpc$", fontsize=16) ax = fig.add_subplot(111) ax.text(0.1, 0.9, r"$\rm Sc-Sb$", fontsize=18, color='#151AB0', transform=ax.transAxes) ax.text(0.1, 0.85, r"$\rm Sa-S0$", fontsize=18, color='#009603', transform=ax.transAxes) # ax.text(0.4, 0.15, r"$\rm Mo, Mao & White Plot$", fontsize=20, transform=ax.transAxes) if len(bt[bt<0])>0: x, y, H, lev, r200_bin, Rd_bin, e_Rd_bin = bins_and_hist(r200, Rd, bt<0, size_hist, sigs) plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Greys'), alpha=0.75) # ------ Median relation x, y, H, lev, r200_bin, Rd_bin, e_Rd_bin = bins_and_hist(r200, Rd, bt<0, size_hist, sigs, bin_size=30) plt.errorbar(log10(r200_bin), log10(Rd_bin), yerr=abs(log10(Rd_bin)-log10(e_Rd_bin)), c='k', fmt='o') else: # spirals w = bt < bt_spirals # plt.plot(log10(r200[w]), log10(Rd[w]), 'b.', alpha=0.01) x, y, H, lev, r200_bin, Rd_bin, e_Rd_bin = bins_and_hist(r200, Rd, w, size_hist, sigs) # plt.errorbar(log10(r200_bin), log10(Rd_bin), yerr=abs(log10(Rd_bin)-log10(e_Rd_bin)), c='b', fmt='o') # plt.contour(x, y, log10(H).T, levels=lev, colors='#151AB0') plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Blues'), alpha=0.75) # Sa and lenticulars w = (bt >= bt_spirals) & (bt < bt_lents) # plt.plot(log10(r200[w]), log10(Rd[w]), 'y.', alpha=0.01) x, y, H, lev, r200_bin, Rd_bin, e_Rd_bin = bins_and_hist(r200, Rd, w, size_hist, sigs) # plt.errorbar(log10(r200_bin), log10(Rd_bin), yerr=abs(log10(Rd_bin)-log10(e_Rd_bin)), c='g', fmt='o') # plt.contour(x, y, log10(H).T, levels=lev, colors='#AB9700') plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Greens'), alpha=0.75) x, y, H, lev, r200_bin, Rd_bin, e_Rd_bin = bins_and_hist(r200, Rd, bt < bt_lents, size_hist, sigs, bin_size=30) plt.errorbar(log10(r200_bin), log10(Rd_bin), yerr=abs(log10(Rd_bin)-log10(e_Rd_bin)), c='k', fmt='o') plt.plot(log10(linspace(110, 800)), log10(0.0112*linspace(110, 800)), 'k-', lw=2) plt.xlim([log10(110.), log10(800.)]) if save_plots: plt.savefig('Rd-r200_ScS0_ref.pdf', bbox_inches='tight') ''' ----------- sTFR ''' fig = plt.figure() plt.ylabel(r"$\log\rm\,M_\ast/M_\odot$", fontsize=16) plt.xlabel(r"$\log\rm\,V_{rot}/km\,s^{-1}$", fontsize=16) plt.ylim(8, 11.5) plt.xlim(1.8, 2.5) ax = fig.add_subplot(111) ax.text(0.1, 0.9, r"$\rm late-types$", fontsize=18, color='#151AB0', transform=ax.transAxes) if len(bt[bt<0])>0: x, y, H, lev, vc_bin, mstar_bin, e_mstar_bin = bins_and_hist(vc, mstar, bt<0, size_hist, sigs) plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Greys'), alpha=0.75) else: # spirals w = bt < bt_spirals # plt.plot(log10(vc[w]), log10(mstar[w]), 'b.', alpha=0.0025) x, y, H, lev, vc_bin, mstar_bin, e_mstar_bin = bins_and_hist(vc, mstar, w, size_hist, sigs) # plt.errorbar(log10(vc_bin), log10(mstar_bin), yerr=abs(log10(mstar_bin)-log10(e_mstar_bin)), c='b', fmt='o') plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Blues')) plt.plot(linspace(1.9, 2.4), log10(Mstar_TF(linspace(1.9, 2.4))), 'k-', lw=3) plt.plot(linspace(1.9, 2.4), log10(Mstar_TF(linspace(1.9, 2.4)))+0.15, 'k--', lw=3) plt.plot(linspace(1.9, 2.4), log10(Mstar_TF(linspace(1.9, 2.4)))-0.15, 'k--', lw=3) if save_plots: plt.savefig('sTF_BT_ref.pdf', bbox_inches='tight') if len(bt[bt<0])>0: pass else: ''' ----------- Faber-Jackson ''' fig = plt.figure() plt.ylabel(r"$\log\rm\,M_\ast/M_\odot$", fontsize=16) plt.xlabel(r"$\log\rm\,\sigma/km\,s^{-1}$", fontsize=16) plt.ylim(9, 11.75) plt.xlim(1.8, 2.6) ax = fig.add_subplot(111) ax.text(0.1, 0.9, r"$\rm early-types$", fontsize=18, color='#BD000D', transform=ax.transAxes) # ellipticals w = bt > bt_ells # turn vc to sigma for ellipticals vc[w] /= factor # Dutton et al. 2010 correction # (vc[w])[log10(mstar[w]) > 10.5] *= 10.**(0.1-0.3*(log10(mstar[w])[log10(mstar[w]) > 10.5]-10.5)) # (vc[w])[log10(mstar[w]) <= 10.5] *= 10.**0.1 # plt.plot(log10(mstar[w]), log10(vc[w]), 'r.', alpha=0.0025) x, y, H, lev, vc_bin, mstar_bin, e_mstar_bin = bins_and_hist(vc, mstar, w, size_hist, sigs) # plt.errorbar(log10(vc_bin), log10(mstar_bin), yerr=abs(log10(mstar_bin)-log10(e_mstar_bin)), c='r', fmt='o') plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Reds')) plt.plot(linspace(1.8, 2.6), log10(Mstar_FJ(linspace(1.8, 2.6))), 'k-', lw=3) plt.plot(linspace(1.8, 2.6), log10(Mstar_FJ(linspace(1.8, 2.6)))+0.1, 'k--', lw=3) plt.plot(linspace(1.8, 2.6), log10(Mstar_FJ(linspace(1.8, 2.6)))-0.1, 'k--', lw=3) if save_plots: plt.savefig('FJ_BT_ref.pdf', bbox_inches='tight') ''' ----------- FP ''' fig = plt.figure() plt.xlabel(r"$\log\rm\,M_\ast/kpc$", fontsize=16) plt.ylabel(r"$\rm\,10.6+2\log\,\sigma/130\,km\,s^{-1}+\log\,R_e/2\,kpc$", fontsize=16) plt.xlim(9, 11.75) plt.xlim(9, 12.) ax = fig.add_subplot(111) ax.text(0.1, 0.825, r"$\rm early-types$", fontsize=18, color='#BD000D', transform=ax.transAxes) # etg w = bt > bt_ells # fp fp = 10.6+2.*log10(vc[w]/factor/130.)+log10(Re[w]/2.) x, y, H, lev, mstar_bin, fp_bin, e_fp_bin = bins_and_hist(mstar[w], 10.**fp, mstar[w] > 0, size_hist, sigs) # plt.errorbar(log10(mstar_bin), fp_bin, yerr=0.5, c='r', fmt='o') plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Reds')) plt.plot(linspace(9, 12), linspace(9, 12), 'k-', lw=3) plt.plot(linspace(9, 12), linspace(9, 12)+0.06, 'k--', lw=3) plt.plot(linspace(9, 12), linspace(9, 12)-0.06, 'k--', lw=3) if save_plots: plt.savefig('FP_BT_ref.pdf', bbox_inches='tight') ''' ----------- Mass - size relation ''' fig = plt.figure() plt.xlabel(r"$\log\rm\,M_\ast/kpc$", fontsize=16) plt.ylabel(r"$\log\rm\,R_e/kpc$", fontsize=16) plt.xlim(8, 11.75) ax = fig.add_subplot(111) ax.text(0.1, 0.9, r"$\rm late-types$", fontsize=18, color='#151AB0', transform=ax.transAxes) # ax.text(0.1, 0.85, r"$\rm Sa-S0$", fontsize=18, color='#009603', transform=ax.transAxes) ax.text(0.1, 0.825, r"$\rm early-types$", fontsize=18, color='#BD000D', transform=ax.transAxes) if len(bt[bt<0])>0: x, y, H, lev, mstar_bin, Re_bin, e_Re_bin = bins_and_hist(mstar, Re, bt<0, size_hist, sigs) plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Greys'), alpha=0.75) else: # spirals w = bt < bt_ltg # plt.plot(log10(r200)[w], log10(Re)[w], 'b.', alpha=0.0075) x, y, H, lev, mstar_bin, Re_bin, e_Re_bin = bins_and_hist(mstar, Re, w, size_hist, sigs) # plt.errorbar(log10(mstar_bin), log10(Re_bin), yerr=abs(log10(Re_bin)-log10(e_Re_bin)), c='b', fmt='o') plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Blues'), alpha=0.75) # ellipticals w = bt > bt_ltg # plt.plot(log10(r200)[w], log10(Re)[w], 'r.', alpha=0.0075) x, y, H, lev, mstar_bin, Re_bin, e_Re_bin = bins_and_hist(mstar, Re, w, size_hist, sigs) # plt.errorbar(log10(mstar_bin), log10(Re_bin), yerr=abs(log10(Re_bin)-log10(e_Re_bin)), c='r', fmt='o') plt.contourf(x, y, log10(H).T, levels=append(lev, log10(H).max()), cmap=plt.get_cmap('Reds'), alpha=0.75) plt.plot(linspace(9, 11.75), mass_size_etg(logspace(9, 11.75)), 'r-', lw=2) plt.plot(linspace(9, 11.75), mass_size_ltg(logspace(9, 11.75)), 'b--', lw=2) if save_plots: plt.savefig('Re-Ms_BT_ref.pdf', bbox_inches='tight') plt.show()
def compute_pos_corr_matrix(X, N): '''computes positional correlation matrix''' pos_mat_prod = dot(tp(X), X)/N pos_avg_prod = dot(tp(matrix(mean(X, 0))), matrix(mean(X, 0))) pos_corr = npabs(pos_mat_prod - pos_avg_prod) return pos_corr
def feature_sign_search_algorithm(self, inp_features, acondtol=1e-3, ret_error=False, display_error=False, max_iter=0, single=False, timed=True, starting_points=None, training=False): ''' Returns sparse features representation ''' self.min_coeff_rat = co.CONST['sparse_fss_min_coeff_rat'] self.min_coeff = max([self.min_coeff, self.min_coeff_rat * np.size(inp_features)]) if self.inp_feat_list is not None: self.inp_feat_list.append(inp_features.ravel()) else: self.inp_feat_list = [inp_features.ravel()] self.inp_features = inp_features.copy().reshape((-1,1)) # Step 1 btb = dot(self.codebook_comps.T, self.codebook_comps) btf = dot(self.codebook_comps.T, self.inp_features) if self.rat is not None: self.gamma = np.max(np.abs(-2 * btf)) * self.rat gamma = self.gamma if starting_points is not None: self.sparse_features = starting_points.reshape((self.sparse_dim, 1)) self.theta = np.sign(self.sparse_features) self.active_set[:] = False self.active_set[self.sparse_features.ravel()!=0] = True step2 = 0 else: step2 = 1 count = 0 prev_objval = 0 if max_iter == 0: max_iter = self.max_iter else: self.max_iter = max_iter self.prev_sparse_feats = None prev_error = 0 initial_energy = compute_lineq_error(inp_features, 0, 0) interm_error = initial_energy SPLOG.info('Initial Signal Energy: ' + str(initial_energy)) SPLOG.info('Initial nonzero elements number: ' + str(np.sum(inp_features!=0))) converged = False for count in range(self.max_iter): # Step 2 if step2: zero_coeffs = (self.sparse_features == 0) qp_der_outfeati = 2 * \ (dot(btb, self.sparse_features) - btf) * zero_coeffs.reshape((-1,1)) i = argmax(npabs(qp_der_outfeati)) if (npabs(qp_der_outfeati[i]) > gamma or npsum(self.active_set) < self.min_coeff): self.theta[i] = -sign(qp_der_outfeati[i]) self.active_set[i] = True # Step 3 codebook_comps_h = self.codebook_comps[:, self.active_set] sparse_feat_h = self.sparse_features[self.active_set].reshape( (-1,1)) theta_h = self.theta[self.active_set].reshape((-1,1)) _q_ = dot(codebook_comps_h.T, self.inp_features) - gamma * theta_h / 2.0 codebook_comps_h2 = dot(codebook_comps_h.T, codebook_comps_h) rank = matrix_rank(codebook_comps_h2) zc_search = True if rank == codebook_comps_h2.shape[0]: new_sparse_f_h = np.linalg.solve(codebook_comps_h2, _q_) else: u,s,v = np.linalg.svd(codebook_comps_h2) col_space = u[:, :rank] null_space = u[:, rank:] #Check if q belongs in column space, ie the projection of #q in the column space is q itself q_proj = np.zeros_like(_q_).reshape(-1, 1) for i in range(col_space.shape[1]): col = col_space[:,i].reshape(-1, 1) q_proj+=((dot(_q_.reshape(1,-1),col) / np.dot(col.T, col).astype(float))*col) ''' LOG.info('q|Projection: ' + str(np.concatenate((_q_.reshape(-1,1),q_proj),axis=1))) LOG.info('Projection Energy: '+ str(np.sum(q_proj**2))) LOG.info('Distance between q and projection: '+str(np.linalg.norm(q_proj.ravel()-_q_.ravel()))) ''' if np.allclose(q_proj.ravel()-_q_.ravel(), 0, atol=1.e-6): new_sparse_f_h = dot(pinv(codebook_comps_h2),_q_) else: #direction z in nullspace of codebook_comps_h2 can not be #perpendicular to _q_, because then _q_ = C(codebook_comps_h2), #which was proven not to hold. #I take the principal vector that belongs in null_space of #codebook_comps_h2 and add it to the current sparse_feat_h #so that to search for zerocrossings #inside the line constructed # by this vector and sparse_feat_h, which has direction, # belonging to null_space of codebook_comps_h2 tmp_sparse_f_h = sparse_feat_h + dot(null_space, np.ones((null_space.shape[1],1))) zero_points_lin_par = sparse_feat_h / (sparse_feat_h - tmp_sparse_f_h).astype(float) # find _t_ that corresponds to the closest zero crossing to # sparse_feat_h _t_ind = np.argmin(np.abs(zero_points_lin_par[ np.isfinite(zero_points_lin_par)])) _t_ = zero_points_lin_par[ np.isfinite(zero_points_lin_par)][_t_ind] null_vec = _t_ * tmp_sparse_f_h + (1 - _t_) * sparse_feat_h new_sparse_f_h = null_vec zc_search = False if (np.prod(sign(sparse_feat_h) != sign(new_sparse_f_h)) and zc_search): zero_points_lin_par = sparse_feat_h / (sparse_feat_h - new_sparse_f_h).astype(float) zero_points_lin_par = concatenate((zero_points_lin_par[ ((zero_points_lin_par > 0) * (zero_points_lin_par < 1)).astype(bool)][:], array([1])), axis=0) _t_ = zero_points_lin_par null_vecs = _t_ * new_sparse_f_h + (1 - _t_) * sparse_feat_h objvals = self.object_val_calc(codebook_comps_h, self.inp_features, gamma, theta_h, null_vecs).flatten() objval_argmin = argmin(objvals) objval = np.min(objvals) new_sparse_f_h = null_vecs[:, objval_argmin][:, None].copy() else: objval = self.object_val_calc(codebook_comps_h, self.inp_features, gamma, theta_h, new_sparse_f_h) self.sparse_features[self.active_set] = new_sparse_f_h.copy() self.active_set[self.active_set] = np.logical_not( isclose(new_sparse_f_h, 0)) if npsum(self.active_set) < self.min_coeff: step2 = 1 continue self.theta = sign(self.sparse_features) # Step 4 nnz_coeff = self.sparse_features != 0 # a new_qp_der_outfeati = 2 * (dot(btb, self.sparse_features) - btf) cond_a = (new_qp_der_outfeati + gamma * sign(self.sparse_features)) * nnz_coeff ''' if np.abs(objval) - np.abs(prev_objval) > 100 and not\ self.allow_big_vals and not count == 0: if self.prev_sparse_feats is not None: SPLOG.info('Current Objective Function value: ' + str(np.abs(objval))) SPLOG.info('Previous Objective Function value: ' + str(np.abs(prev_objval))) SPLOG.info('Problem with big values of inv(B^T*B)' + ',you might want to increase atol' + ' or set flag allow_big_vals to true' + ' (this might cause' + ' problems)') SPLOG.info('Reverting to previous iteration result ' + 'and exiting loop..') self.sparse_features = self.prev_sparse_feats.ravel() break else: LOG.error('Current Objective Function value: ' + str(np.abs(objval))) LOG.error('Previous Objective Function value: ' + str(np.abs(prev_objval))) LOG.error('Problem with big values of inv(B^T*B),increase atol' + ' or set flag allow_big_vals to true (this might cause' + ' serious convergence problems)') LOG.error('Exiting as algorithm has not produced any' + ' output results.') exit() ''' prev_objval = objval self.prev_sparse_feats = self.sparse_features if allclose(cond_a, 0, atol=acondtol): # go to cond b: z_coeff = self.sparse_features == 0 cond_b = npabs(new_qp_der_outfeati * z_coeff) <= gamma if npsum(cond_b) == new_qp_der_outfeati.shape[0]: self.sparse_features = self.sparse_features.reshape((-1,1)) converged = True break else: # go to step 2 step2 = 1 else: # go to step 3 step2 = 0 if count % 10 == 0: interm_error = compute_lineq_error( self.inp_features, self.codebook_comps, self.sparse_features) if interm_error == prev_error or interm_error > initial_energy: converged=True break else: prev_error = interm_error SPLOG.info('\t Epoch:' + str(count)) SPLOG.info('\t\t Intermediate Error=' + str(interm_error)) if interm_error < 0.001: converged=True SPLOG.info('Small error, asssuming convergence') break ''' if initial_energy < interm_error: if not training: LOG.warning('FSS Algorithm did not converge, using pseudoinverse' + ' of provided codebook instead') if self.inv_codebook_comps is None: self.inv_codebook_comps = pinv(self.codebook_comps) self.sparse_features=dot(self.inv_codebook_comps,self.inp_features).ravel() else: SPLOG.info('FSS Algorithm did not converge,' + ' removing sample from training dataset...') self.sparse_features = None return (interm_error), False, initial_energy else: ''' if not converged: SPLOG.info('FSS Algorithm did not converge' + ' in the given iterations') else: SPLOG.info('Successful Convergence') SPLOG.info('\tFinal error: ' + str(interm_error)) SPLOG.info('\tNumber of nonzero elements: ' + str(np.sum(self.sparse_features!=0))) if not single: if self.sparse_feat_list is None: self.sparse_feat_list = [self.sparse_features.ravel()] else: self.sparse_feat_list.append(self.sparse_features.ravel()) if ret_error: return (compute_lineq_error(self.inp_features, self.codebook_comps, self.sparse_features), True, initial_energy) self.sparse_features = self.sparse_features.ravel() return None, True, None
def write_buckling_bdfs(bdf_model, op2_filename, xyz_cid0, patches, patch_edges_array, isubcase=1, mode='displacement', workpath='results'): # TODO: Create 'BDF' of deflected shape that doesn't include the wing up bend from the CAERO1 # boxes. This will allow you to visually see where the wing undulates relative to the # center plane of the wing assert mode in ['load', 'displacement'], 'mode=%r' % mode model = bdf_model subcase = bdf_model.subcases[isubcase] # TODO: add title, subtitle from the actual load case for cross validation header = '' header += 'SOL 105\n' header += 'CEND\n' header += 'ECHO = NONE\n' if subcase.has_parameter('TITLE'): header += ' TITLE = %s\n' % subcase.get_parameter('TITLE')[0] print(subcase.get_parameter('TITLE')) else: header += 'TITLE = BUCKLING\n' header += 'SUBCASE 1\n' # header += ' SUPORT = 1\n' #if mode == 'load': header += ' LOAD = 55\n' header += ' METHOD = 42\n' #header += ' STRESS(PLOT,PRINT,VONMISES,CENTER) = ALL\n' #header += ' SPCFORCES(PLOT,PRINT) = ALL\n' #header += ' STRAIN(PLOT,PRINT,VONMISES,FIBER,CENTER) = ALL\n' header += ' DISPLACEMENT(PLOT,PRINT) = ALL\n' header += ' SPC = 100\n' if 'SUBTITLE' in subcase: header += ' SUBTITLE = %s\n' % subcase.get_parameter('SUBTITLE')[0] #header += ' MPC = 1\n' #header += ' TRIM = 1\n' header += 'BEGIN BULK\n' header += 'PARAM,GRDPNT,0\n' header += 'PARAM,COUPMASS,1\n' #header += 'PARAM,AUNITS,0.00259\n' #header += 'PARAM,WTMASS,0.00259\n' #header += 'PARAM,BAILOUT,-1\n' header += 'PARAM,PRTMAXIM,YES\n' header += 'PARAM,POST,-1 \n' eig1 = 0.0 eig2 = 100. nroots = 20 method = 42 spc_id = 100 load_id = 55 header += 'EIGB,%s,INV,%s,%s,%s\n' % (method, eig1, eig2, nroots) out_model = OP2() print('**** workpath', workpath) out_model.read_op2(op2_filename) if mode == 'displacement': #print('out_model.displacements =', out_model.displacements) displacements = out_model.displacements[isubcase] node_ids_full_model = displacements.node_gridtype[:, 0] ## TODO: check for cd != 0 elif mode == 'load': nodal_forces = out_model.grid_point_forces[isubcase] else: raise RuntimeError(mode) edge_nids = unique(patch_edges_array.ravel()) #free_nodes = setdiff1d(node_ids_full_model, edge_nids) # A-B # TODO: support excluded pids excluded_pids = [] patch_dir = os.path.join(workpath, 'patches') edge_dir = os.path.join(workpath, 'edges') if not os.path.exists(patch_dir): os.makedirs(patch_dir) if not os.path.exists(edge_dir): os.makedirs(edge_dir) for ipatch, patch_eids in enumerate(patches): patch_eids_array = array(patch_eids, dtype='int32') patch_filename = os.path.join(patch_dir, 'patch_%i.bdf' % ipatch) edge_filename = os.path.join(edge_dir, 'edge_%i.csv' % ipatch) patch_file = open(patch_filename, 'w') edge_file = open(edge_filename, 'w') patch_file.write(header) # get list of all nodes on patch_eids_array; write elements all_nids_on_patch = [] pids_on_patch = set([]) for eid in patch_eids_array: elem = model.elements[eid] node_ids = elem.node_ids all_nids_on_patch.append(node_ids) patch_file.write(str(elem)) pids_on_patch.add(elem.Pid()) # TODO: assumes a patch has only one property region pid = elem.Pid() if pid in excluded_pids: print('pid=%s is excluded; patch_filename=%s' % patch_filename) patch_file.close() edge_file.close() os.remove(patch_filename) os.remove(edge_filename) continue all_nids_on_patch = unique(hstack(all_nids_on_patch)) nnodes = len(all_nids_on_patch) # get nodal cd; write nodes if mode == 'load': cd = zeros(nnodes, dtype='int32') for i, nid in enumerate(all_nids_on_patch): node = model.nodes[nid] cdi = node.Cd() patch_file.write(str(node)) cd[i] = cdi elif mode == 'displacement': for i, nid in enumerate(all_nids_on_patch): node = model.nodes[nid] patch_file.write(str(node)) else: raise NotImplementedError(mode) # model._write_common(patch_file, size=8, is_double=False) size = 8 is_double = False model._write_coords(patch_file, size, is_double) model._write_materials(patch_file, size, is_double) for pid in pids_on_patch: prop = model.properties[pid] patch_file.write(prop.write_card(size=size, is_double=is_double)) #model._write_properties(patch_file, size, is_double) # msg = [' G R I D P O I N T F O R C E B A L A N C E\n', # ' POINT-ID ELEMENT-ID SOURCE T1 T2 T3 R1 R2 R3\n', ] #'0 13683 3736 TRIAX6 4.996584E+00 0.0 1.203093E+02 0.0 0.0 0.0' #' 13683 3737 TRIAX6 -4.996584E+00 0.0 -1.203093E+02 0.0 0.0 0.0' #' 13683 *TOTALS* 6.366463E-12 0.0 -1.364242E-12 0.0 0.0 0.0' zero = ' ' if 0: msg = [] self = nodal_forces for ekey, force in sorted(iteritems(self.force_moment)): for iload, forcei in enumerate(force): (f1, f2, f3, m1, m2, m3) = forcei elem_name = self.elemName[ekey][iload] eid = self.eids[ekey][iload] # vals = [f1, f2, f3, m1, m2, m3] # vals2 = write_floats_13e(vals) # [f1, f2, f3, m1, m2, m3] = vals2 if eid == 0: eid = '' msg.append('%s %8s %10s %-8s %-13s %-13s %-13s %-13s %-13s %s\n' % ( zero, ekey, eid, elem_name, f1, f2, f3, m1, m2, m3)) zero = ' ' zero = '0' if mode == 'displacement': # the displacement are in the Cd coordinate frame # because we're just carrying along the GRID cards # we shouldn't need to do anything patch_edge_nids = [] for edge in patch_edges_array: n1, n2 = edge if n1 in all_nids_on_patch and n2 in all_nids_on_patch: patch_edge_nids.append(edge) patch_edge_nids = unique(vstack(patch_edge_nids)) #print('patch_edge_nids = %s' % patch_edge_nids) #print('node_ids_full_model = %s' % node_ids_full_model) idisp_full_model = searchsorted(node_ids_full_model, patch_edge_nids) nids2 = node_ids_full_model[idisp_full_model] #print('idisp_full_model = %s' % idisp_full_model) #print('nids2 = %s' % nids2) #print('delta = %s' % (node_ids_full_model[idisp_full_model] - patch_edge_nids)) # make sure we're in Cd == 0 disp = displacements.data[0, idisp_full_model, :] #disp = displacements.data[:, 3] ipack = 0 spc_pack = [] #$SPCD 100 20028 1.0009906 20028 24.3233-4 #$SPCD 100 20028 3.3169889 20028 41.6345-4 #$SPCD 100 20028 5.0004592 20028 6-2.092-3 #FORCE,1,20036,0,0.000001,1.0,1.0,1.0 #SPC1,100,123456,20028 n1 = patch_edge_nids[0] n2 = patch_edge_nids[1] spc1 = ['SPC1', spc_id, 123] + list(patch_edge_nids) #patch_file.write('SPC1,%i,123456,%i\n' % (spc_id, n1)) patch_file.write(print_card_8(spc1)) # dummy load # TODO: figure out free node #edge_nids = unique(patch_edges_array.ravel()) #free_nodes = setdiff1d(node_ids_full_model, edge_nids) # A-B #free_nodes_on_patch = setdiff1d(free_nodes, all_nids_on_patch) # A-B #free_nodes_on_patch = in1d(free_nodes, all_nids_on_patch) # A and B free_nodes_on_patch = setdiff1d(all_nids_on_patch, edge_nids) #print('free_nodes_on_patch =', free_nodes_on_patch) if len(free_nodes_on_patch) == 0: print('couldnt find free node for patch_filename=%s' % patch_filename) patch_file.close() edge_file.close() #os.remove(patch_filename) #os.remove(edge_filename) continue free_node = free_nodes_on_patch[0] assert free_node in all_nids_on_patch, 'free_node=%s is not patch_filename=in %s' % (free_node, patch_filename) patch_file.write('FORCE,%i,%i,0,0.000001,1.0,1.0,1.0\n' % (load_id, free_node)) for i, nid in enumerate(patch_edge_nids): #if i == 0: #continue xyz = xyz_cid0[nid] edge_file.write('%f, %f, %f\n' % (xyz[0], xyz[1], xyz[2])) assert nids2[i] == nid, 'nid=%s nid2=%s' % (nid, node_ids_full_model[i]) for j in range(3): #for j in range(6): dispi = disp[i, j] if npabs(dispi) > 0.0: # SPCD, sid, g1, c1, d1 #patch_file.write(print_card_8(['SPCD', spc_id, nid, j + 1, dispi])) if ipack == 0: # SPCDs are loads, not SPCs. Don't change this!!! spc_pack = ['SPCD', load_id, nid, j + 1, dispi] ipack += 1 else: # ipack = 1 spc_pack += [nid, j + 1, dispi] patch_file.write(print_card_8(spc_pack)) ipack = 0 spc_pack = [] if len(spc_pack): patch_file.write(print_card_8(spc_pack)) elif mode == 'load': # TODO: does this work for vectorized classes? for node_id, cdi in zip(all_nids_on_patch[1:], cd[1:]): force = nodal_forces.force_moment[node_id] force_moment_sum = zeros(6, dtype='float32') for iload, forcei in enumerate(force): eid = nodal_forces.eids[node_id][iload] element_name = nodal_forces.elemName[node_id][iload] if eid not in patch_eids_array: # neighboring element force_moment_sum += force[iload] elif eid == 0 and element_name != '*TOTALS*': print(element_name) force_moment_sum += force[iload] abs_force_moment_sum = npabs(force_moment_sum) forcei = abs_force_moment_sum[:3] momenti = abs_force_moment_sum[3:] if forcei.sum() > 0.0: # write force card = ['FORCE', load_id, node_id, cdi, 1.0, ] + list(forcei) patch_file.write(print_card_8(card)) if momenti.sum() > 0.0: # write moment card = ['MOMENT', load_id, node_id, cdi, 1.0, ] + list(momenti) patch_file.write(print_card_8(card)) else: raise RuntimeError(mode) patch_file.write('ENDDATA\n') patch_file.close() edge_file.close() return
def plot_mag_series(times, mags, errs=None, outfile=None, sigclip=30.0, timebin=None, yrange=None): '''This plots a magnitude time series. If outfile is none, then plots to matplotlib interactive window. If outfile is a string denoting a filename, uses that to write a png/eps/pdf figure. timebin is either a float indicating binsize in seconds, or None indicating no time-binning is required. ''' if errs is not None: # remove nans find = npisfinite(times) & npisfinite(mags) & npisfinite(errs) ftimes, fmags, ferrs = times[find], mags[find], errs[find] # get the median and stdev = 1.483 x MAD median_mag = npmedian(fmags) stddev_mag = (npmedian(npabs(fmags - median_mag))) * 1.483 # sigclip next if sigclip: sigind = (npabs(fmags - median_mag)) < (sigclip * stddev_mag) stimes = ftimes[sigind] smags = fmags[sigind] serrs = ferrs[sigind] LOGINFO('sigclip = %s: before = %s observations, ' 'after = %s observations' % (sigclip, len(times), len(stimes))) else: stimes = ftimes smags = fmags serrs = ferrs else: # remove nans find = npisfinite(times) & npisfinite(mags) ftimes, fmags, ferrs = times[find], mags[find], None # get the median and stdev = 1.483 x MAD median_mag = npmedian(fmags) stddev_mag = (npmedian(npabs(fmags - median_mag))) * 1.483 # sigclip next if sigclip: sigind = (npabs(fmags - median_mag)) < (sigclip * stddev_mag) stimes = ftimes[sigind] smags = fmags[sigind] serrs = None LOGINFO('sigclip = %s: before = %s observations, ' 'after = %s observations' % (sigclip, len(times), len(stimes))) else: stimes = ftimes smags = fmags serrs = None # now we proceed to binning if timebin and errs is not None: binned = time_bin_magseries_with_errs(stimes, smags, serrs, binsize=timebin) btimes, bmags, berrs = (binned['binnedtimes'], binned['binnedmags'], binned['binnederrs']) elif timebin and errs is None: binned = time_bin_magseries(stimes, smags, binsize=timebin) btimes, bmags, berrs = binned['binnedtimes'], binned['binnedmags'], None else: btimes, bmags, berrs = stimes, smags, serrs # finally, proceed with plotting fig = plt.figure() fig.set_size_inches(7.5,4.8) plt.errorbar(btimes, bmags, fmt='go', yerr=berrs, markersize=2.0, markeredgewidth=0.0, ecolor='grey', capsize=0) # make a grid plt.grid(color='#a9a9a9', alpha=0.9, zorder=0, linewidth=1.0, linestyle=':') # fix the ticks to use no offsets plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) plt.gca().get_xaxis().get_major_formatter().set_useOffset(False) # get the yrange if yrange and isinstance(yrange,list) and len(yrange) == 2: ymin, ymax = yrange else: ymin, ymax = plt.ylim() plt.ylim(ymax,ymin) plt.xlim(npmin(btimes) - 0.001*npmin(btimes), npmax(btimes) + 0.001*npmin(btimes)) plt.xlabel('time [JD]') plt.ylabel('magnitude') if outfile and isinstance(outfile, str): plt.savefig(outfile,bbox_inches='tight') plt.close() return os.path.abspath(outfile) else: plt.show() plt.close() return
def plot_phased_mag_series(times, mags, period, errs=None, epoch='min', outfile=None, sigclip=30.0, phasewrap=True, phasesort=True, phasebin=None, plotphaselim=[-0.8,0.8], yrange=None): '''This plots a phased magnitude time series using the period provided. If epoch is None, uses the min(times) as the epoch. If epoch is a string 'min', then fits a cubic spline to the phased light curve using min(times), finds the magnitude minimum from the fitted light curve, then uses the corresponding time value as the epoch. If epoch is a float, then uses that directly to phase the light curve and as the epoch of the phased mag series plot. If outfile is none, then plots to matplotlib interactive window. If outfile is a string denoting a filename, uses that to write a png/eps/pdf figure. ''' if errs is not None: # remove nans find = npisfinite(times) & npisfinite(mags) & npisfinite(errs) ftimes, fmags, ferrs = times[find], mags[find], errs[find] # get the median and stdev = 1.483 x MAD median_mag = npmedian(fmags) stddev_mag = (npmedian(npabs(fmags - median_mag))) * 1.483 # sigclip next if sigclip: sigind = (npabs(fmags - median_mag)) < (sigclip * stddev_mag) stimes = ftimes[sigind] smags = fmags[sigind] serrs = ferrs[sigind] LOGINFO('sigclip = %s: before = %s observations, ' 'after = %s observations' % (sigclip, len(times), len(stimes))) else: stimes = ftimes smags = fmags serrs = ferrs else: # remove nans find = npisfinite(times) & npisfinite(mags) ftimes, fmags, ferrs = times[find], mags[find], None # get the median and stdev = 1.483 x MAD median_mag = npmedian(fmags) stddev_mag = (npmedian(npabs(fmags - median_mag))) * 1.483 # sigclip next if sigclip: sigind = (npabs(fmags - median_mag)) < (sigclip * stddev_mag) stimes = ftimes[sigind] smags = fmags[sigind] serrs = None LOGINFO('sigclip = %s: before = %s observations, ' 'after = %s observations' % (sigclip, len(times), len(stimes))) else: stimes = ftimes smags = fmags serrs = None # figure out the epoch, if it's None, use the min of the time if epoch is None: epoch = npmin(stimes) # if the epoch is 'min', then fit a spline to the light curve phased # using the min of the time, find the fit mag minimum and use the time for # that as the epoch elif isinstance(epoch,str) and epoch == 'min': spfit = spline_fit_magseries(stimes, smags, serrs, period) epoch = spfit['fitepoch'] # now phase (and optionally, phase bin the light curve) if errs is not None: # phase the magseries phasedlc = phase_magseries_with_errs(stimes, smags, serrs, period, epoch, wrap=phasewrap, sort=phasesort) plotphase = phasedlc['phase'] plotmags = phasedlc['mags'] ploterrs = phasedlc['errs'] # if we're supposed to bin the phases, do so if phasebin: binphasedlc = phase_bin_magseries_with_errs(plotphase, plotmags, ploterrs, binsize=phasebin) plotphase = binphasedlc['binnedphases'] plotmags = binphasedlc['binnedmags'] ploterrs = binphasedlc['binnederrs'] else: # phase the magseries phasedlc = phase_magseries(stimes, smags, period, epoch, wrap=phasewrap, sort=phasesort) plotphase = phasedlc['phase'] plotmags = phasedlc['mags'] ploterrs = None # if we're supposed to bin the phases, do so if phasebin: binphasedlc = phase_bin_magseries(plotphase, plotmags, binsize=phasebin) plotphase = binphasedlc['binnedphases'] plotmags = binphasedlc['binnedmags'] ploterrs = None # finally, make the plots # initialize the plot fig = plt.figure() fig.set_size_inches(7.5,4.8) plt.errorbar(plotphase, plotmags, fmt='bo', yerr=ploterrs, markersize=2.0, markeredgewidth=0.0, ecolor='#B2BEB5', capsize=0) # make a grid plt.grid(color='#a9a9a9', alpha=0.9, zorder=0, linewidth=1.0, linestyle=':') # make lines for phase 0.0, 0.5, and -0.5 plt.axvline(0.0,alpha=0.9,linestyle='dashed',color='g') plt.axvline(-0.5,alpha=0.9,linestyle='dashed',color='g') plt.axvline(0.5,alpha=0.9,linestyle='dashed',color='g') # fix the ticks to use no offsets plt.gca().get_yaxis().get_major_formatter().set_useOffset(False) plt.gca().get_xaxis().get_major_formatter().set_useOffset(False) # get the yrange if yrange and isinstance(yrange,list) and len(yrange) == 2: ymin, ymax = yrange else: ymin, ymax = plt.ylim() plt.ylim(ymax,ymin) # set the x axis limit if not plotphaselim: plot_xlim = plt.xlim() plt.xlim((npmin(plotphase)-0.1, npmax(plotphase)+0.1)) else: plt.xlim((plotphaselim[0],plotphaselim[1])) # set up the labels plt.xlabel('phase') plt.ylabel('magnitude') plt.title('using period: %.6f d and epoch: %.6f' % (period, epoch)) # make the figure if outfile and isinstance(outfile, str): plt.savefig(outfile,bbox_inches='tight') plt.close() return os.path.abspath(outfile) else: plt.show() plt.close() return
def calculate_levels(data, chunk_size, sample_rate, frequency_limits, num_bins, input_channels=2): """Calculate frequency response for each channel defined in frequency_limits :param data: decoder.frames(), audio data for fft calculations :type data: decoder.frames :param chunk_size: chunk size of audio data :type chunk_size: int :param sample_rate: audio file sample rate :type sample_rate: int :param frequency_limits: list of frequency_limits :type frequency_limits: list :param num_bins: length of gpio to process :type num_bins: int :param input_channels: number of audio input channels to process for (default=2) :type input_channels: int :return: :rtype: numpy.array """ # create a numpy array, taking just the left channel if stereo data_stereo = frombuffer(data, dtype=int16) if input_channels == 2: # data has 2 bytes per channel data = empty(len(data) / (2 * input_channels)) # pull out the even values, just using left channel data[:] = data_stereo[::2] elif input_channels == 1: data = data_stereo # if you take an FFT of a chunk of audio, the edges will look like # super high frequency cutoffs. Applying a window tapers the edges # of each end of the chunk down to zero. data = data * hanning(len(data)) # Apply FFT - real data fourier = fft.rfft(data) # Remove last element in array to make it the same size as chunk_size fourier = delete(fourier, len(fourier) - 1) # Calculate the power spectrum power = npabs(fourier) ** 2 matrix = zeros(num_bins, dtype='float64') for pin in range(num_bins): # take the log10 of the resulting sum to approximate how human ears # perceive sound levels # Get the power array index corresponding to a particular frequency. idx1 = int(chunk_size * frequency_limits[pin][0] / sample_rate) idx2 = int(chunk_size * frequency_limits[pin][1] / sample_rate) # if index1 is the same as index2 the value is an invalid value # we can fix this by incrementing index2 by 1, This is a temporary fix # for RuntimeWarning: invalid value encountered in double_scalars # generated while calculating the standard deviation. This warning # results in some channels not lighting up during playback. if idx1 == idx2: idx2 += 1 npsums = npsum(power[idx1:idx2:1]) # if the sum is 0 lets not take log10, just use 0 # eliminates RuntimeWarning: divide by zero encountered in log10, does not insert -inf if npsums == 0: matrix[pin] = 0 else: matrix[pin] = log10(npsums) return matrix
def abs_volume(self): return npabs(self.volume())