<jupyter_start><jupyter_text>Initial imports<jupyter_code>import numpy
import matplotlib.pyplot as plt
import stir
from stirextra import *
import os
import io<jupyter_output><empty_output><jupyter_text>go to directory with input files<jupyter_code># adapt this path to your situation (or start everything in the exercises directory)
os.chdir(os.getenv('STIR_exercises_PATH'))<jupyter_output><empty_output><jupyter_text>change directory to where the output files are<jupyter_code>os.chdir('working_folder/randoms')<jupyter_output><empty_output><jupyter_text>Define functions that read and write the .out files used by STIR<jupyter_code>"""
  STIR uses a Mathematica-type of text-format for arrays, where {} are used for every row.
  Here we define a function that gets rid of the braces and then uses numpy.fromstring
  to read the comma-separated values into a numpy array.
  
  Warning: the next implementation only works for 1D arrays
"""
def readSinglesFile(filename):
    f=io.open(filename,'r');
    lines=f.readlines();
    f.close();
    array=numpy.array([]);
    for l in lines:
        l=l.replace('}','').replace('{','').strip();
        if len(l)!=0:
            a=numpy.fromstring(l,sep=',');
            array=numpy.append(array,a);
    return array;
    
"""
  Write a 1D array as a STIR text-file for 2D arrays, as appropriate for the STIR normalisation code
"""
def writeSinglesFile(filename, singles):
    numpy.savetxt(filename, singles.reshape(1,singles.shape[0]), delimiter=',', fmt='%g',header='{{', footer='}}', comments='');<jupyter_output><empty_output><jupyter_text>read original and estimated singles<jupyter_code>mean_singles=readSinglesFile('true_singles_eff_1_1.out');
estimated_singles=readSinglesFile('estimated_singles_eff_1_10.out');
fansums=readSinglesFile('fansums_for_my_randoms.dat');<jupyter_output><empty_output><jupyter_text>read sinograms<jupyter_code>mean_randoms=to_numpy(stir.ProjData.read_from_file('mean_randoms.hs'));
noisy_randoms=to_numpy(stir.ProjData.read_from_file('my_randoms.hs'));
estimated_randoms=to_numpy(stir.ProjData.read_from_file('estimated_randoms.hs'));<jupyter_output><empty_output><jupyter_text>Plot sinograms<jupyter_code>maxforplot=mean_randoms.max()*1.3;
slice=0;

plt.figure()
ax=plt.subplot(1,3,1);
plt.imshow(mean_randoms[slice,:,:,]);
plt.clim(0,maxforplot)
ax.set_title('mean of\nrandoms');
plt.axis('off');
plt.colorbar();

ax=plt.subplot(1,3,2);
plt.imshow(noisy_randoms[slice,:,:,]);
plt.clim(0,maxforplot);
ax.set_title('noisy randoms\n(i.e. delayeds)');
plt.axis('off');
plt.colorbar();

ax=plt.subplot(1,3,3);
plt.imshow(estimated_randoms[slice,:,:,]);
plt.clim(0,maxforplot);
ax.set_title('estimated\nrandoms');
plt.axis('off');
plt.colorbar();<jupyter_output><empty_output><jupyter_text>plot singles themselves<jupyter_code>plt.figure()
#plt.hold(True)
plt.plot(mean_singles,'b.-')
plt.plot(estimated_singles,'g.-')
plt.legend(('original singles','estimated singles'));
plt.xlabel('detector number')
plt.gca().set_title('singles');<jupyter_output><empty_output><jupyter_text>Plot the ratio of the fansums with estimated<jupyter_code>plt.figure()
plt.plot(fansums/estimated_singles,'r.-')
plt.xlabel('detector number')
plt.gca().set_title('fansums / estimated');<jupyter_output><empty_output><jupyter_text>Extended exercise: set some singles to zero<jupyter_code># Here we modify the mean of the singles (i.e. the simulation input)
# by setting some values to zero (e.g. to simulate a defective block)
# 
# After this, run the run_randoms.sh script again to get new sinograms and estimates.
# You can the just repeat the evaluation by executing this evaluation script again).
mean_singles[10:18]=0;
writeSinglesFile('true_singles_eff_1_1.out',mean_singles)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14198513800424628, 'fraction_numerical': 0.00875796178343949, 'mean_word_length': 7.375555555555556, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '193010', 'license': 'Apache-2.0', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1360, 'n_words': 258, 'path': 'notebooks/evaluate_randoms.ipynb', 'repo_name': 'NikEfth/STIR-exercises', 'size': '3768'}