Predicting Mortality of ICU Patients: The PhysioNet/Computing in Cardiology Challenge 2012 1.0.0
(5,561 bytes)
function [cont_times,cont_values,cont_names,d,dd,md_max,cont_values2,variance]=resample_params(time_series_names,times,values,names,des,des_names)
cont_names=time_series_names;
num_params=max(size(time_series_names));
num_samples=24*2*2;
cont_times=zeros(num_params,num_samples);
cont_values=zeros(num_params,num_samples);
cont_values2=zeros(num_params,num_samples);
xx=linspace(0,num_samples*30,96); %minutes
% cubic spline interolation of time series parameters
for param_id=1:num_params
selector=strcmp(time_series_names(param_id),names);
if sum(selector)==1
Y=values{selector};
x=times{selector};
limits=get_param_limits_by_name(time_series_names{param_id});
for y_idx=1:length(Y)
if Y(y_idx) < limits(1) || Y(y_idx)>limits(2)
Y(y_idx)=NaN;
end
end
x=x(~isnan(Y(:)));
Y=Y(~isnan(Y(:)));
for x_idx=1:length(x)-1
if x(x_idx)==x(x_idx+1)
Y(x_idx)=(Y(x_idx)+Y(x_idx+1))/2;
x(x_idx)=NaN;
Y(x_idx)=NaN;
end
end
x=x(~isnan(Y(:)));
Y=Y(~isnan(Y(:)));
if isempty(Y)
cont_times(param_id,:)=NaN;
cont_values(param_id,:)=NaN;
continue
end
if max(size(Y))==1
cont_times(param_id,:)=xx;
cont_values(param_id,:)=Y;
continue
end
if x(1) > 0
Y=[Y(1) Y];
x=[xx(1) x];
end
if x(end) < xx(end)
Y=[Y Y(end)];
x=[x xx(end)];
end
% size(x)
% if max(size(Y))==1
% pause(1)
% end
% size(xx)
% spline resampling
% yy = spline(x,Y,xx);
% linear resampling
ts1 = timeseries(Y,x,'Name','speed');
res_ts=resample(ts1,xx);
yy=squeeze(res_ts.Data);
for y_idx=1:length(yy)
if yy(y_idx) < limits(1)-0.1*abs(limits(2)-limits(1)) || yy(y_idx)>limits(2)+0.1*abs(limits(2)-limits(1))
yy(y_idx)=median(Y);
end
end
% View the time, data. amd interpolation method:
% Resample ts1 using its default interpolation method:
cont_times(param_id,:)=xx;
cont_values(param_id,:)=yy;
% hold off
% plot(cont_times(param_id,:),cont_values(param_id,:))
% hold on
% scatter(x,Y)
% title(time_series_names{param_id})
% pause(.1)
else
cont_times(param_id,:)=NaN;
cont_values(param_id,:)=NaN;
end
end
% loop goes through time and adds missing params
if des{:,strcmp(des_names,'Gender')}==0
savefile='dynamic_gender0.mat';
load(savefile,'N2','PHAT_all','best_th','A','S','Mu','V', 'CV', 'HP', 'LC','N','pc1','mu','sigma','B');
elseif des{:,strcmp(des_names,'Gender')}==1
savefile='dynamic_gender1.mat';
load(savefile,'N2','PHAT_all','best_th','A','S','Mu','V', 'CV', 'HP', 'LC','N','pc1','mu','sigma','B');
else
savefile='dynamic.mat';
load(savefile,'N2','PHAT_all','best_th','A','S','Mu','V', 'CV', 'HP', 'LC','N','pc1','mu','sigma','B');
end
for sample_id=1:num_samples
X=cont_values(:,sample_id);
S_new=calc_S_new_data(X,A,V,Mu,N,CV);
X_new_rec =( repmat(Mu,1,1) + A*S_new)';
% cont_values(:,sample_id)=X_new_rec';
sigma0 = sigma;
sigma0(sigma0==0) = 1;
z = bsxfun(@minus,X_new_rec', mu');
z = bsxfun(@rdivide, z, sigma0');
x_in=pinv(pc1)*z;
x_in=x_in(1:N2);
% cont_values(1:N2,sample_id)=z;
cont_values(:,sample_id)=z;
cont_values2(:,sample_id)=z;
end
if sum(sum(isnan(cont_values)))>0
cont_values(isnan(cont_values))=0;
end
%if sum(c)
%cont_values=cont_values(1:N2,:);
variance=var(cont_values2,0,2);
data_width=96;
window=24;
step=1;
md_max=-1;
d=[];
dd=[];
%C=cov(cont_values(:,1:end)');
% c = rcond(C);
% if c < 1e-5
% pause(1)
%
% end
for md_idx=window:data_width-window
m1=median(cont_values(:,md_idx-window+1:md_idx),2);
m2=median(cont_values(:,md_idx+1:md_idx+window),2);
md=(m1-m2)'*(m1-m2);
if md > md_max
md_max=md;
d=m1;
dd=m1-m2;
end
end
% if md_max > 1e2
%
%
% for p_idx=1:min(size(cont_values2))
%
% figure(p_idx)
% hold off
%
% scatter(cont_times(p_idx,:),cont_values2(p_idx,:))
% hold on
% %scatter(x,Y)
% title(time_series_names{p_idx})
%
%
%
%
%
%
% end
% pause(.1)
% end
% for idx=learning_width:data_width-test_width
% % For matrix input X, where each row is an observation, and each column is a variable, cov(X) is the covariance matrix
%
% %near history
% % C=cov(selected_data(:,(idx-learning_width+1):idx)');
%
% % all history
% C=cov(selected_data(:,1:idx)');
%
% history_mean=mean(selected_data(:,idx-learning_width+1:idx),2);
% test_mean=mean(selected_data(:,idx+1:idx+test_width),2);
% Md(idx)=(history_mean-test_mean)'*inv(C)*(history_mean-test_mean);
% end
end