forked from FernandoPalazuelos/Displasias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplasia_ttest_2D.m
170 lines (153 loc) · 5.22 KB
/
displasia_ttest_2D.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
function pvals = displasia_ttest_2D(groupA,groupB,...
clusterformingpthreshold,conn,doPlot)
if size(groupA,[1 2]) ~= size(groupB,[1,2])
error('First two dimensions of groupA and groupB must agree.')
return
end
% parametric test for comparison
[h,pAgtB] = ttest2(groupA,groupB,'dim',3,'tail','right');
[h,pAltB] = ttest2(groupA,groupB,'dim',3,'tail','left');
[h,pAdiffB] = ttest2(groupA,groupB,'dim',3,'tail','both');
pvals.pAgtB = pAgtB;
pvals.pAltB = pAltB;
pvals.pAdiffB = pAdiffB;
pvals.diff = nanmean(groupA,3) - nanmean(groupB,3);
CC = bwconncomp(pAgtB < clusterformingpthreshold,conn);
pvals.clustersizes.AgtB = cellfun(@numel,CC.PixelIdxList);
L = labelmatrix(CC);
pvals.clusterlabels.AgtB = L;
CC = bwconncomp(pAltB < clusterformingpthreshold,conn);
pvals.clustersizes.AltB = cellfun(@numel,CC.PixelIdxList);
L = labelmatrix(CC);
pvals.clusterlabels.AltB = L;
CC = bwconncomp(pAdiffB < clusterformingpthreshold,conn);
pvals.clustersizes.AdiffB = cellfun(@numel,CC.PixelIdxList);
L = labelmatrix(CC);
pvals.clusterlabels.AdiffB = L;
if doPlot
figure;
cmin = nanmin([groupA(:);groupB(:)]);
cmax = nanmax([groupA(:);groupB(:)]);
subplot(3,2,1);imagesc(nanmean(groupA,3));set(gca,'Clim',[cmin cmax]);colorbar;title('A')
subplot(3,2,2);imagesc(nanmean(groupB,3));set(gca,'Clim',[cmin cmax]);colorbar;title('B')
subplot(3,2,3);imagesc(pAgtB); set(gca,'Clim',[0 0.05]);colorbar;title('pAgtB')
subplot(3,2,4);imagesc(pAltB); set(gca,'Clim',[0 0.05]);colorbar;title('pAltB')
subplot(3,2,5);imagesc(pAdiffB);set(gca,'Clim',[0 0.05]);colorbar;title('pAdiffB')
end
%CC = bwconncomp(pvals.clusterlabels.AdiffB < clusterformingpthreshold,conn);
%pvals.clustersizes = cellfun(@numel,CC.PixelIdxList);
% end
% clusters = 'blah';
%
% return
%
% rPvalsPerm = zeros(nr*nc,ndiffperms);
% for perm = 1 : ndiffperms
% % dividing into two samples
% permutation = randperm(size(rAB,2));
% idx1 = permutation(1:ngA);
% idx2 = permutation(ngA+1:end);
% randomSample1 = rAB(:,idx1);
% randomSample2 = rAB(:,idx2);
% %[hp,pp] = ttest2(randomSample1,randomSample2,'dim',2);
% %rPvalsPerm(:,perm) = pp;
%
% end
%
%
%
% % find cluster sizes found by chance
% pPerms = reshape(rPvalsPerm,[nr nc nperms]);
% clustersizes = [];
% for perm = 1 : nperms
% CC = bwconncomp(pPerms(:,:,perm) < pthresh,conn);
% numPixels = cellfun(@numel,CC.PixelIdxList);
% clustersizes = [clustersizes numPixels];
% end
%
%
%
%
% clustersizethreshold = round(prctile(clustersizes,tpercentile));
% fprintf(1,'Maximum cluster size at %d percentile after %d permutations: %d\n',tpercentile,nperms,clustersizethreshold);
%
%
% % stats on actual data
% [rH,rP] = ttest2(rA,rB,'dim',2);
% P = reshape(rP,[nr nc]);
% CC = bwconncomp(P < cfthresh,conn);
% numPixels = cellfun(@numel,CC.PixelIdxList);
% [biggest,idx] = max(numPixels);
% L = labelmatrix(CC);
% clusters = ones(size(L));
%
% sigcluster = zeros(1,length(numPixels));
% pclusters = zeros(1,length(numPixels));
% centroids = zeros(length(numPixels),2);
% for lab = 1 : length(numPixels)
% thisnumpixels = numel(find(L==lab));
% nClustersEqualOrLargerThanThisOne = numel(find(clustersizes>=thisnumpixels));
% pclus = nClustersEqualOrLargerThanThisOne ./ numel(clustersizes);
% pclusters(lab) = pclus;
% if pclus <= pthresh
% fprintf(1,'Cluster\t%d\t%d pixels\tp=%1.4f is significant\n',lab,thisnumpixels,pclus);
% thiscentroid = regionprops(L==lab,'centroid');
% centroids(lab,:) = thiscentroid.Centroid;
% else
% fprintf(1,'Cluster\t%d\t%d pixels\tp=%1.4f\n',lab,thisnumpixels,pclus);
% end
% clusters(L==lab) = pclus;
%
% end
%
%
%
%
% if doPlot
% cmap_div = uint8(cbrewer('div','PuOr',128, 'spline') .* 255);
% cmap_warm = uint8(cbrewer('seq','YlOrBr',128,'spline') .* 255);
% cmap_cool = uint8(cbrewer('seq','PuBuGn',128,'spline') .* 255);
% cmap_pval = hot(128);
% cmap_flag = prism(50); cmap_flag(1,:) = [1 1 1];
%
% subplot(2,3,1)
% imagesc(mean(groupA,3)');
% set(gca,'colormap',cmap_cool); colorbar;
% title([datatitle ' | Mean of Group A'])
%
% subplot(2,3,2)
% imagesc(mean(groupB,3)');
% set(gca,'colormap',cmap_cool); colorbar;
% title([datatitle ' | Mean of Group B'])
%
%
% subplot(2,3,3)
% DIFF = mean(groupA,3) - mean(groupB,3);
% imagesc(DIFF');
% lims = get(gca,'Clim');
% newlims = [abs(max(lims))*-1 max(lims)];
% set(gca,'colormap',cmap_div); colorbar;
% set(gca,'Clim',newlims);
% title('A-B')
% subplot(2,3,4)
% imagesc(P');
% set(gca,'colormap',cmap_pval); colorbar;
% set(gca,'Clim',[0 cfthresh]);colorbar
% title('P values');
% subplot(2,3,5)
% histogram(clustersizes);
% title(sprintf('Cluster threshold\n(%d permutations)',nperms))
% vline(clustersizethreshold,'r',num2str(clustersizethreshold))
% subplot(2,3,6)
% imagesc(clusters' < pthresh);
% set(gca,'colormap',cmap_flag); colorbar;
% hold on
% for c = 1 : length(pclusters)
% thispclus = pclusters(c);
% if thispclus < pthresh
% text(centroids(c,2),centroids(c,1),num2str(pclusters(c)));
% end
% end
% title('clusters')
% end
%