forked from BiomedNMR/arrShow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asStatisticsClass.m
369 lines (309 loc) · 13.5 KB
/
asStatisticsClass.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
% Copyright (C) 2009-2013 Biomedizinische NMR Forschungs GmbH
% http://www.biomednmr.mpg.de
% Author: Tilman Johannes Sumpf <[email protected]>
%
% Distributed under the Boost Software License, Version 1.0.
% (See accompanying file LICENSE_1_0.txt or copy at
% http://www.boost.org/LICENSE_1_0.txt)
classdef asStatisticsClass < handle
properties (GetAccess = private, SetAccess = private)
pph = 0; % parent panel handle
ph = 0; % panel handle
panPos = [0 0 1 1] % panel position
ah = [] % axes handle of the reference image
enabled = true;
isComplex = false;
precision = '%2.4g';
fontSize = 8;
% handles for image points
minPointH = [];
maxPointH = [];
% text handles for 'dynamic text'
thDim = 0;
thMin = 0;
thMean = 0;
thMax = 0;
thNorm = 0;
% text handles for 'static text'
sthDim = 0;
sthMin = 0;
sthMean = 0;
sthMax = 0;
sthNorm = 0;
% image stats
imgDim = 0;
imgMin = 0;
imgMinPos = [0,0];
imgMean = 0;
imgMax = 0;
imgMaxPos = [0,0];
imgNorm = 0;
end
properties(Constant)
MIN_VALID_RANGE = 1e-6;
end
methods
function obj = asStatisticsClass(parentPanelHandle, panelPosition)
obj.pph = parentPanelHandle;
obj.panPos = panelPosition;
% create parent panel
obj.ph = uipanel('visible','on','Units','normalized',...
'Position',obj.panPos,'Parent',parentPanelHandle,...
'Tag','asStatisticsPanel');
% Create static text objects for min, mean and max
stw = 1/3; % static textfield width
th = 1/5; % textfield heigth
obj.sthDim = uicontrol('Style','Text','String','Dim :','HorizontalAlignment','left','FontSize',obj.fontSize,...
'Units','normalized','pos',[0 4/5 stw th],'parent',obj.ph,'HandleVisibility','on');
obj.sthMin = uicontrol('Style','togglebutton','String','Min :','HorizontalAlignment','left','FontSize',obj.fontSize,...
'Units','normalized','pos',[0 3/5 stw th],'parent',obj.ph,'HandleVisibility','on');
set(obj.sthMin,'callback',@(src,evnt)obj.setMarkMin(get(obj.sthMin,'value')));
obj.sthMean= uicontrol('Style','Text','String','Mean:','HorizontalAlignment','left','FontSize',obj.fontSize,...
'Units','normalized','pos',[0 2/5 stw th],'parent',obj.ph,'HandleVisibility','on');
obj.sthMax = uicontrol('Style','togglebutton','String','Max :','HorizontalAlignment','left','FontSize',obj.fontSize,...
'Units','normalized','pos',[0 1/5 stw th],'parent',obj.ph,'HandleVisibility','on');
set(obj.sthMax,'callback',@(src,evnt)obj.setMarkMax(get(obj.sthMax,'value')));
obj.sthNorm= uicontrol('Style','Text','String','L2 :','HorizontalAlignment','left','FontSize',obj.fontSize,...
'Units','normalized','pos',[0 0 stw th],'parent',obj.ph,'HandleVisibility','on');
% Create editable text objects for min, mean and max
etw = 1 - stw; % editable text filed width
obj.thDim = uicontrol('Style','Text','String','0.0','HorizontalAlignment','left','FontSize',obj.fontSize,...
'Units','normalized','pos',[stw 4/5 etw th],'parent',obj.ph,'HandleVisibility','on');
obj.thMin = uicontrol('Style','Text','String','0.0','HorizontalAlignment','left','FontSize',obj.fontSize,...
'Units','normalized','pos',[stw 3/5 etw th],'parent',obj.ph,'HandleVisibility','on');
obj.thMean= uicontrol('Style','Text','String','0.0','HorizontalAlignment','left','FontSize',obj.fontSize,...
'Units','normalized','pos',[stw 2/5 etw th],'parent',obj.ph,'HandleVisibility','on');
obj.thMax = uicontrol('Style','Text','String','0.0','HorizontalAlignment','left','FontSize',obj.fontSize,...
'Units','normalized','pos',[stw 1/5 etw th],'parent',obj.ph,'HandleVisibility','on');
obj.thNorm= uicontrol('Style','Text','String','0.0','HorizontalAlignment','left','FontSize',obj.fontSize,...
'Units','normalized','pos',[stw 0 etw th],'parent',obj.ph,'HandleVisibility','on');
end
function range = getImageRange(obj)
range = str2double(get(obj.thMax,'String')) - str2double(get(obj.thMin,'String'));
end
function disableText(obj)
if obj.enabled
childs = get(obj.ph,'Children');
for i = 1 : length(childs)
set(childs(i),'Enable','off');
end
obj.enabled = false;
end
end
function enableText(obj)
if ~obj.enabled
childs = get(obj.ph,'Children');
for i = 1 : length(childs)
set(childs(i),'Enable','on');
end
obj.enabled = true;
end
end
function setImageStats(obj,refImg)
obj.isComplex = false;
if ishandle(refImg)
axesH = get(refImg,'Parent');
if strcmp(get(refImg,'Type'),'hggroup') ||...
strcmp(get(refImg,'Type'),'quiver')
% assume that quiver has been used to illustrate the
% data. Therefore the image should be stored in the
% axes userData
ud = get(axesH,'UserData');
refImg = ud.selectedImage;
else
refImg = get(refImg,'CData');
end
if size(refImg,3) == 3 || ~isreal(refImg)
% assume that we are dealing with an rgb array, made from a
% complex image.
% So get complex image from the axes UserData
ud = get(axesH,'UserData');
refImg = ud.cplxImg;
obj.isComplex = true;
end
obj.ah = axesH;
end
obj.imgDim = size(refImg);
refVect = refImg(:);
obj.imgNorm= norm(refVect,2);
if obj.isComplex
% treat real and imaginary part separately
obj.imgMin = min(real(refVect)) + 1i * min(imag(refVect));
obj.imgMean= mean(real(refVect)) + 1i * mean(imag(refVect));
obj.imgMax = max(real(refVect)) + 1i * max(imag(refVect));
% not implementet yet
obj.imgMinPos = [-1,-1];
obj.imgMaxPos = [-1,-1];
else
obj.imgMin = min (refVect);
obj.imgMean= mean(refVect);
obj.imgMax = max (refVect);
% min and max positions
imgMinInd = find(refVect == obj.imgMin,1,'first');
imgMaxInd = find(refVect == obj.imgMax,1,'first');
[py,px]= ind2sub(obj.imgDim,imgMinInd);
obj.imgMinPos = [py,px];
[py,px]= ind2sub(obj.imgDim,imgMaxInd);
obj.imgMaxPos = [py,px];
end
obj.updateImageStats();
if obj.getImageRange < obj.MIN_VALID_RANGE
set(obj.thMin,'ForegroundColor','red');
set(obj.thMax,'ForegroundColor','red');
else
set(obj.thMin,'ForegroundColor','black');
set(obj.thMax,'ForegroundColor','black');
end
clear refVect refImg
end
% function delete(obj)
% if ishandle(obj.ph)
% delete(obj.ph);
% end
% clear obj;
% end
function str = getImageStatsString(obj)
str = char(...
['min = ', get(obj.thDim ,'String')],...
['min = ', get(obj.thMin ,'String')],...
['mean = ', get(obj.thMean,'String')],...
['max = ', get(obj.thMax ,'String')],...
['L2 = ', get(obj.thNorm,'String')]);
end
function str = getImageStatsCellString(obj)
str = {...
['dim = ', get(obj.thDim ,'String')];...
['min = ', get(obj.thMin ,'String')];...
['mean = ', get(obj.thMean,'String')];...
['max = ', get(obj.thMax ,'String')];...
['L2 = ', get(obj.thNorm,'String')]};
end
function stats = getImageStats(obj)
stats = [ obj.getMin;
obj.getMean;
obj.getMax;
obj.getNorm];
end
function bool = getMarkMinState(obj)
bool = get(obj.sthMin,'value');
end
function bool = getMarkMaxState(obj)
bool = get(obj.sthMax,'value');
end
function setMarkMin(obj, state)
if state
obj.drawMinPoint();
else
delete(obj.minPointH);
end
set(obj.sthMin,'value', state);
end
function setMarkMax(obj, state)
if state
obj.drawMaxPoint();
else
delete(obj.maxPointH);
end
set(obj.sthMax,'value', state);
end
function min = getMin(obj)
min = obj.imgMin;
end
function min = getMean(obj)
min = obj.imgMean;
end
function min = getMax(obj)
min = obj.imgMax;
end
function min = getNorm(obj)
min = obj.imgNorm;
end
function dim = getDimensions(obj)
dim = obj.imgDim;
end
end
methods (Access = private)
function updateImageStats(obj)
obj.setDimStr (num2str(obj.imgDim));
obj.setNormStr(num2str(obj.imgNorm,obj.precision));
if obj.isComplex
obj.setMinStr ('-');
obj.setMeanStr('-');
obj.setMaxStr ('-');
else
minStr = num2str(obj.imgMin, obj.precision);
minTooltipStr = sprintf('%s @ %d / %d',minStr,obj.imgMinPos(1),obj.imgMinPos(2));
obj.setMinStr (minStr,minTooltipStr);
obj.setMeanStr(num2str(obj.imgMean,obj.precision));
maxStr = num2str(obj.imgMax, obj.precision);
maxTooltipStr = sprintf('%s @ %d / %d',maxStr,obj.imgMaxPos(1),obj.imgMaxPos(2));
obj.setMaxStr (maxStr,maxTooltipStr);
if get(obj.sthMin,'value')
% draw minimum marker
obj.drawMinPoint();
end
if get(obj.sthMax,'value')
% draw maximum marker
obj.drawMaxPoint();
end
end
end
function drawMinPoint(obj)
obj.minPointH = impoint(obj.ah,obj.imgMinPos(2),obj.imgMinPos(1));
obj.minPointH.setColor('green');
end
function drawMaxPoint(obj)
obj.maxPointH = impoint(obj.ah,obj.imgMaxPos(2),obj.imgMaxPos(1));
obj.maxPointH.setColor('red');
end
function setDimStr(obj, dim)
if ~obj.enabled
obj.enableText();
end
if ischar(dim)
str = dim;
else
str = num2str(dim(1));
for i = 2 : length(dim)
str = [str,' x ',num2str(dim(i))]; %#ok<AGROW> length(dim) is usually small
end
end
set(obj.thDim,'String',str,'TooltipString',str);
set(obj.sthDim,'TooltipString',str);
end
function setMinStr(obj,str,toolTipStr)
if ~obj.enabled
obj.enableText();
end
if nargin < 3
toolTipStr = str;
end
set(obj.thMin,'String',str,'TooltipString',toolTipStr);
set(obj.sthMin,'TooltipString',toolTipStr);
end
function setMeanStr(obj,str)
if ~obj.enabled
obj.enableText();
end
set(obj.thMean,'String',str,'TooltipString',str);
set(obj.sthMean,'TooltipString',str);
end
function setMaxStr(obj,str,toolTipStr)
if ~obj.enabled
obj.enableText();
end
if nargin < 3
toolTipStr = str;
end
set(obj.thMax,'String',str,'TooltipString',toolTipStr);
set(obj.sthMax,'TooltipString',toolTipStr);
end
function setNormStr(obj,str)
if ~obj.enabled
obj.enableText();
end
set(obj.thNorm,'String',str,'TooltipString',str);
set(obj.sthNorm,'TooltipString',str);
end
end
end