|
| 1 | +function figHandle = plotDataInRoi(varargin) |
| 2 | + % |
| 3 | + % Creates a figure showing a histogram of the content of a set of ROIs used on |
| 4 | + % a set of data files. |
| 5 | + % |
| 6 | + % USAGE:: |
| 7 | + % |
| 8 | + % figHandle = plotDataInRoi(dataImages, roiImages) |
| 9 | + % |
| 10 | + % :param dataImages: |
| 11 | + % :type dataImages: path or cellstr of paths |
| 12 | + % |
| 13 | + % :param roiImages: |
| 14 | + % :type roiImages: path or cellstr of paths |
| 15 | + % |
| 16 | + % |
| 17 | + % EXAMPLE:: |
| 18 | + % |
| 19 | + % mask1 = fullfile(pwd, 'label-V1_mask.nii') |
| 20 | + % mask2 = fullfile(pwd, 'label-V2_mask.nii') |
| 21 | + % |
| 22 | + % data1 = fullfile(pwd, 'label-0001_beta.nii') |
| 23 | + % data2 = fullfile(pwd, 'label-0002_beta.nii') |
| 24 | + % |
| 25 | + % mask = cellstr(cat(1, mask1, mask2)); |
| 26 | + % data = cellstr(cat(1, data1, data2)); |
| 27 | + % |
| 28 | + % plotDataInRoi(data, mask); |
| 29 | + % |
| 30 | + % |
| 31 | + % (C) Copyright 2022 CPP ROI developers |
| 32 | + |
| 33 | + defaultNbBins = 100; |
| 34 | + |
| 35 | + isFile = @(x) iscellstr(x) || exist(x, 'file') == 2; |
| 36 | + |
| 37 | + args = inputParser; |
| 38 | + |
| 39 | + args.addRequired('dataImages', isFile); |
| 40 | + args.addRequired('roiImages', isFile); |
| 41 | + |
| 42 | + args.parse(varargin{:}); |
| 43 | + |
| 44 | + dataImages = args.Results.dataImages; |
| 45 | + roiImages = args.Results.roiImages; |
| 46 | + |
| 47 | + if ischar(dataImages) |
| 48 | + dataImages = {dataImages}; |
| 49 | + end |
| 50 | + |
| 51 | + if ischar(roiImages) |
| 52 | + roiImages = {roiImages}; |
| 53 | + end |
| 54 | + |
| 55 | + nbRois = numel(roiImages); |
| 56 | + nbData = numel(dataImages); |
| 57 | + |
| 58 | + if nbRois == 1 || nbData == 1 |
| 59 | + [rows, cols] = optimizeSubplotNumber(nbRois * nbData); |
| 60 | + else |
| 61 | + rows = nbRois; |
| 62 | + cols = nbData; |
| 63 | + end |
| 64 | + |
| 65 | + figHandle = figure('position', [50 50 300 * rows 300 * cols]); |
| 66 | + |
| 67 | + %% collect info to adapt the graphs later on |
| 68 | + |
| 69 | + % y scale |
| 70 | + maxVox = []; |
| 71 | + |
| 72 | + % x axis |
| 73 | + MIN = []; |
| 74 | + MAX = []; |
| 75 | + |
| 76 | + % use the same number of bins for all graphs |
| 77 | + % based on the minimum number of unique values across all the datasets |
| 78 | + nbBins = []; |
| 79 | + |
| 80 | + % to plot all the modes |
| 81 | + modes = []; |
| 82 | + |
| 83 | + subplotList = []; |
| 84 | + |
| 85 | + idxSubplot = 1; |
| 86 | + |
| 87 | + for iRoi = 1:nbRois |
| 88 | + |
| 89 | + for iData = 1:nbData |
| 90 | + |
| 91 | + data{idxSubplot} = spm_summarise(spm_vol(dataImages{iData}), roiImages{iRoi}); |
| 92 | + |
| 93 | + [~, bins] = hist(data{idxSubplot}, defaultNbBins); |
| 94 | + MAX(end + 1) = max(bins); |
| 95 | + MIN(end + 1) = min(bins); |
| 96 | + |
| 97 | + % modes and nbBins work better on rounded values |
| 98 | + modes(end + 1) = mode(round(data{idxSubplot})); |
| 99 | + nbBins(end + 1) = numel(unique(round(data{idxSubplot}))); |
| 100 | + |
| 101 | + subplotList(iRoi, iData) = idxSubplot; |
| 102 | + |
| 103 | + idxSubplot = idxSubplot + 1; |
| 104 | + |
| 105 | + end |
| 106 | + |
| 107 | + end |
| 108 | + |
| 109 | + nbBins = min(nbBins); |
| 110 | + |
| 111 | + for i = 1:numel(data) |
| 112 | + tmp = hist(data{i}, nbBins); |
| 113 | + maxVox(end + 1) = max(tmp); |
| 114 | + end |
| 115 | + |
| 116 | + %% plot histogram and mode |
| 117 | + |
| 118 | + idxSubplot = 1; |
| 119 | + |
| 120 | + for iRoi = 1:nbRois |
| 121 | + |
| 122 | + for iData = 1:nbData |
| 123 | + |
| 124 | + subplot(rows, cols, idxSubplot); |
| 125 | + |
| 126 | + hold on; |
| 127 | + |
| 128 | + hist(data{idxSubplot}, nbBins); |
| 129 | + |
| 130 | + plot([modes(idxSubplot) modes(idxSubplot)], ... |
| 131 | + [0 max(maxVox)], ... |
| 132 | + '--r', ... |
| 133 | + 'linewidth', 1); |
| 134 | + |
| 135 | + bf = bids.File(roiImages{iRoi}); |
| 136 | + title(['roi: ' bf.entities.label]); |
| 137 | + |
| 138 | + axis([min(MIN) max(MAX) 0 max(maxVox)]); |
| 139 | + |
| 140 | + idxSubplot = idxSubplot + 1; |
| 141 | + |
| 142 | + end |
| 143 | + |
| 144 | + end |
| 145 | + |
| 146 | + for i = 1:cols |
| 147 | + subplot(rows, cols, subplotList(end, i)); |
| 148 | + xlabel('intensities'); |
| 149 | + end |
| 150 | + |
| 151 | + for i = 1:rows |
| 152 | + subplot(rows, cols, subplotList(i, 1)); |
| 153 | + ylabel('nb voxels'); |
| 154 | + end |
| 155 | + |
| 156 | +end |
0 commit comments