def plot_lnD_v_Tinv(Ds, Ts, save = False): ln_D = np.log(np.array(Ds)) T_inv = 1000./np.array(Ts) plt.plot(T_inv, ln_D, 'ro') T_inv_stack = np.column_stack([T_inv**1, T_inv**0]) p, pint, se = regress(T_inv_stack, ln_D, 0.05) ln_D_fit = np.dot(T_inv_stack, p) plt.plot(T_inv, ln_D_fit) plt.xlabel('1000/T (1/K)') plt.ylabel('ln(D)') if save != False: plt.savefig(save) slope = p[0] R = 5.189e19 Na = 6.023e23 E_act = -slope*R/Na E_act_int = - np.array(pint[0]) * R/Na return E_act, E_act_int
def test(): A = np.column_stack([T**0, T**1, T**2, T**3, T**4]) p, pint, se = regress(A, E, alpha=0.05) print p print pint print se # there should not be any np.nan in se assert ~np.isnan(se).all()
def regression(E1, E2, alpha=0.05): '''Perform regression but remove nan values''' xy = np.transpose(np.array([E1, E2])) xy = xy[~np.any(np.isnan(xy), axis=1)] x = xy[:, 0] A = np.column_stack((x**1, x**0)) y = xy[:, 1] pars, pint, se = regress(A, y, alpha) y = np.dot(A, pars) return x, y, pars, pint, se
def plot_msd(t, msd, f = 0.25, skiprows = 0, save = False, show = False, t_units = 'ps', msd_units = '$\AA^{2}$', slope = True, T = None, legend = False, label = None): # TODO make this simpler!!! ''' Plots a mean square displacement trajectory Args: save = filepath to save show = Turns off/on plt.show() Note: Both savefig and show can be done in the main function also slope = Turns slope on/off T = Temperature for legend f = fraction of data to discard for equilibration Units are only for the labels ''' if label!= None: plt.plot(t, msd, label = label, lw=2.5) else: plt.plot(t,msd, lw=2.5) if slope == True: # Cutting out the equilibration data and using the rest to fit slope t_cut = t[int(len(t)*f):] msd_cut= msd[int(len(t)*f):] # Fitting using linear regression and 95 percent confidence intervals - p = [slope, intercept] t_stack = np.column_stack([t_cut**1, t_cut**0]) p, pint, se = regress(t_stack, msd_cut, 0.05) msd_fit = np.dot(t_stack,p) plt.plot(t_cut, msd_fit, 'r--',lw=2.5) plt.xlabel('Time ({0})'.format(t_units)) plt.ylabel('MSD ({0})'.format(msd_units)) if legend ==True: plt.legend(loc = 'best') if save != False: plt.savefig(save) if show == True: plt.show() if slope == True: return p, pint, se
curve1 = df1.iloc[:, 0] curve2 = df1.iloc[:, 1] #curve1.plot() #curve2.plot() #plt.show() T = np.array(np.linspace(0, 1, 419)) print(T) E1 = np.array(curve1) print("T length " + str(E1.size)) E2 = np.array(curve2) print("T length " + str(E2.size)) E = E1 - E2 # columns of the x-values for a line: constant, T A = np.column_stack([T**0, T]) p, pint, se = regress(A, E, alpha=0.005) b = u.ufloat((p[0], se[0])) m = u.ufloat((p[1], se[1])) @u.wrap def f(b, m): X, = fsolve(lambda x: b + m * x, 800) return X print("EER point is " + str(f(b, m)))
# _*_ coding: utf-8 _*_ from pycse import regress import numpy as np time = np.array([0.0, 50.0, 100.0, 150.0, 200.0, 250.0, 300.0]) Ca = np.array([50.0, 38.0, 30.6, 25.6, 22.2, 19.5, 17.4]) * 1e-3 T = np.column_stack([time**0, time, time**2, time**3, time**4]) alpha = 0.05 p, pint, se = regress(T, Ca, alpha) print(pint) # new one import numpy as np from scipy.stats.distributions import t time = np.array([0.0, 50.0, 100.0, 150.0, 200.0, 250.0, 300.0]) Ca = np.array([50.0, 38.0, 30.6, 25.6, 22.2, 19.5, 17.4]) * 1e-3 T = np.column_stack([time**0, time, time**2, time**3, time**4]) p, res, rank, s = np.linalg.lstsq(T, Ca) # the parameter are now in p # compute the confidence intervals n = len(Ca) k = len(p)
def python_calc_single_U(self, key, alphas=(-0.15, -0.07, 0, 0.07, 0.15)): '''This is a function to calculate the linear response U of a single atom''' from pycse import regress from uncertainties import ufloat calc_name = os.path.basename(self.espressodir) if not isdir('Ucalc'): os.mkdir('Ucalc') cwd = os.getcwd() os.chdir(calc_name + '-1-pert') # First assert that the calculations are done for alpha in alphas: assert isfile('results/alpha_{0}.out'.format(alpha)) assert self.check_calc_complete( filename='results/alpha_{0}.out'.format(alpha)) # First create the matrix for storing occupancies occ_0s, occ_fs = [], [] # Store the initial and final occupations in arrays for alpha in alphas: outfile = open('results/alpha_{0}.out'.format(alpha)) lines = outfile.readlines() calc_started = False calc_finished = False for line in lines: # We first want to read the initial occupancies. This happens after # the calculation starts. if line.startswith(' Self'): calc_started = True if line.startswith(' End'): calc_finished = True # We will first if not line.startswith('atom '): continue if not int(line.split()[1]) == key + 1: continue occ = float(line.split()[-1]) if calc_started == True and calc_finished == False: occ_0s.append(occ) elif calc_finished == True: occ_fs.append(occ) else: continue os.chdir(cwd) x = np.column_stack([np.array(alphas)**0, np.array(alphas)]) bare_slope, bare_uncert, bare_e = regress(x, occ_0s, 0.05) convg_slope, convg_uncert, convg_e = regress(x, occ_fs, 0.05) bare_slope = ufloat(bare_slope[1], (abs(bare_uncert[1][0] - bare_uncert[1][1])) / 2) convg_slope = ufloat(convg_slope[1], (abs(convg_uncert[1][0] - convg_uncert[1][1])) / 2) U = 1 / bare_slope - 1 / convg_slope return U