Guest User

Untitled

a guest
Jan 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. # importing all needed packages
  2. from scipy.optimize import leastsq
  3. from numpy.lib import loadtxt
  4. from math import pi, e
  5. from pylab import *
  6.  
  7. data_filename_1 = "Total_data.txt" # file structured with two columns, separated by a tab
  8. data_filename_2 = "Background_data.txt"
  9.  
  10. def load_data_1():
  11. """Return a two-dimensional array containing the data from the first column
  12. and the data from the second column. Preserve the structure when
  13. converting to an array.
  14. """
  15.  
  16. return loadtxt(data_filename_1)
  17.  
  18. def load_data_2():
  19. """Return a two-dimensional array containing the data from the first column
  20. and the data from the second column. Preserve the structure when
  21. converting to an array.
  22. """
  23.  
  24. return loadtxt(data_filename_2)
  25.  
  26. def peval(p, x):
  27. """Calculate the number of decays in each time interval in array times,
  28. based on parameters in list p.
  29. """
  30.  
  31. return p[0] * e**(-p[1] * x) + p[2]
  32.  
  33. def load_data():
  34. load_data1 = load_data_1()
  35. load_data2 = load_data_2()
  36.  
  37. ave2 = sum(data_2[:,1])/len(data_2[:,1])
  38.  
  39. load_data_3 = zeros(shape(load_data1))
  40. count =0 #integer counter for current index in array
  41. for i in load_data1[:,1]: #subtracts the total activity by the average background activity
  42. load_data_3[count][0]= count + 1
  43. load_data_3[count][1] = round(load_data1[count][1]-ave2, 2)
  44. count += 1
  45. #print load_data1
  46. #print load_data2
  47. #print load_data_3
  48. return load_data_3
  49.  
  50. # function to calculate the residuals at a given set of parameters p
  51. # the second and third parameters are actual data
  52. def residuals(p, x, y):
  53. return y - peval(p, x)
  54.  
  55.  
  56. if __name__ == '__main__':
  57. #Total Activity Data
  58. data_1 = load_data_1() # data will be an array, with columns and rows
  59. times_1 = data_1[:, 0] # gets data satisfying requirements of: any row, column 0
  60. counts_1 = data_1[:, 1] # gets data satisfying requirements of: any row, column 1
  61. delta_counts_1 = (sum(counts_1)*20)**0.5
  62. print sum(counts_1)
  63. print delta_counts_1
  64. #Background Activity Data
  65. data_2 = load_data_2() # data will be an array, with columns and rows
  66. times_2 = data_2[:, 0] # gets data satisfying requirements of: any row, column 0
  67. counts_2 = data_2[:, 1] # gets data satisfying requirements of: any row, column 1
  68. delta_counts_2 = sum(counts_2)**0.5
  69. #Total - Background Activity Data
  70. data_3 = load_data()
  71. times_3 = data_3[:,0]
  72. counts_3 = data_3[:,1]
  73. delta_counts_3 = (sum(counts_1)+sum(counts_2))**0.5
  74.  
  75.  
  76. #Create estimates for initial parameters
  77. i = 0
  78. while counts_3[i] > max(counts_3)/e:
  79. i += 1
  80. tau = times_3[i]
  81. gamma = 1 / tau
  82. p0 = (max(counts_3), gamma, 0) # initial guesses for p[0], p[1] in function 'calculation'
  83. success = False
  84. while not success:
  85. p_final, cov_x, info, mesg, success = leastsq(residuals, p0, args=(times_3, counts_3), full_output = True)
  86.  
  87.  
  88. # Calculation of goodness of fit
  89. dof = len(times_3) - 3 # Degrees of freedom, we used 3 parameters
  90. chi_squared = sum( (residuals(p_final, times_3, counts_3)/delta_counts_3)**2 )
  91.  
  92.  
  93. # Use estimate of covariance to get stdev for each parameter
  94. stdev = [((cov_x[i, i])**0.5) for i in xrange(3)] # create list of sqrts of variance of each parameter
  95. # Print parameter results
  96. # #print "Parameters are:\nA = %0.5f +/- %0.5f\ngamma = %0.5f +/- %0.5f\nomega = %0.5f +/- %0.5f\nalpha = %0.5f +/- %0.5f\npsi = %0.5f +/- %0.5f\n" % (p_final[0], stdev[0], p_final[1], stdev[1], p_final[2], stdev[2], p_final[3], stdev[3], p_final[4], stdev[4])
  97. print "Chi squared over v is %0.3f" % (chi_squared / dof)
  98. print stdev
  99. print p_final
  100.  
  101.  
  102.  
  103. #Data plotting
  104. #Ba-137m Decay Plot
  105.  
  106. xlabel("Time Intervals")
  107. ylabel("Activity (Bq)")
  108. title("Ba-137m Decay")
  109.  
  110. plot(times_3, counts_3) # Plotting the actual data
  111. # print peval(p_final, times_3)
  112. plot(times_3, peval(p_final, times_3), 'ro-') # Plotting the fit
  113. # errorbar(times_3, counts_3, delta_counts_3) # Plotting the error bars
  114. #show () # Displays the graph
  115.  
  116. #Total Activity Plot
  117.  
  118.  
  119. xlabel("Time Intervals")
  120. ylabel("Activity (Bq)")
  121. title("Total Activity")
  122.  
  123. plot(times_1, counts_1)
  124. #show()
  125.  
  126.  
  127. #Background Activity Plot
  128. xlabel("Time Intervals")
  129. ylabel("Activity (Bq)")
  130. title("Background Activity")
  131.  
  132. plot(times_2, counts_2)
  133. show()
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. #Plotting of histogram of residuals
  147. #hist(residuals(p_final, times_3, counts_3))
  148. #show()
  149.  
  150. # Residual plotting
  151. #xlabel("Time Intervals")
  152. #ylabel("Residual (Bq)")
  153. #plot(times_3, residuals(p_final, times_3, counts_3)) # Plotting residuals
  154. #show() # Displays the graph
Add Comment
Please, Sign In to add comment