Skip to content

Commit d53ea34

Browse files
Copilottorwager
andauthored
Fix deprecated avifile/addframe API and timeseries_extract_slice returning all zeros (#70)
* Initial plan * Fix deprecated avifile/addframe and all-zeros timeseries_extract_slice Co-authored-by: torwager <6262700+torwager@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: torwager <6262700+torwager@users.noreply.github.com> Co-authored-by: Tor Wager <torwager@gmail.com>
1 parent 48cf89b commit d53ea34

4 files changed

Lines changed: 50 additions & 34 deletions

File tree

CanlabCore/Data_extraction/timeseries_extract_slice.m

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@
66
% :Usage:
77
% ::
88
%
9-
% function sl = timeseries_extract_slice(V,sliceno)
9+
% function sl = timeseries_extract_slice(V, sliceno, orientation)
10+
%
11+
% :Inputs:
12+
%
13+
% **V:**
14+
% image filenames (char) or spm_vol memory-mapped volumes
15+
%
16+
% **sliceno:**
17+
% slice number (voxel index) to extract
18+
%
19+
% **orientation:**
20+
% 'axial' (default), 'sagittal', or 'coronal'
21+
%
22+
% :Output:
23+
%
24+
% **sl:**
25+
% X x Y x time matrix of slice data
1026
%
1127

12-
global defaults
13-
14-
% defaults
15-
switch spm('Ver')
16-
case 'SPM2'
17-
% SPM2: spm_defaults is a script, not callable here
18-
disp('WARNING: spm defaults not set for spm2. Make sure your defaults are set correctly');
19-
otherwise
20-
% SPM5+, including any future versions
21-
spm_defaults()
22-
end
23-
24-
2528
if ischar(V)
2629
V = spm_vol(V);
2730
end
@@ -32,16 +35,29 @@
3235

3336
switch(orientation)
3437
case 'axial'
35-
get_slice = @get_ax_slice;
38+
% Map output pixel (x,y) -> voxel (x, y, sliceno)
39+
mat = spm_matrix([0 0 sliceno]);
40+
for i = 1:length(V)
41+
sl(:,:,i) = spm_slice_vol(V(i), mat, V(i).dim(1:2), 0);
42+
end
43+
3644
case 'sagittal'
37-
get_slice = @get_sag_slice;
45+
% Map output pixel (x,y) -> voxel (sliceno, x, y)
46+
% Matrix maps: x_vox=sliceno (const), y_vox=x_pix, z_vox=y_pix
47+
mat = [0 0 0 sliceno; 1 0 0 0; 0 1 0 0; 0 0 0 1];
48+
for i = 1:length(V)
49+
sl(:,:,i) = spm_slice_vol(V(i), mat, V(i).dim(2:3), 0);
50+
end
51+
3852
case 'coronal'
39-
get_slice = @get_cor_slice;
53+
% Map output pixel (x,y) -> voxel (x, sliceno, y)
54+
% Matrix maps: x_vox=x_pix, y_vox=sliceno (const), z_vox=y_pix
55+
mat = [1 0 0 0; 0 0 0 sliceno; 0 1 0 0; 0 0 0 1];
56+
for i = 1:length(V)
57+
sl(:,:,i) = spm_slice_vol(V(i), mat, [V(i).dim(1) V(i).dim(3)], 0);
58+
end
59+
4060
otherwise
4161
error('Unknown orientation: %s\n', orientation);
4262
end
43-
44-
for i = 1:length(V)
45-
sl(:,:,i) = get_slice(V(i), sliceno);
46-
end
4763
end

CanlabCore/Visualization_functions/Support/movie_tools.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,10 @@
393393
switch(FRAMESTYLE)
394394
case 'avi' % write to avi format directly
395395
if isempty(mov)
396-
mov = avifile('mymovie.avi','Quality',75,'Compression','None','Fps',fps);
396+
mov = VideoWriter('mymovie.avi');
397+
mov.Quality = 75;
398+
mov.FrameRate = fps;
399+
open(mov);
397400
end
398401

399402
case 'matlab' % matlab movie format
@@ -425,7 +428,8 @@
425428
drawnow
426429

427430
try
428-
mov = addframe(mov,H);
431+
frame = getframe(H);
432+
writeVideo(mov, frame);
429433
catch
430434
disp('Cannot write frame. Failed to set stream format??')
431435
mov = close(mov);

CanlabCore/Visualization_functions/movie_of_slice_timeseries.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ function movie_of_slice_timeseries(imgs, slicenumber, moviename, orientation)
5656
title(sprintf('Image %3.0f: %s', i, ff), 'FontSize', 24)
5757

5858
% mov = add_a_frame(mov, H);
59-
writeVideo(mov, H);
59+
frame = getframe(H);
60+
writeVideo(mov, frame);
6061
end
6162

6263
tmp = close(mov); %#ok;
@@ -77,7 +78,7 @@ function movie_of_slice_timeseries(imgs, slicenumber, moviename, orientation)
7778

7879
% frame_rate = 1/tr;
7980
mov = VideoWriter(moviename);
80-
mov.Quality
81+
mov.Quality = 75;
8182
mov.FrameRate = fps;
8283
open(mov);
8384

@@ -96,7 +97,8 @@ function movie_of_slice_timeseries(imgs, slicenumber, moviename, orientation)
9697

9798
lightRestoreSingle(H);
9899
try
99-
mov = addframe(mov, H);
100+
frame = getframe(H);
101+
writeVideo(mov, frame);
100102
catch
101103
disp('Cannot write frame. Failed to set stream format??')
102104
mov = close(mov);

CanlabCore/hewma_utility/get_ax_slice.m

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,10 @@
3939

4040
% Transverse slice
4141
%==========================================================
42-
C = [1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1];
43-
DIM = V(1).dim(1:2);
42+
mat = spm_matrix([0 0 slice_num]);
4443

45-
C(3,4) = slice_num;
46-
%C(3,4)=-p;
47-
48-
% img = rot90(spm_slice_vol(V,C,DIM,0));
49-
% img = spm_slice_vol(V,inv(C),DIM,0);
5044
for i=1:length(V)
51-
slice_data(:,:,i) = spm_slice_vol(V(i), C, DIM, 0);
45+
slice_data(:,:,i) = spm_slice_vol(V(i), mat, V(i).dim(1:2), 0);
5246
end
5347

5448
slice_data = squeeze(slice_data);

0 commit comments

Comments
 (0)