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 [5]:
neutron_pulse.plot_shape_total(x)



In [5]:
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] ...
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] ...
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] ...
File overwritten.

In [6]:
neutron_pulse.param_df


Out[6]:
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.690800e+11
2 1.2589 1.438608 0.191419 0.244708 1.096268 3.072346 1.624800e+11
3 1.4125 1.596790 0.245330 0.278693 1.069329 2.912192 1.565000e+11
4 1.5849 1.609621 0.205219 0.235522 0.989089 2.797244 1.465400e+11
5 1.7783 1.770391 0.245550 0.256945 0.966678 2.501515 1.483800e+11
6 1.9953 1.865351 0.248604 0.241362 0.915315 2.427357 1.386400e+11
7 2.2387 1.995467 0.264083 0.250570 0.866026 2.252184 1.349200e+11
8 2.5119 2.059226 0.214573 0.212069 0.812802 2.094853 1.273300e+11
9 2.8184 2.170841 0.272910 0.228167 0.769122 2.036068 1.194600e+11
10 3.1623 2.247528 0.299277 0.246387 0.723181 2.009028 1.153700e+11
11 3.5481 2.363727 0.324718 0.249329 0.682282 1.906851 1.105000e+11
12 3.9811 2.497796 0.326524 0.247451 0.646808 1.821517 1.043800e+11
13 4.4668 2.675323 0.378949 0.253259 0.621687 1.732479 1.002200e+11
14 5.0119 2.752877 0.345487 0.232800 0.587713 1.624064 9.750400e+10
15 5.6234 2.959394 0.387726 0.241781 0.557301 1.516419 9.349400e+10
16 6.3096 3.218658 0.436194 0.245193 0.533086 1.420658 8.691500e+10
17 7.0795 3.261976 0.413234 0.236794 0.499025 1.349340 8.460900e+10
18 7.9433 3.484328 0.446605 0.232314 0.476379 1.289410 7.858200e+10
19 8.9125 3.672959 0.456831 0.232967 0.454602 1.200240 7.672400e+10
20 10.0000 3.854009 0.462873 0.226653 0.429056 1.131436 7.324400e+10
21 11.2200 4.108835 0.524497 0.232928 0.409342 1.094304 6.701300e+10
22 12.5890 4.296226 0.491331 0.217497 0.389613 0.990003 6.646100e+10
23 14.1250 4.531822 0.546572 0.219798 0.367084 0.970629 6.054300e+10
24 15.8490 4.827153 0.569673 0.218437 0.349905 0.904901 5.844600e+10
25 17.7830 5.110358 0.589717 0.214645 0.332798 0.873774 5.345900e+10
26 19.9530 5.404656 0.653731 0.222322 0.317455 0.814245 5.135200e+10
27 22.3870 5.709795 0.658238 0.210424 0.300369 0.769372 4.795300e+10
28 25.1190 5.776583 0.677443 0.210857 0.282858 0.758408 4.587700e+10
29 28.1840 6.115768 0.740315 0.216927 0.267956 0.729361 4.300000e+10
30 31.6230 6.396599 0.764901 0.215852 0.252751 0.696388 4.038300e+10
31 35.4810 6.825211 0.821937 0.217214 0.244625 0.652638 3.847900e+10
32 39.8110 7.132640 0.878215 0.226213 0.230675 0.631551 3.622900e+10
33 44.6680 7.535518 0.889314 0.217368 0.221196 0.587823 3.475400e+10
34 50.1190 7.923749 0.966749 0.222993 0.208938 0.575228 3.173800e+10
35 56.2340 8.266050 1.009446 0.217088 0.198630 0.537186 3.035100e+10
36 63.0960 8.641333 1.071737 0.216675 0.187846 0.519862 2.837600e+10
37 70.7950 9.058451 1.104085 0.218720 0.180106 0.502957 2.648600e+10
38 79.4330 9.479192 1.121514 0.206853 0.169915 0.472731 2.500900e+10
39 89.1250 10.020192 1.254117 0.220702 0.162841 0.450342 2.368100e+10
40 100.0000 10.510786 1.307059 0.219108 0.154806 0.427161 2.237400e+10
41 112.2000 11.055343 1.326538 0.216414 0.148047 0.399735 2.137300e+10
42 125.8900 11.690150 1.470739 0.222991 0.141383 0.387216 1.982500e+10
43 141.2500 12.296251 1.560805 0.226251 0.134612 0.371305 1.850100e+10
44 158.4900 12.839112 1.625217 0.223580 0.128633 0.353624 1.747800e+10
45 177.8300 13.441988 1.783125 0.229462 0.121722 0.340621 1.625800e+10
46 199.5300 14.263831 1.846465 0.230454 0.117517 0.320108 1.541100e+10
47 223.8700 14.794867 1.929188 0.229547 0.111400 0.307279 1.447800e+10
48 251.1900 15.449801 2.019320 0.227359 0.105691 0.291237 1.375500e+10
49 281.8400 16.157421 2.066968 0.229397 0.101552 0.283195 1.269700e+10
50 316.2300 17.108286 2.452888 0.241402 0.096592 0.269956 1.188100e+10
51 354.8100 17.785742 2.437375 0.232697 0.092895 0.260192 1.103100e+10
52 398.1100 18.674230 2.680520 0.243383 0.088750 0.247124 1.041900e+10
53 446.6800 19.947677 3.065349 0.259356 0.085697 0.235384 9.750500e+09

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



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



In [11]:
neutron_pulse.linear_df


Out[11]:
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.264713 -0.427564

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



In [9]:
neutron_pulse.linear_df


Out[9]:
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.479519

Proton pulse


In [3]:
proton_path = '/Users/y9z/Dropbox (ORNL)/Postdoc_Research/neutron_beam_shape/SNS/proton_pulse/waveform_20170901.txt'
proton_pulse = ProtonPulse(proton_path)


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
/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.


In [5]:
proton_pulse.plot()


Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x118bf6828>

In [4]:
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)


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-dfaa7c1ebb18> in <module>
----> 1 proton_pulse.shape_df.set_index('t_ns').plot(style='.')
      2 plt.xlabel('Time (ns)')
      3 plt.ylabel('Proton pulse intensity (arb. unit)')
      4 plt.xlim(xmin=1300, xmax=2500)
      5 # plt.savefig('proton.png',dpi=300, transparent=True)

AttributeError: 'ProtonPulse' object has no attribute 'shape_df'

In [ ]: