Skip to content

Commit

Permalink
nothing wrong with reading
Browse files Browse the repository at this point in the history
  • Loading branch information
bendhouseart committed Apr 9, 2024
1 parent bee1571 commit 24cb01c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
26 changes: 13 additions & 13 deletions ecat_testing/ints/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ of these steps the outputs are saved in the `steps/` directory with the filestem
make them) as described. These temporary files are written if and only if the environment variable `ECAT_SAVE_STEPS=1`
is set.

| Step | Description | Matlab | Python | FileStem |
|------|---------------------------------|---------------|----------------|------------------------------|
| 1 | Read main header | `read_ecat.m` | `read_ecat.py` | `1_read_mh_ecat*` |
| 2 | Read subheaders | `read_ecat.m` | `read_ecat.py` | `2_read_sh_ecat*` |
| 3 | Determine file/data type | `read_ecat.m` | `read_ecat.py` | `3_determine_data_type*` |
| 4 | Read image data | `read_ecat.m` | `read_ecat.py` | `4_read_img_ecat*` |
| 5 | scale if calibrated | `read_ecat.m` | `read_ecat.py` | `5_scale_img_ecat*` |
| 6 | Pass Data to ecat2nii | `ecat2nii.m` | `ecat2nii.py` | `6_ecat2nii*` |
| 7 | Flip ECAT data into Nifti space | `ecat2nii.m` | `ecat2nii.py` | `7_flip_ecat2nii*` |
| 8 | Rescale to 16 bits | `ecat2nii.m` | `ecat2nii.py` | `8_rescale_to_16_ecat2nii*` |
| 9 | Calibration Units Scaling | `ecat2nii.m` | `ecat2nii.py` | `9_scal_cal_units_ecat2nii*` |
| 10 | Save to Nifti | `ecat2nii.m` | `ecat2nii.py` | `10_save_nii_ecat2nii*` |
| 11 | Additional Steps | TBD | TBD | TBD |
| Step | Description | Matlab | Python | FileStem |
|------|---------------------------------|----------------|----------------|------------------------------|
| 1 | Read main header | `read_ECAT7.m` | `read_ecat.py` | `1_read_mh_ecat*` |
| 2 | Read subheaders | `read_EACT7.m` | `read_ecat.py` | `2_read_sh_ecat*` |
| 3 | Determine file/data type | `read_ECAT7.m` | `read_ecat.py` | `3_determine_data_type*` |
| 4 | Read image data | `read_ECAT7.m` | `read_ecat.py` | `4_read_img_ecat*` |
| 5 | scale if calibrated | `read_ECAT7.m` | `read_ecat.py` | `5_scale_img_ecat*` |
| 6 | Pass Data to ecat2nii | `ecat2nii.m` | `ecat2nii.py` | `6_ecat2nii*` |
| 7 | Flip ECAT data into Nifti space | `ecat2nii.m` | `ecat2nii.py` | `7_flip_ecat2nii*` |
| 8 | Rescale to 16 bits | `ecat2nii.m` | `ecat2nii.py` | `8_rescale_to_16_ecat2nii*` |
| 9 | Calibration Units Scaling | `ecat2nii.m` | `ecat2nii.py` | `9_scal_cal_units_ecat2nii*` |
| 10 | Save to Nifti | `ecat2nii.m` | `ecat2nii.py` | `10_save_nii_ecat2nii*` |
| 11 | Additional Steps | TBD | TBD | TBD |


1. Read main header: this is the first step in reading the ecat file, the main header contains information about the
Expand Down
67 changes: 66 additions & 1 deletion matlab/readECAT7.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
fs = [];
end

%ecat_save_steps = '1'

if length(fs) == 0
origpwd = pwd;
if length(lastpath_) > 0; cd(lastpath_); end
Expand All @@ -77,7 +79,8 @@
end

% open file as ieee big-endian
fid = fopen(fs,'r','ieee-be');
file_endianess = 'ieee-be';
fid = fopen(fs,'r', file_endianess);
if (fid < 0)
error('readECAT: Problem opening file %s', fs);
end
Expand All @@ -86,6 +89,23 @@
% Read in the main header info
mh = readMainheader(fid);

ecat_save_steps = getenv('ECAT_SAVE_STEPS');
ecat_save_steps_dir = mfilename('fullpath');
if contains(ecat_save_steps_dir, 'LiveEditorEvaluationHelper')
ecat_save_steps_dir = matlab.desktop.editor.getActiveFilename;
end
parts = strsplit(ecat_save_steps_dir, filesep);
ecat_save_steps_dir = strjoin([parts(1:end-2), 'ecat_testing', 'steps'], filesep);

% save main header output to json
if (ecat_save_steps == '1')
% save main header
main_header_json = (jsonencode(mh, PrettyPrint=true));
step_1_file = fopen([ecat_save_steps_dir filesep '1_read_mh_ecat_matlab.json'], 'w');
fprintf(step_1_file, '%s', main_header_json);
fclose(step_1_file);
end

if (nargout == 1)
return
end
Expand Down Expand Up @@ -129,6 +149,7 @@

sh = cell(nmat,1);
data = cell(nmat,1);
pixel_data_type = '';

% select proper sh routine
switch mh.file_type
Expand Down Expand Up @@ -173,15 +194,19 @@
switch sh{m}.data_type
case 5 % IEEE float (32 bit)
data{m} = fread(fid, [sz(1)*sz(2) sz(3)],'float32');
pixel_data_type = 'float32';
case 6 % SUN int16
if (matlabVersion(1) < 6)
warning('old matlab version, using int16 to read')
data{m} = int16(fread(fid, [sz(1)*sz(2) sz(3)],'int16'));
pixel_data_type = 'int16';
else
data{m} = fread(fid, [sz(1)*sz(2) sz(3)],'int16=>int16');
pixel_data_type = 'int16=>int16';
end
otherwise
warning('readECAT7: unrecognized data type');
pixel_data_type = 'unrecognized data type';
end
% other sh{m}.data_type: 2 = VAX int16, 3 = VAX int32, 4 = VAX F-float (32 bit), 7 = SUN int32

Expand All @@ -196,6 +221,46 @@
end % loop over matrices

fclose(fid);
if (ecat_save_steps == '1')
% save subheaders header
sub_header_json = (jsonencode(sh, PrettyPrint=true));
step_2_file = fopen([ecat_save_steps_dir filesep '1_read_sh_ecat_matlab.json'], 'w');
fprintf(step_2_file, '%s', sub_header_json);
fclose(step_2_file);

% write out endianess and datatype of the pixel data matrix
step_3_struct = {};
x.data_type = class(data{1});
x.endianess = file_endianess;
step_3_json = (jsonencode(x, PrettyPrint=true));
step_3_file = fopen([ecat_save_steps_dir filesep '3_determine_data_type_matlab.json'], 'w');
fprintf(step_3_file, '%s', step_3_json);
fclose(step_3_file);

% save pixel data (if it exists) but collect only the first, middle, and last frames
% collect the first, middle, and last frames
frames = [1, floor(size(data{1}, 3)/2), size(data{1}, 3)];
disp("these are the frames");
disp(frames);
% collect only a single 2D slice from each of the frames collected in frames
frames_to_record = {};
for i = 1:length(frames)
frame_number = frames(i);
frame_to_slice = data{frame_number};
size_of_frame_to_slice = size(frame_to_slice);
middle_of_frame = int16(size_of_frame_to_slice(3)/2);
slice = data{frame_number}(:,:,middle_of_frame);
frames_to_record{i} = slice;
end

for i = 1:length(frames_to_record)
frame = frames_to_record{i};
writematrix(frame, [ecat_save_steps_dir filesep '4_read_img_ecat_matlab_' num2str(i - 1) '.tsv'], 'Delimiter', 'tab', 'FileType','text');
end

% scale if calibrated

end

return

Expand Down
7 changes: 4 additions & 3 deletions pypet2bids/pypet2bids/read_ecat.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def read_ecat(ecat_file: str, calibrated: bool = False, collect_pixel_data: bool
with open(steps_dir / f'3_determine_data_type_python.json', 'w') as outfile:
json.dump(step_3_dict, outfile)

# collect only the first, middle, and last frames from pixel_data_matrix_4d as first, middle, and last
# collect only the filst, middle, and last frames from pixel_data_matrix_4d as first, middle, and last
# frames are typically the most interesting
frames = [0, len(data) // 2, -1]
frames_to_record = []
Expand All @@ -459,11 +459,12 @@ def read_ecat(ecat_file: str, calibrated: bool = False, collect_pixel_data: bool
for index, frame in enumerate(frames_to_record):
numpy.savetxt(steps_dir / f"4_read_img_ecat_python_{index}.tsv",
frames_to_record[index][:, :, frames_to_record[index].shape[2] // 2],
delimiter="\t")
delimiter="\t", fmt='%s')
if calibrated:
numpy.savetxt(steps_dir / f"5_scale_img_ecat_python_{index}.tsv",
calibrated_pixel_data_matrix_3d[:, :, frames_to_record[index].shape[2] // 2],
delimiter="\t")
delimiter="\t",
fmt='%s')
else:
pixel_data_matrix_4d = None

Expand Down

0 comments on commit 24cb01c

Please sign in to comment.