Skip to content

Commit 1f86774

Browse files
committed
update plotDataInRoi
1 parent db52fc4 commit 1f86774

File tree

2 files changed

+119
-32
lines changed

2 files changed

+119
-32
lines changed

miss_hit.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ tab_width: 2
1616
# metrics limit for the code quality (https://florianschanda.github.io/miss_hit/metrics.html)
1717
metric "cnest": limit 4
1818
metric "file_length": limit 400
19-
metric "cyc": limit 16
19+
metric "cyc": limit 23
2020
metric "parameters": limit 7

src/roi/plotDataInRoi.m

Lines changed: 118 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,32 @@
33
% Creates a figure showing a histogram of the content of a set of ROIs used on
44
% a set of data files.
55
%
6+
% ROI label is extracted from the label entity in the BIDS filename.
7+
%
68
% USAGE::
79
%
8-
% figHandle = plotDataInRoi(dataImages, roiImages)
10+
% figHandle = plotDataInRoi(dataImages, roiImages, ...
11+
% 'scaleFactor', 1, ...
12+
% 'roiAs', 'rows', ...
13+
% 'dataLabel', {})
914
%
1015
% :param dataImages:
1116
% :type dataImages: path or cellstr of paths
1217
%
1318
% :param roiImages:
1419
% :type roiImages: path or cellstr of paths
1520
%
21+
% :param scaleFactor: value to scale the factor by. Default to 1.
22+
% :type scaleFactor: numerical
23+
%
24+
% :param roiAs: Determine if the ROI are supposed to be organized by rows or
25+
% columns.
26+
% Default to 'rows'.
27+
% :type roiAs: 'rows' or 'cols'
28+
%
29+
% :param dataLabel: strings to use to label the data rows or columns.
30+
% :type dataLabel: cellstr
31+
%
1632
%
1733
% EXAMPLE::
1834
%
@@ -33,16 +49,23 @@
3349
defaultNbBins = 100;
3450

3551
isFile = @(x) iscellstr(x) || exist(x, 'file') == 2;
52+
rowOrCol = @(x) ismember(x, {'rows', 'cols'});
3653

3754
args = inputParser;
3855

3956
args.addRequired('dataImages', isFile);
4057
args.addRequired('roiImages', isFile);
58+
args.addParameter('scaleFactor', 1, @isnumeric);
59+
args.addParameter('roiAs', 'rows', rowOrCol);
60+
args.addParameter('dataLabel', {}, @iscellstr);
4161

4262
args.parse(varargin{:});
4363

4464
dataImages = args.Results.dataImages;
4565
roiImages = args.Results.roiImages;
66+
scaleFactor = args.Results.scaleFactor;
67+
roiAs = args.Results.roiAs;
68+
dataLabel = args.Results.dataLabel;
4669

4770
if ischar(dataImages)
4871
dataImages = {dataImages};
@@ -55,26 +78,32 @@
5578
nbRois = numel(roiImages);
5679
nbData = numel(dataImages);
5780

58-
if nbRois == 1 || nbData == 1
59-
[rows, cols] = optimizeSubplotNumber(nbRois * nbData);
60-
else
81+
if numel(dataLabel) ~= nbData
82+
warning('numel dataLabel must be equal to numel dataImages');
83+
dataLabel = {};
84+
end
85+
86+
if strcmp(roiAs, 'rows')
6187
rows = nbRois;
6288
cols = nbData;
89+
elseif strcmp(roiAs, 'cols')
90+
cols = nbRois;
91+
rows = nbData;
6392
end
6493

65-
figHandle = figure('position', [50 50 300 * rows 300 * cols]);
94+
figHandle = figure('position', [50 50 300 * cols 300 * rows]);
6695

6796
%% collect info to adapt the graphs later on
6897

69-
% y scale
98+
% max of scale for data values
7099
maxVox = [];
71100

72-
% x axis
101+
% limit axis of axis for ROI (as nb of voxels)
73102
MIN = [];
74103
MAX = [];
75104

76105
% use the same number of bins for all graphs
77-
% based on the minimum number of unique values across all the datasets
106+
% based on the number of unique values across all the datasets
78107
nbBins = [];
79108

80109
% to plot all the modes
@@ -84,56 +113,75 @@
84113

85114
idxSubplot = 1;
86115

87-
for iRoi = 1:nbRois
116+
for iRow = 1:rows
88117

89-
for iData = 1:nbData
118+
for iCol = 1:cols
90119

91-
data{idxSubplot} = spm_summarise(spm_vol(dataImages{iData}), roiImages{iRoi});
120+
if strcmp(roiAs, 'rows')
121+
data{idxSubplot} = spm_summarise(spm_vol(dataImages{iCol}), roiImages{iRow}) * scaleFactor;
122+
elseif strcmp(roiAs, 'cols')
123+
data{idxSubplot} = spm_summarise(spm_vol(dataImages{iRow}), roiImages{iCol}) * scaleFactor;
124+
end
92125

93-
[~, bins] = hist(data{idxSubplot}, defaultNbBins);
126+
if isempty(data{idxSubplot})
127+
bins = nan;
128+
else
129+
[~, bins] = hist(data{idxSubplot}, defaultNbBins);
130+
end
94131
MAX(end + 1) = max(bins);
95132
MIN(end + 1) = min(bins);
96133

97134
% modes and nbBins work better on rounded values
98135
modes(end + 1) = mode(round(data{idxSubplot}));
99136
nbBins(end + 1) = numel(unique(round(data{idxSubplot})));
100137

101-
subplotList(iRoi, iData) = idxSubplot;
138+
subplotList(iRow, iCol) = idxSubplot;
102139

103140
idxSubplot = idxSubplot + 1;
104141

105142
end
106143

107144
end
108145

109-
nbBins = min(nbBins);
146+
nbBins = max(nbBins);
110147

111148
for i = 1:numel(data)
112149
tmp = hist(data{i}, nbBins);
113-
maxVox(end + 1) = max(tmp);
150+
if isempty(tmp)
151+
maxVox(end + 1) = nan;
152+
else
153+
maxVox(end + 1) = max(tmp);
154+
end
114155
end
115156

116157
%% plot histogram and mode
117158

118159
idxSubplot = 1;
119160

120-
for iRoi = 1:nbRois
161+
for iRow = 1:rows
121162

122-
for iData = 1:nbData
163+
for iCol = 1:cols
123164

124165
subplot(rows, cols, idxSubplot);
125166

126167
hold on;
127168

128-
hist(data{idxSubplot}, nbBins);
169+
if ~isempty(data{idxSubplot})
170+
171+
hist(data{idxSubplot}, nbBins);
129172

130-
plot([modes(idxSubplot) modes(idxSubplot)], ...
131-
[0 max(maxVox)], ...
132-
'--r', ...
133-
'linewidth', 1);
173+
plot([modes(idxSubplot) modes(idxSubplot)], ...
174+
[0 max(maxVox)], ...
175+
'--r', ...
176+
'linewidth', 1);
134177

135-
bf = bids.File(roiImages{iRoi});
136-
title(['roi: ' bf.entities.label]);
178+
t = text(max(MAX) * 0.5, ...
179+
max(maxVox) * 0.85, ...
180+
sprintf('mean=%0.2f', mean(data{idxSubplot})));
181+
182+
set(t, 'FontSize', 10);
183+
184+
end
137185

138186
axis([min(MIN) max(MAX) 0 max(maxVox)]);
139187

@@ -143,14 +191,53 @@
143191

144192
end
145193

146-
for i = 1:cols
147-
subplot(rows, cols, subplotList(end, i));
148-
xlabel('intensities');
149-
end
194+
%% label axis
195+
196+
if strcmp(roiAs, 'rows')
197+
198+
for i = 1:cols
199+
subplot(rows, cols, subplotList(end, i));
200+
xlabel('intensities');
201+
end
202+
203+
for i = 1:rows
204+
205+
subplot(rows, cols, subplotList(i, 1));
206+
207+
bf = bids.File(roiImages{i});
208+
l = ylabel(sprintf('roi: %s\nnb voxels', bf.entities.label));
209+
set(l, 'FontWeight', 'bold');
210+
211+
end
212+
213+
else
214+
215+
for i = 1:cols
216+
217+
subplot(rows, cols, subplotList(1, i));
218+
219+
bf = bids.File(roiImages{i});
220+
title(sprintf('roi: %s', bf.entities.label));
221+
222+
subplot(rows, cols, subplotList(end, i));
223+
224+
xlabel('nb voxels');
225+
226+
end
227+
228+
for i = 1:rows
229+
230+
subplot(rows, cols, subplotList(i, 1));
231+
232+
label = '';
233+
if ~isempty(dataLabel)
234+
label = dataLabel{i};
235+
end
236+
l = ylabel(sprintf('%s', label));
237+
set(l, 'FontWeight', 'bold');
238+
239+
end
150240

151-
for i = 1:rows
152-
subplot(rows, cols, subplotList(i, 1));
153-
ylabel('nb voxels');
154241
end
155242

156243
end

0 commit comments

Comments
 (0)