data_composition_visualization.py Tutorial


The purpose of this notebook is to demonstrate how the data_composition_visualization code works, including an explanation of the variables in the script as well as a walk through of the necessary input documents

data_composition_visualization.py creates graphs of the composition along with the statistical and systematic uncertainties of the isotopes present in the given sample. The code returns the graphs for each of the 16 samples.

The first step imports the necessary modules needed:


In [2]:
import csv
import numpy as np
import matplotlib.pyplot as plt

Each spectra's information on the compositions is loaded onto csv files. Each file contains 27 data entries: three different efficiency measurements (one for the mean, another for the low estimate, and one for the high estimate, all of which are calculated on the efficiencies.py document), three different time measurements (30m, 3h, and 24h measurements), and three different peak entries. Most of the code is written inside a for loop in order to iterate through each of the 16 sample csv's and generate a results graph for each. Before the code is run, the files must also be attained and placed in the correct dirrectory.


In [11]:
spectraQuantity = np.linspace(1,16,16) #an array containing an index per csv file
for quan in spectraQuantity:
    quan = int(quan) #convert the float into an int to get rid of the unecessary decimal
    #The csv files are named NAA_results_with_efficiencies#, so it is easy to call each one
    #For the step below, modify the line to the directory in which the files are located in.
    spectra = '/Users/jackiegasca/Documents/radwatch-analysis/NAA_results_with_efficiencies'+str(quan)+'.csv'

    f = open(spectra, 'r')
    reader = csv.reader(f)

    rows = []
    for row in reader:
        rows.append(row)

    #Each csv has the present isotopes located on the second cell of the last row of the csv
    #This step is in charge of locating them and placing them in an array
    contents = rows[len(rows)-1][1]
    fish = rows[len(rows)-1][2] #This step locates the data on the graph with the fish information.
    elements = contents.split(', ')
    print(elements)

    #'values' below is a dictionary. Each isotope will be entered as a key and the concentrations of 
    #each isotope will be stored as its values -one for the concentration, another for its uncertainty
    #in the form of arrays. 
    values = {}
    for i in elements:

        iso_conc = []
        iso_unc = []
        for row in rows:
            for j in row:
                string = 'g of ' + i
                if string in j: #This step looks for the cells containing the given isotope to extract its values.
                    num, unc = j.split(' +/- ')
                    unc, extra = unc.split(' g') #This step gets rid of the extra string objects attached at the end.
                    
                    #if the concentration is already existant in the array, it is a redundancy and therefore not added
                    if num in iso_conc: 
                        pass
                    else:
                        iso_conc.append(float(num))
                        iso_unc.append(float(unc))

                else:
                    pass
        values[i] = iso_conc, iso_unc
    lowef_avg_conc = [] #This is the high point of the systematic uncertainty
    lowef_avg_unc = []
    midef_avg_conc = [] #This is the supposed concentration of the isotope
    midef_avg_unc = [] #This is the statistic uncertainty of each isotope
    highef_avg_conc = [] #This is the low point of the systematic uncertainty
    highef_avg_unc = []
    total_error = []
    for i in elements:
        arr_size =len(values[i][0]) #This gives the amount of concentration data entries for each isotope
        #Each isotope is present in equal amounts for the three systematic efficiencies so the step below 
        #splits the data into three equal bins in order of lowest to highest efficiency.
        amount = int(arr_size / 3)
        #The concentrations and uncertainty are weighed into an average and returned as a single number
        lowef_avg_conc.append(np.mean(values[i][0][0:amount]))
        lowef_avg_unc.append(np.mean(values[i][1][0:amount]))
        midef_avg_conc.append(np.mean(values[i][0][amount:amount+amount]))
        midef_avg_unc.append(np.mean(values[i][1][amount:amount+amount]))
        highef_avg_conc.append(np.mean(values[i][0][amount+amount:arr_size]))
        highef_avg_unc.append(np.mean(values[i][1][amount+amount:arr_size]))
        total_error.append((np.mean(values[i][0][0:amount])-np.mean(values[i][0][amount+amount:arr_size]))/2)

    #The steps below generate the graphs
    #First, the x axis is created, an x mark per isotope. 
    x=np.linspace(1,len(elements),len(elements))

    mpl_fig = plt.figure(figsize=(12,6))
    ax = mpl_fig.add_subplot(111)
    width = 0.35
    #p3 creates the line error bars for the statistical uncertainty
    p3 = plt.errorbar(x, midef_avg_conc, yerr=midef_avg_unc, fmt='.', ecolor='black', capthick=2, capsize=1)
    #p2 creates an invisible bar between the the ground value and the low point of the uncertainty
    #The point of this is to create the floating effect of the concentration.
    p1 = ax.bar(x, midef_avg_conc, width, color='white')
    #p2 created the salmon colored systematic uncertainty bars
    p2 = ax.bar(x, lowef_avg_conc, width, color=(1.0,0.7,0.68),
             bottom=highef_avg_conc)
    plt.xlabel('Isotope')
    plt.ylabel('Concentration in grams')
    plt.title('Isotope concentration of Sample '+str(quan)+': '+fish)
    plt.xticks(x, elements)
    ax.set_yscale('log')
    plt.show()
    print(midef_avg_conc, midef_avg_unc, total_error)


['Br-81', 'Na-23', 'Sb-121', 'K-41', 'Rb-85', 'Sb-123', 'Co-59', 'Hg-202', 'Cs-133']
[1.3801962804725817e-06, 0.0019603969621458175, 2.1882775588713411e-06, 0.00034847522883925045, 3.2037700964724742e-06, 2.0962256927550442e-07, 8.6238292436011806e-08, 2.0764614238425693e-08, 4.1880015497708003e-07] [6.546256690324152e-08, 1.1316579195880606e-05, 1.9207110052231129e-07, 1.3316554916943369e-05, 8.3655850616832702e-08, 8.752933199764357e-09, 1.1142235534138725e-08, 1.5248150192083915e-09, 3.2525299528589145e-08] [4.4614001350141411e-07, 0.00085706851362898125, 7.0880027694593626e-07, 0.00017838078600001828, 1.5238356021368048e-06, 5.9339989291295121e-08, 4.3683170260904018e-08, 2.5665273996094688e-09, 1.2215900183563634e-07]
['Br-81', 'Na-23', 'As-75', 'K-41', 'Hg-202', 'Ba-130', 'Sb-121', 'Cs-133', 'Rb-85', 'Se-74', 'Co-59', 'Au-197']
[4.5032509811441342e-06, 0.00014233834991866779, 3.1936581818473208e-06, 0.00055975309683840976, 1.5364157486957062e-07, 2.4439269385561461e-08, 2.9027982954116786e-07, 4.6639296628049194e-07, 2.2835654232059976e-07, 3.1974716116795688e-08, 1.1517967536514361e-07, 1.6421892513099547e-09] [1.3117256793367655e-07, 4.5289944988636385e-06, 3.0549043279113623e-07, 1.7091850504028624e-05, 2.0634920526676074e-08, 4.2249592914421673e-09, 9.257511580146415e-08, 8.380822262171181e-08, 2.2893489021859385e-08, 3.7033770053001204e-09, 1.3000836657502034e-08, 2.1114333329045831e-10] [1.4372248403729972e-06, 6.2666088748110233e-05, 9.4326744409067551e-07, 0.00028653169326431447, 1.8990254627133382e-08, 2.9349229883294293e-09, 9.4776224764426185e-08, 1.2593875924709783e-07, 1.0861510616886414e-07, 3.6087893165385252e-09, 5.8488583420612704e-08, 3.0440739600613014e-10]
['Br-81', 'Na-23', 'Co-59', 'Se-74', 'Hg-202', 'Rb-85', 'Sb-123', 'Cs-133', 'K-41', 'As-75']
[4.584288624042647e-06, 0.0002973714049115618, 1.1767749796301746e-06, 3.5317950874059297e-08, 3.1162206296236648e-08, 4.5761656745579354e-07, 5.0720300979003536e-08, 1.365866261381633e-07, 0.00076716127682726959, 2.6816879514751792e-06] [1.4773817903499359e-07, 1.0901398356224275e-05, 1.3156681600214087e-07, 2.7624857673429962e-09, 2.8007525183885512e-09, 4.3354899831164352e-08, 8.864042263701448e-09, 2.0456255735220211e-08, 2.7633016242748392e-05, 3.9520710298323338e-07] [1.4751632362429916e-06, 0.00013194160300955118, 6.0103008464549598e-07, 3.3274140262158453e-09, 3.8516803333418449e-09, 2.1765994332258049e-07, 1.435791063599487e-08, 4.0277330293612678e-08, 0.00039270174814162327, 8.3228101641568271e-07]
['Br-81', 'K-41', 'Co-59', 'As-75', 'Na-23', 'Ba-130', 'Se-74', 'Cs-133', 'Hg-202', 'Rb-85']
[7.0467698941292609e-06, 0.0005813494800604521, 3.9647350208436625e-06, 1.8178915328429951e-06, 0.00042717935919188595, 6.9498661983420174e-08, 4.8611664271928226e-08, 9.1200181035821522e-07, 1.7020394439104688e-07, 2.7129747909448045e-07] [1.668971535337573e-07, 1.5496280723238104e-05, 2.8510383681821225e-07, 2.1093535453036303e-07, 2.0648426311926393e-05, 1.458080064348965e-08, 5.6724412947478774e-09, 2.3715066882078902e-07, 5.0583693199556131e-09, 2.6219870787116693e-08] [2.3115800643464586e-06, 0.00029758665354581726, 2.0127354527892866e-06, 5.4289093885032216e-07, 0.00018386977285090152, 5.5844829763660047e-09, 4.6986317926590431e-09, 2.5772888837144778e-07, 2.1037380313709512e-08, 1.2903945819000086e-07]
['Br-81', 'K-41', 'Co-59', 'As-75', 'Na-23', 'Se-74', 'Rb-85', 'Cs-133', 'Fe-58']
[5.4563794273447788e-06, 0.00057445517811310041, 2.1232546143139794e-06, 1.9988447503207728e-06, 0.00034454812973092768, 3.7879808527050678e-08, 4.2458236020554477e-07, 1.3251961961347477e-07, 2.4063734295663065e-08] [2.8567657622438434e-07, 2.328963649506146e-05, 2.0900967556091178e-07, 1.9246224829535964e-07, 2.9872198197016554e-06, 2.7253357313954097e-09, 3.5108776386336348e-08, 1.3154660239904475e-08, 3.7159901723356535e-09] [1.7207502896379095e-06, 0.00029405753325687601, 1.0758100523742996e-06, 5.5369619048600287e-07, 0.00015098924959425675, 3.5636866498906053e-09, 2.0194761079543683e-07, 4.1897258157809561e-08, 1.2300797009042272e-08]
['Na-23', 'K-41', 'Br-81', 'Se-74', 'As-75', 'Cs-133', 'Rb-85', 'Fe-58', 'Co-59']
[0.0011751078573370829, 0.00083100039671398385, 2.4866597088277416e-06, 1.6239568268945127e-08, 1.467143616803431e-06, 5.7498819343035674e-07, 3.0256630243130234e-06, 4.1712501347679239e-08, 1.1759104754826395e-07] [0.00035607315576029587, 2.7054469852499686e-05, 2.3493356359719188e-07, 1.7960146777502927e-09, 2.0808793676374918e-07, 8.6470773978708352e-08, 1.5264339834534043e-07, 4.6979923645928997e-09, 1.3248316100669946e-08] [0.00047043168725361442, 0.00042538031878457301, 2.7002797470740317e-07, 1.4304912203439212e-09, 4.4309833914335127e-07, 1.6827209667886876e-07, 1.4391210660195748e-06, 2.0953137333921871e-08, 5.9682810919862671e-08]
['Na-23', 'K-41', 'Se-74', 'Rb-85', 'Co-59', 'Hg-202', 'Au-197', 'Br-81']
[0.000388482756575245, 0.00041807359486867313, 1.7604729957233109e-08, 1.4360182197766663e-06, 2.4968002997858912e-07, 6.7609583198354447e-08, 2.1967718956959649e-09, 1.8391203806453816e-06] [2.6329839883682785e-06, 1.6567855815732232e-05, 1.0595730833549769e-09, 6.8116902526140486e-08, 2.2228413918584095e-08, 3.2695165187466058e-09, 1.7006591223558146e-10, 7.8896437120432247e-08] [0.0001700139527086448, 0.00021400745386389788, 1.7458450732180935e-09, 6.8302519304434141e-07, 1.2730328236158945e-07, 8.3566131189494579e-09, 4.0720861609271501e-10, 6.055193328042762e-07]
['Br-81', 'As-75', 'K-41', 'Na-23', 'Co-59', 'Se-74', 'Hg-202', 'Rb-85', 'Cs-133']
[6.2372063232170263e-06, 1.0359229108057572e-05, 0.00060373292381455008, 0.035661635680139664, 8.249959096965239e-07, 1.3237533986997208e-08, 7.2655666876940111e-08, 3.1362260924171876e-07, 1.2701709519651014e-07] [5.4286086107996217e-07, 2.1288659356294317e-06, 4.1779120264790186e-05, 0.012300783411571418, 1.2349300815777226e-07, 8.0379489539048949e-10, 5.4561059814143866e-09, 3.1311434849045055e-08, 1.7219245965689325e-08] [1.9377595400608755e-06, 2.814969453646377e-06, 0.00030904450179386304, 0.012040551047583795, 4.2018661812245594e-07, 1.3379426594978442e-09, 8.9803141842861914e-09, 1.4917090902489368e-07, 4.0743102169728703e-08]
['Br-81', 'As-75', 'K-41', 'Na-23', 'Au-197', 'Se-74', 'Hg-202', 'Sc-45', 'Cs-133', 'Co-59', 'Rb-85']
[8.8134855284555737e-06, 1.618058508751924e-05, 0.0003105183285415101, 0.00047733146189432823, 5.6909681719555565e-09, 3.9452719781540402e-08, 6.1482288442253008e-08, 1.2657259640749863e-09, 1.9631705271275194e-07, 6.1134317611127609e-08, 2.0668894046292463e-07] [5.3816068363435884e-07, 8.3138107705349793e-07, 3.0106127061574532e-05, 5.1653191937521703e-06, 6.5965386932022842e-10, 2.774539329436936e-09, 2.0369308765731852e-09, 1.6699734350671517e-10, 1.4670542394578485e-08, 6.6996453923993251e-09, 1.8625153578460438e-08] [2.705683894178599e-06, 4.7528248097622773e-06, 0.0001589510499703202, 0.00020987311496949566, 1.0549166611563579e-09, 3.5937869703858957e-09, 7.5992732668121442e-09, 5.2536904481268475e-10, 6.32212028840303e-08, 3.0324002411351606e-08, 9.8309165939256055e-08]
['Br-81', 'Na-23', 'As-75', 'K-41', 'Sb-121', 'Ba-130', 'Co-59', 'Hg-202', 'Sc-45', 'Cs-133', 'Rb-85', 'Se-74']
[4.7448209295565725e-06, 0.0001665931683237459, 1.8573312687696676e-06, 0.00048064464736729807, 8.9783614719323111e-08, 1.7843433510572339e-07, 5.5359442340960753e-07, 8.3584597255944376e-09, 8.9930103173421096e-10, 2.9921688940861255e-07, 5.7385020693838897e-07, 1.0031242781556682e-08] [1.5204574600407663e-07, 3.2870355021910719e-06, 3.1349900481307628e-07, 4.026099188283424e-05, 1.5927233150265034e-08, 2.529197233383422e-08, 5.9798062428607304e-08, 1.0879361173134306e-09, 2.0255696832521278e-10, 3.0664278845569266e-08, 4.5974911211977097e-08, 6.2467112882088417e-10] [1.5550430895548626e-06, 7.3464238173980999e-05, 5.6185965684855586e-07, 0.00024603691421530159, 2.3619826435012113e-08, 1.7603752041152769e-08, 2.8058931023439507e-07, 1.0331141073920011e-09, 3.7327584125727338e-10, 8.9007169954906115e-08, 2.7294510819896551e-07, 9.8153519877351782e-10]
['Br-81', 'Na-23', 'As-75', 'K-41', 'Co-59', 'Hg-202', 'Se-74', 'Fe-58', 'Sc-45', 'Rb-85']
/Users/jackiegasca/anaconda3/envs/py34/lib/python3.4/site-packages/numpy/core/fromnumeric.py:2909: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/Users/jackiegasca/anaconda3/envs/py34/lib/python3.4/site-packages/numpy/core/_methods.py:80: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
[1.6292329826962575e-05, 0.0002770328486662521, 2.9091897875317452e-06, 0.00029212637370055637, 4.7853797424169879e-07, 4.9476586826498699e-08, 3.9676092879964378e-08, 5.5157111863330504e-08, nan, 3.6640169652114471e-07] [2.7398614997974663e-06, 2.6743893584292821e-06, 2.5445581457832755e-07, 1.8963258616462068e-05, 2.9574201456457871e-08, 2.8298150453710242e-09, 1.7710133770344126e-09, 6.4445439396984806e-09, nan, 3.7433542707373284e-08] [8.0564565787684068e-06, 0.0001221639748724407, 8.4967865376546945e-07, 0.00014953640270390123, 2.4432473166403271e-07, 7.2101360231926356e-09, 3.5417219867274374e-09, 2.2883561963021874e-08, nan, 1.9696557513226767e-07]
['K-41', 'Na-23', 'Br-81', 'As-75', 'Co-59', 'Hg-202', 'Sb-121', 'Rb-85', 'Sb-123', 'Cs-133', 'Se-74']
[0.00032251886612820436, 0.00024746052735989438, 4.830691878279009e-06, 6.1987328373295297e-06, 3.6790481399191004e-07, 4.148982049494236e-08, 9.4862646928593111e-07, 1.7584377598726632e-07, 1.1176901447962286e-08, 5.0559893860361335e-08, 1.4402653635967757e-08] [3.6233670843719486e-05, 5.3151455334178897e-06, 1.7961861543831011e-07, 6.284723305046005e-07, 4.8328102927767337e-08, 2.4786944388112023e-09, 4.1187085366730639e-07, 2.6602748421659042e-08, 2.266936362605561e-09, 8.4804082595141147e-09, 7.4512743181283974e-10] [0.00016509399830632592, 0.00010898887316676865, 1.513733260993667e-06, 1.8479071223577879e-06, 1.8663118260986671e-07, 5.1281839326489465e-09, 3.0665313696405558e-07, 8.3638025886626664e-08, 3.1639589884057704e-09, 1.6606663287156338e-08, 1.3569523201181739e-09]
['K-41', 'Co-59', 'Br-81', 'As-75', 'Cs-133', 'Na-23', 'Rb-85', 'Se-74', 'Hg-202']
[0.00032557583234021871, 5.6124890938521236e-07, 4.178771105996371e-06, 3.7970146807017559e-06, 2.5576314762828303e-07, 0.00022019844465769652, 4.0910825297001059e-07, 8.6559949780430167e-09, 1.2539910904569415e-08] [4.9872855715265824e-05, 9.813812005552477e-08, 1.6278714038096886e-07, 4.7641706708152229e-07, 5.6084894676195873e-08, 5.9702953161265821e-06, 4.0482548490154039e-08, 6.3771525418664551e-10, 1.3448169906623654e-09] [0.00016665882699584569, 2.8203570080121495e-07, 1.3163475062685208e-06, 1.1170072143872567e-06, 7.968121060171257e-08, 9.2297350554669508e-05, 1.9458753350938152e-07, 8.3583697390247251e-10, 1.5499457180226011e-09]
['Na-23', 'Br-81', 'K-41', 'As-75', 'Rb-85', 'Co-59', 'Sc-45', 'Cs-133', 'Hg-202', 'Se-74']
[0.00035835382294287189, 4.4589835112864227e-05, 0.00042794933854469163, 2.4898374433830188e-06, 6.0616816880958208e-07, 3.109849705621742e-07, 5.5794248353376806e-09, 8.4106172594421944e-07, 1.7247791573442894e-07, 3.3392173986463601e-08] [7.5298875171455567e-06, 7.2147390701451952e-06, 1.9086672997923408e-05, 3.8495136654058177e-07, 7.626196424250769e-08, 4.5214824261277386e-08, 1.336704289985422e-09, 9.5618020620223619e-08, 9.2287796859132899e-09, 1.9736744073348578e-09] [0.00015714616620326693, 1.3410624257202421e-05, 0.00021906274265768325, 7.4360769047951944e-07, 2.8831676702743392e-07, 1.5769046497901658e-07, 2.6753452620894089e-09, 2.6097520116393529e-07, 2.1318445480232866e-08, 3.2081011109201593e-09]
['Na-23', 'K-41', 'Co-59', 'Se-74', 'Sr-84', 'Sc-45', 'As-75', 'Hg-202', 'Rb-85', 'Fe-58', 'Br-81']
[0.00039516738104913894, 0.00016242602005410353, 1.8883650402135622e-06, 8.3121368440298537e-09, 3.7524560397514712e-07, 1.0881932303062156e-08, 1.5532095684199841e-06, 1.8080930771968281e-08, 2.8652012851368884e-07, 5.6685789067647788e-08, 3.9617821338347334e-05] [4.3837925439888381e-06, 1.1950892775697636e-05, 4.8969772654532237e-08, 5.2258214744958238e-10, 1.300732229816387e-08, 6.0706504506057351e-10, 1.8573145270850625e-07, 8.4445371657292986e-10, 1.8155767091760643e-08, 3.0107699279079889e-09, 2.2128109175368197e-06] [0.00017378191119293054, 8.3144162701651009e-05, 9.5957448563821262e-07, 7.6138849612495324e-10, 8.903981391709519e-08, 4.8623250623218919e-09, 4.7510534553585954e-07, 2.234821398744028e-09, 1.3627993252034617e-07, 2.8306852032022463e-08, 1.271033495042932e-05]
['K-41', 'Br-81', 'Na-23', 'Hg-202', 'Co-59', 'Se-74', 'Cs-133', 'Sc-46']
[0.00020827209840965381, 6.0343487617097189e-06, 0.00037103918473825137, 4.7294590123719788e-07, 3.4799071012320673e-07, 2.8132296336610319e-08, 1.8322047643285914e-07, nan] [1.3862530054930947e-05, 2.2214326089687014e-07, 1.00582744243307e-05, 1.4576256074891356e-08, 4.7611937188645116e-08, 1.8669358196655078e-09, 2.6213484350724639e-08, nan] [0.00010661228558465219, 1.9884889331086147e-06, 0.00016928145027830793, 5.8456593516292162e-08, 1.7657206351999571e-07, 2.7450602212010859e-09, 5.0609627685775805e-08, nan]
Additional comments: The log scale on the y-axis creates and uneven appearance between the low point of the efficiency and the high point. The statistical uncertainty bars are barely visible because the statistical uncertianty is very small compared to the systenmatic uncertainty.

In [ ]: