-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrepare_Data_norm.m
252 lines (228 loc) · 7.6 KB
/
Prepare_Data_norm.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
function Out = Prepare_Data_norm(dset,NORM,Parm)
% Out = Prepare_Data_norm(dset,NORM,Parm)
if exist('Parm')==0
Parm.Method = ['tSNE']; % other methods 2) kpca or 3) pca
Parm.Max_Px_Size = 227;
Parm.MPS_Fix = 1; % if this val is 1 then screen will be Max_Px_Size x Max_Px_Size (eg 120x120 )
% otherwise automatically decided by the distribution
% of the input data.
Parm.ValidRatio = 0.1; % 0.1 of Train data will be used as Validation data
Parm.Seed = 108; %random seed
Parm.SnowFall_A = Parm.Max_Px_Size;
Parm.SnowFall_B = Parm.Max_Px_Size;
Parm.SnowFall = 1;
end
%OverWrite (for blur method - if used)
Out.OverWrite=Parm.OverWrite; % 'yes' | 'no'
%Take data on "as is" basis; i.e., no change in Train and Test data
TrueLabel=[];
for j=1:dset.class
TrueLabel=[TrueLabel,ones(1,dset.num_tr(j))*j];
end
Out.YTest=[];
for j=1:dset.class
Out.YTest=[Out.YTest,ones(1,dset.num_tst(j))*j];
end
Out.YTest=categorical(Out.YTest)';
Out.YTrain=categorical(TrueLabel)';
q=1:length(TrueLabel);
clear idx
for j=1:dset.class
rng=q(double(TrueLabel)==j);
rand('seed',Parm.Seed);
idx{j} = rng(randperm(length(rng),round(length(rng)*Parm.ValidRatio)));
end
idx=cell2mat(idx);
dset.XValidation = dset.Xtrain(:,idx,:);
dset.Xtrain(:,idx,:) = [];
Out.YValidation = Out.YTrain(idx);
Out.YTrain(idx) = [];
for j=1:dset.class
dset.num_tr(j)=sum(double(Out.YTrain)==j);
dset.num_val(j)=sum(double(Out.YValidation)==j);
end
switch NORM
case 1
% Norm-3 in org code
Out.Norm=1;
%fprintf(Parm.fid,'\nNORM-1\n');
fprintf('\nNORM-1\n');
%########### Norm-1 ###################
for dsz=1:size(dset.Xtrain,3)
Out.Max=max(dset.Xtrain(:,:,dsz)')';
Out.Min=min(dset.Xtrain(:,:,dsz)')';
dset.Xtrain(:,:,dsz)=(dset.Xtrain(:,:,dsz)-Out.Min)./(Out.Max-Out.Min);
dset.XValidation(:,:,dsz) = (dset.XValidation(:,:,dsz)-Out.Min)./(Out.Max-Out.Min);
dset.Xtest(:,:,dsz) = (dset.Xtest(:,:,dsz)-Out.Min)./(Out.Max-Out.Min);
end
dset.Xtrain(isnan(dset.Xtrain))=0;
dset.Xtest(isnan(dset.Xtest))=0;
dset.XValidation(isnan(dset.XValidation))=0;
dset.XValidation(dset.XValidation>1)=1;
dset.XValidation(dset.XValidation<0)=0;
dset.Xtest(dset.Xtest>1)=1;
dset.Xtest(dset.Xtest<0)=0;
%######################################
case 2
% norm-6 in org ocde
Out.Norm=2;
%fprintf(Parm.fid,'\nNORM-2\n');
fprintf('\nNORM-2\n');
%########### Norm-2 ###################
for dsz=1:size(dset.Xtrain,3)
Out.Min=min(dset.Xtrain(:,:,dsz)')';
dset.Xtrain(:,:,dsz)=log(dset.Xtrain(:,:,dsz)+abs(Out.Min)+1);
indV = dset.XValidation(:,:,dsz)<Out.Min;
indT = dset.Xtest(:,:,dsz)<Out.Min;
for j=1:size(dset.Xtrain,1)
dset.XValidation(j,indV(j,:),dsz)=Out.Min(j);
dset.Xtest(j,indT(j,:),dsz)=Out.Min(j);
end
dset.XValidation(:,:,dsz) = log(dset.XValidation(:,:,dsz)+abs(Out.Min)+1);
dset.Xtest(:,:,dsz)=log(dset.Xtest(:,:,dsz)+abs(Out.Min)+1);
Out.Max=max(max(dset.Xtrain(:,:,dsz)));
dset.Xtrain(:,:,dsz)=dset.Xtrain(:,:,dsz)/Out.Max;
dset.XValidation(:,:,dsz) = dset.XValidation(:,:,dsz)/Out.Max;
dset.Xtest(:,:,dsz) = dset.Xtest(:,:,dsz)/Out.Max;
end
dset.XValidation(dset.XValidation>1)=1;
dset.Xtest(dset.Xtest>1)=1;
%######################################
end
Q.Method = Parm.Method;%['tSNE'];
Q.TightRep = Parm.TightRep;
if strcmp(lower(Q.Method),'lda')==1
Q.Labels=[];
for j=1:dset.class
Q.Labels=[Q.Labels;ones(dset.num_tr(j),1)*j];
end
end
Q.Max_Px_Size = Parm.Max_Px_Size; % <----- NOTE
Q.SnowFall = Parm.SnowFall;
if any(strcmp('Dist',fieldnames(Parm)))==1
Q.Dist=Parm.Dist;
end
if Q.SnowFall==1
Q.SnowFall_A = Parm.SnowFall_A;
Q.SnowFall_B = Parm.SnowFall_B;
end
Q.z=1; % if Q.z=1 then z values will be output and snow-fall will not be used.
if Parm.FeatureMap==0
disp('multi-omics data used for Cart2Pixel');
for dsz=1:size(dset.Xtrain,3)
Q.data = dset.Xtrain(:,:,dsz);
% if dsz==3
% Q.data = Q.data+eps;
% end
% if Q.z==0
% if Parm.MPS_Fix==1
% [Out.XTrain,Out.xp,Out.yp,Out.A,Out.B,Out.Base] = Cart2Pixel(Q,Q.Max_Px_Size,Q.Max_Px_Size);
% else
% [Out.XTrain,Out.xp,Out.yp,Out.A,Out.B,Out.Base] = Cart2Pixel(Q);
% end
% else
if Parm.MPS_Fix==1
[Out.z{dsz}] = Cart2Pixel(Q,Q.Max_Px_Size,Q.Max_Px_Size);
else
[Out.z{dsz}] = Cart2Pixel(Q);
end
Out.z{dsz} = (Out.z{dsz} - min(min(Out.z{dsz})))/(max(max(Out.z{dsz}))-min(min(Out.z{dsz})));
end
Out.z = cell2mat(Out.z);
Q.data = Out.z;
Out = rmfield(Out,'z');
end
Q.z=0;
if Parm.FeatureMap>0
switch Parm.FeatureMap
case 1
disp('Expression data used for Cart2Pixel');
case 2
disp('Methylation data used for Cart2Pixel');
case 3
disp('Mutation data used for Cart2Pixel');
case 4
disp('Concatenate all the layers for Cart2Pixel');
end
if Parm.FeatureMap==4
if size(dset.Xtrain,3)==3
Q.data = [dset.Xtrain(:,:,1),dset.Xtrain(:,:,2),...
dset.Xtrain(:,:,3)];
if strcmp(Q.Method,'lda')==1
Q.Labels = [Q.Labels;Q.Labels;Q.Labels];
end
elseif size(dset.Xtrain,3)==2
Q.data=[dset.Xtrain(:,:,1),dset.Xtrain(:,:,2)];
if strcmp(Q.Method,'lda')==1
Q.Labels = [Q.Labels;Q.Labels];
end
elseif size(dset.Xtrain,3)==1
Q.data=[dset.Xtrain(:,:,1)];
disp(['Since data has only 1 layer, ' ...
'values FeatureMap>0 will use the same algorithm']);
else
disp('Error: data formatis not correct!');
Out=[];
return
end
else
Q.data = dset.Xtrain(:,:,Parm.FeatureMap);
end
end
if Parm.MPS_Fix==1
[tmp,Out.xp,Out.yp,Out.A,Out.B,Out.Base] = Cart2Pixel(Q,Q.Max_Px_Size,Q.Max_Px_Size);
else
[tmp,Out.xp,Out.yp,Out.A,Out.B,Out.Base] = Cart2Pixel(Q);
end
% Re_Out = ReAdjustPoints([Out.xp;Out.yp]',Parm.Max_Px_Size);
% Out.xp = Re_Out(:,1)';
% Out.yp = Re_Out(:,2)';
% Out.A = max(Re_Out(:,1));
% Out.B = max(Re_Out(:,2));
% clear Re_Out;
% assignment
if strcmp(Parm.Assignment,'yes')==1
fprintf('\nAssignment started..\n');
zp_tmp = assignment([Out.xp;Out.yp],'pixel',max([Out.A Out.B]));
Out.xp = zp_tmp(:,1)';
Out.yp = zp_tmp(:,2)';
clear zp_tmp
fprintf('Assignment completed\n');
end
if strcmp(Parm.UseIntegrate,'yes');
Assign = strcmpi(Parm.integrate,'assignment');
if any(Assign)==1
save('NormalizedData.mat','dset','-v7.3');
end
end
fprintf('\n Pixels: %d x %d\n',Out.A,Out.B);
clear Q
for dsz = 1:size(dset.Xtrain,3)
for j=1:length(Out.YTrain)
Out.XTrain(:,:,dsz,j) = ConvPixel(dset.Xtrain(:,j,dsz),Out.xp,Out.yp,Out.A,Out.B,Out.Base,0);
end
end
dset.Xtrain=[];
close all;
for dsz = 1:size(dset.Xtest,3)
for j=1:length(Out.YTest)
Out.XTest(:,:,dsz,j) = ConvPixel(dset.Xtest(:,j,dsz),Out.xp,Out.yp,Out.A,Out.B,Out.Base,0);
end
end
dset.Xtest=[];
for dsz=1:size(dset.XValidation,3)
for j=1:length(Out.YValidation)
Out.XValidation(:,:,dsz,j) = ConvPixel(dset.XValidation(:,j,dsz),Out.xp,Out.yp,Out.A,Out.B,Out.Base,0);
end
end
dset.XValidation=[];
Out.C = size(Out.XTrain,3);
% for j=1:length(Out.YTrain)
% Out.XTrain(:,:,1,j) = Out.M{j};
% end
% clear M X Y
%Out = rmfield(Out,'M');
% Out.XTrain = uint8(mat2gray(Out.XTrain)*255);
% Out.XValidation = uint8(mat2gray(Out.XValidation)*255);
% Out.XTest = uint8(mat2gray(Out.XTest)*255);
end