def calcTvalue(p, df): t = None if (df == 1): t = math.cos(p*math.pi/2.)/math.sin(p*math.pi/2.) elif (df == 2): t = math.sqrt(2./(p*(2. - p)) - 2.) else: a = 1./(df - 0.5) b = 48./(a*a) c = ((20700.*a/b - 98.)*a - 16.)*a + 96.36 d = ((94.5/(b + c) - 3.)/b + 1.)*math.sqrt(a*math.pi*0.5)*df x = d*p y = pow(x, 2./df) if (y > 0.05 + a): x = Norm_z(0.5*(1. - p)) y = x*x if (df < 5.): c = c + 0.3*(df - 4.5)*(x + 0.6) c = (((0.05*d*x - 5.)*x - 7.)*x - 2.)*x + b + c y = (((((0.4*y + 6.3)*y + 36.)*y + 94.5)/c - y - 3.)/b + 1.)*x y = a*y*y if (y > 0.002): y = math.exp(y) - 1. else: y = 0.5*y*y + y t = sqrt(df*y) else: y = ((1./(((df + 6.)/(df*y) - 0.089*d - 0.822)*(df + 2.)*3.) + 0.5/(df + 4.))*y - 1.)*(df + 1.)/(df + 2.) + 1./y; t = math.sqrt(df*y) return t
def SmootBound_LSW(self, myMDP, Gamma, countXVec, beta, startDist): lInfty=int(numpy.linalg.norm(countXVec, Inf)) Vals=[] for k in range(lInfty): Vals.append(self.varPhi_w(countXVec, k, Gamma)*math.exp(-k*beta)) upperBound=numpy.max(Vals) return upperBound
def SmootBound_LSW(self, myMDP, Gamma, countXVec, beta, startDist): lInfty = int(numpy.linalg.norm(countXVec, Inf)) Vals = [] for k in range(lInfty+1): Vals.append(self.varPhi_w(countXVec, k, Gamma) * math.exp(-k*beta)) upperBound = numpy.max(Vals) return upperBound
def SmoothBound_LSL(self,featurmatrix, myMDP, countXVec, rho, regCoef,beta,numTrajectories): normPhi = numpy.linalg.norm(featurmatrix,2) maxRho = numpy.linalg.norm(rho,Inf) c_lambda = normPhi*maxRho/(math.sqrt(2*regCoef)) #print('===============================================') #print(regCoef) l2Rho = numpy.linalg.norm(rho) phi_k = 0 Vals = [] for k in range(0, numTrajectories+1): minVal=0 for s in range(len(myMDP.getStateSpace())): minVal = minVal+rho[s] * min(countXVec[s]+k, numTrajectories) phi_k = c_lambda*math.sqrt(minVal)+l2Rho Vals.append((math.pow(phi_k,2))*math.exp(-k*beta)) upperBound=max(Vals) return upperBound
def computeAggregateSmoothBound(self, z,beta, s,mdp,distance_upper_bound): partitionPoint=int((len(z)+s)/2) a= int(s/beta) #rho=self.computeRho(partitionPoint, Dists,a) temp_1=0 k=0 t_0=partitionPoint+(k+1)*s tempList=[] #print(str(t_0)+" "+str(len(z))) while t_0 <=len(z): rho=self.computeRho(z, t_0 , a, mdp,distance_upper_bound) temp_2=rho*math.exp(-beta*k) tempList.append(temp_2) temp_1=max(temp_1, temp_2) k+=1 t_0=partitionPoint+(k+1)*s #print(tempList) return 2*temp_1
def SmoothBound_LSL(self,featurmatrix, myMDP, countXVec, rho, regCoef,beta,numTrajectories): normPhi=numpy.linalg.norm(featurmatrix) maxRho=numpy.linalg.norm(rho,Inf) c_lambda=normPhi*maxRho/(math.sqrt(2*regCoef)) #print('===============================================') #print(regCoef) l2Rho=numpy.linalg.norm(rho) phi_k=0 Vals=[] for k in range(0,numTrajectories): minVal=0 for s in range(len(myMDP.getStateSpace())-1): minVal=minVal+rho[s]*min(countXVec[s]+k,numTrajectories) phi_k=c_lambda*math.sqrt(minVal)+l2Rho Vals.append((math.pow(phi_k,2))*math.exp(-k*beta)) upperBound=max(Vals) #print('Smooth upper bound= '+str(upperBound)) return upperBound
def computeAggregateSmoothBound(self, z, beta, s, mdp, distance_upper_bound): partitionPoint=int((len(z)+s)/2) a= int(s/beta) #rho=self.computeRho(partitionPoint, Dists,a) temp_1=0 k=0 t_0=partitionPoint+(k+1)*s tempList=[] #print(str(t_0)+" "+str(len(z))) while t_0 <=len(z): rho=self.computeRho(z, t_0 , a, mdp,distance_upper_bound) temp_2=rho*math.exp(-beta*k) tempList.append(temp_2) temp_1=max(temp_1, temp_2) k+=1 t_0=partitionPoint+(k+1)*s #print(tempList) return 2*temp_1
def f(x): f = (math.exp(-x * x / 2)) / math.sqrt(2.0 * math.pi) return f
def exponen(x): return math.exp(x)
def f(inp): return (math.exp(-inp * inp / 2)) / math.sqrt(2.0 * math.pi)
def run_fitting(i_img): # for this thesis just batch simulations, no fitting. # passing the simulation through the fitting mechanism # still useful for processing experimental and # simulation patterns simultaneously global first_run global batch_sim global init_radius global init_height global init_height_flattening_ratio global init_lattice_length global init_damping_length global init_disorder_parameter global init_beam_intensity if batch_sim == True: # setting the simulation parameters d_eff = 11.6 * (0.1 * (i_img - 11)) / 150 init_lattice_length = 2 * np.pi / (0.628 + 1.499 * math.exp(-d_eff / 3.31)) init_height = 1.519 + 1.367 * d_eff - 0.038 * (d_eff**2) init_radius = init_height / 3.1 init_height_flattening_ratio = 0.66 init_disorder_parameter = 0.25 init_damping_length = 30 * nm init_beam_intensity = 4.15e+09 real_data = load_real_data(i_img) fit_objective = ba.FitObjective() fit_objective.addSimulationAndData(get_simulation, real_data) fit_objective.initPrint(1) fit_objective.initPlot(1, PlotObserver( )) #plotting frequency on screen (increase if OpenGL error) params = ba.Parameters( ) #parameter limits only relevant for full fitting, otherwise ignored min_height = 0.1 max_height = 20 min_lattice_length = 0.1 max_lattice_length = 20 min_radius = 0.1 max_radius = 20 min_damping_length = 10 max_damping_length = 1000 min_disorder_parameter = 0.1 max_disorder_parameter = 0.5 min_beam_intensity = 1e+8 max_beam_intensity = 1e+12 params.add("radius", value=init_radius, min=min_radius, max=max_radius, step=0.1 * nm, vary=True) params.add("height", value=init_height, min=min_height, max=max_height, step=0.1 * nm, vary=True) params.add("height_flattening_ratio", value=init_height_flattening_ratio, min=0.55, max=0.75, step=0.01, vary=True) params.add("lattice_length", value=init_lattice_length, min=min_lattice_length, max=max_lattice_length, step=0.1 * nm, vary=True) params.add("damping_length", value=init_damping_length, min=min_damping_length, max=max_damping_length, step=10 * nm, vary=True) params.add("disorder_parameter", value=init_disorder_parameter, min=min_disorder_parameter, max=max_disorder_parameter, step=0.01, vary=True) params.add("beam_intensity", init_beam_intensity, min=min_beam_intensity, max=max_beam_intensity, vary=False) minimizer = ba.Minimizer() minimizer.setMinimizer("Test") # normal simulation (no fitting) #minimizer.setMinimizer("Minuit2", "Migrad", "MaxFunctionCalls=0;Strategy=2;") # minimizer for fitting result = minimizer.minimize(fit_objective.evaluate, params) # runs the simulation fit_objective.finalize(result) fig_name = output_folder + 'fitting_img_#32_' + repr(i_img) + '.png' plt.savefig(fig_name, dpi=500) plt.close(1) print("Fitting completed.") print("chi2:", result.minValue()) for fitPar in result.parameters(): print(fitPar.name(), fitPar.value, fitPar.error) experimental_data = fit_objective.experimentalData() simulation_data = fit_objective.simulationResult() difference = fit_objective.relativeDifference() zmin = 0.1 # range of the colormap zmax = 100 mpl.rcParams['image.cmap'] = 'jet' # saves figures in jet colormap plt.figure(2, figsize=(8, 6)) ba.plot_colormap(experimental_data, title="Experimental data", zmin=zmin, zmax=zmax, units=ba.AxesUnits.QSPACE, xlabel=None, ylabel=None, zlabel='', aspect=None) fig_name = output_folder + 'exp_data_#32_' + repr(i_img) + '_jet' + '.png' plt.savefig(fig_name, dpi=500) plt.close(2) plt.figure(3, figsize=(8, 6)) ba.plot_colormap(simulation_data, title="Simulation result", zmin=zmin, zmax=zmax, units=ba.AxesUnits.QSPACE, xlabel=None, ylabel=None, zlabel='', aspect=None) fig_name = output_folder + 'sim_data_#32_' + repr(i_img) + '_jet' + '.png' plt.savefig(fig_name, dpi=500) plt.close(3) mpl.rcParams[ 'image.cmap'] = 'nipy_spectral' #saves figures in spectral colormap plt.figure(4, figsize=(8, 6)) ba.plot_colormap(experimental_data, title="Experimental data", zmin=zmin, zmax=zmax, units=ba.AxesUnits.QSPACE, xlabel=None, ylabel=None, zlabel='', aspect=None) fig_name = output_folder + 'exp_data_#32_' + repr( i_img) + '_spectrum' + '.png' plt.savefig(fig_name, dpi=500) plt.close(4) plt.figure(5, figsize=(8, 6)) ba.plot_colormap(simulation_data, title="Simulation result", zmin=zmin, zmax=zmax, units=ba.AxesUnits.QSPACE, xlabel=None, ylabel=None, zlabel='', aspect=None) fig_name = output_folder + 'sim_data_#32_' + repr( i_img) + '_spectrum' + '.png' plt.savefig(fig_name, dpi=500) plt.close(5)
""" Euler method solving RC circuit ODE. """ from numpy import array, arange, zeros, empty, linalg, math import pylab as pl rc = 2.0 dt = 0.5 n = 1000 t = 0.0 q = 1.0 qt = [] qt0 = [] time = [] for i in range(n): t = t + dt q = q - q * dt / rc q0 = math.exp(-t / rc) qt.append(q) qt0.append(q0) time.append(t) pl.plot(time, qt, 'ro', label='Euler') pl.plot(time, qt0, 'k-', label='Analytical') pl.xlabel('Time') pl.ylabel('charge') pl.xlim(0, 12) pl.ylim(-0.2, 1.0) pl.legend(loc='upper right') pl.show()
def f(x): f = (math.exp(-x*x/2))/math.sqrt(2.0*math.pi) return f
def estimateP(n,Kn): P = 1 - math.exp((-6 * Kn**2)/(n**3 + n**2)) return P
def plotTF(self , labelX=True , labelY=True, fontsize=12 , ylim=None, patchColor=None , labelColor=None , multicolor=False ,axes=None ,maxAtoms=None, recenter=None,keepValues=False, french=False,Alpha=False,logF=False): """ A time Frequency plot routine using Matplotlib each atom of the approx is plotted in a time-frequency plane with a gray scale for amplitudes Many options are available: - labelX : whether or not to add the Time label on the X axis - labelY : whether or not to add the Frequency label on the Y axis - fontsize : the label fontSize - ylim : the frequency range of the plot - patchColor : specify a single color for all atoms - maxAtoms : specify a maximum number of atom to take into account. Usually atoms are ordered by decreasing amplitude due to MP rules, meaning only the most salient atoms will be plotted - recenter : a couple of values to offset the time and frequency localization of atoms - keepValues : use atom Amplitudes for atom colorations - french : all labels in french - Alpha : use transparency - logF : frequency axis in logarithmic scale """ # plt.figure() if french: labx = "Temps (s)" laby = "Frequence (Hz)" else: labx = "Time (s)" laby = "Frequency (Hz)" Fs = self.samplingFrequency # first loop to recover max value maxValue = 0 maxFreq = 1.0 minFreq = 22000.0 if not keepValues: valueArray = array([math.log10(abs(atom.getAmplitude())) for atom in self.atoms]) else: valueArray = array([abs(atom.getAmplitude()) for atom in self.atoms]) if multicolor: cmap = cc.LinearSegmentedColormap('jet',abs(valueArray) ) for atom in self.atoms: if abs(atom.mdct_value) > maxValue: maxValue = abs(atom.mdct_value) # normalize to 0 -> 1 # if min(valueArray) < 0: if len(valueArray) > 0 and not keepValues: valueArray = valueArray - min(valueArray) valueArray = valueArray/max(valueArray) # Get target axes if axes is None: axes = plt.gca() # normalize the colors # if multicolor: # normedValues = cc.Normalize(valueArray) if maxAtoms is not None: maxTom = min(maxAtoms,len(self.atoms)) else: maxTom = len(self.atoms) # Deal with static offsets if recenter is not None: yOffset= recenter[0] xOffset= recenter[1] else: yOffset,xOffset = 0,0 # print yOffset,xOffset if Alpha: alphaCoeff=0.6 else: alphaCoeff=1 patches = [] colors = [] for i in range(maxTom): atom = self.atoms[i] L = atom.length K = L/2 freq = atom.frequencyBin f = float(freq)*float(Fs)/float(L) bw = ( float(Fs) / float(K) ) #/ 2 p = float(atom.timePosition + K) / float(Fs) l = float(L - K/2) / float(Fs) # print "f = " ,f , " Hz" if f>maxFreq: maxFreq = f+bw+bw if f < minFreq: minFreq = f-bw-10*bw # xy = p, f-bw, xy = p - xOffset, f - yOffset, width, height = l, bw # xy = p - xOffset, log10(f) - yOffset, if logF: xy = p - xOffset , (12*log2(f - yOffset)) height = 1.0 # BUGFIX # print xy , f colorvalue = math.sqrt(valueArray[i]) if multicolor: patchtuplecolor = (math.sqrt(colorvalue), math.exp(-((colorvalue - 0.35)**2)/(0.15)), 1-math.sqrt(colorvalue)) colors.append(colorvalue) else: if patchColor is None: patchtuplecolor = (1-math.sqrt(colorvalue),1- math.sqrt(colorvalue),1- math.sqrt(colorvalue)) colors.append(colorvalue) else: patchtuplecolor = patchColor colors.append(patchColor) # patchtuplecolor = (colorvalue, colorvalue, colorvalue) # patch = mpatches.Rectangle(xy, width, height, facecolor=patchtuplecolor, edgecolor=patchtuplecolor , zorder=colorvalue ) # xy = p + L/2 , f-bw, patch = mpatches.Ellipse(xy, width, height, facecolor=patchtuplecolor, edgecolor=patchtuplecolor , zorder=colorvalue ) if keepValues: patches.append(patch) else: axes.add_patch(patch) if keepValues: # first sort the patches by values sortedIndexes = valueArray.argsort() PatchArray = array(patches) p = PatchCollection(PatchArray[sortedIndexes].tolist(),linewidths=0.,cmap=matplotlib.cm.copper_r,match_original=False, alpha=alphaCoeff) # print array(colors).shape p.set_array(valueArray[sortedIndexes]) axes.add_collection(p) if labelColor is None: labelColor = 'k' # finalize graphic options axes.set_xlim(0- xOffset , (self.length)/float(Fs)- xOffset) if ylim is None: axes.set_ylim(max(0 ,minFreq)- yOffset , maxFreq- yOffset) else: axes.set_ylim(ylim) if labelY: axes.set_ylabel(laby , fontsize=fontsize , color=labelColor) # plt.ylabel.set_color(labelColor) else: plt.setp(axes, 'yticklabels', []) if labelX: axes.set_xlabel(labx, fontsize=fontsize) else: plt.setp(axes, 'xticklabels', []) if keepValues: plt.colorbar(p)