From 56fd087355f50a9d3daef5f53be96ac380872e9b Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sat, 30 Apr 2016 13:10:12 -0400 Subject: [PATCH 01/16] Minor doc fix. --- bricks/fmri_preprocess/niak_brick_slice_timing.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bricks/fmri_preprocess/niak_brick_slice_timing.m b/bricks/fmri_preprocess/niak_brick_slice_timing.m index 969b9bbb..69c81607 100644 --- a/bricks/fmri_preprocess/niak_brick_slice_timing.m +++ b/bricks/fmri_preprocess/niak_brick_slice_timing.m @@ -216,9 +216,9 @@ % _________________________________________________________________________ % Copyright (c) Pierre Bellec, Sebastien Lavoie-Courchesne % Montreal Neurological Institute, 2008-2010 -% Centre de recherche de l'institut de gériatrie de Montréal, +% Centre de recherche de l'institut de geriatrie de Montreal, % Department of Computer Science and Operations Research -% University of Montreal, Québec, Canada, 2010-2014 +% University of Montreal, Qubec, Canada, 2010-2014 % Maintainer : pierre.bellec@criugm.qc.ca % See licensing information in the code. % Keywords : medical imaging, slice timing, fMRI From 6f47181852b013d2735af5f04f12d2f8b61f2eef Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sat, 30 Apr 2016 13:10:31 -0400 Subject: [PATCH 02/16] Minor doc fix. --- commands/read_write/niak_mkdir.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/read_write/niak_mkdir.m b/commands/read_write/niak_mkdir.m index d588ffe4..44d76515 100644 --- a/commands/read_write/niak_mkdir.m +++ b/commands/read_write/niak_mkdir.m @@ -5,7 +5,7 @@ % existing directory. % % SYNTAX: -% [SUCCESS,MESSAGE,MESSAGEID] = MKDIR(PATH_NAME) +% [SUCCESS,MESSAGE,MESSAGEID] = NIAK_MKDIR(PATH_NAME) % % _________________________________________________________________________ % INPUTS: From 6c5cd3ededbb8ec1d78e20aa8c847b38bd32496b Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sat, 30 Apr 2016 13:11:26 -0400 Subject: [PATCH 03/16] Simplified doc. --- commands/read_write/niak_read_hdr_nifti.m | 55 +++++++++-------------- 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/commands/read_write/niak_read_hdr_nifti.m b/commands/read_write/niak_read_hdr_nifti.m index be77f962..65b7adb2 100644 --- a/commands/read_write/niak_read_hdr_nifti.m +++ b/commands/read_write/niak_read_hdr_nifti.m @@ -4,47 +4,32 @@ % affine transformation information will be included. % http://nifti.nimh.nih.gov/nifti-1 % -% SYNTAX: -% HDR = NIAK_READ_HDR_NIFTI(FILE_NAME) +% SYNTAX: HDR = NIAK_READ_HDR_NIFTI(FILE_NAME) % -% _________________________________________________________________________ -% INPUT: +% FILE_NAME (string) name of a single 3D+t nifti file or a 3D nifti file. +% Compressed files (.nii.gz) are not supported. +% HDR (structure) contain a description of the data. For a list of fields +% common to all data types, see NIAK_READ_VOL. +% HDR.DETAILS (structure) contains the standard fields of a nifti file. +% See http://nifti.nimh.nih.gov/nifti-1. % -% FILE_NAME -% (string) name of a single 3D+t minc file or a 3D minc file. +% EXAMPLE: +% hdr = niak_read_hdr_nifti('my_vol.nii'); % -% _________________________________________________________________________ -% OUTPUT: -% -% HDR -% (structure) contain a description of the data. For a list of fields -% common to all data types, see NIAK_READ_VOL. -% -% HDR.DETAILS -% (structure) contains the standard fields of a nifti file. -% See http://nifti.nimh.nih.gov/nifti-1. -% -% _________________________________________________________________________ % SEE ALSO: +% NIAK_READ_NIFTI, NIAK_WRITE_NIFTI, NIAK_READ_VOL, NIAK_WRITE_VOL % -% NIAK_READ_NIFTI, NIAK_WRITE_NIFTI, NIAK_READ_VOL, NIAK_WRITE_VOL -% -% _________________________________________________________________________ -% COMMENTS: -% -% Part of this file is copied and modified under GNU license from -% MRI_TOOLBOX developed by CNSP in Flinders University, Australia -% -% Important parts of this code are copied and modified from a matlab -% toolbox by Jimmy Shen (pls@rotman-baycrest.on.ca). Unfortunately, this -% toolbox did not include a copyright notice. -% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=8797&objectType=file -% -% Copyright (c) Pierre Bellec, Montreal Neurological Institute, 2008. -% Maintainer : pbellec@bic.mni.mcgill.ca +% See license in the code. + +% Copyright (c) Pierre Bellec, Jimmy Shen, 2008-2016. +% Montreal Neurological Institute, 2008-2010 +% Centre de recherche de l'institut de geriatrie de Montreal, +% Department of Computer Science and Operations Research +% University of Montreal, Qubec, Canada, 2010-2016 +% Maintainer: pierre.bellec@criugm.qc.ca % See licensing information in the code. -% Keywords : medical imaging, I/O, reader, nifti - +% Keywords: medical imaging, I/O, reader, nifti +% % Permission is hereby granted, free of charge, to any person obtaining a copy % of this software and associated documentation files (the "Software"), to deal % in the Software without restriction, including without limitation the rights From 3a223233202548d95c9d52f83c0277ee7a9ccda7 Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sat, 30 Apr 2016 13:11:45 -0400 Subject: [PATCH 04/16] Simplified code and doc for the nifti reader. WIP. --- commands/read_write/niak_read_nifti.m | 148 +++++--------------------- 1 file changed, 24 insertions(+), 124 deletions(-) diff --git a/commands/read_write/niak_read_nifti.m b/commands/read_write/niak_read_nifti.m index f72cb0a7..41c718a4 100644 --- a/commands/read_write/niak_read_nifti.m +++ b/commands/read_write/niak_read_nifti.m @@ -4,57 +4,37 @@ % affine transformation information will be included. % http://nifti.nimh.nih.gov/nifti-1 % -% SYNTAX: -% [HDR,VOL] = NIAK_READ_NIFTI(FILE_NAME) +% SYNTAX: [HDR,VOL] = NIAK_READ_NIFTI(FILE_NAME) % -% _________________________________________________________________________ -% INPUT: -% -% FILE_NAME -% (string) a 3D+t or 3D minc file. -% -% _________________________________________________________________________ -% OUTPUT: -% -% VOL -% (3D+t or 3D array of double) the fMRI or MRI data. -% -% HDR -% a structure containing a description of the data. See NIAK_READ_VOL +% FILE_NAME (string) a 3D+t or 3D minc nifti file. +% VOL (3D+t or 3D array of double) the fMRI or MRI data. +% HDR (structure) a description of the data. See NIAK_READ_VOL % and NIAK_READ_HDR_NIFTI for details. % -% _________________________________________________________________________ -% SEE ALSO: +% SEE ALSO: NIAK_READ_HDR_NIFTI, NIAK_WRITE_NIFTI, NIAK_READ_VOL, +% NIAK_WRITE_VOL, NIAK_READ_DATA_NIFI % -% NIAK_READ_HDR_NIFTI, NIAK_WRITE_NIFTI, NIAK_READ_VOL, NIAK_WRITE_VOL -% -% _________________________________________________________________________ % COMMENTS: +% In case of multiple files data (e.g. .IMG + .HDR, or .IMG + .HDR + .MAT), +% specify the name using the .IMG extension only. % -% In case of multiple files data (e.g. .IMG + .HDR, or .IMG + .HDR + .MAT), -% specify the name using the .IMG extension only. -% -% The affine transformation in hdr.info corresponds to the sform, if specified, or -% the qform (if sform unspecified and qform is), or simply the pixel dimension -% and offset, if neither qform or sform are specified. +% The affine transformation in hdr.info corresponds to the sform, if specified, or +% the qform (if sform unspecified and qform is), or simply the pixel dimension +% and offset, if neither qform or sform are specified. % -% Part of this file is copied and modified under GNU license from -% MRI_TOOLBOX developed by CNSP in Flinders University, Australia -% -% Important parts of this code are copied and modified from a matlab -% toolbox by Jimmy Shen (pls@rotman-baycrest.on.ca) who agreed for -% redistribution under the MIT license. +% The data is forced to single precision. % -% Note that the precision of the data is forced to single (32 bits). -% -% Copyright (c) Pierre Bellec, +% See license in the code. + +% Copyright (c) Pierre Bellec, Jimmy Shen, 2008-2016. % Montreal Neurological Institute, 2008-2010 -% Departement d'informatique et de recherche operationnelle -% Centre de recherche de l'institut de Geriatrie de Montreal -% Universite de Montreal, 2011-2015 +% Centre de recherche de l'institut de geriatrie de Montreal, +% Department of Computer Science and Operations Research +% University of Montreal, Qubec, Canada, 2010-2016 +% Maintainer: pierre.bellec@criugm.qc.ca % See licensing information in the code. -% Keywords : medical imaging, I/O, reader, nifti - +% Keywords: medical imaging, I/O, reader, nifti +% % Permission is hereby granted, free of charge, to any person obtaining a copy % of this software and associated documentation files (the "Software"), to deal % in the Software without restriction, including without limitation the rights @@ -73,16 +53,12 @@ % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN % THE SOFTWARE. -if nargin < 2 - precision_data = 'float'; -end - [path_f,name_f,ext_f] = fileparts(file_name); if isempty(path_f) path_f = '.'; end switch ext_f - case '.nii' + case {'.nii','.nii.gz'} file_header = file_name; case '.img' file_header = cat(2,path_f,filesep,name_f,'.hdr'); @@ -92,83 +68,7 @@ %% Parsing the header hdr = niak_read_hdr_nifti(file_header); - +hdr.info.file_parent = file_name; if nargout > 1 - %% Opening the data file - - fid = fopen(file_name,'r',hdr.info.machine); - - if fid < 0, - msg = sprintf('Cannot open file %s.',file_name); - error(msg); - end - - if hdr.details.dim(5) < 1 - hdr.details.dim(5) = 1; - end - - % move pointer to the start of image block - % - switch hdr.type - case {'vol', 'analyze'} - fseek(fid, 0, 'bof'); - case 'nii' - fseek(fid, hdr.details.vox_offset, 'bof'); - end - - - % For each frame, precision of value will be read - % in vol_siz times, where vol_siz is only the - % dimension size of an image, not the byte storage - % size of an image. - vol_siz = prod(hdr.details.dim(2:5)); - - %% For complex float32 or complex float64, voxel values - %% include [real, imag] - if (hdr.details.datatype == 32) || (hdr.details.datatype == 1792) - vol_siz = vol_siz * 2; - end - - %% MPH: For RGB24, voxel values include 3 separate color planes - if (hdr.details.datatype == 128) || (hdr.details.datatype == 511) - vol_siz = vol_siz * 3; - end - - vol = fread(fid, vol_siz, sprintf('*%s',hdr.info.precision)); - - %% For complex float32 or complex float64, voxel values - %% include [real, imag] - if (hdr.details.datatype == 32) || (hdr.details.datatype == 1792) - vol = reshape(vol, [2, length(vol)/2]); - vol = complex(vol(1,:)', vol(2,:)'); - end - - fclose(fid); - - %% Update the global min and max values - hdr.details.glmax = max(double(vol(:))); - hdr.details.glmin = min(double(vol(:))); - - %% Reshape the volume to correct dimensions - vol_idx = 1:hdr.details.dim(5); - - if (hdr.details.datatype == 128) && (hdr.details.bitpix == 24) - vol = squeeze(reshape(vol, [3 hdr.details.dim(2:4) length(vol_idx)])); - vol = permute(vol, [2 3 4 1 5]); - elseif (hdr.details.datatype == 511) && (hdr.details.bitpix == 96) - vol = single(vol); - vol = (vol - min(vol))/(max(vol) - min(vol)); - vol = squeeze(reshape(vol, [3 hdr.details.dim(2:4) length(vol_idx)])); - vol = permute(vol, [2 3 4 1 5]); - else - vol = squeeze(reshape(vol, [hdr.details.dim(2:4) length(vol_idx)])); - end - - if ((hdr.details.scl_slope~=0)&&(hdr.details.scl_slope~=1))||(hdr.details.scl_inter~=0) - vol = hdr.details.scl_slope * single(vol) + hdr.details.scl_inter; - hdr.details.scl_slope = 1; - hdr.details.scl_inter = 0; - end - vol = single(vol); - hdr.info.precision = 'float'; + vol = niak_read_data_nifti(hdr); end From 1844aeec01f50252b0b5cafb2e34e5ca7b6bf4ea Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sat, 30 Apr 2016 13:13:02 -0400 Subject: [PATCH 05/16] Simplified doc. Included some header changes that were previously done at read time. --- commands/read_write/niak_write_nifti.m | 52 ++++++++++++-------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/commands/read_write/niak_write_nifti.m b/commands/read_write/niak_write_nifti.m index 81fe9871..6ab5b1ec 100644 --- a/commands/read_write/niak_write_nifti.m +++ b/commands/read_write/niak_write_nifti.m @@ -4,41 +4,37 @@ % SYNTAX: [] = NIAK_WRITE_NIFTI(HDR,VOL) % % INPUTS: -% VOL (3D or 4D array) a 3D or 3D+t dataset -% HDR.FILE_NAME (string) the name of the file that will be written. -% HDR.TYPE (string, default 'nii') the output format (either 'nii' for -% NIFTI-1 one file data, 'img' for a couple '*.img'/'*.hdr' in -% NIFTI-1 format or 'analyze' for a '*.img'/'*.hdr'/'*.mat') -% HDR.INFO (structure) The subfields are optional, yet they give control +% VOL (3D or 4D array) a 3D or 3D+t dataset +% HDR.FILE_NAME (string) the name of the file that will be written. +% HDR.TYPE (string, default 'nii') the output format (either 'nii' for +% NIFTI-1 one file data, 'img' for a couple '*.img'/'*.hdr' in +% NIFTI-1 format or 'analyze' for a '*.img'/'*.hdr'/'*.mat') +% HDR.INFO (structure) The subfields are optional, yet they give control % on critical space information. See NIAK_WRITE_VOL for more info. -% HDR.DETAILS (structure) the fields are the standard list of a NIFTI header. +% HDR.DETAILS (structure) the fields are the standard list of a NIFTI header. % % OUTPUTS: -% The data called VOL is stored into a file called FILENAME written in -% nifti format. In the case of ANALYZE 7.5 file format, a file '.MAT' will -% also be created with the affine transform. +% The data called VOL is stored into a file called FILENAME written in +% nifti format. In the case of ANALYZE 7.5 file format, a file '.MAT' will +% also be created with the affine transform. % +% NOTE: the output file name can also have a '.gz' extension, in which case +% The output file will be automatically compressed. +% % SEE ALSO: -% NIAK_READ_HDR_NIFTI, NIAK_READ_NIFTI, NIAK_WRITE_VOL, NIAK_READ_VOL +% NIAK_READ_HDR_NIFTI, NIAK_READ_NIFTI, NIAK_READ_DATA_NIFTI, +% NIAK_WRITE_VOL, NIAK_READ_VOL % -% COMMENTS: % See licensing information in the code. -% Maintainer : pierre.bellec@criugm.qc.ca -% Keywords : medical imaging, I/O, writer, nifti -% Part of this file is copied and modified under GNU license from -% MRI_TOOLBOX developed by CNSP in Flinders University, Australia -% -% Important parts of this code are copied and modified from a matlab -% toolbox by Jimmy Shen (pls@rotman-baycrest.on.ca), and reshared here -% with agreement under a MIT license. -% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId= -% 8797&objectType=file -% -% Copyright (c) Pierre Bellec, Montreal Neurological Institute, 2008. -% Centre de recherche de l'institut de griatrie de Montral, +% Copyright (c) Pierre Bellec, Jimmy Shen, 2008-2016. +% Montreal Neurological Institute, 2008-2010 +% Centre de recherche de l'institut de geriatrie de Montreal, % Department of Computer Science and Operations Research -% University of Montreal, Qubec, Canada, 2010-2014 +% University of Montreal, Qubec, Canada, 2010-2016 +% Maintainer: pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords: medical imaging, I/O, reader, nifti % % Permission is hereby granted, free of charge, to any person obtaining a copy % of this software and associated documentation files (the "Software"), to deal @@ -101,8 +97,8 @@ %% Updating information of the header hdr.info.precision = class(vol); -precision = hdr.info.precision; - +hdr.details.scl_slope = 1; +hdr.details.scl_inter = 0; hdr.info.dimensions = size(vol); if ~isfield(hdr.details,'dim') From eb66e98aad8ae25ca867b73b9f59546ede29cf92 Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sat, 30 Apr 2016 13:13:45 -0400 Subject: [PATCH 06/16] A new function to unzip files into temp files. --- commands/read_write/niak_unzip.m | 62 ++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 commands/read_write/niak_unzip.m diff --git a/commands/read_write/niak_unzip.m b/commands/read_write/niak_unzip.m new file mode 100644 index 00000000..8a8cb84a --- /dev/null +++ b/commands/read_write/niak_unzip.m @@ -0,0 +1,62 @@ +function [file_tmp,flag_zip] = niak_unzip(file_name) +% Unzip a zipped file +% +% SYNTAX: [FILE_TMP,FLAG_ZIP] = NIAK_UNZIP(PATH_NAME) +% +% FILE_NAME (string) the name to a zipped file. +% FILE_TMP (string) the name of the unzipped file. +% FLAG_ZIP (boolean) true if the file was indeed zipped. +% +% If the file is zipped, a temporary unzipped file is created. +% If the file is not zipped, FILE_TMP is identical to FILE_NAME. +% +% See license in the code. + +% Copyright (c) Pierre Bellec, 2016. +% Centre de recherche de l'institut de geriatrie de Montreal, +% Department of Computer Science and Operations Research +% University of Montreal, Qubec, Canada, 2016 +% Maintainer: pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +[path_f,name_f,type] = fileparts(file_name); + +flag_zip = strcmp(type,'.gz'); +if flag_zip + %% The file is zipped... Unzip it first and start reading + [path_f_tmp,name_f,type] = fileparts(name_f); + file_tmp = niak_file_tmp([name_f type '.gz']); + + [succ,msg] = system(cat(2,'cp "',file_name,'" ',file_tmp)); + if succ~=0 + error(msg) + end + + instr_unzip = cat(2,'gunzip -f "',file_tmp,'"'); + + [succ,msg] = system(instr_unzip); + if succ ~= 0 + error(cat(2,'niak:read: ',msg,'. There was a problem unzipping the file. Please check that the command ''',gb_niak_unzip,''' works, or change this command using the variable GB_NIAK_UNZIP in the file NIAK_GB_VARS')); + end + file_tmp = file_tmp(1:end-length('.gz')) +else + file_tmp = file_name; +end \ No newline at end of file From 1a395dc8e650c6e0c2a9c4220f70daee5de9de79 Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sat, 30 Apr 2016 13:14:12 -0400 Subject: [PATCH 07/16] A new function to read data from a nifti file, given a header structure. --- commands/read_write/niak_read_data_nifti.m | 127 +++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 commands/read_write/niak_read_data_nifti.m diff --git a/commands/read_write/niak_read_data_nifti.m b/commands/read_write/niak_read_data_nifti.m new file mode 100644 index 00000000..6db30eb7 --- /dev/null +++ b/commands/read_write/niak_read_data_nifti.m @@ -0,0 +1,127 @@ +function vol = niak_read_data_nifti(hdr) +% Read data from a NIFTI file (.NII or .IMG). +% +% SYNTAX: VOL = NIAK_READ_DATA_NIFTI(HDR) +% +% HDR (structure) a description of the data, generated from NIAK_READ_VOL. +% VOL (array) volume data from the nifti file used to read HDR. +% +% SEE ALSO: NIAK_READ_HDR_NIFTI, NIAK_WRITE_NIFTI, NIAK_READ_VOL, +% NIAK_WRITE_VOL, NIAK_READ_NIFI +% +% COMMENTS: the data is forced to single precision. +% +% EXAMPLE: +% % start by reading the header of an existing file +% hdr = niak_read_vol('my_file.nii.gz'); +% % latter read the actual volume data +% vol = niak_read_data_nifti(hdr); +% +% See license in the code. + +% Copyright (c) Pierre Bellec, 2008-2016. +% Montreal Neurological Institute, 2008-2010 +% Centre de recherche de l'institut de geriatrie de Montreal, +% Department of Computer Science and Operations Research +% University of Montreal, Qubec, Canada, 2010-2016 +% Maintainer: pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords: medical imaging, I/O, reader, nifti +% +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +%% Unzip file if necessary +file_name = hdr.info.file_parent; +[file_tmp,flag_zip] = niak_unzip(file_name); + +%% Opening the data file +fid = fopen(file_tmp,'r',hdr.info.machine); + +if fid < 0, + msg = sprintf('Cannot open file %s.',file_name); + error(msg); +end + +if hdr.details.dim(5) < 1 + hdr.details.dim(5) = 1; +end + +% move pointer to the start of image block +% +switch hdr.type + case {'vol', 'analyze'} + fseek(fid, 0, 'bof'); + case 'nii' + fseek(fid, hdr.details.vox_offset, 'bof'); +end + + +% For each frame, precision of value will be read +% in vol_siz times, where vol_siz is only the +% dimension size of an image, not the byte storage +% size of an image. +vol_siz = prod(hdr.details.dim(2:5)); + +%% For complex float32 or complex float64, voxel values +%% include [real, imag] +if (hdr.details.datatype == 32) || (hdr.details.datatype == 1792) + vol_siz = vol_siz * 2; +end + +%% MPH: For RGB24, voxel values include 3 separate color planes +if (hdr.details.datatype == 128) || (hdr.details.datatype == 511) + vol_siz = vol_siz * 3; +end + +vol = fread(fid, vol_siz, sprintf('*%s',hdr.info.precision)); + +%% For complex float32 or complex float64, voxel values +%% include [real, imag] +if (hdr.details.datatype == 32) || (hdr.details.datatype == 1792) + vol = reshape(vol, [2, length(vol)/2]); + vol = complex(vol(1,:)', vol(2,:)'); +end +fclose(fid); + +%% Reshape the volume to correct dimensions +vol_idx = 1:hdr.details.dim(5); + +if (hdr.details.datatype == 128) && (hdr.details.bitpix == 24) + vol = squeeze(reshape(vol, [3 hdr.details.dim(2:4) length(vol_idx)])); + vol = permute(vol, [2 3 4 1 5]); +elseif (hdr.details.datatype == 511) && (hdr.details.bitpix == 96) + vol = single(vol); + vol = (vol - min(vol))/(max(vol) - min(vol)); + vol = squeeze(reshape(vol, [3 hdr.details.dim(2:4) length(vol_idx)])); + vol = permute(vol, [2 3 4 1 5]); +else + vol = squeeze(reshape(vol, [hdr.details.dim(2:4) length(vol_idx)])); +end + +if ((hdr.details.scl_slope~=0)&&(hdr.details.scl_slope~=1))||(hdr.details.scl_inter~=0) + vol = hdr.details.scl_slope * single(vol) + hdr.details.scl_inter; +end + +%% Hack: force to single precision +vol = single(vol); + +%% remove temporary file +if flag_zip + delete(file_tmp); +end \ No newline at end of file From c08dcd81fda00303959d0dce1f7c5e68858a0c09 Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sun, 1 May 2016 11:10:19 -0400 Subject: [PATCH 08/16] A new command to read the data array in a minc file, provided the header has already been parsed. --- commands/read_write/niak_read_data_minc.m | 84 +++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 commands/read_write/niak_read_data_minc.m diff --git a/commands/read_write/niak_read_data_minc.m b/commands/read_write/niak_read_data_minc.m new file mode 100644 index 00000000..dae22b61 --- /dev/null +++ b/commands/read_write/niak_read_data_minc.m @@ -0,0 +1,84 @@ +function vol = niak_read_data_minc(hdr,precision_data) +% Read data from a MINC file (.MNC or .MNC.GZ). +% +% SYNTAX: VOL = NIAK_READ_DATA_MINC(HDR) +% +% HDR (structure) a description of the data, generated from NIAK_READ_VOL. +% VOL (array) volume data from the minc file used to read HDR. +% +% SEE ALSO: NIAK_READ_HDR_MINC, NIAK_WRITE_MINC, NIAK_READ_VOL, +% NIAK_WRITE_VOL, NIAK_READ_MINC +% +% COMMENTS: the data is forced to float precision. +% +% EXAMPLE: +% % start by reading the header of an existing file +% hdr = niak_read_vol('my_file.mnc'); +% % latter read the actual volume data +% vol = niak_read_data_minc(hdr); +% +% See license in the code. + +% Copyright (c) Pierre Bellec, 2008-2016. +% Montreal Neurological Institute, 2008-2010 +% Centre de recherche de l'institut de geriatrie de Montreal, +% Department of Computer Science and Operations Research +% University of Montreal, Qubec, Canada, 2010-2016 +% Maintainer: pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords: medical imaging, I/O, reader, nifti +% +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +if nargin < 2 + precision_data = 'float'; +end + +%% Unzip file if necessary +file_name = hdr.info.file_parent; +[file_tmp_mnc,flag_zip] = niak_unzip(file_name); +[path_tmp,name_tmp,ext_tmp] = fileparts(file_tmp_mnc); + +%% Generating a name for a temporary file +file_tmp = niak_file_tmp([name_tmp '.dat']); + +%% extracting the data in float precision in the temporary file +[flag,str_info] = system(cat(2,'minctoraw -',precision_data,' -normalize ',file_name,' > ',file_tmp)); + +if flag>0 + error(sprintf('niak:read : %s',str_info)) +end + +%% reading information +hf = fopen(file_tmp,'r'); +try + vol = fread(hf,prod(hdr.info.dimensions),['*' precision_data]); +catch + vol = fread(hf,prod(hdr.info.dimensions),precision_data); +end + +%% Removing temporary stuff +fclose(hf); +if flag_zip + delete(file_tmp_mnc); +end +delete(file_tmp); + +%% Shapping vol as 3D+t array +vol = reshape(vol,hdr.info.dimensions); \ No newline at end of file From f503fee774f121165b9d44017f714ce7211dd575 Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sun, 1 May 2016 11:14:27 -0400 Subject: [PATCH 09/16] More doc. --- commands/read_write/niak_read_data_minc.m | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/commands/read_write/niak_read_data_minc.m b/commands/read_write/niak_read_data_minc.m index dae22b61..12536edf 100644 --- a/commands/read_write/niak_read_data_minc.m +++ b/commands/read_write/niak_read_data_minc.m @@ -4,6 +4,8 @@ % SYNTAX: VOL = NIAK_READ_DATA_MINC(HDR) % % HDR (structure) a description of the data, generated from NIAK_READ_VOL. +% The name of the file that will be used to extract data is stored in +% HDR.INFO.FILE_PARENT. % VOL (array) volume data from the minc file used to read HDR. % % SEE ALSO: NIAK_READ_HDR_MINC, NIAK_WRITE_MINC, NIAK_READ_VOL, @@ -16,6 +18,9 @@ % hdr = niak_read_vol('my_file.mnc'); % % latter read the actual volume data % vol = niak_read_data_minc(hdr); +% % read a different file, with identical header structure +% hdr.info.file_parent = 'my_file2.mnc'; +% vol2 = niak_read_data_minc(hdr); % % See license in the code. From e9333244f481780cb4303559fd44fd20b68b430b Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sun, 1 May 2016 11:14:43 -0400 Subject: [PATCH 10/16] More doc. --- commands/read_write/niak_read_data_nifti.m | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/commands/read_write/niak_read_data_nifti.m b/commands/read_write/niak_read_data_nifti.m index 6db30eb7..a7de348b 100644 --- a/commands/read_write/niak_read_data_nifti.m +++ b/commands/read_write/niak_read_data_nifti.m @@ -4,6 +4,8 @@ % SYNTAX: VOL = NIAK_READ_DATA_NIFTI(HDR) % % HDR (structure) a description of the data, generated from NIAK_READ_VOL. +% The name of the file that will be used to extract data is stored in +% HDR.INFO.FILE_PARENT. % VOL (array) volume data from the nifti file used to read HDR. % % SEE ALSO: NIAK_READ_HDR_NIFTI, NIAK_WRITE_NIFTI, NIAK_READ_VOL, @@ -16,6 +18,9 @@ % hdr = niak_read_vol('my_file.nii.gz'); % % latter read the actual volume data % vol = niak_read_data_nifti(hdr); +% % read a different file, with identical header structure +% hdr.info.file_parent = 'my_file2.nii.gz'; +% vol2 = niak_read_data_nifti(hdr); % % See license in the code. From 491a7921c2b38243a55559e3a1aa4463dbf9fddb Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sun, 1 May 2016 11:15:13 -0400 Subject: [PATCH 11/16] 1. Improved doc. 2. NIAK_READ_MINC now depends on NIAK_READ_DATA_MINC. --- commands/read_write/niak_read_minc.m | 99 +++++++++++----------------- 1 file changed, 37 insertions(+), 62 deletions(-) diff --git a/commands/read_write/niak_read_minc.m b/commands/read_write/niak_read_minc.m index 8b06ef43..bfc86239 100644 --- a/commands/read_write/niak_read_minc.m +++ b/commands/read_write/niak_read_minc.m @@ -2,44 +2,36 @@ % Read 3D or 3D+t data in MINC format. % http://www.bic.mni.mcgill.ca/software/minc/ % -% SYNTAX: -% [HDR,VOL] = NIAK_READ_MINC(FILE_NAME) -% -% _________________________________________________________________________ -% INPUT: +% SYNTAX: [HDR,VOL] = NIAK_READ_MINC(FILE_NAME) % -% FILE_NAME -% (string) a 3D+t or 3D minc file. +% FILE_NAME (string) a 3D+t or 3D minc file. +% VOL (3D+t or 3D array of double) the fMRI or MRI data. +% HDR (structure) description of the data. See NIAK_READ_VOL +% and NIAK_READ_HDR_MINC for details. % -% _________________________________________________________________________ -% OUTPUT: +% SEE ALSO: NIAK_READ_HDR_MINC, NIAK_WRITE_MINC, NIAK_READ_VOL, NIAK_WRITE_VOL % -% VOL -% (3D+t or 3D array of double) the fMRI or MRI data. +% COMMENTS: Use shell commands MINCINFO (for minc1), MINCHEADER and MINCTORAW which +% requires a proper install of minc tools. This function is +% creating temporary files. If it does not work, try to change the location +% of temporary files using the GB_NIAK_TMP variable defined in +% the NIAK_GB_VARS function. % -% HDR -% a structure containing a description of the data. See NIAK_READ_VOL -% and NIAK_READ_HDR_MINC for details. +% EXAMPLES: +% To read the header of a file, with the volumetric data +% [hdr,vol] = niak_read_minc('my_file.mnc'); % -% _________________________________________________________________________ -% SEE ALSO: -% -% NIAK_READ_HDR_MINC, NIAK_WRITE_MINC, NIAK_READ_VOL, NIAK_WRITE_VOL -% -% _________________________________________________________________________ -% COMMENTS: -% -% Use shell commands MINCINFO (for minc1), MINCHEADER and MINCTORAW which -% requires a proper install of minc tools. This function is -% creating temporary files. If it does not work, try to change the location -% of temporary files using the GB_NIAK_TMP variable defined in -% the NIAK_GB_VARS function. -% -% Copyright (c) Pierre Bellec, Montreal Neurological Institute, 2008. -% Maintainer : pbellec@bic.mni.mcgill.ca +% See license in the code. + +% Copyright (c) Pierre Bellec, 2008-2016. +% Montreal Neurological Institute, 2008-2010 +% Centre de recherche de l'institut de geriatrie de Montreal, +% Department of Computer Science and Operations Research +% University of Montreal, Qubec, Canada, 2010-2016 +% Maintainer: pierre.bellec@criugm.qc.ca % See licensing information in the code. -% Keywords : medical imaging, I/O, reader, minc - +% Keywords: medical imaging, I/O, reader, minc +% % Permission is hereby granted, free of charge, to any person obtaining a copy % of this software and associated documentation files (the "Software"), to deal % in the Software without restriction, including without limitation the rights @@ -62,35 +54,18 @@ precision_data = 'float'; end -%% Parsing the header -hdr = niak_read_hdr_minc(file_name); -hdr.info.precision = precision_data; - -[path_tmp,name_tmp,ext_tmp] = fileparts(file_name); - -if nargout == 2 - %% Generating a name for a temporary file - file_tmp = niak_file_tmp([name_tmp '.dat']); - - %% extracting the data in float precision in the temporary file - [flag,str_info] = system(cat(2,'minctoraw -',precision_data,' -normalize ',file_name,' > ',file_tmp)); - - if flag>0 - error(sprintf('niak:read : %s',str_info)) - end - - %% reading information - hf = fopen(file_tmp,'r'); - try - vol = fread(hf,prod(hdr.info.dimensions),['*' precision_data]); - catch - vol = fread(hf,prod(hdr.info.dimensions),precision_data); - end +%% Unzip +[path_f,name_f,ext_f] = fileparts(file_name); +path_f = niak_full_path(path_f); +file_name = [path_f,name_f,ext_f]; +[file_tmp,flag_zip] = niak_unzip(file_name); - %% Removing temporary stuff - fclose(hf); - delete(file_tmp); - - %% Shapping vol as 3D+t array - vol = reshape(vol,hdr.info.dimensions); +%% Parsing the header +hdr = niak_read_hdr_minc(file_tmp); +hdr.info.file_parent = file_name; +if nargout > 1 + vol = niak_read_data_minc(hdr); +end +if flag_zip + delete(file_tmp); end \ No newline at end of file From 388c78d36ea2dc43e332c015458efffcbca635d1 Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sun, 1 May 2016 11:16:59 -0400 Subject: [PATCH 12/16] Remove temporary file. --- commands/read_write/niak_read_nifti.m | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/commands/read_write/niak_read_nifti.m b/commands/read_write/niak_read_nifti.m index 41c718a4..9aefb928 100644 --- a/commands/read_write/niak_read_nifti.m +++ b/commands/read_write/niak_read_nifti.m @@ -52,23 +52,15 @@ % LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN % THE SOFTWARE. - [path_f,name_f,ext_f] = fileparts(file_name); -if isempty(path_f) - path_f = '.'; -end -switch ext_f - case {'.nii','.nii.gz'} - file_header = file_name; - case '.img' - file_header = cat(2,path_f,filesep,name_f,'.hdr'); - otherwise - error('niak:read: Unkown extension type : %s. I am expecting ''.nii'' or ''.img''',ext_f) -end - -%% Parsing the header -hdr = niak_read_hdr_nifti(file_header); +path_f = niak_full_path(path_f); +file_name = [path_f,name_f,ext_f]; +[file_tmp,flag_zip] = niak_unzip(file_name); +hdr = niak_read_hdr_nifti(file_tmp); hdr.info.file_parent = file_name; if nargout > 1 - vol = niak_read_data_nifti(hdr); + vol = niak_read_data_nifti(hdr); +end +if flag_zip + delete(file_tmp); end From e23619956a53899be2c923e3973aea60399201aa Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sun, 1 May 2016 11:17:22 -0400 Subject: [PATCH 13/16] 1. Improved doc. 2. NIAK_READ_VOL now does not handle .gz itself (it is done at the level of NIAK_READ_MINC and NIAK_READ_NIFTI). Code is cleaner that way. --- commands/read_write/niak_read_vol.m | 114 +++++++++++----------------- 1 file changed, 45 insertions(+), 69 deletions(-) diff --git a/commands/read_write/niak_read_vol.m b/commands/read_write/niak_read_vol.m index 5be9b02f..beec092c 100644 --- a/commands/read_write/niak_read_vol.m +++ b/commands/read_write/niak_read_vol.m @@ -14,10 +14,10 @@ % Supported formats are either NIFIT (*.nii,*.img/hdr), ANALYZE % (.img/.hdr/.mat) or MINC1/MINC2 (.mnc). Extra blanks are ignored. % File separator can be / or \ on Windows. Gzipped files (with an -% additional .gz) are supported. Frames must be equally spaced in -% time. For single file names, wild cards are supported (mutliple -% files are treated in the same way as a matrix of image files -% names). +% additional .gz) are supported using the gunzip system commad. +% Frames must be equally spaced in time. For single file names, +% wild cards are supported (mutliple files are treated in the same way +% as a matrix of image files names). % % _________________________________________________________________________ % OUTPUTS : @@ -86,49 +86,56 @@ % SEE ALSO : % NIAK_READ_HDR_MINC, NIAK_WRITE_MINC, NIAK_WRITE_VOL, NIAK_READ_HDR_NIFTI, % NIAK_READ_NIFTI, NIAK_HDR_MAT2MINC, NIAK_HDR_MINC2MAT, -% NIAK_COORD_VOX2WORLD, NIAK_COORD_WORLD2VOX. +% NIAK_COORD_VOX2WORLD, NIAK_COORD_WORLD2VOX, NIAK_READ_DATA_NIFTI, +% NIAK_READ_DATA_MINC % % _________________________________________________________________________ % COMMENTS % % NOTE 1: -% If multiple files are specified, make sure all those files are in the -% same space and are simple 3D volumes. -% All data will be concatenated along the 4th dimension in the VOL array, -% i.e. VOL(:,:,:,i) is the data of the ith file. -% The HDR structure have multiple entries, each one corresponding to one -% file. +% If multiple files are specified, make sure all those files are in the +% same space and are simple 3D volumes. +% All data will be concatenated along the 4th dimension in the VOL array, +% i.e. VOL(:,:,:,i) is the data of the ith file. +% The HDR structure have multiple entries, each one corresponding to one +% file. % % NOTE 2: -% In order to read MINC files, a proper installation of minc tools is -% required (see http://www.bic.mni.mcgill.ca/software/minc/). +% In order to read MINC files, a proper installation of minc tools is +% required (see http://www.bic.mni.mcgill.ca/software/minc/). % % NOTE 3: -% The extension of zipped file is assumed to be .gz. The tools used to -% unzip files in 'gunzip'. This setting can be changed by changing the -% variables GB_NIAK_ZIP_EXT and GB_NIAK_UNZIP in the file NIAK_GB_VARS. +% The extension of zipped file is assumed to be .gz. The tools used to +% unzip files is 'gunzip'. % % NOTE 4: -% "voxel coordinates" start from 0. This is not the default matlab -% behaviour, that indexes array starting from 1. To convert coordinates -% from (matlab) voxel system to world system see NIAK_COORD_WORLD2VOX and -% NIAK_COORD_VOX2WORLD. +% "voxel coordinates" start from 0. This is not the default matlab +% behaviour, that indexes array starting from 1. To convert coordinates +% from (matlab) voxel system to world system see NIAK_COORD_WORLD2VOX and +% NIAK_COORD_VOX2WORLD. % % NOTE 5: -% If the name of the file is of the form toto.ext, the reader will look for -% a file toto_extra.mat. If such a file exist, it is assumed to contain matlab -% variables and is loaded in HDR.EXTRA. This generic mechanism makes it easy to -% attach custom information to a file which may not be easy to achieve in a -% consistent way for all data formats. +% If the name of the file is of the form toto.ext, the reader will look for +% a file toto_extra.mat. If such a file exist, it is assumed to contain matlab +% variables and is loaded in HDR.EXTRA. This generic mechanism makes it easy to +% attach custom information to a file. % -% Copyright (c) Pierre Bellec, Montreal Neurological Institute, 2008. -% Centre de recherche de l'institut de Griatrie de Montral, -% Dpartement d'informatique et de recherche oprationnelle, -% Universit de Montral, 2010-2011. -% Maintainer : pierre.bellec@criugm.qc.ca +% _________________________________________________________________________ +% EXAMPLES +% To read the header and data array from a file: +% [hdr,vol] = niak_read_vol('my_file.nii.gz'); +% +% See license in the code. + +% Copyright (c) Pierre Bellec, 2008-2016. +% Montreal Neurological Institute, 2008-2010 +% Centre de recherche de l'institut de geriatrie de Montreal, +% Department of Computer Science and Operations Research +% University of Montreal, Qubec, Canada, 2010-2016 +% Maintainer: pierre.bellec@criugm.qc.ca % See licensing information in the code. -% Keywords : medical imaging, I/O, reader, minc - +% Keywords: medical imaging, I/O, reader, minc, nifti +% % Permission is hereby granted, free of charge, to any person obtaining a copy % of this software and associated documentation files (the "Software"), to deal % in the Software without restriction, including without limitation the rights @@ -147,10 +154,11 @@ % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN % THE SOFTWARE. +flag_gb_niak_fast_gb = true; niak_gb_vars if ~ischar(file_name) - error('niak_read_vol: FILE_NAME should be a string or a matrix of strings') + error('niak_read_vol: FILE_NAME should be a structure, a string or a matrix of strings') end nb_file = size(file_name,1); @@ -213,42 +221,10 @@ %% The file exists file_name = niak_full_file(file_name); - [path_f,name_f,type] = fileparts(file_name); + [path_f,name_f,type] = niak_fileparts(file_name); switch type - - case gb_niak_zip_ext - - %% The file is zipped... Unzip it first and restart reading - - [path_f_tmp,name_f,type] = fileparts(name_f); - file_extra = [path_f filesep name_f '_extra.mat']; - file_tmp_gz = niak_file_tmp([name_f type gb_niak_zip_ext]); - - [succ,msg] = system(cat(2,'cp "',file_name,'" ',file_tmp_gz)); - if succ~=0 - error(msg) - end - - instr_unzip = cat(2,gb_niak_unzip,' "',file_tmp_gz,'"'); - - [succ,msg] = system(instr_unzip); - if succ ~= 0 - error(cat(2,'niak:read: ',msg,'. There was a problem unzipping the file. Please check that the command ''',gb_niak_unzip,''' works, or change this command using the variable GB_NIAK_UNZIP in the file NIAK_GB_VARS')); - end - - if nargout == 2 - [hdr,vol] = niak_read_vol(file_tmp_gz(1:end-length(gb_niak_zip_ext))); - else - hdr = niak_read_vol(file_tmp_gz(1:end-length(gb_niak_zip_ext))); - end - - delete(file_tmp_gz(1:end-length(gb_niak_zip_ext))); - hdr.info.file_parent = file_name; - if psom_exist(file_extra) - hdr.extra = load(file_extra); - end - case {'.mnc'} + case {'.mnc','.mnc.gz'} %% This is either a minc1 or minc2 file if nargout == 2 @@ -261,7 +237,7 @@ hdr.extra = load(file_extra); end - case {'.nii','.img'} + case {'.nii','.nii.gz','.img'} %% This is a nifti file (either one file .nii, or two files %% .img/hdr @@ -278,7 +254,7 @@ otherwise %% Unsupported extension - error('niak:read: Unknown file extension %s. Only .mnc, .nii and .img are supported.',type) + error('niak:read: Unknown file extension %s. Only .mnc(.gz), .nii(.gz) and .img are supported.',type) end end From 82e4992ca8e279a03104abc375656d9d403d29a1 Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Sun, 1 May 2016 11:18:25 -0400 Subject: [PATCH 14/16] Better error message. --- commands/read_write/niak_unzip.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/commands/read_write/niak_unzip.m b/commands/read_write/niak_unzip.m index 8a8cb84a..29b61fb6 100644 --- a/commands/read_write/niak_unzip.m +++ b/commands/read_write/niak_unzip.m @@ -1,7 +1,7 @@ function [file_tmp,flag_zip] = niak_unzip(file_name) % Unzip a zipped file % -% SYNTAX: [FILE_TMP,FLAG_ZIP] = NIAK_UNZIP(PATH_NAME) +% SYNTAX: [FILE_TMP,FLAG_ZIP] = NIAK_UNZIP(FILE_NAME) % % FILE_NAME (string) the name to a zipped file. % FILE_TMP (string) the name of the unzipped file. @@ -54,9 +54,9 @@ [succ,msg] = system(instr_unzip); if succ ~= 0 - error(cat(2,'niak:read: ',msg,'. There was a problem unzipping the file. Please check that the command ''',gb_niak_unzip,''' works, or change this command using the variable GB_NIAK_UNZIP in the file NIAK_GB_VARS')); + error(cat(2,'There was a problem unzipping the file using GUNZIP: ',msg)); end - file_tmp = file_tmp(1:end-length('.gz')) + file_tmp = file_tmp(1:end-length('.gz')); else file_tmp = file_name; end \ No newline at end of file From 3053530df92d1cd4844fddef2b574580399c9f9b Mon Sep 17 00:00:00 2001 From: Pierre Bellec Date: Wed, 3 Aug 2016 12:21:38 -0400 Subject: [PATCH 15/16] Merged master into an old PR. --- Dockerfile | 4 +- bricks/connectome/niak_brick_connectome.m | 15 +- bricks/connectome/niak_brick_rmap.m | 3 + bricks/fir/niak_brick_fir_tseries.m | 9 + .../niak_brick_build_confounds.m | 361 ++++++++ .../niak_brick_qc_coregister.m | 2 +- .../niak_brick_regress_confounds.m | 585 +++--------- .../niak_brick_resample_simple.m | 123 +++ .../fmri_preprocess/niak_brick_resample_vol.m | 47 +- .../fmri_preprocess/niak_brick_slice_timing.m | 4 + bricks/misc/niak_brick_copy.m | 23 +- bricks/misc/niak_brick_mnc2nii.m | 11 +- bricks/misc/niak_brick_montage.m | 201 ++++ bricks/misc/niak_brick_vol2img.m | 187 ++++ bricks/region_growing/niak_brick_merge_part.m | 10 +- bricks/region_growing/niak_brick_tseries.m | 29 +- .../t1_processing/niak_brick_anat2stereolin.m | 43 +- .../t1_processing/niak_brick_anat2stereonl.m | 57 +- bricks/t1_processing/niak_brick_classify.m | 33 +- bricks/t1_processing/niak_brick_inormalize.m | 41 +- bricks/t1_processing/niak_brick_nu_correct.m | 41 +- .../t1_processing/niak_brick_t1_preprocess.m | 6 +- commands/SI_processing/niak_fd2mask.m | 76 ++ commands/fir/niak_brick_fir.m | 9 + commands/fmristat/niak_model_select.m | 11 +- commands/fmristat/niak_normalize_model.m | 2 + commands/formats/niak_transf2param.m | 43 +- commands/misc/niak_gb_vars.m | 5 +- commands/misc/niak_wget.m | 10 +- commands/read_write/niak_cp_fmri.m | 31 +- .../read_write/niak_grab_all_preprocess.m | 104 ++- .../read_write/niak_grab_fmri_preprocess.m | 30 +- .../read_write/niak_grab_report_preprocess.m | 91 ++ commands/read_write/niak_read_csv_cell.m | 57 +- commands/read_write/niak_write_csv_cell.m | 43 +- commands/read_write/niak_write_vol.m | 61 +- commands/visualization/niak_resample_vol.m | 91 ++ commands/visualization/niak_vol2img.m | 173 ++++ demos/pipeline/niak_demo_fmri_preprocess.m | 12 +- demos/pipeline/niak_demo_region_growing.m | 8 +- .../niak_brick_connectome_multiscale.m | 21 +- extensions/jsonlab-1.2/AUTHORS.txt | 53 ++ extensions/jsonlab-1.2/ChangeLog.txt | 96 ++ extensions/jsonlab-1.2/LICENSE_BSD.txt | 25 + extensions/jsonlab-1.2/README.txt | 411 +++++++++ .../jsonlab-1.2/examples/demo_jsonlab_basic.m | 181 ++++ .../jsonlab-1.2/examples/demo_ubjson_basic.m | 180 ++++ extensions/jsonlab-1.2/examples/example1.json | 23 + extensions/jsonlab-1.2/examples/example2.json | 22 + extensions/jsonlab-1.2/examples/example3.json | 11 + extensions/jsonlab-1.2/examples/example4.json | 35 + .../examples/jsonlab_basictest.matlab | 671 ++++++++++++++ .../jsonlab-1.2/examples/jsonlab_selftest.m | 27 + .../examples/jsonlab_selftest.matlab | 154 ++++ .../jsonlab-1.2/examples/jsonlab_speedtest.m | 21 + extensions/jsonlab-1.2/jsonopt.m | 35 + extensions/jsonlab-1.2/loadjson.m | 496 ++++++++++ extensions/jsonlab-1.2/loadubjson.m | 457 +++++++++ extensions/jsonlab-1.2/mergestruct.m | 33 + extensions/jsonlab-1.2/octave-workspace | Bin 0 -> 28882 bytes extensions/jsonlab-1.2/savejson.m | 551 +++++++++++ extensions/jsonlab-1.2/saveubjson.m | 562 +++++++++++ extensions/jsonlab-1.2/struct2jdata.m | 96 ++ extensions/jsonlab-1.2/varargin2struct.m | 40 + extensions/niak_test/niak_brick_cmp_files.m | 18 +- extensions/niak_test/niak_grab_folder.m | 8 +- extensions/niak_test/niak_test_all.m | 46 +- .../niak_test/niak_test_files_preprocess.m | 2 +- extensions/surfstab/niak_brick_scores_fmri.m | 9 +- extensions/surfstab/niak_pipeline_scores.m | 3 - pipeline/niak_pipeline_fmri_preprocess.m | 269 ++---- pipeline/niak_pipeline_fmri_preprocess_ind.m | 198 ++-- .../niak_brick_preproc_anat2report.m | 93 ++ .../niak_brick_preproc_func2report.m | 96 ++ .../niak_brick_preproc_ind_motion2report.m | 93 ++ .../niak_brick_preproc_intra2report.m | 102 ++ .../niak_brick_preproc_motion2report.m | 90 ++ .../niak_brick_preproc_params2report.m | 144 +++ .../niak_brick_preproc_scrubbing2report.m | 110 +++ .../niak_report_fmri_preprocess.m | 315 +++++++ .../templates/assets/backbone/LICENSE.txt | 22 + .../templates/assets/backbone/backbone-min.js | 2 + .../templates/assets/bootstrap/LICENSE.txt | 21 + .../assets/bootstrap/bootstrap.min.css | 5 + .../assets/bootstrap/bootstrap.min.js | 6 + .../templates/assets/c3js/LICENSE | 20 + .../templates/assets/c3js/c3.min.css | 1 + .../templates/assets/c3js/c3.min.js | 6 + .../templates/assets/d3js/LICENSE | 26 + .../templates/assets/d3js/d3.min.js | 5 + .../templates/assets/jquery/LICENSE.txt | 36 + .../templates/assets/jquery/jquery.min.js | 5 + .../assets/jsImageSlider/active-bg.png | Bin 0 -> 2040 bytes .../templates/assets/jsImageSlider/bullet.png | Bin 0 -> 836 bytes .../assets/jsImageSlider/js-image-slider.css | 106 +++ .../assets/jsImageSlider/js-image-slider.js | 21 + .../assets/jsImageSlider/navbuttons.gif | Bin 0 -> 1096 bytes .../assets/jsImageSlider/navbuttons2.gif | Bin 0 -> 1523 bytes .../assets/jsImageSlider/thumbs-bar.gif | Bin 0 -> 171 bytes .../templates/assets/keen/LICENSE.txt | 21 + .../templates/assets/keen/keen-dashboards.css | 98 ++ .../templates/assets/keen/keen.min.js | 29 + .../templates/assets/keen/meta.js | 32 + .../templates/assets/pretty-json/LICENSE.txt | 22 + .../assets/pretty-json/pretty-json-min.js | 14 + .../assets/pretty-json/pretty-json.css | 49 + .../templates/assets/select2/LICENSE.md | 21 + .../templates/assets/select2/select2.min.css | 1 + .../templates/assets/select2/select2.min.js | 2 + .../templates/assets/underscore/LICENSE.txt | 23 + .../assets/underscore/underscore-min.js | 6 + reports/fmri_preprocess/templates/group.html | 132 +++ .../templates/group/listRun.js | 6 + .../templates/group/listSubject.js | 3 + .../templates/group/template_stereotaxic.png | Bin 0 -> 282956 bytes reports/fmri_preprocess/templates/index.html | 439 +++++++++ reports/fmri_preprocess/templates/motion.css | 38 + .../templates/motion/dataMotion.js | 34 + .../templates/motion/median_volume.jpg | Bin 0 -> 1036 bytes .../motion/motion_subject1_native.png | Bin 0 -> 357081 bytes .../motion/motion_subject1_stereo.png | Bin 0 -> 4801 bytes .../templates/motion/motion_template.html | 354 +++++++ .../templates/motion/ref_volume.jpg | Bin 0 -> 1095 bytes .../templates/motion/spacer.png | Bin 0 -> 149 bytes .../templates/motion/spacer_stereo.png | Bin 0 -> 1597 bytes .../templates/registration.html | 275 ++++++ .../templates/registration/X0010001_anat.png | Bin 0 -> 333569 bytes .../templates/registration/X0010001_func.png | Bin 0 -> 389069 bytes .../templates/registration/X0010002_anat.png | Bin 0 -> 350006 bytes .../templates/registration/X0010002_func.png | Bin 0 -> 404346 bytes .../templates/summary/chartBOLD.js | 9 + .../templates/summary/chartBrain.js | 4 + .../templates/summary/chartT1.js | 4 + .../fmri_preprocess/templates/summary/fd.js | 11 + .../templates/summary/filesIn.js | 95 ++ .../templates/summary/pipeSummary.js | 1 + template/niak_confounds.json | 17 + template/niak_template_fmri_preprocess.m | 11 +- util/bin/niak_exec_loop.m | 55 -- util/bin/release_new_target.py | 103 --- util/pyniak/__init__.py | 0 util/pyniak/config.py | 80 -- util/pyniak/process.py | 870 ------------------ 143 files changed, 9444 insertions(+), 2186 deletions(-) create mode 100644 bricks/fmri_preprocess/niak_brick_build_confounds.m create mode 100644 bricks/fmri_preprocess/niak_brick_resample_simple.m create mode 100644 bricks/misc/niak_brick_montage.m create mode 100644 bricks/misc/niak_brick_vol2img.m create mode 100644 commands/SI_processing/niak_fd2mask.m create mode 100644 commands/read_write/niak_grab_report_preprocess.m create mode 100644 commands/visualization/niak_resample_vol.m create mode 100644 commands/visualization/niak_vol2img.m create mode 100644 extensions/jsonlab-1.2/AUTHORS.txt create mode 100644 extensions/jsonlab-1.2/ChangeLog.txt create mode 100644 extensions/jsonlab-1.2/LICENSE_BSD.txt create mode 100644 extensions/jsonlab-1.2/README.txt create mode 100644 extensions/jsonlab-1.2/examples/demo_jsonlab_basic.m create mode 100644 extensions/jsonlab-1.2/examples/demo_ubjson_basic.m create mode 100644 extensions/jsonlab-1.2/examples/example1.json create mode 100644 extensions/jsonlab-1.2/examples/example2.json create mode 100644 extensions/jsonlab-1.2/examples/example3.json create mode 100644 extensions/jsonlab-1.2/examples/example4.json create mode 100644 extensions/jsonlab-1.2/examples/jsonlab_basictest.matlab create mode 100644 extensions/jsonlab-1.2/examples/jsonlab_selftest.m create mode 100644 extensions/jsonlab-1.2/examples/jsonlab_selftest.matlab create mode 100644 extensions/jsonlab-1.2/examples/jsonlab_speedtest.m create mode 100644 extensions/jsonlab-1.2/jsonopt.m create mode 100644 extensions/jsonlab-1.2/loadjson.m create mode 100644 extensions/jsonlab-1.2/loadubjson.m create mode 100644 extensions/jsonlab-1.2/mergestruct.m create mode 100644 extensions/jsonlab-1.2/octave-workspace create mode 100644 extensions/jsonlab-1.2/savejson.m create mode 100644 extensions/jsonlab-1.2/saveubjson.m create mode 100644 extensions/jsonlab-1.2/struct2jdata.m create mode 100644 extensions/jsonlab-1.2/varargin2struct.m create mode 100644 reports/fmri_preprocess/niak_brick_preproc_anat2report.m create mode 100644 reports/fmri_preprocess/niak_brick_preproc_func2report.m create mode 100644 reports/fmri_preprocess/niak_brick_preproc_ind_motion2report.m create mode 100644 reports/fmri_preprocess/niak_brick_preproc_intra2report.m create mode 100644 reports/fmri_preprocess/niak_brick_preproc_motion2report.m create mode 100644 reports/fmri_preprocess/niak_brick_preproc_params2report.m create mode 100644 reports/fmri_preprocess/niak_brick_preproc_scrubbing2report.m create mode 100644 reports/fmri_preprocess/niak_report_fmri_preprocess.m create mode 100644 reports/fmri_preprocess/templates/assets/backbone/LICENSE.txt create mode 100644 reports/fmri_preprocess/templates/assets/backbone/backbone-min.js create mode 100644 reports/fmri_preprocess/templates/assets/bootstrap/LICENSE.txt create mode 100644 reports/fmri_preprocess/templates/assets/bootstrap/bootstrap.min.css create mode 100644 reports/fmri_preprocess/templates/assets/bootstrap/bootstrap.min.js create mode 100644 reports/fmri_preprocess/templates/assets/c3js/LICENSE create mode 100644 reports/fmri_preprocess/templates/assets/c3js/c3.min.css create mode 100644 reports/fmri_preprocess/templates/assets/c3js/c3.min.js create mode 100644 reports/fmri_preprocess/templates/assets/d3js/LICENSE create mode 100644 reports/fmri_preprocess/templates/assets/d3js/d3.min.js create mode 100644 reports/fmri_preprocess/templates/assets/jquery/LICENSE.txt create mode 100644 reports/fmri_preprocess/templates/assets/jquery/jquery.min.js create mode 100644 reports/fmri_preprocess/templates/assets/jsImageSlider/active-bg.png create mode 100644 reports/fmri_preprocess/templates/assets/jsImageSlider/bullet.png create mode 100644 reports/fmri_preprocess/templates/assets/jsImageSlider/js-image-slider.css create mode 100644 reports/fmri_preprocess/templates/assets/jsImageSlider/js-image-slider.js create mode 100644 reports/fmri_preprocess/templates/assets/jsImageSlider/navbuttons.gif create mode 100644 reports/fmri_preprocess/templates/assets/jsImageSlider/navbuttons2.gif create mode 100644 reports/fmri_preprocess/templates/assets/jsImageSlider/thumbs-bar.gif create mode 100644 reports/fmri_preprocess/templates/assets/keen/LICENSE.txt create mode 100644 reports/fmri_preprocess/templates/assets/keen/keen-dashboards.css create mode 100644 reports/fmri_preprocess/templates/assets/keen/keen.min.js create mode 100644 reports/fmri_preprocess/templates/assets/keen/meta.js create mode 100644 reports/fmri_preprocess/templates/assets/pretty-json/LICENSE.txt create mode 100644 reports/fmri_preprocess/templates/assets/pretty-json/pretty-json-min.js create mode 100644 reports/fmri_preprocess/templates/assets/pretty-json/pretty-json.css create mode 100644 reports/fmri_preprocess/templates/assets/select2/LICENSE.md create mode 100644 reports/fmri_preprocess/templates/assets/select2/select2.min.css create mode 100644 reports/fmri_preprocess/templates/assets/select2/select2.min.js create mode 100644 reports/fmri_preprocess/templates/assets/underscore/LICENSE.txt create mode 100644 reports/fmri_preprocess/templates/assets/underscore/underscore-min.js create mode 100644 reports/fmri_preprocess/templates/group.html create mode 100644 reports/fmri_preprocess/templates/group/listRun.js create mode 100644 reports/fmri_preprocess/templates/group/listSubject.js create mode 100644 reports/fmri_preprocess/templates/group/template_stereotaxic.png create mode 100644 reports/fmri_preprocess/templates/index.html create mode 100644 reports/fmri_preprocess/templates/motion.css create mode 100644 reports/fmri_preprocess/templates/motion/dataMotion.js create mode 100644 reports/fmri_preprocess/templates/motion/median_volume.jpg create mode 100644 reports/fmri_preprocess/templates/motion/motion_subject1_native.png create mode 100644 reports/fmri_preprocess/templates/motion/motion_subject1_stereo.png create mode 100644 reports/fmri_preprocess/templates/motion/motion_template.html create mode 100644 reports/fmri_preprocess/templates/motion/ref_volume.jpg create mode 100644 reports/fmri_preprocess/templates/motion/spacer.png create mode 100644 reports/fmri_preprocess/templates/motion/spacer_stereo.png create mode 100644 reports/fmri_preprocess/templates/registration.html create mode 100644 reports/fmri_preprocess/templates/registration/X0010001_anat.png create mode 100644 reports/fmri_preprocess/templates/registration/X0010001_func.png create mode 100644 reports/fmri_preprocess/templates/registration/X0010002_anat.png create mode 100644 reports/fmri_preprocess/templates/registration/X0010002_func.png create mode 100644 reports/fmri_preprocess/templates/summary/chartBOLD.js create mode 100644 reports/fmri_preprocess/templates/summary/chartBrain.js create mode 100644 reports/fmri_preprocess/templates/summary/chartT1.js create mode 100644 reports/fmri_preprocess/templates/summary/fd.js create mode 100644 reports/fmri_preprocess/templates/summary/filesIn.js create mode 100644 reports/fmri_preprocess/templates/summary/pipeSummary.js create mode 100644 template/niak_confounds.json delete mode 100644 util/bin/niak_exec_loop.m delete mode 100644 util/bin/release_new_target.py delete mode 100644 util/pyniak/__init__.py delete mode 100644 util/pyniak/config.py delete mode 100644 util/pyniak/process.py diff --git a/Dockerfile b/Dockerfile index 71e3d94d..6c14a8be 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ -FROM simexp/octave:01_09_2015 +FROM simexp/octave:3.8.1 MAINTAINER Pierre-Olivier Quirion -ENV NIAK_VERSION v0.13.4 +ENV NIAK_VERSION dev-0.14.0 ENV NIAK_RELEASE_NAME niak-with-dependencies # Install NIAK from the tip of master diff --git a/bricks/connectome/niak_brick_connectome.m b/bricks/connectome/niak_brick_connectome.m index 8c60d0be..e9ff4dd3 100644 --- a/bricks/connectome/niak_brick_connectome.m +++ b/bricks/connectome/niak_brick_connectome.m @@ -95,7 +95,7 @@ % % _________________________________________________________________________ % Copyright (c) Pierre Bellec, Christian L. Dansereau, -% Centre de recherche de l'Institut universitaire de gériatrie de Montréal, 2012. +% Centre de recherche de l'Institut universitaire de griatrie de Montral, 2012. % Maintainer : pierre.bellec@criugm.qc.ca % See licensing information in the code. % Keywords : medical imaging, connectome, atoms, fMRI @@ -185,12 +185,12 @@ %% The brick starts here -nb_fmri = length(files_in.fmri); -nb_mask = length(files_in.mask); -all_conn = cell(nb_mask,1); +nb_fmri = length(files_in.fmri); +nb_mask = length(files_in.mask); +all_conn = cell(nb_mask,1); all_ind_roi = cell(nb_mask,1); -all_mask = cell(nb_mask,1); -all_hdr = cell(nb_mask,1); +all_mask = cell(nb_mask,1); +all_hdr = cell(nb_mask,1); if opt.flag_verbose fprintf('Generating ''%s'' connectomes ...\n',type) end @@ -199,6 +199,9 @@ fprintf('Reading fMRI dataset %s ...\n',files_in.fmri{num_f}); end [hdr,vol] = niak_read_vol(files_in.fmri{num_f}); + if isfield(hdr,'extra')&&isfield(hdr.extra,'mask_scubbing') + vol = vol(:,:,:,~hdr.extra.mask_scubbing); + end [nx,ny,nz,nt] = size(vol); for num_m = 1:nb_mask if num_f == 1 diff --git a/bricks/connectome/niak_brick_rmap.m b/bricks/connectome/niak_brick_rmap.m index f8c979c9..03155617 100644 --- a/bricks/connectome/niak_brick_rmap.m +++ b/bricks/connectome/niak_brick_rmap.m @@ -209,6 +209,9 @@ fprintf('Generating connectivity map from fMRI dataset %s, processing seeds:\n ',files_in.fmri{num_f}); end [hdr,vol] = niak_read_vol(files_in.fmri{num_f}); + if isfield(hdr,'extra')&&isfield(hdr.extra,'mask_scubbing') + vol = vol(:,:,:,~hdr.extra.mask_scubbing); + end for num_s = 1:length(list_seed) seed = list_seed{num_s}; if opt.flag_verbose diff --git a/bricks/fir/niak_brick_fir_tseries.m b/bricks/fir/niak_brick_fir_tseries.m index 3d525071..0fcb20df 100644 --- a/bricks/fir/niak_brick_fir_tseries.m +++ b/bricks/fir/niak_brick_fir_tseries.m @@ -232,12 +232,21 @@ % Read the 3D+t [hdr,vol] = niak_read_vol(files_in.fmri{num_r}); + % scrubbing + if isfield(hdr,'extra')&&isfield(hdr.extra,'mask_scrubbing') + mask_scrubbing = hdr.extra.mask_scrubbing; + else + mask_scrubbing = false(size(vol,4),1); + end + % Read the time frames if isfield(hdr,'extra') opt_fir.time_frames = hdr.extra.time_frames; else opt_fir.time_frames = (0:(size(vol,4)-1))*hdr.info.tr; end + opt_fir.time_frames = opt_fir.time_frames(~mask_scrubbing); + vol = vol(:,:,:,~mask_scrubbing); % Read the event times [time_events,labels_conditions] = niak_read_csv(files_in.timing{num_r}); diff --git a/bricks/fmri_preprocess/niak_brick_build_confounds.m b/bricks/fmri_preprocess/niak_brick_build_confounds.m new file mode 100644 index 00000000..6ed5424f --- /dev/null +++ b/bricks/fmri_preprocess/niak_brick_build_confounds.m @@ -0,0 +1,361 @@ +function [files_in,files_out,opt]=niak_brick_build_confounds(files_in,files_out,opt) +% Generate "noise" confounds for fMRI time series. +% +% SYNTAX : +% NIAK_BRICK_BUILD_CONFOUNDS(FILES_IN,FILES_OUT,OPT) +% +% _________________________________________________________________________ +% INPUTS : +% +% FILES_IN +% (structure) with the following fields: +% +% FMRI +% (string) the fmri time-series +% +% DC_LOW +% (string) cosine basis of slow time drifts to be removed +% +% DC_HIGH +% (string) cosine basis of high frequencies to be removed +% +% CUSTOM_PARAM +% (string, optional) a .mat file with one variable 'covar'(TxK) +% +% MOTION_PARAM +% (string) a .mat file with motion parameters (see +% NIAK_PIPELINE_MOTION_CORRECTION) +% +% MASK_WM +% (string) the name of a 3D volume file with a binary mask of +% the white matter +% +% MASK_VENT +% (string) the name of a 3D volume file with a binary mask of +% the ventricle +% +% MASK_BRAIN +% (string) the name of a 3D volume file with a binary mask of the +% brain +% +% FILES_OUT +% (structure) with the following fields (outputs associated with +% absent fields are not generated, a default name is generated for +% empty fields): +% +% CONFOUNDS +% (string, default FOLDER_OUT/_confounds.tsv.gz) the name +% of a file with (compressed) tab-separated values. Each column +% corresponds to a "confound" effect. The confounds include: +% slow time drifts, motion parameters, ventricular and white matter +% average, COMPCOR, global signal, FD, custom regressors. +% +% COMPCOR_MASK +% (string, default FOLDER_OUT/_mask_compcor.) the +% name of a 3D file, with a binary volume of the voxels used for compcor +% regression. +% +% OPT +% +% FOLDER_OUT +% (string, default folder of FMRI) the folder where the default outputs +% are generated. +% +% COMPCOR +% (structure) the options of the COMPCOR method. See the OPT argument +% of NIAK_COMPCOR. +% +% THRE_FD +% (scalar, default 0.5) the maximal acceptable framewise displacement +% after scrubbing. +% +% WW_FD +% (vector, default [3 6]) defines the time window to be removed around each time frame +% identified with excessive motion. First value is for time prior to motion peak, and second value +% is for time following motion peak. +% +% NB_VOL_MIN +% (integer, default 40) the minimal number of volumes remaining after +% scrubbing (unless the data themselves are shorter). If there are not enough +% time frames after scrubbing, the time frames with lowest FD are selected. +% +% FLAG_VERBOSE +% (boolean, default 1) if the flag is 1, then the function +% prints some infos during the processing. +% +% FLAG_TEST +% (boolean, default 0) if FLAG_TEST equals 1, the brick does not +% do anything but update the default values in FILES_IN, +% FILES_OUT and OPT. +% +% _________________________________________________________________________ +% OUTPUTS +% +% The structures FILES_IN, FILES_OUT and OPT are updated with default +% valued. If OPT.FLAG_TEST == 0, the specified outputs are written. +% +% _________________________________________________________________________ +% COMMENTS: +% +% The estimator of the global average using PCA is described in the +% following publication: +% +% F. Carbonell, P. Bellec, A. Shmuel. Validation of a superposition model +% of global and system-specific resting state activity reveals anti-correlated +% networks. Brain Connectivity 2011 1(6): 496-510. doi:10.1089/brain.2011.0065 +% +% For an overview of the regression steps as well as the "scrubbing" of +% volumes with excessive motion, see: +% +% J. D. Power, K. A. Barnes, Abraham Z. Snyder, B. L. Schlaggar, S. E. Petersen +% Spurious but systematic correlations in functional connectivity MRI networks +% arise from subject motion +% NeuroImage Volume 59, Issue 3, 1 February 2012, Pages 21422154 +% +% For a description of the COMPCOR method: +% +% Behzadi, Y., Restom, K., Liau, J., Liu, T. T., Aug. 2007. A component based +% noise correction method (CompCor) for BOLD and perfusion based fMRI. +% NeuroImage 37 (1), 90-101. http://dx.doi.org/10.1016/j.neuroimage.2007.04.042 +% +% This other paper describes more accurately the COMPCOR implemented in NIAK: +% Chai, X. J., Castan, A. N. N., Ongr, D., Whitfield-Gabrieli, S., Jan. 2012. +% Anticorrelations in resting state networks without global signal regression. +% NeuroImage 59 (2), 1420-1428. http://dx.doi.org/10.1016/j.neuroimage.2011.08.048 + +% Note that a maximum number of (# degrees of freedom)/2 are removed through compcor. +% +% Copyright (c) Christian L. Dansereau, Felix Carbonell, Pierre Bellec +% Research Centre of the Montreal Geriatric Institute +% & Department of Computer Science and Operations Research +% University of Montreal, Qubec, Canada, 2012-2015 +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords : pca, glm, confounds, motion parameters + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +%% FILES_IN +list_fields = { 'fmri' , 'dc_low' , 'dc_high' , 'custom_param' , 'motion_param' , 'mask_brain' , 'mask_vent' , 'mask_wm' }; +list_defaults = { NaN , NaN , NaN , 'gb_niak_omitted' , NaN , NaN , NaN , NaN }; +files_in = psom_struct_defaults(files_in,list_fields,list_defaults); + +%% FILES_OUT +list_fields = { 'compcor_mask' , 'confounds' }; +list_defaults = { 'gb_niak_omitted' , 'gb_niak_omitted' }; +files_out = psom_struct_defaults(files_out,list_fields,list_defaults); + +%% OPTIONS +list_fields = { 'thre_fd' , 'ww_fd' , 'nb_min_vol' , 'compcor' , 'folder_out' , 'flag_verbose', 'flag_test' }; +list_defaults = { 0.5 , [3 6] , 40 , struct() , '' , true , false }; +opt = psom_struct_defaults(opt,list_fields,list_defaults); + +[path_f,name_f,ext_f] = niak_fileparts(files_in.fmri); + +if isempty(opt.folder_out) + opt.folder_out = path_f; +end + +if isempty(files_out.confounds) + files_out.confounds = cat(2,opt.folder_out,filesep,name_f,'_confounds.tsv.gz'); +end + +if isempty(files_out.compcor_mask) + files_out.compcor_mask = cat(2,opt.folder_out,filesep,name_f,'_compcor_mask',ext_f); +end + +if opt.flag_test + return +end + +%% Read spatial inputs +if opt.flag_verbose + fprintf('Reading the fMRI dataset ...\n%s\n',files_in.fmri); +end +[hdr_vol,vol] = niak_read_vol(files_in.fmri); % fMRI dataset +y = reshape(vol,[size(vol,1)*size(vol,2)*size(vol,3) size(vol,4)])'; % organize the fMRI dataset as a time x space array +mean_y = mean(y,1); +y = niak_normalize_tseries(y,'mean'); + +if opt.flag_verbose + fprintf('Reading the brain mask ventricle ...\n%s\n',files_in.mask_vent); +end +[hdr_mask,mask_vent] = niak_read_vol(files_in.mask_vent); % mask of the ventricles + +if opt.flag_verbose + fprintf('Reading the brain mask ...\n%s\n',files_in.mask_brain); +end +[hdr_mask,mask_brain] = niak_read_vol(files_in.mask_brain); % mask of the brain +mask = mask_brain>0; + +if opt.flag_verbose + fprintf('Reading the white matter mask ...\n%s\n',files_in.mask_wm); +end +[hdr_mask,mask_wm] = niak_read_vol(files_in.mask_wm); % mask of the white matter + +%% Motion parameters +if opt.flag_verbose + fprintf('Adding motion parameters...\n') +end +labels = { 'motion_tx' 'motion_ty' 'motion_tz' 'motion_rx' 'motion_ry' 'motion_rz' }; +transf = load(files_in.motion_param); +[rot,tsl] = niak_transf2param(transf.transf); +x = [tsl' rot']; + +%% Frame displacement +if opt.flag_verbose + fprintf('Adding frame displacement...\n') +end +rot_d = 50*(rot/360)*pi*2; % adjust rotation parameters to express them as a displacement for a typical distance from the center of 50 mm +rot_d = rot_d(:,2:end) - rot_d(:,1:(end-1)); +tsl_d = tsl(:,2:end) - tsl(:,1:(end-1)); +fd = sum(abs(rot_d)+abs(tsl_d),1)'; +fd = [fd;0]; +labels = [labels {'FD'}]; +x = [x fd]; + +%% Scrubbing +if isfield(hdr_vol,'extra') + time_frames = hdr_vol.extra.time_frames; +else + time_frames = (0:(size(vol,4)-1))*hdr_vol.info.tr; +end +opt_s.thre = opt.thre_fd; +opt_s.ww = opt.ww_fd; +opt_s.nb_min_vol = opt.nb_min_vol; +scrub = niak_fd2mask(fd,time_frames,opt_s); +labels = [labels {'scrub'}]; +x = [x scrub]; + +%% Add Time filter dc low +if opt.flag_verbose + fprintf('Adding slow time drifts ...\n') +end +slow_drift = load(files_in.dc_low); +slow_drift = slow_drift.tseries_dc_low; +mask_i = std(slow_drift,[],1)~=0; +slow_drift = slow_drift(:,mask_i); % get rid of the intercept in the slow time drifts +x = [x slow_drift]; +labels = [labels repmat({'slow_drift'},[1 size(slow_drift,2)])]; + +%% Add Time filter dc high +if opt.flag_verbose + fprintf('Adding high frequencies ...\n') +end +high_freq = load(files_in.dc_high); +if ~isempty(high_freq.tseries_dc_high) + high_freq = high_freq.tseries_dc_high; +else + high_freq = zeros([size(x,1) 0]); +end +x = [x high_freq]; +labels = [labels repmat({'high_freq'},[1 size(high_freq,2)])]; + +%% Add white matter average +if opt.flag_verbose + fprintf('Adding white matter average ...\n') +end +wm_av = mean(y(:,mask_wm>0),2); +x = [x,wm_av]; +labels = [labels {'wm_avg'}]; + +%% Add ventricular average +if opt.flag_verbose + fprintf('Adding ventricular average ...\n') +end +vent_av = mean(y(:,mask_vent>0),2); +x = [x,vent_av]; +labels = [labels {'vent_avg'}]; + +%% Add Global signal +if opt.flag_verbose + fprintf('Generate a PCA-based estimation of the global signal ...\n') +end +pc_spatial_av = sub_pc_spatial_av(vol,mask); +x = [x pc_spatial_av]; +labels = [labels {'global_signal_pca'}]; + +%% Add Compcor +if opt.flag_verbose + fprintf('Adding COMPCOR...\n') +end +[x_comp,mask_comp] = niak_compcor(vol,opt.compcor,mask_wm|mask_vent); +nb_comp_max = floor((size(y,1)-size(x,2))/2); +x_comp = x_comp(:,1:min(size(x_comp,2),nb_comp_max)); +x = [x x_comp]; +labels = [ labels repmat({'compcor'},[1 size(x_comp,2)]) ]; + +%% Custom parameters +if ~strcmp(files_in.custom_param,'gb_niak_omitted') + if opt.flag_verbose + fprintf('Adding custom parameters ...\n') + end + covar = load(files_in.custom_param); + if isfield(hdr,'extra') + covar = covar.covar(~hdr.extra.mask_suppressed,:); + else + covar = covar.covar; + end + if ~isempty(covar) && (size(covar,1)==size(y,1)) + covar = niak_normalize_tseries(covar); + x = [x covar]; + labels = [ labels repmat({'custom'},[1 size(covar,2)]) ]; + else + error('The dimensions of the user-specified covariates are inappropriate (%i samples, functional datasets has %i time points)',size(covar,1),size(y,1)) + end +end + +%% Save the COMPCOR mask +if ~strcmp(files_out.compcor_mask,'gb_niak_omitted') + hdr_vol.file_name = files_out.compcor_mask; + if isfield(hdr_vol,'extra') + hdr_vol = rmfield(hdr_vol,'extra'); + end + niak_write_vol(hdr_vol,mask_comp); +end + +%% Save the confounds +if ~strcmp(files_out.confounds,'gb_niak_omitted') + niak_write_csv_cell(files_out.confounds,[labels ; num2cell(x)]); +end + +%%%%%%%%%%%%%%%%%% +%% SUBFUNCTIONS %% +%%%%%%%%%%%%%%%%%% + +function pc_spatial_av = sub_pc_spatial_av(vol,mask) +%% global signal estimation using a combination of the global average (target) and PCA (explanatory variables) +% Coded after: +% F. Carbonell, P. Bellec, A. Shmuel. Validation of a superposition model +% of global and system-specific resting state activity reveals anti-correlated +% networks. To appear in Brain Connectivity. + +% PCA +tseries = niak_vol2tseries(vol,mask); +[eigenvalues,eigenvariates,weights] = niak_pca(tseries'); + +% Spatial Average +spatial_av = niak_normalize_tseries(mean(tseries,2)); +eigenvariates = niak_normalize_tseries(eigenvariates); + +% Determine PC to be removed +r = (1/(length(spatial_av)-1))*(spatial_av'*eigenvariates); +[coeff_av,ind_pca] = max(abs(r)); +pc_spatial_av = eigenvariates(:,ind_pca); +pc_spatial_av = pc_spatial_av*sign((pc_spatial_av'*spatial_av)); diff --git a/bricks/fmri_preprocess/niak_brick_qc_coregister.m b/bricks/fmri_preprocess/niak_brick_qc_coregister.m index a0e4f911..ca72dd25 100644 --- a/bricks/fmri_preprocess/niak_brick_qc_coregister.m +++ b/bricks/fmri_preprocess/niak_brick_qc_coregister.m @@ -286,7 +286,7 @@ if flag_4d std_vol = std_vol/length(files_in.vol); else - std_vol = sqrt((std_vol-length(files_in.vol)*(mean_vol.^2))/(length(files_in.vol)-1)); + std_vol = sqrt(abs((std_vol-length(files_in.vol)*(mean_vol.^2)))/(length(files_in.vol)-1)); end if flag_verbose diff --git a/bricks/fmri_preprocess/niak_brick_regress_confounds.m b/bricks/fmri_preprocess/niak_brick_regress_confounds.m index 4e43773a..92d82a78 100644 --- a/bricks/fmri_preprocess/niak_brick_regress_confounds.m +++ b/bricks/fmri_preprocess/niak_brick_regress_confounds.m @@ -1,6 +1,5 @@ function [files_in,files_out,opt]=niak_brick_regress_confounds(files_in,files_out,opt) -% Regress slow time drift, global signal, motion parameters -% as well as "scrubbing" of time frames with excessive motion +% Regress confounds from fMRI time series % % SYNTAX : % NIAK_BRICK_REGRESS_CONFOUNDS(FILES_IN,FILES_OUT,OPT) @@ -14,98 +13,15 @@ % FMRI % (string) the fmri time-series % -% DC_LOW -% (string) cosine basis of slow time drifts to be removed -% -% DC_HIGH -% (string) cosine basis of high frequencies to be removed -% -% CUSTOM_PARAM -% (string, optional) a .mat file with one variable 'covar'(TxK) -% -% MOTION_PARAM -% (string) a .mat file with motion parameters (see -% NIAK_PIPELINE_MOTION_CORRECTION) -% -% MASK_WM -% (string) the name of a 3D volume file with a binary mask of -% the white matter -% -% MASK_VENT -% (string) the name of a 3D volume file with a binary mask of -% the ventricle -% -% MASK_BRAIN -% (string) the name of a 3D volume file with a binary mask of the -% brain +% CONFOUNDS +% (string) the name of a file with (compressed) tab-separated values. +% Each column corresponds to a "confound" effect. +% see template/niak_confounds.json for a list of expected confounds. % % FILES_OUT -% (structure) with the following fields (outputs associated with -% absent fields are not generated, a default name is generated for -% empty fields): -% -% FILTERED_DATA % (string, default FOLDER_OUT/_cor.) the name % of a 3D+t file. Same as FMRI with the confounds regressed out. % -% CONFOUNDS -% (string, default FOLDER_OUT/_cor.mat) a .mat file with -% the covariates that were regressed out (X, LABELS for stage 1, -% including slow time drifts, motion parameters and white matter -% average, and X2, LABELS2 for stage 2, including custom covariates -% and global signal). -% -% SCRUBBING -% (string, default FOLDER_OUT/_scrub.mat) a .mat file with -% the following variables: -% MASK_SCRUB (vector of boolean): if MASK_SCRUB(I) is true, the -% volume #I is scrubbed. -% FD (vector) FD(I) is the framewise displacement at volume I. -% DVARS (vector) DVARS(I) is the mean squares variance -% of residuals. -% -% COMPCOR_MASK -% (string, default FOLDER_OUT/_mask_compcor.) the -% name of a 3D file, with a binary volume of the voxels used for compcor -% regression. -% -% QC_SLOW_DRIFT -% (string, default FOLDER_OUT/qc__ftest_slow_drift.) -% the name of a volume file with the f-test of the slow time drifts -% -% QC_HIGH -% (string, default FOLDER_OUT/qc__ftest_high.) -% the name of a volume file with the f-test of the removal of high frequencies -% (low-pass filter). -% -% QC_VENT -% (string, default FOLDER_OUT/qc__ftest_vent.) -% the name of a volume file with the f-test of the average ventricular -% signal -% -% QC_WM -% (string, default FOLDER_OUT/qc__ftest_wm.) -% the name of a volume file with the f-test of the average white matter -% signal -% -% QC_COMPCOR -% (string, default FOLDER_OUT/qc__ftest_compcor.) -% the name of a volume file with the f-test of compcor -% -% QC_MOTION -% (string, default FOLDER_OUT/qc__ftest_motion.) -% the name of a volume file with the f-test of the motion parameters -% (after PCA reduction). -% -% QC_GSE -% (string, default FOLDER_OUT/qc__ftest_gse.) -% the name of a volume file with the f-test of the global signal PCA -% estimate. -% -% QC_CUSTOM_PARAM -% (string, default FOLDER_OUT/qc__ftest_customparam.) -% the name of a volume with the f-test of the custom params -% % OPT % % FOLDER_OUT @@ -140,15 +56,6 @@ % (boolean, default true) turn on/off the "scrubbing" of volumes with % excessive motion. % -% THRE_FD -% (scalar, default 0.5) the maximal acceptable framewise displacement -% after scrubbing. -% -% NB_VOL_MIN -% (integer, default 40) the minimal number of volumes remaining after -% scrubbing (unless the data themselves are shorter). If there are not enough -% time frames after scrubbing, the time frames with lowest FD are selected. -% % PCT_VAR_EXPLAINED % (boolean, default 0.95) the % of variance explained by the selected % PCA components when reducing the dimensionality of motion parameters. @@ -157,10 +64,6 @@ % (boolean, default true) turn on/off the PCA reduction of motion % parameters. % -% COMPCOR -% (structure) the options of the COMPCOR method. See the OPT argument -% of NIAK_COMPCOR. -% % _________________________________________________________________________ % OUTPUTS % @@ -170,6 +73,9 @@ % _________________________________________________________________________ % COMMENTS: % +% See template/niak_confounds.json in the niak folder for a list of +% admissible confound labels. +% % The estimator of the global average using PCA is described in the % following publication: % @@ -183,7 +89,7 @@ % J. D. Power, K. A. Barnes, Abraham Z. Snyder, B. L. Schlaggar, S. E. Petersen % Spurious but systematic correlations in functional connectivity MRI networks % arise from subject motion -% NeuroImage Volume 59, Issue 3, 1 February 2012, Pages 2142–2154 +% NeuroImage Volume 59, Issue 3, 1 February 2012, Pages 21422154 % % Note that the scrubbing is based solely on the FD index, and that DVARS is not % derived. The paper of Power et al. included both indices. @@ -195,7 +101,7 @@ % NeuroImage 37 (1), 90-101. http://dx.doi.org/10.1016/j.neuroimage.2007.04.042 % % This other paper describes more accurately the COMPCOR implemented in NIAK: -% Chai, X. J., Castañón, A. N. N., Ongür, D., Whitfield-Gabrieli, S., Jan. 2012. +% Chai, X. J., Castan, A. N. N., Ongr, D., Whitfield-Gabrieli, S., Jan. 2012. % Anticorrelations in resting state networks without global signal regression. % NeuroImage 59 (2), 1420-1428. http://dx.doi.org/10.1016/j.neuroimage.2011.08.048 @@ -204,7 +110,7 @@ % Copyright (c) Christian L. Dansereau, Felix Carbonell, Pierre Bellec % Research Centre of the Montreal Geriatric Institute % & Department of Computer Science and Operations Research -% University of Montreal, Québec, Canada, 2012 +% University of Montreal, Qubec, Canada, 2012 % Maintainer : pierre.bellec@criugm.qc.ca % See licensing information in the code. % Keywords : pca, glm, confounds, motion parameters @@ -228,397 +134,209 @@ % THE SOFTWARE. %% FILES_IN -list_fields = { 'fmri' , 'dc_low' , 'dc_high' , 'custom_param' , 'motion_param' , 'mask_brain' , 'mask_vent' , 'mask_wm' }; -list_defaults = { NaN , NaN , NaN , 'gb_niak_omitted' , NaN , NaN , NaN , NaN }; +list_fields = { 'fmri' , 'confounds' }; +list_defaults = { NaN , NaN }; files_in = psom_struct_defaults(files_in,list_fields,list_defaults); %% FILES_OUT -list_fields = { 'scrubbing' , 'compcor_mask' , 'confounds' , 'filtered_data' , 'qc_compcor' , 'qc_slow_drift' , 'qc_high' , 'qc_wm' , 'qc_vent' , 'qc_motion' , 'qc_custom_param' , 'qc_gse' }; -list_defaults = { 'gb_niak_omitted' , 'gb_niak_omitted' , 'gb_niak_omitted' , 'gb_niak_omitted' , 'gb_niak_omitted' , 'gb_niak_omitted' , 'gb_niak_omitted' , 'gb_niak_omitted' , 'gb_niak_omitted' , 'gb_niak_omitted' , 'gb_niak_omitted' , 'gb_niak_omitted' }; +list_fields = { 'filtered_data' , 'scrubbing' }; +list_defaults = { 'gb_niak_omitted' , 'gb_niak_omitted' }; files_out = psom_struct_defaults(files_out,list_fields,list_defaults); %% OPTIONS -list_fields = { 'flag_compcor' , 'compcor' , 'nb_vol_min' , 'flag_scrubbing' , 'thre_fd' , 'flag_slow' , 'flag_high' , 'folder_out' , 'flag_verbose', 'flag_motion_params', 'flag_wm', 'flag_vent' , 'flag_gsc' , 'flag_pca_motion', 'flag_test', 'pct_var_explained'}; -list_defaults = { false , struct() , 40 , true , 0.5 , true , false , '' , true , true , true , true , false , true , false , 0.95 }; +list_fields = { 'flag_compcor' , 'nb_vol_min' , 'flag_scrubbing' , 'thre_fd' , 'flag_slow' , 'flag_high' , 'folder_out' , 'flag_verbose', 'flag_motion_params', 'flag_wm', 'flag_vent' , 'flag_gsc' , 'flag_pca_motion', 'flag_test', 'pct_var_explained'}; +list_defaults = { false , 40 , true , 0.5 , true , false , '' , true , true , true , true , false , true , false , 0.95 }; opt = psom_struct_defaults(opt,list_fields,list_defaults); - [path_f,name_f,ext_f] = niak_fileparts(files_in.fmri); if isempty(opt.folder_out) opt.folder_out = path_f; end -if isempty(files_out.confounds) - files_out.confounds = cat(2,opt.folder_out,filesep,name_f,'_cor.mat'); -end - -if isempty(files_out.compcor_mask) - files_out.compcor_mask = cat(2,opt.folder_out,filesep,name_f,'_compcor_mask',ext_f); -end - if isempty(files_out.filtered_data) files_out.filtered_data = cat(2,opt.folder_out,filesep,name_f,'_cor',ext_f); end -if isempty(files_out.qc_slow_drift) - files_out.qc_slow_drift = cat(2,opt.folder_out,filesep,'qc_',name_f,'_ftest_slow_drift',ext_f); -end - -if isempty(files_out.qc_high) - files_out.qc_high = cat(2,opt.folder_out,filesep,'qc_',name_f,'_ftest_high',ext_f); -end - -if isempty(files_out.qc_wm) - files_out.qc_wm = cat(2,opt.folder_out,filesep,'qc_',name_f,'_ftest_wm',ext_f); -end - -if isempty(files_out.qc_vent) - files_out.qc_vent = cat(2,opt.folder_out,filesep,'qc_',name_f,'_ftest_vent',ext_f); -end - -if isempty(files_out.qc_motion) - files_out.qc_motion = cat(2,opt.folder_out,filesep,'qc_',name_f,'_ftest_motion',ext_f); -end - -if isempty(files_out.qc_custom_param) - files_out.qc_custom_param = cat(2,opt.folder_out,filesep,'qc_',name_f,'_ftest_customparam',ext_f); -end - -if isempty(files_out.qc_compcor) - files_out.qc_compcor = cat(2,opt.folder_out,filesep,'qc_',name_f,'_ftest_compcor',ext_f); -end - -if isempty(files_out.qc_gse) - files_out.qc_gse = cat(2,opt.folder_out,filesep,'qc_',name_f,'_ftest_gse',ext_f); +if isempty(files_out.scrubbing) + files_out.scrubbing = cat(2,opt.folder_out,filesep,name_f,'_scrub.mat'); end if opt.flag_test return end -%% Read spatial inputs +%% Read the fMRI dataset if opt.flag_verbose fprintf('Reading the fMRI dataset ...\n%s\n',files_in.fmri); end [hdr_vol,vol] = niak_read_vol(files_in.fmri); % fMRI dataset y = reshape(vol,[size(vol,1)*size(vol,2)*size(vol,3) size(vol,4)])'; % organize the fMRI dataset as a time x space array -mean_y = mean(y,1); -y = niak_normalize_tseries(y,'mean'); +%% Read the confounds if opt.flag_verbose - fprintf('Reading the brain mask ventricle ...\n%s\n',files_in.mask_vent); + fprintf('Reading the confounds...\n%s\n',files_in.confounds); end -[hdr_mask,mask_vent] = niak_read_vol(files_in.mask_vent); % mask of the ventricles - -if opt.flag_verbose - fprintf('Reading the brain mask ...\n%s\n',files_in.mask_brain); -end -[hdr_mask,mask_brain] = niak_read_vol(files_in.mask_brain); % mask of the brain -mask = mask_brain>0; - -if opt.flag_verbose - fprintf('Reading the white matter mask ...\n%s\n',files_in.mask_wm); -end -[hdr_mask,mask_wm] = niak_read_vol(files_in.mask_wm); % mask of the white matter +tab = niak_read_csv_cell(files_in.confounds); +x = str2double(tab(2:end,:)); +all_labels = tab(1,:); %% Scrubbing if opt.flag_verbose fprintf('Scrubbing frames exhibiting large motion ...\n') end +mask_scrubbing = x(:,strcmp(all_labels,'scrub')); +fd = x(:,strcmp(all_labels,'FD')); +fd = fd(1:end-1); +hdr_vol.extra.mask_scrubbing = mask_scrubbing; -transf = load(files_in.motion_param); -[rot,tsl] = niak_transf2param(transf.transf); -rot_d = 50*(rot/360)*pi*2; % adjust rotation parameters to express them as a displacement for a typical distance from the center of 50 mm -rot_d = rot_d(:,2:end) - rot_d(:,1:(end-1)); -tsl_d = tsl(:,2:end) - tsl(:,1:(end-1)); -fd = sum(abs(rot_d)+abs(tsl_d),1)'; -mask_scrubbing = false(size(y,1),1); -if opt.flag_scrubbing - mask_scrubbing(2:end) = (fd>opt.thre_fd); - mask_scrubbing2 = mask_scrubbing; - mask_scrubbing2(1:(end-1)) = mask_scrubbing2(1:(end-1))|mask_scrubbing(2:end); - mask_scrubbing2(2:end) = mask_scrubbing2(2:end)|mask_scrubbing(1:(end-1)); - mask_scrubbing2(3:end) = mask_scrubbing2(3:end)|mask_scrubbing(1:(end-2)); - mask_scrubbing = mask_scrubbing2; - if sum(~mask_scrubbing) < opt.nb_vol_min - mask_scrubbing = true(size(mask_scrubbing)); - [val_scr,order_scr] = sort(max([0 ; fd(:)],[fd(:) ; 0]),'ascend'); - mask_scrubbing(order_scr(1:min(length(mask_scrubbing),opt.nb_vol_min))) = false; - warning('There was not enough time frames left after scrubbing, kept the %i time frames with smallest frame displacement. See OPT.NB_VOL_MIN.',opt.nb_vol_min) - end - y = y(~mask_scrubbing,:); -end - -if isfield(hdr_vol,'extra') - %% Add the scrubbing information in the 'extra' .mat companion that comes with the 3D+t dataset - hdr_vol.extra.mask_suppressed(~hdr_vol.extra.mask_suppressed) = mask_scrubbing; - hdr_vol.extra.time_frames = hdr_vol.extra.time_frames(~mask_scrubbing); -end - -%% Initialization -x = []; +%% Build model +x2 = []; labels = {}; -opt_glm.flag_residuals = true; -opt_glm.test = 'none'; -%% Add Time filter dc low -if opt.flag_verbose - fprintf('Reading slow time drifts ...\n') -end -slow_drift = load(files_in.dc_low); -slow_drift = slow_drift.tseries_dc_low(~mask_scrubbing,:); -mask_i = std(slow_drift,[],1)~=0; -slow_drift = slow_drift(:,mask_i); % get rid of the intercept in the slow time drifts -if opt.flag_slow - x = [x slow_drift]; - labels = [labels repmat({'slow_drift'},[1 size(slow_drift,2)])]; +%% Slow time drifts +if opt.flag_slow + if opt.flag_verbose + fprintf('Adding slow time drifts ...\n') + end + mask_slow = strcmp(all_labels,'slow_drift'); + x2 = [x2 x(:,mask_slow)]; + labels = [labels all_labels(mask_slow)]; +else + if opt.flag_verbose + fprintf('Ignoring slow time drifts...\n') + end end -%% Add Time filter dc high -if opt.flag_verbose - fprintf('Reading high frequencies ...\n') -end -high_freq = load(files_in.dc_high); -if ~isempty(high_freq.tseries_dc_high) - high_freq = high_freq.tseries_dc_high(~mask_scrubbing,:); +%% High frequencies +if opt.flag_high + if opt.flag_verbose + fprintf('Adding high frequency noise...\n') + end + mask_high = strcmp(all_labels,'high_freq'); + x2 = [x2 x(:,mask_high)]; + labels = [labels all_labels(mask_high)]; else - high_freq = zeros([sum(~mask_scrubbing) 0]); -end -if opt.flag_high - x = [x high_freq]; - labels = [labels repmat({'high'},[1 size(high_freq,2)])]; + if opt.flag_verbose + fprintf('Ignoring high frequency noise...\n') + end end %% Motion parameters -if opt.flag_verbose - fprintf('Reading (and reducing) the motion parameters ...\n') -end -transf = load(files_in.motion_param); -[rot,tsl] = niak_transf2param(transf.transf); -rot = rot(:,~mask_scrubbing); -tsl = tsl(:,~mask_scrubbing); -rot = niak_normalize_tseries(rot'); -tsl = niak_normalize_tseries(tsl'); +rot = niak_normalize_tseries(x(:,ismember(all_labels,{'motion_rx','motion_ry','motion_rz'}))); +tsl = niak_normalize_tseries(x(:,ismember(all_labels,{'motion_tx','motion_ty','motion_tz'}))); motion_param = [rot,tsl,rot.^2,tsl.^2]; if opt.flag_pca_motion [eig_val,motion_param] = niak_pca(motion_param',opt.pct_var_explained); end if opt.flag_motion_params - x = [x motion_param]; - labels = [labels repmat({'motion'},[1 size(motion_param,2)])]; -end - -%% Add white matter average -if opt.flag_verbose - fprintf('White matter average ...\n') -end -wm_av = mean(y(:,mask_wm>0),2); -if opt.flag_wm - x = [x,wm_av]; - labels = [labels {'wm_av'}]; -end - -%% Add ventricular average -if opt.flag_verbose - fprintf('Ventricular average ...\n') -end -vent_av = mean(y(:,mask_vent>0),2); -if opt.flag_vent - x = [x,vent_av]; - labels = [labels {'vent_av'}]; -end - -%% Generate F-TEST maps for all components of the model for quality control purposes -hdr_qc = hdr_mask; -model.y = y; -model.x = niak_normalize_tseries([slow_drift motion_param wm_av vent_av]); -labels_all = [repmat({'slow_drift'},[1 size(slow_drift,2)]) repmat({'motion'},[1 size(motion_param,2)]) {'wm_av','vent_av'}]; -opt_qc.test ='ftest'; - -%% F-test slow drift -if ~strcmp(files_out.qc_slow_drift,'gb_niak_omitted') if opt.flag_verbose - fprintf('Generate a F-test map for the slow time drifts ...\n') + fprintf('Adding high frequency noise...\n') end - model.c = ismember(labels_all,'slow_drift'); - if any(model.c) - res = niak_glm(model,opt_qc); - qc = reshape(res.ftest,size(mask_brain)); - else - qc = zeros(size(mask_brain)); - end - hdr_qc.file_name = files_out.qc_slow_drift; - niak_write_vol(hdr_qc,qc); -end - -%% F-test high frequencies -if ~strcmp(files_out.qc_high,'gb_niak_omitted') + x2 = [x2 motion_param]; + labels = [labels repmat({'motion'},[1 size(motion_param,2)])]; +else if opt.flag_verbose - fprintf('Generate a F-test map for the high frequencies ...\n') - end - model.c = ismember(labels_all,'high'); - if any(model.c) - res = niak_glm(model,opt_qc); - qc = reshape(res.ftest,size(mask_brain)); - else - qc = zeros(size(mask_brain)); + fprintf('Ignoring motion parameters...\n') end - hdr_qc.file_name = files_out.qc_high; - niak_write_vol(hdr_qc,qc); end -%% F-test white matter -if ~strcmp(files_out.qc_wm,'gb_niak_omitted') +%% Add white matter average +if opt.flag_wm if opt.flag_verbose - fprintf('Generate a F-test map for the average signal in the white matter ...\n') + fprintf('Adding white matter average...\n') end - model.c = ismember(labels_all,'wm_av'); - res = niak_glm(model,opt_qc); - hdr_qc.file_name = files_out.qc_wm; - niak_write_vol(hdr_qc,reshape(res.ftest,size(mask_brain))); -end - -%% F-test ventricles -if ~strcmp(files_out.qc_vent,'gb_niak_omitted') + mask_wm = strcmp(all_labels,'wm_avg'); + x2 = [x2 x(:,mask_wm)]; + labels = [labels all_labels(mask_wm)]; +else if opt.flag_verbose - fprintf('Generate a F-test map for the average signal in the ventricles ...\n') + fprintf('Ignoring white matter average...\n') end - model.c = ismember(labels_all,'vent_av'); - res = niak_glm(model,opt_qc); - hdr_qc.file_name = files_out.qc_vent; - niak_write_vol(hdr_qc,reshape(res.ftest,size(mask_brain))); end -%% F-test motion -if ~strcmp(files_out.qc_motion,'gb_niak_omitted') +%% Add ventricle average +if opt.flag_vent if opt.flag_verbose - fprintf('Generate a F-test map for the motion parameters ...\n') + fprintf('Adding ventricular average...\n') end - model.c = ismember(labels_all,'motion'); - res = niak_glm(model,opt_qc); - hdr_qc.file_name = files_out.qc_motion; - niak_write_vol(hdr_qc,reshape(res.ftest,size(mask_brain))); -end - -%% Regress confounds stage 1 (slow time drifts, average WM, vent, motion parameters) -if ~isempty(x) + mask_vent = strcmp(all_labels,'vent_avg'); + x2 = [x2 x(:,mask_vent)]; + labels = [labels all_labels(mask_vent)]; +else if opt.flag_verbose - fprintf('Regress the confounds stage 1 (slow time drifts, average WM, vent, motion parameters) ...\n') + fprintf('Ignoring ventricular average...\n') end - model.x = niak_normalize_tseries(x); - res = niak_glm(model,opt_glm); - y = res.e; - vol = reshape(y',[size(vol,1) size(vol,2) size(vol,3) size(y,1)]); end %% Add Global signal -if opt.flag_verbose - fprintf('Generate a PCA-based estimation of the global signal ...\n') -end -pc_spatial_av = sub_pc_spatial_av(vol,mask); - if opt.flag_gsc - x2 = pc_spatial_av; - labels2 = {'pc_spatial_av'}; + if opt.flag_verbose + fprintf('Adding global signal...\n') + end + mask_gs = strcmp(all_labels,'global_signal_pca'); + x2 = [x2 x(:,mask_gs)]; + labels = [labels all_labels(mask_gs)]; else - x2 = []; - labels2 = {}; + if opt.flag_verbose + fprintf('Ignoring global signal...\n') + end end %% Add Compcor -if opt.flag_verbose - fprintf('Generate a PCA-based estimation of components to regress in the WM/Vent/high std ...\n') -end -[x_comp,mask_comp] = niak_compcor(vol,opt.compcor,mask_wm|mask_vent); -nb_comp_max = floor((size(y,1)-size(x,2))/2); -x_comp = x_comp(:,1:min(size(x_comp,2),nb_comp_max)); - if opt.flag_compcor - x2 = [x2 x_comp]; - labels2 = [ labels2 repmat({'compcor'},[1 size(x_comp,2)]) ]; -end - -%% Custom parameters to be regressed -covar=[]; -if ~strcmp(files_in.custom_param,'gb_niak_omitted') if opt.flag_verbose - fprintf('Regress custom parameters ...\n') - end - covar = load(files_in.custom_param); - covar = covar.covar(~mask_scrubbing,:); - model_covar.y = covar; - model_covar.x = x; - res = niak_glm(model_covar,opt_glm); - covar = res.e; - - if ~isempty(covar) && (size(covar,1)==size(y,1)) - covar = niak_normalize_tseries(covar); - x2 = [x2 covar]; - labels2 = [ labels2 repmat({'custom'},[1 size(covar,2)]) ]; - else - error('The dimensions of the user-specified covariates are inappropriate (%i samples, functional datasets has %i time points)',size(covar,1),size(y,1)) + fprintf('Adding COMPCOR...\n') end + mask_compcor = strcmp(all_labels,'compcor'); + x2 = [x2 x(:,mask_compcor)]; + labels = [labels all_labels(mask_compcor)]; else - covar = []; -end - -%% F-TEST stage 2 (global signal + compcor + custom covariates) -model.y = y; -model.x = [pc_spatial_av x_comp covar]; -labels_all2 = [ repmat({'pc_spatial_av'},[1 size(pc_spatial_av,2)]) repmat({'compcor'},[1 size(x_comp,2)]) repmat({'custom'},[1 size(covar,2)]) ]; - -%% The global signal -if ~strcmp(files_out.qc_gse,'gb_niak_omitted') if opt.flag_verbose - fprintf('Generate a F-test map for the global signal estimate ...\n') + fprintf('Ignoring COMPCOR...\n') end - model.c = ismember(labels_all2,'pc_spatial_av'); - res = niak_glm(model,opt_qc); - hdr_qc.file_name = files_out.qc_gse; - niak_write_vol(hdr_qc,reshape(res.ftest,size(mask_brain))); end -%% The COMPCOR -if ~strcmp(files_out.qc_compcor,'gb_niak_omitted') +%% Add custom regressors +mask_manual = strcmp(all_labels,'manual'); +if any(mask_manual) if opt.flag_verbose - fprintf('Generate a F-test map for the COMPCOR estimate ...\n') - end - model.c = ismember(labels_all2,'compcor'); - res = niak_glm(model,opt_qc); - hdr_qc.file_name = files_out.qc_compcor; - niak_write_vol(hdr_qc,reshape(res.ftest,size(mask_brain))); + fprintf('Adding user-specified confounds...\n') + end + x2 = [x2 x(:,mask_manual)]; + labels = [labels all_labels(mask_manual)]; end -%% The custom covariates -if ~strcmp(files_out.qc_custom_param,'gb_niak_omitted') - if opt.flag_verbose - fprintf('Generate a F-test map for the custom covariates ...\n') - end - model.c = ismember(labels_all2,'custom'); - if any(model.c) - res = niak_glm(model,opt_qc); - qc = reshape(res.ftest,size(mask_brain)); - else - qc = zeros(size(mask_brain)); - end - hdr_qc.file_name = files_out.qc_custom_param; - niak_write_vol(hdr_qc,qc); -end - -%% Regress confounds stage 2 +%% Regress confounds if ~isempty(x2) if opt.flag_verbose - fprintf('Regress the confounds stage 2 (global signal + custom covariates)...\n') + fprintf('Regressing the confounds...\n Total number of confounds: %i\n Total number of time points for regression: %i\n',size(x,2),sum(~mask_scrubbing)) end - [tmp,reg] = niak_lse(x2,x); - model.x=reg; - res = niak_glm(model,opt_glm); - y = res.e; -end -y = y + repmat(mean_y,[size(y,1) 1]); % put the mean back in the time series -vol_denoised = reshape(y',size(vol)); + + %% Normalize data + y_mean = mean(y(~mask_scrubbing,:),1); % Exclude time points with excessive motion to estimate mean/std + y_std = std(y(~mask_scrubbing,:),[],1); + y = (y-repmat(y_mean,[size(y,1),1]))./repmat(y_std,[size(y,1) 1]); + x2_mean = mean(x2(~mask_scrubbing,:),1); % Exclude time points with excessive motion to estimate mean/std + x2_std = std(x2(~mask_scrubbing,:),[],1); + x2 = (x2-repmat(x2_mean,[size(x2,1),1]))./repmat(x2_std,[size(x2,1) 1]); + + %% Run the regression + model.y = y(~mask_scrubbing,:); + model.x = x2(~mask_scrubbing,:); + opt_glm.flag_beta = true; + res = niak_glm(model,opt_glm); % Run the regression excluding time points with excessive motion + y = y - x2*res.beta; % Generate the residuals for all time points combined + y = y + repmat(y_mean,[size(y,1) 1]); % put the mean back in the time series + vol_denoised = reshape(y',size(vol)); + +else + warning('Found no confounds to regress! Leaving the dataset as is') + vol_denoised = vol; + +end + %% Save the fMRI dataset after regressing out the confounds if ~strcmp(files_out.filtered_data,'gb_niak_omitted') if opt.flag_verbose @@ -627,62 +345,13 @@ hdr_vol.file_name = files_out.filtered_data; if isfield(hdr_vol,'extra') % Store the regression covariates in the extra .mat companion that comes with the 3D+t dataset - hdr_vol.extra.confounds = [x x2]; - hdr_vol.extra.labels_confounds = [labels(:) ; labels2(:)]; + hdr_vol.extra.confounds = x2; + hdr_vol.extra.labels_confounds = labels(:); end niak_write_vol(hdr_vol,vol_denoised); end -%% Save the COMPCOR mask -if ~strcmp(files_out.compcor_mask,'gb_niak_omitted') - hdr_vol.file_name = files_out.compcor_mask; - if isfield(hdr_vol,'extra') - hdr_vol = rmfield(hdr_vol,'extra'); - end - niak_write_vol(hdr_vol,mask_comp); -end - -%% Merge all the flags into one structure -flags.compcor = opt.flag_compcor; -flags.gsc = opt.flag_gsc; -flags.motion_params = opt.flag_motion_params; -flags.pca_motion = opt.flag_pca_motion; -flags.scrubbing = opt.flag_scrubbing; -flags.slow = opt.flag_slow; -flags.vent = opt.flag_vent; -flags.wm = opt.flag_wm; - -%% Save the confounds -if ~strcmp(files_out.confounds,'gb_niak_omitted') - save(files_out.confounds, 'x' , 'x2' , 'labels' , 'labels2' , 'slow_drift' , 'motion_param' , 'wm_av' , 'vent_av' , 'pc_spatial_av' , 'covar','flags'); -end - %% Save the scrubbing parameters if ~strcmp(files_out.scrubbing,'gb_niak_omitted') save(files_out.scrubbing,'mask_scrubbing','fd'); -end - -%%%%%%%%%%%%%%%%%% -%% SUBFUNCTIONS %% -%%%%%%%%%%%%%%%%%% - -function pc_spatial_av = sub_pc_spatial_av(vol,mask) -%% global signal estimation using a combination of the global average (target) and PCA (explanatory variables) -% Coded after: -% F. Carbonell, P. Bellec, A. Shmuel. Validation of a superposition model -% of global and system-specific resting state activity reveals anti-correlated -% networks. To appear in Brain Connectivity. - -% PCA -tseries = niak_vol2tseries(vol,mask); -[eigenvalues,eigenvariates,weights] = niak_pca(tseries'); - -% Spatial Average -spatial_av = niak_normalize_tseries(mean(tseries,2)); -eigenvariates = niak_normalize_tseries(eigenvariates); - -% Determine PC to be removed -r = (1/(length(spatial_av)-1))*(spatial_av'*eigenvariates); -[coeff_av,ind_pca] = max(abs(r)); -pc_spatial_av = eigenvariates(:,ind_pca); -pc_spatial_av = pc_spatial_av*sign((pc_spatial_av'*spatial_av)); +end \ No newline at end of file diff --git a/bricks/fmri_preprocess/niak_brick_resample_simple.m b/bricks/fmri_preprocess/niak_brick_resample_simple.m new file mode 100644 index 00000000..c1f5a997 --- /dev/null +++ b/bricks/fmri_preprocess/niak_brick_resample_simple.m @@ -0,0 +1,123 @@ +function [in,out,opt] = niak_brick_resample_simple(in,out,opt) +% Resample a 3D/4D volume into a target space using nearest neighbour interpolation. +% No transformation supported. +% +% [FILES_IN,FILES_OUT,OPT] = NIAK_BRICK_RESAMPLE_SIMPLE(FILES_IN,FILES_OUT,OPT) +% +% _________________________________________________________________________ +% INPUTS: +% +% FILES_IN +% (structure) with the following fields : +% +% SOURCE +% (string) name of the file to resample (can be 3D+t). +% +% TARGET +% (string, default none) name of the file defining space. +% +% FILES_OUT +% (string) the name of the output resampled volume. +% +% OPT +% (structure, optional) has the following fields: +% +% FLAG_TEST +% (boolean, default: 0) if FLAG_TEST equals 1, the brick does not +% do anything but update the default values in FILES_IN and +% FILES_OUT. +% +% FLAG_VERBOSE +% (boolean, default 1) if the flag is 1, then the function prints +% some infos during the processing. +% +% +% _________________________________________________________________________ +% OUTPUTS: +% +% The structures FILES_IN, FILES_OUT and OPT are updated with default +% values. If OPT.FLAG_TEST == 0, the specified outputs are written. +% +% _________________________________________________________________________ +% COMMENTS: +% +% This implementation is based purely on Matlab/Octave code, and currently +% only supports nearest_neighbour interpolation. +% +% Input/output can be minc or nifti (any variant), and on-the-flight conversion +% between formats is also supported (e.g. the input can be minc and the output +% nifti). +% +% Copyright (c) Pierre Bellec +% Centre de recherche de l'institut de geriatrie de Montreal, +% Department of Computer Science and Operations Research +% University of Montreal, Quebec, Canada, 2016 +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords : medical imaging, resampling + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +% Setting up inputs +in = psom_struct_defaults(in, ... + {'source', 'target' }, ... + {NaN , '' }); + +% Cheking outputs +if ~ischar(out) + error('OUT should be a string') +end + +% Setting up options +if nargin < 3 + opt = struct(); +end +opt = psom_struct_defaults(opt, ... + { 'flag_test' , 'flag_verbose' }, ..., + { false , true }); + +if opt.flag_test == 1 + return +end + +%% Reading source +if opt.flag_verbose + fprintf('Reading source volume %s...\n',in.source); +end +[hdr.source,vol] = niak_read_vol(in.source); + +%% Reading target +if isempty(in.target) + if opt.flag_verbose + fprintf('No target is specified, will resample data on itself\n') + end + hdr.target = []; +else + if opt.flag_verbose + fprintf('Reading target volume %s...\n',in.target); + end + hdr.target = niak_read_vol(in.target); +end + +%% Resampling +vol_r = niak_resample_vol(hdr,vol); + +%% Write results +hdr.target.file_name = out; +niak_write_vol(hdr.target,vol_r); diff --git a/bricks/fmri_preprocess/niak_brick_resample_vol.m b/bricks/fmri_preprocess/niak_brick_resample_vol.m index 0cc6b581..ff1be967 100644 --- a/bricks/fmri_preprocess/niak_brick_resample_vol.m +++ b/bricks/fmri_preprocess/niak_brick_resample_vol.m @@ -121,9 +121,9 @@ % % Copyright (c) Pierre Bellec, McConnell Brain Imaging Center, % Montreal Neurological Institute, 2008-2010 -% Centre de recherche de l'institut de gériatrie de Montréal, +% Centre de recherche de l'institut de griatrie de Montral, % Department of Computer Science and Operations Research -% University of Montreal, Québec, Canada, 2010-2014 +% University of Montreal, Qubec, Canada, 2010-2014 % Maintainer : pierre.bellec@criugm.qc.ca % See licensing information in the code. % Keywords : medical imaging, minc, resampling @@ -213,6 +213,49 @@ return end +%%%%%%%%%%%%%%%%%%%%% +%% Checking format %% +%%%%%%%%%%%%%%%%%%%%% +[path_s,name_s,ext_s] = niak_fileparts(files_in.source); +[path_t,name_t,ext_t] = niak_fileparts(files_in.target); +[path_o,name_o,ext_o] = niak_fileparts(files_out); +flag_conv = false; +path_tmp = niak_path_tmp(['_' name_o]); +if ~ismember(ext_s,{'.mnc','.mnc.gz'}) + flag_conv = true; + in_source = [path_tmp 'source.mnc']; + niak_brick_copy(files_in.source,in_source,struct('flag_fmri',true)); +else + in_source = files_in.source; +end +if ~ismember(ext_t,{'.mnc','.mnc.gz'}) + flag_conv = true; + in_target = [path_tmp 'target.mnc']; + niak_brick_copy(files_in.target,in_target,struct('flag_fmri',true)); +else + in_target = files_in.target; +end +flag_conv_out = false; +if ~ismember(ext_o,{'.mnc','.mnc.gz'}) + flag_conv = true; + flag_conv_out = true; + tmp_out = [path_tmp 'resample.mnc']; +else + tmp_out = files_out; +end + +if flag_conv + files_in_tmp = files_in; + files_in_tmp.source = in_source; + files_in_tmp.target = in_target; + niak_brick_resample_vol(files_in_tmp,tmp_out,opt); + if flag_conv_out + niak_brick_copy(tmp_out,files_out,struct('flag_fmri',true)); + end + psom_clean(path_tmp); + return +end + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Convert the transformation %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/bricks/fmri_preprocess/niak_brick_slice_timing.m b/bricks/fmri_preprocess/niak_brick_slice_timing.m index 69c81607..87257fb1 100644 --- a/bricks/fmri_preprocess/niak_brick_slice_timing.m +++ b/bricks/fmri_preprocess/niak_brick_slice_timing.m @@ -218,7 +218,11 @@ % Montreal Neurological Institute, 2008-2010 % Centre de recherche de l'institut de geriatrie de Montreal, % Department of Computer Science and Operations Research +<<<<<<< HEAD % University of Montreal, Qubec, Canada, 2010-2014 +======= +% University of Montreal, Qubec, Canada, 2010-2016 +>>>>>>> master % Maintainer : pierre.bellec@criugm.qc.ca % See licensing information in the code. % Keywords : medical imaging, slice timing, fMRI diff --git a/bricks/misc/niak_brick_copy.m b/bricks/misc/niak_brick_copy.m index 92f795ce..c5d8e166 100644 --- a/bricks/misc/niak_brick_copy.m +++ b/bricks/misc/niak_brick_copy.m @@ -11,7 +11,7 @@ % (cell of strings) a list of file names % % FILES_OUT -% (cell of strings, default OPT.FOLDER_OUT/_copy.) +% (cell of strings, default OPT.FOLDER_OUT/(NAME_FILES_IN)) % File name for outputs. If FILES_OUT is an empty string, the default % name is generated. % @@ -24,7 +24,7 @@ % along with fMRI datasets. % % FOLDER_OUT -% (string, default: same as FILES_IN{1}) If present, all default +% (string, default: folder of FILES_IN{1}) If present, all default % outputs will be created in the folder FOLDER_OUT. The folder % needs to be created beforehand. % @@ -46,6 +46,9 @@ % _________________________________________________________________________ % COMMENTS % +% FILES_IN and FILES_OUT can also be passed as strings, in which case a single +% files is copied. +% % _________________________________________________________________________ % Copyright (c) Pierre Bellec, Montreal Neurological Institute, 2008. % Maintainer : pbellec@bic.mni.mcgill.ca @@ -80,6 +83,16 @@ error('niak:brick','syntax: [FILES_IN,FILES_OUT,OPT] = NIAK_BRICK_COPY(FILES_IN,FILES_OUT,OPT).\n Type ''help niak_brick_copy'' for more info.') end +%% Check inputs +if ischar(files_in)&&ischar(files_out) + files_in = {files_in}; + files_out = {files_out}; +end + +if ~iscellstr(files_in)||~iscellstr(files_out) + error('FILES_IN and FILES_OUT need to be both either strings or cell of strings') +end + %% Options gb_name_structure = 'opt'; gb_list_fields = {'flag_fmri' , 'flag_verbose' , 'flag_test' , 'folder_out' }; @@ -87,7 +100,6 @@ niak_set_defaults %% Output files - [path_f,name_f,ext_f] = fileparts(files_in{1}); if isempty(path_f) path_f = '.'; @@ -101,6 +113,7 @@ if strcmp(opt.folder_out,'') opt.folder_out = path_f; end +opt.folder_out = niak_full_path(opt.folder_out); %% Building default output names nb_files = length(files_in); @@ -111,7 +124,7 @@ for num_f = 1:nb_files [path_f,name_f,ext_f] = niak_fileparts(files_in{num_f}); - files_out{num_f} = cat(2,opt.folder_out,filesep,name_f,'_copy',ext_f); + files_out{num_f} = cat(2,opt.folder_out,name_f,ext_f); end end @@ -127,7 +140,7 @@ for num_f = 1:nb_files if flag_verbose - msg = sprintf('Copying file %s to %s',files_in{num_f},files_out{num_f}); + msg = sprintf('Copying or converting file %s to %s',files_in{num_f},files_out{num_f}); fprintf('%s\n',msg); end if opt.flag_fmri diff --git a/bricks/misc/niak_brick_mnc2nii.m b/bricks/misc/niak_brick_mnc2nii.m index 3ea114c1..96fcbf23 100644 --- a/bricks/misc/niak_brick_mnc2nii.m +++ b/bricks/misc/niak_brick_mnc2nii.m @@ -29,6 +29,10 @@ % can be changed by editing the variable GB_NIAK_ZIP in the file % NIAK_GB_VARS. % +% FLAG_TEST +% (boolean, default false) if FLAG_TEST is true, the brick only +% updates FILES_IN, FILES_OUT and OPT, and does nothing else. +% % FLAG_VERBOSE % (boolean, default 1) if the flag is 1, then the function prints % some infos during the processing. @@ -86,10 +90,13 @@ %% Options gb_name_structure = 'opt'; -gb_list_fields = { 'flag_zip' , 'flag_recursive' , 'flag_verbose' , 'arg_mnc2nii' }; -gb_list_defaults = { true , true , true , '' }; +gb_list_fields = { 'flag_test' , 'flag_zip' , 'flag_recursive' , 'flag_verbose' , 'arg_mnc2nii' }; +gb_list_defaults = { false , true , true , true , '' }; niak_set_defaults +if opt.flag_test + return +end dir_files = dir(files_in); mask_dir = [dir_files.isdir]; diff --git a/bricks/misc/niak_brick_montage.m b/bricks/misc/niak_brick_montage.m new file mode 100644 index 00000000..137e0ac6 --- /dev/null +++ b/bricks/misc/niak_brick_montage.m @@ -0,0 +1,201 @@ +function [in,out,opt] = niak_brick_montage(in,out,opt) +% Generate a figure with a montage of sagital slices in the volume +% +% SYNTAX: [IN,OUT,OPT] = NIAK_BRICK_VOL2IMG(IN,OUT,OPT) +% +% IN.SOURCE (string) the file name of a 3D volume +% IN.TARGET (string, default '') the file name of a 3D volume defining the target space. +% If left empty, or unspecified, OUT is the world space associated with IN.SOURCE +% i.e. the volume is resamples to have no direction cosines. +% OUT.MONTAGE (string) the file name for the figure. The extension will determine the type. +% OUT.COLORMAP (string) the file name for a figure with the color map. +% OUT.QUANTIZATION (string) the file name for a .mat file with a variable DATA. +% DATA(N) is the data point associated with the Nth color. +% OPT.NB_SLICES (scalar, default Inf) the number of slices to produce (with a parameter +% Inf, all possible slices will be generated). +% OPT.COLORMAP (string, default 'gray') The type of colormap. Anything supported by +% the instruction `colormap` will work, as well as 'hot_cold' (see niak_hot_cold). +% This last color map always centers on zero. +% OPT.NB_COLOR (default 256) the number of colors to use in quantization. If Inf is +% specified, all values are included in the colormap. This is handy for integer +% values images (e.g. parcellation). +% OPT.QUALITY (default 90) for jpg images, set the quality of the outputs (from 0, bad, to 100, perfect). +% OPT.THRESH (scalar or vector, default []) if empty, does nothing. If a scalar, any value +% below threshold becomes transparent. If two values, any values that falls between the +% bounds become transparent. +% OPT.LIMITS (vector 1x2) the limits for the colormap. By defaut it is using [min,max]. +% If a string is specified, the function will implement an adaptative strategy. +% OPT.FLAG_TEST (boolean, default false) if the flag is true, the brick does nothing but +% update IN, OUT and OPT. +% +% The montage is generated in voxel space associated with the target. If no target is specified, +% the source space is resampled with direction cosines, and the field of view is adjusted +% such that it includes all of the voxels. Only nearest neighbour interpolation is +% available. For 4D data, the median volume is extracted. +% +% If OUT is a string, only OUT.MONTAGE is generated. +% +% Copyright (c) Pierre Bellec +% Centre de recherche de l'Institut universitaire de griatrie de Montral, 2016. +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords : visualization, montage, 3D brain volumes + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +%% Defaults +in = psom_struct_defaults( in , ... + { 'source' , 'target' }, ... + { NaN , '' }); + +if nargin < 3 + opt = struct; +end + +if ischar(out) + out = struct('montage',out); +end + +out = psom_struct_defaults( out , ... + { 'montage' , 'colormap' , 'quantization' }, ... + { NaN , 'gb_niak_omitted' , 'gb_niak_omitted' }); + +opt = psom_struct_defaults ( opt , ... + { 'nb_color' , 'quality' , 'thresh' , 'nb_slices' , 'type_view' , 'limits' , 'colormap' , 'flag_test' }, ... + { 256 , 90 , [] , Inf , 'sagital' , '' , 'gray' , false }); + +if opt.flag_test + return +end + +%% Read headers +[hdr.source,vol] = niak_read_vol(in.source); +if ~isempty(in.target) + hdr.target = niak_read_vol(in.target); +else + hdr.target = ''; +end +if ndims(vol)==4 + vol = median(vol,4); +end + +%% resample volume +vol_r = niak_resample_vol(hdr,vol); + +%% Build montage +dim_v = size(vol_r); +switch opt.type_view +case 'sagital' + vol_rf = zeros(dim_v(1),dim_v(3),dim_v(2)); + for xx = 1:dim_v(1) + vol_rf(xx,:,:) = niak_flip_vol(squeeze(vol_r(xx,:,:)),'rot90'); + end + dim_v = size(vol_rf); + npix = sqrt(prod(dim_v)); + wy = floor(npix/dim_v(3)); + wx = ceil(prod(dim_v)/(wy*dim_v(3)*dim_v(2))); + img = zeros([wx*dim_v(2) wy*dim_v(3)]); + ss = 1; + for xx = 1:wx + for yy = 1:wy + indx_l = (xx-1)*dim_v(2)+1; + indx_h = xx*dim_v(2); + indy_l = (yy-1)*dim_v(3)+1; + indy_h = yy*dim_v(3); + if ss<=size(vol_r,1) + img(indx_l:indx_h,indy_l:indy_h) = squeeze(vol_rf(ss,:,:)); + ss = ss + 1; + end + end + end +otherwise + error('Only sagital slices are supported') +end + +%% image limits +if ischar(opt.limits) + mask = niak_mask_brain(vol); + mvol = median(vol(mask)); + svol = niak_mad(vol(mask)); + climits = [0 mvol+2*svol]; + opt.limits = climits; +end + +if isempty(opt.limits) + opt.limits = [min(img(:)) max(img(:))]; +end +climits = opt.limits; + +%% Generate colormap +img(img>climits(2)) = climits(2); +img(img0) && (opt.limits(1)<0) + per_hot = opt.limits(2)/(opt.limits(2)-opt.limits(1)); + elseif opt.limits(2)<=0 + per_hot = 0; + else + per_hot = 1; + end + cm = niak_hot_cold(opt.nb_color,per_hot); + case 'gray' + cm = gray(opt.nb_color); + case 'jet' + cm = jet(opt.nb_color); + otherwise + error('only jet, gray and hot_cold color maps currently supported') +end + +%% build the image +[tmp,idx] = histc(img,bins); +idx(idx==0) = 1; +rgb = zeros([size(img),3]); +rgb(:,:,1) = reshape(cm(idx(:),1),size(img)); +rgb(:,:,2) = reshape(cm(idx(:),2),size(img)); +rgb(:,:,3) = reshape(cm(idx(:),3),size(img)); +if ~isempty(opt.thresh) + if length(opt.thresh)==1 + opt.thresh = [-Inf opt.thresh]; + end + imwrite(rgb,out.montage,'quality',opt.quality,'Alpha',double((img>opt.thresh(2))|(imgclimits(2)) = climits(2); + img(imgopt.thre); +for pp = 1:length(list_peak) + peak = list_peak(pp); + mask_before = (time_frames(peak)>time_frames)&(time_frames>=time_frames(peak)-opt.ww(1)); + if isempty(mask_before)&&(peak>1) + mask_before(peak-1) = true; + end + mask_after = (time_frames(peak)=opt.nb_min_vol + mask = mask|mask_scrub; + else + warning('There was not enough time frames left after scrubbing, kept %i time frames. See OPT.NB_VOL_MIN.',sum(~mask)) + break + end +end \ No newline at end of file diff --git a/commands/fir/niak_brick_fir.m b/commands/fir/niak_brick_fir.m index f4d657da..d9c49492 100644 --- a/commands/fir/niak_brick_fir.m +++ b/commands/fir/niak_brick_fir.m @@ -191,13 +191,22 @@ % Read the 3D+t dataset [hdr,vol] = niak_read_vol(files_in.fmri{num_r}); + % scrubbing + if isfield(hdr,'extra')&&isfield(hdr.extra,'mask_scrubbing') + mask_scrubbing = hdr.extra.mask_scrubbing; + else + mask_scrubbing = false(size(vol,4),1); + end + % Read the time frames if isfield(hdr,'extra') opt_fir.time_frames = hdr.extra.time_frames; else opt_fir.time_frames = (0:(size(vol,4)-1))*hdr.info.tr; end + opt_fir.time_frames = opt_fir.time_frames(~mask_scrubbing); tseries = niak_vol2tseries(vol,mask); + tseries = tseries(~mask_scrubbing,:); % Read the event times [time_events,labels_conditions] = niak_read_csv(files_in.timing{num_r}); diff --git a/commands/fmristat/niak_model_select.m b/commands/fmristat/niak_model_select.m index b4100082..abbcf5ff 100644 --- a/commands/fmristat/niak_model_select.m +++ b/commands/fmristat/niak_model_select.m @@ -1,4 +1,4 @@ -function [labels_x,ind_x] = niak_model_select (model, opt) +function [labels_x,ind_x,model_select] = niak_model_select (model, opt) % Select entries in a linear model % [LABELS_X,IND_X] = NIAK_MODEL_SELECT( MODEL , OPT ) % @@ -28,6 +28,8 @@ % LABELS_X (cell of strings) same as MODEL.LABELS_X, but filtered and re-ordered based % on OPT. % IND_X (vector) the indices of extracted rows, i.e. LABELS_X = MODEL.LABELS_X(IND_X); +% MODEL_SELECT (array) same as MODEL.X, but filtered and re-ordered based +% on OPT. % % COMMENTS: % In the selection process, if several covariates are associated with OPT.SELECT.LABEL, @@ -173,11 +175,14 @@ %% Filter out the NaN entries if (opt.flag_filter_nan)&&~isempty(model.x) - mask_nan = max(isnan(model.x),[],2); + mask_nan = max(isnan(model.x),[],2); if any(mask_nan) warning('The following entries were suppressed because they were associated to NaNs') char(model.labels_x{mask_nan}) end labels_x = labels_x(~mask_nan); ind_x = ind_x(~mask_nan); -end \ No newline at end of file + model.x = model.x(~mask_nan, :); +end + +model_select = model.x; \ No newline at end of file diff --git a/commands/fmristat/niak_normalize_model.m b/commands/fmristat/niak_normalize_model.m index 9be73452..4881e712 100644 --- a/commands/fmristat/niak_normalize_model.m +++ b/commands/fmristat/niak_normalize_model.m @@ -242,6 +242,8 @@ ind = find(mask == 1); if length(ind)>1 error('Attempt to define an interaction term using the label %s, which is associated with more than one covariate',factor) + elseif isempty(ind) + warning('Attempt to define an interaction term using the label %s, but it is not a covariate in FILES_IN.MODEL',factor) end % Optional : normalisation of the covariate, which is involved in this interaction, BEFORE building the crossproduct if isfield(opt.interaction(num_i),'flag_normalize_inter') && ~opt.interaction(num_i).flag_normalize_inter diff --git a/commands/formats/niak_transf2param.m b/commands/formats/niak_transf2param.m index 87567f04..5ea4e6cb 100644 --- a/commands/formats/niak_transf2param.m +++ b/commands/formats/niak_transf2param.m @@ -3,42 +3,27 @@ % 4*4 matrix array (y=M*x+T) to the x/y/z rotation and translation % parameters. % -% SYNTAX: -% [ROT,TSL] = NIAK_TRANSF2PARAM(TRANSF) +% SYNTAX: [ROT,TSL] = NIAK_TRANSF2PARAM(TRANSF) % -% _________________________________________________________________________ -% INPUT: -% -% TRANSF -% (4*4*N array) TRANSF(:,:,n) is an lsq6 transformation, usually seen -% as a "voxel-to-world" space transform. -% -% _________________________________________________________________________ -% OUTPUTS: -% -% ROT -% (array 3*N) the rotation parameters (in x, y and z planes). -% Unit is degrees. -% +% TRANSF (4*4*N array) TRANSF(:,:,n) is an lsq6 transformation, usually seen +% as a "voxel-to-world" space transform. +% ROT (array 3*N) the rotation parameters (in x, y and z planes). +% Unit is degrees. % TSL -% (array 3*N) the translation parameters. -% -% _________________________________________________________________________ -% COMMENTS: +% (array 3*N) the translation parameters. Unit is consistent with the units of +% TRANSF (generally mm). % -% This code was written by Giampiero Campa, -% PhD, Research Assistant Professor -% West Virginia University, Aerospace Engineering Dept. +% This code was written by Giampiero Campa, PhD, Research Assistant Professor +% West Virginia University, Aerospace Engineering Dept. % Morgantown, WV, 26506-6106, USA, Copyright 1/11/96 -% See -% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId= -% 956&objectType=File +% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=956&objectType=File % % Modified by Pierre Bellec, McConnel Brain Imaging Center, Montreal % Neurological Institute, McGill University, Montreal, Canada, 2008. -% Changing inputs/outputs formats. -% Maintainer : pbellec@bic.mni.mcgill.ca -% See licensing information in the code. +% Centre de recherche de l'institut de geriatrie de Montreal, +% Department of Computer Science and Operations Research +% University of Montreal, Qubec, Canada, 2008-2016 +% Maintainer : pierre.bellec@criugm.qc.ca % Keywords : rigid-body transformation, rotation, Euler angles % % See licensing information in the code diff --git a/commands/misc/niak_gb_vars.m b/commands/misc/niak_gb_vars.m index 1b5b5c40..d9291402 100644 --- a/commands/misc/niak_gb_vars.m +++ b/commands/misc/niak_gb_vars.m @@ -54,7 +54,7 @@ % All niak var that has an equivalent in psom should be assigned in % this if block if ~exist('gb_psom_gb_vars','var')&&exist('psom_gb_vars.m','file') - gb_psom_gb_var = true; + gb_psom_gb_vars = true; psom_gb_vars end @@ -92,7 +92,6 @@ elseif ~isempty(findstr('linux',comp)) gb_niak_OS = 'unix'; else - warning('System %s unknown!\n',comp); gb_niak_OS = 'unkown'; end @@ -147,7 +146,7 @@ gb_niak_version = 'dev'; %% Target for tests -gb_niak_target_test = '0.13.6'; +gb_niak_target_test = '0.14.0'; %% In which path is NIAK ? str_read_vol = which('niak_read_vol'); diff --git a/commands/misc/niak_wget.m b/commands/misc/niak_wget.m index 2c963d37..358983d6 100644 --- a/commands/misc/niak_wget.m +++ b/commands/misc/niak_wget.m @@ -4,9 +4,12 @@ % SYNTAX: [STATUS,MSG,DATA] = NIAK_WGET(DATA) % % DATA.TYPE (string, default '') If provided, this sets the default for datasets -% 'data_test_niak_mnc1': The small version of the demoniak dataset. +% 'data_test_niak_mnc1': The small version of the demoniak dataset (minc 1 format). +% 'data_test_niak_nii': The small version of the demoniak dataset (nifti format). % 'target_test_niak_mnc1': The results of all NIAK pipelines on the demoniak minc1 data. % 'single_subject_cambridge_preprocessed_nii': preprocessed data for a single subject. +% 'cambridge_template_nii': The Cambridge BASC multiresolution parcels (nifti format). +% 'cambridge_template_mnc': The Cambridge BASC multiresolution parcels (minc format). % DATA.URL (string, see DATA.TYPE for default) the url to get the dataset. % DATA.NAME (string, see DATA.TYPE for default) the name of the file to retrieve. % DATA.PATH (string, [pwd filesep DATA.NAME], without the extension of the name) @@ -62,13 +65,16 @@ case 'data_test_niak_mnc1' data.name = 'data_test_niak_mnc1.zip'; data.url = ['http://www.nitrc.org/frs/download.php/7241/' data.name]; + case 'data_test_niak_nii' + data.name = 'data_test_niak_nii.zip'; + data.url = ['http://www.nitrc.org/frs/download.php/9120/' data.name]; case 'target_test_niak_mnc1' data.name = ['target_test_niak_mnc1-' gb_niak_target_test '.zip'] data.url = ['https://github.com/simexp/niak_target/archive/' data.name]; case 'single_subject_cambridge_preprocessed_nii' data.name = 'single_subject_cambridge_preprocessed_nii.zip'; data.url = 'http://www.nitrc.org/frs/download.php/6784/single_subject_cambridge_preprocessed_nii.zip'; - case 'cambridge_template_mnc' + case 'cambridge_template_mnc1' data.name = 'template_cambridge_basc_multiscale_mnc_sym.zip'; data.url = 'http://files.figshare.com/1861821/template_cambridge_basc_multiscale_mnc_sym.zip'; case 'cambridge_template_nii' diff --git a/commands/read_write/niak_cp_fmri.m b/commands/read_write/niak_cp_fmri.m index f389a8c0..925daf4f 100644 --- a/commands/read_write/niak_cp_fmri.m +++ b/commands/read_write/niak_cp_fmri.m @@ -34,7 +34,7 @@ % Copyright (c) Pierre Bellec, % Research Centre of the Montreal Geriatric Institute % & Department of Computer Science and Operations Research -% University of Montreal, Québec, Canada, 2012. +% University of Montreal, Qubec, Canada, 2012. % Maintainer : pierre.bellec@criugm.qc.ca % See licensing information in the code. % Keywords : fMRI @@ -56,19 +56,32 @@ % LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN % THE SOFTWARE. + +% Check if this is an omitted file +if strcmp(source,'gb_niak_omitted') + return +end + +% Parse folder information source = niak_full_file(source); target = niak_full_file(target); +[path_s,name_s,ext_s] = niak_fileparts(source); +[path_t,name_t,ext_t] = niak_fileparts(target); -instr_copy = cat(2,'cp ',source,' ',target); - -[status,msg] = system(instr_copy); -if status~=0 - error(msg) -end +% Copy or convert, if necessary +if strcmp(ext_s,ext_t) + instr_copy = cat(2,'cp ',source,' ',target); + [status,msg] = system(instr_copy); + if status~=0 + error(msg) + end +else + [hdr,vol] = niak_read_vol(source); + hdr.file_name = target; + niak_write_vol(hdr,vol); +end % Copy the "extra" file, if present -[path_s,name_s,ext_s] = niak_fileparts(source); -[path_t,name_t,ext_t] = niak_fileparts(target); file_extra_s = [path_s filesep name_s '_extra.mat']; file_extra_t = [path_t filesep name_t '_extra.mat']; if psom_exist(file_extra_s) diff --git a/commands/read_write/niak_grab_all_preprocess.m b/commands/read_write/niak_grab_all_preprocess.m index 7c2a2482..2d96318a 100644 --- a/commands/read_write/niak_grab_all_preprocess.m +++ b/commands/read_write/niak_grab_all_preprocess.m @@ -39,8 +39,9 @@ % reproducibility tests across NIAK versions and production sites. % % The grabber will build a fairly exhaustive list of outputs. -% If FILES_IN is specified, the list can build even if PATH_DATA does not -% exist. Otherwise, a limited number of outputs actually need to be present +% If FILES_IN is specified, or a file `pipe_parameters.mat` can be found in +% PATH_DATA, the list will build even if PATH_DATA does not contain all expected +% outputs. Otherwise, a limited number of outputs actually need to be present % for the list to build: % * The individual subfolders in the 'quality_control' folder. % * The qc_scrubbing_group.csv file in 'quality_control/group_motion' @@ -80,11 +81,21 @@ end if nargin<2 - files_in = struct(); - flag_in = false; - ext_f = ''; + file_params = [path_data 'pipe_parameters.mat']; + if psom_exist(file_params) + params = load(file_params); + files_in = params.files_in; + flag_in = true; + else + files_in = struct(); + flag_in = false; + ext = ''; + end else flag_in = true; +end + +if flag_in [files_c,label] = niak_fmri2cell(files_in); [path_tmp,name_tmp,ext] = niak_fileparts(files_c{1}); end @@ -96,8 +107,9 @@ path_qc = [path_data 'quality_control' filesep]; path_fmri = [path_data 'fmri' filesep]; path_inter = [path_data 'intermediate' filesep]; +path_res = [path_data 'resample' filesep]; -if ~flag_in&&(~exist(path_anat,'dir')||~exist(path_qc,'dir')||~exist(path_fmri,'dir')||~exist(path_inter,'dir')) +if ~flag_in&&(~exist(path_anat,'dir')||~exist(path_qc,'dir')||~exist(path_fmri,'dir')||~exist(path_inter,'dir')||~exist(path_res,'dir')) warning('The specified folder does not contain some expected outputs from the fMRI preprocess (anat ; quality_control ; fmri ; intermediate)') end @@ -171,6 +183,14 @@ end end +%% Grab the parameters +files.params = [ path_data 'pipe_parameters.mat']; + +%% Grab the templates +files.template.anat = [path_anat 'template_anat_stereo' ext]; +files.template.fmri = [path_anat 'template_fmri_stereo' ext]; +files.template.aal = [path_anat 'template_aal' ext]; + %% Grab the preprocessed anat datasets for num_s = 1:length(list_subject) subject = list_subject{num_s}; @@ -193,7 +213,11 @@ list_func = { 'mask_stereonl' , ... 'mean_stereonl' , ... - 'std_stereonl'}; + 'std_stereonl' , ... + 'mask_stem_stereo' , ... + 'mask_vent_stereo' , ... + 'mask_wm_stereo' + }; list_transf = { 'nativefunc_to_stereolin' , ... 'nativefunc_to_stereonl' , ... 'nativet1_to_stereolin' , ... @@ -213,19 +237,6 @@ files.anat.(subject).stereolin_to_stereonl_grid = [path_anat_subj 'transf_' subject '_stereolin_to_stereonl_grid.mnc']; end -%% Grab the AAL template -files.template_aal = [path_anat 'template_aal.mnc.gz']; - -%% Grab the results of quality control -- Group confounds -list_conf = { 'gse' , 'high' , 'motion' , 'slow_drift' , 'vent' , 'wm' }; -for num_f = 1:length(list_conf) - conf = list_conf{num_f}; - files.quality_control.group_confounds.(conf).pdf = [path_qc 'group_confounds' filesep 'func_qc_' conf '_stereonl_fit.pdf']; - files.quality_control.group_confounds.(conf).csv = [path_qc 'group_confounds' filesep 'func_qc_' conf '_stereonl_fit.csv']; - files.quality_control.group_confounds.(conf).mean = [path_qc 'group_confounds' filesep 'func_qc_' conf '_stereonl_mean' ext]; - files.quality_control.group_confounds.(conf).std = [path_qc 'group_confounds' filesep 'func_qc_' conf '_stereonl_std' ext]; -end - %% Grab the results of quality control -- Group coregistration files.quality_control.group_coregistration.anat.stereolin.pdf = [path_qc 'group_coregistration' filesep 'anat_fig_qc_coregister_stereolin.pdf' ]; files.quality_control.group_coregistration.anat.stereolin.csv = [path_qc 'group_coregistration' filesep 'anat_tab_qc_coregister_stereolin.csv' ]; @@ -248,11 +259,6 @@ files.quality_control.group_coregistration.func.mean_average = [path_qc 'group_coregistration' filesep 'func_mean_average_stereonl' ext]; files.quality_control.group_coregistration.func.mean_std = [path_qc 'group_coregistration' filesep 'func_mean_std_stereonl' ext]; -%% Grab the results of quality control -- CORSICA -% files.quality_control.group_corsica.pdf = [path_qc 'group_corsica' filesep 'func_ratio_var_corsica_stereonl_fit.pdf']; % Commented out because this is an optional output -% files.quality_control.group_corsica.csv = [path_qc 'group_corsica' filesep 'func_ratio_var_corsica_stereonl_fit.csv']; % Commented out because this is an optional output -% files.quality_control.group_corsica.mean = [path_qc 'group_corsica' filesep 'func_ratio_var_corsica_stereonl_mean' ext]; % Commented out because this is an optional output -% files.quality_control.group_corsica.std = [path_qc 'group_corsica' filesep 'func_ratio_var_corsica_stereonl_std' ext]; % Commented out because this is an optional output %% Grab the results of quality control -- MOTION files.quality_control.group_motion.between_run.csv = [path_qc 'group_motion' filesep 'qc_coregister_between_runs_group.csv']; @@ -266,18 +272,18 @@ subject = list_subject{num_s}; %% Subject level - - % CORSICA - files.quality_control.individual.(subject).corsica.stem = [path_qc subject filesep 'corsica' filesep subject '_mask_stem_funcstereonl' ext]; - files.quality_control.individual.(subject).corsica.vent = [path_qc subject filesep 'corsica' filesep subject '_mask_vent_funcstereonl' ext]; - files.quality_control.individual.(subject).corsica.wm = [path_qc subject filesep 'corsica' filesep subject '_mask_wm_funcstereonl' ext]; - + % MOTION files.quality_control.individual.(subject).motion.coregister.csv = [path_qc subject filesep 'motion_correction' filesep 'tab_coregister_motion.csv']; files.quality_control.individual.(subject).motion.coregister.pdf = [path_qc subject filesep 'motion_correction' filesep 'fig_coregister_motion.pdf']; files.quality_control.individual.(subject).motion.mask = [path_qc subject filesep 'motion_correction' filesep 'func_' subject '_mask_average_stereonl' ext]; files.quality_control.individual.(subject).motion.within_run = [path_qc subject filesep 'motion_correction' filesep 'fig_motion_within_run.pdf']; - +end + +%% Grab resampled data +for num_s = 1:length(list_subject) + subject = list_subject{num_s}; + %% Run level list_session = fieldnames ( files.fmri.vol.(subject) ); for num_sess = 1:length(list_session) @@ -285,16 +291,10 @@ list_run = fieldnames( files.fmri.vol.(subject).(session) ); for num_r = 1:length(list_run) run = list_run{num_r}; - - % CORSICA - % files.quality_control.individual.(subject).corsica.pdf.(session).(run) = [path_qc subject filesep 'corsica' filesep 'fmri_' subject '_' session '_' run '_cor_sica_space_qc_corsica.pdf']; % Commented out because this is an optional output - % files.quality_control.individual.(subject).corsica.var.(session).(run) = [path_qc subject filesep 'corsica' filesep 'qc_corsica_var_' subject '_' session '_' run '_funcstereonl' ext]; % Commented out because this is an optional output - - % CONFOUNDS - for num_c = 1:length(list_conf) - conf = list_conf{num_c}; - files.quality_control.individual.(subject).confounds.(session).(run).(conf) = [path_qc subject filesep 'regress_confounds' filesep subject '_' session '_' run '_qc_' conf '_funcstereonl' ext]; - end + files.resample.fmri.(subject).(session).(run) = [path_res filesep 'fmri_' subject '_' session '_' run '_n' ext]; + files.resample.extra.(subject).(session).(run) = [path_res filesep 'fmri_' subject '_' session '_' run '_n_extra.mat']; + files.resample.confounds.(subject).(session).(run) = [path_res filesep 'fmri_' subject '_' session '_' run '_n_confounds.tsv.gz']; + files.resample.keys = [path_res filesep 'niak_confounds.json']; end end end @@ -310,13 +310,17 @@ list_run = fieldnames( files.fmri.vol.(subject).(session) ); for num_r = 1:length(list_run) run = list_run{num_r}; - files.intermediate.(subject).(session).(run).motion.target = [path_inter subject filesep 'motion_correction' filesep 'motion_target_' subject '_' session '_' run ext]; - files.intermediate.(subject).(session).(run).motion.with_run = [path_inter subject filesep 'motion_correction' filesep 'motion_Wrun_' subject '_' session '_' run '.mat']; - files.intermediate.(subject).(session).(run).motion.parameters = [path_inter subject filesep 'motion_correction' filesep 'motion_parameters_' subject '_' session '_' run '.mat']; - files.intermediate.(subject).(session).(run).confounds = [path_inter subject filesep 'regress_confounds' filesep 'confounds_gs_' subject '_' session '_' run '_cor.mat']; - files.intermediate.(subject).(session).(run).scrubbing = [path_inter subject filesep 'regress_confounds' filesep 'scrubbing_' subject '_' session '_' run '.mat']; - files.intermediate.(subject).(session).(run).filter.high = [path_inter subject filesep 'time_filter' filesep 'fmri_' subject '_' session '_' run '_a_res_dc_high.mat']; - files.intermediate.(subject).(session).(run).filter.low = [path_inter subject filesep 'time_filter' filesep 'fmri_' subject '_' session '_' run '_a_res_dc_low.mat']; + files.intermediate.(subject).(session).(run).slice_timing = [path_inter subject filesep 'slice_timing' filesep 'fmri_' subject '_' session '_' run '_a' ext]; + files.intermediate.(subject).(session).(run).slice_timing_extra = [path_inter subject filesep 'slice_timing' filesep 'fmri_' subject '_' session '_' run '_a_extra.mat']; + files.intermediate.(subject).(session).(run).motion.target = [path_inter subject filesep 'motion_correction' filesep 'motion_target_' subject '_' session '_' run ext]; + files.intermediate.(subject).(session).(run).motion.with_run = [path_inter subject filesep 'motion_correction' filesep 'motion_Wrun_' subject '_' session '_' run '.mat']; + files.intermediate.(subject).(session).(run).motion.parameters = [path_inter subject filesep 'motion_correction' filesep 'motion_parameters_' subject '_' session '_' run '.mat']; + files.intermediate.(subject).(session).(run).confounds = [path_inter subject filesep 'regress_confounds' filesep 'fmri_' subject '_' session '_' run '_cor' ext]; + files.intermediate.(subject).(session).(run).confounds_mask = [path_inter subject filesep 'regress_confounds' filesep 'fmri_' subject '_' session '_' run '_mask_compcor_stereo' ext]; + files.intermediate.(subject).(session).(run).confounds_extra = [path_inter subject filesep 'regress_confounds' filesep 'fmri_' subject '_' session '_' run '_cor_extra.mat']; + files.intermediate.(subject).(session).(run).scrubbing = [path_inter subject filesep 'regress_confounds' filesep 'scrubbing_' subject '_' session '_' run '.mat']; + files.intermediate.(subject).(session).(run).filter.high = [path_inter subject filesep 'time_filter' filesep 'fmri_' subject '_' session '_' run '_n_dc_high.mat']; + files.intermediate.(subject).(session).(run).filter.low = [path_inter subject filesep 'time_filter' filesep 'fmri_' subject '_' session '_' run '_n_dc_low.mat']; end end -end +end \ No newline at end of file diff --git a/commands/read_write/niak_grab_fmri_preprocess.m b/commands/read_write/niak_grab_fmri_preprocess.m index bbcbe42d..f106a186 100644 --- a/commands/read_write/niak_grab_fmri_preprocess.m +++ b/commands/read_write/niak_grab_fmri_preprocess.m @@ -191,23 +191,23 @@ %% check max motion file_motion = [path_qc 'group_motion' filesep 'qc_motion_group.csv']; -[tab_motion,labx,laby] = niak_read_csv(file_motion); mask_keep = true(nb_subject,1); if (opt.max_translationchar')'; @@ -100,6 +141,10 @@ csv_cell = strtrim(csv_cell); end +if flag_zip + psom_clean(file_name,struct('flag_verbose',false)); +end + function cell_values = sub_csv(str_values,separator) if ~isempty(str_values) diff --git a/commands/read_write/niak_write_csv_cell.m b/commands/read_write/niak_write_csv_cell.m index e8b5ff2c..197316c4 100644 --- a/commands/read_write/niak_write_csv_cell.m +++ b/commands/read_write/niak_write_csv_cell.m @@ -8,13 +8,16 @@ % INPUTS: % % FILE_NAME -% (string) the name of the text file (usually ends in .csv) +% (string) the name of the text file. This usually ends in .csv for comma-separated +% values, .tsv for tabulation-separated values and and can have a .gz extension for +% compressed files. % % CSV_CELL % (cell of strings/float) the data % % SEPARATOR -% (string, default ',') The character used to separate values. +% (string, default ',' for csv files, char(9) - tabulation - for .tsv files, ',' otherwise) +% The character used to separate values. % % _________________________________________________________________________ % OUTPUTS: @@ -32,10 +35,14 @@ % _________________________________________________________________________ % COMMENTS: % +% The extension of zipped files is assumed to be .gz. The tools used to zip +% files is 'gzip'. This setting can be changed by changing the variables +% GB_NIAK_ZIP_EXT and GB_NIAK_UNZIP in the file NIAK_GB_VARS. + % Copyright (c) Pierre Bellec, -% Centre de recherche de l'institut de gériatrie de Montréal, +% Centre de recherche de l'institut de griatrie de Montral, % Department of Computer Science and Operations Research -% University of Montreal, Québec, Canada, 2013 +% University of Montreal, Qubec, Canada, 2013-2015 % Maintainer : pierre.bellec@criugm.qc.ca % See licensing information in the code. % Keywords : table, CSV @@ -58,14 +65,29 @@ % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN % THE SOFTWARE. +% Load global variables +flag_gb_niak_fast_gb = 1; +niak_gb_vars + %% Default inputs if ~exist('csv_cell','var')||~exist('file_name','var') error('Please specify FILE_NAME and CSV_CELL as inputs'); end +%% Check extension +[path_f,name_f,ext_f,flag_zip,ext_short] = niak_fileparts(file_name); +file_name = [path_f,filesep,name_f,ext_short]; + %% Options if nargin < 3 - separator = ','; + switch ext_short + case '.csv' + separator = ','; + case '.tsv' + separator = char(9); + otherwise + separator = ','; + end end [nx,ny] = size(csv_cell); @@ -92,7 +114,7 @@ error('all cells should be either string or numeric variables') end if numy ~= ny - fprintf(hf,[sw ','],csv_cell{numx,numy}); + fprintf(hf,[sw separator],csv_cell{numx,numy}); else fprintf(hf,[sw '\n'],csv_cell{numx,numy}); end @@ -100,3 +122,12 @@ end fclose(hf); + +%% Compress file +if flag_zip + instr_zip = cat(2,gb_niak_zip,' ',file_name); + [status,msg] = system(instr_zip); + if status~=0 + error(cat(2,'niak:write: ',msg,'. There was a problem when attempting to zip the file. Please check that the command ''',gb_niak_zip,''' works, or change program using the variable GB_NIAK_ZIP in the file NIAK_GB_VARS')); + end +end \ No newline at end of file diff --git a/commands/read_write/niak_write_vol.m b/commands/read_write/niak_write_vol.m index a8fb59db..34286ec0 100644 --- a/commands/read_write/niak_write_vol.m +++ b/commands/read_write/niak_write_vol.m @@ -102,13 +102,23 @@ % COMMENTS: % % As mentioned in the description of HDR.FILE_NAME, the extension of zipped -% file is assumed to be .gz. The tools used to zip files in 'gzip'. This +% file is assumed to be .gz. The tools used to zip files is 'gzip'. This % setting can be changed by changing the variables GB_NIAK_ZIP_EXT and % GB_NIAK_UNZIP in the file NIAK_GB_VARS. % % Other fields of HDR can be used in MINC format to speed up writting. % See the help of NIAK_WRITE_MINC. % +% The field HDR.TYPE is forced by the extension of the file name: +% '.mnc.gz' -> ',minc1' ; '.mnc' -> 'minc2'; +% '.nii' or '.nii.gz' -> 'nii'; +% '.img' -> HDR.TYPE if it is 'img' or 'analyze', 'img' otherwise. +% +% If the file type is changed, HDR.DETAILS is ignored and all header fields +% are extrapolated from HDR.INFO. The exception to this behaviour is +% a conversion across nifti/analyze variants, which share the structure of +% their details. +% % Copyright (c) Pierre Bellec, Montreal Neurological Institute, 2008. % Maintainer : pbellec@bic.mni.mcgill.ca % See licensing information in the code. @@ -221,18 +231,43 @@ else %% Case 2b : a regular string - try - type_f = hdr.type; - catch - error('niak:write: Please specify a file format in hdr.type.\n') - end - - [path_f,name_f,ext_f] = fileparts(hdr.file_name); + %% Check the type of the file + [path_f,name_f,ext_f] = niak_fileparts(hdr.file_name); if isempty(path_f) path_f = '.'; end + if ~isfield(hdr,'type') + error('niak:write: Please specify a file format in hdr.type.') + end + switch ext_f + case '.mnc.gz' + type_f = 'minc1'; + case '.mnc' + type_f = 'minc2'; + case {'.nii','.nii.gz'} + type_f = 'nii'; + case '.img' + if ismember(hdr.type,{'img','analyze'}) + type_f = hdr.type; + else + type_f = 'img'; + end + otherwise + error('%s is not a supported extension',ext_f) + end + + %% Check if the format has changed + %% if it has, remove details, unless we are only moving between nifti subtypes + if ~strcmp(type_f,hdr.type)&&~min(ismember({type_f,hdr.type},{'analyze','nii','img'})) + hdr.type = type_f; + if isfield(hdr,'details') + hdr = rmfield(hdr,'details'); + end + end + + %% Deal with extra information if isfield(hdr,'extra') extra = hdr.extra; hdr = rmfield(hdr,'extra'); @@ -245,8 +280,11 @@ error(sprintf('Could not write %s, the folder %s does not exist !',hdr.file_name,path_f)); end file_name = hdr.file_name; - if strcmp(ext_f,gb_niak_zip_ext) - hdr.file_name = niak_file_tmp(['_' name_f]); + if (length(ext_f)>=3) && strcmp( ext_f((end-2):end) , '.gz'); + flag_zip = true; + hdr.file_name = niak_file_tmp(['_' name_f ext_f(1:(end-3))]); + else + flag_zip = false; end switch type_f case {'minc1','minc2'} % That's a minc file @@ -257,7 +295,7 @@ error('niak:write: %s : unrecognized file format\n',type_f); end - if strcmp(ext_f,gb_niak_zip_ext) + if flag_zip instr_zip = cat(2,gb_niak_zip,' ',hdr.file_name); [status,msg] = system(instr_zip); if status~=0 @@ -270,6 +308,7 @@ end end + %% Copy extra information, only if the number of time frames match with the actual data if (length(fieldnames(extra))>1)&&(length(extra.time_frames)==size(vol,4)) [path_extra,name_extra] = niak_fileparts(file_name); file_extra = [path_extra filesep name_extra '_extra.mat']; diff --git a/commands/visualization/niak_resample_vol.m b/commands/visualization/niak_resample_vol.m new file mode 100644 index 00000000..50eca6ba --- /dev/null +++ b/commands/visualization/niak_resample_vol.m @@ -0,0 +1,91 @@ +function vol_r = niak_resample_vol(hdr,vol) +% Resample a volume in a given space +% +% VOL_R = NIAK_RESAMPLE_VOL( HDR , VOL , OPT ) +% +% HDR.SOURCE (structure) the header of the volume +% HDR.TARGET (structure) the header of a volume defining the sampling space +% VOL (3D array) brain volume, in stereotaxic space. +% VOL_R (3D array) same as VOL, resampled in the voxel space of TARGET +% +% Note: The new volume is generated in the voxel space associated with the target. +% If no target is specified, the source space is resampled with direction cosines, +% and the field of view is adjusted such that it includes all of the voxels. +% Only nearest neighbour interpolation is available. +% +% Copyright (c) Pierre Bellec +% Centre de recherche de l'Institut universitaire de griatrie de Montral, 2016. +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords : visualization + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +%% Default options +if nargin < 2 + error('Please specify VOL and HDR') +end + +if ~isfield(hdr,'source')||~isfield(hdr,'target') + hdr = struct('source',hdr,'target',[]); +end + +%% size of the source space +dim_s = hdr.source.info.dimensions(1:3); + +%% Extract world coordinates for the source sampling grid +[xx_vs,yy_vs,zz_vs] = ndgrid(1:dim_s(1),1:dim_s(2),1:dim_s(3)); +slice_ws = niak_coord_vox2world([xx_vs(:) yy_vs(:) zz_vs(:)],hdr.source.info.mat); +xx_ws = reshape(slice_ws(:,1),size(xx_vs)); +yy_ws = reshape(slice_ws(:,2),size(yy_vs)); +zz_ws = reshape(slice_ws(:,3),size(zz_vs)); + +%% Automatically choose axis and bounding box if a single image is provided +if isempty(hdr.target) + N = [diag(hdr.source.info.voxel_size) zeros(3,1) ; 0 0 0 1]; + N(1:3,4) = -hdr.source.info.mat(1:3,4); + hdr.target.info.mat = N; + coord_vt = niak_coord_world2vox(slice_ws,hdr.target.info.mat); + cmin = floor(min(coord_vt,[],1)); + cmax = ceil(max(coord_vt,[],1)); + hdr.target.info.dimensions = cmax - cmin + 1; + tsl = hdr.target.info.mat(1:3,1:3)*(cmin(:) -1); + hdr.target.info.mat(1:3,4) = hdr.target.info.mat(1:3,4) + tsl(:); +end + +%% Size of the target space +dim_t = hdr.target.info.dimensions(1:3); + +%% get coordinates in voxel (target) space +[xx_vt,yy_vt,zz_vt] = ndgrid(1:dim_t(1),1:dim_t(2),1:dim_t(3)); +slice_wt = niak_coord_vox2world([xx_vt(:) yy_vt(:) zz_vt(:)],hdr.target.info.mat); +xx_wt = reshape(slice_wt(:,1),size(xx_vt)); +yy_wt = reshape(slice_wt(:,2),size(yy_vt)); +zz_wt = reshape(slice_wt(:,3),size(zz_vt)); + +% Build voxel coordinates from target to source space +slice_vt = niak_coord_world2vox(slice_wt,hdr.source.info.mat); +slice_vt = round(slice_vt); +mask = (slice_vt(:,1)>=1)&(slice_vt(:,1)<=dim_s(1))&(slice_vt(:,2)>=1)&(slice_vt(:,2)<=dim_s(2))&(slice_vt(:,3)>=1)&(slice_vt(:,3)<=dim_s(3)); +slice_vt = slice_vt(mask,:); + +% resample +vol_r = zeros(size(xx_vt)); +ind_slice = niak_sub2ind_3d(size(vol),slice_vt(:,1),slice_vt(:,2),slice_vt(:,3)); +vol_r(mask) = vol(ind_slice); \ No newline at end of file diff --git a/commands/visualization/niak_vol2img.m b/commands/visualization/niak_vol2img.m new file mode 100644 index 00000000..68347aff --- /dev/null +++ b/commands/visualization/niak_vol2img.m @@ -0,0 +1,173 @@ +function [img,slices] = niak_vol2img(hdr,vol,coord,opt) +% Generate an image file with a series of brain views. +% +% [IMG,SLICES] = NIAK_VOL2IMG( VOL , HDR , COORD , OPT ) +% +% HDR.SOURCE (structure) the header of the volume +% HDR.TARGET (structure) the header of a volume defining the sampling space +% VOL (3D array) brain volume, in stereotaxic space. +% COORD (vector Nx3) each row define a series of slices to display (X,Y,Z). +% OPT.TYPE_FLIP (string, default 'rot90') how to flip slices to represent them. +% OPT.TYPE_VIEW (string, default 'all') the type of views to include: +% 'axial', 'sagital', 'coronal', 'all' +% IMG (array) all three slices assembled into a single image, in target space. +% SLICES (cell of array) each entry is one slice (x, y, then z). +% +% Note: The montage is generated in voxel space associated with the target. +% If no target is specified, the source space is resampled with direction cosines, +% and the field of view is adjusted such that it includes all of the voxels. +% Only nearest neighbour interpolation is available. +% +% Copyright (c) Pierre Bellec +% Centre de recherche de l'Institut universitaire de griatrie de Montral, 2016. +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords : visualization + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +%% Default options +if nargin < 3 + error('Please specify VOL, HDR, and COORD') +end + +if ~isfield(hdr,'source')||~isfield(hdr,'target') + hdr = struct('source',hdr,'target',[]); +end + +if nargin < 4 + opt = struct; + method = 'linear'; +end + +opt = psom_struct_defaults(opt, ... + { 'type_view' , 'method' , 'type_flip' }, ... + {'all' , 'linear' , 'rot90' }); + +%% stack slices, if multiple sets of coordinates are specified +nb_slices = size(coord,1); +if nb_slices > 1 + img = []; + for ss = 1:nb_slices + switch opt.type_view + case 'all' + img = [img ; niak_vol2img(hdr,vol,coord(ss,:),opt)]; + case {'sagital','coronal','axial'} + img = [img niak_vol2img(hdr,vol,coord(ss,:),opt)]; + otherwise + error('%s is an unknown type of view',opt.type_view); + end + end + return +end + +%% size of the source space +dim_s = hdr.source.info.dimensions(1:3); + +%% Extract world coordinates for the source sampling grid +[xx_vs,yy_vs,zz_vs] = ndgrid(1:dim_s(1),1:dim_s(2),1:dim_s(3)); +slice_ws = niak_coord_vox2world([xx_vs(:) yy_vs(:) zz_vs(:)],hdr.source.info.mat); +xx_ws = reshape(slice_ws(:,1),size(xx_vs)); +yy_ws = reshape(slice_ws(:,2),size(yy_vs)); +zz_ws = reshape(slice_ws(:,3),size(zz_vs)); + +%% Automatically choose axis and bounding box if a single image is provided +if isempty(hdr.target) + N = [diag(hdr.source.info.voxel_size) zeros(3,1) ; 0 0 0 1]; + N(1:3,4) = -hdr.source.info.mat(1:3,4); + hdr.target.info.mat = N; + coord_vt = niak_coord_world2vox(slice_ws,hdr.target.info.mat); + cmin = floor(min(coord_vt,[],1)); + cmax = ceil(max(coord_vt,[],1)); + hdr.target.info.dimensions = cmax - cmin + 1; + tsl = hdr.target.info.mat(1:3,1:3)*(cmin(:) -1); + hdr.target.info.mat(1:3,4) = hdr.target.info.mat(1:3,4) + tsl(:); +end + +%% Size of the target space +dim_t = hdr.target.info.dimensions(1:3); + +%% Check if user selected center +if ischar(coord) + coord = niak_coord_vox2world( floor(dim_t/2),hdr.target.info.mat ); +end + +%% get target coordinates +coord_w = coord(1:3); +coord_w = coord_w(:)'; + +%% get coordinates in voxel (target) space +coord_vt = round(niak_coord_world2vox(coord_w,hdr.target.info.mat)); + +%% Type of view +switch opt.type_view + case 'all' + list_view = 1:3; + case 'axial' + list_view = 3; + case 'sagital' + list_view = 1; + case 'coronal' + list_view = 2; + otherwise + error('%s is an unknown type of view',opt.type_view); +end + +%% resample the three slices +slices = cell(length(list_view),1); +for vv = 1:length(list_view) % loop over types of views + % generate the source image + % and the coordinates of pixels in source and target + switch list_view(vv) + case 1 + [xx_vt,yy_vt,zz_vt] = ndgrid(coord_vt(1),1:dim_t(2),1:dim_t(3)); + case 2 + [xx_vt,yy_vt,zz_vt] = ndgrid(1:dim_t(1),coord_vt(2),1:dim_t(3)); + case 3 + [xx_vt,yy_vt,zz_vt] = ndgrid(1:dim_t(1),1:dim_t(2),coord_vt(3)); + end + + % Build voxel coordinates in source space for the slice in target space + slice_wt = niak_coord_vox2world([xx_vt(:) yy_vt(:) zz_vt(:)],hdr.target.info.mat); + slice_vt = niak_coord_world2vox(slice_wt,hdr.source.info.mat); + slice_vt = round(slice_vt); + mask = (slice_vt(:,1)>=1)&(slice_vt(:,1)<=dim_s(1))&(slice_vt(:,2)>=1)&(slice_vt(:,2)<=dim_s(2))&(slice_vt(:,3)>=1)&(slice_vt(:,3)<=dim_s(3)); + slice_vt = slice_vt(mask,:); + + % resample + slice_res = zeros(size(xx_vt)); + ind_slice = niak_sub2ind_3d(size(vol),slice_vt(:,1),slice_vt(:,2),slice_vt(:,3)); + slice_res(mask) = vol(ind_slice); + slices{vv} = niak_flip_vol(squeeze(slice_res),opt.type_flip); +end + +%% The montage image +size_h = 0; +size_w = 0; +for vv = 1:length(list_view) + size_h = max( size_h , size(slices{vv},1) ); + size_w = size_w + size(slices{vv},2); +end +img = zeros(size_h,size_w); +pos = 0; +for vv = 1:length(list_view) + npad = floor((size_h-size(slices{vv},1))/2); + img((npad+1):(npad+size(slices{vv},1)),(pos+1):(pos+size(slices{vv},2))) = slices{vv}; + pos = pos+size(slices{vv},2); +end \ No newline at end of file diff --git a/demos/pipeline/niak_demo_fmri_preprocess.m b/demos/pipeline/niak_demo_fmri_preprocess.m index 606788ea..0e252600 100644 --- a/demos/pipeline/niak_demo_fmri_preprocess.m +++ b/demos/pipeline/niak_demo_fmri_preprocess.m @@ -101,7 +101,12 @@ opt = psom_struct_defaults(opt,{'folder_out'},{folder_out},false); end -% Hard-coded processing parameters +%% Check if the input folder exists +if (~isfield(opt,'flag_test')||(isfield(opt,'flag_test')&&~opt.flag_test))&&~psom_exist(path_demo) + error('The input folder %s does not exist',path_demo) +end + +%% Hard-coded processing parameters opt.slice_timing.type_acquisition = 'interleaved ascending'; opt.slice_timing.type_scanner = 'Bruker'; opt.t1_preprocess.nu_correct.arg = '-distance 50'; @@ -112,9 +117,8 @@ error('analyze format is not currently supported'); elseif psom_exist(cat(2,path_demo,'anat_subject1.mnc.gz')) ext = '.mnc.gz'; -elseif psom_exist(cat(2,path_demo,'anat_subject1.nii')) - ext = '.nii'; - error('analyze format is not currently supported'); +elseif psom_exist(cat(2,path_demo,'anat_subject1.nii.gz')) + ext = '.nii.gz'; else ext = '.mnc'; end diff --git a/demos/pipeline/niak_demo_region_growing.m b/demos/pipeline/niak_demo_region_growing.m index 70ee696b..c4d29c02 100644 --- a/demos/pipeline/niak_demo_region_growing.m +++ b/demos/pipeline/niak_demo_region_growing.m @@ -54,9 +54,9 @@ % % _________________________________________________________________________ % Copyright (c) Pierre Bellec -% Centre de recherche de l'institut de gériatrie de Montréal, +% Centre de recherche de l'institut de griatrie de Montral, % Department of Computer Science and Operations Research -% University of Montreal, Québec, Canada, 2013 +% University of Montreal, Qubec, Canada, 2013 % Maintainer : pierre.bellec@criugm.qc.ca % See licensing information in the code. % Keywords : region growing, fMRI @@ -87,11 +87,11 @@ end path_demo = niak_full_path(path_demo); -opt = psom_struct_defaults(opt,{'files_in','folder_out','flag_test'},{'',[path_demo,filesep,'region_growing',filesep],false}); +opt = psom_struct_defaults(opt,{'files_in','folder_out','flag_test'},{'',[path_demo,filesep,'region_growing',filesep],false},false); if isempty(opt.files_in)&&~strcmp(opt.files_in,'gb_niak_omitted') %% Grab the results from the NIAK fMRI preprocessing pipeline - opt_g.min_nb_vol = 30; % the demo dataset is very short, so we have to lower considerably the minimum acceptable number of volumes per run + opt_g.min_nb_vol = 10; % the demo dataset is very short, so we have to lower considerably the minimum acceptable number of volumes per run opt_g.type_files = 'roi'; % Specify to the grabber to prepare the files for the region growing pipeline files_in = niak_grab_fmri_preprocess(path_demo,opt_g); % Replace the folder by the path where the results of the fMRI preprocessing pipeline were stored. else diff --git a/extensions/glm_connectome/niak_brick_connectome_multiscale.m b/extensions/glm_connectome/niak_brick_connectome_multiscale.m index 2006c48e..0c3cef9e 100644 --- a/extensions/glm_connectome/niak_brick_connectome_multiscale.m +++ b/extensions/glm_connectome/niak_brick_connectome_multiscale.m @@ -235,8 +235,8 @@ % is used at the intra-run level. % % Copyright (c) Pierre Bellec, Centre de recherche de l'institut de -% Gériatrie de Montréal, Département d'informatique et de recherche -% opérationnelle, Université de Montréal, 2010-2013. +% Griatrie de Montral, Dpartement d'informatique et de recherche +% oprationnelle, Universit de Montral, 2010-2013. % Maintainer : pierre.bellec@criugm.qc.ca % See licensing information in the code. % Keywords : GLM, functional connectivity, connectome, PPI. @@ -478,22 +478,29 @@ [hdr_fmri,vol] = niak_read_vol(file_run); intra_run.tseries = niak_vol2tseries(vol); if isfield(hdr_fmri,'extra') - intra_run.time_frames = hdr_fmri.extra.time_frames; + if isfield(hdr_fmri.extra,'mask_scrubbing') + mask_scrubbing = hdr_fmri.extra.mask_scrubbing; + else + mask_scrubbing = false(size(intra_run.tseries,1),1); + end + intra_run.time_frames = hdr_fmri.extra.time_frames(~mask_scrubbing); if isfield(hdr_fmri.extra,'confounds') - intra_run.confounds = hdr_fmri.extra.confounds; + intra_run.confounds = hdr_fmri.extra.confounds(~mask_scrubbing,:); intra_run.labels_confounds = hdr_fmri.extra.labels_confounds; else intra_run.confounds = []; intra_run.labels_confounds = {}; end if isfield(hdr_fmri.extra,'mask_suppressed') - intra_run.mask_suppressed = hdr_fmri.extra.mask_suppressed; + intra_run.mask_suppressed = hdr_fmri.extra.mask_suppressed|mask_scrubbing; else intra_run.mask_suppressed = false(size(intra_run.tseries,1),1); end else intra_run.time_frames = (0:(size(intra_run.tseries,1)-1))*hdr_fmri.info.tr; - intra_run.confounds = []; + intra_run.confounds = []; intra_run.labels_confounds = {}; - intra_run.mask_suppressed = false(size(intra_run.tseries,1),1); + intra_run.mask_suppressed = false(size(intra_run.tseries,1),1); + mask_scrubbing = false(size(intra_run.tseries,1),1); end +intra_run.tseries = intra_run.tseries(~mask_scrubbing,:); \ No newline at end of file diff --git a/extensions/jsonlab-1.2/AUTHORS.txt b/extensions/jsonlab-1.2/AUTHORS.txt new file mode 100644 index 00000000..139040f5 --- /dev/null +++ b/extensions/jsonlab-1.2/AUTHORS.txt @@ -0,0 +1,53 @@ +The author of "jsonlab" toolbox is Qianqian Fang. Qianqian +is currently an Assistant Professor in the Department of Bioengineering, +Northeastern University. + +Address: Qianqian Fang + Department of Bioengineering + Northeastern University + 212A Lake Hall + 360 Huntington Ave, Boston, MA 02115, USA + Office: 503 Holmes Hall + Phone[O]: 617-373-3829 +URL: http://fanglab.org +Email: and + + +The script loadjson.m was built upon previous works by + +- Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713 + date: 2009/11/02 +- François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393 + date: 2009/03/22 +- Joel Feenstra: http://www.mathworks.com/matlabcentral/fileexchange/20565 + date: 2008/07/03 + + +This toolbox contains patches submitted by the following contributors: + +- Blake Johnson + part of revision 341 + +- Niclas Borlin + various fixes in revision 394, including + - loadjson crashes for all-zero sparse matrix. + - loadjson crashes for empty sparse matrix. + - Non-zero size of 0-by-N and N-by-0 empty matrices is lost after savejson/loadjson. + - loadjson crashes for sparse real column vector. + - loadjson crashes for sparse complex column vector. + - Data is corrupted by savejson for sparse real row vector. + - savejson crashes for sparse complex row vector. + +- Yul Kang + patches for svn revision 415. + - savejson saves an empty cell array as [] instead of null + - loadjson differentiates an empty struct from an empty array + +- Mykhailo Bratukha + (Pull#14) Bug fix: File path is wrongly inerpreted as JSON string + +- Insik Kim + (Pull#12) Bug fix: Resolving bug that cell type is converted to json with transposed data + +- Sertan Senturk + (Pull#10,#11) Feature: Added matlab object saving to savejson and saveubjson diff --git a/extensions/jsonlab-1.2/ChangeLog.txt b/extensions/jsonlab-1.2/ChangeLog.txt new file mode 100644 index 00000000..65016b96 --- /dev/null +++ b/extensions/jsonlab-1.2/ChangeLog.txt @@ -0,0 +1,96 @@ +============================================================================ + + JSONlab - a toolbox to encode/decode JSON/UBJSON files in MATLAB/Octave + +---------------------------------------------------------------------------- + +JSONlab ChangeLog (key features marked by *): + +== JSONlab 1.2 (codename: Optimus - Update 2), FangQ == + + 2015/12/16 replacing string concatenation by str cells to gain 2x speed in savejson (Issue#17) + 2015/12/11 fix FileName option case bug (SVN rev#495) + 2015/12/11 add SingletCell option, add SingletArray to replace NoRowBracket (Issue#15,#8) + 2015/11/10 fix bug for inerpreting file names as JSON string - by Mykhailo Bratukha (Pull#14) + 2015/10/16 fix bug for cell with transposed data - by Insik Kim (Pull#12) + 2015/09/25 support exporting matlab object to JSON - by Sertan Senturk (Pull#10, #11) + +== JSONlab 1.1 (codename: Optimus - Update 1), FangQ == + + 2015/05/05 *massively accelerating loadjson for parsing large collection of unstructured small objects + 2015/05/05 force array bracket in 1x1 struct to maintain depth (Issue#1) + 2015/05/05 parse logicals in loadjson + 2015/05/05 make options case insensitive + 2015/05/01 reading unicode encoded json files (thanks to Sertan Senturk,Issue#3) + 2015/04/30 allow \uXXXX to represent a unicode in a string (Issue#2) + 2015/03/30 save a 0x0 solid real empty array as null and handel empty struct array + 2015/03/30 properly handle escape characters in a string + 2015/01/24 *implement the UBJSON Draft12 new name format + 2015/01/13 correct cell array indentation inconsistency + +== JSONlab 1.0 (codename: Optimus - Final), FangQ == + + 2015/01/02 polish help info for all major functions, update examples, finalize 1.0 + 2014/12/19 fix a bug to strictly respect NoRowBracket in savejson + +== JSONlab 1.0.0-RC2 (codename: Optimus - RC2), FangQ == + + 2014/11/22 show progress bar in loadjson ('ShowProgress') + 2014/11/17 *add Compact option in savejson to output compact JSON format ('Compact') + 2014/11/17 add FastArrayParser in loadjson to specify fast parser applicable levels + 2014/09/18 *start official github mirror: https://github.com/fangq/jsonlab + +== JSONlab 1.0.0-RC1 (codename: Optimus - RC1), FangQ == + + 2014/09/17 fix several compatibility issues when running on octave versions 3.2-3.8 + 2014/09/17 *support 2D cell and struct arrays in both savejson and saveubjson + 2014/08/04 escape special characters in a JSON string + 2014/02/16 fix a bug when saving ubjson files + +== JSONlab 0.9.9 (codename: Optimus - beta), FangQ == + + 2014/01/22 use binary read and write in saveubjson and loadubjson + +== JSONlab 0.9.8-1 (codename: Optimus - alpha update 1), FangQ == + + 2013/10/07 better round-trip conservation for empty arrays and structs (patch submitted by Yul Kang) + +== JSONlab 0.9.8 (codename: Optimus - alpha), FangQ == + 2013/08/23 *universal Binary JSON (UBJSON) support, including both saveubjson and loadubjson + +== JSONlab 0.9.1 (codename: Rodimus, update 1), FangQ == + 2012/12/18 *handling of various empty and sparse matrices (fixes submitted by Niclas Borlin) + +== JSONlab 0.9.0 (codename: Rodimus), FangQ == + + 2012/06/17 *new format for an invalid leading char, unpacking hex code in savejson + 2012/06/01 support JSONP in savejson + 2012/05/25 fix the empty cell bug (reported by Cyril Davin) + 2012/04/05 savejson can save to a file (suggested by Patrick Rapin) + +== JSONlab 0.8.1 (codename: Sentiel, Update 1), FangQ == + + 2012/02/28 loadjson quotation mark escape bug, see http://bit.ly/yyk1nS + 2012/01/25 patch to handle root-less objects, contributed by Blake Johnson + +== JSONlab 0.8.0 (codename: Sentiel), FangQ == + + 2012/01/13 *speed up loadjson by 20 fold when parsing large data arrays in matlab + 2012/01/11 remove row bracket if an array has 1 element, suggested by Mykel Kochenderfer + 2011/12/22 *accept sequence of 'param',value input in savejson and loadjson + 2011/11/18 fix struct array bug reported by Mykel Kochenderfer + +== JSONlab 0.5.1 (codename: Nexus Update 1), FangQ == + + 2011/10/21 fix a bug in loadjson, previous code does not use any of the acceleration + 2011/10/20 loadjson supports JSON collections - concatenated JSON objects + +== JSONlab 0.5.0 (codename: Nexus), FangQ == + + 2011/10/16 package and release jsonlab 0.5.0 + 2011/10/15 *add json demo and regression test, support cpx numbers, fix double quote bug + 2011/10/11 *speed up readjson dramatically, interpret _Array* tags, show data in root level + 2011/10/10 create jsonlab project, start jsonlab website, add online documentation + 2011/10/07 *speed up savejson by 25x using sprintf instead of mat2str, add options support + 2011/10/06 *savejson works for structs, cells and arrays + 2011/09/09 derive loadjson from JSON parser from MATLAB Central, draft savejson.m diff --git a/extensions/jsonlab-1.2/LICENSE_BSD.txt b/extensions/jsonlab-1.2/LICENSE_BSD.txt new file mode 100644 index 00000000..32d66cbc --- /dev/null +++ b/extensions/jsonlab-1.2/LICENSE_BSD.txt @@ -0,0 +1,25 @@ +Copyright 2011-2015 Qianqian Fang . All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are +permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this list of + conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, this list + of conditions and the following disclaimer in the documentation and/or other materials + provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS +OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The views and conclusions contained in the software and documentation are those of the +authors and should not be interpreted as representing official policies, either expressed +or implied, of the copyright holders. diff --git a/extensions/jsonlab-1.2/README.txt b/extensions/jsonlab-1.2/README.txt new file mode 100644 index 00000000..849a0f78 --- /dev/null +++ b/extensions/jsonlab-1.2/README.txt @@ -0,0 +1,411 @@ +=============================================================================== += JSONLab = += An open-source MATLAB/Octave JSON encoder and decoder = +=============================================================================== + +*Copyright (C) 2011-2015 Qianqian Fang +*License: BSD License, see License_BSD.txt for details +*Version: 1.2 (Optimus - Update 2) + +------------------------------------------------------------------------------- + +Table of Content: + +I. Introduction +II. Installation +III.Using JSONLab +IV. Known Issues and TODOs +V. Contribution and feedback + +------------------------------------------------------------------------------- + +I. Introduction + +JSON ([http://www.json.org/ JavaScript Object Notation]) is a highly portable, +human-readable and "[http://en.wikipedia.org/wiki/JSON fat-free]" text format +to represent complex and hierarchical data. It is as powerful as +[http://en.wikipedia.org/wiki/XML XML], but less verbose. JSON format is widely +used for data-exchange in applications, and is essential for the wild success +of [http://en.wikipedia.org/wiki/Ajax_(programming) Ajax] and +[http://en.wikipedia.org/wiki/Web_2.0 Web2.0]. + +UBJSON (Universal Binary JSON) is a binary JSON format, specifically +optimized for compact file size and better performance while keeping +the semantics as simple as the text-based JSON format. Using the UBJSON +format allows to wrap complex binary data in a flexible and extensible +structure, making it possible to process complex and large dataset +without accuracy loss due to text conversions. + +We envision that both JSON and its binary version will serve as part of +the mainstream data-exchange formats for scientific research in the future. +It will provide the flexibility and generality achieved by other popular +general-purpose file specifications, such as +[http://www.hdfgroup.org/HDF5/whatishdf5.html HDF5], with significantly +reduced complexity and enhanced performance. + +JSONLab is a free and open-source implementation of a JSON/UBJSON encoder +and a decoder in the native MATLAB language. It can be used to convert a MATLAB +data structure (array, struct, cell, struct array and cell array) into +JSON/UBJSON formatted strings, or to decode a JSON/UBJSON file into MATLAB +data structure. JSONLab supports both MATLAB and +[http://www.gnu.org/software/octave/ GNU Octave] (a free MATLAB clone). + +------------------------------------------------------------------------------- + +II. Installation + +The installation of JSONLab is no different than any other simple +MATLAB toolbox. You only need to download/unzip the JSONLab package +to a folder, and add the folder's path to MATLAB/Octave's path list +by using the following command: + + addpath('/path/to/jsonlab'); + +If you want to add this path permanently, you need to type "pathtool", +browse to the jsonlab root folder and add to the list, then click "Save". +Then, run "rehash" in MATLAB, and type "which loadjson", if you see an +output, that means JSONLab is installed for MATLAB/Octave. + +------------------------------------------------------------------------------- + +III.Using JSONLab + +JSONLab provides two functions, loadjson.m -- a MATLAB->JSON decoder, +and savejson.m -- a MATLAB->JSON encoder, for the text-based JSON, and +two equivallent functions -- loadubjson and saveubjson for the binary +JSON. The detailed help info for the four functions can be found below: + +=== loadjson.m === +
+  data=loadjson(fname,opt)
+     or
+  data=loadjson(fname,'param1',value1,'param2',value2,...)
+ 
+  parse a JSON (JavaScript Object Notation) file or string
+ 
+  authors:Qianqian Fang (fangq nmr.mgh.harvard.edu)
+  created on 2011/09/09, including previous works from 
+ 
+          Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713
+             created on 2009/11/02
+          Franois Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393
+             created on  2009/03/22
+          Joel Feenstra:
+          http://www.mathworks.com/matlabcentral/fileexchange/20565
+             created on 2008/07/03
+ 
+  $Id: loadjson.m 487 2015-05-06 18:19:07Z fangq $
+ 
+  input:
+       fname: input file name, if fname contains "{}" or "[]", fname
+              will be interpreted as a JSON string
+       opt: a struct to store parsing options, opt can be replaced by 
+            a list of ('param',value) pairs - the param string is equivallent
+            to a field in opt. opt can have the following 
+            fields (first in [.|.] is the default)
+ 
+            opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat
+                          for each element of the JSON data, and group 
+                          arrays based on the cell2mat rules.
+            opt.FastArrayParser [1|0 or integer]: if set to 1, use a
+                          speed-optimized array parser when loading an 
+                          array object. The fast array parser may 
+                          collapse block arrays into a single large
+                          array similar to rules defined in cell2mat; 0 to 
+                          use a legacy parser; if set to a larger-than-1
+                          value, this option will specify the minimum
+                          dimension to enable the fast array parser. For
+                          example, if the input is a 3D array, setting
+                          FastArrayParser to 1 will return a 3D array;
+                          setting to 2 will return a cell array of 2D
+                          arrays; setting to 3 will return to a 2D cell
+                          array of 1D vectors; setting to 4 will return a
+                          3D cell array.
+            opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.
+ 
+  output:
+       dat: a cell array, where {...} blocks are converted into cell arrays,
+            and [...] are converted to arrays
+ 
+  examples:
+       dat=loadjson('{"obj":{"string":"value","array":[1,2,3]}}')
+       dat=loadjson(['examples' filesep 'example1.json'])
+       dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)
+
+ +=== savejson.m === + +
+  json=savejson(rootname,obj,filename)
+     or
+  json=savejson(rootname,obj,opt)
+  json=savejson(rootname,obj,'param1',value1,'param2',value2,...)
+ 
+  convert a MATLAB object (cell, struct or array) into a JSON (JavaScript
+  Object Notation) string
+ 
+  author: Qianqian Fang (fangq nmr.mgh.harvard.edu)
+  created on 2011/09/09
+ 
+  $Id: savejson.m 486 2015-05-05 20:37:11Z fangq $
+ 
+  input:
+       rootname: the name of the root-object, when set to '', the root name
+         is ignored, however, when opt.ForceRootName is set to 1 (see below),
+         the MATLAB variable name will be used as the root name.
+       obj: a MATLAB object (array, cell, cell array, struct, struct array).
+       filename: a string for the file name to save the output JSON data.
+       opt: a struct for additional options, ignore to use default values.
+         opt can have the following fields (first in [.|.] is the default)
+ 
+         opt.FileName [''|string]: a file name to save the output JSON data
+         opt.FloatFormat ['%.10g'|string]: format to show each numeric element
+                          of a 1D/2D array;
+         opt.ArrayIndent [1|0]: if 1, output explicit data array with
+                          precedent indentation; if 0, no indentation
+         opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D
+                          array in JSON array format; if sets to 1, an
+                          array will be shown as a struct with fields
+                          "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for
+                          sparse arrays, the non-zero elements will be
+                          saved to _ArrayData_ field in triplet-format i.e.
+                          (ix,iy,val) and "_ArrayIsSparse_" will be added
+                          with a value of 1; for a complex array, the 
+                          _ArrayData_ array will include two columns 
+                          (4 for sparse) to record the real and imaginary 
+                          parts, and also "_ArrayIsComplex_":1 is added. 
+         opt.ParseLogical [0|1]: if this is set to 1, logical array elem
+                          will use true/false rather than 1/0.
+         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single
+                          numerical element will be shown without a square
+                          bracket, unless it is the root object; if 0, square
+                          brackets are forced for any numerical arrays.
+         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson
+                          will use the name of the passed obj variable as the 
+                          root object name; if obj is an expression and 
+                          does not have a name, 'root' will be used; if this 
+                          is set to 0 and rootname is empty, the root level 
+                          will be merged down to the lower level.
+         opt.Inf ['"$1_Inf_"'|string]: a customized regular expression pattern
+                          to represent +/-Inf. The matched pattern is '([-+]*)Inf'
+                          and $1 represents the sign. For those who want to use
+                          1e999 to represent Inf, they can set opt.Inf to '$11e999'
+         opt.NaN ['"_NaN_"'|string]: a customized regular expression pattern
+                          to represent NaN
+         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),
+                          for example, if opt.JSONP='foo', the JSON data is
+                          wrapped inside a function call as 'foo(...);'
+         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson 
+                          back to the string form
+         opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.
+         opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)
+ 
+         opt can be replaced by a list of ('param',value) pairs. The param 
+         string is equivallent to a field in opt and is case sensitive.
+  output:
+       json: a string in the JSON format (see http://json.org)
+ 
+  examples:
+       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... 
+                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...
+                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...
+                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...
+                'MeshCreator','FangQ','MeshTitle','T6 Cube',...
+                'SpecialData',[nan, inf, -inf]);
+       savejson('jmesh',jsonmesh)
+       savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\t%.5g')
+ 
+ +=== loadubjson.m === + +
+  data=loadubjson(fname,opt)
+     or
+  data=loadubjson(fname,'param1',value1,'param2',value2,...)
+ 
+  parse a JSON (JavaScript Object Notation) file or string
+ 
+  authors:Qianqian Fang (fangq nmr.mgh.harvard.edu)
+  created on 2013/08/01
+ 
+  $Id: loadubjson.m 487 2015-05-06 18:19:07Z fangq $
+ 
+  input:
+       fname: input file name, if fname contains "{}" or "[]", fname
+              will be interpreted as a UBJSON string
+       opt: a struct to store parsing options, opt can be replaced by 
+            a list of ('param',value) pairs - the param string is equivallent
+            to a field in opt. opt can have the following 
+            fields (first in [.|.] is the default)
+ 
+            opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat
+                          for each element of the JSON data, and group 
+                          arrays based on the cell2mat rules.
+            opt.IntEndian [B|L]: specify the endianness of the integer fields
+                          in the UBJSON input data. B - Big-Endian format for 
+                          integers (as required in the UBJSON specification); 
+                          L - input integer fields are in Little-Endian order.
+            opt.NameIsString [0|1]: for UBJSON Specification Draft 8 or 
+                          earlier versions (JSONLab 1.0 final or earlier), 
+                          the "name" tag is treated as a string. To load 
+                          these UBJSON data, you need to manually set this 
+                          flag to 1.
+ 
+  output:
+       dat: a cell array, where {...} blocks are converted into cell arrays,
+            and [...] are converted to arrays
+ 
+  examples:
+       obj=struct('string','value','array',[1 2 3]);
+       ubjdata=saveubjson('obj',obj);
+       dat=loadubjson(ubjdata)
+       dat=loadubjson(['examples' filesep 'example1.ubj'])
+       dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)
+
+ +=== saveubjson.m === + +
+  json=saveubjson(rootname,obj,filename)
+     or
+  json=saveubjson(rootname,obj,opt)
+  json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)
+ 
+  convert a MATLAB object (cell, struct or array) into a Universal 
+  Binary JSON (UBJSON) binary string
+ 
+  author: Qianqian Fang (fangq nmr.mgh.harvard.edu)
+  created on 2013/08/17
+ 
+  $Id: saveubjson.m 465 2015-01-25 00:46:07Z fangq $
+ 
+  input:
+       rootname: the name of the root-object, when set to '', the root name
+         is ignored, however, when opt.ForceRootName is set to 1 (see below),
+         the MATLAB variable name will be used as the root name.
+       obj: a MATLAB object (array, cell, cell array, struct, struct array)
+       filename: a string for the file name to save the output UBJSON data
+       opt: a struct for additional options, ignore to use default values.
+         opt can have the following fields (first in [.|.] is the default)
+ 
+         opt.FileName [''|string]: a file name to save the output JSON data
+         opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D
+                          array in JSON array format; if sets to 1, an
+                          array will be shown as a struct with fields
+                          "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for
+                          sparse arrays, the non-zero elements will be
+                          saved to _ArrayData_ field in triplet-format i.e.
+                          (ix,iy,val) and "_ArrayIsSparse_" will be added
+                          with a value of 1; for a complex array, the 
+                          _ArrayData_ array will include two columns 
+                          (4 for sparse) to record the real and imaginary 
+                          parts, and also "_ArrayIsComplex_":1 is added. 
+         opt.ParseLogical [1|0]: if this is set to 1, logical array elem
+                          will use true/false rather than 1/0.
+         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single
+                          numerical element will be shown without a square
+                          bracket, unless it is the root object; if 0, square
+                          brackets are forced for any numerical arrays.
+         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson
+                          will use the name of the passed obj variable as the 
+                          root object name; if obj is an expression and 
+                          does not have a name, 'root' will be used; if this 
+                          is set to 0 and rootname is empty, the root level 
+                          will be merged down to the lower level.
+         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),
+                          for example, if opt.JSON='foo', the JSON data is
+                          wrapped inside a function call as 'foo(...);'
+         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson 
+                          back to the string form
+ 
+         opt can be replaced by a list of ('param',value) pairs. The param 
+         string is equivallent to a field in opt and is case sensitive.
+  output:
+       json: a binary string in the UBJSON format (see http://ubjson.org)
+ 
+  examples:
+       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... 
+                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...
+                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...
+                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...
+                'MeshCreator','FangQ','MeshTitle','T6 Cube',...
+                'SpecialData',[nan, inf, -inf]);
+       saveubjson('jsonmesh',jsonmesh)
+       saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')
+
+ + +=== examples === + +Under the "examples" folder, you can find several scripts to demonstrate the +basic utilities of JSONLab. Running the "demo_jsonlab_basic.m" script, you +will see the conversions from MATLAB data structure to JSON text and backward. +In "jsonlab_selftest.m", we load complex JSON files downloaded from the Internet +and validate the loadjson/savejson functions for regression testing purposes. +Similarly, a "demo_ubjson_basic.m" script is provided to test the saveubjson +and loadubjson functions for various matlab data structures. + +Please run these examples and understand how JSONLab works before you use +it to process your data. + +------------------------------------------------------------------------------- + +IV. Known Issues and TODOs + +JSONLab has several known limitations. We are striving to make it more general +and robust. Hopefully in a few future releases, the limitations become less. + +Here are the known issues: + +# 3D or higher dimensional cell/struct-arrays will be converted to 2D arrays; +# When processing names containing multi-byte characters, Octave and MATLAB \ +can give different field-names; you can use feature('DefaultCharacterSet','latin1') \ +in MATLAB to get consistant results +# savejson can not handle class and dataset. +# saveubjson converts a logical array into a uint8 ([U]) array +# an unofficial N-D array count syntax is implemented in saveubjson. We are \ +actively communicating with the UBJSON spec maintainer to investigate the \ +possibility of making it upstream +# loadubjson can not parse all UBJSON Specification (Draft 9) compliant \ +files, however, it can parse all UBJSON files produced by saveubjson. + +------------------------------------------------------------------------------- + +V. Contribution and feedback + +JSONLab is an open-source project. This means you can not only use it and modify +it as you wish, but also you can contribute your changes back to JSONLab so +that everyone else can enjoy the improvement. For anyone who want to contribute, +please download JSONLab source code from its source code repositories by using the +following command: + + git clone https://github.com/fangq/jsonlab.git jsonlab + +or browsing the github site at + + https://github.com/fangq/jsonlab + +alternatively, if you prefer svn, you can checkout the latest code by using + + svn checkout svn://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab jsonlab + +You can make changes to the files as needed. Once you are satisfied with your +changes, and ready to share it with others, please cd the root directory of +JSONLab, and type + + git diff --no-prefix > yourname_featurename.patch + +or + + svn diff > yourname_featurename.patch + +You then email the .patch file to JSONLab's maintainer, Qianqian Fang, at +the email address shown in the beginning of this file. Qianqian will review +the changes and commit it to the subversion if they are satisfactory. + +We appreciate any suggestions and feedbacks from you. Please use the following +mailing list to report any questions you may have regarding JSONLab: + +https://groups.google.com/forum/?hl=en#!forum/jsonlab-users + +(Subscription to the mailing list is needed in order to post messages). diff --git a/extensions/jsonlab-1.2/examples/demo_jsonlab_basic.m b/extensions/jsonlab-1.2/examples/demo_jsonlab_basic.m new file mode 100644 index 00000000..d44d4ff1 --- /dev/null +++ b/extensions/jsonlab-1.2/examples/demo_jsonlab_basic.m @@ -0,0 +1,181 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Demonstration of Basic Utilities of JSONlab +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +rngstate = rand ('state'); +randseed=hex2dec('623F9A9E'); +clear data2json json2data + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a simple scalar value \n') +fprintf(1,'%%=================================================\n\n') + +data2json=pi +savejson('',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a complex number\n') +fprintf(1,'%%=================================================\n\n') + +clear i; +data2json=1+2*i +savejson('',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a complex matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=magic(6); +data2json=data2json(:,1:3)+data2json(:,4:6)*i +savejson('',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% MATLAB special constants\n') +fprintf(1,'%%=================================================\n\n') + +data2json=[NaN Inf -Inf] +savejson('specials',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a real sparse matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=sprand(10,10,0.1) +savejson('sparse',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a complex sparse matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=data2json-data2json*i +savejson('complex_sparse',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% an all-zero sparse matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=sparse(2,3); +savejson('all_zero_sparse',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% an empty sparse matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=sparse([]); +savejson('empty_sparse',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% an empty 0-by-0 real matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=[]; +savejson('empty_0by0_real',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% an empty 0-by-3 real matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=zeros(0,3); +savejson('empty_0by3_real',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a sparse real column vector\n') +fprintf(1,'%%=================================================\n\n') + +data2json=sparse([0,3,0,1,4]'); +savejson('sparse_column_vector',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a sparse complex column vector\n') +fprintf(1,'%%=================================================\n\n') + +data2json=data2json-1i*data2json; +savejson('complex_sparse_column_vector',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a sparse real row vector\n') +fprintf(1,'%%=================================================\n\n') + +data2json=sparse([0,3,0,1,4]); +savejson('sparse_row_vector',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a sparse complex row vector\n') +fprintf(1,'%%=================================================\n\n') + +data2json=data2json-1i*data2json; +savejson('complex_sparse_row_vector',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a structure\n') +fprintf(1,'%%=================================================\n\n') + +data2json=struct('name','Think Different','year',1997,'magic',magic(3),... + 'misfits',[Inf,NaN],'embedded',struct('left',true,'right',false)) +savejson('astruct',data2json,struct('ParseLogical',1)) +json2data=loadjson(ans) +class(json2data.astruct.embedded.left) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a structure array\n') +fprintf(1,'%%=================================================\n\n') + +data2json=struct('name','Nexus Prime','rank',9); +data2json(2)=struct('name','Sentinel Prime','rank',9); +data2json(3)=struct('name','Optimus Prime','rank',9); +savejson('Supreme Commander',data2json) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a cell array\n') +fprintf(1,'%%=================================================\n\n') + +data2json=cell(3,1); +data2json{1}=struct('buzz',1.1,'rex',1.2,'bo',1.3,'hamm',2.0,'slink',2.1,'potato',2.2,... + 'woody',3.0,'sarge',3.1,'etch',4.0,'lenny',5.0,'squeeze',6.0,'wheezy',7.0); +data2json{2}=struct('Ubuntu',['Kubuntu';'Xubuntu';'Lubuntu']); +data2json{3}=[10.04,10.10,11.04,11.10] +savejson('debian',data2json,struct('FloatFormat','%.2f')) +json2data=loadjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% invalid field-name handling\n') +fprintf(1,'%%=================================================\n\n') + +json2data=loadjson('{"ValidName":1, "_InvalidName":2, ":Field:":3, "项目":"绝密"}') + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a 2D cell array\n') +fprintf(1,'%%=================================================\n\n') + +data2json={{1,{2,3}},{4,5},{6};{7},{8,9},{10}}; +savejson('data2json',data2json) +json2data=loadjson(ans) % only savejson works for cell arrays, loadjson has issues + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a 2D struct array\n') +fprintf(1,'%%=================================================\n\n') + +data2json=repmat(struct('idx',0,'data','structs'),[2,3]) +for i=1:6 + data2json(i).idx=i; +end +savejson('data2json',data2json) +json2data=loadjson(ans) + +rand ('state',rngstate); + diff --git a/extensions/jsonlab-1.2/examples/demo_ubjson_basic.m b/extensions/jsonlab-1.2/examples/demo_ubjson_basic.m new file mode 100644 index 00000000..0c35433a --- /dev/null +++ b/extensions/jsonlab-1.2/examples/demo_ubjson_basic.m @@ -0,0 +1,180 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Demonstration of Basic Utilities of JSONlab +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +rngstate = rand ('state'); +randseed=hex2dec('623F9A9E'); +clear data2json json2data + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a simple scalar value \n') +fprintf(1,'%%=================================================\n\n') + +data2json=pi +saveubjson('',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a complex number\n') +fprintf(1,'%%=================================================\n\n') + +clear i; +data2json=1+2*i +saveubjson('',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a complex matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=magic(6); +data2json=data2json(:,1:3)+data2json(:,4:6)*i +saveubjson('',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% MATLAB special constants\n') +fprintf(1,'%%=================================================\n\n') + +data2json=[NaN Inf -Inf] +saveubjson('specials',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a real sparse matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=sprand(10,10,0.1) +saveubjson('sparse',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a complex sparse matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=data2json-data2json*i +saveubjson('complex_sparse',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% an all-zero sparse matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=sparse(2,3); +saveubjson('all_zero_sparse',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% an empty sparse matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=sparse([]); +saveubjson('empty_sparse',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% an empty 0-by-0 real matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=[]; +saveubjson('empty_0by0_real',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% an empty 0-by-3 real matrix\n') +fprintf(1,'%%=================================================\n\n') + +data2json=zeros(0,3); +saveubjson('empty_0by3_real',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a sparse real column vector\n') +fprintf(1,'%%=================================================\n\n') + +data2json=sparse([0,3,0,1,4]'); +saveubjson('sparse_column_vector',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a sparse complex column vector\n') +fprintf(1,'%%=================================================\n\n') + +data2json=data2json-1i*data2json; +saveubjson('complex_sparse_column_vector',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a sparse real row vector\n') +fprintf(1,'%%=================================================\n\n') + +data2json=sparse([0,3,0,1,4]); +saveubjson('sparse_row_vector',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a sparse complex row vector\n') +fprintf(1,'%%=================================================\n\n') + +data2json=data2json-1i*data2json; +saveubjson('complex_sparse_row_vector',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a structure\n') +fprintf(1,'%%=================================================\n\n') + +data2json=struct('name','Think Different','year',1997,'magic',magic(3),... + 'misfits',[Inf,NaN],'embedded',struct('left',true,'right',false)) +saveubjson('astruct',data2json,struct('ParseLogical',1)) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a structure array\n') +fprintf(1,'%%=================================================\n\n') + +data2json=struct('name','Nexus Prime','rank',9); +data2json(2)=struct('name','Sentinel Prime','rank',9); +data2json(3)=struct('name','Optimus Prime','rank',9); +saveubjson('Supreme Commander',data2json) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a cell array\n') +fprintf(1,'%%=================================================\n\n') + +data2json=cell(3,1); +data2json{1}=struct('buzz',1.1,'rex',1.2,'bo',1.3,'hamm',2.0,'slink',2.1,'potato',2.2,... + 'woody',3.0,'sarge',3.1,'etch',4.0,'lenny',5.0,'squeeze',6.0,'wheezy',7.0); +data2json{2}=struct('Ubuntu',['Kubuntu';'Xubuntu';'Lubuntu']); +data2json{3}=[10.04,10.10,11.04,11.10] +saveubjson('debian',data2json,struct('FloatFormat','%.2f')) +json2data=loadubjson(ans) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% invalid field-name handling\n') +fprintf(1,'%%=================================================\n\n') + +json2data=loadubjson(saveubjson('',loadjson('{"ValidName":1, "_InvalidName":2, ":Field:":3, "项目":"绝密"}'))) + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a 2D cell array\n') +fprintf(1,'%%=================================================\n\n') + +data2json={{1,{2,3}},{4,5},{6};{7},{8,9},{10}}; +saveubjson('data2json',data2json) +json2data=loadubjson(ans) % only savejson works for cell arrays, loadjson has issues + +fprintf(1,'\n%%=================================================\n') +fprintf(1,'%% a 2D struct array\n') +fprintf(1,'%%=================================================\n\n') + +data2json=repmat(struct('idx',0,'data','structs'),[2,3]) +for i=1:6 + data2json(i).idx=i; +end +saveubjson('data2json',data2json) +json2data=loadubjson(ans) + +rand ('state',rngstate); + diff --git a/extensions/jsonlab-1.2/examples/example1.json b/extensions/jsonlab-1.2/examples/example1.json new file mode 100644 index 00000000..be0993ef --- /dev/null +++ b/extensions/jsonlab-1.2/examples/example1.json @@ -0,0 +1,23 @@ + { + "firstName": "John", + "lastName": "Smith", + "age": 25, + "address": + { + "streetAddress": "21 2nd Street", + "city": "New York", + "state": "NY", + "postalCode": "10021" + }, + "phoneNumber": + [ + { + "type": "home", + "number": "212 555-1234" + }, + { + "type": "fax", + "number": "646 555-4567" + } + ] + } diff --git a/extensions/jsonlab-1.2/examples/example2.json b/extensions/jsonlab-1.2/examples/example2.json new file mode 100644 index 00000000..eacfbf5e --- /dev/null +++ b/extensions/jsonlab-1.2/examples/example2.json @@ -0,0 +1,22 @@ +{ + "glossary": { + "title": "example glossary", + "GlossDiv": { + "title": "S", + "GlossList": { + "GlossEntry": { + "ID": "SGML", + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "GlossDef": { + "para": "A meta-markup language, used to create markup languages such as DocBook.", + "GlossSeeAlso": ["GML", "XML"] + }, + "GlossSee": "markup" + } + } + } + } +} diff --git a/extensions/jsonlab-1.2/examples/example3.json b/extensions/jsonlab-1.2/examples/example3.json new file mode 100644 index 00000000..f52661bf --- /dev/null +++ b/extensions/jsonlab-1.2/examples/example3.json @@ -0,0 +1,11 @@ +{"menu": { + "id": "file", + "value": "_&File", + "popup": { + "menuitem": [ + {"value": "_&New", "onclick": "CreateNewDoc(\"'\\\"Untitled\\\"'\")"}, + {"value": "_&Open", "onclick": "OpenDoc()"}, + {"value": "_&Close", "onclick": "CloseDoc()"} + ] + } +}} diff --git a/extensions/jsonlab-1.2/examples/example4.json b/extensions/jsonlab-1.2/examples/example4.json new file mode 100644 index 00000000..1872c12f --- /dev/null +++ b/extensions/jsonlab-1.2/examples/example4.json @@ -0,0 +1,35 @@ +[ + { + "sample" : { + "rho" : 1 + } + }, + { + "sample" : { + "rho" : 2 + } + }, + [ + { + "_ArrayType_" : "double", + "_ArraySize_" : [1,2], + "_ArrayData_" : [1,0] + }, + { + "_ArrayType_" : "double", + "_ArraySize_" : [1,2], + "_ArrayData_" : [1,1] + }, + { + "_ArrayType_" : "double", + "_ArraySize_" : [1,2], + "_ArrayData_" : [1,2] + } + ], + [ + "Paper", + "Scissors", + "Stone" + ], + ["a", "b\\", "c\"","d\\\"","e\"[","f\\\"[","g[\\","h[\\\""] +] diff --git a/extensions/jsonlab-1.2/examples/jsonlab_basictest.matlab b/extensions/jsonlab-1.2/examples/jsonlab_basictest.matlab new file mode 100644 index 00000000..b41c7058 --- /dev/null +++ b/extensions/jsonlab-1.2/examples/jsonlab_basictest.matlab @@ -0,0 +1,671 @@ + + < M A T L A B (R) > + Copyright 1984-2010 The MathWorks, Inc. + Version 7.11.0.584 (R2010b) 64-bit (glnxa64) + August 16, 2010 + + + To get started, type one of these: helpwin, helpdesk, or demo. + For product information, visit www.mathworks.com. + +>> >> >> >> >> >> >> >> >> +%================================================= +>> % a simple scalar value +>> %================================================= + +>> >> +data2json = + + 3.1416 + +>> +ans = + +[3.141592654] + + +>> +json2data = + + 3.1416 + +>> >> +%================================================= +>> % a complex number +>> %================================================= + +>> >> >> +data2json = + + 1.0000 + 2.0000i + +>> +ans = + +{ + "_ArrayType_": "double", + "_ArraySize_": [1,1], + "_ArrayIsComplex_": 1, + "_ArrayData_": [1,2] +} + + +>> +json2data = + + 1.0000 + 2.0000i + +>> >> +%================================================= +>> % a complex matrix +>> %================================================= + +>> >> >> +data2json = + + 35.0000 +26.0000i 1.0000 +19.0000i 6.0000 +24.0000i + 3.0000 +21.0000i 32.0000 +23.0000i 7.0000 +25.0000i + 31.0000 +22.0000i 9.0000 +27.0000i 2.0000 +20.0000i + 8.0000 +17.0000i 28.0000 +10.0000i 33.0000 +15.0000i + 30.0000 +12.0000i 5.0000 +14.0000i 34.0000 +16.0000i + 4.0000 +13.0000i 36.0000 +18.0000i 29.0000 +11.0000i + +>> +ans = + +{ + "_ArrayType_": "double", + "_ArraySize_": [6,3], + "_ArrayIsComplex_": 1, + "_ArrayData_": [ + [35,26], + [3,21], + [31,22], + [8,17], + [30,12], + [4,13], + [1,19], + [32,23], + [9,27], + [28,10], + [5,14], + [36,18], + [6,24], + [7,25], + [2,20], + [33,15], + [34,16], + [29,11] + ] +} + + +>> +json2data = + + 35.0000 +26.0000i 1.0000 +19.0000i 6.0000 +24.0000i + 3.0000 +21.0000i 32.0000 +23.0000i 7.0000 +25.0000i + 31.0000 +22.0000i 9.0000 +27.0000i 2.0000 +20.0000i + 8.0000 +17.0000i 28.0000 +10.0000i 33.0000 +15.0000i + 30.0000 +12.0000i 5.0000 +14.0000i 34.0000 +16.0000i + 4.0000 +13.0000i 36.0000 +18.0000i 29.0000 +11.0000i + +>> >> +%================================================= +>> % MATLAB special constants +>> %================================================= + +>> >> +data2json = + + NaN Inf -Inf + +>> +ans = + +{ + "specials": ["_NaN_","_Inf_","-_Inf_"] +} + + +>> +json2data = + + specials: [NaN Inf -Inf] + +>> >> +%================================================= +>> % a real sparse matrix +>> %================================================= + +>> >> +data2json = + + (1,2) 0.6557 + (9,2) 0.7577 + (3,5) 0.8491 + (10,5) 0.7431 + (10,8) 0.3922 + (7,9) 0.6787 + (2,10) 0.0357 + (6,10) 0.9340 + (10,10) 0.6555 + +>> +ans = + +{ + "sparse": { + "_ArrayType_": "double", + "_ArraySize_": [10,10], + "_ArrayIsSparse_": 1, + "_ArrayData_": [ + [1,2,0.6557406992], + [9,2,0.7577401306], + [3,5,0.8491293059], + [10,5,0.7431324681], + [10,8,0.3922270195], + [7,9,0.6787351549], + [2,10,0.03571167857], + [6,10,0.9339932478], + [10,10,0.6554778902] + ] + } +} + + +>> +json2data = + + sparse: [10x10 double] + +>> >> +%================================================= +>> % a complex sparse matrix +>> %================================================= + +>> >> +data2json = + + (1,2) 0.6557 - 0.6557i + (9,2) 0.7577 - 0.7577i + (3,5) 0.8491 - 0.8491i + (10,5) 0.7431 - 0.7431i + (10,8) 0.3922 - 0.3922i + (7,9) 0.6787 - 0.6787i + (2,10) 0.0357 - 0.0357i + (6,10) 0.9340 - 0.9340i + (10,10) 0.6555 - 0.6555i + +>> +ans = + +{ + "complex_sparse": { + "_ArrayType_": "double", + "_ArraySize_": [10,10], + "_ArrayIsComplex_": 1, + "_ArrayIsSparse_": 1, + "_ArrayData_": [ + [1,2,0.6557406992,-0.6557406992], + [9,2,0.7577401306,-0.7577401306], + [3,5,0.8491293059,-0.8491293059], + [10,5,0.7431324681,-0.7431324681], + [10,8,0.3922270195,-0.3922270195], + [7,9,0.6787351549,-0.6787351549], + [2,10,0.03571167857,-0.03571167857], + [6,10,0.9339932478,-0.9339932478], + [10,10,0.6554778902,-0.6554778902] + ] + } +} + + +>> +json2data = + + complex_sparse: [10x10 double] + +>> >> +%================================================= +>> % an all-zero sparse matrix +>> %================================================= + +>> >> >> +ans = + +{ + "all_zero_sparse": { + "_ArrayType_": "double", + "_ArraySize_": [2,3], + "_ArrayIsSparse_": 1, + "_ArrayData_": null + } +} + + +>> +json2data = + + all_zero_sparse: [2x3 double] + +>> >> +%================================================= +>> % an empty sparse matrix +>> %================================================= + +>> >> >> +ans = + +{ + "empty_sparse": { + "_ArrayType_": "double", + "_ArraySize_": [0,0], + "_ArrayIsSparse_": 1, + "_ArrayData_": null + } +} + + +>> +json2data = + + empty_sparse: [] + +>> >> +%================================================= +>> % an empty 0-by-0 real matrix +>> %================================================= + +>> >> >> +ans = + +{ + "empty_0by0_real": null +} + + +>> +json2data = + + empty_0by0_real: [] + +>> >> +%================================================= +>> % an empty 0-by-3 real matrix +>> %================================================= + +>> >> >> +ans = + +{ + "empty_0by3_real": { + "_ArrayType_": "double", + "_ArraySize_": [0,3], + "_ArrayData_": null + } +} + + +>> +json2data = + + empty_0by3_real: [0x3 double] + +>> >> +%================================================= +>> % a sparse real column vector +>> %================================================= + +>> >> >> +ans = + +{ + "sparse_column_vector": { + "_ArrayType_": "double", + "_ArraySize_": [5,1], + "_ArrayIsSparse_": 1, + "_ArrayData_": [ + [2,3], + [4,1], + [5,4] + ] + } +} + + +>> +json2data = + + sparse_column_vector: [5x1 double] + +>> >> +%================================================= +>> % a sparse complex column vector +>> %================================================= + +>> >> >> +ans = + +{ + "complex_sparse_column_vector": { + "_ArrayType_": "double", + "_ArraySize_": [5,1], + "_ArrayIsComplex_": 1, + "_ArrayIsSparse_": 1, + "_ArrayData_": [ + [2,3,-3], + [4,1,-1], + [5,4,-4] + ] + } +} + + +>> +json2data = + + complex_sparse_column_vector: [5x1 double] + +>> >> +%================================================= +>> % a sparse real row vector +>> %================================================= + +>> >> >> +ans = + +{ + "sparse_row_vector": { + "_ArrayType_": "double", + "_ArraySize_": [1,5], + "_ArrayIsSparse_": 1, + "_ArrayData_": [ + [2,3], + [4,1], + [5,4] + ] + } +} + + +>> +json2data = + + sparse_row_vector: [0 3 0 1 4] + +>> >> +%================================================= +>> % a sparse complex row vector +>> %================================================= + +>> >> >> +ans = + +{ + "complex_sparse_row_vector": { + "_ArrayType_": "double", + "_ArraySize_": [1,5], + "_ArrayIsComplex_": 1, + "_ArrayIsSparse_": 1, + "_ArrayData_": [ + [2,3,-3], + [4,1,-1], + [5,4,-4] + ] + } +} + + +>> +json2data = + + complex_sparse_row_vector: [1x5 double] + +>> >> +%================================================= +>> % a structure +>> %================================================= + +>> >> +data2json = + + name: 'Think Different' + year: 1997 + magic: [3x3 double] + misfits: [Inf NaN] + embedded: [1x1 struct] + +>> +ans = + +{ + "astruct": { + "name": "Think Different", + "year": 1997, + "magic": [ + [8,1,6], + [3,5,7], + [4,9,2] + ], + "misfits": ["_Inf_","_NaN_"], + "embedded": { + "left": true, + "right": false + } + } +} + + +>> +json2data = + + astruct: [1x1 struct] + +>> +ans = + +logical + +>> >> +%================================================= +>> % a structure array +>> %================================================= + +>> >> >> >> >> +ans = + +{ + "Supreme Commander": [ + { + "name": "Nexus Prime", + "rank": 9 + }, + { + "name": "Sentinel Prime", + "rank": 9 + }, + { + "name": "Optimus Prime", + "rank": 9 + } + ] +} + + +>> +json2data = + + Supreme_0x20_Commander: {[1x1 struct] [1x1 struct] [1x1 struct]} + +>> >> +%================================================= +>> % a cell array +>> %================================================= + +>> >> >> >> >> +data2json = + + [1x1 struct] + [1x1 struct] + [1x4 double] + +>> +ans = + +{ + "debian": [ + [ + { + "buzz": 1.10, + "rex": 1.20, + "bo": 1.30, + "hamm": 2.00, + "slink": 2.10, + "potato": 2.20, + "woody": 3.00, + "sarge": 3.10, + "etch": 4.00, + "lenny": 5.00, + "squeeze": 6.00, + "wheezy": 7.00 + } + ], + [ + { + "Ubuntu": [ + "Kubuntu", + "Xubuntu", + "Lubuntu" + ] + } + ], + [ + [10.04,10.10,11.04,11.10] + ] + ] +} + + +>> +json2data = + + debian: {{1x1 cell} {1x1 cell} [10.0400 10.1000 11.0400 11.1000]} + +>> >> +%================================================= +>> % invalid field-name handling +>> %================================================= + +>> >> +json2data = + + ValidName: 1 + x0x5F_InvalidName: 2 + x0x3A_Field_0x3A_: 3 + x0xE9A1B9__0xE79BAE_: '绝密' + +>> >> +%================================================= +>> % a 2D cell array +>> %================================================= + +>> >> >> +ans = + +{ + "data2json": [ + [ + [ + 1, + [ + 2, + 3 + ] + ], + [ + 4, + 5 + ], + [ + 6 + ] + ], + [ + [ + 7 + ], + [ + 8, + 9 + ], + [ + 10 + ] + ] + ] +} + + +>> +json2data = + + data2json: {{1x3 cell} {1x3 cell}} + +>> >> +%================================================= +>> % a 2D struct array +>> %================================================= + +>> >> +data2json = + +2x3 struct array with fields: + idx + data + +>> >> +ans = + +{ + "data2json": [ + [ + { + "idx": 1, + "data": "structs" + }, + { + "idx": 2, + "data": "structs" + } + ], + [ + { + "idx": 3, + "data": "structs" + }, + { + "idx": 4, + "data": "structs" + } + ], + [ + { + "idx": 5, + "data": "structs" + }, + { + "idx": 6, + "data": "structs" + } + ] + ] +} + + +>> +json2data = + + data2json: {{1x2 cell} {1x2 cell} {1x2 cell}} + +>> >> >> >> \ No newline at end of file diff --git a/extensions/jsonlab-1.2/examples/jsonlab_selftest.m b/extensions/jsonlab-1.2/examples/jsonlab_selftest.m new file mode 100644 index 00000000..27aee244 --- /dev/null +++ b/extensions/jsonlab-1.2/examples/jsonlab_selftest.m @@ -0,0 +1,27 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Regression Test Unit of loadjson and savejson +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +for i=1:4 + fname=sprintf('example%d.json',i); + if(exist(fname,'file')==0) break; end + fprintf(1,'===============================================\n>> %s\n',fname); + json=savejson('data',loadjson(fname)); + fprintf(1,'%s\n',json); + fprintf(1,'%s\n',savejson('data',loadjson(fname),'Compact',1)); + data=loadjson(json); + savejson('data',data,'selftest.json'); + data=loadjson('selftest.json'); +end + +for i=1:4 + fname=sprintf('example%d.json',i); + if(exist(fname,'file')==0) break; end + fprintf(1,'===============================================\n>> %s\n',fname); + json=saveubjson('data',loadjson(fname)); + fprintf(1,'%s\n',json); + data=loadubjson(json); + savejson('',data); + saveubjson('data',data,'selftest.ubj'); + data=loadubjson('selftest.ubj'); +end diff --git a/extensions/jsonlab-1.2/examples/jsonlab_selftest.matlab b/extensions/jsonlab-1.2/examples/jsonlab_selftest.matlab new file mode 100644 index 00000000..ac652c56 --- /dev/null +++ b/extensions/jsonlab-1.2/examples/jsonlab_selftest.matlab @@ -0,0 +1,154 @@ + + < M A T L A B (R) > + Copyright 1984-2010 The MathWorks, Inc. + Version 7.11.0.584 (R2010b) 64-bit (glnxa64) + August 16, 2010 + + + To get started, type one of these: helpwin, helpdesk, or demo. + For product information, visit www.mathworks.com. + +>> >> >> >> >> =============================================== +>> example1.json +{ + "data": { + "firstName": "John", + "lastName": "Smith", + "age": 25, + "address": { + "streetAddress": "21 2nd Street", + "city": "New York", + "state": "NY", + "postalCode": "10021" + }, + "phoneNumber": [ + { + "type": "home", + "number": "212 555-1234" + }, + { + "type": "fax", + "number": "646 555-4567" + } + ] + } +} + +{"data": {"firstName": "John","lastName": "Smith","age": 25,"address": {"streetAddress": "21 2nd Street","city": "New York","state": "NY","postalCode": "10021"},"phoneNumber": [{"type": "home","number": "212 555-1234"},{"type": "fax","number": "646 555-4567"}]}} + +=============================================== +>> example2.json +{ + "data": { + "glossary": { + "title": "example glossary", + "GlossDiv": { + "title": "S", + "GlossList": { + "GlossEntry": { + "ID": "SGML", + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "GlossDef": { + "para": "A meta-markup language, used to create markup languages such as DocBook.", + "GlossSeeAlso": [ + "GML", + "XML" + ] + }, + "GlossSee": "markup" + } + } + } + } + } +} + +{"data": {"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {"GlossEntry": {"ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML","XML"]},"GlossSee": "markup"}}}}}} + +=============================================== +>> example3.json +{ + "data": { + "menu": { + "id": "file", + "value": "_&File", + "popup": { + "menuitem": [ + { + "value": "_&New", + "onclick": "CreateNewDoc(\"'\\\"Untitled\\\"'\")" + }, + { + "value": "_&Open", + "onclick": "OpenDoc()" + }, + { + "value": "_&Close", + "onclick": "CloseDoc()" + } + ] + } + } + } +} + +{"data": {"menu": {"id": "file","value": "_&File","popup": {"menuitem": [{"value": "_&New","onclick": "CreateNewDoc(\"'\\\"Untitled\\\"'\")"},{"value": "_&Open","onclick": "OpenDoc()"},{"value": "_&Close","onclick": "CloseDoc()"}]}}}} + +=============================================== +>> example4.json +{ + "data": [ + { + "sample": { + "rho": 1 + } + }, + { + "sample": { + "rho": 2 + } + }, + [ + [1,0], + [1,1], + [1,2] + ], + [ + "Paper", + "Scissors", + "Stone" + ], + [ + "a", + "b\\", + "c\"", + "d\\\"", + "e\"[", + "f\\\"[", + "g[\\", + "h[\\\"" + ] + ] +} + +{"data": [{"sample": {"rho": 1}},{"sample": {"rho": 2}},[[1,0],[1,1],[1,2]],["Paper","Scissors","Stone"],["a","b\\","c\"","d\\\"","e\"[","f\\\"[","g[\\","h[\\\""]]} + +>> >> =============================================== +>> example1.json +{Udata{U firstNameSUJohnUlastNameSUSmithUageiUaddress{U streetAddressSU 21 2nd StreetUcitySUNew YorkUstateSUNYU +postalCodeSU10021}U phoneNumber[{UtypeSUhomeUnumberSU 212 555-1234}{UtypeSUfaxUnumberSU 646 555-4567}]}} +=============================================== +>> example2.json +{Udata{Uglossary{UtitleSUexample glossaryUGlossDiv{UtitleCSU GlossList{U +GlossEntry{UIDSUSGMLUSortAsSUSGMLU GlossTermSU$Standard Generalized Markup LanguageUAcronymSUSGMLUAbbrevSU ISO 8879:1986UGlossDef{UparaSUHA meta-markup language, used to create markup languages such as DocBook.U GlossSeeAlso[SUGMLSUXML]}UGlossSeeSUmarkup}}}}}} +=============================================== +>> example3.json +{Udata{Umenu{UidSUfileUvalueSU_&FileUpopup{Umenuitem[{UvalueSU_&NewUonclickSUCreateNewDoc("'\"Untitled\"'")}{UvalueSU_&OpenUonclickSU OpenDoc()}{UvalueSU_&CloseUonclickSU +CloseDoc()}]}}}} +=============================================== +>> example4.json +{Udata[{Usample{Urhoi}}{Usample{Urhoi}}[[$i#U[$i#U[$i#U][SUPaperSUScissorsSUStone][CaSUb\SUc"SUd\"SUe"[SUf\"[SUg[\SUh[\"]]} +>> \ No newline at end of file diff --git a/extensions/jsonlab-1.2/examples/jsonlab_speedtest.m b/extensions/jsonlab-1.2/examples/jsonlab_speedtest.m new file mode 100644 index 00000000..4990fba0 --- /dev/null +++ b/extensions/jsonlab-1.2/examples/jsonlab_speedtest.m @@ -0,0 +1,21 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Benchmarking processing speed of savejson and loadjson +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +datalen=[1e3 1e4 1e5 1e6]; +len=length(datalen); +tsave=zeros(len,1); +tload=zeros(len,1); +for i=1:len + tic; + json=savejson('data',struct('d1',rand(datalen(i),3),'d2',rand(datalen(i),3)>0.5)); + tsave(i)=toc; + data=loadjson(json); + tload(i)=toc-tsave(i); + fprintf(1,'matrix size: %d\n',datalen(i)); +end + +loglog(datalen,tsave,'o-',datalen,tload,'r*-'); +legend('savejson runtime (s)','loadjson runtime (s)'); +xlabel('array size'); +ylabel('running time (s)'); diff --git a/extensions/jsonlab-1.2/jsonopt.m b/extensions/jsonlab-1.2/jsonopt.m new file mode 100644 index 00000000..c347e967 --- /dev/null +++ b/extensions/jsonlab-1.2/jsonopt.m @@ -0,0 +1,35 @@ +function val=jsonopt(key,default,varargin) +% +% val=jsonopt(key,default,optstruct) +% +% setting options based on a struct. The struct can be produced +% by varargin2struct from a list of 'param','value' pairs +% +% authors:Qianqian Fang (fangq nmr.mgh.harvard.edu) +% +% $Id: loadjson.m 371 2012-06-20 12:43:06Z fangq $ +% +% input: +% key: a string with which one look up a value from a struct +% default: if the key does not exist, return default +% optstruct: a struct where each sub-field is a key +% +% output: +% val: if key exists, val=optstruct.key; otherwise val=default +% +% license: +% BSD License, see LICENSE_BSD.txt files for details +% +% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) +% + +val=default; +if(nargin<=2) return; end +opt=varargin{1}; +if(isstruct(opt)) + if(isfield(opt,key)) + val=getfield(opt,key); + elseif(isfield(opt,lower(key))) + val=getfield(opt,lower(key)); + end +end diff --git a/extensions/jsonlab-1.2/loadjson.m b/extensions/jsonlab-1.2/loadjson.m new file mode 100644 index 00000000..788b5496 --- /dev/null +++ b/extensions/jsonlab-1.2/loadjson.m @@ -0,0 +1,496 @@ +function data = loadjson(fname,varargin) +% +% data=loadjson(fname,opt) +% or +% data=loadjson(fname,'param1',value1,'param2',value2,...) +% +% parse a JSON (JavaScript Object Notation) file or string +% +% authors:Qianqian Fang (fangq nmr.mgh.harvard.edu) +% created on 2011/09/09, including previous works from +% +% Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713 +% created on 2009/11/02 +% François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393 +% created on 2009/03/22 +% Joel Feenstra: +% http://www.mathworks.com/matlabcentral/fileexchange/20565 +% created on 2008/07/03 +% +% $Id$ +% +% input: +% fname: input file name, if fname contains "{}" or "[]", fname +% will be interpreted as a JSON string +% opt: a struct to store parsing options, opt can be replaced by +% a list of ('param',value) pairs - the param string is equivallent +% to a field in opt. opt can have the following +% fields (first in [.|.] is the default) +% +% opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat +% for each element of the JSON data, and group +% arrays based on the cell2mat rules. +% opt.FastArrayParser [1|0 or integer]: if set to 1, use a +% speed-optimized array parser when loading an +% array object. The fast array parser may +% collapse block arrays into a single large +% array similar to rules defined in cell2mat; 0 to +% use a legacy parser; if set to a larger-than-1 +% value, this option will specify the minimum +% dimension to enable the fast array parser. For +% example, if the input is a 3D array, setting +% FastArrayParser to 1 will return a 3D array; +% setting to 2 will return a cell array of 2D +% arrays; setting to 3 will return to a 2D cell +% array of 1D vectors; setting to 4 will return a +% 3D cell array. +% opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar. +% +% output: +% dat: a cell array, where {...} blocks are converted into cell arrays, +% and [...] are converted to arrays +% +% examples: +% dat=loadjson('{"obj":{"string":"value","array":[1,2,3]}}') +% dat=loadjson(['examples' filesep 'example1.json']) +% dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1) +% +% license: +% BSD License, see LICENSE_BSD.txt files for details +% +% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) +% + +global pos inStr len esc index_esc len_esc isoct arraytoken + +if(regexp(fname,'^\s*(?:\[.+\])|(?:\{.+\})\s*$','once')) + string=fname; +elseif(exist(fname,'file')) + try + string = fileread(fname); + catch + try + string = urlread(['file://',fname]); + catch + string = urlread(['file://',fullfile(pwd,fname)]); + end + end +else + error('input file does not exist'); +end + +pos = 1; len = length(string); inStr = string; +isoct=exist('OCTAVE_VERSION','builtin'); +arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); +jstr=regexprep(inStr,'\\\\',' '); +escquote=regexp(jstr,'\\"'); +arraytoken=sort([arraytoken escquote]); + +% String delimiters and escape chars identified to improve speed: +esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); +index_esc = 1; len_esc = length(esc); + +opt=varargin2struct(varargin{:}); + +if(jsonopt('ShowProgress',0,opt)==1) + opt.progressbar_=waitbar(0,'loading ...'); +end +jsoncount=1; +while pos <= len + switch(next_char) + case '{' + data{jsoncount} = parse_object(opt); + case '[' + data{jsoncount} = parse_array(opt); + otherwise + error_pos('Outer level structure must be an object or an array'); + end + jsoncount=jsoncount+1; +end % while + +jsoncount=length(data); +if(jsoncount==1 && iscell(data)) + data=data{1}; +end + +if(isfield(opt,'progressbar_')) + close(opt.progressbar_); +end + +%%------------------------------------------------------------------------- +function object = parse_object(varargin) + parse_char('{'); + object = []; + if next_char ~= '}' + while 1 + str = parseStr(varargin{:}); + if isempty(str) + error_pos('Name of value at position %d cannot be empty'); + end + parse_char(':'); + val = parse_value(varargin{:}); + object.(valid_field(str))=val; + if next_char == '}' + break; + end + parse_char(','); + end + end + parse_char('}'); + if(isstruct(object)) + object=struct2jdata(object); + end + +%%------------------------------------------------------------------------- + +function object = parse_array(varargin) % JSON array is written in row-major order +global pos inStr isoct + parse_char('['); + object = cell(0, 1); + dim2=[]; + arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:}); + pbar=-1; + if(isfield(varargin{1},'progressbar_')) + pbar=varargin{1}.progressbar_; + end + + if next_char ~= ']' + if(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:})) + [endpos, e1l, e1r]=matching_bracket(inStr,pos); + arraystr=['[' inStr(pos:endpos)]; + arraystr=regexprep(arraystr,'"_NaN_"','NaN'); + arraystr=regexprep(arraystr,'"([-+]*)_Inf_"','$1Inf'); + arraystr(arraystr==sprintf('\n'))=[]; + arraystr(arraystr==sprintf('\r'))=[]; + %arraystr=regexprep(arraystr,'\s*,',','); % this is slow,sometimes needed + if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D + astr=inStr((e1l+1):(e1r-1)); + astr=regexprep(astr,'"_NaN_"','NaN'); + astr=regexprep(astr,'"([-+]*)_Inf_"','$1Inf'); + astr(astr==sprintf('\n'))=[]; + astr(astr==sprintf('\r'))=[]; + astr(astr==' ')=''; + if(isempty(find(astr=='[', 1))) % array is 2D + dim2=length(sscanf(astr,'%f,',[1 inf])); + end + else % array is 1D + astr=arraystr(2:end-1); + astr(astr==' ')=''; + [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]); + if(nextidx>=length(astr)-1) + object=obj; + pos=endpos; + parse_char(']'); + return; + end + end + if(~isempty(dim2)) + astr=arraystr; + astr(astr=='[')=''; + astr(astr==']')=''; + astr(astr==' ')=''; + [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf); + if(nextidx>=length(astr)-1) + object=reshape(obj,dim2,numel(obj)/dim2)'; + pos=endpos; + parse_char(']'); + if(pbar>0) + waitbar(pos/length(inStr),pbar,'loading ...'); + end + return; + end + end + arraystr=regexprep(arraystr,'\]\s*,','];'); + else + arraystr='['; + end + try + if(isoct && regexp(arraystr,'"','once')) + error('Octave eval can produce empty cells for JSON-like input'); + end + object=eval(arraystr); + pos=endpos; + catch + while 1 + newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1); + val = parse_value(newopt); + object{end+1} = val; + if next_char == ']' + break; + end + parse_char(','); + end + end + end + if(jsonopt('SimplifyCell',0,varargin{:})==1) + try + oldobj=object; + object=cell2mat(object')'; + if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) + object=oldobj; + elseif(size(object,1)>1 && ismatrix(object)) + object=object'; + end + catch + end + end + parse_char(']'); + + if(pbar>0) + waitbar(pos/length(inStr),pbar,'loading ...'); + end +%%------------------------------------------------------------------------- + +function parse_char(c) + global pos inStr len + pos=skip_whitespace(pos,inStr,len); + if pos > len || inStr(pos) ~= c + error_pos(sprintf('Expected %c at position %%d', c)); + else + pos = pos + 1; + pos=skip_whitespace(pos,inStr,len); + end + +%%------------------------------------------------------------------------- + +function c = next_char + global pos inStr len + pos=skip_whitespace(pos,inStr,len); + if pos > len + c = []; + else + c = inStr(pos); + end + +%%------------------------------------------------------------------------- + +function newpos=skip_whitespace(pos,inStr,len) + newpos=pos; + while newpos <= len && isspace(inStr(newpos)) + newpos = newpos + 1; + end + +%%------------------------------------------------------------------------- +function str = parseStr(varargin) + global pos inStr len esc index_esc len_esc + % len, ns = length(inStr), keyboard + if inStr(pos) ~= '"' + error_pos('String starting with " expected at position %d'); + else + pos = pos + 1; + end + str = ''; + while pos <= len + while index_esc <= len_esc && esc(index_esc) < pos + index_esc = index_esc + 1; + end + if index_esc > len_esc + str = [str inStr(pos:len)]; + pos = len + 1; + break; + else + str = [str inStr(pos:esc(index_esc)-1)]; + pos = esc(index_esc); + end + nstr = length(str); + switch inStr(pos) + case '"' + pos = pos + 1; + if(~isempty(str)) + if(strcmp(str,'_Inf_')) + str=Inf; + elseif(strcmp(str,'-_Inf_')) + str=-Inf; + elseif(strcmp(str,'_NaN_')) + str=NaN; + end + end + return; + case '\' + if pos+1 > len + error_pos('End of file reached right after escape character'); + end + pos = pos + 1; + switch inStr(pos) + case {'"' '\' '/'} + str(nstr+1) = inStr(pos); + pos = pos + 1; + case {'b' 'f' 'n' 'r' 't'} + str(nstr+1) = sprintf(['\' inStr(pos)]); + pos = pos + 1; + case 'u' + if pos+4 > len + error_pos('End of file reached in escaped unicode character'); + end + str(nstr+(1:6)) = inStr(pos-1:pos+4); + pos = pos + 5; + end + otherwise % should never happen + str(nstr+1) = inStr(pos); + keyboard; + pos = pos + 1; + end + end + error_pos('End of file while expecting end of inStr'); + +%%------------------------------------------------------------------------- + +function num = parse_number(varargin) + global pos inStr isoct + currstr=inStr(pos:min(pos+30,end)); + if(isoct~=0) + numstr=regexp(currstr,'^\s*-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+\-]?\d+)?','end'); + [num] = sscanf(currstr, '%f', 1); + delta=numstr+1; + else + [num, one, err, delta] = sscanf(currstr, '%f', 1); + if ~isempty(err) + error_pos('Error reading number at position %d'); + end + end + pos = pos + delta-1; + +%%------------------------------------------------------------------------- + +function val = parse_value(varargin) + global pos inStr len + + if(isfield(varargin{1},'progressbar_')) + waitbar(pos/len,varargin{1}.progressbar_,'loading ...'); + end + + switch(inStr(pos)) + case '"' + val = parseStr(varargin{:}); + return; + case '[' + val = parse_array(varargin{:}); + return; + case '{' + val = parse_object(varargin{:}); + return; + case {'-','0','1','2','3','4','5','6','7','8','9'} + val = parse_number(varargin{:}); + return; + case 't' + if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true') + val = true; + pos = pos + 4; + return; + end + case 'f' + if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false') + val = false; + pos = pos + 5; + return; + end + case 'n' + if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null') + val = []; + pos = pos + 4; + return; + end + end + error_pos('Value expected at position %d'); +%%------------------------------------------------------------------------- + +function error_pos(msg) + global pos inStr len + poShow = max(min([pos-15 pos-1 pos pos+20],len),1); + if poShow(3) == poShow(2) + poShow(3:4) = poShow(2)+[0 -1]; % display nothing after + end + msg = [sprintf(msg, pos) ': ' ... + inStr(poShow(1):poShow(2)) '' inStr(poShow(3):poShow(4)) ]; + error( ['JSONparser:invalidFormat: ' msg] ); + +%%------------------------------------------------------------------------- + +function str = valid_field(str) +global isoct +% From MATLAB doc: field names must begin with a letter, which may be +% followed by any combination of letters, digits, and underscores. +% Invalid characters will be converted to underscores, and the prefix +% "x0x[Hex code]_" will be added if the first character is not a letter. + pos=regexp(str,'^[^A-Za-z]','once'); + if(~isempty(pos)) + if(~isoct) + str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); + else + str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); + end + end + if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) + return; + end + if(~isoct) + str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); + else + pos=regexp(str,'[^0-9A-Za-z_]'); + if(isempty(pos)) + return; + end + str0=str; + pos0=[0 pos(:)' length(str)]; + str=''; + for i=1:length(pos) + str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; + end + if(pos(end)~=length(str)) + str=[str str0(pos0(end-1)+1:pos0(end))]; + end + end + %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; + +%%------------------------------------------------------------------------- +function endpos = matching_quote(str,pos) +len=length(str); +while(pos1 && str(pos-1)=='\')) + endpos=pos; + return; + end + end + pos=pos+1; +end +error('unmatched quotation mark'); +%%------------------------------------------------------------------------- +function [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos) +global arraytoken +level=1; +maxlevel=level; +endpos=0; +bpos=arraytoken(arraytoken>=pos); +tokens=str(bpos); +len=length(tokens); +pos=1; +e1l=[]; +e1r=[]; +while(pos<=len) + c=tokens(pos); + if(c==']') + level=level-1; + if(isempty(e1r)) + e1r=bpos(pos); + end + if(level==0) + endpos=bpos(pos); + return + end + end + if(c=='[') + if(isempty(e1l)) + e1l=bpos(pos); + end + level=level+1; + maxlevel=max(maxlevel,level); + end + if(c=='"') + pos=matching_quote(tokens,pos+1); + end + pos=pos+1; +end +if(endpos==0) + error('unmatched "]"'); +end diff --git a/extensions/jsonlab-1.2/loadubjson.m b/extensions/jsonlab-1.2/loadubjson.m new file mode 100644 index 00000000..bb47a6a3 --- /dev/null +++ b/extensions/jsonlab-1.2/loadubjson.m @@ -0,0 +1,457 @@ +function data = loadubjson(fname,varargin) +% +% data=loadubjson(fname,opt) +% or +% data=loadubjson(fname,'param1',value1,'param2',value2,...) +% +% parse a JSON (JavaScript Object Notation) file or string +% +% authors:Qianqian Fang (fangq nmr.mgh.harvard.edu) +% created on 2013/08/01 +% +% $Id$ +% +% input: +% fname: input file name, if fname contains "{}" or "[]", fname +% will be interpreted as a UBJSON string +% opt: a struct to store parsing options, opt can be replaced by +% a list of ('param',value) pairs - the param string is equivallent +% to a field in opt. opt can have the following +% fields (first in [.|.] is the default) +% +% opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat +% for each element of the JSON data, and group +% arrays based on the cell2mat rules. +% opt.IntEndian [B|L]: specify the endianness of the integer fields +% in the UBJSON input data. B - Big-Endian format for +% integers (as required in the UBJSON specification); +% L - input integer fields are in Little-Endian order. +% opt.NameIsString [0|1]: for UBJSON Specification Draft 8 or +% earlier versions (JSONLab 1.0 final or earlier), +% the "name" tag is treated as a string. To load +% these UBJSON data, you need to manually set this +% flag to 1. +% +% output: +% dat: a cell array, where {...} blocks are converted into cell arrays, +% and [...] are converted to arrays +% +% examples: +% obj=struct('string','value','array',[1 2 3]); +% ubjdata=saveubjson('obj',obj); +% dat=loadubjson(ubjdata) +% dat=loadubjson(['examples' filesep 'example1.ubj']) +% dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1) +% +% license: +% BSD License, see LICENSE_BSD.txt files for details +% +% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) +% + +global pos inStr len esc index_esc len_esc isoct arraytoken fileendian systemendian + +if(regexp(fname,'[\{\}\]\[]','once')) + string=fname; +elseif(exist(fname,'file')) + fid = fopen(fname,'rb'); + string = fread(fid,inf,'uint8=>char')'; + fclose(fid); +else + error('input file does not exist'); +end + +pos = 1; len = length(string); inStr = string; +isoct=exist('OCTAVE_VERSION','builtin'); +arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); +jstr=regexprep(inStr,'\\\\',' '); +escquote=regexp(jstr,'\\"'); +arraytoken=sort([arraytoken escquote]); + +% String delimiters and escape chars identified to improve speed: +esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); +index_esc = 1; len_esc = length(esc); + +opt=varargin2struct(varargin{:}); +fileendian=upper(jsonopt('IntEndian','B',opt)); +[os,maxelem,systemendian]=computer; + +jsoncount=1; +while pos <= len + switch(next_char) + case '{' + data{jsoncount} = parse_object(opt); + case '[' + data{jsoncount} = parse_array(opt); + otherwise + error_pos('Outer level structure must be an object or an array'); + end + jsoncount=jsoncount+1; +end % while + +jsoncount=length(data); +if(jsoncount==1 && iscell(data)) + data=data{1}; +end + +%%------------------------------------------------------------------------- +function object = parse_object(varargin) + parse_char('{'); + object = []; + type=''; + count=-1; + if(next_char == '$') + type=inStr(pos+1); % TODO + pos=pos+2; + end + if(next_char == '#') + pos=pos+1; + count=double(parse_number()); + end + if next_char ~= '}' + num=0; + while 1 + if(jsonopt('NameIsString',0,varargin{:})) + str = parseStr(varargin{:}); + else + str = parse_name(varargin{:}); + end + if isempty(str) + error_pos('Name of value at position %d cannot be empty'); + end + %parse_char(':'); + val = parse_value(varargin{:}); + num=num+1; + object.(valid_field(str))=val; + if next_char == '}' || (count>=0 && num>=count) + break; + end + %parse_char(','); + end + end + if(count==-1) + parse_char('}'); + end + if(isstruct(object)) + object=struct2jdata(object); + end + +%%------------------------------------------------------------------------- +function [cid,len]=elem_info(type) +id=strfind('iUIlLdD',type); +dataclass={'int8','uint8','int16','int32','int64','single','double'}; +bytelen=[1,1,2,4,8,4,8]; +if(id>0) + cid=dataclass{id}; + len=bytelen(id); +else + error_pos('unsupported type at position %d'); +end +%%------------------------------------------------------------------------- + + +function [data, adv]=parse_block(type,count,varargin) +global pos inStr isoct fileendian systemendian +[cid,len]=elem_info(type); +datastr=inStr(pos:pos+len*count-1); +if(isoct) + newdata=int8(datastr); +else + newdata=uint8(datastr); +end +id=strfind('iUIlLdD',type); +if(id<=5 && fileendian~=systemendian) + newdata=swapbytes(typecast(newdata,cid)); +end +data=typecast(newdata,cid); +adv=double(len*count); + +%%------------------------------------------------------------------------- + + +function object = parse_array(varargin) % JSON array is written in row-major order +global pos inStr + parse_char('['); + object = cell(0, 1); + dim=[]; + type=''; + count=-1; + if(next_char == '$') + type=inStr(pos+1); + pos=pos+2; + end + if(next_char == '#') + pos=pos+1; + if(next_char=='[') + dim=parse_array(varargin{:}); + count=prod(double(dim)); + else + count=double(parse_number()); + end + end + if(~isempty(type)) + if(count>=0) + [object, adv]=parse_block(type,count,varargin{:}); + if(~isempty(dim)) + object=reshape(object,dim); + end + pos=pos+adv; + return; + else + endpos=matching_bracket(inStr,pos); + [cid,len]=elem_info(type); + count=(endpos-pos)/len; + [object, adv]=parse_block(type,count,varargin{:}); + pos=pos+adv; + parse_char(']'); + return; + end + end + if next_char ~= ']' + while 1 + val = parse_value(varargin{:}); + object{end+1} = val; + if next_char == ']' + break; + end + %parse_char(','); + end + end + if(jsonopt('SimplifyCell',0,varargin{:})==1) + try + oldobj=object; + object=cell2mat(object')'; + if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) + object=oldobj; + elseif(size(object,1)>1 && ismatrix(object)) + object=object'; + end + catch + end + end + if(count==-1) + parse_char(']'); + end + +%%------------------------------------------------------------------------- + +function parse_char(c) + global pos inStr len + skip_whitespace; + if pos > len || inStr(pos) ~= c + error_pos(sprintf('Expected %c at position %%d', c)); + else + pos = pos + 1; + skip_whitespace; + end + +%%------------------------------------------------------------------------- + +function c = next_char + global pos inStr len + skip_whitespace; + if pos > len + c = []; + else + c = inStr(pos); + end + +%%------------------------------------------------------------------------- + +function skip_whitespace + global pos inStr len + while pos <= len && isspace(inStr(pos)) + pos = pos + 1; + end + +%%------------------------------------------------------------------------- +function str = parse_name(varargin) + global pos inStr + bytelen=double(parse_number()); + if(length(inStr)>=pos+bytelen-1) + str=inStr(pos:pos+bytelen-1); + pos=pos+bytelen; + else + error_pos('End of file while expecting end of name'); + end +%%------------------------------------------------------------------------- + +function str = parseStr(varargin) + global pos inStr + % len, ns = length(inStr), keyboard + type=inStr(pos); + if type ~= 'S' && type ~= 'C' && type ~= 'H' + error_pos('String starting with S expected at position %d'); + else + pos = pos + 1; + end + if(type == 'C') + str=inStr(pos); + pos=pos+1; + return; + end + bytelen=double(parse_number()); + if(length(inStr)>=pos+bytelen-1) + str=inStr(pos:pos+bytelen-1); + pos=pos+bytelen; + else + error_pos('End of file while expecting end of inStr'); + end + +%%------------------------------------------------------------------------- + +function num = parse_number(varargin) + global pos inStr isoct fileendian systemendian + id=strfind('iUIlLdD',inStr(pos)); + if(isempty(id)) + error_pos('expecting a number at position %d'); + end + type={'int8','uint8','int16','int32','int64','single','double'}; + bytelen=[1,1,2,4,8,4,8]; + datastr=inStr(pos+1:pos+bytelen(id)); + if(isoct) + newdata=int8(datastr); + else + newdata=uint8(datastr); + end + if(id<=5 && fileendian~=systemendian) + newdata=swapbytes(typecast(newdata,type{id})); + end + num=typecast(newdata,type{id}); + pos = pos + bytelen(id)+1; + +%%------------------------------------------------------------------------- + +function val = parse_value(varargin) + global pos inStr + + switch(inStr(pos)) + case {'S','C','H'} + val = parseStr(varargin{:}); + return; + case '[' + val = parse_array(varargin{:}); + return; + case '{' + val = parse_object(varargin{:}); + return; + case {'i','U','I','l','L','d','D'} + val = parse_number(varargin{:}); + return; + case 'T' + val = true; + pos = pos + 1; + return; + case 'F' + val = false; + pos = pos + 1; + return; + case {'Z','N'} + val = []; + pos = pos + 1; + return; + end + error_pos('Value expected at position %d'); +%%------------------------------------------------------------------------- + +function error_pos(msg) + global pos inStr len + poShow = max(min([pos-15 pos-1 pos pos+20],len),1); + if poShow(3) == poShow(2) + poShow(3:4) = poShow(2)+[0 -1]; % display nothing after + end + msg = [sprintf(msg, pos) ': ' ... + inStr(poShow(1):poShow(2)) '' inStr(poShow(3):poShow(4)) ]; + error( ['JSONparser:invalidFormat: ' msg] ); + +%%------------------------------------------------------------------------- + +function str = valid_field(str) +global isoct +% From MATLAB doc: field names must begin with a letter, which may be +% followed by any combination of letters, digits, and underscores. +% Invalid characters will be converted to underscores, and the prefix +% "x0x[Hex code]_" will be added if the first character is not a letter. + pos=regexp(str,'^[^A-Za-z]','once'); + if(~isempty(pos)) + if(~isoct) + str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); + else + str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); + end + end + if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) + return; + end + if(~isoct) + str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); + else + pos=regexp(str,'[^0-9A-Za-z_]'); + if(isempty(pos)) + return; + end + str0=str; + pos0=[0 pos(:)' length(str)]; + str=''; + for i=1:length(pos) + str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; + end + if(pos(end)~=length(str)) + str=[str str0(pos0(end-1)+1:pos0(end))]; + end + end + %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; + +%%------------------------------------------------------------------------- +function endpos = matching_quote(str,pos) +len=length(str); +while(pos1 && str(pos-1)=='\')) + endpos=pos; + return; + end + end + pos=pos+1; +end +error('unmatched quotation mark'); +%%------------------------------------------------------------------------- +function [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos) +global arraytoken +level=1; +maxlevel=level; +endpos=0; +bpos=arraytoken(arraytoken>=pos); +tokens=str(bpos); +len=length(tokens); +pos=1; +e1l=[]; +e1r=[]; +while(pos<=len) + c=tokens(pos); + if(c==']') + level=level-1; + if(isempty(e1r)) + e1r=bpos(pos); + end + if(level==0) + endpos=bpos(pos); + return + end + end + if(c=='[') + if(isempty(e1l)) + e1l=bpos(pos); + end + level=level+1; + maxlevel=max(maxlevel,level); + end + if(c=='"') + pos=matching_quote(tokens,pos+1); + end + pos=pos+1; +end +if(endpos==0) + error('unmatched "]"'); +end + diff --git a/extensions/jsonlab-1.2/mergestruct.m b/extensions/jsonlab-1.2/mergestruct.m new file mode 100644 index 00000000..c89b9527 --- /dev/null +++ b/extensions/jsonlab-1.2/mergestruct.m @@ -0,0 +1,33 @@ +function s=mergestruct(s1,s2) +% +% s=mergestruct(s1,s2) +% +% merge two struct objects into one +% +% authors:Qianqian Fang (fangq nmr.mgh.harvard.edu) +% date: 2012/12/22 +% +% input: +% s1,s2: a struct object, s1 and s2 can not be arrays +% +% output: +% s: the merged struct object. fields in s1 and s2 will be combined in s. +% +% license: +% BSD License, see LICENSE_BSD.txt files for details +% +% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) +% + +if(~isstruct(s1) || ~isstruct(s2)) + error('input parameters contain non-struct'); +end +if(length(s1)>1 || length(s2)>1) + error('can not merge struct arrays'); +end +fn=fieldnames(s2); +s=s1; +for i=1:length(fn) + s=setfield(s,fn{i},getfield(s2,fn{i})); +end + diff --git a/extensions/jsonlab-1.2/octave-workspace b/extensions/jsonlab-1.2/octave-workspace new file mode 100644 index 0000000000000000000000000000000000000000..12bc9c8d4331770f0e7cbb335d5db5fb731dea7d GIT binary patch literal 28882 zcmeHQ{gT{95nmHRVkbcOEcgTJ>Ie{mZ{=IZfRm71J}HCCPQ~UUU=Xv~xjmirWofkW zB^8R-fTzi8@D_OjO#fyit#lg68cBD)JMOA>SF0UO_w@Aqrn{$S?zSV?if?Ui-JCo% znM_!a(EDWa=*#pr4?}N)iv<%5cPEd&N^gn7JQi;xB2FFgCA#q}y>?xXC$<~VWm~Z% zo$qr;Y&R4G3n0h>CRCA&^pQNUiI^KK|NH3CqYHH7ReD)_q0g;o&OMJimhdABT-t&s z!VX+^-}VD%+d@TlPIOAQrpr%2VAqdb`p~fo0%%dflf(^!CRS1GX?ppg2xI!Bsmen2R_#0G*6Z%mY@IcriwC{5s(Y+>`U#DM|b0vn*cZJ{!*&-$UnR>F; zl}9T4*{KtL&hZGshh)Pj)99Mqe?Y%NlN@)&+q7QOtIyM8g2hWtN**zX)7K`QkP0Ap z%)`KgqV9TZX~!&BsAn)JdT$VV67D`Te z94=ZJ{p8pfRjgF`gf2=x=b?9k#ffn**<`CFT{d<4la(cqPBQti-@@ZV`t5iWGfHD_ ze_YneqPJIMA++%LN{vT77@?ZjC3MfmXQA~UyPoG}Hlh~ptd0~C0>4t_iTeq+!&F3R z8~YL!J?|3H*d?{*F&=4Y?#nf10A(>xQcqY5?uXJ+jA94qA<2EmiYBEk9IPzxQxXjCbYhBh2XoC0o!YwAzMt&p(m;f-C6@_9h#zmBb znXcDnb6CE3JYr(cZV?p9)wQ9m15lZTsUY!OhugyS$>^uAgbFn-Ui0s zH*p2o>nPnYb9{$>ks>0aWyKd8EOB_SAQB)c>mk=B2zZ-0{}n3wcAVblF|Np3FG$un zq~9msqt^pz)l)g!3FDZ&A6&Y*;iY{HH_7tI6q6hg)TI#uM2x`@q@iUK8od;JlcP0*2ZOsB25=Wj1Lc2QIkcXHM$+O-VkYko zt)wstNl&;<4rqHxG>%3-BO8<3EAnjdSJ$gtq^s~D_iVtDDk!^~5hy;)y--L8GXwp- zxFbJGpXimu4?`Lv&9v|ktonKCdJAfAup6eqqN%f?@t`O62>o}nIUQ^%hy0m;{|kTr zc~dIH+@`U!CS58<576Iq2Yw9+m_(91mNcGtnqJb&#}|#@fEpkZv6%Ty;0fvenqKvJ zvI&$Um&UiOlT-gDQ=bWNvW&wtbAfAm0U?1CGAZmCD`1qVEKAH9AdD=EW&^|0tRJpu$@6*eDhQdAd+Sv zMH91j(H$K)=3&x26xmP=4V8hKabk}jQFYxrHJ=neLap04rBGt87xYcQAWm9X!09v*vczdOmL_u(^xv zB?o5v`o+i!4`X`#@w>M6rXpFN1*zaadrGhm}I79fY<9)0NK3cj(l;Y2nq&ro?< zsZi+wP4Q7w2vL!;`$b-Zc4B^sO39UG9H5ZWwBxJaxybMDV6o6fp<aL%`u*mfz=fJA+V%Rx1M-Yp1bwA1b#YjbO zk44IIY)+ib8mEp0=Al`LWDswUAntQic?=DHlNR`inC^BX3Wj_q7i$kKTo#i zEH-B5gAw|Pwo(MWQErSuEg8Vu)`BH_^N_`h#CQmeCJ8Xo2WrPKb)+PGE|{qi{KpaW zfShe14z>_L?5z%e8<~$1PknrYF{Zx~3s&jcSV6}yNfL^!IAT_GkH0Vi(lN9oXCeEP zBgND~`NIfWqT|DGk{g2!%2uSDJS1usonq=qk1?T?*jQg?BL)G$BBV#xV+8PiM72}{ zcq*$IVamP%y4E^-2H;xj>=|Htv90ZW186_)SgBd0=;gLrYf&g8Ecv-n)n;tJ(pGmK zpcop!`jrWhApxpuT?iH7mcH@0*NmuCgF2@8{ptW(h8TUh1F7BtBCBiVJp({f!}^av zHoXHlU3ec5ph22d;{=io@qq!DE+{fMz|v*dRejI?0hBI?Ri)K4K=M|X&KWfTvNM>X z;n;cInBY2QVC2Pn*&HXs0TrGp%$SKD`9>!S3M0N#cK5IVO6 zgrwVJsB&GVGTD+o0f1k2=!cT`+S^&ds{Z3}yG*Ba-N%9N0z&ddEDs!co-XPKgCG;?P$m;@l{XjGl9W+pw*;W;RI-!+L}LgWz|}Ux zWTujE3^IbWNk&c6o__DSW}BlXa#3qCIu`<@J^95sNA$HR^XRAaOCx@vjyr090|j7_ z_atkL(*PAVB($}L81VduDRLU>SyWn?wYt^WPBu$Mb8ev4cGX5+R+~~Hi!RS8Yu~-q zU9r%Bl48-;-gCR`g)&qX(=|F8;?PcD5(fWF}IY_FLh3KNG^T#Er z(RZzqv`!;(V`h;qmvyUP4_yZfrR@p>97Max0@(`}Xm*3rqYxbk#955)%E3#xbTAqo zTA!XKk2P{aFN|q<>(Eb6(|0Ef-2QM|I$&o@4&Yx>*xTz*X6*pZ(*A7tOJ ztIvPFukKH#+5H!@@3(do%XAi4mOWV|hoMr=G^MZj`)CK_R`Tz&NZx8r9i>B~4xXbj z@+1$r+bko6o5(AreZ_fZtE`INo`DdAD?Bdm=qtzbcegk0X6c_8wo>E^PJw5+>smdG zQHmMmLffFn9^HIha&;G>DC?V>j+@55owgo##rTHiOB1Sag@5{TwB-lP=W$ zla}KoPB2QeI@ZdB&?vw;PSkKhYoNi7Vha`txs$%52?1+opNk{0Aiha;S0Uo<+c)1G z1`^+;Iu9L2at5pkxO%$jbAUCWaH8?hloN~+tsMw({*5Rhc?N~#GYTnpj z0V(}I1Sd`eRv?`4mOAKdV2}>G-!=bmJ`p4Lt^B4ALkKD zk4}6&g%Qdh(vZDDC&{kd1)UK|@rR9U^yZ%f0}@q8z)J0@4Mg;Zgbs%ooh3-1|4Be+ zkeoxpS%rkzFrNZ`AWrr_6Lvy<%l2cJE_3s!a$486_O!0EgbLg8+~9LmD5JvZh(T`9 zfGybw(pebSN;nJ&r`k^F4+&ik@H nmr.mgh.harvard.edu) +% created on 2011/09/09 +% +% $Id$ +% +% input: +% rootname: the name of the root-object, when set to '', the root name +% is ignored, however, when opt.ForceRootName is set to 1 (see below), +% the MATLAB variable name will be used as the root name. +% obj: a MATLAB object (array, cell, cell array, struct, struct array, +% class instance). +% filename: a string for the file name to save the output JSON data. +% opt: a struct for additional options, ignore to use default values. +% opt can have the following fields (first in [.|.] is the default) +% +% opt.FileName [''|string]: a file name to save the output JSON data +% opt.FloatFormat ['%.10g'|string]: format to show each numeric element +% of a 1D/2D array; +% opt.ArrayIndent [1|0]: if 1, output explicit data array with +% precedent indentation; if 0, no indentation +% opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D +% array in JSON array format; if sets to 1, an +% array will be shown as a struct with fields +% "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for +% sparse arrays, the non-zero elements will be +% saved to _ArrayData_ field in triplet-format i.e. +% (ix,iy,val) and "_ArrayIsSparse_" will be added +% with a value of 1; for a complex array, the +% _ArrayData_ array will include two columns +% (4 for sparse) to record the real and imaginary +% parts, and also "_ArrayIsComplex_":1 is added. +% opt.ParseLogical [0|1]: if this is set to 1, logical array elem +% will use true/false rather than 1/0. +% opt.SingletArray [0|1]: if this is set to 1, arrays with a single +% numerical element will be shown without a square +% bracket, unless it is the root object; if 0, square +% brackets are forced for any numerical arrays. +% opt.SingletCell [1|0]: if 1, always enclose a cell with "[]" +% even it has only one element; if 0, brackets +% are ignored when a cell has only 1 element. +% opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson +% will use the name of the passed obj variable as the +% root object name; if obj is an expression and +% does not have a name, 'root' will be used; if this +% is set to 0 and rootname is empty, the root level +% will be merged down to the lower level. +% opt.Inf ['"$1_Inf_"'|string]: a customized regular expression pattern +% to represent +/-Inf. The matched pattern is '([-+]*)Inf' +% and $1 represents the sign. For those who want to use +% 1e999 to represent Inf, they can set opt.Inf to '$11e999' +% opt.NaN ['"_NaN_"'|string]: a customized regular expression pattern +% to represent NaN +% opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), +% for example, if opt.JSONP='foo', the JSON data is +% wrapped inside a function call as 'foo(...);' +% opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson +% back to the string form +% opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode. +% opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs) +% +% opt can be replaced by a list of ('param',value) pairs. The param +% string is equivallent to a field in opt and is case sensitive. +% output: +% json: a string in the JSON format (see http://json.org) +% +% examples: +% jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... +% 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... +% 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... +% 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... +% 'MeshCreator','FangQ','MeshTitle','T6 Cube',... +% 'SpecialData',[nan, inf, -inf]); +% savejson('jmesh',jsonmesh) +% savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\t%.5g') +% +% license: +% BSD License, see LICENSE_BSD.txt files for details +% +% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) +% + +if(nargin==1) + varname=inputname(1); + obj=rootname; + if(isempty(varname)) + varname='root'; + end + rootname=varname; +else + varname=inputname(2); +end +if(length(varargin)==1 && ischar(varargin{1})) + opt=struct('filename',varargin{1}); +else + opt=varargin2struct(varargin{:}); +end +opt.IsOctave=exist('OCTAVE_VERSION','builtin'); +if(isfield(opt,'norowbracket')) + warning('Option ''NoRowBracket'' is depreciated, please use ''SingletArray'' and set its value to not(NoRowBracket)'); + if(~isfield(opt,'singletarray')) + opt.singletarray=not(opt.norowbracket); + end +end +rootisarray=0; +rootlevel=1; +forceroot=jsonopt('ForceRootName',0,opt); +if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || ... + iscell(obj) || isobject(obj)) && isempty(rootname) && forceroot==0) + rootisarray=1; + rootlevel=0; +else + if(isempty(rootname)) + rootname=varname; + end +end +if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) + rootname='root'; +end + +whitespaces=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); +if(jsonopt('Compact',0,opt)==1) + whitespaces=struct('tab','','newline','','sep',','); +end +if(~isfield(opt,'whitespaces_')) + opt.whitespaces_=whitespaces; +end + +nl=whitespaces.newline; + +json=obj2json(rootname,obj,rootlevel,opt); +if(rootisarray) + json=sprintf('%s%s',json,nl); +else + json=sprintf('{%s%s%s}\n',nl,json,nl); +end + +jsonp=jsonopt('JSONP','',opt); +if(~isempty(jsonp)) + json=sprintf('%s(%s);%s',jsonp,json,nl); +end + +% save to a file if FileName is set, suggested by Patrick Rapin +filename=jsonopt('FileName','',opt); +if(~isempty(filename)) + if(jsonopt('SaveBinary',0,opt)==1) + fid = fopen(filename, 'wb'); + fwrite(fid,json); + else + fid = fopen(filename, 'wt'); + fwrite(fid,json,'char'); + end + fclose(fid); +end + +%%------------------------------------------------------------------------- +function txt=obj2json(name,item,level,varargin) + +if(iscell(item)) + txt=cell2json(name,item,level,varargin{:}); +elseif(isstruct(item)) + txt=struct2json(name,item,level,varargin{:}); +elseif(ischar(item)) + txt=str2json(name,item,level,varargin{:}); +elseif(isobject(item)) + txt=matlabobject2json(name,item,level,varargin{:}); +else + txt=mat2json(name,item,level,varargin{:}); +end + +%%------------------------------------------------------------------------- +function txt=cell2json(name,item,level,varargin) +txt={}; +if(~iscell(item)) + error('input is not a cell'); +end + +dim=size(item); +if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now + item=reshape(item,dim(1),numel(item)/dim(1)); + dim=size(item); +end +len=numel(item); +ws=jsonopt('whitespaces_',struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')),varargin{:}); +padding0=repmat(ws.tab,1,level); +padding2=repmat(ws.tab,1,level+1); +nl=ws.newline; +bracketlevel=~jsonopt('singletcell',1,varargin{:}); +if(len>bracketlevel) + if(~isempty(name)) + txt={padding0, '"', checkname(name,varargin{:}),'": [', nl}; name=''; + else + txt={padding0, '[', nl}; + end +elseif(len==0) + if(~isempty(name)) + txt={padding0, '"' checkname(name,varargin{:}) '": []'}; name=''; + else + txt={padding0, '[]'}; + end +end +for i=1:dim(1) + if(dim(1)>1) + txt(end+1:end+3)={padding2,'[',nl}; + end + for j=1:dim(2) + txt{end+1}=obj2json(name,item{i,j},level+(dim(1)>1)+(len>bracketlevel),varargin{:}); + if(j1) + txt(end+1:end+3)={nl,padding2,']'}; + end + if(ibracketlevel) + txt(end+1:end+3)={nl,padding0,']'}; +end +txt = sprintf('%s',txt{:}); + +%%------------------------------------------------------------------------- +function txt=struct2json(name,item,level,varargin) +txt={}; +if(~isstruct(item)) + error('input is not a struct'); +end +dim=size(item); +if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now + item=reshape(item,dim(1),numel(item)/dim(1)); + dim=size(item); +end +len=numel(item); +forcearray= (len>1 || (jsonopt('SingletArray',0,varargin{:})==1 && level>0)); +ws=struct('tab',sprintf('\t'),'newline',sprintf('\n')); +ws=jsonopt('whitespaces_',ws,varargin{:}); +padding0=repmat(ws.tab,1,level); +padding2=repmat(ws.tab,1,level+1); +padding1=repmat(ws.tab,1,level+(dim(1)>1)+forcearray); +nl=ws.newline; + +if(isempty(item)) + if(~isempty(name)) + txt={padding0, '"', checkname(name,varargin{:}),'": []'}; + else + txt={padding0, '[]'}; + end + return; +end +if(~isempty(name)) + if(forcearray) + txt={padding0, '"', checkname(name,varargin{:}),'": [', nl}; + end +else + if(forcearray) + txt={padding0, '[', nl}; + end +end +for j=1:dim(2) + if(dim(1)>1) + txt(end+1:end+3)={padding2,'[',nl}; + end + for i=1:dim(1) + names = fieldnames(item(i,j)); + if(~isempty(name) && len==1 && ~forcearray) + txt(end+1:end+5)={padding1, '"', checkname(name,varargin{:}),'": {', nl}; + else + txt(end+1:end+3)={padding1, '{', nl}; + end + if(~isempty(names)) + for e=1:length(names) + txt{end+1}=obj2json(names{e},item(i,j).(names{e}),... + level+(dim(1)>1)+1+forcearray,varargin{:}); + if(e1) + txt(end+1:end+3)={nl,padding2,']'}; + end + if(j1) + txt={padding1, '"', checkname(name,varargin{:}),'": [', nl}; + end +else + if(len>1) + txt={padding1, '[', nl}; + end +end +for e=1:len + val=escapejsonstring(item(e,:)); + if(len==1) + obj=['"' checkname(name,varargin{:}) '": ' '"',val,'"']; + if(isempty(name)) + obj=['"',val,'"']; + end + txt(end+1:end+2)={padding1, obj}; + else + txt(end+1:end+4)={padding0,'"',val,'"'}; + end + if(e==len) + sep=''; + end + txt{end+1}=sep; +end +if(len>1) + txt(end+1:end+3)={nl,padding1,']'}; +end +txt = sprintf('%s',txt{:}); + +%%------------------------------------------------------------------------- +function txt=mat2json(name,item,level,varargin) +if(~isnumeric(item) && ~islogical(item)) + error('input is not an array'); +end +ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); +ws=jsonopt('whitespaces_',ws,varargin{:}); +padding1=repmat(ws.tab,1,level); +padding0=repmat(ws.tab,1,level+1); +nl=ws.newline; +sep=ws.sep; + +if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... + (isempty(item) && any(size(item))) ||jsonopt('ArrayToStruct',0,varargin{:})) + if(isempty(name)) + txt=sprintf('%s{%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... + padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); + else + txt=sprintf('%s"%s": {%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... + padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); + end +else + if(numel(item)==1 && jsonopt('SingletArray',0,varargin{:})==0 && level>0) + numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\[',''),']',''); + else + numtxt=matdata2json(item,level+1,varargin{:}); + end + if(isempty(name)) + txt=sprintf('%s%s',padding1,numtxt); + else + if(numel(item)==1 && jsonopt('SingletArray',0,varargin{:})==0) + txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); + else + txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); + end + end + return; +end +dataformat='%s%s%s%s%s'; + +if(issparse(item)) + [ix,iy]=find(item); + data=full(item(find(item))); + if(~isreal(item)) + data=[real(data(:)),imag(data(:))]; + if(size(item,1)==1) + % Kludge to have data's 'transposedness' match item's. + % (Necessary for complex row vector handling below.) + data=data'; + end + txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); + end + txt=sprintf(dataformat,txt,padding0,'"_ArrayIsSparse_": ','1', sep); + if(size(item,1)==1) + % Row vector, store only column indices. + txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... + matdata2json([iy(:),data'],level+2,varargin{:}), nl); + elseif(size(item,2)==1) + % Column vector, store only row indices. + txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... + matdata2json([ix,data],level+2,varargin{:}), nl); + else + % General case, store row and column indices. + txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... + matdata2json([ix,iy,data],level+2,varargin{:}), nl); + end +else + if(isreal(item)) + txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... + matdata2json(item(:)',level+2,varargin{:}), nl); + else + txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); + txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... + matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl); + end +end +txt=sprintf('%s%s%s',txt,padding1,'}'); + +%%------------------------------------------------------------------------- +function txt=matlabobject2json(name,item,level,varargin) +if numel(item) == 0 %empty object + st = struct(); +else + % "st = struct(item);" would produce an inmutable warning, because it + % make the protected and private properties visible. Instead we get the + % visible properties + propertynames = properties(item); + for p = 1:numel(propertynames) + for o = numel(item):-1:1 % aray of objects + st(o).(propertynames{p}) = item(o).(propertynames{p}); + end + end +end +txt=struct2json(name,st,level,varargin{:}); + +%%------------------------------------------------------------------------- +function txt=matdata2json(mat,level,varargin) + +ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); +ws=jsonopt('whitespaces_',ws,varargin{:}); +tab=ws.tab; +nl=ws.newline; + +if(size(mat,1)==1) + pre=''; + post=''; + level=level-1; +else + pre=sprintf('[%s',nl); + post=sprintf('%s%s]',nl,repmat(tab,1,level-1)); +end + +if(isempty(mat)) + txt='null'; + return; +end +floatformat=jsonopt('FloatFormat','%.10g',varargin{:}); +%if(numel(mat)>1) + formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]]; +%else +% formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\n')]]; +%end + +if(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1) + formatstr=[repmat(tab,1,level) formatstr]; +end + +txt=sprintf(formatstr,mat'); +txt(end-length(nl):end)=[]; +if(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1) + txt=regexprep(txt,'1','true'); + txt=regexprep(txt,'0','false'); +end +%txt=regexprep(mat2str(mat),'\s+',','); +%txt=regexprep(txt,';',sprintf('],\n[')); +% if(nargin>=2 && size(mat,1)>1) +% txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); +% end +txt=[pre txt post]; +if(any(isinf(mat(:)))) + txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); +end +if(any(isnan(mat(:)))) + txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); +end + +%%------------------------------------------------------------------------- +function newname=checkname(name,varargin) +isunpack=jsonopt('UnpackHex',1,varargin{:}); +newname=name; +if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) + return +end +if(isunpack) + isoct=jsonopt('IsOctave',0,varargin{:}); + if(~isoct) + newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); + else + pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); + pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); + if(isempty(pos)) + return; + end + str0=name; + pos0=[0 pend(:)' length(name)]; + newname=''; + for i=1:length(pos) + newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; + end + if(pos(end)~=length(name)) + newname=[newname str0(pos0(end-1)+1:pos0(end))]; + end + end +end + +%%------------------------------------------------------------------------- +function newstr=escapejsonstring(str) +newstr=str; +isoct=exist('OCTAVE_VERSION','builtin'); +if(isoct) + vv=sscanf(OCTAVE_VERSION,'%f'); + if(vv(1)>=3.8) + isoct=0; + end +end +if(isoct) + escapechars={'\\','\"','\/','\a','\f','\n','\r','\t','\v'}; + for i=1:length(escapechars); + newstr=regexprep(newstr,escapechars{i},escapechars{i}); + end + newstr=regexprep(newstr,'\\\\(u[0-9a-fA-F]{4}[^0-9a-fA-F]*)','\$1'); +else + escapechars={'\\','\"','\/','\a','\b','\f','\n','\r','\t','\v'}; + for i=1:length(escapechars); + newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\','\\\\')); + end + newstr=regexprep(newstr,'\\\\(u[0-9a-fA-F]{4}[^0-9a-fA-F]*)','\\$1'); +end diff --git a/extensions/jsonlab-1.2/saveubjson.m b/extensions/jsonlab-1.2/saveubjson.m new file mode 100644 index 00000000..cfba94c0 --- /dev/null +++ b/extensions/jsonlab-1.2/saveubjson.m @@ -0,0 +1,562 @@ +function json=saveubjson(rootname,obj,varargin) +% +% json=saveubjson(rootname,obj,filename) +% or +% json=saveubjson(rootname,obj,opt) +% json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...) +% +% convert a MATLAB object (cell, struct or array) into a Universal +% Binary JSON (UBJSON) binary string +% +% author: Qianqian Fang (fangq nmr.mgh.harvard.edu) +% created on 2013/08/17 +% +% $Id$ +% +% input: +% rootname: the name of the root-object, when set to '', the root name +% is ignored, however, when opt.ForceRootName is set to 1 (see below), +% the MATLAB variable name will be used as the root name. +% obj: a MATLAB object (array, cell, cell array, struct, struct array, +% class instance) +% filename: a string for the file name to save the output UBJSON data +% opt: a struct for additional options, ignore to use default values. +% opt can have the following fields (first in [.|.] is the default) +% +% opt.FileName [''|string]: a file name to save the output JSON data +% opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D +% array in JSON array format; if sets to 1, an +% array will be shown as a struct with fields +% "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for +% sparse arrays, the non-zero elements will be +% saved to _ArrayData_ field in triplet-format i.e. +% (ix,iy,val) and "_ArrayIsSparse_" will be added +% with a value of 1; for a complex array, the +% _ArrayData_ array will include two columns +% (4 for sparse) to record the real and imaginary +% parts, and also "_ArrayIsComplex_":1 is added. +% opt.ParseLogical [1|0]: if this is set to 1, logical array elem +% will use true/false rather than 1/0. +% opt.SingletArray [0|1]: if this is set to 1, arrays with a single +% numerical element will be shown without a square +% bracket, unless it is the root object; if 0, square +% brackets are forced for any numerical arrays. +% opt.SingletCell [1|0]: if 1, always enclose a cell with "[]" +% even it has only one element; if 0, brackets +% are ignored when a cell has only 1 element. +% opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson +% will use the name of the passed obj variable as the +% root object name; if obj is an expression and +% does not have a name, 'root' will be used; if this +% is set to 0 and rootname is empty, the root level +% will be merged down to the lower level. +% opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), +% for example, if opt.JSON='foo', the JSON data is +% wrapped inside a function call as 'foo(...);' +% opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson +% back to the string form +% +% opt can be replaced by a list of ('param',value) pairs. The param +% string is equivallent to a field in opt and is case sensitive. +% output: +% json: a binary string in the UBJSON format (see http://ubjson.org) +% +% examples: +% jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... +% 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... +% 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... +% 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... +% 'MeshCreator','FangQ','MeshTitle','T6 Cube',... +% 'SpecialData',[nan, inf, -inf]); +% saveubjson('jsonmesh',jsonmesh) +% saveubjson('jsonmesh',jsonmesh,'meshdata.ubj') +% +% license: +% BSD License, see LICENSE_BSD.txt files for details +% +% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) +% + +if(nargin==1) + varname=inputname(1); + obj=rootname; + if(isempty(varname)) + varname='root'; + end + rootname=varname; +else + varname=inputname(2); +end +if(length(varargin)==1 && ischar(varargin{1})) + opt=struct('filename',varargin{1}); +else + opt=varargin2struct(varargin{:}); +end +opt.IsOctave=exist('OCTAVE_VERSION','builtin'); +if(isfield(opt,'norowbracket')) + warning('Option ''NoRowBracket'' is depreciated, please use ''SingletArray'' and set its value to not(NoRowBracket)'); + if(~isfield(opt,'singletarray')) + opt.singletarray=not(opt.norowbracket); + end +end +rootisarray=0; +rootlevel=1; +forceroot=jsonopt('ForceRootName',0,opt); +if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || ... + iscell(obj) || isobject(obj)) && isempty(rootname) && forceroot==0) + rootisarray=1; + rootlevel=0; +else + if(isempty(rootname)) + rootname=varname; + end +end +if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) + rootname='root'; +end +json=obj2ubjson(rootname,obj,rootlevel,opt); +if(~rootisarray) + json=['{' json '}']; +end + +jsonp=jsonopt('JSONP','',opt); +if(~isempty(jsonp)) + json=[jsonp '(' json ')']; +end + +% save to a file if FileName is set, suggested by Patrick Rapin +filename=jsonopt('FileName','',opt); +if(~isempty(filename)) + fid = fopen(filename, 'wb'); + fwrite(fid,json); + fclose(fid); +end + +%%------------------------------------------------------------------------- +function txt=obj2ubjson(name,item,level,varargin) + +if(iscell(item)) + txt=cell2ubjson(name,item,level,varargin{:}); +elseif(isstruct(item)) + txt=struct2ubjson(name,item,level,varargin{:}); +elseif(ischar(item)) + txt=str2ubjson(name,item,level,varargin{:}); +elseif(isobject(item)) + txt=matlabobject2ubjson(name,item,level,varargin{:}); +else + txt=mat2ubjson(name,item,level,varargin{:}); +end + +%%------------------------------------------------------------------------- +function txt=cell2ubjson(name,item,level,varargin) +txt=''; +if(~iscell(item)) + error('input is not a cell'); +end + +dim=size(item); +if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now + item=reshape(item,dim(1),numel(item)/dim(1)); + dim=size(item); +end +bracketlevel=~jsonopt('singletcell',1,varargin{:}); +len=numel(item); % let's handle 1D cell first +if(len>bracketlevel) + if(~isempty(name)) + txt=[N_(checkname(name,varargin{:})) '[']; name=''; + else + txt='['; + end +elseif(len==0) + if(~isempty(name)) + txt=[N_(checkname(name,varargin{:})) 'Z']; name=''; + else + txt='Z'; + end +end +for j=1:dim(2) + if(dim(1)>1) + txt=[txt '[']; + end + for i=1:dim(1) + txt=[txt obj2ubjson(name,item{i,j},level+(len>bracketlevel),varargin{:})]; + end + if(dim(1)>1) + txt=[txt ']']; + end +end +if(len>bracketlevel) + txt=[txt ']']; +end + +%%------------------------------------------------------------------------- +function txt=struct2ubjson(name,item,level,varargin) +txt=''; +if(~isstruct(item)) + error('input is not a struct'); +end +dim=size(item); +if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now + item=reshape(item,dim(1),numel(item)/dim(1)); + dim=size(item); +end +len=numel(item); +forcearray= (len>1 || (jsonopt('SingletArray',0,varargin{:})==1 && level>0)); + +if(~isempty(name)) + if(forcearray) + txt=[N_(checkname(name,varargin{:})) '[']; + end +else + if(forcearray) + txt='['; + end +end +for j=1:dim(2) + if(dim(1)>1) + txt=[txt '[']; + end + for i=1:dim(1) + names = fieldnames(item(i,j)); + if(~isempty(name) && len==1 && ~forcearray) + txt=[txt N_(checkname(name,varargin{:})) '{']; + else + txt=[txt '{']; + end + if(~isempty(names)) + for e=1:length(names) + txt=[txt obj2ubjson(names{e},item(i,j).(names{e}),... + level+(dim(1)>1)+1+forcearray,varargin{:})]; + end + end + txt=[txt '}']; + end + if(dim(1)>1) + txt=[txt ']']; + end +end +if(forcearray) + txt=[txt ']']; +end + +%%------------------------------------------------------------------------- +function txt=str2ubjson(name,item,level,varargin) +txt=''; +if(~ischar(item)) + error('input is not a string'); +end +item=reshape(item, max(size(item),[1 0])); +len=size(item,1); + +if(~isempty(name)) + if(len>1) + txt=[N_(checkname(name,varargin{:})) '[']; + end +else + if(len>1) + txt='['; + end +end +for e=1:len + val=item(e,:); + if(len==1) + obj=[N_(checkname(name,varargin{:})) '' '',S_(val),'']; + if(isempty(name)) + obj=['',S_(val),'']; + end + txt=[txt,'',obj]; + else + txt=[txt,'',['',S_(val),'']]; + end +end +if(len>1) + txt=[txt ']']; +end + +%%------------------------------------------------------------------------- +function txt=mat2ubjson(name,item,level,varargin) +if(~isnumeric(item) && ~islogical(item)) + error('input is not an array'); +end + +if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... + (isempty(item) && any(size(item))) ||jsonopt('ArrayToStruct',0,varargin{:})) + cid=I_(uint32(max(size(item)))); + if(isempty(name)) + txt=['{' N_('_ArrayType_'),S_(class(item)),N_('_ArraySize_'),I_a(size(item),cid(1)) ]; + else + if(isempty(item)) + txt=[N_(checkname(name,varargin{:})),'Z']; + return; + else + txt=[N_(checkname(name,varargin{:})),'{',N_('_ArrayType_'),S_(class(item)),N_('_ArraySize_'),I_a(size(item),cid(1))]; + end + end +else + if(isempty(name)) + txt=matdata2ubjson(item,level+1,varargin{:}); + else + if(numel(item)==1 && jsonopt('SingletArray',0,varargin{:})==0) + numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\[',''),']',''); + txt=[N_(checkname(name,varargin{:})) numtxt]; + else + txt=[N_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})]; + end + end + return; +end +if(issparse(item)) + [ix,iy]=find(item); + data=full(item(find(item))); + if(~isreal(item)) + data=[real(data(:)),imag(data(:))]; + if(size(item,1)==1) + % Kludge to have data's 'transposedness' match item's. + % (Necessary for complex row vector handling below.) + data=data'; + end + txt=[txt,N_('_ArrayIsComplex_'),'T']; + end + txt=[txt,N_('_ArrayIsSparse_'),'T']; + if(size(item,1)==1) + % Row vector, store only column indices. + txt=[txt,N_('_ArrayData_'),... + matdata2ubjson([iy(:),data'],level+2,varargin{:})]; + elseif(size(item,2)==1) + % Column vector, store only row indices. + txt=[txt,N_('_ArrayData_'),... + matdata2ubjson([ix,data],level+2,varargin{:})]; + else + % General case, store row and column indices. + txt=[txt,N_('_ArrayData_'),... + matdata2ubjson([ix,iy,data],level+2,varargin{:})]; + end +else + if(isreal(item)) + txt=[txt,N_('_ArrayData_'),... + matdata2ubjson(item(:)',level+2,varargin{:})]; + else + txt=[txt,N_('_ArrayIsComplex_'),'T']; + txt=[txt,N_('_ArrayData_'),... + matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})]; + end +end +txt=[txt,'}']; + +%%------------------------------------------------------------------------- +function txt=matlabobject2ubjson(name,item,level,varargin) +if numel(item) == 0 %empty object + st = struct(); +else + % "st = struct(item);" would produce an inmutable warning, because it + % make the protected and private properties visible. Instead we get the + % visible properties + propertynames = properties(item); + for p = 1:numel(propertynames) + for o = numel(item):-1:1 % aray of objects + st(o).(propertynames{p}) = item(o).(propertynames{p}); + end + end +end +txt=struct2ubjson(name,st,level,varargin{:}); + +%%------------------------------------------------------------------------- +function txt=matdata2ubjson(mat,level,varargin) +if(isempty(mat)) + txt='Z'; + return; +end +type=''; +hasnegtive=(mat<0); +if(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0))) + if(isempty(hasnegtive)) + if(max(mat(:))<=2^8) + type='U'; + end + end + if(isempty(type)) + % todo - need to consider negative ones separately + id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]); + if(isempty(id~=0)) + error('high-precision data is not yet supported'); + end + key='iIlL'; + type=key(id~=0); + end + txt=[I_a(mat(:),type,size(mat))]; +elseif(islogical(mat)) + logicalval='FT'; + if(numel(mat)==1) + txt=logicalval(mat+1); + else + txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')]; + end +else + if(numel(mat)==1) + txt=['[' D_(mat) ']']; + else + txt=D_a(mat(:),'D',size(mat)); + end +end + +%txt=regexprep(mat2str(mat),'\s+',','); +%txt=regexprep(txt,';',sprintf('],[')); +% if(nargin>=2 && size(mat,1)>1) +% txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); +% end +if(any(isinf(mat(:)))) + txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); +end +if(any(isnan(mat(:)))) + txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); +end + +%%------------------------------------------------------------------------- +function newname=checkname(name,varargin) +isunpack=jsonopt('UnpackHex',1,varargin{:}); +newname=name; +if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) + return +end +if(isunpack) + isoct=jsonopt('IsOctave',0,varargin{:}); + if(~isoct) + newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); + else + pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); + pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); + if(isempty(pos)) + return; + end + str0=name; + pos0=[0 pend(:)' length(name)]; + newname=''; + for i=1:length(pos) + newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; + end + if(pos(end)~=length(name)) + newname=[newname str0(pos0(end-1)+1:pos0(end))]; + end + end +end +%%------------------------------------------------------------------------- +function val=N_(str) +val=[I_(int32(length(str))) str]; +%%------------------------------------------------------------------------- +function val=S_(str) +if(length(str)==1) + val=['C' str]; +else + val=['S' I_(int32(length(str))) str]; +end +%%------------------------------------------------------------------------- +function val=I_(num) +if(~isinteger(num)) + error('input is not an integer'); +end +if(num>=0 && num<255) + val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')]; + return; +end +key='iIlL'; +cid={'int8','int16','int32','int64'}; +for i=1:4 + if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1))) + val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')]; + return; + end +end +error('unsupported integer'); + +%%------------------------------------------------------------------------- +function val=D_(num) +if(~isfloat(num)) + error('input is not a float'); +end + +if(isa(num,'single')) + val=['d' data2byte(num,'uint8')]; +else + val=['D' data2byte(num,'uint8')]; +end +%%------------------------------------------------------------------------- +function data=I_a(num,type,dim,format) +id=find(ismember('iUIlL',type)); + +if(id==0) + error('unsupported integer array'); +end + +% based on UBJSON specs, all integer types are stored in big endian format + +if(id==1) + data=data2byte(swapbytes(int8(num)),'uint8'); + blen=1; +elseif(id==2) + data=data2byte(swapbytes(uint8(num)),'uint8'); + blen=1; +elseif(id==3) + data=data2byte(swapbytes(int16(num)),'uint8'); + blen=2; +elseif(id==4) + data=data2byte(swapbytes(int32(num)),'uint8'); + blen=4; +elseif(id==5) + data=data2byte(swapbytes(int64(num)),'uint8'); + blen=8; +end + +if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) + format='opt'; +end +if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) + if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) + cid=I_(uint32(max(dim))); + data=['$' type '#' I_a(dim,cid(1)) data(:)']; + else + data=['$' type '#' I_(int32(numel(data)/blen)) data(:)']; + end + data=['[' data(:)']; +else + data=reshape(data,blen,numel(data)/blen); + data(2:blen+1,:)=data; + data(1,:)=type; + data=data(:)'; + data=['[' data(:)' ']']; +end +%%------------------------------------------------------------------------- +function data=D_a(num,type,dim,format) +id=find(ismember('dD',type)); + +if(id==0) + error('unsupported float array'); +end + +if(id==1) + data=data2byte(single(num),'uint8'); +elseif(id==2) + data=data2byte(double(num),'uint8'); +end + +if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) + format='opt'; +end +if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) + if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) + cid=I_(uint32(max(dim))); + data=['$' type '#' I_a(dim,cid(1)) data(:)']; + else + data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)']; + end + data=['[' data]; +else + data=reshape(data,(id*4),length(data)/(id*4)); + data(2:(id*4+1),:)=data; + data(1,:)=type; + data=data(:)'; + data=['[' data(:)' ']']; +end +%%------------------------------------------------------------------------- +function bytes=data2byte(varargin) +bytes=typecast(varargin{:}); +bytes=bytes(:)'; diff --git a/extensions/jsonlab-1.2/struct2jdata.m b/extensions/jsonlab-1.2/struct2jdata.m new file mode 100644 index 00000000..6526390d --- /dev/null +++ b/extensions/jsonlab-1.2/struct2jdata.m @@ -0,0 +1,96 @@ +function newdata=struct2jdata(data,varargin) +% +% newdata=struct2jdata(data,opt,...) +% +% convert a JData object (in the form of a struct array) into an array +% +% authors:Qianqian Fang (fangq nmr.mgh.harvard.edu) +% +% input: +% data: a struct array. If data contains JData keywords in the first +% level children, these fields are parsed and regrouped into a +% data object (arrays, trees, graphs etc) based on JData +% specification. The JData keywords are +% "_ArrayType_", "_ArraySize_", "_ArrayData_" +% "_ArrayIsSparse_", "_ArrayIsComplex_" +% opt: (optional) a list of 'Param',value pairs for additional options +% The supported options include +% 'Recursive', if set to 1, will apply the conversion to +% every child; 0 to disable +% +% output: +% newdata: the covnerted data if the input data does contain a JData +% structure; otherwise, the same as the input. +% +% examples: +% obj=struct('_ArrayType_','double','_ArraySize_',[2 3], +% '_ArrayIsSparse_',1 ,'_ArrayData_',null); +% ubjdata=struct2jdata(obj); +% +% license: +% BSD License, see LICENSE_BSD.txt files for details +% +% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) +% + +fn=fieldnames(data); +newdata=data; +len=length(data); +if(jsonopt('Recursive',0,varargin{:})==1) + for i=1:length(fn) % depth-first + for j=1:len + if(isstruct(getfield(data(j),fn{i}))) + newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); + end + end + end +end +if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) + newdata=cell(len,1); + for j=1:len + ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); + iscpx=0; + if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) + if(data(j).x0x5F_ArrayIsComplex_) + iscpx=1; + end + end + if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) + if(data(j).x0x5F_ArrayIsSparse_) + if(~isempty(strmatch('x0x5F_ArraySize_',fn))) + dim=double(data(j).x0x5F_ArraySize_); + if(iscpx && size(ndata,2)==4-any(dim==1)) + ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); + end + if isempty(ndata) + % All-zeros sparse + ndata=sparse(dim(1),prod(dim(2:end))); + elseif dim(1)==1 + % Sparse row vector + ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); + elseif dim(2)==1 + % Sparse column vector + ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); + else + % Generic sparse array. + ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); + end + else + if(iscpx && size(ndata,2)==4) + ndata(:,3)=complex(ndata(:,3),ndata(:,4)); + end + ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); + end + end + elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) + if(iscpx && size(ndata,2)==2) + ndata=complex(ndata(:,1),ndata(:,2)); + end + ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); + end + newdata{j}=ndata; + end + if(len==1) + newdata=newdata{1}; + end +end \ No newline at end of file diff --git a/extensions/jsonlab-1.2/varargin2struct.m b/extensions/jsonlab-1.2/varargin2struct.m new file mode 100644 index 00000000..675be650 --- /dev/null +++ b/extensions/jsonlab-1.2/varargin2struct.m @@ -0,0 +1,40 @@ +function opt=varargin2struct(varargin) +% +% opt=varargin2struct('param1',value1,'param2',value2,...) +% or +% opt=varargin2struct(...,optstruct,...) +% +% convert a series of input parameters into a structure +% +% authors:Qianqian Fang (fangq nmr.mgh.harvard.edu) +% date: 2012/12/22 +% +% input: +% 'param', value: the input parameters should be pairs of a string and a value +% optstruct: if a parameter is a struct, the fields will be merged to the output struct +% +% output: +% opt: a struct where opt.param1=value1, opt.param2=value2 ... +% +% license: +% BSD License, see LICENSE_BSD.txt files for details +% +% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) +% + +len=length(varargin); +opt=struct; +if(len==0) return; end +i=1; +while(i<=len) + if(isstruct(varargin{i})) + opt=mergestruct(opt,varargin{i}); + elseif(ischar(varargin{i}) && i1 [FDhdr,vol] = niak_read_vol(in.fmri{rr}); - end + end + if isfield(FDhdr,'extra')&&isfield(FDhdr.extra,'mask_scrubbing') + vol = vol(:,:,:,~FDhdr.extra.mask_scrubbing); + end if rr == 1 tseries = niak_normalize_tseries(niak_vol2tseries(vol,mask)); else diff --git a/extensions/surfstab/niak_pipeline_scores.m b/extensions/surfstab/niak_pipeline_scores.m index 8d219065..a9f0c221 100644 --- a/extensions/surfstab/niak_pipeline_scores.m +++ b/extensions/surfstab/niak_pipeline_scores.m @@ -250,9 +250,6 @@ elseif ischar(opt.files_out.(out_name)) error('OPT.FILES_OUT can only have boolean values but not %s',class(opt.files_out.(out_name))); end - if ~isdir([opt.folder_out filesep out_name]) - psom_mkdir([opt.folder_out filesep out_name]); - end end s_opt = opt.scores; if ~opt.flag_rand diff --git a/pipeline/niak_pipeline_fmri_preprocess.m b/pipeline/niak_pipeline_fmri_preprocess.m index d589a33a..6d039314 100644 --- a/pipeline/niak_pipeline_fmri_preprocess.m +++ b/pipeline/niak_pipeline_fmri_preprocess.m @@ -27,7 +27,7 @@ % % SIZE_OUTPUT % (string, default 'quality_control') possible values : -% 'quality_control, all. +% 'quality_control', 'all'. % The quantity of intermediate results that are generated. % * With the option quality_control, only the preprocessed % data and quality controls at the final stage are generated. @@ -217,6 +217,26 @@ % (real, default: Inf) the cut-off frequency for low pass % filtering. opt.lp = Inf means no low-pass filtering. % +% BUILD_CONFOUNDS +% (structure) Options of NIAK_BRICK_BUILD_CONFOUNDS. +% +% WW_FD +% (vector, default [3 6]) defines the time window to be removed around each time frame +% identified with excessive motion. First value is for time prior to motion peak, and second value +% is for time following motion peak. +% +% NB_VOL_MIN +% (integer, default 40) the minimal number of volumes remaining after +% scrubbing (unless the data themselves are shorter). If there are not enough +% time frames after scrubbing, the time frames with lowest FD are selected. +% +% THRE_FD +% (scalar, default 0.5) the maximal acceptable framewise displacement +% after scrubbing. +% +% COMPCOR +% (structure, default see NIAK_COMPCOR) the OPT argument of NIAK_COMPCOR. +% % REGRESS_CONFOUNDS % (structure) Options of NIAK_BRICK_REGRESS_CONFOUNDS. % FOLDER_OUT @@ -259,27 +279,6 @@ % (boolean, default true) turn on/off the PCA reduction of motion % parameters. % -% CORSICA -% (structure) options of NIAK_PIPELINE_CORSICA (correction of the -% physiological noise based on automatic component selection in an -% independent component analysis). -% -% SICA.NB_COMP -% (integer, default min(60,foor(0.95*T))) -% Number of components to compute (for default : T is the number -% of time samples). -% -% COMPONENT_SUPP.THRESHOLD -% (scalar, default 0.15) a threshold to apply on the score for -% suppression (scores above the thresholds are selected, values -% from 0 to 1). -% -% FLAG_SKIP -% (boolean, default false) if FLAG_SKIP is true, the brick does -% not do anything, just copying the inputs to the outputs (the -% ICA decomposition will still be generated and the component -% selection will still be generated for quality control purposes) -% % SMOOTH_VOL % (structure) options of NIAK_BRICK_SMOOTH_VOL (spatial smoothing). % @@ -292,33 +291,8 @@ % (boolean, default false) if FLAG_SKIP==1, the brick does not do % anything, just copy the input on the output. % -% REGION_GROWING -% (structure) options of NIAK_PIPELINE_REGION_GROWING. -% -% THRE_SIZE -% (integer,default 1000 mm3) threshold on the region size (maximum). -% -% THRE_NB_ROIS -% (integer, default 0) the minimum number of homogeneous -% regions (if no threshold are fixed on size and similarity, -% THRE_NB_ROIS will be the actual number of homogeneous regions). -% -% CORRECTION_AVERAGE -% (structure, default CORRECTION.TYPE = 'mean') the temporal -% normalization to apply on the individual time series before -% averaging in each ROI. See OPT in NIAK_NORMALIZE_TSERIES. -% -% IND_ROIS -% (vector of integer, default all) list of ROIs index that will -% be included in the analysis. By default, the brick is processing -% all the ROIs found in FILES_IN.MASK -% -% FLAG_SKIP -% (boolean, default true) if FLAG_SKIP==1, the brick does not do -% anything. -% % CIVET -% (structure)If this field is present, NIAK will not process the T1 image, +% (structure) If this field is present, NIAK will not process the T1 image, % but will rather grab the (previously generated) results of the CIVET % pipeline, i.e. copy/rename them. The following fields need % to be specified : @@ -397,17 +371,16 @@ % See NIAK_BRICK_QC_COREGISTER % 9. Estimation of a temporal model of slow time drifts. % See NIAK_BRICK_TIME_FILTER -% 10. Regression of confounds (slow time drifts, motion parameters, -% WM average, global signal) and scrubbing of time frames with -% an excessive motion. +% 10. Generation of confounds (slow time drifts, motion parameters, +% WM average, COMPCOR, global signal) preceeded by scrubbing of time frames +% with an excessive motion. +% See NIAK_BRICK_BUILD_CONFOUNDS and OPT.BUILD_CONFOUNDS. +% 11. Regression of confounds (slow time drifts, motion parameters, +% WM average, COMPCOR, global signal) preceeded by scrubbing of time frames +% with an excessive motion. % See NIAK_BRICK_REGRESS_CONFOUNDS and OPT.REGRESS_CONFOUNDS -% 11. Correction of physiological noise. -% See NIAK_PIPELINE_CORSICA and OPT.CORSICA -% Also, see NIAK_BRICK_MASK_CORSICA (no option there) % 12. Spatial smoothing. % See NIAK_BRICK_SMOOTH_VOL and OPT.SMOOTH_VOL -% 13. Region growing. -% See NIAK_PIPELINE_REGION_GROWING and OPT.REGION_GROWING % % In addition the following jobs are performed at the group level: % 1. Group quality control for motion correction. @@ -418,48 +391,22 @@ % See NIAK_BRICK_QC_COREGISTER % 3. Group quality control for coregistration of fMRI in stereotaxic % space. -% 4. Group quality control for the change in variance following -% motion correction. -% 5. Group quality control for the variance explained by slow time -% drifts. -% 6. Group quality control for the variance explained by the regression -% of motion parameters (F-test). -% 7. Group quality control for the variance explained by the regression -% of the average signal in the white matter (F-test). -% 8. Group quality control for the variance explained by the regression -% of the global signal estimate (using PCA, F-test). -% 9. Group quality control for the variance explained by custom confound -% covariates (F-test). -% 10. Group quality control for the explained explained by the selection of -% physiological noise components in an ICA (CORSICA). % % NOTE 2: -% The physiological & motion noise correction CORSICA is changing the -% degrees of freedom of the data. It is usullay negligible for -% intra-subject analysis, and will have no impact on the between-subject -% variance estimate (except those should be less noisy). The slice timing -% and scrubbing of time series also changes the sampling grid in time. -% These effects have to be taken into account in the linear model -% analysis. This will be taken care of in the (yet to come) -% NIAK_PIPELINE_FMRISTAT -% -% NOTE 3: % The exact list of outputs generated by the pipeline depend on the % OPT.SIZE_OUTPUTS field. See the internet documentation for details : % http://www.nitrc.org/plugins/mwiki/index.php/niak:FmriPreprocessing % -% NOTE 4: +% NOTE 3: % The PSOM pipeline manager is used to process the pipeline if % OPT.FLAG_TEST is false. PSOM has a number of interesting features to % deal with job failures or pipeline updates. You can read the following -% tutorial for a review of its capabilities : -% http://code.google.com/p/psom/wiki/HowToUsePsom -% http://code.google.com/p/psom/wiki/PsomConfiguration -% +% documentation to learn more on its capabilities : +% http://psom.simexp-lab.org % _________________________________________________________________________ % Copyright (c) Pierre Bellec, Centre de recherche de l'institut de % geriatrie de Montreal, Departement d'informatique et recherche -% operationnelle, Universite de Montreal, 2010-2014. +% operationnelle, Universite de Montreal, 2010-2016. % Maintainer : pierre.bellec@criugm.qc.ca % See licensing information in the code. % Keywords : pipeline, niak, preprocessing, fMRI, psom @@ -502,8 +449,8 @@ %% OPT opt = sub_backwards(opt); % Fiddling with OPT for backwards compatibility -list_fields = { 'civet' , 'target_space' , 'flag_rand' , 'granularity' , 'tune' , 'flag_verbose' , 'template' , 'size_output' , 'folder_out' , 'folder_logs' , 'folder_fmri' , 'folder_anat' , 'folder_qc' , 'folder_intermediate' , 'flag_test' , 'psom' , 'slice_timing' , 'motion' , 'qc_motion_correction_ind' , 't1_preprocess' , 'pve' , 'mask_anat2func' , 'anat2func' , 'qc_coregister' , 'corsica' , 'time_filter' , 'resample_vol' , 'smooth_vol' , 'region_growing' , 'regress_confounds' }; -list_defaults = { 'gb_niak_omitted' , 'stereonl' , false , 'cleanup' , struct() , true , 'mni_icbm152_nlin_sym_09a' , 'quality_control' , NaN , '' , '' , '' , '' , '' , false , struct() , struct() , struct() , struct() , struct() , struct(), struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() }; +list_fields = { 'civet' , 'target_space' , 'flag_rand' , 'granularity' , 'tune' , 'flag_verbose' , 'template' , 'size_output' , 'folder_out' , 'folder_logs' , 'folder_fmri' , 'folder_anat' , 'folder_qc' , 'folder_intermediate' , 'flag_test' , 'psom' , 'slice_timing' , 'motion' , 'qc_motion_correction_ind' , 't1_preprocess' , 'pve' , 'mask_anat2func' , 'anat2func' , 'qc_coregister' , 'time_filter' , 'resample_vol' , 'smooth_vol' , 'build_confounds' , 'regress_confounds' }; +list_defaults = { 'gb_niak_omitted' , 'stereonl' , false , 'cleanup' , struct() , true , 'mni_icbm152_nlin_sym_09a' , 'quality_control' , NaN , '' , '' , '' , '' , '' , false , struct() , struct() , struct() , struct() , struct() , struct(), struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() }; opt = psom_struct_defaults(opt,list_fields,list_defaults); opt.folder_out = niak_full_path(opt.folder_out); opt.psom.path_logs = [opt.folder_out 'logs' filesep]; @@ -514,10 +461,6 @@ opt.civet = psom_struct_defaults(opt.civet,list_fields,list_defaults); end -if ~isfield(opt.region_growing,'flag_skip') % By default, skip the region growing - opt.region_growing.flag_skip = true; -end - if ~ismember(opt.size_output,{'quality_control','all'}) % check that the size of outputs is a valid option error('%s is an unknown option for OPT.SIZE_OUTPUT. Available options are ''quality_control'', ''all''',opt.size_output) end @@ -562,6 +505,7 @@ opt.template = psom_struct_defaults(opt.template, ... { 't1' , 'fmri' , 'aal' , 'mask' , 'mask_dilated' , 'mask_eroded' , 'mask_bold' , 'mask_avg' , 'mask_wm' , 'mask_vent' , 'mask_willis' }, ... { NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN }); + template = opt.template; end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -569,22 +513,39 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% pipeline = struct(); +%% Save the pipeline parameters +pipeline.pipe_params.command = 'save(files_out,''-struct'',''opt'')'; +pipeline.pipe_params.files_out = [opt.folder_out 'pipe_parameters.mat']; +pipeline.pipe_params.opt.opt = opt; +pipeline.pipe_params.opt.files_in = files_in; + %% Resample the AAL template clear job_in job_out job_opt [path_t,name_t,ext_t] = niak_fileparts(opt.template.fmri); job_in.source = opt.template.aal; job_in.target = opt.template.aal; -job_out = [opt.folder_out 'anat' filesep 'template_aal' ext_t]; +job_out = [opt.folder_out 'anat' filesep 'template_aal' ext_f]; job_opt = opt.resample_vol; job_opt.interpolation = 'nearest_neighbour'; pipeline = psom_add_job(pipeline,'resample_aal','niak_brick_resample_vol',job_in,job_out,job_opt); opt.template.aal = pipeline.resample_aal.files_out; +%% Copy the description of confounds +pipeline.cp_confounds_keys.files_in = [gb_niak_path_niak 'template' filesep 'niak_confounds.json']; +pipeline.cp_confounds_keys.files_out = [opt.folder_out 'resample' filesep 'niak_confounds.json']; +pipeline.cp_confounds_keys.command = '[status,msg] = copyfile(files_in,files_out); if status~=0; error(msg); end'; + +%% Copy the template +pipeline.cp_template.files_in = {template.t1}; +pipeline.cp_template.files_out = {[opt.folder_out 'anat' filesep 'template_anat_stereo' ext_f]}; +pipeline.cp_template.opt.flag_fmri = true; +pipeline.cp_template.command = 'niak_brick_copy(files_in,files_out,opt);'; + %% Resample the fMRI stereotaxic space, if needed pipeline.resample_fmri_stereo = pipeline.resample_aal; pipeline.resample_fmri_stereo.files_in.source = opt.template.fmri; pipeline.resample_fmri_stereo.files_in.target = opt.template.fmri; -pipeline.resample_fmri_stereo.files_out = [opt.folder_out 'anat' filesep 'template_fmri_stereo' ext_t]; +pipeline.resample_fmri_stereo.files_out = [opt.folder_out 'anat' filesep 'template_fmri_stereo' ext_f]; opt.template.fmri = pipeline.resample_fmri_stereo.files_out; %% Build individual pipelines @@ -749,46 +710,6 @@ fprintf('%1.2f sec\n',etime(clock,t1)); end -%% GROUP QC CONFOUNDS : regression of slow time drifts, white matter average, motion parameters, global signal and (optional) custom parameters -if opt.flag_verbose - t1 = clock; - fprintf('Adding group-level quality control of confound regression (slow time drifts, motion parameters, etc; F-test) ; '); -end -list_maps = { 'qc_wm' , 'qc_vent' , 'qc_slow_drift' , 'qc_high' , 'qc_motion' , 'qc_compcor' , 'qc_gse' , 'qc_custom_param' }; -for num_m = 1:length(list_maps) - clear job_in job_out job_opt - job_in.vol = cell([length(fmri_c) 1]); - job_in.mask = pipeline.qc_group_coregister_func.files_out.mask_group; - for num_e = 1:length(fmri_c) - if strcmp(opt.granularity,'subject') - ind = find(ismember(pipeline.(['preproc_' label(num_e).subject]).opt.list_jobs,['confounds_' label(num_e).name])); - tmp = pipeline.(['preproc_' label(num_e).subject]).opt.pipeline{ind}.files_out; - else - tmp = pipeline.(['confounds_' label(num_e).name]).files_out; - end - if isfield(tmp,list_maps{num_m})&&~strcmp(tmp.(list_maps{num_m}),'gb_niak_omitted') - job_in.vol{num_e} = tmp.(list_maps{num_m}); - flag_ok = true; - else - flag_ok = false; - continue - end - end - if ~flag_ok - continue - end - job_out.mean_vol = [opt.folder_out filesep 'quality_control' filesep 'group_confounds' filesep 'func_' list_maps{num_m} '_' opt.target_space '_mean' ext_f]; - job_out.std_vol = [opt.folder_out filesep 'quality_control' filesep 'group_confounds' filesep 'func_' list_maps{num_m} '_' opt.target_space '_std' ext_f]; - job_out.fig_coregister = [opt.folder_out filesep 'quality_control' filesep 'group_confounds' filesep 'func_' list_maps{num_m} '_' opt.target_space '_fit.pdf']; - job_out.tab_coregister = [opt.folder_out filesep 'quality_control' filesep 'group_confounds' filesep 'func_' list_maps{num_m} '_' opt.target_space '_fit.csv']; - job_opt = opt.qc_coregister; - job_opt.labels_subject = {label.name}; - pipeline = psom_add_job(pipeline,['qc_group_' list_maps{num_m}(4:end)],'niak_brick_qc_coregister',job_in,job_out,job_opt); -end -if opt.flag_verbose - fprintf('%1.2f sec\n',etime(clock,t1)); -end - %% GROUP QC CONFOUNDS : scrubbing if opt.flag_verbose t1 = clock; @@ -800,7 +721,7 @@ ind = find(ismember(pipeline.(['preproc_' label(num_e).subject]).opt.list_jobs,['confounds_' label(num_e).name])); job_in.(label(num_e).name) = pipeline.(['preproc_' label(num_e).subject]).opt.pipeline{ind}.files_out.scrubbing; else - job_in.(label(num_e).name) = pipeline.(['confounds_' label(num_e).name]).files_out.scrubbing; + job_in.(label(num_e).name) = pipeline.(['regress_confounds_' label(num_e).name]).files_out.scrubbing; end end job_out = [opt.folder_out filesep 'quality_control' filesep 'group_motion' filesep 'qc_scrubbing_group.csv']; @@ -810,64 +731,19 @@ fprintf('%1.2f sec\n',etime(clock,t1)); end -%% GROUP QC CORSICA +%% Generate the pipeline report if opt.flag_verbose t1 = clock; - fprintf('Adding group-level quality control of CORSICA (ratio of variance maps) ; '); + fprintf('Adding the report on fMRI preprocessing ; '); end -clear job_in job_out job_opt -job_in.vol = cell([length(fmri_c) 1]); -job_in.mask = pipeline.qc_group_coregister_func.files_out.mask_group; -for num_e = 1:length(fmri_c) - if strcmp(opt.granularity,'subject') - ind = find(ismember(pipeline.(['preproc_' label(num_e).subject]).opt.list_jobs,['qc_corsica_var_' label(num_e).name])); - job_in.vol{num_e} = pipeline.(['preproc_' label(num_e).subject]).opt.pipeline{ind}.files_out; - else - job_in.vol{num_e} = pipeline.(['qc_corsica_var_' label(num_e).name]).files_out; - end -end -job_out.mean_vol = [opt.folder_out filesep 'quality_control' filesep 'group_corsica' filesep 'func_ratio_var_corsica_' opt.target_space '_mean' ext_f]; -job_out.std_vol = [opt.folder_out filesep 'quality_control' filesep 'group_corsica' filesep 'func_ratio_var_corsica_' opt.target_space '_std' ext_f]; -job_out.fig_coregister = [opt.folder_out filesep 'quality_control' filesep 'group_corsica' filesep 'func_ratio_var_corsica_' opt.target_space '_fit.pdf']; -job_out.tab_coregister = [opt.folder_out filesep 'quality_control' filesep 'group_corsica' filesep 'func_ratio_var_corsica_' opt.target_space '_fit.csv']; -job_opt = opt.qc_coregister; -job_opt.labels_subject = {label.name}; -pipeline = psom_add_job(pipeline,'qc_group_corsica_var_group','niak_brick_qc_coregister',job_in,job_out,job_opt); +files_report = niak_grab_report_preprocess(opt.folder_out,files_in); +opt_rep.folder_out = [opt.folder_out 'report']; +opt_rep.flag_test = true; +pipeline = psom_merge_pipeline(pipeline,niak_report_fmri_preprocess(files_report,opt_rep),'rep_'); if opt.flag_verbose fprintf('%1.2f sec\n',etime(clock,t1)); end -%% Region Growing -if ~opt.region_growing.flag_skip - if opt.flag_verbose - t1 = clock; - fprintf('Generating pipeline for the region growing ; '); - end - clear job_in job_out job_opt - job_opt = rmfield(opt.region_growing,'flag_skip'); - job_opt.folder_out = [opt.folder_out filesep 'region_growing' filesep]; - job_opt.flag_test = true; - for num_e = 1:length(fmri_c) - if strcmp(opt.granularity,'subject') - ind = find(ismember(pipeline.(['preproc_' label(num_e).subject]).opt.list_jobs,['smooth_' label(num_e).name])); - job_in.fmri.(label(num_e).subject).(label(num_e).session).(label(num_e).run) = pipeline.(['preproc_' label(num_e).subject]).opt.pipeline{ind}.files_out; - else - job_in.fmri.(label(num_e).subject).(label(num_e).session).(label(num_e).run) = pipeline.(['smooth_' label(num_e).name]).files_out; - end - end - job_in.areas = pipeline.resample_aal.files_out; - job_in.mask = pipeline.qc_group_coregister_func.files_out.mask_group; - if strcmp(opt.granularity,'subject') - [pipeline.region_growing,pipeline.clean_region_growing] = psom_pipeline2job(niak_pipeline_region_growing(job_in,job_opt),[opt.psom.path_logs 'region_growing']); - else - pipeline = psom_merge_pipeline(pipeline,niak_pipeline_region_growing(job_in,job_opt)); - end - - if opt.flag_verbose - fprintf('%1.2f sec\n',etime(clock,t1)); - end -end - %% Run the pipeline if ~opt.flag_test psom_run_pipeline(pipeline,opt.psom); @@ -945,28 +821,9 @@ opt = rmfield(opt,'template_t1'); end -if isfield(opt,'bricks') +if isfield(opt,'bricks') % Backwards compatibility with opt.bricks opt = psom_merge_pipeline(opt,opt.bricks); opt = rmfield(opt,'bricks'); - if isfield(opt,'flag_corsica'); - opt.corsica.flag_skip = ~opt.flag_corsica; - opt = rmfield(opt,'flag_corsica'); - end - if isfield(opt,'sica'); - opt.corsica.sica = opt.sica; - opt = rmfield(opt,'sica'); - end - if isfield(opt,'component_sel'); - opt.corsica.component_sel = opt.component_sel; - opt = rmfield(opt,'component_sel'); - end - if isfield(opt,'component_supp'); - opt.corsica.component_supp = opt.component_supp; - opt = rmfield(opt,'component_supp'); - if isfield(opt.corsica.component_supp,'threshold') - opt.corsica.threshold = opt.corsica.component_supp.threshold; - end - end end function opt_ind = sub_tune(opt,subject) diff --git a/pipeline/niak_pipeline_fmri_preprocess_ind.m b/pipeline/niak_pipeline_fmri_preprocess_ind.m index f02fe14e..e8a5430b 100644 --- a/pipeline/niak_pipeline_fmri_preprocess_ind.m +++ b/pipeline/niak_pipeline_fmri_preprocess_ind.m @@ -88,8 +88,12 @@ % (string, default FOLDER_OUT/logs/) where to write the logs of the % pipeline. % +% FOLDER_RESAMPLE +% (string, default FOLDER_OUT/resample/) where to write the minimally +% preprocessed (spatially resampled) fMRI volumes. +% % FOLDER_FMRI -% (string, default FOLDER_OUT/fmri/) where to write the preprocessed +% (string, default FOLDER_OUT/fmri/) where to write the fully preprocessed % fMRI volumes. % % FOLDER_ANAT @@ -246,11 +250,28 @@ % (real, default: Inf) the cut-off frequency for low pass % filtering. opt.lp = Inf means no low-pass filtering. % +% BUILD_CONFOUNDS +% (structure) Options of NIAK_BRICK_BUILD_CONFOUNDS. +% +% WW_FD +% (vector, default [3 6]) defines the time window to be removed around each time frame +% identified with excessive motion. First value is for time prior to motion peak, and second value +% is for time following motion peak. +% +% NB_VOL_MIN +% (integer, default 40) the minimal number of volumes remaining after +% scrubbing (unless the data themselves are shorter). If there are not enough +% time frames after scrubbing, the time frames with lowest FD are selected. +% +% THRE_FD +% (scalar, default 0.5) the maximal acceptable framewise displacement +% after scrubbing. +% +% COMPCOR +% (structure, default see NIAK_COMPCOR) the OPT argument of NIAK_COMPCOR. +% % REGRESS_CONFOUNDS % (structure) Options of NIAK_BRICK_REGRESS_CONFOUNDS. -% FOLDER_OUT -% (string, default folder of FMRI) the folder where the default outputs -% are generated. % % FLAG_SLOW % (boolean, default true) turn on/off the correction of slow time drifts @@ -277,38 +298,10 @@ % (boolean, default true) turn on/off the removal of the average % signal in the lateral ventricles. % -% PCT_VAR_EXPLAINED -% (boolean, default 0.95) the % of variance explained by the selected -% PCA components when reducing the dimensionality of motion parameters. -% -% COMPCOR -% (structure, default see NIAK_COMPCOR) the OPT argument of NIAK_COMPCOR. -% % FLAG_PCA_MOTION % (boolean, default true) turn on/off the PCA reduction of motion % parameters. % -% CORSICA -% (structure) options of NIAK_PIPELINE_CORSICA (correction of the -% physiological noise based on automatic component selection in an -% independent component analysis). -% -% SICA.NB_COMP -% (integer, default min(60,foor(0.95*T))) -% Number of components to compute (for default : T is the number -% of time samples). -% -% COMPONENT_SUPP.THRESHOLD -% (scalar, default 0.15) a threshold to apply on the score for -% suppression (scores above the thresholds are selected, values -% from 0 to 1). -% -% FLAG_SKIP -% (boolean, default true) if FLAG_SKIP is true, the brick does -% not do anything, just copying the inputs to the outputs (the -% ICA decomposition will still be generated and the component -% selection will still be generated for quality control purposes) -% % SMOOTH_VOL % (structure) options of NIAK_BRICK_SMOOTH_VOL (spatial smoothing). % @@ -374,32 +367,23 @@ % See NIAK_BRICK_QC_COREGISTER % 9. Estimation of a temporal model of slow time drifts. % See NIAK_BRICK_TIME_FILTER -% 10. Regression of confounds (slow time drifts, motion parameters, +% 10. Generation of confounds (slow time drifts, motion parameters, +% WM average, COMPCOR, global signal) preceeded by scrubbing of time frames +% with an excessive motion. +% See NIAK_BRICK_BUILD_CONFOUNDS and OPT.BUILD_CONFOUNDS. +% 11. Regression of confounds (slow time drifts, motion parameters, % WM average, COMPCOR, global signal) preceeded by scrubbing of time frames % with an excessive motion. % See NIAK_BRICK_REGRESS_CONFOUNDS and OPT.REGRESS_CONFOUNDS -% 11. Correction of physiological noise. -% See NIAK_PIPELINE_CORSICA and OPT.CORSICA -% Also, see NIAK_BRICK_MASK_CORSICA (no option there) % 12. Spatial smoothing. % See NIAK_BRICK_SMOOTH_VOL and OPT.SMOOTH_VOL % % NOTE 2: -% The physiological & motion noise correction CORSICA is changing the -% degrees of freedom of the data. It is usullay negligible for -% intra-label analysis, and will have no impact on the between-label -% variance estimate (except those should be less noisy). The slice timing -% and scrubbing of time series also changes the sampling grid in time. -% These effects have to be taken into account in the linear model -% analysis. This will be taken care of in the (yet to come) -% NIAK_PIPELINE_FMRISTAT -% -% NOTE 3: % The exact list of outputs generated by the pipeline depend on the % OPT.SIZE_OUTPUTS field. See the internet documentation for details : % http://www.nitrc.org/plugins/mwiki/index.php/niak:FmriPreprocessing % -% NOTE 4: +% NOTE 3: % The PSOM pipeline manager is used to process the pipeline if % OPT.FLAG_TEST is false. PSOM has a number of interesting features to % deal with job failures or pipeline updates. You can read the following @@ -449,13 +433,11 @@ files_in = sub_check_format(files_in); % Checking that FILES_IN is in the correct format %% OPT -list_fields = { 'civet' , 'target_space' , 'rand_seed' , 'subject' , 'template' , 'size_output' , 'folder_out' , 'folder_logs' , 'folder_fmri' , 'folder_anat' , 'folder_qc' , 'folder_intermediate' , 'flag_test' , 'flag_verbose' , 'psom' , 'slice_timing' , 'motion' , 'qc_motion_correction_ind' , 't1_preprocess' , 'pve' , 'mask_anat2func' , 'anat2func' , 'qc_coregister' , 'corsica' , 'time_filter' , 'resample_vol' , 'smooth_vol' , 'region_growing' , 'regress_confounds'}; -list_defaults = { 'gb_niak_omitted' , 'stereonl' , [] , NaN , NaN , 'quality_control' , NaN , '' , '' , '' , '' , '' , false , false , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() }; +list_fields = { 'civet' , 'target_space' , 'rand_seed' , 'subject' , 'template' , 'size_output' , 'folder_out' , 'folder_logs' , 'folder_resample' , 'folder_fmri' , 'folder_anat' , 'folder_qc' , 'folder_intermediate' , 'flag_test' , 'flag_verbose' , 'psom' , 'slice_timing' , 'motion' , 'qc_motion_correction_ind' , 't1_preprocess' , 'pve' , 'mask_anat2func' , 'anat2func' , 'qc_coregister' , 'time_filter' , 'resample_vol' , 'smooth_vol' , 'build_confounds' , 'regress_confounds'}; +list_defaults = { 'gb_niak_omitted' , 'stereonl' , [] , NaN , NaN , 'quality_control' , NaN , '' , '' , '' , '' , '' , '' , false , false , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() , struct() }; opt = psom_struct_defaults(opt,list_fields,list_defaults); subject = opt.subject; -opt.corsica = psom_struct_defaults(opt.corsica,{'flag_skip'},{true},false); % Skip CORSICA by default - opt.template = psom_struct_defaults(opt.template, ... { 't1' , 'fmri' , 'aal' , 'mask' , 'mask_dilated' , 'mask_eroded' , 'mask_bold' , 'mask_avg' , 'mask_wm' , 'mask_vent' , 'mask_willis' }, ... { NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN }); @@ -474,6 +456,10 @@ opt.folder_logs = [opt.folder_out 'logs']; end +if isempty(opt.folder_resample) + opt.folder_resample = [opt.folder_out 'resample']; +end + if isempty(opt.folder_fmri) opt.folder_fmri = [opt.folder_out 'fmri']; end @@ -687,13 +673,10 @@ otherwise error('%s is an unknown target space (see OPT.TARGET_SPACE)',opt.target_space) end - job_out = ''; + job_out = [opt.folder_resample filesep 'fmri_' label(num_e).name '_n' ext_f]; job_opt = opt.resample_vol; - job_opt.folder_out = [opt.folder_intermediate 'resample' filesep]; + job_opt.folder_out = [opt.folder_resample filesep]; pipeline = psom_add_job(pipeline,['resample_' label(num_e).name],'niak_brick_resample_vol',job_in,job_out,job_opt); - if strcmp(opt.size_output,'quality_control') - pipeline = psom_add_clean(pipeline,['clean_resample_' label(num_e).name],pipeline.(['resample_' label(num_e).name]).files_out); - end end if opt.flag_verbose fprintf('%1.2f sec) - ',etime(clock,t1)); @@ -720,7 +703,7 @@ fprintf('%1.2f sec) - ',etime(clock,t1)); end -%% CORSICA masks +%% Confounds masks if opt.flag_verbose t1 = clock; fprintf('corsica ('); @@ -734,12 +717,12 @@ job_in.functional_space = pipeline.(['qc_motion_' subject]).files_out.mask_group; job_in.transformation_nl = pipeline.(['t1_preprocess_',subject]).files_out.transformation_nl; job_in.segmentation = pipeline.(['t1_preprocess_' subject]).files_out.classify; -job_out.mask_vent_ind = [opt.folder_qc 'corsica' filesep subject '_mask_vent_func' opt.target_space ext_f]; -job_out.mask_stem_ind = [opt.folder_qc 'corsica' filesep subject '_mask_stem_func' opt.target_space ext_f]; -job_out.white_matter_ind = [opt.folder_qc 'corsica' filesep subject '_mask_wm_func' opt.target_space ext_f]; +job_out.mask_vent_ind = [opt.folder_anat 'func_' subject '_mask_vent_stereo' ext_f]; +job_out.mask_stem_ind = [opt.folder_anat 'func_' subject '_mask_stem_stereo' ext_f]; +job_out.white_matter_ind = [opt.folder_anat 'func_' subject '_mask_wm_stereo' ext_f]; job_opt.target_space = opt.target_space; job_opt.flag_test = false; -pipeline = psom_add_job(pipeline,['mask_corsica_' subject],'niak_brick_mask_corsica',job_in,job_out,job_opt); +pipeline = psom_add_job(pipeline,['mask_confounds_' subject],'niak_brick_mask_corsica',job_in,job_out,job_opt); %% temporal filtering if opt.flag_verbose @@ -762,94 +745,55 @@ fprintf('%1.2f sec) - ',etime(clock,t1)); end -%% Regress Confounds +%% Build confounds if opt.flag_verbose t1 = clock; - fprintf('regress confounds ('); + fprintf('Build confounds ('); end for num_e = 1:length(fmri) clear job_opt job_in job_out job_in.fmri = pipeline.(['resample_' label(num_e).name]).files_out; job_in.dc_low = pipeline.(['time_filter_' label(num_e).name]).files_out.dc_low; job_in.dc_high = pipeline.(['time_filter_' label(num_e).name]).files_out.dc_high; - job_in.mask_vent = pipeline.(['mask_corsica_' subject]).files_out.mask_vent_ind; - job_in.mask_wm = pipeline.(['mask_corsica_' subject]).files_out.white_matter_ind; + job_in.mask_vent = pipeline.(['mask_confounds_' subject]).files_out.mask_vent_ind; + job_in.mask_wm = pipeline.(['mask_confounds_' subject]).files_out.white_matter_ind; job_in.mask_brain = pipeline.(['qc_motion_' subject]).files_out.mask_group; job_in.motion_param = pipeline.(['motion_parameters_' label(num_e).name]).files_out; job_in.custom_param = files_in.custom_confounds; - job_opt = opt.regress_confounds; - job_opt.folder_out = [opt.folder_intermediate 'regress_confounds' filesep]; - job_out.filtered_data = [job_opt.folder_out filesep 'fmri_' label(num_e).name '_cor' ext_f]; - job_out.confounds = [job_opt.folder_out filesep 'confounds_gs_' label(num_e).name '_cor.mat']; - job_out.scrubbing = [job_opt.folder_out filesep 'scrubbing_' label(num_e).name '.mat']; - job_out.compcor_mask = [job_opt.folder_out filesep 'compcor_mask_' label(num_e).name ext_f]; - job_out.qc_wm = [opt.folder_qc filesep 'regress_confounds' filesep label(num_e).name '_qc_wm_func' opt.target_space ext_f]; - job_out.qc_vent = [opt.folder_qc filesep 'regress_confounds' filesep label(num_e).name '_qc_vent_func' opt.target_space ext_f]; - job_out.qc_slow_drift = [opt.folder_qc filesep 'regress_confounds' filesep label(num_e).name '_qc_slow_drift_func' opt.target_space ext_f]; - job_out.qc_high = [opt.folder_qc filesep 'regress_confounds' filesep label(num_e).name '_qc_high_func' opt.target_space ext_f]; - job_out.qc_motion = [opt.folder_qc filesep 'regress_confounds' filesep label(num_e).name '_qc_motion_func' opt.target_space ext_f]; - job_out.qc_compcor = [opt.folder_qc filesep 'regress_confounds' filesep label(num_e).name '_qc_compcor_func' opt.target_space ext_f]; - job_out.qc_gse = [opt.folder_qc filesep 'regress_confounds' filesep label(num_e).name '_qc_gse_func' opt.target_space ext_f]; - if ~strcmp(job_in.custom_param,'gb_niak_omitted') - job_out.qc_custom_param = [opt.folder_qc filesep 'regress_confounds' filesep label(num_e).name '_qc_gse_func' opt.target_space ext_f]; - end - pipeline = psom_add_job(pipeline,['confounds_' label(num_e).name],'niak_brick_regress_confounds',job_in,job_out,job_opt); - if strcmp(opt.size_output,'quality_control') - pipeline = psom_add_clean(pipeline,['clean_confounds_' label(num_e).name],pipeline.(['confounds_' label(num_e).name]).files_out.filtered_data); - end + job_opt = opt.build_confounds; + job_opt.folder_out = opt.folder_resample; + + job_out.confounds = [job_opt.folder_out filesep 'fmri_' label(num_e).name '_n_confounds.tsv' gb_niak_zip_ext]; + job_out.compcor_mask = [opt.folder_intermediate filesep 'regress_confounds' filesep 'fmri_' label(num_e).name '_mask_compcor_stereo' ext_f]; + pipeline = psom_add_job(pipeline,['build_confounds_' label(num_e).name],'niak_brick_build_confounds',job_in,job_out,job_opt); end if opt.flag_verbose fprintf('%1.2f sec) - ',etime(clock,t1)); end -%% CORSICA -clear job_in job_out job_opt -job_in.(subject).fmri = cell(length(fmri),1); -for num_e = 1:length(fmri) - job_in.(subject).fmri{num_e} = pipeline.(['confounds_' label(num_e).name]).files_out.filtered_data; -end -job_in.(subject).component_to_keep = files_in.component_to_keep; -job_in.(subject).mask_brain = pipeline.(['qc_motion_' subject]).files_out.mask_group; -job_in.(subject).mask_selection{1} = pipeline.(['mask_corsica_' subject]).files_out.mask_vent_ind; -job_in.(subject).mask_selection{2} = pipeline.(['mask_corsica_' subject]).files_out.mask_stem_ind; -job_opt = opt.corsica; -job_opt.rand_seed = opt.rand_seed; -if isfield(opt.corsica,'size_output') - job_opt.size_output = opt.corsica.size_output; -else - job_opt.size_output = opt.size_output; +%% Regress Confounds +if opt.flag_verbose + t1 = clock; + fprintf('regress confounds ('); end -job_opt.folder_out = [opt.folder_intermediate 'corsica' filesep]; -job_opt.folder_sica = [opt.folder_qc 'corsica' filesep]; -job_opt.flag_test = true; -job_opt.labels_mask = {'ventricles','stem'}; -[pipeline_corsica,job_opt,files_co] = niak_pipeline_corsica(job_in,job_opt); -files_co = files_co.suppress_vol.(subject); -pipeline = psom_merge_pipeline(pipeline,pipeline_corsica); -if strcmp(opt.size_output,'quality_control') - pipeline = psom_add_clean(pipeline,['clean_corsica_' subject],files_co); +for num_e = 1:length(fmri) + clear job_opt job_in job_out + job_in.fmri = pipeline.(['resample_' label(num_e).name]).files_out; + job_in.confounds = pipeline.(['build_confounds_' label(num_e).name]).files_out.confounds; + job_opt = opt.regress_confounds; + job_opt.folder_out = [opt.folder_intermediate 'regress_confounds' filesep]; + job_out.filtered_data = [job_opt.folder_out filesep 'fmri_' label(num_e).name '_cor' ext_f]; + job_out.scrubbing = [job_opt.folder_out filesep 'scrubbing_' label(num_e).name '.mat']; + pipeline = psom_add_job(pipeline,['regress_confounds_' label(num_e).name],'niak_brick_regress_confounds',job_in,job_out,job_opt); + if strcmp(opt.size_output,'quality_control') + pipeline = psom_add_clean(pipeline,['clean_confounds_' label(num_e).name],pipeline.(['regress_confounds_' label(num_e).name]).files_out.filtered_data); + end end - if opt.flag_verbose fprintf('%1.2f sec) - ',etime(clock,t1)); end -%% CORSICA QC: variance explained -for num_e = 1:size(files_co,1) - clear job_in job_out job_opt - name_job = ['qc_corsica_var_' label(num_e).name]; - job_in{1} = files_co{num_e}; - job_in{2} = pipeline.(['confounds_' label(num_e).name]).files_out.filtered_data; - job_out = [opt.folder_qc 'corsica' filesep name_job '_func' opt.target_space ext_f]; - job_opt.operation = [ 'var1 = std(vol_in{1},[],4).^2; ' ... - 'var2 = std(vol_in{2},[],4).^2; ' ... - 'mask = (var1>0) & (var2>0); ' ... - 'vol = ones(size(var1)); ' ... - 'vol(mask)=var1(mask)./var2(mask);' ]; - pipeline = psom_add_job(pipeline,name_job,'niak_brick_math_vol',job_in,job_out,job_opt); -end - %% Spatial smoothing (stereotaxic space) if opt.flag_verbose t1 = clock; @@ -857,7 +801,7 @@ end for num_e = 1:length(fmri) clear job_in job_out job_opt - job_in = files_co{num_e}; + job_in = pipeline.(['regress_confounds_' label(num_e).name]).files_out.filtered_data; job_out = [opt.folder_fmri filesep 'fmri_' label(num_e).name ext_f]; job_opt = opt.smooth_vol; pipeline = psom_add_job(pipeline,['smooth_' label(num_e).name],'niak_brick_smooth_vol',job_in,job_out,job_opt); diff --git a/reports/fmri_preprocess/niak_brick_preproc_anat2report.m b/reports/fmri_preprocess/niak_brick_preproc_anat2report.m new file mode 100644 index 00000000..287e0410 --- /dev/null +++ b/reports/fmri_preprocess/niak_brick_preproc_anat2report.m @@ -0,0 +1,93 @@ +function [in,out,opt] = niak_brick_preproc_anat2report(in,out,opt) +% Generate javascript formatted measure of quality for anatomical registration +% +% SYNTAX: [IN,OUT,OPT] = NIAK_BRICK_PREPROC_ANAT2REPORT(IN,OUT,OPT) +% +% IN (string) The name of a .csv file with measures of spatial correlation +% between individual and average group BOLD images, after registration. +% OUT (string) The name of a .js file with one variable: +% dataT1 spatial correlation of individual and group anat images. +% OPT.FLAG_TEST (boolean, default false) if the flag is true, the brick does nothing but +% update IN, OUT and OPT. +% +% Copyright (c) Pierre Bellec +% Centre de recherche de l'Institut universitaire de griatrie de Montral, 2016. +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords: preprocessing report + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +%% Defaults +if ~ischar(in) + error('IN should be a string'); +end + +if ~ischar(out) + error('OUT should be a string'); +end + +if nargin < 3 + opt = struct; +end +opt = psom_struct_defaults ( opt , ... + { 'flag_test' }, ... + { false }); + +if opt.flag_test + return +end + +%% Load parameters +tab = niak_read_csv_cell(in); + +%% Spatial correlation +text_t1 = sprintf('var dataT1 = [\n[''Subject'' '); +val = str2double(tab(2:end,3)); +[val,order] = sort(val,'ascend'); +order = order(:)'+1; +for ss = order + text_t1 = [text_t1, ', ''' tab{ss,1} '''']; +end +text_t1 = [text_t1 sprintf('],\n[''corr_target'' ')]; +for ss = order + text_t1 = [text_t1, ', ''' tab{ss,3} '''']; +end +text_t1 = [text_t1 sprintf(']\n];\nvar dataOverlapT1 = [\n[''Subject'' ')]; + +%% overlap with brain mask +val = str2double(tab(2:end,2)); +[val,order] = sort(val,'ascend'); +order = order(:)'+1; +for ss = order + text_t1 = [text_t1, ', ''' tab{ss,1} '''']; +end +text_t1 = [text_t1 sprintf('],\n[''overlap_brain'' ')]; +for ss = order + text_t1 = [text_t1, ', ''' tab{ss,2} '''']; +end +text_t1 = [text_t1 sprintf(']\n];\n')]; + +%% Write output +[hf,msg] = fopen(out,'w'); +if hf == -1 + error(msg) +end +fprintf(hf,'%s',text_t1); +fclose(hf); diff --git a/reports/fmri_preprocess/niak_brick_preproc_func2report.m b/reports/fmri_preprocess/niak_brick_preproc_func2report.m new file mode 100644 index 00000000..e2b96883 --- /dev/null +++ b/reports/fmri_preprocess/niak_brick_preproc_func2report.m @@ -0,0 +1,96 @@ +function [in,out,opt] = niak_brick_preproc_func2report(in,out,opt) +% Generate javascript formatted measure of quality for functional registration +% +% SYNTAX: [IN,OUT,OPT] = NIAK_BRICK_PREPROC_FUNC2REPORT(IN,OUT,OPT) +% +% IN (string) The name of a .csv file with measures of brain mask overlap with +% group brain mask, as well as spatial correlation between individual and +% average group BOLD images, after registration. +% OUT (string) the name of a .js file with two variables: +% dataBOLD spatial correlation of individual and group BOLD images. +% dataBrain is a measure of overlap between the individual brain functional +% mask and the group functional mask. +% OPT.FLAG_TEST (boolean, default false) if the flag is true, the brick does nothing but +% update IN, OUT and OPT. +% +% Copyright (c) Pierre Bellec +% Centre de recherche de l'Institut universitaire de griatrie de Montral, 2016. +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords: preprocessing report + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +%% Defaults +if ~ischar(in) + error('IN should be a string'); +end + +if ~ischar(out) + error('OUT should be a string'); +end + +if nargin < 3 + opt = struct; +end +opt = psom_struct_defaults ( opt , ... + { 'flag_test' }, ... + { false }); + +if opt.flag_test + return +end + +%% Load parameters +tab = niak_read_csv_cell(in); + +%% Spatial correlation +text_bold = sprintf('var dataBOLD = [\n[''Subject'' '); +val = str2double(tab(2:end,3)); +[val,order] = sort(val,'ascend'); +order = order(:)'+1; +for ss = order + text_bold = [text_bold, ', ''' tab{ss,1} '''']; +end +text_bold = [text_bold sprintf('],\n[''corr_target'' ')]; +for ss = order + text_bold = [text_bold, ', ''' tab{ss,3} '''']; +end +text_bold = [text_bold sprintf(']\n];\nvar dataBrain = [\n[''Subject'' ')]; + +%% overlap with brain mask +val = str2double(tab(2:end,2)); +[val,order] = sort(val,'ascend'); +order = order(:)'+1; +for ss = order + text_bold = [text_bold, ', ''' tab{ss,1} '''']; +end +text_bold = [text_bold sprintf('],\n[''overlap_brain'' ')]; +for ss = order + text_bold = [text_bold, ', ''' tab{ss,2} '''']; +end +text_bold = [text_bold sprintf(']\n];\n')]; + +%% Write output +[hf,msg] = fopen(out,'w'); +if hf == -1 + error(msg) +end +fprintf(hf,'%s',text_bold); +fclose(hf); diff --git a/reports/fmri_preprocess/niak_brick_preproc_ind_motion2report.m b/reports/fmri_preprocess/niak_brick_preproc_ind_motion2report.m new file mode 100644 index 00000000..64d04b94 --- /dev/null +++ b/reports/fmri_preprocess/niak_brick_preproc_ind_motion2report.m @@ -0,0 +1,93 @@ +function [in,out,opt] = niak_brick_preproc_ind_motion2report(in,out,opt) +% Generate javascript formatted measure of intra-subject inter-run registration +% +% SYNTAX: [IN,OUT,OPT] = NIAK_BRICK_PREPROC_IND_MOTION2REPORT(IN,OUT,OPT) +% +% IN.(SUBJECT) (string) The name of a .csv file with individual confound measures +% for one run. +% OUT (string) the name of a .js file with three variables: +% tsl, rot and fd. +% OPT.FLAG_TEST (boolean, default false) if the flag is true, the brick does nothing but +% update IN, OUT and OPT. +% +% Copyright (c) Pierre Bellec +% Centre de recherche de l'Institut universitaire de griatrie de Montral, 2016. +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords: preprocessing report + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +%% Defaults +if ~ischar(in) + error('IN should be a string'); +end + +if ~ischar(out) + error('OUT should be a string'); +end + +if nargin < 3 + opt = struct; +end +opt = psom_struct_defaults ( opt , ... + { 'flag_test' }, ... + { false }); + +if opt.flag_test + return +end + +%% Load confounds +tab = niak_read_csv_cell(in); +labels_tsl = {'motion_tx','motion_ty','motion_tz'}; +text_js = sub_add_js('',tab(:,ismember(tab(1,:),labels_tsl)),'tsl'); +labels_rot = {'motion_rx','motion_ry','motion_rz'}; +text_js = sub_add_js(text_js,tab(:,ismember(tab(1,:),labels_rot)),'rot'); +labels_fd = {'FD','scrub'}; +text_js = sub_add_js(text_js,tab(:,ismember(tab(1,:),labels_fd)),'fd'); + +%% Write output +[hf,msg] = fopen(out,'w'); +if hf == -1 + error(msg) +end +fprintf(hf,'%s',text_js); +fclose(hf); + +function text_js = sub_add_js(text_js,tab,name) + +%% Compose js text +text_js = [text_js sprintf('var %s = {\n columns: [\n',name)]; +for ii = 1:size(tab,2) + text_js = [text_js sprintf(' [''%s'' ',tab{1,ii})]; + for ss = 2:size(tab,1) + text_js = [text_js, ', ' tab{ss,ii} ]; + end + if ii == size(tab,2) + text_js = [text_js sprintf(']\n')]; + else + text_js = [text_js sprintf('],\n')]; + end +end +text_js = sprintf([text_js ' ],\n selection: {\n' ... + ' enabled: true\n' ... + ' },\n' ... + ' onclick: function (d) { selectTime(d.index);}\n' ... + '};\n']); \ No newline at end of file diff --git a/reports/fmri_preprocess/niak_brick_preproc_intra2report.m b/reports/fmri_preprocess/niak_brick_preproc_intra2report.m new file mode 100644 index 00000000..bb11bb9e --- /dev/null +++ b/reports/fmri_preprocess/niak_brick_preproc_intra2report.m @@ -0,0 +1,102 @@ +function [in,out,opt] = niak_brick_preproc_intra2report(in,out,opt) +% Generate javascript formatted measure of intra-subject inter-run registration +% +% SYNTAX: [IN,OUT,OPT] = NIAK_BRICK_PREPROC_INTRA2REPORT(IN,OUT,OPT) +% +% IN.(SUBJECT) (string) The name of a .csv file with measures of intra-subject, +% inter-run motion levels. +% OUT (string) the name of a .js file with one variable: +% dataFD +% OPT.FLAG_TEST (boolean, default false) if the flag is true, the brick does nothing but +% update IN, OUT and OPT. +% +% Copyright (c) Pierre Bellec +% Centre de recherche de l'Institut universitaire de griatrie de Montral, 2016. +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords: preprocessing report + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +%% Defaults +if ~isstruct(in) + error('IN should be a structure'); +end + +if ~ischar(out) + error('OUT should be a string'); +end + +if nargin < 3 + opt = struct; +end +opt = psom_struct_defaults ( opt , ... + { 'flag_test' }, ... + { false }); + +if opt.flag_test + return +end + +%% Load parameters +list_subject = fieldnames(in); +for ss = 1:length(list_subject) + tab_tmp = niak_read_csv_cell(in.(list_subject{ss})); + if ss == 1 + tab = tab_tmp; + else + tab = [ tab ; tab_tmp(2:end,:)]; + end +end + +%% Get rid of prefix/suffix +for rr=2:size(tab,1) + label_run = tab{rr,1}; + tab{rr,1} = label_run(6:(end-2)); +end + +%% Compose js text +list_ind = [1 3]; +ind_ref = 3; +labels = { 'Run' , 'corr_target' }; +val = str2double(tab(2:end,ind_ref)); +[val,order] = sort(val,'ascend'); +order = order(:)'+1; + +text_js = sprintf('var dataIntra = [\n'); +for ii = 1:length(list_ind) + text_js = [text_js sprintf(' [''%s'' ',labels{ii})]; + for ss = order + text_js = [text_js, ', ''' tab{ss,list_ind(ii)} '''']; + end + if ii == length(list_ind) + text_js = [text_js sprintf(']\n')]; + else + text_js = [text_js sprintf('],\n')]; + end +end +text_js = [text_js sprintf('];\n')]; + +%% Write output +[hf,msg] = fopen(out,'w'); +if hf == -1 + error(msg) +end +fprintf(hf,'%s',text_js); +fclose(hf); diff --git a/reports/fmri_preprocess/niak_brick_preproc_motion2report.m b/reports/fmri_preprocess/niak_brick_preproc_motion2report.m new file mode 100644 index 00000000..b2813449 --- /dev/null +++ b/reports/fmri_preprocess/niak_brick_preproc_motion2report.m @@ -0,0 +1,90 @@ +function [in,out,opt] = niak_brick_preproc_motion2report(in,out,opt) +% Generate an html motion report +% +% SYNTAX: [IN,OUT,OPT] = NIAK_BRICK_PREPROC_PARAMS2REPORT(IN,OUT,OPT) +% +% IN (string) not used. +% OUT (string) the name of the .html report. +% OPT.NUM_RUN (integer) the number of the run, as indexed in the full report. +% OPT.LABEL (string) subject_session_run label. +% OPT.LABEL_REF (string) subject_session_run label for the volume of reference (to +% check coregistration). +% OPT.FLAG_TEST (boolean, default false) if the flag is true, the brick does nothing but +% update IN, OUT and OPT. +% +% Copyright (c) Pierre Bellec +% Centre de recherche de l'Institut universitaire de griatrie de Montral, 2016. +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords: preprocessing report + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +%% Defaults +if ~ischar(out) + error('OUT should be a string'); +end + +if nargin < 3 + opt = struct; +end +opt = psom_struct_defaults ( opt , ... + { 'num_run' , 'label_ref' , 'label' , 'flag_test' }, ... + { NaN , NaN , NaN , false }); + +if opt.flag_test + return +end + +%% The template +niak_gb_vars; +file_template = [gb_niak_path_niak 'reports' filesep 'fmri_preprocess' filesep 'templates' filesep 'motion' filesep 'motion_template.html']; + +%% Read template +hf = fopen(file_template); +str_template = fread(hf,Inf,'uint8=>char')'; +fclose(hf); + +%% Update template + +% Motion native +str_template = strrep(str_template,'$MOTION_NATIVE',['motion_native_' opt.label '.png']); +% Motion stereo +str_template = strrep(str_template,'$MOTION_STEREO',['motion_stereo_' opt.label '.png']); +% spacer +str_template = strrep(str_template,'$SPACER_NATIVE',['target_native_' opt.label '.png']); +% spacer stereo (same as native ...) +str_template = strrep(str_template,'$SPACER_STEREO',['target_stereo_' opt.label '.png']); +% The target space +str_template = strrep(str_template,'$VOL_RUN',['target_stereo_' opt.label '.png']); +% The target space +str_template = strrep(str_template,'$NUM_RUN',num2str(opt.num_run)); +% The reference volume +str_template = strrep(str_template,'$REF_VOLUME',['target_stereo_' opt.label_ref '.png']); + +% file with motion parameters +str_template = strrep(str_template,'$dataMotion',['dataMotion_' opt.label '.js']); + +%% Write report +[hf,msg] = fopen(out,'w'); +if hf == -1 + error(msg) +end +fprintf(hf,'%s',str_template); +fclose(hf); \ No newline at end of file diff --git a/reports/fmri_preprocess/niak_brick_preproc_params2report.m b/reports/fmri_preprocess/niak_brick_preproc_params2report.m new file mode 100644 index 00000000..177e2cb6 --- /dev/null +++ b/reports/fmri_preprocess/niak_brick_preproc_params2report.m @@ -0,0 +1,144 @@ +function [in,out,opt] = niak_brick_preproc_params2report(in,out,opt) +% Generate javascript formatted description of fMRI preprocessing parameters +% +% SYNTAX: [IN,OUT,OPT] = NIAK_BRICK_PREPROC_PARAMS2REPORT(IN,OUT,OPT) +% +% IN (string) The name of a .mat file with two variables FILES_IN (the input files) and +% OPT (the options), describing the parameters of the pipeline. +% OUT.LIST_SUBJECT (string) the name of a .js file with a description of the list +% of subjects, in a variable listSubject. +% OUT.LIST_RUN (string) the name of a .js file with a description of the list of +% runs, in a variable listRun. +% OUT.FILES_IN (string) the name of a .js file with a json description of the +% pipeline options, in a variable opt, as well as a function buildFilesIn that +% generates a .json description of the input file for a particular subject. +% OUT.SUMMARY (string) the name of a .js file with a string summarizing the +% pipeline options. +% OPT.FLAG_TEST (boolean, default false) if the flag is true, the brick does nothing but +% update IN, OUT and OPT. +% +% Copyright (c) Pierre Bellec +% Centre de recherche de l'Institut universitaire de griatrie de Montral, 2016. +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords: preprocessing report + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +%% Defaults +if ~ischar(in) + error('IN should be a string'); +end + +out = psom_struct_defaults ( out , ... + { 'list_subject' , 'list_run' , 'files_in' , 'summary' }, ... + { NaN , NaN , NaN , NaN }); + +if nargin < 3 + opt = struct; +end +opt = psom_struct_defaults ( opt , ... + { 'flag_test' }, ... + { false }); + +if opt.flag_test + return +end + +%% Load parameters +data = load(in); +if ~isfield(data,'files_in') + error('I could not find the variable FILES_IN in the input file'); +end +if ~isfield(data,'opt') + error('I could not find the variable OPT in the input file'); +end +[list_fmri,labels] = niak_fmri2cell(data.files_in); +list_subject = fieldnames(data.files_in); + +%% List of subjects +text_subject = sprintf('var listSubject = [\n'); +for ss = 1:(length(list_subject)-1) + text_subject = [text_subject sprintf('{id: %i, text: ''%s'' },\n',ss,list_subject{ss})]; +end +text_subject = sprintf('%s{id: %i, text: ''%s'' }\n];\n',text_subject,length(list_subject),list_subject{end}); + +[hf,msg] = fopen(out.list_subject,'w'); +if hf == -1 + error(msg) +end +fprintf(hf,'%s',text_subject); +fclose(hf); + +%% List of runs +text_run = sprintf([' // Data structure describing all available runs\n' ... + ' var dataRun = [\n']); +for ll = 1:(length(labels)-1) + text_run = [ text_run sprintf(' { id: %i, text: ''%s'' },\n',ll,labels(ll).name)]; +end +text_run = [ text_run sprintf(' { id: %i, text: ''%s'' }\n ];\n',length(labels),labels(end).name)]; + +[hf,msg] = fopen(out.list_run,'w'); +if hf == -1 + error(msg) +end +fprintf(hf,'%s',text_run); +fclose(hf); + +%% List of input filesep +text_files = sprintf(['function buildFilesIn (evt) {\n' ... + ' switch(evt.params.data.id) {\n']); +for ss = 1:length(list_subject) + json_files = savejson('',data.files_in.(list_subject{ss})); + text_files = [text_files sprintf(' case "%i":\n var filesIn = %s\n',ss,json_files)]; +end +text_files = [text_files sprintf(['};\n' ... + 'return filesIn \n}\n'])]; + +text_files = [text_files sprintf('\nvar opt = %s\n',savejson('',data.opt))]; + +[hf,msg] = fopen(out.files_in,'w'); +if hf == -1 + error(msg) +end +fprintf(hf,'%s',text_files); +fclose(hf); + +%% Pipeline summary +niak_gb_vars; +psom_gb_vars; +[status,ver_minc] = system('echo $MINC_TOOLKIT_VERSION'); +ver_minc = ver_minc(1:(end-1)); +%String.fromCharCode(13) +text_js = sprintf(['var pipeSummary = ''

This report on fMRI preprocessing for %i subjects' ... + ' was prepared by user "%s" using the system "%s", ' ... + 'on %s. The version of the software was as follows:

' ... + '

%s version "%s"

' ... + '

Minc-toolkit version "%s"

' ... + '

PSOM version "%s", located in %s

' ... + '

NIAK version "%s", located in %s

'';'],length(list_subject),gb_niak_user, ... + gb_psom_localhost,datestr(clock),gb_niak_language,gb_niak_language_version, ... + ver_minc,gb_psom_version,gb_psom_path_psom,gb_niak_version,gb_niak_path_niak); +[hf,msg] = fopen(out.summary,'w'); +if hf == -1 + error(msg) +end +fprintf(hf,'%s',text_js); +fclose(hf); + diff --git a/reports/fmri_preprocess/niak_brick_preproc_scrubbing2report.m b/reports/fmri_preprocess/niak_brick_preproc_scrubbing2report.m new file mode 100644 index 00000000..c1cce417 --- /dev/null +++ b/reports/fmri_preprocess/niak_brick_preproc_scrubbing2report.m @@ -0,0 +1,110 @@ +function [in,out,opt] = niak_brick_preproc_scrubbing2report(in,out,opt) +% Generate javascript formatted measure of motion levels and scrubbing +% +% SYNTAX: [IN,OUT,OPT] = NIAK_BRICK_PREPROC_SCRUBBING2REPORT(IN,OUT,OPT) +% +% IN (string) The name of a .csv file with measures of motion levels (frame +% displacement and scrubbing). +% OUT (string) the name of a .js file with one variable: +% dataFD +% OPT.FLAG_TEST (boolean, default false) if the flag is true, the brick does nothing but +% update IN, OUT and OPT. +% +% Copyright (c) Pierre Bellec +% Centre de recherche de l'Institut universitaire de griatrie de Montral, 2016. +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords: preprocessing report + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +%% Defaults +if ~ischar(in) + error('IN should be a string'); +end + +if ~ischar(out) + error('OUT should be a string'); +end + +if nargin < 3 + opt = struct; +end +opt = psom_struct_defaults ( opt , ... + { 'flag_test' }, ... + { false }); + +if opt.flag_test + return +end + +%% Load parameters +tab = niak_read_csv_cell(in); + +%% Compose js text -- FD +list_ind = [1 4 5]; +ind_ref = 4; +labels = { 'Run' , 'FD_before' , 'FD_after'}; +val = str2double(tab(2:end,ind_ref)); +[val,order] = sort(val,'descend'); +order = order(:)'+1; + +text_js = sprintf('var dataFD = [\n'); +for ii = 1:length(list_ind) + text_js = [text_js sprintf(' [''%s'' ',labels{ii})]; + for ss = order + text_js = [text_js, ', ''' tab{ss,list_ind(ii)} '''']; + end + if ii == length(list_ind) + text_js = [text_js sprintf(']\n')]; + else + text_js = [text_js sprintf('],\n')]; + end +end +text_js = [text_js sprintf('];\n')]; + +%% Compose js text -- # time points +list_ind = [1 2 3]; +ind_ref = 3; +labels = { 'Run' , 'vol_scrubbed' , 'vol_ok' }; +val = str2double(tab(2:end,ind_ref)); +[val,order] = sort(val,'descend'); +order = order(:)'+1; + +text_js = [text_js sprintf('var dataNbVol = [\n')]; +for ii = 1:length(list_ind) + text_js = [text_js sprintf(' [''%s'' ',labels{ii})]; + for ss = order + text_js = [text_js, ', ''' tab{ss,list_ind(ii)} '''']; + end + if ii == length(list_ind) + text_js = [text_js sprintf(']\n')]; + else + text_js = [text_js sprintf('],\n')]; + end +end +text_js = [text_js sprintf('];\n')]; + +%% Write output +[hf,msg] = fopen(out,'w'); +if hf == -1 + error(msg) +end +fprintf(hf,'%s',text_js); +fclose(hf); diff --git a/reports/fmri_preprocess/niak_report_fmri_preprocess.m b/reports/fmri_preprocess/niak_report_fmri_preprocess.m new file mode 100644 index 00000000..6fbf1108 --- /dev/null +++ b/reports/fmri_preprocess/niak_report_fmri_preprocess.m @@ -0,0 +1,315 @@ +function pipeline = niak_report_fmri_preprocess(in,opt) +% Generate a report for the fMRI preprocessing pipeline +% +% SYNTAX: PIPE = NIAK_REPORT_FMRI_PREPROCESS(IN,OPT) +% +% IN.PARAMS (string) +% The name of a .mat file with two variables FILES_IN (the input files) and +% OPT (the options), describing the parameters of the pipeline. +% +% IN.GROUP (structure) +% with the following fields: +% AVG_T1 (string) the file name of the average T1 of all subjects, +% after non-linear coregistration in stereotaxic space. +% AVG_FUNC (string) the file name of the average BOLD volume of all subjects, +% after non-linear coregistration in stereotaxic space. +% AVG_MASK_FUNC (string) the file name of the average indivudal BOLD mask, +% after non-linear coregistration in stereotaxic space. +% MASK_FUNC_GROUP (string) the file name of the group mask for BOLD data, +% in non-linear stereotaxic space. +% SUMMARY_SCRUBBING (string) the file name of a .csv file with a summary of +% scrubbing of fMRI time series. +% SUMMARY_FUNC (string) the file name of a .csv file with a summary of +% BOLD registration. +% SUMMARY_ANAT (string) the file name of a .csv file with a summary of +% T1 registration. +% +% IN.IND (structure) +% with the following fields: +% FMRI_NATIVE.(SUBJECT).(SESSION).(RUN) (string) file name of an fMRI dataset +% in native space (before resampling for motion). +% FMRI_STEREO.(SUBJECT).(SESSION).(RUN) (string) file name of an fMRI dataset +% after spatial resampling to correct for motion. +% CONFOUNDS.(SUBJECT).(SESSION).(RUN) (string) file name of a .tsv file +% with confound variables (motion parameters, FD and scrubbing mask). +% ANAT.(SUBJECT) (string) the file name of an individual T1 volume (in stereotaxic space). +% FUNC.(SUBJECT) (string) the file name of an individual functional volume (in stereotaxic space) +% REGISTRATION.(SUBJECT) (string) the file name of a .csv file with measures of +% intra-subject, inter-run coregistration quality. +% +% IN.TEMPLATE.ANAT (string) +% the file name of the template used for registration in stereotaxic space. +% IN.TEMPLATE.FMRI (string) +% the file name of the template used to resample fMRI data. +% +% OPT +% (structure) with the following fields: +% FOLDER_OUT (string) where to generate the outputs. +% COORD (array N x 3) Coordinates for the registration figures. +% The default is: +% [-30 , -65 , -15 ; +% -8 , -25 , 10 ; +% 30 , 45 , 60]; +% PSOM (structure) options for PSOM. See PSOM_RUN_PIPELINE. +% FLAG_VERBOSE (boolean, default true) if true, verbose on progress. +% FLAG_TEST (boolean, default false) if the flag is true, the pipeline will +% be generated but no processing will occur. +% +% Note: +% Labels SUBJECT, SESSION and RUN are arbitrary but need to conform to matlab's +% specifications for field names. +% +% This pipeline needs the PSOM library to run. +% http://psom.simexp-lab.org/ +% +% Copyright (c) Pierre Bellec +% Centre de recherche de l'Institut universitaire de griatrie de Montral, 2016. +% Maintainer : pierre.bellec@criugm.qc.ca +% See licensing information in the code. +% Keywords : visualization, montage, 3D brain volumes + +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +% THE SOFTWARE. + +psom_gb_vars; + +%% Defaults + +% Inputs +in = psom_struct_defaults( in , ... + { 'params' , 'group' , 'ind' , 'template' }, ... + { NaN , NaN , NaN , NaN }); + +in.group = psom_struct_defaults( in.group , ... + { 'avg_t1' , 'avg_func' , 'avg_mask_func' , 'mask_func_group' , 'summary_func' , 'summary_anat' , 'summary_scrubbing' }, ... + { NaN , NaN , NaN , NaN , NaN , NaN , NaN }); + +in.ind = psom_struct_defaults( in.ind , ... + { 'fmri_native' , 'fmri_stereo' , 'confounds' , 'anat' , 'func' , 'registration' }, ... + { NaN , NaN , NaN , NaN , NaN , NaN }); + +in.template = psom_struct_defaults( in.template , ... + { 'anat' , 'fmri' }, ... + { NaN , NaN }); + +list_subject = fieldnames(in.ind.anat); + +%% Options +if nargin < 2 + opt = struct; +end +coord_def =[-30 , -65 , -15 ; + -8 , -25 , 10 ; + 30 , 45 , 60]; +opt = psom_struct_defaults ( opt , ... + { 'folder_out' , 'coord' , 'flag_test' , 'psom' , 'flag_verbose' }, ... + { pwd , coord_def , false , struct() , true }); + +opt.folder_out = niak_full_path(opt.folder_out); +opt.psom.path_logs = [opt.folder_out 'logs' filesep]; + +%% Build file names + +%% Copy and update the report templates +pipeline = struct; +clear jin jout jopt +niak_gb_vars +path_template = [gb_niak_path_niak 'reports' filesep 'fmri_preprocess' filesep 'templates' filesep ]; +jin = niak_grab_folder( path_template , {'.git',[path_template 'motion'],[path_template 'registration'],[path_template 'summary'],[path_template 'group']}); +jout = strrep(jin,path_template,opt.folder_out); +jopt.folder_out = opt.folder_out; +pipeline = psom_add_job(pipeline,'cp_report_templates','niak_brick_copy',jin,jout,jopt); + +%% Write a text description of the pipeline parameters +clear jin jout jopt +jin = in.params; +jout.list_subject = [opt.folder_out 'group' filesep 'listSubject.js']; +jout.list_run = [opt.folder_out 'group' filesep 'listRun.js']; +jout.files_in = [opt.folder_out 'summary' filesep 'filesIn.js']; +jout.summary = [opt.folder_out 'summary' filesep 'pipeSummary.js']; +pipeline = psom_add_job(pipeline,'params','niak_brick_preproc_params2report',jin,jout); + +%% The summary of BOLD registration +clear jin jout jopt +jin = in.group.summary_func; +jout = [opt.folder_out 'summary' filesep 'chartBOLD.js']; +pipeline = psom_add_job(pipeline,'summary_func','niak_brick_preproc_func2report',jin,jout); + +%% The summary of T1 registration +clear jin jout jopt +jin = in.group.summary_func; +jout = [opt.folder_out 'summary' filesep 'chartT1.js']; +pipeline = psom_add_job(pipeline,'summary_anat','niak_brick_preproc_anat2report',jin,jout); + +%% The summary of FD +clear jin jout jopt +jin = in.group.summary_scrubbing; +jout = [opt.folder_out 'summary' filesep 'fd.js']; +pipeline = psom_add_job(pipeline,'summary_scrubbing','niak_brick_preproc_scrubbing2report',jin,jout); + +%% The summary of brain masks +clear jin jout jopt +jin = in.ind.registration; +jout = [opt.folder_out 'summary' filesep 'chartBrain.js']; +pipeline = psom_add_job(pipeline,'summary_intra','niak_brick_preproc_intra2report',jin,jout); + +%% Generate group images +clear jin jout jopt +jin.target = in.template.anat; +jopt.coord = opt.coord; +jopt.colorbar = true; + +% Template +jin.source = in.template.anat; +jout = [opt.folder_out 'group' filesep 'template_stereotaxic.png']; +jopt.colormap = 'gray'; +jopt.colorbar = false; +jopt.limits = 'adaptative'; +jopt.title = 'T1 Template'; +pipeline = psom_add_job(pipeline,'template_stereo','niak_brick_vol2img',jin,jout,jopt); + +% Group average T1 +jin.source = in.group.avg_t1; +jout = [opt.folder_out 'group' filesep 'average_t1_stereotaxic.png']; +jopt.colormap = 'gray'; +jopt.limits = 'adaptative'; +jopt.title = 'Group average T1'; +pipeline = psom_add_job(pipeline,'average_t1_stereo','niak_brick_vol2img',jin,jout,jopt); + +% Group average BOLD +jin.source = in.group.avg_func; +jout = [opt.folder_out 'group' filesep 'average_func_stereotaxic.png']; +jopt.colormap = 'jet'; +jopt.limits = 'adaptative'; +jopt.title = 'Group average BOLD'; +pipeline = psom_add_job(pipeline,'average_func_stereo','niak_brick_vol2img',jin,jout,jopt); + +% Group BOLD mask +jin.source = in.group.mask_func_group; +jout = [opt.folder_out 'group' filesep 'mask_func_group_stereotaxic.png']; +jopt.colormap = 'jet'; +jopt.limits = [0 1]; +jopt.title = 'Group BOLD mask'; +pipeline = psom_add_job(pipeline,'mask_func_group_stereo','niak_brick_vol2img',jin,jout,jopt); + +% Average BOLD mask +jin.source = in.group.avg_mask_func; +jout = [opt.folder_out 'group' filesep 'average_mask_func_stereotaxic.png']; +jopt.colormap = 'jet'; +jopt.limits = [0 1]; +jopt.title = 'Average BOLD mask'; +pipeline = psom_add_job(pipeline,'avg_mask_func_stereo','niak_brick_vol2img',jin,jout,jopt); + +%% Panel on individual registration + +% Individual T1 images +jopt.colormap = 'gray'; +jopt.limits = 'adaptative'; +jopt.method = 'linear'; +for ss = 1:length(list_subject) + jin.source = in.ind.anat.(list_subject{ss}); + jout = [opt.folder_out 'registration' filesep list_subject{ss} '_anat.png']; + jopt.title = sprintf('Individual T1 subject %s',list_subject{ss}); + pipeline = psom_add_job(pipeline,['t1_' list_subject{ss}],'niak_brick_vol2img',jin,jout,jopt); +end + +% Individual BOLD images +jopt.colormap = 'jet'; +jopt.limits = 'adaptative'; +jopt.method = 'linear'; +for ss = 1:length(list_subject) + jin.source = in.ind.func.(list_subject{ss}); + jout = [opt.folder_out 'registration' filesep list_subject{ss} '_func.png']; + jopt.title = sprintf('Individual BOLD subject %s',list_subject{ss}); + pipeline = psom_add_job(pipeline,['bold_' list_subject{ss}],'niak_brick_vol2img',jin,jout,jopt); +end + +%% Panel on motion + +% Movies (and target image for all runs) +[list_fmri_native,labels] = niak_fmri2cell(in.ind.fmri_native); +[list_fmri_stereo,labels] = niak_fmri2cell(in.ind.fmri_stereo); +for ll = 1:length(labels) + clear jin jout jopt + + % Native movie + jin.source = list_fmri_native{ll}; + jin.target = list_fmri_native{ll}; + jout = [opt.folder_out 'motion' filesep 'motion_native_' labels(ll).name '.png']; + jopt.coord = 'CEN'; + jopt.colormap = 'jet'; + jopt.flag_vertical = false; + jopt.limits = 'adaptative'; + jopt.flag_decoration = false; + pipeline = psom_add_job(pipeline,['motion_native_' labels(ll).name],'niak_brick_vol2img',jin,jout,jopt); + + % Native spacer + jopt.flag_median = true; + jout = [opt.folder_out 'motion' filesep 'target_native_' labels(ll).name '.png']; + pipeline = psom_add_job(pipeline,['target_native_' labels(ll).name],'niak_brick_vol2img',jin,jout,jopt); + + % Stereotaxic movie + jopt.flag_median = false; + jopt.coord = [0 0 0]; + jin.source = list_fmri_stereo{ll}; + jin.target = list_fmri_stereo{ll}; + jout = [opt.folder_out 'motion' filesep 'motion_stereo_' labels(ll).name '.png']; + pipeline = psom_add_job(pipeline,['motion_stereo_' labels(ll).name],'niak_brick_vol2img',jin,jout,jopt); + + % Stereotaxic spacer + jopt.flag_median = true; + jout = [opt.folder_out 'motion' filesep 'target_stereo_' labels(ll).name '.png']; + pipeline = psom_add_job(pipeline,['target_stereo_' labels(ll).name],'niak_brick_vol2img',jin,jout,jopt); +end + +% Motion parameters +[list_confounds,labels] = niak_fmri2cell(in.ind.confounds); +for ll = 1:length(labels) + clear jin jout jopt + jin = list_confounds{ll}; + jout = [opt.folder_out 'motion' filesep 'dataMotion_' labels(ll).name '.js']; + pipeline = psom_add_job(pipeline,['motion_ind_' labels(ll).name],'niak_brick_preproc_ind_motion2report',jin,jout); +end + +% Pick reference runs +labels_ref = struct; +for ss = 1:length(list_subject) + session = fieldnames(in.ind.fmri_native.(list_subject{ss})); + session = session{1}; + run = fieldnames(in.ind.fmri_native.(list_subject{ss}).(session)); + run = run{1}; + labels_ref.(list_subject{ss}) = [list_subject{ss} '_' session '_' run]; +end + +% Generate the motion report +for ll = 1:length(labels) + clear jin jout jopt + jout = [opt.folder_out 'motion' filesep 'motion_report_' labels(ll).name '.html']; + jopt.label = labels(ll).name; + jopt.label_ref = labels_ref.(labels(ll).subject); + jopt.num_run = ll; + pipeline = psom_add_job(pipeline,['motion_report_' labels(ll).name],'niak_brick_preproc_motion2report','',jout,jopt); + if ll==1 + jout = [opt.folder_out 'motion' filesep 'motion.html']; + pipeline = psom_add_job(pipeline,'motion_report','niak_brick_preproc_motion2report','',jout,jopt); + end +end + +if ~opt.flag_test + psom_run_pipeline(pipeline,opt.psom); +end \ No newline at end of file diff --git a/reports/fmri_preprocess/templates/assets/backbone/LICENSE.txt b/reports/fmri_preprocess/templates/assets/backbone/LICENSE.txt new file mode 100644 index 00000000..02c89b26 --- /dev/null +++ b/reports/fmri_preprocess/templates/assets/backbone/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2010-2016 Jeremy Ashkenas, DocumentCloud + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/reports/fmri_preprocess/templates/assets/backbone/backbone-min.js b/reports/fmri_preprocess/templates/assets/backbone/backbone-min.js new file mode 100644 index 00000000..8ea4b13d --- /dev/null +++ b/reports/fmri_preprocess/templates/assets/backbone/backbone-min.js @@ -0,0 +1,2 @@ +(function(t,e){if(typeof define==="function"&&define.amd){define(["underscore","jquery","exports"],function(i,r,s){t.Backbone=e(t,s,i,r)})}else if(typeof exports!=="undefined"){var i=require("underscore");e(t,exports,i)}else{t.Backbone=e(t,{},t._,t.jQuery||t.Zepto||t.ender||t.$)}})(this,function(t,e,i,r){var s=t.Backbone;var n=[];var a=n.push;var o=n.slice;var h=n.splice;e.VERSION="1.1.2";e.$=r;e.noConflict=function(){t.Backbone=s;return this};e.emulateHTTP=false;e.emulateJSON=false;var u=e.Events={on:function(t,e,i){if(!c(this,"on",t,[e,i])||!e)return this;this._events||(this._events={});var r=this._events[t]||(this._events[t]=[]);r.push({callback:e,context:i,ctx:i||this});return this},once:function(t,e,r){if(!c(this,"once",t,[e,r])||!e)return this;var s=this;var n=i.once(function(){s.off(t,n);e.apply(this,arguments)});n._callback=e;return this.on(t,n,r)},off:function(t,e,r){var s,n,a,o,h,u,l,f;if(!this._events||!c(this,"off",t,[e,r]))return this;if(!t&&!e&&!r){this._events=void 0;return this}o=t?[t]:i.keys(this._events);for(h=0,u=o.length;h").attr(t);this.setElement(r,false)}else{this.setElement(i.result(this,"el"),false)}}});e.sync=function(t,r,s){var n=T[t];i.defaults(s||(s={}),{emulateHTTP:e.emulateHTTP,emulateJSON:e.emulateJSON});var a={type:n,dataType:"json"};if(!s.url){a.url=i.result(r,"url")||M()}if(s.data==null&&r&&(t==="create"||t==="update"||t==="patch")){a.contentType="application/json";a.data=JSON.stringify(s.attrs||r.toJSON(s))}if(s.emulateJSON){a.contentType="application/x-www-form-urlencoded";a.data=a.data?{model:a.data}:{}}if(s.emulateHTTP&&(n==="PUT"||n==="DELETE"||n==="PATCH")){a.type="POST";if(s.emulateJSON)a.data._method=n;var o=s.beforeSend;s.beforeSend=function(t){t.setRequestHeader("X-HTTP-Method-Override",n);if(o)return o.apply(this,arguments)}}if(a.type!=="GET"&&!s.emulateJSON){a.processData=false}if(a.type==="PATCH"&&k){a.xhr=function(){return new ActiveXObject("Microsoft.XMLHTTP")}}var h=s.xhr=e.ajax(i.extend(a,s));r.trigger("request",r,h,s);return h};var k=typeof window!=="undefined"&&!!window.ActiveXObject&&!(window.XMLHttpRequest&&(new XMLHttpRequest).dispatchEvent);var T={create:"POST",update:"PUT",patch:"PATCH","delete":"DELETE",read:"GET"};e.ajax=function(){return e.$.ajax.apply(e.$,arguments)};var $=e.Router=function(t){t||(t={});if(t.routes)this.routes=t.routes;this._bindRoutes();this.initialize.apply(this,arguments)};var S=/\((.*?)\)/g;var H=/(\(\?)?:\w+/g;var A=/\*\w+/g;var I=/[\-{}\[\]+?.,\\\^$|#\s]/g;i.extend($.prototype,u,{initialize:function(){},route:function(t,r,s){if(!i.isRegExp(t))t=this._routeToRegExp(t);if(i.isFunction(r)){s=r;r=""}if(!s)s=this[r];var n=this;e.history.route(t,function(i){var a=n._extractParameters(t,i);n.execute(s,a);n.trigger.apply(n,["route:"+r].concat(a));n.trigger("route",r,a);e.history.trigger("route",n,r,a)});return this},execute:function(t,e){if(t)t.apply(this,e)},navigate:function(t,i){e.history.navigate(t,i);return this},_bindRoutes:function(){if(!this.routes)return;this.routes=i.result(this,"routes");var t,e=i.keys(this.routes);while((t=e.pop())!=null){this.route(t,this.routes[t])}},_routeToRegExp:function(t){t=t.replace(I,"\\$&").replace(S,"(?:$1)?").replace(H,function(t,e){return e?t:"([^/?]+)"}).replace(A,"([^?]*?)");return new RegExp("^"+t+"(?:\\?([\\s\\S]*))?$")},_extractParameters:function(t,e){var r=t.exec(e).slice(1);return i.map(r,function(t,e){if(e===r.length-1)return t||null;return t?decodeURIComponent(t):null})}});var N=e.History=function(){this.handlers=[];i.bindAll(this,"checkUrl");if(typeof window!=="undefined"){this.location=window.location;this.history=window.history}};var R=/^[#\/]|\s+$/g;var O=/^\/+|\/+$/g;var P=/msie [\w.]+/;var C=/\/$/;var j=/#.*$/;N.started=false;i.extend(N.prototype,u,{interval:50,atRoot:function(){return this.location.pathname.replace(/[^\/]$/,"$&/")===this.root},getHash:function(t){var e=(t||this).location.href.match(/#(.*)$/);return e?e[1]:""},getFragment:function(t,e){if(t==null){if(this._hasPushState||!this._wantsHashChange||e){t=decodeURI(this.location.pathname+this.location.search);var i=this.root.replace(C,"");if(!t.indexOf(i))t=t.slice(i.length)}else{t=this.getHash()}}return t.replace(R,"")},start:function(t){if(N.started)throw new Error("Backbone.history has already been started");N.started=true;this.options=i.extend({root:"/"},this.options,t);this.root=this.options.root;this._wantsHashChange=this.options.hashChange!==false;this._wantsPushState=!!this.options.pushState;this._hasPushState=!!(this.options.pushState&&this.history&&this.history.pushState);var r=this.getFragment();var s=document.documentMode;var n=P.exec(navigator.userAgent.toLowerCase())&&(!s||s<=7);this.root=("/"+this.root+"/").replace(O,"/");if(n&&this._wantsHashChange){var a=e.$('