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

function [ fio2_held ] = pnFiO2(data)
%PNFIO2	Extract FiO2 at time points associated with a PaO2 or PaCO2
%measurement
%	[ fio2 ] = pnFiO2(data) extracts the FiO2 at the time points present in
%	the data set. Additionally, it uses sample-and-hold to interpolate the
%	FiO2 value at time points associated with PaO2 or PaCO2 measurements in
%	the data. The interpolation used the following rules:
%       If the patient was never on a mv then FiO2 was always = 1.
%       If the patient was on a mechanical ventilation:
%           If the first FiO2 when vented was missing then it was set = 1.
%           If a subsequent FiO2 was missing, then the value before it was 
%           carried forward.
%
%	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:
%		fio2    - Cell array of only fio2 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
%           'FiO2')
%           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
%       [ fio2 ] = pnFiO2(data);
%	
%	See also 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 16:57:08
%	Contact: alistairewj@gmail.com

%=== Extract fields of interest
pao2 = pnExtractField(data,'PaO2');
pco2 = pnExtractField(data,'PaCO2');
fio2 = pnExtractField(data,'FiO2');
mv = pnExtractField(data,'MechVent');

fio2_held = fio2;

%=== Loop through each patient
N = size(pao2,1);
for k=1:N
    %=== If PaO2 and PaCO2 are empty, no need to do anything
    if isempty(pao2{k,2}) && isempty(pco2{k,2}) 
        continue; % Both missing, no imputation
    else
        t1 = pao2{k,2}; t2 = pco2{k,2};
        t = unique([t1;t2]);
        
        %=== Check MechVent - if empty, FiO2 is 1 always
        if isempty(mv{k,2})
            % Impute 1 for all PaO2 and PaCO2 entries
            fio2_held{k,2} = t;
            fio2_held{k,3} = repmat({'FiO2'},numel(t),1);
            fio2_held{k,4} = ones(numel(t),1);
        elseif isempty(fio2{k,2})
            % Impute 1 for all PaO2 and PaCO2 entries
            fio2_held{k,2} = t;
            fio2_held{k,3} = repmat({'FiO2'},numel(t),1);
            fio2_held{k,4} = ones(numel(t),1);
        else
            %=== Carry forward existing fio2 values
            t_held = [fio2{k,2};t];
            d_held = [fio2{k,4};zeros(numel(t),1)];
            [t_held,idxSort] = unique(t_held,'first');
            d_held = d_held(idxSort);
            
            %=== If the first time measurement point is 0, we must impute
            %an FiO2 of 1, as it was not measured
            % fprintf('Record %g - Imputing FiO2 value of 1 as earliest PaO2/PaCO2 measurement is before FiO2 measurement.\n',k);
            if d_held(1)==0
                d_held(1) = 1;
            end
            
            %=== Sample and hold values
            valind = find(d_held); % non-zero values
            d_held(valind(2:end)) = diff(d_held(valind));
            d_held = cumsum(d_held);
            
            %=== Push to new cell
            fio2_held{k,2} = t_held;
            fio2_held{k,3} = repmat({'FiO2'},numel(t_held),1);
            fio2_held{k,4} = d_held;
        end
    end
    
    
%     %=== Print percent completion
%     checkpoint = mod(k,ceil(N*0.20));
%     if checkpoint==0
%         fprintf('%2.0f%% complete.\n',floor(k/N*100));
%     end        
    
end


end