-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst_steps_v2.m
executable file
·239 lines (172 loc) · 6.2 KB
/
first_steps_v2.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
%% First steps working with live cell imaging data and functional data analysis:
% Warning: The workspace will be cleared!
% All test code for finding bug in FDA package removed
close all
clear all
clc
remotepath = mypath();
fdaMPath = [remotepath 'fda'];
addpath(fdaMPath)
grabdataPath = [remotepath 'Code + Stage and Outputsignal'];
addpath(grabdataPath)
% Possible stages/sites:
sites = [2, 4, 7, 8, 11, 13, 14, 17, 19, 22, 24, 28, 33, 34, 37, ...
39, 40, 41, 42, 44, 47, 48, 53, 54, 57, 59, 60, 64, 67, 68];
% Stage 17 <--> IGF 100 (from Stage_Treatment_Outputsignal.xlsx)
% site = 17;
% site = 11; % Unstimulated
site = 13; % [13 14 17 53 54 57 64 67 68];
if exist(remotepath,'dir')
[timestamp,intensity] = grabdata(site);
else
load(['./Workspaces/site_' num2str(site)])
end
log_trafo = 1; % log-transform c_signal
if log_trafo
c_signal = log10(intensity);
end
range_c_signal = [min(min(c_signal)) max(max(c_signal))];
return
%% Reproduce plots from grabdata
close all
plot(timestamp,c_signal,'g','color',[0.7 0.7 0.7])
hold on
plot(timestamp,nanmean(c_signal,2),'color','k','LineWidth',2)
set(gca,'XLim',[50 650]) % see Pat's Poster
%% Plot every data set with distinct color
close all
plot_sites = site;
% plot_sites = sites;
first_n = 20; % Plot only first_n data-sets
for ip = 1:length(plot_sites)
if length(plot_sites) > 1
if exist(remotepath,'dir')
[timestamp,intensity] = grabdata(plot_sites(ip));
else
load(['./Workspaces/site_' num2str(plot_sites(ip))])
end
c_signal = log10(intensity);
end
first_n = min(first_n,size(c_signal,2));
f = figure;
set(f,'DefaultAxesColorOrder',jet(first_n))
plot(timestamp,c_signal(:,1:first_n))
title(['Site ' num2str(plot_sites(ip))])
set(gca,'XLim',[50 650])
hold on
plot(timestamp,nanmean(c_signal,2),'color','k','LineWidth',2)
if length(plot_sites) > 1
waitforbuttonpress;
close gcf
end
end
%% Generate spline fits to individual data sets with nbasis basis functions
close all
nbasis = 50;
% time_range = [min(timestamp) max(timestamp)];
time_range = [50 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;
basis = create_bspline_basis([timestamp(range_ind(1)) timestamp(range_ind(end))], nbasis);
smoothed_data = smooth_basis(timestamp(range_ind),c_signal(range_ind,:),basis);
f = figure;
set(f,'DefaultAxesColorOrder',jet(size(c_signal,2)))
hold on
plot(smoothed_data)
plot(timestamp(range_ind),c_signal(range_ind,:),'o')
%% Check smoothed_data: Reading coefs works, reconstruction of data spline works!
close all
time_range = [50 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);
tmpcoef = getcoef(smoothed_data);
basis_evaluated = eval_basis(basis,times_fine);
figure
plot(times_fine,basis_evaluated*tmpcoef)
hold on
plot(timestamp(range_ind),c_signal(range_ind,:),'o')
%% Make FPCA with data generated in previous block - wip
close all
% nharm = 6;
nharm = 8;
c_signal_pcastr = pca_fd(smoothed_data, nharm);
% disp(c_signal_pcastr.values(1:4))
plot_pca_fd(c_signal_pcastr, 1, 0)
% c_signal_rotpcastr = varmx_pca(c_signal_pcastr);
% plot_pca_fd(c_signal_rotpcastr, 1, 0)
%% 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));
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 thres_var],'--')
xlabel('fPCA basis functions')
ylabel('cumulative variance explained')
fprintf('To explain %s variance, use %i fPCA basis functions.\n\n',num2str(thres_var,3),thres_ind-1);
%% Alternative: Use pareto function of Matlab
close all
thres_var = 0.9;
percent_explained = 100*c_signal_pcastr.varprop;
pareto(percent_explained)
% hold on
% plot(0:thres_ind-1,ones(1,thres_ind)*100*thres_var,'--')
% plot([thres_ind-1 thres_ind-1],[0 100*thres_var],'--')
xlabel('Principal Component')
ylabel('Variance Explained (%)')
%% Plot: Harmonic scores PC1 vs. PC2
close all
plot(c_signal_pcastr.harmscr(:,1),c_signal_pcastr.harmscr(:,2),'.')
%% Plot harmonics from harmfd object
close all
nperplot = 4;
nsubplots = ceil(size(c_signal,2)./nperplot);
rowstocols = 0.5;
time_range = [50 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_fd(c_signal_pcastr.harmfd,times_fine);
nrows = ceil(nsubplots^rowstocols);
ncols = ceil(nsubplots / nrows);
data_fpca_repr = c_signal_pcastr.harmscr*harm_fine';
mean_fine = eval_fd(c_signal_pcastr.meanfd,times_fine);
for iplot = 1:nsubplots
subplot(nrows,ncols,iplot)
inds = ((iplot-1)*nperplot+1):min([size(c_signal,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,inds),'o')
end
%% Generate smoothing spline fits using as many basis functions as data points
close all
lambda = 1e4; % smoothing parameter
nbasis = length(timestamp);
basis_full = create_bspline_basis(timestamp(end), nbasis);
fdparobj = fdPar(basis_full,2,lambda);
spline_data = smooth_basis(timestamp,c_signal,fdparobj);
f = figure;
set(f,'DefaultAxesColorOrder',jet(size(c_signal,2)))
hold on
plot(spline_data)
plot(timestamp,c_signal,'o')
%% Make FPCA with data generated in previous block - wip
close all
nharm = 2;
c_signal_pcaspline = pca_fd(spline_data, nharm);
disp(c_signal_pcaspline.values(1:4))
plot_pca_fd(c_signal_pcaspline, 1, 0)
c_signal_rotpcaspline = varmx_pca(c_signal_pcaspline);
plot_pca_fd(c_signal_rotpcaspline, 1, 0)
%% Plot data with mean subtracted
close all
f = figure;
set(f,'DefaultAxesColorOrder',hsv(first_n))
plot(timestamp,c_signal-repmat(nanmean(c_signal,2),1,size(c_signal,2)))
set(gca,'XLim',[50 650])