<jupyter_start><jupyter_text>Rate vs. temperatureAuthor:<NAME> //  // 2020-06-11As elucidated in the instrumentation paper (see ``0. before getting started`` for reference), we observed a temperature dependent rate. Here we show this correlation by loading data that was generated using ``6. Get Data`` in this folder<jupyter_code># First we need some more packages!
!pip install pyarrow
!pip install fastparquet
import socket
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numba
import uproot
import os
import shutil
from matplotlib import rc
from scipy import optimize
# Set nice big fonts for plots
rc('font', size = 15)

# Let's make a copy locally of the coincidence_matching schript
shutil.copy('../01_coincidence_measurements/coincidence_matching.py', 'coincidence_matching.py')
from coincidence_matching import *<jupyter_output><empty_output><jupyter_text>Great lets open that data!<jupyter_code># Here we will loop over all datasets in the Ti44 folder! 
# We append each of the datasets seperately since that allows us to calculate the rate in that dataset
dsets = []
zip_files = '../data/temperature/ti44/'
for dset in os.listdir(zip_files):
    if 'zip' in dset:
        _path = os.path.join(zip_files, dset)
        d = pd.read_parquet(_path)
        dsets.append(d)
        del d
# Now we have a list of datasets. See for example the first one: 
dsets[0]<jupyter_output><empty_output><jupyter_text>Calculate the efficiency for a given dataset. Below you insert how you want to calculate the efficiency. If you get stuck here, please ask questions!The goal is to automatically calculate the efficiency for a given dataset. This you then add as a prpoerty to the dataframe that is being created inside ``parse_datasets``.<jupyter_code>def your_efficiency_function(data, normalization_factor = 1):
    """
    automatically calculate the effiency for a given dataset.
    """
    # You can add how you calculate the efficiency here for a given dataset. 
    # Plese note that both channels are in the 
    matched_data = easy_coincidence_matching(data, source = 'Ti44', check_time_order = True)
    
    # Efficiency = ??
    
    # For now we will just return one for as a placeholder
    return 1 / normalization_factor
def parse_datasets(dsets):
    """
    Extract data from the datasets. 
    
    return: table containing the data of interest.
    """
    res = {'time':[],
           'temp':[],
           'dtemp':[],
           'counts':[],
           'dt':[],
           'efficiency':[]}

    for d in dsets:
        # CHANGE THIS. For now I will only show the 511 keV peak to calculate the rate for this energy.
        selected_data = select_peak(d, 'integral', 511, 25)
        
        # This is a crude approximation as you have seen before. Can we do a better job?
        counts = len(selected_data)
        
        # For this dataset, just take the mean and standard deviation and save those values
        res['time'].append(np.mean(selected_data['time']))
        res['temp'].append(np.mean(selected_data['temp']))
        res['dtemp'].append(np.std(selected_data['temp']))
        
        # Save the counts. NB: this is the combined count for both channels!!!
        res['counts'].append(counts)
        
        # Calculate the duration of the dataset (needed to convert counts to a rate)
        res['dt'].append(selected_data['time'].max() - selected_data['time'].min())
        
        # Most importantly calculate the efficiency for the given dataset
        efficiency = your_efficiency_function(selected_data, normalization_factor = 1)
        res['efficiency'].append(efficiency)
    return pd.DataFrame(res)
res = parse_datasets(dsets)
# Our results are saved in the following dataframe:
res<jupyter_output>Checking time order of your data. This can take a while. Disable with check_time_order = False
Warning! Your data was not time ordered, doing that now. May take a while
Checking time order of your data. This can take a while. Disable with check_time_order = False
Warning! Your data was not time ordered, doing that now. May take a while
Checking time order of your data. This can take a while. Disable with check_time_order = False
Warning! Your data was not time ordered, doing that now. May take a while
Checking time order of your data. This can take a while. Disable with check_time_order = False
Warning! Your data was not time ordered, doing that now. May take a while
Checking time order of your data. This can take a while. Disable with check_time_order = False
Warning! Your data was not time ordered, doing that now. May take a while
Checking time order of your data. This can take a while. Disable with check_time_order = False
Warning! Your data was not time ordered, doing that now. May[...]<jupyter_text>Check temperature rate - temperature correlationTo illustrate the correlation I'll plot the rate vs temperature below. Can you make a similar plot for the efficiency?<jupyter_code>def lin_function(x, a, b):
    return a * x + b 
def plot_res():
    """
    Make a plot of the temperature versus rate (uncorrected for the normal exponential decay).
    """
    
    plt.figure(figsize = (10,8))
    # Select the values to plot
    temp = res['temp']
    dtemp = res['dtemp']
    rate = res['counts'] / res['dt']
    drate = np.sqrt(res['counts']) / res['dt']
    time = (res['time']-res['time'].values.min())/(3600*24)
    
    # Plot (plot first the uncertainties, then the points with the colorcoding indicating the time)
    plt.errorbar(temp, rate, xerr = dtemp, yerr = drate, c = 'k', marker = 'o',
                 label = 'data', linestyle = 'None', capsize = 5)

    cm = plt.get_cmap('rainbow')
    plt.scatter(temp, rate, c = time, cmap=cm,
                 zorder = 1000, s= 100, linestyle = 'None', marker = 'o')
    # plot the colorbar. Note this is in days since first dataset
    plt.colorbar(label = 'Time since first dataset [days]')
    
    # Add labels
    plt.xlabel('Temperature [$^{\circ}$C]')
    plt.ylabel('(uncorrected) Rate [Hz]')
    
    # Do a simple fit and show the best fit. This syntax may be confusing.
    # Please ask questions if you don't understand. 
    # The most important take away message is that this shows that the rate decreases 
    # for higher temperatures.
    fit_res = optimize.curve_fit(lin_function, temp, rate, sigma = drate)
    plt.plot(temp, lin_function(temp, *fit_res[0]), c = 'red', label = 'linear fit')
    print_res = ('Fit res: $a x + b$\n' +
                 '$a$ = %.2f +/- %.2f [Hz/${}^\circ$C]\n'%(fit_res[0][0], np.sqrt(fit_res[1][0][0])) +
                 '$b$ = %.1f +/- %.1f Hz'% (fit_res[0][1], np.sqrt(fit_res[1][1][1])))
    plt.text(np.min(temp), np.min(rate), print_res, verticalalignment = 'center')
    plt.legend()
plot_res()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09989821142940236, 'fraction_numerical': 0.00930638359749891, 'mean_word_length': 3.394888178913738, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '311795', 'license': 'MIT', 'n_tokens_mistral': 2079, 'n_tokens_neox': 1977, 'n_words': 799, 'path': '03_temperature_deps/7. Rate - Temperature correlation.ipynb', 'repo_name': 'jorana/RP2020', 'size': '6883'}