-
Notifications
You must be signed in to change notification settings - Fork 2
/
createSpotTrainingSet.m
executable file
·276 lines (247 loc) · 11.2 KB
/
createSpotTrainingSet.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
function trainingSet=createSpotTrainingSet(stackName,probeName,varargin)
%% ========================================================================
% Name: createSpotTrainingSet
% Version: 2.5 25th Apr. 2013
% Author: Allison Wu
% Command:
% trainingSet=createSpotTrainingSet(stackName,probeName,appendTrainingSet*)
% *Optional Input
%
% Description:
% - Create training set by using identifySpots to identify good spots and bad spots as training set.
% - Create a data matrix (row: spots, columns:features) ready for Matlab decision trees.
% - appendTrainingSet=varargin{1}:
% * appendTrainingSet==1, append spots to existing training set and recalcalculate statistics
% * appendTrainingSet==0, overwrite any existing training set
% - Prompts the user about whether to overwrite or to append spots to an existing training set
% when appendTrainingSet==0 but an exisiting training set is found.
%
% Files required: {dye}_{stackSuffix}_SegStacks.mat, {dye}_{stackSuffix}_wormGaussianFit.mat
% Files generated: trainingSet_{dye}_{probeName}.mat.
% Updates:
% - Built-in version check to make sure the new stats are added.
% - Fix a bug that introduces mismatches of stats and spotInfo.
%
%
% Attribution: Wu, AC-Y and SA Rifkin. spotFinding Suite version 2.5, 2013 [journal citation TBA]
% License: Creative Commons Attribution-ShareAlike 3.0 United States, http://creativecommons.org/licenses/by-sa/3.0/us/
% Website: http://www.biology.ucsd.edu/labs/rifkin/software/spotFindingSuite
% Email for comments, questions, bugs, requests: Allison Wu < dblue0406 at gmail dot com >, Scott Rifkin < sarifkin at ucsd dot edu >
%
%% ========================================================================
run('Aro_parameters.m');
[dye, stackSuffix, wormGaussianFitName, segStacksName,~]=parseStackNames(stackName);
trainingSetName=fullfile(TrainingSetDir,['trainingSet_' dye '_' probeName '.mat']);
if isempty(varargin)
appendTrainingSet=0;
else
appendTrainingSet=varargin{1};
end
if exist(trainingSetName, 'file')
reply='p';
while ~strcmpi(reply,'n') && ~strcmpi(reply,'y')
reply=input('There is an exisiting training set. Do you want to overwrite it? \n(Otherwise, the program will append new spots to the existing training set.) \n Y/N [N]:','s');
if isempty(reply)
reply='N';
end
if strcmpi(reply,'y')
appendTrainingSet=0;
else strcmpi(reply,'n')
appendTrainingSet=1;
end
end;
else
appendTrainingSet=0;
end
%if appending then take some parameters from pre-existing file
if appendTrainingSet
ts=load(trainingSetName);
statsToUse=ts.trainingSet.statsUsed;
clear ts
end;
%Identify Spots
posNumber=str2num(cell2mat(regexp(stackSuffix,'\d+','match')));
disp('Load in spots information...')
load(fullfile(WormGaussianFitDir,dye,wormGaussianFitName));
% Version check
if ~strcmp('v2.5',worms{1}.version)
display('Detect an older version. Update the wormGaussianFit with new stats.')
worms=addStatsToWormGaussian(worms);
end
wormNum=size(worms,1);
stackH=worms{1}.numberOfPlanes;
w=1:wormNum;
spotsInWorm=zeros(wormNum,1);
for wi=1:wormNum
spotsInWorm(wi)=length(worms{wi}.spotDataVectors.rawValue);
end
[~,index]=sort(spotsInWorm,'descend');
w=w(index);%have it do the one with the most spots first for efficiency
disp('Load in segmented stacks...')
load(fullfile(SegStacksDir,dye,segStacksName));
disp('Identify spots in worms...')
for wi=1:wormNum
[goodSpots,badSpots,doAnotherWorm]=identifySpots(floor(stackH/8),segStacks,segMasks,worms,w(wi));
if wi==1
goldSpotsData=goodSpots;
rejectedSpotsData=badSpots;
else
if ~isempty(goodSpots)
goldSpotsData=[goldSpotsData;goodSpots];
end
if ~isempty(badSpots)
rejectedSpotsData=[rejectedSpotsData;badSpots];
end
end
if ~doAnotherWorm
break
else %doAnotherWorm
disp(['# good spots: ' num2str(length(goodSpots)) ' # bad spots: ' num2str(length(badSpots))]);
if wi==wormNum
disp('No more worms to do in this image!');
end;
end;
end
clear segStacks
goldNum=size(goldSpotsData,1);
rejNum=size(rejectedSpotsData,1);
spotNum=goldNum+rejNum;
if ~exist('trainingSet','var')
trainingSet=struct;
trainingSet.spotInfo=[];
end;
% spotInfo is: [posNumber, wormNumber, spotIndex, classification] #Note that spotIndex is worms{x}.spotDataVectors.spotInfoNumberInWorm
if goldNum>0
trainingSet.spotInfo=[ones(goldNum,1)*posNumber goldSpotsData(:,end-1:end) ones(goldNum,1)]; %good
end;
if rejNum>0
trainingSet.spotInfo=[trainingSet.spotInfo; ones(rejNum,1)*posNumber rejectedSpotsData(:,end-1:end) zeros(rejNum,1)]; %append bad
end;
trainingSet.stats=struct;
% Add stats info to training set
fieldsToAdd=fields(worms{1}.spotDataVectors);
for fta=1:length(fieldsToAdd)
for k=1:2
for wi=1:length(worms)
wormData=worms{wi};
if k==1
wormIndex=(trainingSet.spotInfo(:,2)==wi & trainingSet.spotInfo(:,end)==1);
else
wormIndex=(trainingSet.spotInfo(:,2)==wi & trainingSet.spotInfo(:,end)==0);
end
spotIndex=(trainingSet.spotInfo(wormIndex,3));
if ~sum(strcmp(fieldsToAdd{fta},{'spotInfoNumberInWorm','nucLocation','distanceToNuc'}))
if ~isfield(trainingSet.stats, fieldsToAdd{fta})
if ~sum(strcmp(fieldsToAdd{fta},{'dataMat','dataFit'}))
trainingSet.stats.(fieldsToAdd{fta})=wormData.spotDataVectors.(fieldsToAdd{fta})(spotIndex,:);
else
trainingSet.stats.(fieldsToAdd{fta})=wormData.spotDataVectors.(fieldsToAdd{fta})(spotIndex,:,:);
end
else
if ~sum(strcmp(fieldsToAdd{fta},{'dataMat','dataFit'}))
trainingSet.stats.(fieldsToAdd{fta})=[trainingSet.stats.(fieldsToAdd{fta});wormData.spotDataVectors.(fieldsToAdd{fta})(spotIndex,:)];
else
trainingSet.stats.(fieldsToAdd{fta})=[trainingSet.stats.(fieldsToAdd{fta});wormData.spotDataVectors.(fieldsToAdd{fta})(spotIndex,:,:)];
end
end
end
end
end
end
% Calculate SVD
allDataPixelValues=trainingSet.stats.dataMat(:,:);
%center data
trainingSet.allDataCenter=mean(allDataPixelValues,1);
allDataPixelValuesCentered=allDataPixelValues-repmat(trainingSet.allDataCenter,size(allDataPixelValues,1),1);
[~,~,v]=svd(allDataPixelValuesCentered,0);
trainingSet.svdBasisRightMultiplier=(v')^(-1);
rotatedAllDataPixelValues=allDataPixelValuesCentered*trainingSet.svdBasisRightMultiplier;
%trainingSet.stats.sv=zeros(length(trainingSet.spotInfo),5);
for i=1:5
%take the first five coordinates of in the new basis
trainingSet.stats.(['sv' num2str(i)])=rotatedAllDataPixelValues(:,i);
end
% Stores a dataMatrix ready for Matlab random forest.
%statsToUse is in Aro_parameters
trainingSet.statsUsed=statsToUse;
% Create dataMatrix (with predictor X and response Y) for Matlab Random Forest
trainingSet.dataMatrix.X=zeros(spotNum,length(statsToUse));
startj=1;
for j=1:length(statsToUse)
stat=trainingSet.stats.(statsToUse{j});
endj=startj-1+size(stat,2);
trainingSet.dataMatrix.X(:,startj:endj)=stat;
startj=endj+1;
end
trainingSet.dataMatrix.Y=trainingSet.spotInfo(:,end);
% Append new training set to exisiting training set if necessary.
if appendTrainingSet==0
trainingSet.version= 'ver. 2.5, new stats added';
trainingSet.appended=0; % 0, if it's a newly created training set. 1, if it's a training set that had been appended with new spots.
disp('Saving the training set...')
save(trainingSetName,'trainingSet')
elseif appendTrainingSet==1
%Should check for duplicates!!
disp('Append new spot information and stats to existing training set...')
trainingSetToAppend=trainingSet;
load(trainingSetName)
% Check if new stats are added.
if isfield(trainingSet,'version')
if ~strcmp('ver. 2.5, new stats added',trainingSet.version)
display('Detect an older version. Update the trainingSet with new stats.')
trainingSet=addStatsToTrainingSet(trainingSet,1);
end
end;
trainingSet.appended=1;
% Find spots that were not in the training set.
a=trainingSetToAppend.spotInfo(:,1:3);
b=trainingSet.spotInfo(:,1:3);
[~,~,iAppend]=union(b,a,'rows');
% Find duplicated spots.
disp('Find duplicated spots....')
[d,iOrigi,iUpdate]=intersect(b,a,'rows');
% Update the classification for duplicated spots if necessary.
if ~isempty(iUpdate)
disp('Duplicated spot:')% Print out the spot information for debugging.
disp('Update the classification...')
trainingSet.dataMatrix.Y(iOrigi)=trainingSetToAppend.dataMatrix.Y(iUpdate);
trainingSet.spotInfo(iOrigi,4)=trainingSetToAppend.spotInfo(iUpdate,4);
end
% Append the rest of the newly picked spots.
if ~isempty(iAppend)
disp('Append newly picked spots to the training set...')
trainingSet.spotInfo=[trainingSet.spotInfo;trainingSetToAppend.spotInfo(iAppend,:)];
for k=1:length(fieldsToAdd)
if ~sum(strcmp(fieldsToAdd{k},{'spotInfoNumberInWorm','nucLocation','distanceToNuc'}))
if ~sum(strcmp(fieldsToAdd{k},{'dataMat','dataFit'}))
trainingSet.stats.(fieldsToAdd{k})=[trainingSet.stats.(fieldsToAdd{k});trainingSetToAppend.stats.(fieldsToAdd{k})(iAppend,:)];
else
trainingSet.stats.(fieldsToAdd{k})=[trainingSet.stats.(fieldsToAdd{k});trainingSetToAppend.stats.(fieldsToAdd{k})(iAppend,:,:)];
end
end
end
trainingSet.dataMatrix.Y=[trainingSet.dataMatrix.Y;trainingSetToAppend.dataMatrix.Y(iAppend,:)];
end
disp('Recalculate SVD...')
% Recalculate SVD
allDataPixelValues=trainingSet.stats.dataMat(:,:);
%center data
trainingSet.allDataCenter=mean(allDataPixelValues,1);
allDataPixelValuesCentered=allDataPixelValues-repmat(trainingSet.allDataCenter,size(allDataPixelValues,1),1);
[~,~,v]=svd(allDataPixelValuesCentered,0);
trainingSet.svdBasisRightMultiplier=(v')^(-1);
rotatedAllDataPixelValues=allDataPixelValuesCentered*trainingSet.svdBasisRightMultiplier;
%trainingSet.stats.sv=zeros(length(trainingSet.spotInfo),5);
for i=1:5
%take the first five coordinates in the new basis
trainingSet.stats.(['sv' num2str(i)])=rotatedAllDataPixelValues(:,i);
end;
trainingSet.dataMatrix.X=[trainingSet.dataMatrix.X;trainingSetToAppend.dataMatrix.X(iAppend,:)];
%trainingSet.dataMatrix.X(:,end-4:end)=trainingSet.stats.sv;
end
trainingSet.FileName=trainingSetName;
disp('Saving the training set...')
save(trainingSetName,'trainingSet')
fprintf('There are %d spots in total in the training set.\n', length(trainingSet.dataMatrix.Y))
fprintf('%d good spots and %d bad spots were chosen.\n', sum(trainingSet.dataMatrix.Y),sum(trainingSet.dataMatrix.Y==0));
end