Skip to content

Commit 0135f6d

Browse files
committed
Merge branch '263-telementry-add-tracking-of-code-usage' into 319-standardize-radiochemistry-metadata-caseorder
2 parents f312bbd + 349ef31 commit 0135f6d

File tree

10 files changed

+465
-60
lines changed

10 files changed

+465
-60
lines changed

matlab/dcm2niix4pet.m

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function dcm2niix4pet(FolderList,MetaList,varargin)
1212
%
1313
% :param FolderList: Cell array of char strings with filenames and paths
1414
% :param MetaList: Cell array of structures for metadata
15-
% :param options:
15+
% :param varargin:
1616
% - *deletedcm* to be 'on' or 'off'
1717
% - *o* the output directory or cell arrays of directories
1818
% IF the folder is BIDS sub-xx files are renamed automatically
@@ -30,6 +30,7 @@ function dcm2niix4pet(FolderList,MetaList,varargin)
3030
% - *w* = 2; % write behavior for name conflicts (0,1,2, default 2: 0=skip duplicates, 1=overwrite, 2=add suffix)
3131
% - *x* = 'n'; % crop 3D acquisitions (y/n/i, default n, use 'i'gnore to neither crop nor rotate 3D acquisitions)
3232
% - *z* = 'n'; % gz compress images (y/o/i/n/3, default y) [y=pigz, o=optimal pigz, i=internal:miniz, n=no, 3=no,3D]
33+
% :param notrack: boolean to Opt-out of sending tracking information of this run to the PET2BIDS developers. This information helps to improve PET2BIDS and provides an indicator of real world usage crucial for obtaining funding."
3334
%
3435
% .. code-block::
3536
%
@@ -50,7 +51,7 @@ function dcm2niix4pet(FolderList,MetaList,varargin)
5051

5152
dcm2niixpath = 'D:\MRI\MRIcroGL12win\Resources\dcm2niix.exe'; % for windows machine indicate here, where is dcm2niix
5253
if ispc && ~exist(dcm2niixpath,'file')
53-
error('for windows machine please edit the function line 42 and indicate the dcm2niix path')
54+
error('for windows machine please edit the function line 51 and indicate the dcm2niix path')
5455
end
5556

5657
if ~ispc % overwrite if not windowns (as it should be in the computer path)
@@ -63,9 +64,19 @@ function dcm2niix4pet(FolderList,MetaList,varargin)
6364
minimum_version = 'v1.0.20220720';
6465
minimum_version_date = datetime(minimum_version(6:end), 'InputFormat', 'yyyyMMdd');
6566
version_cmd = ['dcm2niix', ' -v'];
67+
6668
[status, version_output_string] = system(version_cmd);
6769
version = regexp(version_output_string, 'v[0-9].[0-9].{8}[0-9]', 'match');
6870

71+
72+
% initalize telemetry data fror later uploading
73+
telemetry_data = {};
74+
dcm2niix_data = {};
75+
dcm2niix_data.version = version(1);
76+
dcm2niix_data.returncode = 0;
77+
telemetry_data.dcm2niix = dcm2niix_data;
78+
telemetry_data.description = "Matlab_dcm2niix4pet.m"
79+
6980
if length(version) >= 1
7081
version_date = version{1}(6:end);
7182
version_date = datetime(version_date, 'InputFormat', 'yyyyMMdd');
@@ -209,7 +220,12 @@ function dcm2niix4pet(FolderList,MetaList,varargin)
209220
end
210221
elseif strcmpi(varargin{var},'o')
211222
outputdir = varargin{var+1};
223+
elseif strcmpi(varargin{var},'notrack')
224+
setenv('TELEMETRY_ENABLED', 'False')
225+
else
226+
error('unknown option %s',varargin{var})
212227
end
228+
213229
end
214230

215231
if isempty(outputdir)
@@ -225,6 +241,8 @@ function dcm2niix4pet(FolderList,MetaList,varargin)
225241
end
226242
end
227243

244+
245+
228246
%% convert
229247
% ----------
230248
for folder = 1:size(FolderList,1)
@@ -249,10 +267,16 @@ function dcm2niix4pet(FolderList,MetaList,varargin)
249267
end
250268

251269
out = system(command);
270+
telemetry_data.dcm2niix.returncode = out;
271+
% we still want to send telemetry even if this fails
252272
if out ~= 0
273+
telemetry_data.returncode = 1;
274+
telemetry(telemetry_data, folder);
253275
error('%s did not run properly',command)
276+
254277
end
255-
278+
279+
256280
% deal with dcm files
257281
dcmfiles = dir(fullfile(FolderList{folder},'*.dcm'));
258282
if isempty(dcmfiles) % since sometimes they have no ext :-(
@@ -318,4 +342,9 @@ function dcm2niix4pet(FolderList,MetaList,varargin)
318342
jsonfilename = newmetadata;
319343
end
320344
updatejsonpetfile(jsonfilename,MetaList,dcminfo);
345+
346+
% if this all goes well update the telemetry data and send it with a positive return code of 0
347+
telemetry_data.returncode = 0;
348+
telemetry(telemetry_data, FolderList{folder})
349+
321350
end

matlab/ecat2nii.m

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
parts = strsplit(ecat_save_steps_dir, filesep);
5959
ecat_save_steps_dir = strjoin([parts(1:end-2), 'ecat_testing', 'steps'], filesep);
6060

61+
%% initialize telemetry variable for reporting
62+
telemetry_data = {};
63+
telemetry_data.description = "Matlab_ecat2nii";
64+
6165
%% check inputs
6266
% ------------
6367

@@ -110,6 +114,8 @@
110114
gz = varargin{v+1};
111115
elseif strcmpi(varargin{v},'savemat')
112116
savemat = varargin{v+1};
117+
elseif strcmpi(varargin{v},'notrack')
118+
setenv('TELEMETRY_ENABLED', 'False')
113119
end
114120
end
115121

@@ -131,14 +137,18 @@
131137
%% Read and write data
132138
% --------------------
133139
for j=1:length(FileListIn)
134-
140+
135141
try
136142
fprintf('Conversion of file: %s\n',FileListIn{j});
137-
143+
138144
% quickly ensure we have the TimeZero - key to all analyzes!
139145
info = MetaList{1};
140146
if ~isfield(info,'TimeZero')
141-
error('Metadata TimeZero is missing - set to ScanStart or empty to use the scanning time as injection time')
147+
error_text = 'Metadata TimeZero is missing - set to ScanStart or empty to use the scanning time as injection time';
148+
telemetry_data.returncode = 1;
149+
telemetry_data.error = error_text;
150+
telemetry(telemetry_data, FileListIn{j})
151+
error(error_text)
142152
end
143153

144154
% Read ECAT file headers
@@ -180,6 +190,9 @@
180190
end
181191
end
182192

193+
% capture ecat version
194+
telemetry_data.InputType = append("ECAT", string(mh.sw_version));
195+
183196
% save debugging steps 6 and 7
184197
if (ecat_save_steps == '1')
185198
first_middle_last_frames_to_text_cell(data,ecat_save_steps_dir, '6_ecat2nii_matlab');
@@ -206,8 +219,6 @@
206219
fclose(fid);
207220
end
208221

209-
210-
211222
% save debugging step 8 - rescale to 16 bits
212223
if (ecat_save_steps == '1')
213224
first_middle_last_frames_to_text(img_temp,ecat_save_steps_dir, '8_rescale_to_16_ecat2nii_matlab');
@@ -489,8 +500,14 @@
489500
% FileListOut{j} = [filenameout '_pet.nii'];
490501
% niftiwrite(img_temp,FileListOut{j},info,'Endian','little','Compressed',false);
491502
% end
503+
504+
telemetry_data.returncode = 0;
505+
telemetry(telemetry_data, FileListIn{j});
492506

493507
catch conversionerr
508+
telemetry_data.returncode = 1;
509+
telemetry_data.error = conversionerr.message;
510+
telemetry(telemetry_data, FileListIn{j});
494511
FileListOut{j} = sprintf('%s failed to convert:%s',FileListIn{j},conversionerr.message, conversionerr.stack.line);
495512
end
496513

matlab/telemetry.m

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
function telemetry(telemetry_data, input_path, output_path)
2+
arguments
3+
telemetry_data (1,:) struct
4+
input_path (1,:) string = ''
5+
output_path (1,:) string = ''
6+
end
7+
8+
if telemetry_enabled
9+
% do all the things
10+
11+
telemetry_data.description = "Matlab";
12+
13+
if strcmp(input_path, '')
14+
% do nothing
15+
else
16+
input_file_count = count_input_files(input_path);
17+
telemetry_data.TotalInputFiles = input_file_count.TotalInputFiles;
18+
telemetry_data.TotalInputFileSize = input_file_count.TotalInputFileSize;
19+
end
20+
21+
url = 'http://52.87.154.236/telemetry/';
22+
options = weboptions('MediaType', 'application/json');
23+
response = webwrite(url, telemetry_data, options);
24+
25+
else
26+
% don't do anything
27+
end
28+
end
29+
30+
function e = telemetry_enabled()
31+
% checks to see if the telemetry is enabled or disabled
32+
environment = getenv();
33+
% check environment too before loading the config file
34+
if isfield(environment, 'TELEMETRY_ENABLED')
35+
disable_telemetry_env = strcmpi(getenv("TELEMETRY_ENABLED"), 'false');
36+
else
37+
disable_telemetry_env = false;
38+
end
39+
40+
home_dir = environment("HOME");
41+
try
42+
loadenv(fullfile(home_dir, '.pet2bidsconfig'), FileType='env');
43+
% convert string to boolean/logical
44+
catch ME
45+
disable_telemetry = false;
46+
end
47+
48+
disable_telemetry = strcmpi(getenv("TELEMETRY_ENABLED"), 'false');
49+
50+
if disable_telemetry | disable_telemetry_env
51+
e = false;
52+
else
53+
e = true;
54+
end
55+
56+
end
57+
58+
59+
function c = count_input_files(input_path)
60+
% generate a list of all the files in the input directory
61+
% count the number of files in the input directory
62+
% count the total size of the files in the input directory
63+
% return the count and the size
64+
65+
% if the input path is a file then return 1 and the size of the file
66+
if isfile(input_path)
67+
input_file = dir(input_path);
68+
c.TotalInputFiles = 1;
69+
c.TotalInputFileSize = input_file.bytes;
70+
return
71+
elseif isfolder(input_path)
72+
% get the list of files in the input directory
73+
input_files = dir(input_path);
74+
% count the number of files in the input directory
75+
file_count = length(input_files);
76+
% count the total size of the files in the input directory
77+
total_size = 0;
78+
for i = 1:file_count
79+
total_size = total_size + input_files(i).bytes;
80+
end
81+
c.TotalInputFiles = file_count;
82+
c.TotalInputFileSize = total_size;
83+
return
84+
else
85+
error('Input path is not a file or a directory');
86+
end
87+
end

0 commit comments

Comments
 (0)