def gauss(x, p, mode='eval'): """Gaussian defined by amplitide Function: :math:`f(x) = k + m*x + p_2 \exp\left(\\frac{-(x - p_0)^2}{2p_1^2}\\right)` """ try: if mode == 'eval': cent=p[0];wid=p[1];amp=p[2];const=p[3];slope=p[4] #out = const + amp * np.exp(-1.0 * (x - cent)**2 / (2 * wid**2)) #Inserted the 0.5* because we want FWHM out, not HWHM #out = const + slope*x + amp * np.exp(-1.0 * (x - cent)**2 / (2 * (0.5*wid)**2)) conversion =2.0*np.sqrt(2.0*np.log(2)) #Hutchings et al. Introduction to the characterisation of residual stress by neutron diffraction.2005. Page 159 #conversion = 2.0 out = const + slope*x + amp * np.exp(-1.0 * (x - cent)**2 / (2 * (wid/conversion)**2)) elif mode == 'params': out = ['cent', 'sigma', 'amp', 'const', 'slope'] elif mode == 'name': out = "Gaussian" elif mode == 'guess': g = fitfuncs.peakguess(x, p) out = [g[0], g[1] / (4 * np.log(2)), g[3], g[4], g[5]] else: out = [] except: out = [0,0,0,0,0] return out
def simulated_annealing_plot(problem, values_for_schedule): """[Figure 4.5] CAUTION: This differs from the pseudocode as it returns a state instead of a Node.""" schedule = exp_schedule(values_for_schedule[0], values_for_schedule[1], values_for_schedule[2]) x = list() y = list() current = Node(problem.initial) for t in range(sys.maxsize): T = schedule(t) if T == 0: plt.scatter(x, y) plt.show() return current.state neighbors = current.expand(problem) if not neighbors: plt.scatter(x, y) plt.show() return current.state next_choice = random.choice(neighbors) delta_e = problem.value(next_choice.state) - problem.value( current.state) y.append(problem.value(current.state)) x.append(t) if delta_e > 0 or probability(np.exp(delta_e / T)): current = next_choice
def reflection(xvals, p): #position=p[0]; intensity=p[1]; slit=p[2]; stth=p[3]; background=p[4]; thickness=p[5]; absorption=p[6] x0 = p[0] i0 = p[1] w = p[2] theta = p[3] ib = p[4] thick = p[5] u = p[6] th = np.deg2rad(theta) p0 = w * np.sin(th) / np.sin(2 * th) out = [] for x in xvals: if x < (x0 - p0): val = ib elif ((x >= (x0 - p0)) and (x < x0)): val = i0 * ((x - x0 + p0) / u) - i0 * (np.sin(th) / (2 * u * u)) * (1 - np.exp( (-2 * u / np.sin(th)) * (x - x0 + p0))) + ib elif ((x >= x0) and (x < (x0 + p0))): val = i0 * (np.sin(th) / (2 * u * u)) * (np.exp( (-2 * u / np.sin(th)) * (x - x0 + p0)) - 2 * np.exp( (-2 * u / np.sin(th)) * (x - x0)) + 1) + i0 * ((x0 + p0 - x) / u) + ib elif (x >= (x0 + p0)): val = i0 * np.sin(th) / ( 2 * u * u) * (np.exp(-2 * u * (x - x0 + p0) / np.sin(th)) - 2 * np.exp(-2 * u * (x - x0) / np.sin(th)) + np.exp(-2 * u * (x - x0 - p0) / np.sin(th))) + ib out = out + [val] return np.array(out)
def func(x, a1, a2, a3): return a1 * x + a2 / (1 + np.exp(-a3 * x)) - a2 / 2
def reflection1(xvals, parms): #position=p[0]; intensity=p[1]; slit=p[2]; stth=p[3]; background=p[4]; thickness=p[5]; absorption=p[6] x0 = parms[0] i0 = parms[1] w = parms[2] theta = parms[3] ib = parms[4] thick = parms[5] u = parms[6] th = np.deg2rad(theta) p = w * np.sin(th) Atot = w * w / np.sin(2.0 * th) out = [] ni = 5 irng = np.array(range(1, ni + 1)) for x in xvals: if x < (x0 - p): val = ib elif ((x0 - p) < x and x0 > x): l1 = x - (x0 - p) nrleft = int(np.ceil(l1 / (2 * p) * ni)) if nrleft < 1: nrleft = 1 irngleft = np.array(range(1, nrleft + 1)) dl1 = l1 / float(nrleft) dl = irngleft * dl1 triA = dl * dl / np.tan(th) #triangle areas secA = [triA[0] ] + [triA[i] - triA[i - 1] for i in range(1, nrleft)] #section areas secA = np.array(secA) m1 = np.linspace( x0 - p + dl1 / 2.0, x - dl1 / 2.0, nrleft ) #section midpoint position - path length calculated from this plen = np.abs(2 * m1 / np.sin(th)) val = ib + np.sum(i0 * secA * np.exp(-u * plen)) elif (x0 <= x) and (x0 + p >= x): l1 = p nrleft = int(np.ceil(l1 / (2 * p) * ni)) if nrleft < 1: nrleft = 1 irngleft = np.array(range(1, nrleft + 1)) dl1left = l1 / float(nrleft) dlleft = irngleft * dl1left triAleft = dlleft * dlleft / np.tan(th) #triangle areas secAleft = [triAleft[0]] + [ triAleft[i] - triAleft[i - 1] for i in range(1, nrleft) ] #section areas secAleft = np.array(secAleft) m1left = np.linspace(x0 - p + dl1left / 2.0, x0 - dl1left / 2.0, nrleft) + (x - x0) plenleft = np.abs(2 * m1left / np.sin(th)) valleft = np.sum(i0 * secAleft * np.exp(-u * plenleft)) l2 = x - x0 nrright = int(np.ceil(x - x0 / (2 * p) * ni)) if nrright < 1: nrright = 1 irngright = np.array(range(1, nrright + 1)) dl1right = l2 / float(nrright) dlright = p - np.append(0.0, dl1right * irngright) triAright = dlright * dlright / np.tan(th) secAright = [ triAright[i] - triAright[i + 1] for i in range(nrright) ] secAright = np.array(secAright) m1right = np.linspace(x - x0 - dl1right / 2.0, dl1right / 2.0, nrright) plenright = np.abs(2 * m1right / np.sin(th)) valright = np.sum(i0 * secAright * np.exp(-u * plenright)) val = ib + valleft + valright elif (x > x0 + p): l1 = p #nrleft = int(np.ceil(l1/(x-(x0-p))*ni)); nrleft = int(np.ceil(l1 / (2 * p) * ni)) if nrleft < 1: nrleft = 1 irngleft = np.array(range(1, nrleft + 1)) dl1left = l1 / float(nrleft) dlleft = irngleft * dl1left triAleft = dlleft * dlleft / np.tan(th) #triangle areas secAleft = [triAleft[0]] + [ triAleft[i] - triAleft[i - 1] for i in range(1, nrleft) ] #section areas secAleft = np.array(secAleft) m1left = np.linspace(x0 - p + dl1left / 2.0, x0 - dl1left / 2.0, nrleft) + (x - x0) plenleft = np.abs(2 * m1left / np.sin(th)) valleft = np.sum(i0 * secAleft * np.exp(-u * plenleft)) l2 = p nrright = int(np.ceil(l2 / (2 * p) * ni)) if nrright < 1: nrright = 1 irngright = np.array(range(1, nrright + 1)) dl1right = l2 / float(nrright) dlright = p - np.append(0.0, dl1right * irngright) triAright = dlright * dlright / np.tan(th) secAright = [ triAright[i] - triAright[i + 1] for i in range(nrright) ] secAright = np.array(secAright) m1right = np.linspace(dlright[0] - dl1right / 2.0, dlright[-1] + dl1right / 2.0, nrright) + (x - (x0 + p)) plenright = np.abs(2 * m1right / np.sin(th)) valright = np.sum(i0 * secAright * np.exp(-u * plenright)) val = ib + valleft + valright out = out + [val] return np.array(out)
def sigmoid(z): #e的-z次方 # g(z) = 1/(1+e^(-z)) #z=W^tX return (1.0 / (1 + np.exp(-z)))
# time with actual spiking activity spike_times = [timeline[i + 1] for i, dv in enumerate(dVs) if dv > 0] time_first_spike, time_last_spike = spike_times[0], spike_times[-1] active_time = int(time_last_spike - time_first_spike) # ms # print("First spike occured at {}, last spike at {}".format(time_first_spike, time_last_spike)) # mean spike height mean_decrease = np.mean([dv for dv in dVs if dv < 0]) dv_mean = np.mean([dv - mean_decrease for dv in dVs if dv > 0 ]) # mean decrease per timestep needs to be added # because it exists even if there is a spike print("Mean Spike Height: {} mV".format(dv_mean)) # expected spike height exp_tc = np.exp(float(-ts) / tau_m) # time constant multiplier dv_expected = (tau_m / cm) * weight * (1.0 - exp_tc ) # equation for pulse input print("Expected Spike Height: {} mV".format(dv_expected)) # Difference discrepancy = dv_expected - dv_mean print("Discrepancy between mean and expected: {} mV".format(discrepancy)) # TODO Open Question: Is the discrepancy small enough and can the above formula be applied? # Use the mean spike height to count the spikes spike_height = dv_mean spike_count = 0 spikes = [dv for dv in dVs if dv > 0] for spike in spikes:
def exp_schedule(k, lam, limit): """One possible schedule function for simulated annealing""" return lambda t: (k * np.exp(-lam * t) if t < limit else 0)