In [1]:
import os
import sys
root_folder = os.path.dirname(os.getcwd())
sys.path.append(root_folder)
from ResoFit.calibration import Calibration
from ResoFit.fitresonance import FitResonance
from ResoFit.experiment import Experiment
from ResoFit.simulation import Simulation
from ImagingReso.resonance import Resonance
import matplotlib.pyplot as plt
import numpy as np
import scipy.signal as ss
import pprint
from ResoFit._utilities import get_foil_density_gcm3
from ResoFit._utilities import Layer
import peakutils as pku

In [2]:
%matplotlib notebook

In [3]:
# Global parameters
energy_min = 7
energy_max = 150
energy_step = 0.01

folder = 'data/IPTS_18521/reso_data_18521'
data_file = 'run_33_resonance.txt'
spectra_file = 'Image033_Spectra.txt'
# image_start = None  # Can be omitted or =None
# image_end = None  # Can be omitted or =None
norm_to_file = 'run_33_resonance_ob.txt'#None  # 'sphere_background_1.csv'
# baseline = True
# each_step = False

repeat = 1
source_to_detector_m = 15.48 #15.4318  # 16#16.445359069030175#16.447496101100739
offset_us = 2.376 #2.7  # 0#2.7120797253959119#2.7355447625559037
simu = Simulation(energy_min=energy_min, energy_max=energy_max, energy_step=energy_step)
simu.add_layer(layer='UO3', layer_thickness_mm=2.6, layer_density_gcm3=0.07875)
simu.plot(mixed= False, items_to_plot=[['UO3', 'U', '238-U'], ['UO3', 'U', '235-U']], logx=False)


First time using database 'ENDF_VIII'? 
I will retrieve and store a local copy of database'ENDF_VIII': 
Downloading 'ENDF_VIII.zip'... 

Download completed.
Unzipping 'ENDF_VIII.zip'... 
'ENDF_VIII.zip' has been unzipped and database 'ENDF_VIII' is ready to use.
'ENDF_VIII.zip' has been deleted
Out[3]:
<matplotlib.axes._subplots.AxesSubplot at 0x1c150179b0>

In [5]:
exp = Experiment(spectra_file=spectra_file, data_file=data_file, folder=folder)
exp.norm_to(norm_to_file)

In [8]:
ax1 = exp.plot(x_type='energy', y_type='attenuation',
         source_to_detector_m=15.48, offset_us=2.376,
         logx=True, baseline=True, deg=7, mt='-', ms=3)
_legend = ax1.legend()
_legend.get_texts()[0].set_text('Exp. Data (TRISO)')
ax1.set_xlim(left=10, right=500)
ax1.set_ylim(bottom=-0.01, top=0.52)

# ax1.grid()


Out[8]:
(-0.01, 0.52)

In [5]:
# Fit the peak height
fit = FitResonance(spectra_file=spectra_file,
                   data_file=data_file,
                   folder=folder,
                   repeat=repeat,
                   energy_min=energy_min,
                   energy_max=energy_max,
                   energy_step=energy_step,
                   calibrated_offset_us=calibration.calibrated_offset_us,
                   calibrated_source_to_detector_m=calibration.calibrated_source_to_detector_m,
                   norm_to_file=norm_to_file,
                   slice_start=image_start,
                   slice_end=image_end,
                   baseline=baseline)
fit.fit(layer, vary='density', each_step=each_step)
fit.molar_conc()
# fit.fit_iso(layer_1)
fit.plot(error=True)


+----------------- Fitting (density) -----------------+
Params before:
Name               Value      Min      Max   Stderr     Vary     Expr Brute_Step
density_gcm3_U     18.95        0      inf     None     True     None     None
thickness_mm_U     0.018        0      inf     None    False     None     None

Params after:
Name               Value      Min      Max   Stderr     Vary     Expr Brute_Step
density_gcm3_U     15.45        0      inf   0.1271     True     None     None
thickness_mm_U     0.018        0      inf        0    False     None     None
Fitting chi^2 : 9.898755540459574

Molar-conc. (mol/cm3)	Before_fit	After_fit
U	0.07961217820137899	0.06489243350265303



In [3]:
# Global parameters
energy_min = 3.8
energy_max = 300
energy_step = 0.01
# Input sample name or names as str, case sensitive
# density = get_foil_density_gcm3(length_mm=25, width_mm=25, thickness_mm=0.025, mass_g=0.14)

simu = Simulation(energy_min=energy_min, energy_max=energy_max, energy_step=energy_step)
simu.add_layer(layer='Ag', layer_thickness_mm=0.025)
simu.add_layer(layer='Co', layer_thickness_mm=0.025)
simu.add_layer(layer='Hf', layer_thickness_mm=0.025)
simu.add_layer(layer='W', layer_thickness_mm=0.05)
simu.add_layer(layer='In', layer_thickness_mm=0.05)
simu.add_layer(layer='Cd', layer_thickness_mm=0.5)
simu.add_layer(layer='Au', layer_thickness_mm=0.01)
# simu.add_layer(formula='Ag', thickness=0.025)
# simu.add_layer(formula='Co', thickness=0.025)
# simu.add_layer(formula='Hf', thickness=0.025)
# simu.add_layer(formula='W', thickness=0.05)
# simu.add_layer(formula='In', thickness=0.05)
# simu.add_layer(formula='Cd', thickness=0.5)
# simu.add_layer(formula='Au', thickness=0.01)
# simu.add_layer(layer='Ag', thickness_mm=0.025, density_gcm3=None)
# layer.add_layer(layer='Co', thickness_mm=0.025, density_gcm3=None)
# layer.add_layer(layer='Hf', thickness_mm=0.025, density_gcm3=None)
# layer.add_layer(layer='W', thickness_mm=0.05, density_gcm3=None)
# layer.add_layer(layer='In', thickness_mm=0.05, density_gcm3=None)
# layer.add_layer(layer='Cd', thickness_mm=0.5, density_gcm3=None)
# layer.add_layer(layer='Au', thickness_mm=0.01, density_gcm3=None)

In [4]:
simu.plot(mixed= False, all_elements=True, logx=True)


Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x120e300b8>

In [ ]:
# Calibrate the peak positions
calibration = Calibration(data_file=data_file,
                          spectra_file=spectra_file,
                          layer=layer,
                          energy_min=energy_min,
                          energy_max=energy_max,
                          energy_step=energy_step,
                          repeat=repeat,
                          folder=folder,
                          baseline=baseline)

calibration.norm_to(norm_to_file)
calibration.slice(slice_start=image_start, slice_end=image_end)

calibrate_result = calibration.calibrate(source_to_detector_m=source_to_detector_m,
                                         offset_us=offset_us,
                                         vary='all',
                                         each_step=each_step)
calibration.index_peak(thres=0.5, min_dist=50)
# calibration.analyze_peak()
pprint.pprint(calibration.experiment.o_peak.peak_map_indexed)
# peak_df = calibration.peak_df_scaled

calibration.plot(before=before, table=table, peak_id='all')

In [3]:
# Fit the peak height
fit = FitResonance(folder=folder,
                   spectra_file=spectra_file,
                   data_file=data_file,
                   repeat=repeat,
                   energy_min=energy_min,
                   energy_max=energy_max,
                   energy_step=energy_step,
                   calibrated_offset_us=calibration.calibrated_offset_us,
                   calibrated_source_to_detector_m=calibration.calibrated_source_to_detector_m,
                   norm_to_file=norm_to_file,
                   slice_start=image_start,
                   slice_end=image_end,
                   baseline=baseline)
fit_result = fit.fit(layer, vary=fit_vary, each_step=each_step)
fit.molar_conc()
# fit.fit_iso(layer=layer_1)
fit.index_peak(thres=0.5, min_dist=50)
fit.plot(before=before, table=table, peak_id='all')

In [5]:
# foil_list = ['Ag', 'Co', 'Hf', 'W', 'In', 'Cd', 'Au', 'all']
# foil_list = ['Ag', 'Co', 'Hf', 'W', 'In', 'Cd', 'Au']
# foil_list = ['Co', 'Cd', 'W', 'In', 'Hf', 'Ag', 'Au']
# foil_list = ['Co', 'Cd', 'W', 'In', 'Hf', 'Ag', 'Au']

data_file_list = [x + '.csv' for x in foil_list]
folder = 'data/IPTS_13639/reso_data_13639'
spectra_file = 'spectra.csv'
exps = {}
for each_data in data_file_list:
    _ele_name = each_data.split('.')[0]
    exps[_ele_name] = Experiment(spectra_file=spectra_file, data_file=each_data, folder=folder)
    exps[_ele_name].slice(start=294, end=2720)
#     exps[_ele_name].slice(start=294, end=2570)
    if _ele_name == 'Co':
        exps[_ele_name].norm_to('Ag.csv')
    elif _ele_name == 'Cd':
        exps[_ele_name].norm_to('In.csv')
    elif _ele_name == 'W':
        exps[_ele_name].norm_to('Hf.csv')
    else:
        exps[_ele_name].norm_to('ob_all.csv')

In [6]:
exps


Out[6]:
{'Co': <ResoFit.experiment.Experiment at 0x123f79710>,
 'Cd': <ResoFit.experiment.Experiment at 0x125eacf28>,
 'W': <ResoFit.experiment.Experiment at 0x125e70048>,
 'In': <ResoFit.experiment.Experiment at 0x125ea80f0>,
 'Hf': <ResoFit.experiment.Experiment at 0x125e7ff28>,
 'Ag': <ResoFit.experiment.Experiment at 0x125e9ff28>,
 'Au': <ResoFit.experiment.Experiment at 0x1205f3f28>}

In [7]:
image_start = 500  # Can be omitted or =None
image_end = 1600  # Can be omitted or =None
# norm_to_file = 'ob_1.csv'  #'Ag.csv'
# norm_to_file = 'Ag.csv'
norm_to_file = None

repeat = 1
source_to_detector_m = 16.123278721983177  # 16#16.445359069030175#16.447496101100739
offset_us = -12112.494119089204  # 0#2.7120797253959119#2.7355447625559037

In [8]:
exps['Co'].plot(x_type='number', y_type='transmission',
                source_to_detector_m=source_to_detector_m, offset_us=offset_us,
                logx=False, baseline=True, deg=7)