ET_PDToolkit/PDToolkit/@PDExperiment/export_stats.m

225 lines
7.9 KiB
Matlab

function[] = export_stats(e, file_out, varargin)
% Export routine for the PDExperiment class:
%
% Writes a textfile with relevant properties and statistics
% Options: Delimiter -> ','
%
% Thresholding:
%
% Quality -> Thresholding.Quality = [0/1],
% Default Quality Threshold adopted from
% PDExperiment class, can be altered in
% extra parameter QualityThreshold
%
% Baseline -> Thresholding.Baseline = [0/1]
% Check whether baseline is within range
% BaselineThreshold specifies this setting
%
% BlinkCount -> Thresholding.BlinkCount = [0/1]
% Check whether BlinkCount is within range
% BlinkCountThreshold specifies this setting
%
% MissingLabels -> Thresholding.MissingLabels = [0/1]
% MissingLabels is an optional cell array
% to filter trials without those labels/fieldnames
%
% MatchingLabels -> Thresholding.Label = [0/1]
% MatchingLabels is an optional cell array
% to filter labels with a certain value
%
settings = struct('Delimiter', ',',...
'LineEndings', '\n',...
'Thresholding', struct('Quality', 0, ...
'Baseline', 0, ...
'BlinkCount', 0, ...
'MissingLabels',0,...
'Label',0), ...), ...
'QualityThreshold', e.settings.QualityThreshold, ...
'BaselineThreshold', [],...
'BlinkCountThreshold', Inf, ...
'MissingLabels', {},...
'MatchingLabels', {});
settings(1).QualityThreshold = e.settings.QualityThreshold;
settings(1).Delimiter = ',';
settings(1).LineEndings = '\n';
settings = parse(settings, varargin);
fp = fopen(file_out, 'w+');
fprintf(fp,'%s\\n', repmat('-',1,100));
fprintf(fp,'\tExport on:\t %s\n', datestr(now));
%sprintf('\tPDParser version:\t%s\n', num2str(e.version));
sep = settings.Delimiter; % separator
le = settings.LineEndings; %'\n'; % line ending
int_f = '%d'; % integer format
dec_f = '%8.4f'; % decimal format
str_f = '"%s"'; %string format
max_typelength = -1; max_tl_ind = -1;
for i = 1:length(e.subjects(1).sessions(1).trials)
if max_typelength < length(fieldnames(e.subjects(1).sessions(1).trials(i).type))
max_tl_ind = i;
end
max_typelength = max(max_typelength, length(fieldnames(e.subjects(1).sessions(1).trials(i).type)));
end
types = fieldnames(e.subjects(1).sessions(1).trials(max_tl_ind).type);
type_header = '';format_type_header ='';
for t = 1:length(types)
type_header = [type_header types{t} sep '# of chars' sep];
format_type_header = [format_type_header str_f sep];
end
cov_header = []; cov_format = [];
for i =1:length(e.subjects(1).covariates)
cov_header = [cov_header sprintf('Covariate%d', i) sep];
cov_format = [cov_format int_f sep];
end
header = ['Subject' sep ...
cov_header ...
'Datafile' sep ...
'Subject Id' sep ...
'Session' sep ...
'Trial' sep ...
'Quality (%)' sep ...
'BlinkCount' sep ...
type_header, ...
'Baseline (raw)' sep ...
'Max' sep ...
'TimeOfMax (rel to s.o.)' sep ...
'Mean' sep ...
'Median' sep ...
'Mode' sep ...
'Q1 (25%)' sep ...
'Q2 (median)' sep ...
'Q3 (75%)' sep ...
'AUC' ];
format = [str_f sep ... % subject
'%s' ... % covariates
str_f sep ... % datafile
int_f sep ... % subject id
int_f sep ... % session
int_f sep ... % trial
dec_f sep ... % quality
int_f sep ... % blinkcount
'%s' ... % types are formatted within trial, so string with no formatting
dec_f sep ... % baseline
dec_f sep ... % max
dec_f sep ... % maxtime
dec_f sep ... % mean
dec_f sep ... % median
dec_f sep ... % mode
dec_f sep ... % q1
dec_f sep ... % q2
dec_f sep ... % q3
dec_f le]; % AUC
fprintf(fp,'%s\n', repmat('-',1,100));
fprintf(fp, '%s', header);
fprintf(fp, '\n');
for s = 1:length(e.subjects)
covariates = [];
for cov =1:length(e.subjects(1).covariates)
covariates = [covariates sprintf([dec_f sep], e.subjects(s).covariates(cov))];
end
for sess = 1:length(e.subjects(s).sessions)
for t = 1:length(e.subjects(s).sessions(sess).trials)
trial = e.subjects(s).sessions(sess).trials(t);
%Continue with the next trial if thresholding is enabled and
%trial suffers quality
if ((~isempty(settings.Thresholding)) && (settings.Thresholding.Quality) && (trial.quality < settings.QualityThreshold))
continue;
end
if ((~isempty(settings.Thresholding)) && (settings.Thresholding.Quality) && (trial.quality < settings.QualityThreshold))
continue;
end
%% Baseline items can be skipped
if (strmatch(trial.type.TRIAL_VAR_stim, 'Baseline'))
continue;
end
tt_string = ''; % build string containg type identifiers
for tt = 1:length(types)
if isfield(trial.type,types{tt})
if ischar(trial.type.(types{tt}))
numchars = length(strtrim(trial.type.(types{tt})));
else
numchars = -1;
end
trial_type_value = trial.type.(types{tt});
tt_string = [tt_string ...
sprintf([str_f sep], strrep(trial_type_value(1,:),'"', '')) ...
sprintf([int_f sep], numchars)];
else
tt_string = [tt_string ...
sprintf([str_f sep], ''), ...
sprintf([int_f sep], numchars)];
end
end
if ~isempty(trial.stats)
fprintf(fp, format, ...
e.subjects(s).name, ...
covariates,...
e.subjects(s).sessions(sess).datafile, ...
s, ...
sess, ...
t, ...
trial.quality, ...
trial.blink_count, ...
tt_string, ...
trial.baseline, ...
trial.stats.max.value, ...
trial.stats.max.time, ...
trial.stats.mean, ...
trial.stats.median, ...
trial.stats.mode, ...
trial.stats.q1, ...
trial.stats.q2, ...
trial.stats.q3, ...
trial.stats.auc);
end
end
end
end
fclose(fp);
display(sprintf('\t*) Statistics written to %s', file_out));
function[settings] = parse(settings,varlist);
if length(varlist)<2;return;end
f=fieldnames(settings);
for k=2:2:length(varlist);
if ~any(strcmp(varlist{k-1},f));
error(sprintf('invalid field %s\n',varlist{k-1}));
end
if isstruct(varlist{k})
substructfields = fieldnames(varlist{k});
for sf = 1:length(substructfields)
settings(1).(varlist{k-1}).(substructfields{sf}) = varlist{k}.(substructfields{sf});
end
else
settings=setfield(settings,varlist{k-1},varlist{k});
end
end