95 KiB
Executable File
95 KiB
Executable File
In [1]:
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
import pandas as pdIn [33]:
τ = 0.24e-3
background: pd.DataFrame = pd.DataFrame({
't': [180.86, 182.01, 180.18],
'N': [44, 48, 45],
'√N': [np.nan]*3,
'η_b': [np.nan]*3,
'δη_b': [np.nan]*3
})
background['√N'] = np.sqrt(background['N'])
background['η_b'] = background['N'] / (background['t'] - background['N'] * τ)
background['δη_b'] = background['√N'] / (background['t'] - background['N']*τ)
backgroundOut [33]:
| t | N | √N | η_b | δη_b | |
|---|---|---|---|---|---|
| 0 | 180.86 | 44 | 6.633250 | 0.243296 | 0.036678 |
| 1 | 182.01 | 48 | 6.928203 | 0.263738 | 0.038067 |
| 2 | 180.18 | 45 | 6.708204 | 0.249765 | 0.037233 |
In [34]:
η_b_mean = np.mean(background['η_b'])
δη_b_mean = np.std(background['η_b'], ddof=1) / np.sqrt(len(background))
print(f"η_b = {η_b_mean:.4f} ± {δη_b_mean:.4f} counts/s")η_b = 0.2523 ± 0.0060 counts/s
In [18]:
# create a helper that formats a pair (η_b, δη_b) without overwriting the sci used elsewhere
sci_pair = lambda eta, delta, n=2: f"{eta:.{n}e} ± {delta:.{n}e} "
# add a combined formatted column to the DataFrame (operate on the DataFrame, not on the Styler)
background['η_b ± δη_b'] = background.apply(
lambda row: sci_pair(row['η_b'], row['δη_b'], 2),
axis=1
)
background.drop(columns=['η_b', 'δη_b'], inplace=False)
# create the Styler for display; format numeric columns as desired
background_latex = background.style.format({
't': '{:.0f}',
'N': '{:.0f}',
'√N': '{:.2f}',
# 'η_b': '{:.2e}',
# 'δη_b': '{:.2e}',
# the combined column is already a formatted string so no formatter needed
})
# optionally hide the separate numeric η_b / δη_b columns if you only want the combined column visible
# background_latex = background_latex.hide_columns(['η_b', 'δη_b'])
# background_latex.to_latex(hrules=True)
background_latex
# optionally hide the separate numeric η_b / δη_b columns if you only want the combined column visible
# background_latex = background_latex.hide_columns(['η_b', 'δη_b'])
# background_latex.to_latex(hrules=True)
# background_latex.drop(columns=['η_b', 'δη_b'])
# optionally hide the separate numeric η_b / δη_b columns if you only want the combined column visible
# background_latex = background_latex.hide_columns(['η_b', 'δη_b'])
# background_latex.to_latex(hrules=True)
background_latexOut [18]:
| t | N | √N | η_b | δη_b | η_b ± δη_b | |
|---|---|---|---|---|---|---|
| 0 | 181 | 44 | 6.63 | 0.258368 | 0.038950 | 2.58e-01 ± 3.90e-02 |
| 1 | 182 | 48 | 6.93 | 0.281541 | 0.040637 | 2.82e-01 ± 4.06e-02 |
| 2 | 180 | 45 | 6.71 | 0.265675 | 0.039604 | 2.66e-01 ± 3.96e-02 |
In [94]:
sci_pair = lambda eta, delta, n=2: f"{eta:.{n}f} ± {delta:.{n}f} "
decay: pd.DataFrame = pd.DataFrame({
't': [ 0, 265, 523, 782, 1023, 1305, 1564, 1825, 2088, 2349, 2610, 2869, 3136, 3401, 3664, 3923, 4188, 4454, 4723, 4997, 5249, 5521, 5784, 6049, 6318],
'δt': [256]*25,
'N': [14220, 13275, 12794, 11997, 11271, 10807, 9938, 9722, 9227, 8582, 8342, 7821, 7343, 7092, 6686, 6437, 5893, 5456, 5192, 4986, 4905, 4599, 4190, 4113, 3870],
})
decay['√N'] = np.sqrt(decay['N'])
decay['η'] = decay['N'] / (decay['δt'] - decay['N']*τ)
decay['δη'] = decay['√N'] / (decay['δt'] - decay['N']*τ)
decay['t_i'] = decay['t'] + decay['δt']/2
decay['η*'] = decay['η'] - η_b_mean
decay['δη*'] = np.sqrt(decay['δη']**2 + δη_b_mean**2)
decay['ln(η*)'] = np.log(decay['η*'])
decay['δln(η*)'] = decay['δη*'] / decay['η*']In [99]:
a, b = stats.linregress(decay['t_i'], decay['ln(η*)'])[:2]
plt.figure(figsize=(8,6))
plt.plot(decay['t_i'], decay['ln(η*)'], label='Decay rate')
plt.errorbar(decay['t_i'], decay['ln(η*)'], yerr=decay['δln(η*)'], fmt='o', label='Measured data', capsize=3)
plt.plot(decay['t_i'], a*decay['t_i'] + b, label='Linear fit', color='orange')
plt.title('Logarithm of Corrected Decay Rate over Time')
plt.xlabel('Time (s)')
plt.ylabel('ln(Decay Rate) (ln(counts/s))')
plt.legend()
plt.grid(True)
plt.text(0, 3, f"Slope = {a:.4e} ± {stats.linregress(decay['t_i'], decay['ln(η*)']).stderr:.4e} 1/s\nIntercept = {b:.4f}",
bbox=dict(facecolor='white', alpha=0.5))
plt.show()In [91]:
decay['N ± √N'] = decay.apply(
lambda row: sci_pair(row['N'], row['√N'], 0),
axis=1
)
decay['η ± δη'] = decay.apply(
lambda row: sci_pair(row['η'], row['δη'], 2),
axis=1
)
decay['η* ± δη*'] = decay.apply(
lambda row: sci_pair(row['η*'], row['δη*'], 2),
axis=1
)
decay['ln(η*) ± δln(η*)'] = decay.apply(
lambda row: sci_pair(row['ln(η*)'], row['δln(η*)'], 2),
axis=1
)
decay.drop(columns=['N', '√N', 'η', 'δη', 'η*', 'δη*', 'ln(η*)', 'δln(η*)'], inplace=True)
decayOut [91]:
| t | δt | t_i | N ± √N | η ± δη | η* ± δη* | ln(η*) ± δln(η*) | |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 256 | 128.0 | 14220 ± 119 | 56.30 ± 0.47 | 56.05 ± 0.47 | 4.03 ± 0.01 |
| 1 | 265 | 256 | 393.0 | 13275 ± 115 | 52.51 ± 0.46 | 52.26 ± 0.46 | 3.96 ± 0.01 |
| 2 | 523 | 256 | 651.0 | 12794 ± 113 | 50.58 ± 0.45 | 50.33 ± 0.45 | 3.92 ± 0.01 |
| 3 | 782 | 256 | 910.0 | 11997 ± 110 | 47.40 ± 0.43 | 47.14 ± 0.43 | 3.85 ± 0.01 |
| 4 | 1023 | 256 | 1151.0 | 11271 ± 106 | 44.50 ± 0.42 | 44.25 ± 0.42 | 3.79 ± 0.01 |
| 5 | 1305 | 256 | 1433.0 | 10807 ± 104 | 42.65 ± 0.41 | 42.39 ± 0.41 | 3.75 ± 0.01 |
| 6 | 1564 | 256 | 1692.0 | 9938 ± 100 | 39.19 ± 0.39 | 38.93 ± 0.39 | 3.66 ± 0.01 |
| 7 | 1825 | 256 | 1953.0 | 9722 ± 99 | 38.33 ± 0.39 | 38.07 ± 0.39 | 3.64 ± 0.01 |
| 8 | 2088 | 256 | 2216.0 | 9227 ± 96 | 36.36 ± 0.38 | 36.11 ± 0.38 | 3.59 ± 0.01 |
| 9 | 2349 | 256 | 2477.0 | 8582 ± 93 | 33.80 ± 0.36 | 33.54 ± 0.36 | 3.51 ± 0.01 |
| 10 | 2610 | 256 | 2738.0 | 8342 ± 91 | 32.84 ± 0.36 | 32.59 ± 0.36 | 3.48 ± 0.01 |
| 11 | 2869 | 256 | 2997.0 | 7821 ± 88 | 30.78 ± 0.35 | 30.52 ± 0.35 | 3.42 ± 0.01 |
| 12 | 3136 | 256 | 3264.0 | 7343 ± 86 | 28.88 ± 0.34 | 28.63 ± 0.34 | 3.35 ± 0.01 |
| 13 | 3401 | 256 | 3529.0 | 7092 ± 84 | 27.89 ± 0.33 | 27.64 ± 0.33 | 3.32 ± 0.01 |
| 14 | 3664 | 256 | 3792.0 | 6686 ± 82 | 26.28 ± 0.32 | 26.03 ± 0.32 | 3.26 ± 0.01 |
| 15 | 3923 | 256 | 4051.0 | 6437 ± 80 | 25.30 ± 0.32 | 25.04 ± 0.32 | 3.22 ± 0.01 |
| 16 | 4188 | 256 | 4316.0 | 5893 ± 77 | 23.15 ± 0.30 | 22.90 ± 0.30 | 3.13 ± 0.01 |
| 17 | 4454 | 256 | 4582.0 | 5456 ± 74 | 21.42 ± 0.29 | 21.17 ± 0.29 | 3.05 ± 0.01 |
| 18 | 4723 | 256 | 4851.0 | 5192 ± 72 | 20.38 ± 0.28 | 20.13 ± 0.28 | 3.00 ± 0.01 |
| 19 | 4997 | 256 | 5125.0 | 4986 ± 71 | 19.57 ± 0.28 | 19.32 ± 0.28 | 2.96 ± 0.01 |
| 20 | 5249 | 256 | 5377.0 | 4905 ± 70 | 19.25 ± 0.27 | 19.00 ± 0.27 | 2.94 ± 0.01 |
| 21 | 5521 | 256 | 5649.0 | 4599 ± 68 | 18.04 ± 0.27 | 17.79 ± 0.27 | 2.88 ± 0.01 |
| 22 | 5784 | 256 | 5912.0 | 4190 ± 65 | 16.43 ± 0.25 | 16.18 ± 0.25 | 2.78 ± 0.02 |
| 23 | 6049 | 256 | 6177.0 | 4113 ± 64 | 16.13 ± 0.25 | 15.88 ± 0.25 | 2.76 ± 0.02 |
| 24 | 6318 | 256 | 6446.0 | 3870 ± 62 | 15.17 ± 0.24 | 14.92 ± 0.24 | 2.70 ± 0.02 |
In [107]:
decay.to_csv('decay_data_formatted.csv', index=False)In [103]:
print(f"{np.log(2)/(-a):.4f} ± { (np.log(2)/a**2) * stats.linregress(decay['t_i'], decay['ln(η*)']).stderr :.4f} s ")3316.9930 ± 27.3962 s
In [ ]:
print(f"{np.exp(b)/(-0.3*a):.4f} ± {np.sqrt((np.exp(b)/(-0.3*a) * stats.linregress(decay['t_i'], decay['ln(η*)']).intercept_stderr)**2 + (np.exp(b)/(-2*0.3*a) * stats.linregress(decay['t_i'], decay['ln(η*)']).stderr)**2)} Bq ")908782.5989 ± 5933.8630366652005 Bq
[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. [1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. [1;31mClick <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. [1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.
In [ ]: