Predicting Mortality of ICU Patients: The PhysioNet/Computing in Cardiology Challenge 2012 1.0.0

function [ pafi ] = pnPaFi(data)
%PNPAFI	Extract PaFi, ensuring they are measured at the same time point
%	[ pafi ] = pnPaFi(data) extracts the PaFi ratio, or the ratio between
%	PaO2 and FiO2 (PaO2 / FiO2) for syncronously measured PaO2 and FiO2
%	values. FiO2 is interpolated using sample-and-hold as detailed in
%	pnFIO2.
%	
%	Inputs:
%		data    - Cell array of data inputs in standard format:
%           Column 1 - Subject IDs
%           Column 2 - Time stamp vectors for each subject
%           Column 3 - Feature name vectors for each subject
%           Column 4 - Data value vectors for each subject
%       Empty cells indicate no available measurements (missing data).
%
%	Outputs:
%		pafi    - Cell array of only pafi data in standard format:
%           Column 1 - Subject IDs
%           Column 2 - Time stamp vectors for each subject
%           Column 3 - Feature name vectors for each subject (only contains
%           'PaFi')
%           Column 4 - Data value vectors for each subject
%       Empty cells indicate no available measurements (missing data).
%
%	Example
%       %=== Load data in
%       load('data_processed_cell.mat');
%
%       %=== Calculate score
%       [ pafi ] = pnPaFi(data);
%	
%	See also PNFIO2 PNEXTRACTFIELD PNLOADTEXTFILESCELL PNIANDREW PNMAIN 

%	References:
%       Physionet Challenge 2012
%       http://physionet.org/challenge/2012/

%	Copyright 2012 Alistair Johnson

%	$LastChangedBy: alistair $
%	$LastChangedDate: 2012-04-24 22:39:24 +0100 (Tue, 24 Apr 2012) $
%	$Revision: 342 $
%	Originally written on GLNXA64 by Alistair Johnson, 24-Apr-2012 14:02:09
%	Contact: alistairewj@gmail.com

%=== PaFi = PaO2 / FiO2
pao2 = 'PaO2';
pao2 = pnExtractField(data,pao2);

%=== Extract sample and hold FiO2 values
[ fio2 ] = pnFiO2(data);

%=== For each record, extract PaO2 times and calculate PaFi at them
pafi = cell(size(pao2));
pafi(:,1) = pao2(:,1); % subject ID

N = size(pao2,1);
for k=1:N
    if isempty(pao2{k,2})
        continue;
    else
        %=== Get times for pao2 and fio2
        t1 = pao2{k,2}; t2 = fio2{k,2};
        
        %=== Intersection
        [t_impute,ti,tj] = intersect(t1,t2);
        
        pafi{k,2} = t_impute;
        pafi{k,3} = repmat({'PaFi'},numel(t_impute),1);
        pafi{k,4} = pao2{k,4}(ti) ./ fio2{k,4}(tj);
    end
end

end