<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi again,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I'm following up with some further calculations/derivations of the relative statistical uncertainty. According to the docs, GATE outputs the relative statistical uncertainty. Using the usual statistical formulae, this would be the standard deviation divided
 by the dose. Given that the standard deviation is the square root of the sample variance, the final formula should be
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<div style="color: #d4d4d4; background-color: #1e1e1e; font-family: 'Droid Sans Mono', 'monospace', monospace, 'Droid Sans Fallback'; font-weight: normal; font-size: 14px; line-height: 19px">
<div><span></span></div>
<div style="color: #d4d4d4; background-color: #1e1e1e; font-family: 'Droid Sans Mono', 'monospace', monospace, 'Droid Sans Fallback'; font-weight: normal">
<div><span>        sampleVariance = (</span><span style="color: #b5cea8">1</span><span>/(N-</span><span style="color: #b5cea8">1</span><span>)) * (sq_dose -
</span><span style="color: #dcdcaa">pow</span><span>(dose,</span><span style="color: #b5cea8">2</span><span>)/N)</span></div>
<div><span>        standardDeviation = np.sqrt(sampleVariance)</span></div>
<div><span>        <span>std_dose</span> = standardDeviation/dose</span></div>
<div><span>        </span><span style="color: #6a9955"># or inline:<br>
</span></div>
<div><span>        std_dose = np.sqrt( (</span><span style="color: #b5cea8">1</span><span>/(N-</span><span style="color: #b5cea8">1</span><span>))*(sq_dose -
</span><span style="color: #dcdcaa">pow</span><span>(dose,</span><span style="color: #b5cea8">2</span><span>)/N) )/dose</span></div>
</div>
</div>
<br>
</div>
<div>
<div id="appendonsend"></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
When comparing my calculated uncertainty to the reference uncertainty, I am now 58% different at the origin (opposed to >8000%) but the average percent difference is 77% (opposed to 1% using the GATE formula). I am overall quite confused how the uncertainty
 is calculated by GATE, especially considering that the docs (<a href="https://opengate.readthedocs.io/en/latest/voxelized_source_and_phantom.html#dose-collection">https://opengate.readthedocs.io/en/latest/voxelized_source_and_phantom.html#dose-collection</a>)
 outline the dose calculations using the sample variance with a formula that represents the population variance.</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
If anyone would be so kind to instruct me how GATE calculates the uncertainty, and/or how I can reproduce the uncertainty using the Dose, Dose-Squared, and NbOfHits files, it would be greatly appreciated!</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Positively,</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Matthew</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b> Gate-users <gate-users-bounces@lists.opengatecollaboration.org> on behalf of Matthew Strugari <matthew.strugari@dal.ca><br>
<b>Sent:</b> February 6, 2020 2:59 PM<br>
<b>To:</b> gate-users@lists.opengatecollaboration.org <gate-users@lists.opengatecollaboration.org><br>
<b>Subject:</b> [Gate-users] Problems/inconsistencies calculating dose uncertainty</font>
<div> </div>
</div>
<div dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Hi all,</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
I am in need of assistance when calculating the dose uncertainty using output from the dose actor. I would like to merge multiple dose point kernel outputs from the dose actor to speed up my overall simulation time. Using the output from a single simulation
 to verify the calculation of the uncertainty, my results are ~1% different from the reference GATE output on average with a >8000% difference at the origin. I am using the formula found in GateDoseActor.cc:</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<div style="color:#d4d4d4; background-color:#1e1e1e; font-family:'Droid Sans Mono','monospace',monospace,'Droid Sans Fallback'; font-weight:normal; font-size:14px; line-height:19px">
<div><span>      </span><span style="color:#569cd6">double</span><span> std_dose =
</span><span style="color:#dcdcaa">sqrt</span><span>( (</span><span style="color:#b5cea8">1.0</span><span>/(N-</span><span style="color:#b5cea8">1</span><span>))*(sq_dose/N -
</span><span style="color:#dcdcaa">pow</span><span>(dose/N, </span><span style="color:#b5cea8">2</span><span>)) )/(dose/N);</span></div>
<div><span>      </span><span style="color:#c586c0">if</span><span>( dose == </span>
<span style="color:#b5cea8">0.0</span><span> || N == </span><span style="color:#b5cea8">1</span><span> || sq_dose ==
</span><span style="color:#b5cea8">0</span><span> )</span></div>
<div><span>        std_dose = </span><span style="color:#b5cea8">1.0</span><span>;</span></div>
</div>
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Here, the corresponding image data (attached in doseActorOutput.zip) are assigned as dose = Dose.mhd, sq_dose = Dose-Squared.mhd, and N = NbOfHits.mhd and then element-wise operations are performed. My approach is slightly different as I mask the matrices to
 avoid nan/inf results:<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<div style="color:#d4d4d4; background-color:#1e1e1e; font-family:'Droid Sans Mono','monospace',monospace,'Droid Sans Fallback'; font-weight:normal; font-size:14px; line-height:19px">
<div><span>        N = np.ma.masked_equal(N, </span><span style="color:#b5cea8">0</span><span>)</span></div>
<div><span>        N = np.ma.masked_equal(N, </span><span style="color:#b5cea8">1</span><span>)</span></div>
<div><span>        dose = np.ma.masked_equal(dose, </span><span style="color:#b5cea8">0.0</span><span>)</span></div>
<div><span>        sq_dose = np.ma.masked_equal(sq_dose,</span><span style="color:#b5cea8">0</span><span>)</span></div>
<br>
<div><span>        </span><span style="color:#6a9955"># calculate uncertainty using formula from GateDoseActor.cc</span></div>
<div><span>        std_dose = np.sqrt( (</span><span style="color:#b5cea8">1.0</span><span>/(N-</span><span style="color:#b5cea8">1</span><span>))*(sq_dose/N -
</span><span style="color:#dcdcaa">pow</span><span>(dose/N, </span><span style="color:#b5cea8">2</span><span>)) )/(dose/N)</span></div>
<div><span>
<div style="color:#d4d4d4; background-color:#1e1e1e; font-family:'Droid Sans Mono','monospace',monospace,'Droid Sans Fallback'; font-weight:normal">
<div><span></span>
<div style="color:#d4d4d4; background-color:#1e1e1e; font-family:'Droid Sans Mono','monospace',monospace,'Droid Sans Fallback'; font-weight:normal">
<div><span>        std_dose = np.ma.filled(std_dose, </span><span style="color:#b5cea8">1.0</span><span>).astype(np.float32)</span></div>
</div>
<span></span></div>
</div>
</span></div>
</div>
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Some/very few of the calculated std_dose values are greater than one (1.0000001 so I am not sure if this is indicative of an error or if I should just normalize std_dose before filling the masked values with 1. Also, the Dose-Uncertainty.mhd images appear identical
 in ImageJ but the actual values appear to be scaled differently (see attached doseKernel.zip).
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Please correct me in my calculation or provide any information on the correct way to calculate the uncertainty using the Dose, Dose-Squared, and NbOfHits files.</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Positively,</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Matthew<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
</div>
</div>
</body>
</html>