-
Notifications
You must be signed in to change notification settings - Fork 0
/
fit_with_harmonics_highres.m
232 lines (169 loc) · 6.77 KB
/
fit_with_harmonics_highres.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
%% Fit additional data-sets on harmonics generated from initial data-sets
close all
clear all
clc
remotepath = mypath();
fdaMPath = [remotepath 'fda'];
addpath(fdaMPath)
grabdataPath = [remotepath 'Code + Stage and Outputsignal'];
addpath(grabdataPath)
dataPath = 'Dataset2_130825_2minute_interval';
sites = [4 17 37 44 57 64];
input_names = {'EGF','IGF','HRG','HGF','EPR','BTC'};
sites_for_harmonics = [17 57 64];
% all ligands highest dose
times = cell(0);
signals = cell(0);
celltype = [];
for isite = sites
if exist(remotepath,'dir')
[times{end+1},intensity] = grabdata(isite,dataPath);
else
load(['./Workspaces/site_' num2str(isite)])
times{end+1} = timestamp;
end
log_trafo = 1; % log-transform signal
if log_trafo
signals{end+1} = log10(intensity);
else
signals{end+1} = intensity;
end
celltype = [celltype ones(1,size(intensity,2))*isite];
end
timestamp = times{1}; % same time sampling for all data sets
c_signal = cell2mat(signals);
return
%% Generate spline fits to data-sets given in sites_for_harmonics
close all
nbasis = 40;
% time_range = [min(timestamp) max(timestamp)];
time_range = [200 650];
[tmp range_ind_min] = min(abs(timestamp - time_range(1)));
[tmp range_ind_max] = min(abs(timestamp - time_range(2)));
range_ind = range_ind_min:range_ind_max;
ind_harm = ismember(celltype,sites_for_harmonics);
ind_fit = ~ind_harm;
basis = create_bspline_basis([timestamp(range_ind(1)) timestamp(range_ind(end))], nbasis);
smoothed_data = smooth_basis(timestamp(range_ind),c_signal(range_ind,ind_harm),basis);
f = figure;
set(f,'DefaultAxesColorOrder',jet(size(c_signal(1,ind_harm),2)))
hold on
plot(smoothed_data)
plot(timestamp(range_ind),c_signal(range_ind,ind_harm),'o')
%% Make FPCA with data generated in previous block - wip
close all
nharm = 8;
c_signal_pcastr = pca_fd(smoothed_data, nharm);
plot_pca_fd(c_signal_pcastr, 1, 0)
%% Plot: Eigenfunctions
close all
rowstocols = 0.5;
nrows = ceil(nharm^rowstocols);
ncols = ceil(nharm / nrows);
time_range = [200 650];
[tmp range_ind_min] = min(abs(timestamp - time_range(1)));
[tmp range_ind_max] = min(abs(timestamp - time_range(2)));
range_ind = range_ind_min:range_ind_max;
times_fine = linspace(timestamp(range_ind(1)),timestamp(range_ind(end)),501);
harm_eval = eval_fd(c_signal_pcastr.harmfd,times_fine);
for iplot = 1:nharm
subplot(nrows,ncols,iplot)
plot(times_fine,harm_eval(:,iplot))
xlabel(['Harmonic ' num2str(iplot)])
end
%% Plot: %variance explained vs. #basis functions
close all
thres_var = 0.9;
cumprobs = cumsum([0;c_signal_pcastr.varprop]);
[tmp thres_ind] = min(abs(cumprobs - thres_var));
if cumprobs(thres_ind)-thres_var < 0
thres_ind = thres_ind + 1;
end
plot(0:length(c_signal_pcastr.varprop),cumprobs)
hold on
plot(0:thres_ind-1,ones(1,thres_ind)*thres_var,'--')
plot([thres_ind-1 thres_ind-1],[0 cumprobs(thres_ind)],'--')
xlabel('fPCA basis functions')
ylabel('cumulative variance explained')
fprintf('To explain at least %s variance, use %i fPCA basis functions.\n\n',num2str(thres_var,3),thres_ind-1);
%% Fit additional data with basis from fPCA
close all
harm_basis = create_fd_basis(c_signal_pcastr.harmfd);
mean_fit = eval_fd(c_signal_pcastr.meanfd,timestamp(range_ind));
smoothed_additional = smooth_basis(timestamp(range_ind),c_signal(range_ind,ind_fit)-repmat(mean_fit,1,sum(ind_fit)),harm_basis);
f = figure;
set(f,'DefaultAxesColorOrder',jet(size(c_signal(1,ind_fit),2)))
hold on
plot(smoothed_additional+c_signal_pcastr.meanfd)
plot(timestamp(range_ind),c_signal(range_ind,ind_fit),'o')
%% Check if data is fitted by harmonics
close all
nperplot = 8;
nsubplots = ceil(size(c_signal(1,ind_fit),2)./nperplot);
rowstocols = 0.5;
time_range = [200 650];
[tmp range_ind_min] = min(abs(timestamp - time_range(1)));
[tmp range_ind_max] = min(abs(timestamp - time_range(2)));
range_ind = range_ind_min:range_ind_max;
times_fine = linspace(timestamp(range_ind(1)),timestamp(range_ind(end)),501);
harm_fine = eval_basis(harm_basis,times_fine);
nrows = ceil(nsubplots^rowstocols);
ncols = ceil(nsubplots / nrows);
fitcoef = getcoef(smoothed_additional);
data_fpca_repr = fitcoef'*harm_fine';
mean_fine = eval_fd(c_signal_pcastr.meanfd,times_fine);
ind_fit_no = find(ind_fit);
for iplot = 1:nsubplots
subplot(nrows,ncols,iplot)
inds = ((iplot-1)*nperplot+1):min([size(c_signal(1,ind_fit),2) iplot*nperplot]);
plot(times_fine,repmat(mean_fine,1,length(inds))+data_fpca_repr(inds,:)')
hold on
plot(timestamp(range_ind),c_signal(range_ind,ind_fit_no(inds)),'o')
end
%% Plot: Triagonal Matrix of PCs (the bold points are fitted)
close all
max_pc = 5;
figure
color = hsv(length(signals));
legendstyles = nan(1,length(signals));
hold on
unitypes = unique(celltype);
linewidth = 1;
markers = {'+','o','*','x','s','d','^','v','>','<','p','h','.'};
xpos = .07;
ypos = .04;
for irow = 1:max_pc-1
for icol = 1:irow
h = subplot(max_pc-1,max_pc-1,(irow-1)*(max_pc-1)+icol);
pos = get(h,'Pos');
set(h,'Pos',[pos(1)-xpos*(max_pc-1-icol)/max_pc pos(2)-ypos*irow/max_pc pos(3)*1.2 pos(4)*1.2])
hold on
if irow == max_pc-1;
xlabel(['PC ' num2str(icol)])
else
set(gca,'XTickLabel',[])
end
if icol == 1
ylabel(['PC ' num2str(irow+1)])
else
set(gca,'YTickLabel',[])
end
for ilig = 1:length(signals)
if sum(unitypes(ilig)==sites_for_harmonics)
% Data used for harmonics
legendstyles(ilig) = plot(c_signal_pcastr.harmscr(celltype(ind_harm) == unitypes(ilig),icol),c_signal_pcastr.harmscr(celltype(ind_harm) == unitypes(ilig),irow+1),'o','MarkerFaceColor',color(ilig,:),'LineWidth',linewidth);
else
% Data used for fitting
legendstyles(ilig) = plot(fitcoef(icol,celltype(ind_fit) == unitypes(ilig)),fitcoef(irow+1,celltype(ind_fit) == unitypes(ilig)),'s','MarkerEdgeColor',color(ilig,:),'LineWidth',linewidth);
end
end
xrange = max([c_signal_pcastr.harmscr(:,icol);fitcoef(icol,:)']) - min([c_signal_pcastr.harmscr(:,icol);fitcoef(icol,:)']);
yrange = max([c_signal_pcastr.harmscr(:,irow+1);fitcoef(irow+1,:)']) - min([c_signal_pcastr.harmscr(:,irow+1);fitcoef(irow+1,:)']);
scalefac = .05;
set(gca,'XLim',[min([c_signal_pcastr.harmscr(:,icol);fitcoef(icol,:)']) - xrange*scalefac max([c_signal_pcastr.harmscr(:,icol);fitcoef(icol,:)']) + xrange*scalefac])
set(gca,'YLim',[min([c_signal_pcastr.harmscr(:,irow+1);fitcoef(irow+1,:)']) - yrange*scalefac max([c_signal_pcastr.harmscr(:,irow+1);fitcoef(irow+1,:)']) + yrange*scalefac])
end
end
g = subplot(max_pc-1,max_pc-1,max_pc-1);
set(gca,'Visible','off')
legend(g,legendstyles,input_names)