Skip to content

Commit

Permalink
Merge pull request automaticanalysis#173 from tiborauer/master
Browse files Browse the repository at this point in the history
Improvements and debugs
  • Loading branch information
jooh committed Mar 7, 2019
2 parents ee48221 + 80beecd commit 1a68e1d
Show file tree
Hide file tree
Showing 47 changed files with 476 additions and 375 deletions.
4 changes: 4 additions & 0 deletions aa_engine/aa_doprocessing.m
Expand Up @@ -433,6 +433,10 @@
aapsavefn=fullfile(aapsavepth,'aap_parameters');
aap.internal.aapversion=aa.Version;
aap.internal.aappath=aa.Path;
aap.internal.spmversion=spm('Version');
aap.internal.spmpath=spm('Dir');
aap.internal.matlabversion=version;
aap.internal.matlabpath=matlabroot;
save(aapsavefn,'aap');
end
end
Expand Down
4 changes: 4 additions & 0 deletions aa_engine/aaq/nonDCS/PoolClass.m
Expand Up @@ -4,6 +4,9 @@
JobStorageLocation
Jobs = JobClass.empty

Host = getenv('HOSTNAME')
NumWorkers

reqMemory = 1
reqWalltime = 1
initialConfiguration = ''
Expand Down Expand Up @@ -51,6 +54,7 @@

obj.Type = pool.Type;
obj.JobStorageLocation = pool.JobStorageLocation;
obj.NumWorkers = pool.NumWorkers;

switch obj.Type
case 'Torque'
Expand Down
1 change: 1 addition & 0 deletions aa_engine/aaq/nonDCS/TaskClass.m
Expand Up @@ -79,6 +79,7 @@
function val = get.State(obj)
val = 'unknown';
if exist(obj.ProcessFile,'file')
val = 'running';
fid = fopen(obj.ProcessFile,'r');
lines = textscan(fid,'%s','delimiter','@'); lines = lines{1};
fclose(fid);
Expand Down
4 changes: 4 additions & 0 deletions aa_engine/aaq/nonDCS/aaq_qsub_nonDCS.m
Expand Up @@ -26,11 +26,15 @@
pool.ResourceTemplate = '';
pool.SubmitArguments = '';
pool.AdditionalProperties.AdditionalSubmitArgs = '';

keyInd = arrayfun(@(x) strcmp(x.ATTRIBUTE.name,'NumWorkers'), xml.settings(2).settings.settings.key);
pool.NumWorkers = xml.settings(2).settings.settings.key(keyInd).double.value;
end
end
obj.pool = PoolClass(pool,obj.initialSubmitArguments,obj.poolConf{3});
obj.pool.reqMemory = aaparallel.memory;
obj.pool.reqWalltime = aaparallel.walltime;
obj.pool.NumWorkers = aaparallel.numberofworkers;
end

function obj = pool_args(obj,varargin)
Expand Down
7 changes: 3 additions & 4 deletions aa_engine/aas_checkreg.m
Expand Up @@ -57,9 +57,6 @@ function aas_checkreg(aap,varargin)
if ~isfield(aap.tasklist.currenttask.settings,'diagnostic') ||...
isstruct(aap.tasklist.currenttask.settings.diagnostic) ||...
aap.tasklist.currenttask.settings.diagnostic
% This will only work for 1-7 overlays
OVERcolours = {[1 0 0], [0 1 0], [0 0 1], ...
[1 1 0], [1 0 1], [0 1 1], [1 1 1]};

% One-by-one
for i = 1:size(image{1},1)
Expand All @@ -83,9 +80,11 @@ function aas_checkreg(aap,varargin)
end
if i > 1
% Summary
OVERcolours = distinguishable_colors(size(image{1},1));

spm_check_registration(image{2});
for i = 1:size(image{1},1)
spm_orthviews('addcolouredimage',1,image{1}(i,:), OVERcolours{i})
spm_orthviews('addcolouredimage',1,image{1}(i,:), OVERcolours(i,:))
end
aas_checkreg_avi(aap, index, 0,'_summary');
close(1);
Expand Down
41 changes: 21 additions & 20 deletions aa_engine/aas_connectAApipelines.m
Expand Up @@ -66,38 +66,35 @@
%
% -------------------------------------------------------
% created by: cwild 2014-03-10
%
% updates:
%
% rhodri & cwild 2014-09-*: Update to allow fully qualified stream names in
% local and remote analyses. E.g., using aamod_realign_00001.epi to fetch
% the epi stream from the realign stage of a remote analysis, instead of
% the last occurence of epi.
% cwild 2014-09-09: output/input stream searching respects branches in the
% local and remote analyses.
% cwild 2014-04-02: Major update, added check for udpated data on the
% remote
% cwild 2014-03-18: misc cleaning
%

% Error checking:
if isempty(aap.acq_details.subjects)
aas_log(aap, 1, 'aas_connectAApipelines() should be used after you have added subjects in your user script.');
end

if isempty(aap.acq_details.sessions)
aas_log(aap, 1, 'aas_connectAApipelines() should be used after you have added sessions in your user script.');
end

if any(~isfield(remoteAAlocations, {'host', 'directory', 'allowcache', 'maxstagetag', 'checkMD5'}))
aas_log(aap, 1, 'remoteAAlocations (input to aas_connectAApipelines) should be a struct array with the following fields: ''host'', ''directory'', ''allowcache'', ''maxstagetag'' ''checkMD5''');
end

global aaworker;

% We need to transfer over the remote AAP files, will put them here
studyPath = aas_getstudypath(aap);
% We need study directory for aas_findinputstreamsources and to transfer over the remote AAP files
studyPath = fullfile(aas_getstudypath(aap),[aap.directory_conventions.analysisid aap.directory_conventions.analysisid_suffix]);
aas_makedir(aap, studyPath);

% Initialise aap
% - evaluate subject names
aap=aas_doprocessing_initialisationmodules(aap);
% - integrate initial streams into the pipelines, if any
aap=aas_builddependencymap(aap);
v0 = aap.options.verbose;
aap.options.verbose = -1; % mute error on missing streams, for they will be connected later
aap=aas_findinputstreamsources(aap);
aap.options.verbose = v0;
% - remove partial initialisation
aap = rmfield(aap,'internal');

% Collect remote AA structures here
remoteAA = {};
Expand Down Expand Up @@ -284,6 +281,8 @@
srcDomain = remoteAA{remoteOutput.locI}.schema.tasksettings.(remoteModule.name)(remoteModule.index).ATTRIBUTE.domain;
srcModality = remoteAA{remoteOutput.locI}.schema.tasksettings.(remoteModule.name)(remoteModule.index).ATTRIBUTE.modality;

if strcmp(trgDomain,'*'), trgDomain = srcDomain; end % for general purpose modules, such as aamod_maths

remoteStreams(end+1) = struct('stream', inputStreams{iI}, ...
'stagetag', aas_getstagetag(remoteAA{remoteOutput.locI}, remoteOutput.modI), ...
'sourcedomain', srcDomain, ...
Expand Down Expand Up @@ -401,9 +400,11 @@
else

if isstruct(aap.schema.tasksettings.(mod.name)(mod.index).inputstreams.stream{iI}) && ...
isfield(aap.schema.tasksettings.(mod.name)(mod.index).inputstreams.stream{iI}, 'isessential') && ...
aap.schema.tasksettings.(mod.name)(mod.index).inputstreams.stream{iI}.ATTRIBUTE.isessential
aas_log(aap, 1, sprintf('%s''s input stream ''%s'' does not come from any module in this AA, or from one of your remote locations.\nTry connecting the AA pipelines *after* all aas_addinitialstream() calls in your user script.', mod.name, inputStreams{iI}));
(~isfield(aap.schema.tasksettings.(mod.name)(mod.index).inputstreams.stream{iI}, 'isessential') || ...
aap.schema.tasksettings.(mod.name)(mod.index).inputstreams.stream{iI}.isessential) && ...
(~isfield(aap.schema.tasksettings.(mod.name)(mod.index).inputstreams.stream{iI}.ATTRIBUTE, 'isessential') || ...
aap.schema.tasksettings.(mod.name)(mod.index).inputstreams.stream{iI}.ATTRIBUTE.isessential)
aas_log(aap, 1, sprintf('%s''s input stream ''%s'' does not come from any module in this pipeline, or from one of your remote locations.\nTry connecting the pipelines *after* all aas_addinitialstream() and aas_renamestream() calls in your user script.', mod.name, inputStreams{iI}));
end

end
Expand Down
3 changes: 1 addition & 2 deletions aa_engine/aas_convertseries_fromstream.m
Expand Up @@ -80,7 +80,6 @@
memLimit = meminfo; memLimit = memLimit.ResFree;
k=1;


while (k<=size(dicomdata_subdir,1))

oldAcquisitionNumber=-1;
Expand All @@ -90,7 +89,7 @@
% This array is used to collect sliceing timing info so we can
% reconstruct the slice order
sliceInfoD = zeros(0, 3);

while (k<=size(dicomdata_subdir,1))

tmp = spm_dicom_headers(deblank(dicomdata_subdir(k,:)));
Expand Down
10 changes: 5 additions & 5 deletions aa_engine/aas_findinputstreamsources.m
Expand Up @@ -64,7 +64,7 @@
end;

% Now we have one stream per cell
for i=1:length(streamlist)
for i=1:numel(streamlist)
inputstreamname=inputstreams.stream{i};
ismodified=1; isessential=1;
if isstruct(inputstreamname)
Expand Down Expand Up @@ -153,12 +153,12 @@
end;
end;
end;
if ~isempty(findremote) || ~isempty(stagethatoutputs)
% change domain and modality if needed (due to input)
if (~isempty(findremote) || ~isempty(stagethatoutputs)) && (i==numel(streamlist))
% change domain and modality if needed (due to input, which is expected to be the last stream)
currstage = aap.schema.tasksettings.(stagename)(index).ATTRIBUTE;
currstream = aap.internal.inputstreamsources{k1}.stream(end);
if (strcmp(currstage.domain,'*') && strcmp(currstage.modality,'MRI')) && ... % general-purpose module
(~isempty(strfind(currstream.sourcedomain,'session')) || ~strcmp(currstream.sourcemodality,'MRI')) % special
if (strcmp(currstage.domain,'*') && strcmp(currstage.modality,'MRI'))% general-purpose module
% (~isempty(strfind(currstream.sourcedomain,'session')) || ~strcmp(currstream.sourcemodality,'MRI')) % special
aap.schema.tasksettings.(stagename)(index).ATTRIBUTE.modality = currstream.sourcemodality;
aap.internal.aap_initial.schema.tasksettings.(stagename)(index).ATTRIBUTE.modality = currstream.sourcemodality;
aap.schema.tasksettings.(stagename)(index).ATTRIBUTE.domain = currstream.sourcedomain;
Expand Down
150 changes: 87 additions & 63 deletions aa_modules/aamod_CoSMoMVPA.m
Expand Up @@ -3,46 +3,48 @@

switch task
case 'report'
% localpath = aas_getpath_bydomain(aap,aap.tasklist.currenttask.domain,[subj,sess]);
%
% fdiag = dir(fullfile(localpath,'diagnostic_*.jpg'));
% if isempty(fdiag)
% streams=aas_getstreams(aap,'output');
% for streamind=1:length(streams)
% % obtain output
% outputfnames = aas_getfiles_bystream(aap,aap.tasklist.currenttask.domain,[subj sess],streams{streamind},'output');
%
% % perform diagnostics
% do_diag(outputfnames);
% end
% fdiag = dir(fullfile(localpath,'diagnostic_*.jpg'));
% end
%
% for d = 1:numel(fdiag)
% aap = aas_report_add(aap,subj,'<table><tr><td>');
% imgpath = fullfile(localpath,fdiag(d).name);
% aap=aas_report_addimage(aap,subj,imgpath);
% aap = aas_report_add(aap,subj,'</td></tr></table>');
% end
% localpath = aas_getpath_bydomain(aap,aap.tasklist.currenttask.domain,[subj,sess]);
%
% fdiag = dir(fullfile(localpath,'diagnostic_*.jpg'));
% if isempty(fdiag)
% streams=aas_getstreams(aap,'output');
% for streamind=1:length(streams)
% % obtain output
% outputfnames = aas_getfiles_bystream(aap,aap.tasklist.currenttask.domain,[subj sess],streams{streamind},'output');
%
% % perform diagnostics
% do_diag(outputfnames);
% end
% fdiag = dir(fullfile(localpath,'diagnostic_*.jpg'));
% end
%
% for d = 1:numel(fdiag)
% aap = aas_report_add(aap,subj,'<table><tr><td>');
% imgpath = fullfile(localpath,fdiag(d).name);
% aap=aas_report_addimage(aap,subj,imgpath);
% aap = aas_report_add(aap,subj,'</td></tr></table>');
% end
case 'doit'
%% Prepare data
TASKS = textscan(aas_getsetting(aap,'tasks'),'%s','Delimiter',':'); TASKS = TASKS{1}';

RSAROOT = fullfile(aas_getsubjpath(aap,subj),'RSA');
aas_makedir(aap,RSAROOT);

inps = aas_getstreams(aap,'input');
inps = inps(logical(cellfun(@(x) exist(aas_getinputstreamfilename(aap,'subject',subj,x),'file'), inps)));
struct_fn = aas_getfiles_bystream(aap,'subject',subj,inps{1});

fnMask = cellfun(@(x) aas_getfiles_bystream(aap,'subject',subj,x), inps(cell_index(inps,'firstlevel_brainmask')),'UniformOutput',false);
fnMask = cellfun(@(x) aas_getfiles_bystream(aap,'subject',subj,x), inps(cell_index(inps,'mask')),'UniformOutput',false);
fnSPM = cellfun(@(x) aas_getfiles_bystream(aap,'subject',subj,x), inps(cellfun(@(x) ~isempty(regexp(x,'firstlevel_spm$', 'once')), inps)),'UniformOutput',false);
fnTmaps = cellfun(@(x) aas_getfiles_bystream(aap,'subject',subj,x), inps(cell_index(inps,'firstlevel_spmts')),'UniformOutput',false);

if numel(fnSPM) > 1
if numel(fnMask) > 1
brain_mask = spm_imcalc(spm_vol(char(fnMask)),fullfile(RSAROOT,'brain_mask.nii'),'min(X)',{1});
else
brain_mask.fname = char(fnMask);
end

ITEMS = aas_getsetting(aap,'itemList');
fnT = {};
for run = 1:numel(fnSPM)
Expand All @@ -60,46 +62,68 @@
cosmo_set_path
cosmo_check_external('-tic');

% Data
ds=cosmo_fmri_dataset(fullfile(RSAROOT,'glm_T_stats_perrun.nii'),'mask',brain_mask.fname,...
'targets',repmat(1:numel(ITEMS),1,numel(fnSPM))');
ds=cosmo_fx(ds, @(x)mean(x,1), 'targets', 1);
ds.sa.labels=cellfun(@(x) x{1}, ITEMS, 'UniformOutput', false)';
ds.sa.set=(1:numel(ITEMS))';
cosmo_check_dataset(ds);

% Searchlight
nbrhood=cosmo_spherical_neighborhood(ds,'count',aas_getsetting(aap,'searchlightVox'));
if cell_index(TASKS, 'RSA') && isempty(aas_getsetting(aap,'bsMatrix')), TASKS(cell_index(TASKS, 'RSA')) = []; end

% Model
target_dsm=importdata(aas_getsetting(aap,'bsMatrix'));
measure=@cosmo_target_dsm_corr_measure;
measure_args=struct();
measure_args.target_dsm=target_dsm;

%% Info
aas_log(aap,false,'INFO:Dataset input:'); cosmo_disp(ds);
aas_log(aap,false,'INFO:Searchlight neighborhood definition:'); cosmo_disp(nbrhood);
aas_log(aap,false,'INFO:Target DSM:'); disp(target_dsm);

% imagesc(target_dsm)
% set(gca,'XTick',1:size(ds.samples,1),'XTickLabel',ds.sa.labels,...
% 'YTick',1:size(ds.samples,1),'YTickLabel',ds.sa.labels)

%% Run
ds_rsm_behav=cosmo_searchlight(ds,nbrhood,measure,measure_args);

% cosmo_plot_slices(ds_rsm_behav);

% store results
rsa_fn=fullfile(RSAROOT,'RSAmap.nii');
cosmo_map2fmri(ds_rsm_behav,rsa_fn);

%% Cleanup
path(oldPath);

aap=aas_desc_outputs(aap,'subject',subj,'RSAmap',rsa_fn);

for t = TASKS
% Data
switch t{1}
case 'RSA'
ds=cosmo_fmri_dataset(fullfile(RSAROOT,'glm_T_stats_perrun.nii'),'mask',brain_mask.fname,...
'targets',repmat(1:numel(ITEMS),1,numel(fnSPM))');
ds=cosmo_fx(ds, @(x)mean(x,1), 'targets', 1);
ds.sa.labels=cellfun(@(x) x{1}, ITEMS, 'UniformOutput', false)';
ds.sa.set=(1:numel(ITEMS))';
case 'C'
ds=cosmo_fmri_dataset(fullfile(RSAROOT,'glm_T_stats_perrun.nii'),'mask',brain_mask.fname,...
'targets',repmat(1:numel(ITEMS),1,numel(fnSPM))','chunks',floor(((1:numel(ITEMS)*numel(fnSPM))-1)/numel(ITEMS))+1);
ds.sa.labels=cellfun(@(x) x{1}, repmat(ITEMS,1,numel(fnSPM)), 'UniformOutput', false)';
ds.sa.set=repmat((1:numel(ITEMS))',numel(fnSPM),1);
end

% Data
cosmo_check_dataset(ds);

% Searchlight
nbrhood=cosmo_spherical_neighborhood(ds,'count',aas_getsetting(aap,'searchlightVox'));

% Model
target_dsm = 'not specified';
switch t{1}
case 'RSA'
target_dsm=importdata(aas_getsetting(aap,'bsMatrix'));
measure=@cosmo_target_dsm_corr_measure;
measure_args = aas_getsetting(aap,'RSAsettings');
measure_args.target_dsm=target_dsm;
case 'C'
measure_args = aas_getsetting(aap,'Csettings');
measure_args.classifier = str2func(['cosmo_classify_' lower(measure_args.classifier)]);
measure_args.partitions = cosmo_balance_partitions(cosmo_nfold_partitioner(ds), ds);
measure=@cosmo_crossvalidation_measure;
end

%% Info
aas_log(aap,false,'INFO:Dataset input:'); cosmo_disp(ds);
aas_log(aap,false,'INFO:Searchlight neighborhood definition:'); cosmo_disp(nbrhood);
aas_log(aap,false,'INFO:Target DSM:'); disp(target_dsm);

% imagesc(target_dsm)
% set(gca,'XTick',1:size(ds.samples,1),'XTickLabel',ds.sa.labels,...
% 'YTick',1:size(ds.samples,1),'YTickLabel',ds.sa.labels)

%% Run
ds_rsm_behav=cosmo_searchlight(ds,nbrhood,measure,measure_args);

% cosmo_plot_slices(ds_rsm_behav);

% store results
rsa_fn=fullfile(RSAROOT,[t{1} 'map.nii']);
cosmo_map2fmri(ds_rsm_behav,rsa_fn);

%% Cleanup
path(oldPath);

aap=aas_desc_outputs(aap,'subject',subj,[t{1} 'map'],rsa_fn);
end
case 'checkrequirements'

otherwise
Expand Down

0 comments on commit 1a68e1d

Please sign in to comment.