Skip to content

Commit 8ca6a9b

Browse files
committed
Add structure decoupling script
1 parent 0ef2f41 commit 8ca6a9b

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

matlab/decouple_struct_motion.m

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
2+
%%%CHANGE THESE PARAMETERS
3+
pxsize = 0.0967821;
4+
DT = 0.0064;
5+
base_dir = '/mnt/data4/SPT_fidelity2024_data/SPT_fidelity2024_data_raw/recordings/TOMM20';
6+
traj_fname = 'tracks_rad=0.375_th=0.8_dist=0.3_gapdist=0.3_framegap=20';
7+
mask_fname = 'C2-210311_COS67_TOM20-Halo_Mito-mNeonG_untreated_3.czi.tif_avg17_crop_Simple Segmentation_bin_openCirc1px_compstrck_1_1015.tif';
8+
out_dir = '/tmp';
9+
10+
11+
%%%CODE STARTS HERE
12+
tiff_info = imfinfo(sprintf('%s/%s', base_dir, mask_fname));
13+
mask = zeros(size(tiff_info, 1), tiff_info(1).Height, tiff_info(1).Width);
14+
for i=1:size(tiff_info, 1)
15+
mask(i,:,:) = imread(sprintf('%s/%s', base_dir, mask_fname), i);
16+
end
17+
18+
tab_comps = [];
19+
hulls = cell(max(mask(:)));
20+
for i=1:size(mask,1)
21+
cur = squeeze(mask(i,:,:));
22+
for j=unique(cur(cur > 0))'
23+
if isempty(hulls{j})
24+
hulls{j} = {};
25+
end
26+
27+
idxs = find(cur == j);
28+
[ix, iy] = ind2sub(size(cur), idxs);
29+
pts = [ix iy] * pxsize;
30+
tab_comps = [tab_comps; j i mean(pts)];
31+
32+
hl = struct();
33+
hl.frame = i;
34+
hl.poly = pts(convhull(pts), :);
35+
hulls{j} = [hulls{j}; hl];
36+
end
37+
end
38+
tr_ids = unique(tab_comps(:,1));
39+
40+
41+
tab = csvread(sprintf('%s/%s', base_dir, traj_fname), 5, 1);
42+
tab = tab(:, [2 8 5 4 8]);
43+
tab(:,2) = tab(:,2) * DT;
44+
45+
tab2 = [];
46+
for i=unique(tab(:,1))'
47+
tr = tab(tab(:,1) == i, :);
48+
[no, idxs] = sort(tr(:,5));
49+
tab2 = [tab2; tr(idxs, :)];
50+
end
51+
tab = tab2;
52+
53+
54+
tab_structs = cell(length(tr_ids), 1);
55+
ii = 1;
56+
for i=tr_ids'
57+
cur_hulls = hulls{i};
58+
frames = cellfun(@(x) x.frame, cur_hulls);
59+
idxs = unique(tab(tab(:,5) >= min(frames) & tab(:,5) <= max(frames), 1));
60+
for j=idxs'
61+
tr = tab(tab(:,1) == j, :);
62+
for k=1:size(tr,1)
63+
hidx = find(cellfun(@(x) x.frame, cur_hulls) == tr(k, 5) + 1);
64+
if ~isempty(hidx) && inpolygon(tr(k,3), tr(k,4), cur_hulls{hidx}.poly(:,1), cur_hulls{hidx}.poly(:,2))
65+
tab_structs{ii} = [tab_structs{ii}; tr];
66+
break
67+
end
68+
end
69+
end
70+
ii = ii + 1;
71+
end
72+
73+
for u=1:length(tr_ids)
74+
selected_comp = tr_ids(u);
75+
76+
if isempty(tab_structs{u})
77+
continue
78+
end
79+
80+
for selected_tr=unique(tab_structs{u}(:,1))'
81+
tr = tab_structs{u}(tab_structs{u}(:,1) == selected_tr, :);
82+
83+
if size(tr, 1) < 5
84+
continue
85+
end
86+
87+
tr_str = tab_comps(tab_comps(:,1) == selected_comp, :);
88+
89+
if size(tr_str, 1) < 5
90+
continue
91+
end
92+
93+
tr_str(:, 2) = tr_str(:,2) * DT;
94+
tr_str = tr_str(tr_str(:,2) >= tr(1,2) & tr_str(:,2) < tr(end,2), :);
95+
tr = tr(tr(:,2) >= tr_str(1,2) & tr(:,2) <= tr_str(end,2), :);
96+
tr_str = tr_str(tr_str(:,2) >= tr(1,2) & tr_str(:,2) < tr(end,2), :);
97+
98+
idxs = [];
99+
j = 1;
100+
for i=1:size(tr,1)
101+
while j <= size(tr_str,1) && tr(i,2) ~= tr_str(j,2)
102+
j = j + 1;
103+
end
104+
if j < size(tr_str,1) && tr(i,2) == tr_str(j,2)
105+
idxs = [idxs; i j];
106+
end
107+
end
108+
109+
figure
110+
hold on
111+
plot(tr_str(:,3), tr_str(:,4), 'k')
112+
plot(tr(:,3), tr(:,4), 'b')
113+
hold off
114+
daspect([1 1 1])
115+
legend({'Structure', 'Particle'})
116+
pause(1)
117+
print(sprintf('%s/trajStruct_vs_traj_%d_%d.svg', out_dir, selected_comp, selected_tr), '-dsvg')
118+
119+
tr2 = [];
120+
for i=1:(size(tr,1)-2)
121+
tmp = tr(i,:);
122+
tmp(3:4) = tmp(3:4) - tr_str(idxs(i, 2), 3:4);
123+
tr2 = [tr2; tmp];
124+
end
125+
126+
figure
127+
hold on
128+
plot(tr(:,3) - tr_str(idxs(1,2), 3) , tr(:,4) - tr_str(idxs(1,2), 4), 'k')
129+
plot(tr2(:,3), tr2(:,4), 'r')
130+
hold off
131+
legend({'Raw', 'Decoupled'})
132+
daspect([1 1 1])
133+
pause(1)
134+
print(sprintf('%s/trajs_corr_%d_%d.svg', out_dir, selected_comp, selected_tr), '-dsvg')
135+
end
136+
end

0 commit comments

Comments
 (0)