Pulse shape fitting

Neutron pulse


In [1]:
import os
import sys
root_folder = os.path.dirname(os.getcwd())
sys.path.append(root_folder)
from ResoFit._pulse_shape import NeutronPulse
from ResoFit._pulse_shape import ProtonPulse
from lmfit import Parameters
import numpy as np
import matplotlib.pyplot as plt
from lmfit import Model
import pprint
from lmfit.models import LinearModel

In [2]:
%matplotlib notebook

In [3]:
# path1 = '/Users/Shawn/Dropbox (ORNL)/Postdoc_Research/neutron_beam_shape/SNS/neutron_pulse/source_section_1.dat'
# path2 = '/Users/Shawn/Dropbox (ORNL)/Postdoc_Research/neutron_beam_shape/SNS/neutron_pulse/source_section_2.dat'
path1 = '/Users/y9z/Dropbox (ORNL)/Postdoc_Research/neutron_beam_shape/SNS/neutron_pulse/source_section_1.dat'
path2 = '/Users/y9z/Dropbox (ORNL)/Postdoc_Research/neutron_beam_shape/SNS/neutron_pulse/source_section_2.dat'

Model index:

  • 1: 'ikeda_carpenter',
  • 2: 'cole_windsor',
  • 3: 'pseudo_voigt',
  • 4: 'ikeda_carpenter_jparc',
  • 5: 'cole_windsor_jparc'

In [4]:
neutron_pulse = NeutronPulse(path1, model_index=1)
neutron_pulse.load_shape_each(path2)


/Users/y9z/anaconda3/envs/py36/lib/python3.6/site-packages/lmfit/models.py:30: FutureWarning:

'argmin' is deprecated, use 'idxmin' instead. The behavior of 'argmin'
will be corrected to return the positional minimum in the future.
Use 'series.values.argmin' to get the position of the minimum now.

Fitted params for raw proton pulse shape:
Name          Value      Min      Max   Stderr     Vary     Expr Brute_Step
amplitude  1.984e+04     -inf      inf    36.58     True     None     None
center         1909     -inf      inf   0.3126     True     None     None
fwhm          345.7     -inf      inf   0.7362    False 2.3548200*sigma     None
height         53.9     -inf      inf  0.09941    False 0.3989423*amplitude/max(1.e-15, sigma)     None
sigma         146.8        0      inf   0.3126     True     None     None

In [6]:
neutron_pulse.plot_shape_total(x1_type='energy', x2_type='none')



In [9]:
neutron_pulse.shape_total_df.to_clipboard()

In [7]:
neutron_pulse.fit_shape(e_min=1, e_max=500,
                        drop=False, norm=True, check_each=True,
                        save_fig=False, overwrite_csv=True)


✅ '/Users/y9z/Documents/GitHub/ResoFit/ResoFit/result/neutron_pulse/ikeda_carpenter/Neutron_fitted_params_1eV_500eV_ikeda_carpenter.csv' exists...
File overwriting...
New fitting starts...
Fitting [1.0 eV] ...
Fitting [1.122 eV] ...
/Users/y9z/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py:3215: MatplotlibDeprecationWarning:


The `xmin` argument was deprecated in Matplotlib 3.0 and will be removed in 3.2. Use `left` instead.

/Users/y9z/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py:3221: MatplotlibDeprecationWarning:


The `xmax` argument was deprecated in Matplotlib 3.0 and will be removed in 3.2. Use `right` instead.

Fitting [1.2589 eV] ...
Fitting [1.4125 eV] ...
Fitting [1.5849 eV] ...
Fitting [1.7783 eV] ...
Fitting [1.9953 eV] ...
Fitting [2.2387 eV] ...
Fitting [2.5119 eV] ...
Fitting [2.8184 eV] ...
Fitting [3.1623 eV] ...
Fitting [3.5481 eV] ...
Fitting [3.9811 eV] ...
Fitting [4.4668 eV] ...
Fitting [5.0119 eV] ...
Fitting [5.6234 eV] ...
Fitting [6.3096 eV] ...
Fitting [7.0795 eV] ...
Fitting [7.9433 eV] ...
Fitting [8.9125 eV] ...
/Users/y9z/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/pyplot.py:514: RuntimeWarning:

More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).

Fitting [10.0 eV] ...
Fitting [11.22 eV] ...
Fitting [12.589 eV] ...
Fitting [14.125 eV] ...
Fitting [15.849 eV] ...
Fitting [17.783 eV] ...
Fitting [19.953 eV] ...
Fitting [22.387 eV] ...
Fitting [25.119 eV] ...
Fitting [28.184 eV] ...
Fitting [31.623 eV] ...
Fitting [35.481 eV] ...
Fitting [39.811 eV] ...
Fitting [44.668 eV] ...
Fitting [50.119 eV] ...
Fitting [56.234 eV] ...
Fitting [63.096 eV] ...
Fitting [70.795 eV] ...
Fitting [79.433 eV] ...
Fitting [89.125 eV] ...
Fitting [100.0 eV] ...
Fitting [112.2 eV] ...
Fitting [125.89 eV] ...
Fitting [141.25 eV] ...
Fitting [158.49 eV] ...
Fitting [177.83 eV] ...
Fitting [199.53 eV] ...
Fitting [223.87 eV] ...
Fitting [251.19 eV] ...
Fitting [281.84 eV] ...
Fitting [316.23 eV] ...
Fitting [354.81 eV] ...
Fitting [398.11 eV] ...
Fitting [446.68 eV] ...
Parameters fitting file (ikeda_carpenter) has been saved at '/Users/y9z/Documents/GitHub/ResoFit/ResoFit/result/neutron_pulse/ikeda_carpenter/Neutron_fitted_params_1eV_500eV_ikeda_carpenter.csv'
File overwritten.

In [9]:
neutron_pulse.param_df


Out[9]:
E_eV alpha beta fraction t0 norm_factor f_max
0 1.0000 1.320161 0.190874 0.258585 1.239271 3.526935 1.748800e+11
1 1.1220 1.366588 0.180714 0.242742 1.167587 3.305042 1.897078e+11
2 1.2589 1.438608 0.191419 0.244708 1.096268 3.072346 2.045461e+11
3 1.4125 1.596790 0.245330 0.278693 1.069329 2.912192 2.210562e+11
4 1.5849 1.609621 0.205219 0.235522 0.989089 2.797244 2.322512e+11
5 1.7783 1.770391 0.245550 0.256945 0.966678 2.501515 2.638642e+11
6 1.9953 1.865351 0.248604 0.241362 0.915315 2.427357 2.766284e+11
7 2.2387 1.995468 0.264083 0.250570 0.866026 2.252184 3.020454e+11
8 2.5119 2.059226 0.214573 0.212069 0.812802 2.094853 3.198402e+11
9 2.8184 2.170841 0.272910 0.228167 0.769122 2.036068 3.366861e+11
10 3.1623 2.247528 0.299277 0.246387 0.723181 2.009028 3.648346e+11
11 3.5481 2.363727 0.324718 0.249329 0.682282 1.906851 3.920650e+11
12 3.9811 2.497796 0.326524 0.247451 0.646808 1.821517 4.155472e+11
13 4.4668 2.675323 0.378949 0.253259 0.621687 1.732479 4.476627e+11
14 5.0119 2.752877 0.345487 0.232800 0.587713 1.624064 4.886803e+11
15 5.6234 2.959394 0.387726 0.241781 0.557301 1.516419 5.257542e+11
16 6.3096 3.218658 0.436194 0.245193 0.533086 1.420658 5.483989e+11
17 7.0795 3.261976 0.413234 0.236794 0.499025 1.349340 5.989894e+11
18 7.9433 3.484328 0.446605 0.232314 0.476379 1.289410 6.242004e+11
19 8.9125 3.672959 0.456831 0.232967 0.454602 1.200240 6.838026e+11
20 10.0000 3.854009 0.462873 0.226653 0.429056 1.131436 7.324400e+11
21 11.2200 4.108835 0.524497 0.232928 0.409342 1.094304 7.518859e+11
22 12.5890 4.296226 0.491331 0.217497 0.389613 0.990003 8.366775e+11
23 14.1250 4.531822 0.546572 0.219798 0.367084 0.970629 8.551699e+11
24 15.8490 4.827153 0.569673 0.218437 0.349905 0.904901 9.263107e+11
25 17.7830 5.110358 0.589717 0.214645 0.332798 0.873774 9.506614e+11
26 19.9530 5.404656 0.653731 0.222322 0.317455 0.814245 1.024626e+12
27 22.3870 5.709795 0.658238 0.210424 0.300369 0.769372 1.073524e+12
28 25.1190 5.776583 0.677443 0.210857 0.282858 0.758408 1.152384e+12
29 28.1840 6.115768 0.740315 0.216927 0.267956 0.729361 1.211912e+12
30 31.6230 6.396599 0.764901 0.215852 0.252751 0.696388 1.277032e+12
31 35.4810 6.825211 0.821937 0.217214 0.244625 0.652638 1.365273e+12
32 39.8110 7.132640 0.878215 0.226213 0.230675 0.631551 1.442313e+12
33 44.6680 7.535518 0.889314 0.217368 0.221196 0.587823 1.552392e+12
34 50.1190 7.923749 0.966749 0.222993 0.208938 0.575228 1.590677e+12
35 56.2340 8.266050 1.009446 0.217088 0.198630 0.537186 1.706758e+12
36 63.0960 8.641334 1.071737 0.216675 0.187846 0.519862 1.790412e+12
37 70.7950 9.058451 1.104085 0.218720 0.180106 0.502957 1.875076e+12
38 79.4330 9.479192 1.121514 0.206853 0.169915 0.472731 1.986540e+12
39 89.1250 10.020196 1.254123 0.220703 0.162841 0.450342 2.110569e+12
40 100.0000 10.510786 1.307059 0.219108 0.154806 0.427161 2.237400e+12
41 112.2000 11.055343 1.326538 0.216414 0.148047 0.399735 2.398051e+12
42 125.8900 11.690150 1.470739 0.222991 0.141383 0.387216 2.495769e+12
43 141.2500 12.296251 1.560805 0.226251 0.134612 0.371305 2.613266e+12
44 158.4900 12.839112 1.625217 0.223580 0.128633 0.353624 2.770088e+12
45 177.8300 13.441988 1.783124 0.229462 0.121722 0.340621 2.891160e+12
46 199.5300 14.263831 1.846465 0.230454 0.117517 0.320108 3.074957e+12
47 223.8700 14.794867 1.929188 0.229547 0.111400 0.307279 3.241190e+12
48 251.1900 15.449801 2.019320 0.227359 0.105691 0.291237 3.455118e+12
49 281.8400 16.157421 2.066968 0.229397 0.101552 0.283195 3.578522e+12
50 316.2300 17.108287 2.452888 0.241402 0.096592 0.269956 3.757129e+12
51 354.8100 17.785742 2.437375 0.232697 0.092895 0.260192 3.913909e+12
52 398.1100 18.674230 2.680520 0.243383 0.088750 0.247124 4.147908e+12
53 446.6800 19.947677 3.065349 0.259356 0.085697 0.235384 4.355353e+12

In [8]:
neutron_pulse.plot_params_vs_e(loglog=True)



In [9]:
neutron_pulse.fit_params(check_each=True, loglog_fit=True, overwrite_csv=True)


✅ '/Users/y9z/Documents/GitHub/ResoFit/ResoFit/result/neutron_pulse/ikeda_carpenter/Loglog_linear_within_1eV_500eV_ikeda_carpenter.csv' exists...
File overwriting...
New fitting starts...
Parameters linear fitting file has been saved at '/Users/y9z/Documents/GitHub/ResoFit/ResoFit/result/neutron_pulse/ikeda_carpenter/Loglog_linear_within_1eV_500eV_ikeda_carpenter.csv'
File overwritten.

In [10]:
neutron_pulse.linear_df


Out[10]:
param_name intercept slope
0 alpha 0.137757 0.442439
1 beta -0.754535 0.440475
2 fraction -0.617902 -0.014741
3 t0 0.081157 -0.442690
4 norm_factor 0.510796 -0.439397
5 f_max 11.305960 0.520481

In [12]:
neutron_pulse.fit_params(check_each=True, loglog_fit=False, overwrite_csv=True)


'/Users/y9z/Documents/GitHub/ResoFit/ResoFit/result/neutron_pulse/ikeda_carpenter/Loglog_linear_within_1eV_500eV_ikeda_carpenter.csv' exists...
File overwriting...
New fitting starts...
File overwritten.

In [13]:
neutron_pulse.linear_df


Out[13]:
param_name intercept slope
0 alpha 0.163801 0.428096
1 beta -0.831860 0.481183
2 fraction -0.615815 -0.015789
3 t0 0.090941 -0.454234
4 norm_factor 0.532477 -0.466436
5 f_max 11.389698 0.475280

Proton pulse


In [5]:
proton_path = '/Users/Shawn/Dropbox (ORNL)/Postdoc_research/neutron_beam_shape/SNS/proton_pulse/waveform_20170901.txt'
proton_pulse = ProtonPulse(proton_path)

In [8]:
proton_pulse.shape_df.set_index('t_ns').plot(style='.')
plt.xlabel('Time (ns)')
plt.ylabel('Proton pulse intensity (arb. unit)')
plt.xlim(xmin=1300, xmax=2500)
# plt.savefig('proton.png',dpi=300, transparent=True)
plt.show()



In [ ]: