-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_data.m
More file actions
425 lines (350 loc) · 14.1 KB
/
prepare_data.m
File metadata and controls
425 lines (350 loc) · 14.1 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
function expData = prepare_data(material_id, opts)
% PREPARE_DATA Load and preprocess experimental IMR data
%
% This function loads dimensional R(t) data, aligns trials at peak radius,
% nondimensionalizes, computes velocities and strain quantities, and applies
% high-information gating masks following the paper (Eq. 4-6).
%
% It also computes baseline noise scales and weights for the Bayesian IMR
% likelihood:
% sigma0_R, sigma0_Rdot, weights_w
%
% Syntax:
% expData = prepare_data(material_id)
% expData = prepare_data(material_id, opts)
%
% Inputs:
% material_id - Integer selecting material dataset:
% 1: UM1 (10% Gelatin)
% 2: UM2 (0.2% Fibrin)
% 3: UM3 (8%/0.26% PAAm)
% 4: UT1 (10% Gelatin)
% 5: UT2 (5% Agarose)
% 6-9: Other materials
%
% opts - Optional settings structure:
% .dataDir - Path to experiments/ folder (default: './experiments')
% .rho - Density [kg/m³] (default: 1064)
% .p_inf - Far-field pressure [Pa] (default: 101325)
% .epsH_rel_frac - Strain threshold fraction (default: 0.10)
% .epsdot_base - Strain rate threshold base (default: 1e5)
% .useEllipticalGate - Use elliptical gate (Eq. 6) (default: true)
% .verbose - Display progress (default: true)
%
% Outputs:
% expData - Structure containing:
% .Rmatrix [nTime x nTrials] - Nondimensional radius R*
% .Rdotmatrix [nTime x nTrials] - Nondimensional velocity dR*/dτ
% .tmatrix [nTime x nTrials] - Nondimensional time τ
% .strain [nTime x nTrials] - Hencky strain ε* (Eq. 4)
% .strainRate [nTime x nTrials] - Strain rate ε̇* (Eq. 4)
% .mask [nTime x nTrials] - High-information gate (Eq. 6)
% .tc [1 x nTrials] - Characteristic time per trial
% .Rmax_range [1 x nTrials] - Peak radius per trial
% .Req_each [1 x nTrials] - Equilibrium radius per trial
% .material_name - String describing material
% .rho, .p_inf - Physical parameters used for nondimensionalization
%
% Additional fields for Bayesian likelihood:
% .sigma0_R [nTime x nTrials] - Baseline std for R*
% .sigma0_Rdot [nTime x nTrials] - Baseline std for dR*/dτ
% .weights_w [nTime x nTrials] - Per-point weights (1 in gate, 0 out)
%
% Example:
% expData = prepare_data(1); % Load UM1 (10% Gelatin)
% fprintf('Loaded %d trials with %d time steps\n', ...
% size(expData.Rmatrix, 2), size(expData.Rmatrix, 1));
%
% See also: build_priors, train_gpr_surrogate, forward_solver_wrapper
%% Parse inputs
if nargin < 2, opts = struct(); end
opts = parse_options(opts);
if opts.verbose
fprintf('\n=== IMR Data Preparation ===\n');
fprintf('Material ID: %d\n', material_id);
end
%% Load dimensional data: R_d [nTime x nTrials], t_d [nTime x nTrials]
[R_d, t_d, material_name] = load_dimensional_data(material_id, opts);
if opts.verbose
fprintf('Material: %s\n', material_name);
fprintf('Loaded: %d trials, %d time points\n', size(R_d,2), size(R_d,1));
end
%% Align at peak radius and truncate
[R_d, t_d] = align_at_peak(R_d, t_d, opts);
%% Nondimensionalize
Rmax_range = max(R_d, [], 1); % [1 x nTrials]
tc = Rmax_range .* sqrt(opts.rho / opts.p_inf); % [1 x nTrials]
Rmatrix = R_d ./ Rmax_range; % R* ∈ [0,1]
tmatrix = t_d ./ tc; % τ (dimensionless)
if opts.verbose
fprintf('Nondimensionalized: R* ∈ [%.3f, %.3f], τ ∈ [%.3f, %.3f]\n', ...
min(Rmatrix(:)), max(Rmatrix(:)), ...
min(tmatrix(:)), max(tmatrix(:)));
end
%% Compute velocities (use dataprep if available, else gradient)
Rdotmatrix = compute_velocities(Rmatrix, tmatrix, tc, R_d, Rmax_range);
%% Compute equilibrium radius per trial (median of last 10% of dimensional R_d, BIMR-style)
[nTime_d, J] = size(R_d);
tailLo = max(1, floor(0.9 * nTime_d)); % last 10% of samples
Req_each = nan(1, J); % dimensional Req per trial
for j = 1:J
rj = R_d(:, j);
rj = rj(isfinite(rj)); % drop NaNs/Infs
if isempty(rj)
% fallback: equilibrium = Rmax for that trial
Req_each(j) = Rmax_range(j);
continue;
end
% tail segment
tail = rj(tailLo : min(numel(rj), nTime_d));
if isempty(tail)
tail = rj;
end
req = median(tail, 'omitnan');
if ~isfinite(req) || req <= 0
req = median(rj, 'omitnan');
end
if ~isfinite(req) || req <= 0
req = Rmax_range(j); % final safety fallback
end
Req_each(j) = req;
end
%% Compute strain and strain rate (Paper Eq. 4)
[strain, strainRate] = compute_strain_quantities(R_d, Rdotmatrix, Rmatrix, ...
Req_each, tc, opts);
%% Apply high-information gate (Paper Eq. 5-6)
mask = compute_high_info_mask(strain, strainRate, tc, opts);
if opts.verbose
fprintf('High-info gate: %.1f%% of points retained\n', ...
100*nnz(mask)/numel(mask));
end
%% ========== Likelihood baseline fields (for Bayesian IMR) ==========
[nTime, nTrials] = size(Rmatrix);
% BIMR-style baseline std across trials
sigmaR_time = max(std(Rmatrix, 0, 2, 'omitnan'), 1e-12); % [nTime x 1]
sigmaRdot_time = max(std(Rdotmatrix,0, 2, 'omitnan'), 1e-12); % [nTime x 1]
sigma0_R = repmat(sigmaR_time, 1, nTrials); % [nTime x nTrials]
sigma0_Rdot = repmat(sigmaRdot_time, 1, nTrials); % [nTime x nTrials]
% Heteroscedastic weights (based on strain rate)
% Parameters for logistic weighting
kappa = opts.kappa; % Logistic steepness
m_floor = opts.m_floor; % Minimum weight
% Strain-rate threshold per trial
tc_row = repmat(tc, nTime, 1);
epsdot_th = opts.epsdot_base * tc_row;
% Logistic activation based on distance from threshold
z = (epsdot_th - abs(strainRate)) ./ max(epsdot_th, eps);
a = 1 ./ (1 + exp(-kappa .* z));
% Map activation to weight range [m_floor, 1] and clamp
weights_w = m_floor + (1 - m_floor) .* a;
weights_w = min(max(weights_w, m_floor), 1);
% Apply high-information gate: zero weight outside mask
weights_w(~mask) = 0;
weights_w(mask) = max(weights_w(mask), m_floor);
%% Package output
expData = struct();
expData.Rmatrix = Rmatrix;
expData.Rdotmatrix = Rdotmatrix;
expData.tmatrix = tmatrix;
expData.strain = strain;
expData.strainRate = strainRate;
expData.mask = mask;
expData.tc = tc;
expData.Rmax_range = Rmax_range;
expData.Req_each = Req_each;
expData.material_id = material_id;
expData.material_name = material_name;
expData.rho = opts.rho;
expData.p_inf = opts.p_inf;
% Average quantities for forward model
expData.tc_mean = mean(tc, 'omitnan');
expData.Rmax_mean = mean(Rmax_range,'omitnan');
% Likelihood-related fields
expData.sigma0_R = sigma0_R;
expData.sigma0_Rdot = sigma0_Rdot;
expData.weights_w = weights_w;
if opts.verbose
fprintf('=== Data preparation complete ===\n\n');
end
end
%% ==================== Helper Functions ====================
function opts = parse_options(opts)
% Set default options
if ~isfield(opts, 'dataDir'), opts.dataDir = './experiments'; end
if ~isfield(opts, 'rho'), opts.rho = 1064; end
if ~isfield(opts, 'p_inf'), opts.p_inf = 101325; end
if ~isfield(opts, 'epsH_rel_frac'), opts.epsH_rel_frac = 0.10; end
if ~isfield(opts, 'epsdot_base'), opts.epsdot_base = 1e5; end
if ~isfield(opts, 'useEllipticalGate'), opts.useEllipticalGate = true; end
if ~isfield(opts, 'verbose'), opts.verbose = true; end
if ~isfield(opts, 'epsH_abs_floor'), opts.epsH_abs_floor = 0.00; end
if ~isfield(opts, 'kappa'), opts.kappa = 1; end
if ~isfield(opts, 'm_floor'), opts.m_floor = 0.10; end
end
function [R_d, t_d, material_name] = load_dimensional_data(material_id, opts)
% Load dimensional R(t) data for specified material
% Returns: R_d [nTime x nTrials], t_d [nTime x nTrials], material_name (string)
switch material_id
case 0 % Synthetic data
load(fullfile(opts.dataDir,'synthetic_data.mat'));
lambda_vs_Rmax_fig; close all;
Rmax_range = linspace(Rmax-.1*Rmax_std,Rmax+.1*Rmax_std,32);
Req_range = Rmax_range./lambda;
for i = 1:length(synthetic_data)
R_d(:,i) = synthetic_data{i}(:,2).*Rmax_range(i);
t_d(:,i) = linspace(0,150e-6,256);
end
material_name = 'synthetic data';
case 1 % UM1: 10% Gelatin
load(fullfile(opts.dataDir, 'Processed_Data.mat'), 'expts');
k = 1;
for i = 7:15
R_d(:,k) = expts(i).Roft;
t_d(:,k) = expts(i).t;
k = k + 1;
end
material_name = 'UM1 (10% Gelatin)';
case 2 % UM2: 0.2% Fibrin
load(fullfile(opts.dataDir, 'Processed_Data.mat'), 'expts');
k = 1;
for i = 78:98
R_d(:,k) = expts(i).Roft;
t_d(:,k) = expts(i).t;
k = k + 1;
end
material_name = 'UM2 (0.2% Fibrin)';
case 3 % UM3: 8%/0.26% PAAm
load(fullfile(opts.dataDir, 'Processed_Data.mat'), 'expts');
k = 1;
for i = 223:234
R_d(:,k) = expts(i).Roft;
t_d(:,k) = expts(i).t;
k = k + 1;
end
material_name = 'UM3 (8%/0.26% PAAm)';
case 4 % UT1: 10% Gelatin
data = load(fullfile(opts.dataDir, '10percent', 'Rt_nondim_exp.mat'));
[R_d, t_d] = convert_from_nondim(data, [1,2,3,4], opts);
material_name = 'UT1 (10% Gelatin)';
case 5 % UT2: 5% Agarose
data = load(fullfile(opts.dataDir, 'Ag5.0', 'Rt_nondim_exp.mat'));
indices = [1:5, 7:10];
[R_d, t_d] = convert_from_nondim(data, indices, opts);
material_name = 'UT2 (5% Agarose)';
case 6 % Ag2.5
data = load(fullfile(opts.dataDir, 'Ag2.5', 'Rt_nondim_exp.mat'));
indices = [1:6, 8:11];
[R_d, t_d] = convert_from_nondim(data, indices, opts);
material_name = 'Ag2.5';
case 7 % Ag0.5
data = load(fullfile(opts.dataDir, 'Ag0.5', 'Rt_nondim_exp.mat'));
indices = 2:11;
[R_d, t_d] = convert_from_nondim(data, indices, opts);
material_name = 'Ag0.5';
case 8 % PA1.7
data = load(fullfile(opts.dataDir, 'PA1pt7', 'Rt_nondim_exp.mat'));
indices = 1:10;
[R_d, t_d] = convert_from_nondim(data, indices, opts);
material_name = 'PA1.7';
case 9 % PEGDA
data = load(fullfile(opts.dataDir, 'forbenchmarks', ...
'Rt_nondim_exp_PEGDA_S25L6.mat'));
indices = 1:7;
[R_d, t_d] = convert_from_nondim(data, indices, opts);
material_name = 'PEGDA S25L6';
otherwise
error('Unknown material_id: %d. Valid range: 1-9', material_id);
end
end
function [R_d, t_d] = convert_from_nondim(data, indices, opts)
% Convert nondimensional data back to dimensional
R_nondim_All = data.R_nondim_All;
t_nondim_All = data.t_nondim_All;
RmaxList = data.RmaxList;
if isfield(data, 'P_inf')
P_inf = data.P_inf;
else
P_inf = opts.p_inf;
end
minLen = min(cellfun(@(x) length(x), R_nondim_All(indices)));
k = 1;
for i = indices
tc_i = RmaxList(i) * sqrt(opts.rho / P_inf);
R_d(:,k) = R_nondim_All{i}(1:minLen) * RmaxList(i);
t_d(:,k) = t_nondim_All{i}(1:minLen) * tc_i;
k = k + 1;
end
end
function [R_aligned, t_aligned] = align_at_peak(R_d, t_d, opts) %#ok<INUSD>
% Align all trials at their peak radius
[~, startIdx] = max(R_d, [], 1);
nCols = size(R_d, 2);
tailLen = size(R_d, 1) - startIdx + 1;
minLen = min(tailLen);
R_aligned = zeros(minLen, nCols);
t_aligned = zeros(minLen, nCols);
for j = 1:nCols
idx = startIdx(j):(startIdx(j) + minLen - 1);
R_aligned(:,j) = R_d(idx, j);
t_aligned(:,j) = t_d(idx, j);
% Reset time to start at zero
t_aligned(:,j) = t_aligned(:,j) - min(t_aligned(:,j));
end
end
function Rdotmatrix = compute_velocities(Rmatrix, tmatrix, tc, R_d, Rmax_range)
% Compute bubble wall velocities (try dataprep first, else gradient)
if exist('dataprep', 'file') == 2
try
[~,~,~,~,~,~,Rdotmatrix,~,~,~] = ...
dataprep(tmatrix, Rmatrix, tc, R_d, Rmax_range);
return;
catch
% Fall through to gradient
end
end
% Fallback: simple gradient in nondimensional coordinates
Rdotmatrix = zeros(size(Rmatrix));
for j = 1:size(Rmatrix, 2)
Rdotmatrix(:,j) = gradient(Rmatrix(:,j), tmatrix(:,j));
end
end
function [strain, strainRate] = compute_strain_quantities(R_d, Rdotmatrix, ...
Rmatrix, Req_each, tc, opts) %#ok<INUSD>
% Compute Hencky strain and strain rate (Paper Eq. 4)
[nTime, nTrials] = size(Rmatrix);
% Equilibrium-referenced log strain: ε* = 0.5 * log((R/Req)^(-4))
strain = zeros(nTime, nTrials);
for j = 1:nTrials
denom = max(Req_each(j), 1e-12);
strain(:,j) = 0.5 * log(max((R_d(:,j) ./ denom).^(-4), 1e-12));
end
% Strain rate: ε̇* = -2 (Ṙ/R) * tc (Paper Eq. 4)
tc_row = repmat(tc, nTime, 1);
strainRate = -2 .* (Rdotmatrix ./ max(Rmatrix, 1e-12)) .* tc_row;
% Clean infinities / NaNs
strain(~isfinite(strain)) = NaN;
strainRate(~isfinite(strainRate)) = NaN;
end
function mask = compute_high_info_mask(strain, strainRate, tc, opts)
% Apply high-information gate (Paper Eq. 5-6)
[nTime, nTrials] = size(strain);
% Thresholds per trial (Paper Eq. 5)
epsH_max_per_trial = max(abs(strain), [], 1, 'omitnan');
epsH_th_trial = max(opts.epsH_abs_floor, ...
opts.epsH_rel_frac .* epsH_max_per_trial);
epsH_th_mat = repmat(epsH_th_trial, nTime, 1);
epsdot_th_trial = opts.epsdot_base * tc;
epsdot_th_mat = repmat(epsdot_th_trial, nTime, 1);
% Base mask: finite values
mask = isfinite(strain) & isfinite(strainRate);
% Elliptical gate (Paper Eq. 6)
if opts.useEllipticalGate
inHigh = ((abs(strain) ./ max(epsH_th_mat, eps)).^2 + ...
(abs(strainRate)./ max(epsdot_th_mat, eps)).^2) >= 1;
else
% Rectangular gate
inHigh = (abs(strain) >= epsH_th_mat) | ...
(abs(strainRate) >= epsdot_th_mat);
end
mask = mask & inHigh;
end