From 9e7219f37c5371b2c0d40b00561ec1c9165207d4 Mon Sep 17 00:00:00 2001 From: marcobarilari Date: Mon, 8 Mar 2021 15:36:12 +0100 Subject: [PATCH] remove specific sourceMRI code which is not in common between the two repos --- demos/sourceDataProcessing/batchSource.m | 24 --- demos/sourceDataProcessing/getOptionSource.m | 53 ------- src/batches/setBatchGZip.m | 31 ---- src/defaults/checkOptionsSource.m | 74 --------- src/utils/convert3Dto4D.m | 154 ------------------- src/workflows/bidsGZipRawFolder.m | 41 ----- tests/test_checkOptionsSource.m | 51 ------ tests/test_setBatchGZip.m | 22 --- 8 files changed, 450 deletions(-) delete mode 100644 demos/sourceDataProcessing/batchSource.m delete mode 100644 demos/sourceDataProcessing/getOptionSource.m delete mode 100644 src/batches/setBatchGZip.m delete mode 100644 src/defaults/checkOptionsSource.m delete mode 100644 src/utils/convert3Dto4D.m delete mode 100644 src/workflows/bidsGZipRawFolder.m delete mode 100644 tests/test_checkOptionsSource.m delete mode 100644 tests/test_setBatchGZip.m diff --git a/demos/sourceDataProcessing/batchSource.m b/demos/sourceDataProcessing/batchSource.m deleted file mode 100644 index 7334115f..00000000 --- a/demos/sourceDataProcessing/batchSource.m +++ /dev/null @@ -1,24 +0,0 @@ -% (C) Copyright 2020 CPP BIDS SPM-pipeline developers - -clear; -clc; - -% Directory with this script becomes the current directory -pth = fileparts(mfilename('fullpath')); - -% We add all the subfunctions that are in the sub directories -addpath(genpath(fullfile(pth, '..', '..', 'src'))); -addpath(genpath(fullfile(pth, '..', '..', 'lib'))); - -%% Run batches - -optSource = getOptionSource(); - -% Single volumes to 4D volumes conversion + remove n dummies -convert3Dto4D(optSource); - -% Deface anatomical volumes in a raw folder -% defaceAnat(optSource); COMING SOON - -% GZip the volumes in a raw folder -bidsGZipRawFolder(optSource, 0); diff --git a/demos/sourceDataProcessing/getOptionSource.m b/demos/sourceDataProcessing/getOptionSource.m deleted file mode 100644 index 27689884..00000000 --- a/demos/sourceDataProcessing/getOptionSource.m +++ /dev/null @@ -1,53 +0,0 @@ -% (C) Copyright 2019 CPP BIDS SPM-pipeline developers - -function optSource = getOptionSource() - % - % Returns a structure that contains the options chosen by the user to run the source processing - % batch workflow - % - % USAGE:: - % - % optSource = getOptionSource() - % - % :returns: - :optSource: (struct) - - if nargin < 1 - optSource = []; - end - - % Set the folder where sequences folders exist - optSource.sourceDir = '/Users/barilari/Desktop/DICOM_UCL_leuven/renamed/sub-pilot001/ses-002/MRI'; - - optSource.dataDir = '/Users/barilari/Desktop/DICOM_UCL_leuven/raw'; - - % List of the sequences that you want to skip (folder name pattern) - optSource.sequenceToIgnore = {'AAHead_Scout', ... - 'b1map', ... - 't1', ... - 'gre_field'}; - - % Number of volumes to discard ad dummies, (0 is default) - optSource.nbDummies = 5; - - % List of the sequences where you want to remove dummies (folder name pattern) - optSource.sequenceRmDummies = {'cmrr_mbep2d_p3_mb2_1.6iso_AABrain', ... - 'cmrr_mbep2d_p4_mb2_750um_AAbrain'}; - - % Set data format conversion (0 is default) - - % 0: SAME - % 2: UINT8 - unsigned char - % 4: INT16 - signed short - % 8: INT32 - signed int - % 16: FLOAT32 - single prec. float - % 64: FLOAT64 - double prec. float - - optSource.dataType = 0; - - % Boolean to enable gzip of the new 4D file (0 is default) - optSource.zip = 0; - - % Check the options provided - optSource = checkOptionsSource(optSource); - -end diff --git a/src/batches/setBatchGZip.m b/src/batches/setBatchGZip.m deleted file mode 100644 index 81fb9fa5..00000000 --- a/src/batches/setBatchGZip.m +++ /dev/null @@ -1,31 +0,0 @@ -% (C) Copyright 2020 CPP BIDS SPM-pipeline developers - -function matlabbatch = setBatchGZip(matlabbatch, unzippedNiifiles, keepUnzippedNii) - % - % Set the batch for GZip the 4D volumes - % - % USAGE:: - % - % matlabbatch = setBatchGZip(matlabbatch, unzippedNiifiles, keepUnzippedNii = false) - % - % :param matlabbatch: - % :type matlabbatch: structure - % :param unzippedNiifiles: List of volumes to be gzipped - % :type unzippedNiifiles: array - % :param keepUnzippedNii: Boolean to decide to delete the unzipped files - % :type keepUnzippedNii: boolean - % - % :returns: - :matlabbatch: (struct) The matlabbath ready to run the spm job - - if nargin < 3 || isempty(keepUnzippedNii) - % delete the original unzipped .nii - keepUnzippedNii = false; - end - - printBatchName('zipping'); - - matlabbatch{end + 1}.cfg_basicio.file_dir.file_ops.cfg_gzip_files.files = unzippedNiifiles; - matlabbatch{end}.cfg_basicio.file_dir.file_ops.cfg_gzip_files.outdir = {''}; - matlabbatch{end}.cfg_basicio.file_dir.file_ops.cfg_gzip_files.keep = keepUnzippedNii; - -end diff --git a/src/defaults/checkOptionsSource.m b/src/defaults/checkOptionsSource.m deleted file mode 100644 index 3320f740..00000000 --- a/src/defaults/checkOptionsSource.m +++ /dev/null @@ -1,74 +0,0 @@ -% (C) Copyright 2020 CPP BIDS SPM-pipeline developers - -function optSource = checkOptionsSource(optSource) - % - % Check the option inputs for source data and add any missing field with some defaults - % - % USAGE:: - % - % optSource = checkOptionsSource(optSource) - % - % :param optSource: Obligatory argument. The structure that contains the options set by the user - % to run the batch workflow for source processing - % - % :returns: - :optSource: (struc) The structure with any unset fields with the deaufalt values - % - % OPTIONS (with their defaults): - % - ``optSource.sourceDir = ''`` - The directory where the source data are located. - % - ``optSource.dataDir = ''`` - The directory where the raw data to apply changes are located. - % - ``optSource.sequenceToIgnore = {}`` - The list of sequence(s) to ignore. - % - ``optSource.dataType = 0`` - Data format conversion (0 is reccomended). - % - ``optSource.zip = 0`` - Boolean to enable gzip of the new 4D file in ``convert3Dto4D``. - % - ``optSource.nbDummies = 0`` - Number of volumes to discard ad dummies in ``convert3Dto4D``. - % - ``optSource.sequenceRmDummies = {}`` - The list of sequence(s) where to discarding the - % dummies. - - fieldsToSet = setDefaultOptionSource(); - - optSource = setDefaultFields(optSource, fieldsToSet); - - if isempty(optSource.sourceDir) || ~isdir(optSource.sourceDir) - - warning('The source folder is not provided or does not exist.'); - - end - - if isempty(optSource.dataDir) || ~isdir(optSource.dataDir) - - warning('The raw folder is not provided or does not exist.'); - - end - - if isempty(optSource.sequenceToIgnore) - - warning('No sequence-to-ignore provided, I will convert all the images that I can found'); - - end - -end - -function fieldsToSet = setDefaultOptionSource() - % This defines the missing fields - - % The directory where the source data are located - fieldsToSet.sourceDir = ''; - - % The directory where the raw data to apply changes are located - fieldsToSet.dataDir = ''; - - % The list of sequence(s) to ignore - fieldsToSet.sequenceToIgnore = {}; - - % Data format conversion (0 is reccomended) - fieldsToSet.dataType = 0; - - % Boolean to enable gzip of the new 4D file - fieldsToSet.zip = 0; - - % Number of volumes to discard ad dummies - fieldsToSet.nbDummies = 0; - - % The list of sequence(s) where to discarding the dummies - fieldsToSet.sequenceRmDummies = {}; - -end diff --git a/src/utils/convert3Dto4D.m b/src/utils/convert3Dto4D.m deleted file mode 100644 index bafea7e0..00000000 --- a/src/utils/convert3Dto4D.m +++ /dev/null @@ -1,154 +0,0 @@ -% (C) Copyright 2020 CPP BIDS SPM-pipeline developers - -function convert3Dto4D(optSource) - % - % It converts single volumes of a sequence in a 4D file, remove the dummies (optional), zip the - % 4D file (optional) and delete the converted files. Recursevly loops through a folder in which a - % not-yet-BIDS dataset live and the nii files are sorted in each sequence folder. - % - % USAGE:: - % - % convert3Dto4D(optSource) - % - % :param optSource: Obligatory argument. The structure that contains the options set by the user - % to run the batch workflow for source processing - % - % .. todo: - % - % - expand to run through multiple subjs ans groups - % (https://stackoverflow.com/questions/8748976/ - % list-the-subfolders-in-a-folder-matlab-only-subfolders-not-files) - % - generalize how to retrieve RT from sidecar json file - % - saveMatlabBatch(matlabbatch, ... - % ['3Dto4D_dataType-' num2str(dataType) '_RT-' num2str(RT)], opt, subID); - % - Cover the MoCo use case: if the sequence is MoCo (motion corrected when the "scanner" - % reconstructs the images - an option on can tick on Siemens scanner and that output an - % additional MoCo file with the regular sequence) then each JSON file of each volume contains - % the motion correction information for that volume. So only taking the JSON of the first - % volume means we "lose" the realignment parameters that could be useful later. - - % Get source folder content - sourceDataStruc = dir(optSource.sourceDir); - - isDir = [sourceDataStruc(:).isdir]; - - optSource.sequenceList = {sourceDataStruc(isDir).name}'; - - % Loop through the sequence folders - - tic; - - for iSeq = 1:size(optSource.sequenceList, 1) - - % Skip 'non' folders - if length(optSource.sequenceList{iSeq}) > 2 - - % Check if sequence to ignore or not - if contains(optSource.sequenceList(iSeq), optSource.sequenceToIgnore) - - warning('\nIGNORING SEQUENCE: %s\n', string(optSource.sequenceList(iSeq))); - - else - - fprintf('\n\nCONVERTING SEQUENCE: %s \n', char(optSource.sequenceList(iSeq))); - - % Set whether to remove dummies or not - - nbDummies = 0; - - if contains(optSource.sequenceList(iSeq), optSource.sequenceRmDummies) - - nbDummies = optSource.nbDummies; - - fprintf('\n\nREMOVING %s DUMMIES\n\n', num2str(nbDummies)); - - end - - % Get sequence folder path - sequencePath = fullfile(optSource.sourceDir, optSource.sequenceList{iSeq}); - - % Retrieve volume files info - [volumesList, outputNameImage] = parseFiles('nii', sequencePath, nbDummies); - - % Set output name, it takes the file name of the 1st volume of the 4D file and add subfix - outputNameImage = strrep(outputNameImage, '.nii', '_4D.nii'); - - % Retrieve sidecar json files info - [jsonList, outputNameJson] = parseFiles('json', sequencePath, nbDummies); - - jsonFile = spm_jsonread(jsonList{1}); - - % % % % % % LIEGE SPECIFIC % % % % % % % - RT = jsonFile.acqpar.RepetitionTime / 1000; - % % % % % % % % % % % % % % % % % % % % % - - % Set and run spm batch, input all the volumes minus the dummies if > 0 - matlabbatch = []; - matlabbatch = setBatch3Dto4D(matlabbatch, ... - volumesList(nbDummies + 1:end, :), ... - RT, ... - outputNameImage, ... - optSource.dataType); - - spm_jobman('run', matlabbatch); - - if optSource.zip - - % Zip and delete the and the new 4D file - fprintf(1, 'ZIP AND DELETE THE NEW 4D BRAIN \n\n'); - - gzip([sequencePath filesep outputNameImage]); - - delete([sequencePath filesep outputNameImage]); - - end - - % Save one sidecar json file, it takes the file name of the 1st volume of the 4D file and - % add subfix - if ~isempty(jsonList) - - copyfile(jsonList{1}, [sequencePath filesep strrep(outputNameJson, '.json', '_4D.json')]); - - end - - % Delete all the single volumes .nii and .json files - fprintf(1, 'EXTERMINATE SINGLE VOLUMES FILES \n\n'); - - for iDel = 1:length(volumesList) - - delete(volumesList{iDel}); - delete(jsonList{iDel}); - - end - - end - - end - - end - - toc; - -end - -function [fileList, outputName] = parseFiles(fileExtention, sequencePath, nbDummies) - - fileList = spm_select('list', sequencePath, fileExtention); - - if size(fileList, 1) > 0 - - outputName = fileList(nbDummies + 1, :); - - fileList = strcat(sequencePath, filesep, cellstr(fileList)); - - else - - fileList = {}; - - outputName = []; - - warning('\nI have found 0 files with extension ''.%s'' \n', fileExtention); - - end - -end diff --git a/src/workflows/bidsGZipRawFolder.m b/src/workflows/bidsGZipRawFolder.m deleted file mode 100644 index 99003501..00000000 --- a/src/workflows/bidsGZipRawFolder.m +++ /dev/null @@ -1,41 +0,0 @@ -% (C) Copyright 2020 CPP BIDS SPM-pipeline developers - -function bidsGZipRawFolder(opt, keepUnzippedNii) - % - % - % GZip the nii files in a ``raw`` bids folders from the. It will do it independently of the task. - % - % USAGE:: - % - % bidsGZipRawFolder(optSource ... - % [, keepUnzippedNii = false]) - % - % :param opt: The structure that contains the options set by the user to run the batch - % workflow for source processing - % :type opt: structure - % :param keepUnzippedNii: will keep the original ``.nii`` if set to ``true``. Default is false - % :type keepUnzippedNii: boolean - - %% input variables default values - - if nargin < 2 || isempty(keepUnzippedNii) - % delete the original unzipped .nii - keepUnzippedNii = false; - end - - tic; - - printWorklowName('GZip data'); - - rawDir = opt.dataDir; - - unzippedNiifiles = cellstr(spm_select('FPListRec', rawDir, '^.*.nii$')); - - matlabbatch = []; - matlabbatch = setBatchGZip(matlabbatch, unzippedNiifiles, keepUnzippedNii); - - spm_jobman('run', matlabbatch); - - toc; - -end diff --git a/tests/test_checkOptionsSource.m b/tests/test_checkOptionsSource.m deleted file mode 100644 index 1d0f69a9..00000000 --- a/tests/test_checkOptionsSource.m +++ /dev/null @@ -1,51 +0,0 @@ -function test_suite = test_checkOptionsSource %#ok<*STOUT> - try % assignment of 'localfunctions' is necessary in Matlab >= 2016 - test_functions = localfunctions(); %#ok<*NASGU> - catch % no problem; early Matlab versions can use initTestSuite fine - end - initTestSuite; -end - -function test_checkOptionsSourceBasic() - - optSource.nbDummies = 0; - optSource = checkOptionsSource(optSource); - - expectedOptionsSource = defaultOptionsSource(); - expectedOptionsSource.nbDummies = 0; - - assertEqual(optSource, expectedOptionsSource); - -end - -function test_checkOptionsSourceDoNotOverwrite() - - optSource.dataType = 666; - optSource.someExtraField = 'test'; - optSource.nbDummies = 42; - - optSource = checkOptionsSource(optSource); - - assertEqual(optSource.dataType, 666); - assertEqual(optSource.someExtraField, 'test'); - assertEqual(optSource.nbDummies, 42); - -end - -function expectedOptionsSource = defaultOptionsSource() - - expectedOptionsSource.sourceDir = ''; - - expectedOptionsSource.dataDir = ''; - - expectedOptionsSource.sequenceToIgnore = {}; - - expectedOptionsSource.dataType = 0; - - expectedOptionsSource.zip = 0; - - expectedOptionsSource.nbDummies = 0; - - expectedOptionsSource.sequenceRmDummies = {}; - -end diff --git a/tests/test_setBatchGZip.m b/tests/test_setBatchGZip.m deleted file mode 100644 index 2651e069..00000000 --- a/tests/test_setBatchGZip.m +++ /dev/null @@ -1,22 +0,0 @@ -function test_suite = test_setBatchGZip %#ok<*STOUT> - try % assignment of 'localfunctions' is necessary in Matlab >= 2016 - test_functions = localfunctions(); %#ok<*NASGU> - catch % no problem; early Matlab versions can use initTestSuite fine - end - initTestSuite; -end - -function test_setBatchGZipBasic() - - unzippedNiifiles = 'sub-01_ses-01_T1w.nii'; - - matlabbatch = []; - matlabbatch = setBatchGZip(matlabbatch, unzippedNiifiles); - - expectedBatch{1}.cfg_basicio.file_dir.file_ops.cfg_gzip_files.files = 'sub-01_ses-01_T1w.nii'; - expectedBatch{1}.cfg_basicio.file_dir.file_ops.cfg_gzip_files.outdir = {''}; - expectedBatch{1}.cfg_basicio.file_dir.file_ops.cfg_gzip_files.keep = false(); - - assertEqual(matlabbatch, expectedBatch); - -end