function output = get_sample_mitotic_score (sample_area, true_mitotic_rate, cp1, cp2, cells_per_area)
% -------------------------------------------------------------------------------------------------
% Michael Bonert, BASc (Mech Eng), MASc (Biomed Eng), MD, FRCPC
% September 30, 2012
% March 15, 2013 -- fix calc
% July 31, 2016 -- typo in comments
% -------------------------------------------------------------------------------------------------
% Copyright (c) 2013 Michael Bonert
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
% 02110-1301 USA
% -------------------------------------------------------------------------------------------------
% function simulates sampling procedure that a pathologists does
%
% Testing
% get_sample_mitotic_score( 2, 4.9, 3.466722, 7.106899, 2500)
% get_sample_mitotic_score( 2.2902210445, 4.9, 3.466722, 7.106899, 2500)
% -------------------------------------------------------------------------------------------------
% Input
% sample_area area sampled by pathologist (mm^2)
% true_mitotic_rate mitotic rate if the whole tumour is sampled (mitoses/mm^2)
% cp1 cutpoint one (mitoses/mm^2)
% cp2 cutpoint two (mitoses/mm^2)
% cells_per_area cells per area (cells/mm^2)
% Output
% output = [m_expected, true_mitotic_score, m_cp1, m_cp2, P_sample, m, m_sampled_per_area, m_score, distance2cp];
% m_expected expected number of mitoses (based on sample_area & true_mitotic_rate)
% true_mitotic_score the mitotic score based on the true_mitotic rate (1,2,3)
% m_cp1 lower mitotic cut point
% m_cp2 upper mitotic cut point
% P_sample percentile randomized sample mitotic rate (random number 0-1.0)
% m sample mitotic count
% m_sampled_per_area sample mitotic count per sample area
% m_score mitotic score based on cutpoints (1,2,3)
% distance2cp number of mitoses to nearest cutpoint (integer)
P_sample=rand();
TP= true_mitotic_rate/cells_per_area;
s=round(sample_area*cells_per_area);
m_expected=round(true_mitotic_rate*sample_area);
cdf_at_m_expected=binocdf(m_expected,s,TP);
if(cdf_at_m_expectedP_sample)
break;
endif
endif
ctr_m=ctr_m+1;
m=m+1*m_adjust_sign;
endwhile
% calculated sampled mitotic rate
m_sampled_per_area=m/sample_area;
% calculate sampled score
if (m_sampled_per_area<=cp1)
sample_m_score=1;
else
if (m_sampled_per_area>cp2)
sample_m_score=3;
else
sample_m_score=2;
endif
endif
% calculate mitotic cut points
m_cp1=round(cp1*sample_area);
m_cp2=round(cp2*sample_area);
% calculate distance to cutpoint
distance_cp1=m_cp1-m;
distance_cp2=m_cp2-m;
% calculate true score
if (m_expected<=m_cp1)
true_m_score=1;
else
if (m_expected>m_cp2)
true_m_score=3;
else
true_m_score=2;
endif
endif
if( abs(distance_cp1)