def fit_voigt_over_linear(q, I, cen=1, sig=0.002, sigmin=1e-4, sigmax=0.01, amplmin=0, amplmax=500, trim=0.06, plot=False): trim = logical_and(q < cen + trim, q > cen - trim) q = q[trim] I = I[trim] mod = LinearModel() mod.set_param_hint('slope', value=-20) mod.set_param_hint('intercept', value=10) lineout = mod.fit(I, x=q) pars = lineout.params mod += VoigtModel() pars.add('center', value=cen) pars.add('sigma', value=sig, max=sigmax, min=sigmin) pars.add('amplitude', value=amplmin / 2 + amplmax / 2, min=amplmin, max=amplmax) out = mod.fit(I, pars, x=q) return out
def get_db_pixel(tths, xs, ys): """Get direct beam pixel at TTH = 0 degrees. This is done by taking a total of 40 images around TTH = 0, finding the direct beam pixel for each of them and fitting these positions to a line to get the direct beam pixel position at TTH = 0 Arguments: tths {[float]} -- array of 2th values xs {[float]} -- array of the x positions of the direct beam pixel ys {[float]} -- array of the y positions of the direct beam pixel Returns: tuple -- x0, y0 (direct beam pixel position at 2th = 0) """ n0 = len(tths)//2 s_ = np.s_[n0-20:n0+20] xs, ys, tths = xs[s_], ys[s_], tths[s_] mod_xs = LinearModel() params_xs = mod_xs.guess(xs, x=tths) fit_xs = mod_xs.fit(xs, params=params_xs, x=tths) fit_xs = mod_xs.fit(xs, params=fit_xs.params, x=tths) mod_ys = LinearModel() params_ys = mod_ys.guess(ys, x=tths) fit_ys = mod_ys.fit(ys, params=params_ys, x=tths) fit_ys = mod_ys.fit(ys, params=fit_ys.params, x=tths) x0 = fit_xs.eval(params=fit_xs.params, x=0.0) y0 = fit_ys.eval(params=fit_ys.params, x=0.0) return x0, y0
def correct_data(xData, yData, startIdx=0, endIdx=-1): y = yData # start x at zero x = xData - xData[0] # linear model lr = LinearModel() params = lr.make_params() # fit model xSelection = x[startIdx:endIdx] ySelection = y[startIdx:endIdx] outParams = lr.fit(ySelection, params, x=xSelection) # construct corrected data linear_trend = x * outParams.best_values['slope'] + outParams.best_values[ 'intercept'] yCorrected = y - linear_trend + y[0] result = { "correctedData": json.loads( pd.DataFrame({ "x": xData.tolist(), "y": yCorrected.tolist() }).to_json(orient='records')) } return result
def rotational_temperature_analysis(L, E_upper): """ Function that will perform a rotational temperature analysis. This will perform a least-squares fit of log(L), which is related to the theoretical line strength and integrated flux, and the upper state energy for the same transition. Parameters ---------- L - 1D array Value L related to the line and theoretical line strength E_upper - 1D array The upper state energy in wavenumbers. Returns ------- ModelResult - object Result of the least-squares fit """ # Convert the upper state energy E_upper *= units.kbcm logL = np.log(L) model = LinearModel() params = model.make_params() result = model.fit(x=E_upper, y=logL, params=params) return result
def line_fit(x, y, errors=True): """ Simple helper function that speeds up line fitting. Uses lmfit Parameters ---------- x, y (float) Sample length x, y arrays of data to be fitted Returns ------- Returns slope and intercept, with uncertainties (use uncertainties package if availabe) Also returns fit object out, can be dropped """ from lmfit.models import LinearModel mod = LinearModel() par = mod.guess(y, x=x) out = mod.fit(y, par, x=x) s = out.params['slope'] i = out.params['intercept'] if errors: try: from uncertainties import ufloat return ufloat(s.value, s.stderr), ufloat(i.value, i.stderr), out except: return s.value, s.stderr, i.value, i.stderr, out else: return s.value, i.value, out
def calibrate_pitch(mono='111'): BMMuser = user_ns['BMMuser'] # read content from INI file datafile = os.path.join(BMMuser.DATA, 'edges%s.ini' % mono) print(f'reading {datafile}') config.read_file(open(datafile)) edges = dict() for i in config.items('edges'): el = i[0] vals = [float(j) for j in i[1].split(',') ] # convert CSV string -> list of strings -> list of floats edges[el] = vals # organize the data from the INI file ordered = [y[1] for y in sorted([(edges[x][1], x) for x in edges.keys()])] ee = list() tt = list() for el in ordered: ee.append(edges[el][1]) tt.append(edges[el][3]) mod = LinearModel() pars = mod.guess(tt, x=ee) out = mod.fit(tt, pars, x=ee) print(whisper(out.fit_report(min_correl=0))) out.plot()
def line_fit(x, y, errors=True): """ Simple helper function that speeds up line fitting. Uses lmfit Parameters ---------- x, y (float) Sample length x, y arrays of data to be fitted Returns ------- Returns slope and intercept, with uncertainties (use uncertainties package if availabe) Also returns fit object out, can be dropped """ from lmfit.models import LinearModel mod = LinearModel() par = mod.guess(y, x=x) out = mod.fit(y, par, x=x) s = out.params["slope"] i = out.params["intercept"] if errors: try: from uncertainties import ufloat return ufloat(s.value, s.stderr), ufloat(i.value, i.stderr), out except: return s.value, s.stderr, i.value, i.stderr, out else: return s.value, i.value, out
def find_peaks(filename, show_plots=False): #subtract background and find peaks num_samp_left = 200 num_samp_right = 200 data = np.genfromtxt(filename) x = data[:, 0] y = data[:, 1] x_bg = np.hstack([x[:num_samp_left], x[-num_samp_right:]]) y_bg = np.hstack([y[:num_samp_left], y[-num_samp_right:]]) model = LinearModel() params = model.guess(y_bg, x=x_bg) out = model.fit(y_bg, x=x_bg, params=params) if show_plots: plt.plot(x, y) plt.plot(x_bg, y_bg, '.') y_fit = out.model.func(x, **out.best_values) data = y - y_fit if show_plots: plt.plot(x, data) plt.show() indexes = peakutils.indexes(-data, thres=0.25, min_dist=65) print(indexes) pplot(x, data, indexes) peaks_x = peakutils.interpolate(x, data, ind=indexes) print(peaks_x)
def fitting_math( self, xfile: List[str], yfile: List[str], flag: int = 1, ) -> Any: """PeakLogic.fitting_math() fits the data to a cosh and a gaussian, then subtracts the cosh to find peak current..""" try: center: float = self.app.peak_center_.get() x: "np.ndarray[Any, np.dtype[np.float64]]" = np.array( xfile, dtype=np.float64) y: "np.ndarray[Any, np.dtype[np.float64]]" = np.array( yfile, dtype=np.float64) # cut out outliers passingx: "np.ndarray[Any, np.dtype[np.float64]]" passingy: "np.ndarray[Any, np.dtype[np.float64]]" passingx, passingy = self.trunc_edges(xfile, yfile) rough_peak_positions = [min(passingx), center] min_y = float(min(passingy)) model = LinearModel(prefix="Background") params = model.make_params() # a=0, b=0, c=0 params.add("slope", 0, min=0) # params.add("b", 0, min=0) params.add("intercept", 0, min=min_y) for i, cen in enumerate(rough_peak_positions): peak, pars = self.add_lz_peak(f"Peak_{i+1}", cen) model = model + peak params.update(pars) _ = model.eval(params, x=passingx) result = model.fit(passingy, params, x=passingx) comps = result.eval_components() ip = float(max(comps["Peak_2"])) if flag == 1: return ip if flag == 0: return ( x, y, result.best_fit, comps["Background"], comps["Peak_1"], comps["Peak_2"], ip, passingx, ) except Exception: # pragma: no cover print("Error Fitting") print(sys.exc_info()) return -1
def lenar_calc(x,y): mod = LinearModel() pars = mod.guess(y, x=x) out = mod.fit(y, pars, x=x) calc= out.best_values['slope'] stress=calc*multi() stress=round(stress,3) #plt.plot(x,out.bes_fit) return stress, x , out.best_fit,out
def lenar_calc(x, y): global dados mod = LinearModel() pars = mod.guess(y, x=x) out = mod.fit(y, pars, x=x) ## print out.best_values plt.plot(x, out.best_fit) calc = out.best_values['slope'] ## print calc,multi() stresslocal = calc * multi() globalstress.append(stresslocal) print 'Value:', dados, round(stresslocal, 3)
def fF_plot(pressures, volumes, out_params): p = pressures[:, 0] sigP = pressures[:, 1] V = volumes[:, 0] sigV = volumes[:, 1] results = out_params.params.valuesdict() Vo = results['vo'] ko = results['ko'] kpo = results['kp'] sigVo = out_params.params['vo'].stderr #ignore the divide by zero error if the first piece of data is at 0 GPa np.seterr(divide='ignore', invalid='ignore') #f = (1.0/2.0)*(((V/Vo)**(-2.0/3.0))-1.0) f = (((Vo / V)**(2. / 3.)) - 1.) / 2. F = p / (3. * f * (1. + (2. * f))**(5. / 2.)) eta = V / Vo sigeta = np.abs(eta) * ((((sigV / V)**2.0) + ((sigVo / Vo)**2))**(1.0 / 2.0)) sigprime = ((7.0 * (eta**(-2.0 / 3.0)) - 5.0) * sigeta) / (2.0 * (1.0 - (eta**-2.0 / 3.0)) * eta) sigF = F * np.sqrt(((sigP / p)**2.0) + (sigprime**2)) line_mod = LinearModel() pars = line_mod.guess(f) out = line_mod.fit(F, pars, x=f) plt.figure(4) plt.plot(f, out.best_fit, '-', color='black') plt.errorbar(f, F, fmt='ko', xerr=0, yerr=sigF, alpha=1.0, capsize=3.) plt.xlabel('Eulerian strain $\mathit{f_E}$', fontweight='bold') plt.ylabel('Normalized pressure $\mathbf{F_E}$ (GPa)', fontweight='bold') plt.tick_params(direction='in', bottom=1, top=1, left=1, right=1) plt.title("$\mathit{f_E}$-F", fontweight='bold') #plt.savefig('Ff-plot.png',dpi=600,bbox_inches='tight') print(out.fit_report()) slope = uct.ufloat(out.params['slope'], out.params['slope'].stderr) inter = uct.ufloat(out.params['intercept'], out.params['intercept'].stderr) k_p = ((2.0 * slope) / (3 * inter)) + 4 return k_p, f, F, sigF, out.best_fit
def direct_method(x, y, bin_sizes, fractions, num_samples, weights=None, shuffle_threshold=0.02, return_slope=False, return_for_graph=False): if not x.shape[0] == y.shape[0]: raise ValueError("x and y must have same length in first dimension.") if y.ndim == 1: MIs = create_samples_1d(x, y, bin_sizes, fractions, num_samples, weights=weights) else: MIs = create_samples_1d_dd(x, y, bin_sizes, fractions, num_samples, weights=weights) MIs = MIs.reshape((-1, len(fractions), 2)) params = fit_linear_to_array(MIs[..., 0], 1 / fractions, MIs[..., 1]) params = params.reshape((len(bin_sizes), 2, 4)) where_less = params[:, 1, 2] < shuffle_threshold if where_less.sum() < 3: where_less = np.zeros(params.shape[0], dtype=bool) where_less[:3] = True model = LinearModel() result = model.fit( data=params[where_less, 0, 2], x=1 / bin_sizes[where_less]**2, weights=params[where_less, 0, 3]**2, ) mi = result.params["intercept"].value mi_err = result.params["intercept"].stderr if not return_for_graph: if return_slope: return mi, mi_err, result.params["slope"].value else: return mi, mi_err else: return (mi, mi_err, MIs.reshape(len(bin_sizes), 2, len(fractions), 2), params, result.params["slope"].value, where_less)
def fit_GC_residual(x, y, peak, peak_center): mod = LinearModel(prefix='bkg_') pars = mod.guess(y, x) # mod.make_params() #pars['bkg_intercept'].value = 1e5# #pars['bkg_slope'].value = 500 out = mod.fit(y, pars, x=x) # if peak == 'H2': # #print('Nfev = ', out.nfev) # print(out.fit_report()) # #print(out.pars['peak_amplitude'].value) return out
def fit_adev(tau, adev, err_lo, err_high): fit_tau_over = 499 # If there are at least 2 datapoints to fit at large tau_values, fit them if len(tau[np.where(tau > fit_tau_over)]) >= 2: # TODO: take into account asymmetric errorbars weights = (err_lo + err_high) / 2 # take naive 1-std errorbar average x = np.array([t for t in tau if t > fit_tau_over]) # only fit long tau values y = np.array([a for i, a in enumerate(adev) if tau[i] > fit_tau_over ]) # take equivalent in adev array w = np.array([ h for i, h in enumerate(weights) if tau[i] > fit_tau_over ]) # take equivalent in weights array # Fit straight line on a log10 scale x = np.log10(x) y = np.log10(y) w_log = np.log10(np.exp(1)) * (w / y ) # error propagation for log10(y +- w) # Weighted Least Squares fit; ax + b model = LinearModel() params = model.make_params() params['intercept'].max = -10 params['intercept'].value = -15 params['intercept'].min = -19 params['intercept'].brute_step = 0.005 params[ 'slope'].value = -0.5 # assume white noise dominates on fitting range params['slope'].vary = False # ... so we keep this parameter fixed res = model.fit(y, params, weights=1 / w_log**2, x=x) a = res.values['slope'] b = res.values['intercept'] x_smooth = np.logspace(0, 5, 20) return x_smooth, 10**(res.eval(x=np.log10(x_smooth))), a, b # Else if there are not enough large tau_values to fit, return empty arrays else: return [], [], 0.5, -1
def estimate_continuum(self): ''' if this cube is a continuum cube, calculate the continuum image with this cube ''' x=self.spectral_axis.to(u.AA).value cube_conti_para=np.zeros((2,self._data.shape[1],self._data.shape[2])) for i in range(cube_conti_para.shape[1]): for j in range(cube_conti_para.shape[2]): lmodel=LinearModel() para=lmodel.guess(data=self._data[:,i,j],x=x) result=lmodel.fit(data=self._data[:,i,j],x=x,params=para,method='bfgsb') cube_conti_para[:,i,j]=[result.best_values['slope'],result.best_values['intercept']] return cube_conti_para
def replaceSpike(x, y, I): """ y = replaceSpike(x, y, I) Replace bad points in y by good ones. I is the index of bad points. Works by doing a linear fit over the data. """ mod = LinearModel() params = mod.guess(data=np.delete(y, I), x=np.delete(x, I)) # print(params) # print(np.delete(y, I)) result = mod.fit(np.delete(y, I), params, x=np.delete(x, I)) # print(result.fit_report()) # print(result.best_values) yy = mod.eval(x=x, slope=result.best_values['slope'], intercept=result.best_values['intercept']) y[I] = yy[I] return y
def calculateSlope(self, x, y, numPoints = 50): nop = self.__regPoints if nop > len(x): _x=x _y=y else: _x = x[-nop:] _y = y[-nop:] mod = LinearModel() pars = mod.make_params() pars['slope'].set(0.0) pars['intercept'].set(0.0) out = mod.fit(_y, pars, x=_x) slope = str("{0:.3f}".format(out.best_values['slope']*1000)) self.returnSlope.emit(slope) _time = self.setTimeLabel(_x) self.__Regres.setData(_time, out.best_fit)
def test_final_parameter_values(): model = LinearModel() params = model.make_params() params['intercept'].set(value=-1, min=-20, max=0) params['slope'].set(value=1, min=-100, max=400) np.random.seed(78281) x = np.linspace(0, 9, 10) y = x * 1.34 - 4.5 + np.random.normal(scale=0.05, size=x.size) result = model.fit(y, x=x, method='nelder', params=params) assert_almost_equal(result.chisqr, 0.014625543, decimal=6) assert_almost_equal(result.params['intercept'].value, -4.511087126, decimal=6) assert_almost_equal(result.params['slope'].value, 1.339685514, decimal=6)
def removebackground(n, y, x): def list(vetor): newvetor = [] for i in vetor: newvetor.append(i) return newvetor def minimo(y): minimo = min(y) for i in range(len(y)): y[i] -= minimo return y x1 = list(x) y = list(y) Xn = [] y = minimo(y) #min values for i in x1[:n]: Xn.append(i) for i in x1[-n:]: Xn.append(i) mod = LinearModel() pars = mod.guess(y[:n] + y[-n:], x=Xn) out = mod.fit(y[:n] + y[-n:], pars, x=Xn) m = out.values['slope'] b = out.values['intercept'] Z = m * x + b minimo = min(Z) for i in range(len(Z)): if Z[i] < minimo: Z[i] = minimo newy = y - Z newy = savgol_filter(newy, 15, 9) return newy
def bg_correct(file,show_plot=False): num_samp_left = 50 num_samp_right = 50 x_bg = np.hstack([time[:num_samp_left],time[-num_samp_right:]]) y_bg = np.hstack([file[:num_samp_left],file[-num_samp_right:]]) mod = LinearModel() pars = mod.guess(y_bg, x=x_bg) out = mod.fit(y_bg, pars, x=x_bg) y_fit = out.model.func(time,**out.best_values) if show_plot: plt.plot(time, spectra) plt.plot(x_bg,y_bg,'.') plt.plot(time,y_fit,'r--') plt.show() if show_plot: plt.plot(time, spectra-y_fit) plt.show() data=spectra-y_fit return data
def background (n,ys,xs): def list(vetor): newvetor = [] for i in vetor: newvetor.append(i) return newvetor x1=list(xs) ys=list(ys) #print 'dados:', len(x), len(y), len(x1) #print y[-n:]+y[:n] Xn=[] for i in x1[:n]: Xn.append(i) for i in x1[-n:]: Xn.append(i) #print len(x1[-n:]+x1[:n]), len(y[-n:]+y[:n]), len(Xn) mod = LinearModel() pars = mod.guess(ys[-n:]+ys[:n], x=Xn) out = mod.fit(ys[-n:]+ys[:n], pars, x=Xn) m=out.values['slope'] b=out.values['intercept'] Z=m*xs + b #print 'Z: ',len(Z) minimo = min(Z) for i in range(len(Z)): if Z[i]<minimo: Z[i]=minimo return Z
def fit_linear_to_array(data, x, data_err): """ Runs the same linear fit for multiple data sets. data: Array of shape (samples, N). Where samples is the number of fits to run and N is the number of datapoints per fit. x: Array of shape (N,). The x values for the fit. data_err: Array of shape (samples, N). The std values for data. Returns params: Array of shape (samples, 4), where params[i, j] is the slope (j=0) of data[i, :], the slope standard deviation (j=1), intercept (j=2), intercept standard deviation (j=3). """ params = np.empty((data.shape[0], 4)) model = LinearModel() for i in range(data.shape[0]): result = model.fit(data=data[i, :], x=x, weights=1 / data_err[i, :]**2) params[i, 0] = result.params["slope"].value params[i, 1] = result.params["slope"].stderr params[i, 2] = result.params["intercept"].value params[i, 3] = result.params["intercept"].stderr return params
def LinearWarren(x, y): global mintheta, maxtheta mod = LinearModel() pars = mod.guess(y, x=x) out = mod.fit(y, pars, x=x) XS = out.values['intercept'] / out.values['slope'] * -1 XS = int(XS) La = XS slope, intercept, r_value, p_value, std_err = stats.linregress(x, y) ## print 'SLOPE ',slope ## print 'Intercepe ', intercept Da = lambida_func() * intercept / ( 2 * (np.sin(np.radians(maxtheta / 2)) - np.sin(np.radians(mintheta / 2)))) print 'D:', int(Da), 'nm' # Create a list of values in the best fit line abline_values = [slope * i + intercept for i in x] lx = [] ly = [] endwhile = True i = 0 while (endwhile): lx.append(i) valuey = slope * i + intercept ly.append(valuey) if valuey <= 0: endwhile = False else: i += 1 valuey = 0 #pdb.set_trace() plt.plot(lx, ly, 'red')
def fit(filename): kB_wn = 0.69503477 # cm-1/K data = np.loadtxt(filename) trans = transitions() trans = assign(data, trans) trans = sorted(trans, key=lambda t: t.E) xxx = [t.E for t in trans] y_exp = [t.Y / (t.A * t.g) for t in trans] y_exp = list(np.log(y_exp)) model = LinearModel() pars = model.make_params(intercept=5.0, slope=-1.0 / (kB_wn * 300)) out = model.fit(y_exp, pars, x=xxx) y_calc = out.best_fit T = -1 / (kB_wn * out.params['slope']) return xxx, y_exp, y_calc, T, trans
def get_old_new_DM(df_info): # Get variables from the parameter file. mean = df_info.MEAN.values mean_error = df_info.MEAN_ERROR.values freqMHz = df_info.FREQ.values psrname = df_info.PSRJ.values[0] DMorig = df_info.DM_ORIG # Fit a DM model to delta mu. # Get rid of the nans. freqMHz = freqMHz[~np.isnan(mean)] mean_error = mean_error[~np.isnan(mean)] mean = mean[~np.isnan(mean)] delnuarray = np.array([(1 / freqMHz[i]**2 - 1 / freqMHz[0]**2) for i in range(len(mean))]) ##in MHz delmuarray = np.array([(mean[i] - mean[0]) for i in range(len(mean))]) ##in seconds delmu_stdarray = np.array([(mean_error[i] - mean_error[0]) for i in range(len(mean))]) ##in seconds if len(delmuarray) < 4: print('{}: only {} freqs, not enough to measure DM'.format( psrname, len(delmuarray))) return np.nan, np.nan, np.nan, np.nan, np.nan else: linmod = LinearModel() DM_linpars = linmod.guess(delmuarray, x=delnuarray) DM_linout = linmod.fit(delmuarray, DM_linpars, x=delnuarray) DM_CCval = DM_linout.best_values['slope'] DM_CCvalstd = DM_linout.params['slope'].stderr DMmodelfit = DM_linout.best_fit ##model gives deltime in seconds (used to shift data) DMconstant = 4148.808 #uncertainty in the constant is 0.003 - only affects the Delta DM value in the 9th decimal DMdelta = (DM_CCval / DMconstant) DMdeltastd = (DM_CCvalstd / DMconstant) # Return the original DM, deltaDM, delta-dispersion-constant (ie the best fit not divided by 4148.808), and the new DM, along with their errors. DMnew = DMorig + DMdelta return DMdelta, DMdeltastd, DM_CCval, DM_CCvalstd, DMnew
mod = LinearModel() vetor=[1,5,10,15,20,25,30,35,40,45,50,55,60] plt.subplot(L,C,P);P=P+1 for i in range(80): xxplot=[xx1[i],xx3[i]] yyplot=[vetor1[i],vetor3[i]] #if i in vetor: plt.plot(xxplot,yyplot,'-o') x=xxplot y=yyplot try: pars = mod.guess(y, x=x) out = mod.fit(y, pars, x=x) print i,'microdeformacao: ', abs((out.values['slope']/(-2*pi**2))) slope.append( ( out.values['slope']/(2*pi**2))) intercep.append( ( out.values['intercept'] ) ) except: pass plt.xlabel('$1/d^2(nm^2)$') plt.ylabel("$LN(L_A(nm))$")
def extract_density(field, rxy, field_cutoffs, plot_fit=False): """Extract the carriers density from the low field resistance dependence. The extraction relies on a simple linear fit performed on a specified field range. Parameters ---------- field : np.ndarray Magnetic field values for which the the transverse resistance was measured. This can be a multidimensional array in which case the last dimension will be considered as the swept dimension. rxy : np.ndarray Transverse resistance values which were measured. This can be a multidimensional array in which case the last dimension will be considered as the swept dimension. field_cutoffs : tuple | np.ndarray Pairs of low/high field values on which to perform the linear fit. If only one pair of value is provided it will be used for all fits. Returns ------- densities : float | np.ndarray Densities extracted from the linear fit. Will be a float if a single value was extracted from the provided data. densities_stderr : float | np.ndarray Incertitude on the density expressed as the standard deviation. Will be a float if a single value was extracted from the provided data. """ # Identify the shape of the data and make them suitable for the following # treatment. if len(field.shape) >= 2: input_is_1d = False original_shape = field.shape[:-1] trace_number = np.prod(original_shape) field = field.reshape((trace_number, -1)) rxy = rxy.reshape((trace_number, -1)) if len(field_cutoffs) == 2: fc = np.empty(original_shape + (2, )) fc[..., 0] = field_cutoffs[0] fc[..., 1] = field_cutoffs[1] field_cutoffs = fc field_cutoffs = field_cutoffs.reshape((trace_number, -1)) else: input_is_1d = True trace_number = 1 field = np.array((field, )) rxy = np.array((rxy, )) field_cutoffs = np.array((field_cutoffs, )) results = np.empty((2, trace_number)) model = LinearModel() # Perform a linear fit in the specified field range and extract the slope for i in range(trace_number): start_field, stop_field = field_cutoffs[i] start_ind = np.argmin(np.abs(field[i] - start_field)) stop_ind = np.argmin(np.abs(field[i] - stop_field)) start_ind, stop_ind =\ min(start_ind, stop_ind), max(start_ind, stop_ind) f = field[i][start_ind:stop_ind] r = rxy[i][start_ind:stop_ind] res = model.fit(r, x=f) results[0, i] = 1 / res.best_values['slope'] / cs.e # value in m^-2 results[1, i] = results[0, i] * (res.params['slope'].stderr / res.best_values['slope']) # If requested plot the result in a dedicated window. if plot_fit: plt.figure() plt.plot(f, r, '+') plt.plot(f, res.best_fit) plt.xlabel('Field') plt.ylabel('Rxy') plt.tight_layout() if input_is_1d: return results[:, 0] else: return results.reshape((2, ) + original_shape)
print "Number of plotted channels: %d/%d" %(npch, nch) bestpT_highSNR = bestpT bestpT_std_highSNR = bestpT_std """Calculate fits for parameters sigma and mu""" """Fit a DM model to delta mu""" delnuarray = [-(1/freqMHz_highsnr[-1]**2-1/freqMHz_highsnr[i]**2) for i in range(npch)] ##in MHz delmuarray = [(bestpT_highSNR[1][-1] - bestpT_highSNR[1][i])*pbs for i in range(npch)] ##in seconds delmu_stdarray = [(bestpT_std_highSNR[1][-1] - bestpT_std_highSNR[1][i])*pbs for i in range(npch)] linmod = LinearModel() DM_linpars = linmod.guess(delmuarray, x=delnuarray) # DM_linout = linmod.fit(delmuarray, DM_linpars, x=delnuarray, weights=1/(np.power(delmu_stdarray,2))) DM_linout = linmod.fit(delmuarray, DM_linpars, x=delnuarray) DM_CCval = DM_linout.best_values['slope'] DM_CCvalstd = DM_linout.params['slope'].stderr DMmodelfit = DM_linout.best_fit ##model gives deltime in seconds (used to shift data) DMconstant = 4148.808 #uncertainty in the constant is 0.003 - only affects the Delta DM value in the 9th decimal DMval = (DM_CCval/DMconstant) DMvalstd = (DM_CCvalstd/DMconstant) DMcheck = psr.DM_checker(freqmsMHz,bestpT_highSNR[1]*pbs) """Plotting starts"""
def measure_line_index(wave, flux, flux_err=None, mask=None, z=None, line_info=None, num_refit=(100, None), filepath=None, return_type='dict', verbose=False): """ Measure line index / EW and have it plotted Parameters ---------- wave: array-like wavelength vector flux: array-like flux vector flux_err: array-like flux error vector (optional) If un-specified, auto-generate an np.ones array mask: array-like andmask or ormask (optional) If un-specified, auto-generate an np.ones array (evenly weighted) line_info: dict information about spectral line, eg: line_info_dib5780 = {'line_center': 5780, 'line_range': (5775, 5785), 'line_shoulder_left': (5755, 5775), 'line_shoulder_right': (5805, 5825)} num_refit: non-negative integer number of refitting. If 0, no refit will be performed If positive, refits will be performed after adding normal random noise z: float redshift (only specify when z is large) filepath: string path of the diagnostic figure if None, do nothing, else print diagnostic figure return_type: string 'dict' or 'array' if 'array', np.array(return dict.values()) verbose: bool if True, print details Returns ------- line_indx: dict A dictionary type result of line index. If any problem encountered, return the default result (filled with nan). """ try: # 0. do some input check # 0.1> check line_info line_info_keys = line_info.keys() assert 'line_range' in line_info_keys assert 'line_shoulder_left' in line_info_keys assert 'line_shoulder_right' in line_info_keys # 0.2> check line range/shoulder in spectral range assert np.min(wave) <= line_info['line_shoulder_left'][0] assert np.max(wave) >= line_info['line_shoulder_right'][0] # 1. get line information # line_center = line_info['line_center'] # not used line_range = line_info['line_range'] line_shoulder_left = line_info['line_shoulder_left'] line_shoulder_right = line_info['line_shoulder_right'] # 2. shift spectra to rest-frame wave = np.array(wave) flux = np.array(flux) if z is not None: wave /= 1. + z # 3. estimate the local continuum # 3.1> shoulder wavelength range ind_shoulder = np.any([ np.all([wave > line_shoulder_left[0], wave < line_shoulder_left[1]], axis=0), np.all([wave > line_shoulder_right[0], wave < line_shoulder_right[1]], axis=0)], axis=0) wave_shoulder = wave[ind_shoulder] flux_shoulder = flux[ind_shoulder] # 3.2> integrated/fitted wavelength range ind_range = np.logical_and(wave > line_range[0], wave < line_range[1]) wave_range = wave[ind_range] flux_range = flux[ind_range] # flux_err_range = flux_err[ind_range] # not used mask_range = mask[ind_range] flux_err_shoulder = flux_err[ind_shoulder] # mask_shoulder = mask[ind_shoulder] # not used # 4. linear model mod_linear = LinearModel(prefix='mod_linear_') par_linear = mod_linear.guess(flux_shoulder, x=wave_shoulder) # ############################################# # # to see the parameter names: # # model_linear.param_names # # {'linear_fun_intercept', 'linear_fun_slope'} # # ############################################# # out_linear = mod_linear.fit(flux_shoulder, par_linear, x=wave_shoulder, method='leastsq') # 5. estimate continuum cont_shoulder = out_linear.best_fit noise_std = np.std(flux_shoulder / cont_shoulder) cont_range = mod_linear.eval(out_linear.params, x=wave_range) resi_range = 1 - flux_range / cont_range # 6.1 Integrated EW ( # estimate EW_int wave_diff = np.diff(wave_range) wave_step = np.mean(np.vstack([np.hstack([wave_diff[0], wave_diff]), np.hstack([wave_diff, wave_diff[-1]])]), axis=0) EW_int = np.dot(resi_range, wave_step) # estimate EW_int_err num_refit_ = num_refit[0] if num_refit_ is not None and num_refit_>0: EW_int_err = np.std(np.dot( (resi_range.reshape(1, -1).repeat(num_refit_, axis=0) + np.random.randn(num_refit_, resi_range.size) * noise_std), wave_step)) # 6.2 Gaussian model # estimate EW_fit mod_gauss = GaussianModel(prefix='mod_gauss_') par_gauss = mod_gauss.guess(resi_range, x=wave_range) out_gauss = mod_gauss.fit(resi_range, par_gauss, x=wave_range) line_indx = collections.OrderedDict([ ('SN_local_flux_err', np.median(flux_shoulder / flux_err_shoulder)), ('SN_local_flux_std', 1. / noise_std), ('num_bad_pixel', np.sum(mask_range != 0)), ('EW_int', EW_int), ('EW_int_err', EW_int_err), ('mod_linear_slope', out_linear.params[mod_linear.prefix + 'slope'].value), ('mod_linear_slope_err', out_linear.params[mod_linear.prefix + 'slope'].stderr), ('mod_linear_intercept', out_linear.params[mod_linear.prefix + 'intercept'].value), ('mod_linear_intercept_err', out_linear.params[mod_linear.prefix + 'intercept'].stderr), ('mod_gauss_amplitude', out_gauss.params[mod_gauss.prefix + 'amplitude'].value), ('mod_gauss_amplitude_err', out_gauss.params[mod_gauss.prefix + 'amplitude'].stderr), ('mod_gauss_center', out_gauss.params[mod_gauss.prefix + 'center'].value), ('mod_gauss_center_err', out_gauss.params[mod_gauss.prefix + 'center'].stderr), ('mod_gauss_sigma', out_gauss.params[mod_gauss.prefix + 'sigma'].value), ('mod_gauss_sigma_err', out_gauss.params[mod_gauss.prefix + 'sigma'].stderr), ('mod_gauss_amplitude_std', np.nan), ('mod_gauss_center_std', np.nan), ('mod_gauss_sigma_std', np.nan)]) # estimate EW_fit_err num_refit_ = num_refit[1] if num_refit_ is not None and num_refit_ > 2: # {'mod_gauss_amplitude', # 'mod_gauss_center', # 'mod_gauss_fwhm', # 'mod_gauss_sigma'} out_gauss_refit_amplitude = np.zeros(num_refit_) out_gauss_refit_center = np.zeros(num_refit_) out_gauss_refit_sigma = np.zeros(num_refit_) # noise_fit = np.random.randn(num_refit,resi_range.size)*noise_std for i in range(int(num_refit_)): # resi_range_with_noise = resi_range + noise_fit[i,:] resi_range_with_noise = resi_range + \ np.random.randn(resi_range.size) * noise_std out_gauss_refit = mod_gauss.fit(resi_range_with_noise, par_gauss, x=wave_range) out_gauss_refit_amplitude[i],\ out_gauss_refit_center[i],\ out_gauss_refit_sigma[i] =\ out_gauss_refit.params[mod_gauss.prefix + 'amplitude'].value,\ out_gauss_refit.params[mod_gauss.prefix + 'center'].value,\ out_gauss_refit.params[mod_gauss.prefix + 'sigma'].value print(out_gauss_refit_amplitude[i], out_gauss_refit_center[i], out_gauss_refit_sigma[i]) line_indx.update({'mod_gauss_amplitude_std': np.nanstd(out_gauss_refit_amplitude), 'mod_gauss_center_std': np.nanstd(out_gauss_refit_center), 'mod_gauss_sigma_std': np.nanstd(out_gauss_refit_sigma)}) # 7. plot and save image if filepath is not None and os.path.exists(os.path.dirname(filepath)): save_image_line_indice(filepath, wave, flux, ind_range, cont_range, ind_shoulder, line_info) # if necessary, convert to array # NOTE: for a non-ordered dict the order of keys and values may change! if return_type == 'array': return np.array(line_indx.values()) return line_indx except Exception: return measure_line_index_null_result(return_type)
def produce_taufits(filepath,meth='iso',pulseperiod=None,snr_cut=None, verbose=True, plotparams=False, plotflux=False, savefigure=False): pulsar, nch, nbins,nsub, lm_rms, tsub = read_headerfull(filepath) if verbose == True: verboseTag = True else: verboseTag = False print0 = "Pulsar name: %s" %pulsar print1 = "Number of freq. channels: %d \nFreq channels will be labeled 0 - %d" %(nch, nch-1) print2 = "Number of bins: %d" %nbins print3 = "RMS: %.2f" %lm_rms print4 = "Tsub: %.2f sec" %tsub for k in range(5): print eval('print{0}'.format(k)) print"--------------------------------------------------------" if pulseperiod==None: ## Define time axis, and time/bins conversions print ("Using Tsub in header to convert bins to time. Assumption is that tsub corresponds to 1.0 phase, corresponding to nbins. This should be adapted for search data.") pulseperiod = tsub else: pulseperiod = pulseperiod #set to provided pulseperiod in seconds profilexaxis = np.linspace(0,pulseperiod,nbins) pbs = pulseperiod/nbins tbs = tsub/nbins """Initialise vector outputs""" obtainedtaus, lmfittausstds = [], [] """freqmsMHz will correctly associate scattering time values (tau) with frequency, taking into account frequency integration across a sub-band. Whereas freqcsMHz is the centre freq. to the subband""" freqmsMHz, freqcsMHz = [], [] noiselessmodels =[] results, datas, comp_SNRs, comp_rmss = [], [], [], [] redchis, paramset, paramset_std, correls = [], [], [], [] halfway = nbins/2. for i in range(nch): print"--------------------------------------------------------" print "Channel %d" %i """Read in (pdv) data""" data, freqc, freqm = read_data(filepath,i,nbins) freqmsMHz.append(freqm) freqcsMHz.append(freqc) # roll the data of lowest freq channel to middle of bins if i ==0: peakbin = np.argmax(data) shift = int(halfway -int(peakbin)) if verboseTag: print 'peak bin at lowest freq channel:%d' %peakbin else: peakbin = peakbin shift = int(halfway - int(peakbin)) data = np.roll(data,shift) if verboseTag: print "Rolling data by -%d bins" %shift comp_rms = find_rms(data,nbins) if meth is None: print "No fitting method was chosen. Will default to an isotropic fitting model. \n Use option -m with 'onedim' to change." result, noiselessmodel, besttau, taustd, bestparams, bestparams_std, redchi, corsig = tau_fitter(data,nbins,verbose=verboseTag) elif meth == 'iso': result, noiselessmodel, besttau, taustd, bestparams, bestparams_std, redchi, corsig = tau_fitter(data,nbins,verbose=verboseTag) elif meth == 'onedim': result, noiselessmodel, besttau, taustd, bestparams, bestparams_std, redchi, corsig = tau_1D_fitter(data,nbins) comp_SNR_model = find_peaksnr(noiselessmodel,comp_rms) if verboseTag: print 'Estimated SNR (from model peak and data rms): %.2f' % comp_SNR_model comp_SNR = find_peaksnr_smooth(data,comp_rms) print 'Estimated SNR (from data peak and rms): %.2f' % comp_SNR print 'Channel Tau (ms): %.2f \pm %.2f ms' %(besttau,taustd) obtainedtaus.append(besttau) lmfittausstds.append(taustd) noiselessmodels.append(noiselessmodel) results.append(result) datas.append(data) comp_SNRs.append(comp_SNR) #new: comp_rmss.append(comp_rms) redchis.append(redchi) paramset.append(bestparams) paramset_std.append(bestparams_std) # if plotflux == True: # correls.append(corsig) #if plotflux == True: # cor_sigA = np.zeros(len(correls)) # for i in range(len(correls)): # cor_sigA[i] = correls[i]['A'] paramset = np.transpose(paramset) paramset_std = np.transpose(paramset_std) """Next check if any of the subbands contain only zeros. This happens with high RFI excision in LOFAR bands""" zero_ch = [] for i in range(nch): all_zeros = not np.any(datas[i]) if all_zeros: zero_ch.append(i) print"--------------------------------------------------------" if zero_ch: print "\n" print "%d channels have all zeroes (channels(s):" %len(zero_ch), zero_ch, ") and will be removed." if verboseTag: print "All zero channels are assigned SNR of 0" if snr_cut: print "Using SNR cutoff of %.2f" %snr_cut comp_SNRs = np.nan_to_num(comp_SNRs) (ind_lowSNR,) = np.where(np.array(comp_SNRs) < snr_cut) print "SNR cutoff will exclude %d channels (channel(s): %s)" %(len(ind_lowSNR), ind_lowSNR) data_highsnr = np.delete(np.array(datas),ind_lowSNR,0) model_highsnr = np.delete(np.array(noiselessmodels),ind_lowSNR,0) taus_highsnr = np.delete(np.array(obtainedtaus),ind_lowSNR) lmfitstds_highsnr = np.delete(np.array(lmfittausstds),ind_lowSNR) freqMHz_highsnr = np.delete(np.array(freqmsMHz),ind_lowSNR) #New: comp_rmss_highsnr = np.delete(np.array(comp_rmss),ind_lowSNR) redchis_highsnr = np.delete(np.array(redchis),ind_lowSNR) #corsigA_highsnr = np.delete(np.array(cor_sigA),ind_lowSNR) paramset_highsnr = np.zeros([len(paramset),len(data_highsnr)]) paramsetstd_highsnr = np.zeros([len(paramset),len(data_highsnr)]) for i in range(len(paramset)): paramset_highsnr[i]= np.delete(paramset[i],ind_lowSNR) paramsetstd_highsnr[i]= np.delete(paramset_std[i],ind_lowSNR) elif (snr_cut == None) and (zero_ch != []): print "Used no SNR cutoff" """Rename array to be same as when cut-off is used""" """If no SNR cutoff is used, remove channels with all zeroes -- these will automatically be removed by any snr_cut > 0""" data_highsnr = np.delete(np.array(datas),zero_ch,0) model_highsnr = np.delete(np.array(noiselessmodels),zero_ch,0) taus_highsnr = np.delete(np.array(obtainedtaus),zero_ch) lmfitstds_highsnr = np.delete(np.array(lmfittausstds),zero_ch) freqMHz_highsnr = np.delete(np.array(freqmsMHz),zero_ch) # New: comp_rmss_highsnr = np.delete(np.array(comp_rmss),zero_ch) redchis_highsnr = np.delete(np.array(redchis),zero_ch) #corsigA_highsnr = np.delete(np.array(cor_sigA),zero_ch) paramset_highsnr = np.zeros([len(paramset),len(data_highsnr)]) paramsetstd_highsnr = np.zeros([len(paramset),len(data_highsnr)]) for i in range(len(paramset)): paramset_highsnr[i]= np.delete(paramset[i],zero_ch) paramsetstd_highsnr[i]= np.delete(paramset_std[i],zero_ch) else: print "Used no SNR cutoff and there are no empty channels" data_highsnr = np.array(datas) model_highsnr = np.array(noiselessmodels) taus_highsnr = np.array(obtainedtaus) lmfitstds_highsnr = np.array(lmfittausstds) freqMHz_highsnr = np.array(freqmsMHz) # New: comp_rmss_highsnr = np.array(comp_rmss) redchis_highsnr = np.array(redchis) paramset_highsnr = np.array(paramset) paramsetstd_highsnr = np.array(paramset_std) taussec_highsnr = taus_highsnr*pbs lmfitstdssec_highsnr = lmfitstds_highsnr*pbs number_of_plotted_channels = len(data_highsnr) npch = number_of_plotted_channels print "Will plot remaining %d/%d channels" %(npch, nch) """Plotting starts""" #plot onedim in blue dashed #else plot in red if meth == 'onedim': prof = 'b--' lcol='b' else: prof = 'r-' lcol ='r' """1. PLOT PROFILES""" dimx, dimy = 3., 3. numsubplots = dimx*dimy numplots = int(np.ceil(npch/numsubplots)) print "Num profile plots:", numplots """Compute residuals""" #"""Plot 1: Pulse profiles and fits""" if npch > 0: resdata = data_highsnr - model_highsnr resnormed = (resdata-resdata.mean())/resdata.std() if taussec_highsnr[0] > 1: taulabel = taussec_highsnr taulabelerr = lmfitstdssec_highsnr taustring = 'sec' else: taulabel = taussec_highsnr*1000 taulabelerr = lmfitstdssec_highsnr*1000 taustring = 'ms' for k in range(numplots): j = int(numsubplots*k) figg = plt.figure(k+1,figsize=(int(4*dimx),int(3*dimy))) plots_remaining = int(npch - numsubplots*k) #print "Plots remaining", plots_remaining for i in range(np.min([int(numsubplots),int(plots_remaining)])): figg.subplots_adjust(left = 0.08, right = 0.98, wspace=0.35,hspace=0.35,bottom=0.15) #plt.rc('text', usetex=True) plt.rc('font', family='serif') plt.subplot(dimx,dimy,i+1) plt.plot(profilexaxis,data_highsnr[j+i],alpha = 0.20) plt.plot(profilexaxis,model_highsnr[j+i],lw = 2.0, alpha = 0.85, label=r'$\tau: %.2f \pm%.2f$ %s' %(taulabel[j+i], taulabelerr[j+i], taustring)) plt.title('%s at %.1f MHz' %(pulsar, freqMHz_highsnr[j+i])) plt.ylim(ymax=1.3*np.max(data_highsnr[j+i])) plt.xlim(xmax=pulseperiod) plt.xticks(fontsize=11) plt.yticks(fontsize=11) plt.xlabel('time (s)',fontsize=11) plt.legend(fontsize=11,numpoints=1) plt.ylabel('normalized intensity',fontsize=11) plt.tight_layout() if savefigure == True: figname = '%s_%s_%s_%d.png' %(os.path.basename(filepath),'fitted_profiles', meth, k) plt.savefig(figname, dpi=200) print "Saved figure %s in ./" %figname if noshow == False: plt.show() if verboseTag: for i in range(npch): print "Channel %d" %i print'Tau (ms): %.2f' %(1000*taussec_highsnr[i]) tau1GHz = tauatfreq(freqMHz_highsnr[i]/1000.,taussec_highsnr[i],1.0,4) print 'tau1GHz_alpha_4 (ms) ~ %.4f' %(tau1GHz*1000) lmfitstdssec_highsnr = lmfitstdssec_highsnr[np.nonzero(lmfitstdssec_highsnr)] taussec_highsnr = taussec_highsnr[np.nonzero(lmfitstdssec_highsnr)] freqMHz_highsnr = freqMHz_highsnr[np.nonzero(lmfitstdssec_highsnr)] """Plot 2: Plot Gaussian fitting parameters and DM if selected""" if plotparams==True: print "\nPlotting Gaussian fit parameters w.r.t frequency\n" """Set plotting parameters""" alfval = 0.6 markr= '*' msize=12 plt.figure(numplots+1, figsize=(12,8)) plt.subplots_adjust(left = 0.055, right=0.98,wspace=0.35,hspace=0.4,bottom=0.08) """Fit models to sigma""" powmod = PowerLawModel() powpars = powmod.guess(paramset_highsnr[0], x=freqMHz_highsnr) powout = powmod.fit(paramset_highsnr[0], powpars, x=freqMHz_highsnr, weights=1/((paramsetstd_highsnr[0])**2)) linmod = LinearModel() if len(freqMHz_highsnr) < 3: raise RuntimeError("plotparams == True: Less than three frequency channels. Cannot compute quadratic or exponential fit for width evolution. Consider lowering snr_cut.") else: quadmod = QuadraticModel() quadpars = quadmod.guess(paramset_highsnr[0], x=freqMHz_highsnr) quadout = quadmod.fit(paramset_highsnr[0], quadpars, x=freqMHz_highsnr, weights=1/((paramsetstd_highsnr[0])**2)) expmod = ExponentialModel() exppars = expmod.guess(paramset_highsnr[0], x=freqMHz_highsnr) expout = expmod.fit(paramset_highsnr[0], exppars, x=freqMHz_highsnr, weights=1/((paramsetstd_highsnr[0])**2)) """Fit a DM model to delta mu""" delnuarray = [-(1/freqMHz_highsnr[-1]**2-1/freqMHz_highsnr[i]**2) for i in range(npch)] ##in MHz delmuarray = [(paramset_highsnr[1][-1] - paramset_highsnr[1][i])*pbs for i in range(npch)] ##in seconds delmu_stdarray = [(paramsetstd_highsnr[1][-1] - paramsetstd_highsnr[1][i])*pbs for i in range(npch)] DM_linpars = linmod.guess(delmuarray, x=delnuarray) DM_linout = linmod.fit(delmuarray, DM_linpars, x=delnuarray) DM_CCval = DM_linout.best_values['slope'] DM_CCvalstd = DM_linout.params['slope'].stderr DMmodelfit = DM_linout.best_fit ##model gives deltime in seconds (used to shift data) DMconstant = 4148.808 #uncertainty in the constant is 0.003 - only affects the Delta DM value in the 9th decimal DMval = (DM_CCval/DMconstant) DMvalstd = (DM_CCvalstd/DMconstant) #DMcheck = psr.DM_checker(freqmsMHz,bestpT_highSNR[1]*pbs) ## Plot reduced chi square: plt.subplot(2,3,1) plt.plot(freqMHz_highsnr, redchis_highsnr/np.power(comp_rmss_highsnr,2), markr,alpha=alfval,markersize = msize) plt.title(r'Reduced $\chi^2$ values', fontsize=12) plt.yticks(fontsize=12) plt.xticks(fontsize=12) plt.xlabel(r'$\nu$ MHz',fontsize=12) plt.ylabel(r'$\chi^2$',fontsize=12) ## Plot sigma: plt.subplot(2,3,2) #plt.errorbar(freqMHz_highsnr,paramset_highsnr[0]*pbs) plt.errorbar(freqMHz_highsnr,paramset_highsnr[0]*pbs,yerr =paramsetstd_highsnr[0]*pbs, fmt = markr,markersize=msize,capthick=2,linewidth=1.5,alpha=alfval) plt.plot(freqMHz_highsnr,powout.best_fit*pbs,'-', alpha=alfval,label='pow = %.2f' %powout.best_values['exponent']) plt.plot(freqMHz_highsnr,quadout.best_fit*pbs,'-',alpha=alfval, label='quad: a,b = %.3f,%.3f' %(quadout.best_values['a'],quadout.best_values['b'])) plt.ylabel(r'$\sigma$ (sec)') plt.title(r'Width evolution', fontsize=12) plt.yticks(fontsize=12) plt.xticks(fontsize=12) plt.xlabel(r'$\nu$ MHz',fontsize=14) plt.legend(fontsize = 10, loc='best') ## Plot mean: plt.subplot(2,3,3) #plt.errorbar(freqMHz_highsnr,paramset_highsnr[1]*pbs) plt.errorbar(freqMHz_highsnr,paramset_highsnr[1]*pbs,yerr =paramsetstd_highsnr[1]*pbs, fmt = markr,markersize=msize,capthick=2,linewidth=1.5,alpha=alfval) plt.ylabel(r'$\mu$ (sec)') plt.title(r'Centroid evolution', fontsize=12) plt.yticks(fontsize=12) plt.xticks(fontsize=12) plt.xlabel(r'$\nu$ MHz',fontsize=14) #plt.legend(fontsize = 9, loc='best') ## Plot amplitude: plt.subplot(2,3,4) #plt.errorbar(freqMHz_highsnr,paramset_highsnr[2]*pbs) plt.errorbar(freqMHz_highsnr,paramset_highsnr[2]*pbs,yerr =paramsetstd_highsnr[2]*pbs, fmt = markr,markersize=msize,capthick=2,linewidth=1.5,alpha=alfval) plt.ylabel(r'$\mu$ (sec)') plt.title(r'Amplitude evolution', fontsize=12) plt.yticks(fontsize=12) plt.xticks(fontsize=12) plt.xlabel(r'$\nu$ MHz',fontsize=14) #plt.legend(fontsize = 9, loc='best') ## Plot DC: plt.subplot(2,3,5) #plt.errorbar(freqMHz_highsnr,paramset_highsnr[2]*pbs) plt.errorbar(freqMHz_highsnr,paramset_highsnr[3]*pbs,yerr =paramsetstd_highsnr[3]*pbs, fmt = markr,markersize=msize,capthick=2,linewidth=1.5,alpha=alfval) plt.ylabel(r'$\mu$ (sec)') plt.title(r'DC offset', fontsize=12) plt.yticks(fontsize=12) plt.xticks(fontsize=12) plt.xlabel(r'$\nu$ MHz',fontsize=14) #plt.legend(fontsize = 9, loc='best') ## Plot DM: plt.subplot(2,3,6) plt.errorbar(delmuarray,freqMHz_highsnr, fmt = markr, xerr=delmu_stdarray, alpha = alfval, markersize=msize) plt.plot(DMmodelfit,freqMHz_highsnr, '-', label=r'DM: $%.3f \pm %.3f$ $\rm{pc.cm}^{-3}$' %(DMval,DMvalstd), alpha = alfval) plt.xlabel(r'$\Delta \mu$ (sec)', fontsize =12) plt.yticks(fontsize=12) plt.xticks(fontsize=12) plt.title('Delta DM', fontsize=12) plt.ylabel(r'$\nu$ (MHz)',fontsize=14) plt.ticklabel_format(style='sci', axis='x',scilimits=(0,0)) plt.legend(fontsize = 10, loc='best') plt.tight_layout() if savefigure == True: figname2 = '%s_%s.png' %(os.path.basename(filepath),'fitting_parameters') plt.savefig(figname2, dpi=200) print "Saved figure %s in ./" %figname2 if noshow == False: plt.show() if plotflux == True: ##Flux section needs debugging ls = 'solid' """Plot flux, and corrected flux spectrum""" """Create unscattered profiles, i.e. Guassians""" bins, profiles = [],[makeprofile(nbins = nbins, ncomps = 1, amps = paramset_highsnr[2][j], means = paramset_highsnr[1][j], sigmas = paramset_highsnr[0][j]) for j in range(npch)] unscatflux = [] for i in range(npch): unscatfl = np.sum(profiles[j])/nbins unscatflux.append(unscatfl) #smootheddata = smooth(data_highsnr[j],int(0.05*nbins)) scatflux = [find_modelflux(model_highsnr[i],nbins) for i in range(npch)] climbvals = [] for i in range(npch): climb = returnclimb(np.linspace(1,nbins,nbins),paramset_highsnr[1][i],paramset_highsnr[0][i],paramset_highsnr[2][i],taussec_highsnr[i],paramset_highsnr[3][i],nbins) climbvals.append(climb) correctedflux = np.array([scatflux[i] + climbvals[i] for i in range(npch)]) print scatflux print climbvals #per bin meancorflux = np.mean(correctedflux) meancorfluxerr = np.sqrt(np.sum(correctedflux**2))/len(correctedflux) """Calculate error in Flux""" sigmaWIDTH = paramsetstd_highsnr[0]*pbs #in seconds sigmaAMP = paramsetstd_highsnr[2] #in mJy WIDTHS =paramset_highsnr[0]*pbs #in seconds AMPS =paramset_highsnr[2] #in mJy Expr1 = np.sqrt(2*np.pi)*AMPS Expr2 = np.sqrt(WIDTHS) AreaExpression = Expr1*Expr2 sigmaEx1 = np.sqrt(2*np.pi)*sigmaAMP sigmaEx2 = Expr2*0.5*sigmaWIDTH/WIDTHS sigmaFlux =AreaExpression*np.sqrt(np.power(sigmaEx1/Expr1,2)+ np.power(sigmaEx2/Expr2,2))#+ 2*corsigA_highsnr*sigmaEx1*sigmaEx2/(Expr1*Expr2) plt.figure(figsize=(10,6)) plt.plot(freqMHz_highsnr, correctedflux,'k-', linewidth=2.0) plt.plot(freqMHz_highsnr, scatflux,'r--', linewidth=2.0) plt.fill_between(freqMHz_highsnr,scatflux,correctedflux, alpha=alfval,facecolor='r') eb = plt.errorbar(freqMHz_highsnr,correctedflux,yerr=sigmaFlux, fmt=markr,markersize=10.0, alpha=1.0,capthick=2,linewidth=1.5) eb[-1][0].set_linestyle(ls) #plt.errorbar(freqMHz_highsnr, unscatflux,yerr=sigmaFlux, fmt=markr,markersize=10.0, alpha=alfval) plt.title('Flux Spectrum', fontsize=12) plt.xticks(fontsize=12) plt.yticks(fontsize=12) plt.xlabel(r'$\nu$ (MHz)',fontsize=12) plt.ylabel(r'Calibrated flux (mJy)',fontsize=12) return freqMHz_highsnr, taussec_highsnr, lmfitstdssec_highsnr
for i, fname in enumerate(filenames): # Find temperature split_name = fname.split('_') for part in split_name: try: c_temp = int(part) except ValueError: pass invT[i] = 1000.0 / (c_temp + 273.15) yprime = pd.read_csv(fname, header=0, index_col='# f', usecols=['# f', 'realY']) yprime = yprime * thickness / area ax1.loglog(yprime.index, yprime, '.', c=colors[i]) sigmas[i] = np.log10(yprime[yprime.index < 50].median()) ax1.axhline(10**sigmas[i], c='r') ax2.plot(invT[i], sigmas[i], 'o', c=colors[i]) plt.savefig('test_{:02d}.png'.format(i), dpi=96) m2 = LinearModel() # Initialize parameters for linear model p2 = m2.make_params() # Fit Arrhenius out2 = m2.fit(sigmas, p2, x=invT) EA = out2.values['slope'] * 8.6173303e-5 * -1000 / np.log10(np.e) ax2.plot(invT, out2.best_fit, 'r-', label=r'E$_A$ = {:.3f} eV'.format(EA)) ax2.legend() plt.show() plt.savefig('impedance_activation.png'.format(i), dpi=96)
label=f'By={f} mT') axes[0].set_xlabel('Gate voltage (V)') axes[0].set_ylabel('Phase difference (rad)') axes[0].legend() for i, g in enumerate(results['gate']): if i == 0: continue # Perform a linear fit field = results['field'] dphi = results['dphi'][:, i] if len(dphi) > 1: model = LinearModel() mask = np.logical_and(np.greater(field, -450), np.less(field, 450)) p = model.guess(dphi[mask], x=field[mask]) res = model.fit(dphi[mask], p, x=field[mask]) ex_field = np.linspace(min(0, np.min(field)), max(0, np.max(field))) axes[1].plot(ex_field, res.eval(x=ex_field), color=f'C{i}') axes[1].errorbar(field, dphi, yerr=0.15, fmt='+', color=f'C{i}', label=f'Vg={g} V') axes[1].set_xlabel('Parallel field (mT)') axes[1].set_ylabel('Phase difference (rad)') axes[1].set_ylim((0 if np.min(dphi) > 0 else None, None)) axes[1].legend()
def Fourier(): print "Fourier" plt.close() global x, y, La x1 = copy.copy(x) y1 = copy.copy(y) mini, maxi = getminmax() x = x[mini:maxi] y = y[mini:maxi] armonico = [] #numeros armonicos AN = [] # real tamanho = len(y) def list(vetor): newvetor = [] for i in vetor: newvetor.append(i) return newvetor x = list(x) y = list(y) a = [] for i in range(0, 21): a.append(0) Nx = [] for i in range(-1 * y.index(max(y)), y.index(max(y))): Nx.append(i) primeiro = 0 maior = 0 menor = 0 for i in x: if primeiro == 0: if not i == 0: menor = i primeiro = 1 if primeiro == 1: if i == 0: primeiro = 2 if primeiro == 1: maior = i yy = [] for i in range(len(Nx)): try: yy.append(y[i]) except: pass for i in range(len(yy)): #armonico.append(i) soma = 0 for j in range(len(yy) - 1): soma = soma + yy[j] * cos(2 * pi * i * Nx[j] / tamanho) if soma <= 0: pass else: AN.append(soma / tamanho) armonico.append(i) lambida = radiation(comboBoxrad.get()) menor = radians(menor / 2) maior = radians(maior / 2) for i in range(len(armonico)): armonico[i] = (i * lambida) / ((sin(maior) - sin(menor)) * 2) for i in range(len(armonico)): if armonico[i] < 0: armonico[i] *= -1 plt.figure(1) plt.subplot(221) plt.grid() plt.xlabel('position ($2\Theta$)') plt.ylabel("Intensity") plt.title("SAMPLE") plt.plot(x, y, linestyle='-', marker='o') plt.subplot(222) plt.grid() plt.plot(armonico[0:30], AN[0:30], c='k', linestyle='-', marker='o') plt.xlabel('L(nm)') plt.ylabel("A(L)") plt.title("SAMPLE - Fourier ") inicio = int(boxFmin.get()) fim = int(boxFmax.get()) if (inicio == fim - 1): fim += 1 elif (inicio == fim): fim += 2 elif (inicio > fim): fim = inicio + 3 y = AN[inicio:fim] x = armonico[inicio:fim] mod = LinearModel() pars = mod.guess(y, x=x) out = mod.fit(y, pars, x=x) XS = out.values['intercept'] / out.values['slope'] * -1 XS = int(XS) La = XS slope, intercept, r_value, p_value, std_err = stats.linregress(x, y) plt.subplot(212) plt.grid() plt.plot(armonico[0:30], AN[0:30], linestyle='--', marker='o') plt.plot(x, out.best_fit, 'r-', label='$L_A(nm)$: ' + str("XS")) plt.xlabel('L(nm)') plt.ylabel("A(L)") plt.title("SAMPLE - Fourier ") plt.legend() # Create a list of values in the best fit line abline_values = [slope * i + intercept for i in armonico] lx = [] ly = [] for i in range(len(abline_values)): if abline_values[i] >= 0: lx.append(armonico[i]) ly.append(abline_values[i]) plt.plot(lx, ly, 'red') x = x1 y = y1 plt.show()
def FourierDouble(): print "Fourier" plt.close() global x, y, xs, ys, La mini, maxi = getminmax() minis, maxis = stgetminmax() copyx = copy.copy(x) copyy = copy.copy(y) copyxs = copy.copy(xs) copyys = copy.copy(ys) x1 = copy.copy(x) y1 = copy.copy(y) x = x[mini:maxi] y = y[mini:maxi] xs = xs[minis:maxis] ys = ys[minis:maxis] AN, armonico = calc_Fourier(x, y) ANST, armonicoST = calc_Fourier(xs, ys) ANi, armonicoi = calc_Fourier_img(x, y) ANSTi, armonicoSTi = calc_Fourier_img(xs, ys) plt.figure(1) plt.subplot(221) plt.grid() plt.xlabel('L(nm)') plt.ylabel("A(L)") plt.title("SAMPLE") plt.plot(armonico[0:30], AN[0:30], linestyle='-', marker='o') plt.subplot(222) plt.grid() plt.plot(armonicoST[0:30], ANST[0:30], c='k', linestyle='-', marker='o') plt.xlabel('L(nm)') plt.ylabel("A(L)") plt.title("STANDARD ") newAN = [] newarmonico = [] for i in range(len(AN)): try: newarmonico.append(armonico[i]) cima = AN[i] * ANST[i] + ANi[i] * ANSTi[i] baixo = pow(ANST[i], 2) + pow(ANSTi[i], 2) newAN.append(cima / baixo) except: pass ############################ inicio = int(boxFminst.get()) fim = int(boxFmaxst.get()) if (inicio == fim - 1): fim += 1 elif (inicio == fim): fim += 2 elif (inicio > fim): fim = inicio + 3 y = newAN[inicio:fim] x = newarmonico[inicio:fim] mod = LinearModel() pars = mod.guess(y, x=x) out = mod.fit(y, pars, x=x) XS = out.values['intercept'] / out.values['slope'] * -1 La = int(XS) slope, intercept, r_value, p_value, std_err = stats.linregress(x, y) # Create a list of values in the best fit line abline_values = [slope * i + intercept for i in newarmonico] lx = [] ly = [] for i in range(len(abline_values)): if abline_values[i] >= 0: lx.append(newarmonico[i]) ly.append(abline_values[i]) x = x1 y = y1 plt.subplot(212) plt.grid() pdb.set_trace() plt.plot(newarmonico[0:10], newAN[0:10], c='k', linestyle='-', marker='o', label='$L_A(nm)$: ' + str(int(XS))) plt.plot(lx, ly, 'red') plt.xlabel('L(nm)') plt.ylabel("A(L)") plt.legend() plt.title("SAMPLE DECONVOLUTION ") x = copyx xs = copyxs y = copyy ys = copyys orig_stdout = sys.stdout f = open('fourierconvoluido.xy', 'w') sys.stdout = f for i in range(len(newarmonico[0:30])): print newarmonico[i], str(' '), newAN[i] sys.stdout = orig_stdout f.close() plt.show()
plt.savefig(figname, dpi=199) #plot semilog of viable cell concentration fig = plt.figure(3) viable_log = np.log(np.array(viable).astype(float)) mod = LinearModel() pars = mod.guess(viable_log[first_linearpoint:first_linearpoint + linearpoints], x=hours[first_linearpoint:first_linearpoint + linearpoints]) init = mod.eval(pars, x=hours[first_linearpoint:first_linearpoint + linearpoints]) out = mod.fit(viable_log[first_linearpoint:first_linearpoint + linearpoints], pars, x=hours[first_linearpoint:first_linearpoint + linearpoints]) slope = out.params['slope'].value intercept = out.params['intercept'].value y = slope * np.arange(-1, 10) + intercept doubling_time = int(np.log(2) / slope * 24) #doubling time in hours plt.plot(np.arange(-1, 10), y, 'r-', label='linear fit') plt.errorbar(hours, viable_log, yerr=viable_err / viable, label='viable cells', fmt='-o') plt.xlabel('days') plt.ylabel('ln(millions of cells/ml)') plt.legend(loc='upper left')