goal_def_UI.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. function varargout = goal_def_UI(varargin)
  2. % GOAL_DEF_TEST MATLAB code for goal_def_test.fig
  3. % GOAL_DEF_TEST, by itself, creates a new GOAL_DEF_TEST or raises the existing
  4. % singleton*.
  5. %
  6. % H = GOAL_DEF_TEST returns the handle to a new GOAL_DEF_TEST or the handle to
  7. % the existing singleton*.
  8. %
  9. % GOAL_DEF_TEST('CALLBACK',hObject,eventData,handles,...) calls the local
  10. % function named CALLBACK in GOAL_DEF_TEST.M with the given input arguments.
  11. %
  12. % GOAL_DEF_TEST('Property','Value',...) creates a new GOAL_DEF_TEST or raises the
  13. % existing singleton*. Starting from the left, property value pairs are
  14. % applied to the GUI before goal_def_test_OpeningFcn gets called. An
  15. % unrecognized property name or invalid value makes property application
  16. % stop. All inputs are passed to goal_def_test_OpeningFcn via varargin.
  17. %
  18. % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
  19. % instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES
  22. % Edit the above text to modify the response to help goal_def_test
  23. % Last Modified by GUIDE v2.5 04-Jun-2020 04:51:32
  24. % Begin initialization code - DO NOT EDIT
  25. gui_Singleton = 1;
  26. gui_State = struct('gui_Name', mfilename, ...
  27. 'gui_Singleton', gui_Singleton, ...
  28. 'gui_OpeningFcn', @goal_def_test_OpeningFcn, ...
  29. 'gui_OutputFcn', @goal_def_test_OutputFcn, ...
  30. 'gui_LayoutFcn', [] , ...
  31. 'gui_Callback', []);
  32. if nargin && ischar(varargin{1})
  33. gui_State.gui_Callback = str2func(varargin{1});
  34. end
  35. if nargout
  36. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  37. else
  38. gui_mainfcn(gui_State, varargin{:});
  39. end
  40. % End initialization code - DO NOT EDIT
  41. end
  42. % --- Executes just before goal_def_test is made visible.
  43. function goal_def_test_OpeningFcn(hObject, eventdata, handles, varargin)
  44. % This function has no output args, see OutputFcn.
  45. % hObject handle to figure
  46. % eventdata reserved - to be defined in a future version of MATLAB
  47. % handles structure with handles and user data (see GUIDATA)
  48. % varargin command line arguments to goal_def_test (see VARARGIN)
  49. % Choose default command line output for goal_def_test
  50. handles.output = hObject;
  51. %% GET THE INITIAL GEOMETRY DATA
  52. disp('varargin')
  53. if numel(varargin) == 0
  54. load('WiscPlan_preferences.mat')
  55. [handles.Data.Geo_fileName,handles.Data.Geo_path,FilterIndex] = uigetfile([WiscPlan_preferences.patientDataPath '\matlab_files\*.mat'], 'Select Geometry file');
  56. load([handles.Data.Geo_path, handles.Data.Geo_fileName]);
  57. eval('handles.Data.Geometry = Geometry;')
  58. end
  59. %% POPULATE THE TABLE
  60. % == populate ROI options ==
  61. for i = 1: numel(Geometry.ROIS)
  62. handles.uitable1.ColumnFormat{2}{i} = Geometry.ROIS{i}.name;
  63. end
  64. % == populate function options ==
  65. handles.uitable1.ColumnFormat{5}{1} = 'min';
  66. handles.uitable1.ColumnFormat{5}{2} = 'max';
  67. handles.uitable1.ColumnFormat{5}{3} = 'min_sq';
  68. handles.uitable1.ColumnFormat{5}{4} = 'max_sq';
  69. handles.uitable1.ColumnFormat{5}{5} = 'min_step';
  70. handles.uitable1.ColumnFormat{5}{6} = 'max_step';
  71. handles.uitable1.ColumnFormat{5}{7} = 'LeastSquare';
  72. handles.uitable1.ColumnFormat{5}{8} = 'min_perc_Volume';
  73. handles.uitable1.ColumnFormat{5}{9} = 'max_perc_Volume';
  74. handles.uitable1.ColumnFormat{5}{8} = 'min_sq_voxwgt';
  75. handles.uitable1.ColumnFormat{5}{9} = 'max_sq_voxwgt';
  76. % == populate the first entry ==
  77. handles.uitable1.Data = handles.uitable1.Data(1,:);
  78. handles.uitable1.Data{1} = 'Goal 1';
  79. handles.uitable1.Data{2} = 'Target';
  80. handles.uitable1.Data{3} = 'Fixed dose';
  81. handles.uitable1.Data{4} = '60';
  82. handles.uitable1.Data{5} = 'min_sq';
  83. handles.uitable1.Data{6} = 100;
  84. handles.uitable1.Data{7} = 'null';
  85. handles.uitable1.Data{8} = 0;
  86. handles.uitable1.Data{9} = 0;
  87. % Update handles structure
  88. guidata(hObject, handles);
  89. % UIWAIT makes goal_def_test wait for user response (see UIRESUME)
  90. % uiwait(handles.figure1);
  91. end
  92. % --- Outputs from this function are returned to the command line.
  93. function varargout = goal_def_test_OutputFcn(hObject, eventdata, handles)
  94. % varargout cell array for returning output args (see VARARGOUT);
  95. % hObject handle to figure
  96. % eventdata reserved - to be defined in a future version of MATLAB
  97. % handles structure with handles and user data (see GUIDATA)
  98. % Get default command line output from handles structure
  99. varargout{1} = handles.output;
  100. end
  101. % --- Executes on button press in addRow.
  102. function addRow_Callback(hObject, eventdata, handles)
  103. % adds new empty row to table
  104. handles.uitable1.Data{end+1, 1} = ['Goal ' num2str(size(handles.uitable1.Data,1)+1)];
  105. end
  106. % --- Executes on button press in removeRow.
  107. function removeRow_Callback(hObject, eventdata, handles)
  108. % Removes row number selected by Remove_row_N
  109. newTable = handles.uitable1.Data;
  110. rowToDelete = str2double(handles.Remove_row_N.String);
  111. if rowToDelete>size(newTable, 1)
  112. disp(['No row ' num2str(rowToDelete)])
  113. else
  114. newTable(rowToDelete,:) = [];
  115. handles.uitable1.Data = newTable;
  116. end
  117. end
  118. function Remove_row_N_Callback(hObject, eventdata, handles)
  119. % Identifies what is the number for removing a row and checks validty of
  120. % input.
  121. num = str2double(handles.Remove_row_N.String);
  122. if isnan(num)
  123. disp('You''re a silly goose. Enter a number.')
  124. elseif num<1
  125. disp('Row number must be greater than 0')
  126. num = 1;
  127. handles.Remove_row_N.String = 1;
  128. else
  129. r = rem(num, 1);
  130. if r ~= 0
  131. disp('Enter an integer, duh.')
  132. num = round(num);
  133. handles.Remove_row_N.String = round(num);
  134. end
  135. disp(num)
  136. end
  137. end
  138. % --- Executes on button press in Save_goal_button.
  139. function Save_goal_button_Callback(hObject, eventdata, handles)
  140. % hObject handle to Save_goal_button (see GCBO)
  141. % eventdata reserved - to be defined in a future version of MATLAB
  142. % handles structure with handles and user data (see GUIDATA)
  143. Save_goal_function(handles);
  144. end
  145. function Save_goal_function(handles)
  146. % this function creates and outputs the OptGoals file
  147. [file,path,indx] = uiputfile([handles.Data.Geo_path, '*.mat'],'Choose where to save OptGoals');
  148. OptGoals.geometryPath = [handles.Data.Geo_path handles.Data.Geo_fileName];
  149. disp('Loading Geometry ...')
  150. load([OptGoals.geometryPath])
  151. % disp('Loading beamlets ...')
  152. % [beamlets, beamlets_joined, numBeamlet, numBeam, beam_i_list] = get_beam_lets(Geometry, Geometry.patient_dir);
  153. OptGoals.data={};
  154. for i = 1 : size(handles.uitable1.Data, 1)
  155. % -- START DEFINITION OF GOAL --
  156. % == goal name
  157. goal_i.name = handles.uitable1.Data{i,1};
  158. % == ROI name
  159. goal_i.ROI_name = handles.uitable1.Data{i,2};
  160. % == ROI index
  161. for j = 1:size(Geometry.ROIS,2)
  162. if strcmp(Geometry.ROIS{j}.name, handles.uitable1.Data{i,2})
  163. goal_i.ROI_idx = Geometry.ROIS{j}.ind;
  164. break
  165. end
  166. if j == size(Geometry.ROIS,2)
  167. error(['Invalid ROI name selected in goal ' num2str(i)])
  168. end
  169. end
  170. % == image dimensions
  171. goal_i.imgDim = size(Geometry.data);
  172. % == goal dose
  173. if strcmp (handles.uitable1.Data{i,3}, 'Fixed dose')
  174. goal_i.D_final = str2double(handles.uitable1.Data{i,4}) * ones(size(goal_i.ROI_idx));
  175. elseif strcmp (handles.uitable1.Data{i,3}, 'Dose map')
  176. % warning('Works only for NRRD (for now)')
  177. dose_in = nrrdread(handles.uitable1.Data{i,4});
  178. colorwash(Geometry.data-1000, dose_in, [-500, 500], [0, 1.0*(max(dose_in(:)))], 1, goal_i.name)
  179. goal_i.D_final = dose_in(goal_i.ROI_idx);
  180. % loading of data comes here
  181. % matrix size checks
  182. end
  183. % == goal penalty function
  184. goal_i.function = handles.uitable1.Data{i,5};
  185. % == goal penalty function
  186. if strcmp (handles.uitable1.Data{i,5}, 'min_sq_voxwgt')
  187. handles.uitable1.Data{i,2};
  188. voxwgt_in = nrrdread(handles.uitable1.Data{i, 7});
  189. % load(handles.uitable1.Data{i, 7});
  190. % voxwgt_in = miTLM_3;
  191. goal_i.wgt_map = voxwgt_in(goal_i.ROI_idx);
  192. goal_i.wgt_map = goal_i.wgt_map / mean(goal_i.wgt_map(:)); % normalize
  193. elseif strcmp (handles.uitable1.Data{i,5}, 'max_sq_voxwgt')
  194. voxwgt_in = nrrdread(handles.uitable1.Data{i, 7});
  195. % load(handles.uitable1.Data{i, 7});
  196. % voxwgt_in = miTLM_3;
  197. goal_i.wgt_map = voxwgt_in(goal_i.ROI_idx);
  198. goal_i.wgt_map = goal_i.wgt_map / mean(goal_i.wgt_map(:)); % normalize
  199. else
  200. if isfield (goal_i, 'wgt_map')
  201. goal_i = rmfield(goal_i, 'wgt_map');
  202. end
  203. end
  204. % == goal weight
  205. goal_i.opt_weight = handles.uitable1.Data{i,6} / numel(goal_i.ROI_idx); % normalize to volume of target area
  206. % == optional
  207. goal_i.optionalParam = handles.uitable1.Data{i,7};
  208. % == Number of supervoxels
  209. goal_i.SupVox_num = handles.uitable1.Data{i,9};
  210. % -- END DEFINITION OF GOAL --
  211. % assign target
  212. OptGoals.data{end+1}=goal_i;
  213. end
  214. disp('-- all OptGoals exported!')
  215. OptGoals.goals = [handles.uitable1.Data];
  216. OptGoals.maxModulation = str2double(handles.maxModulation.String);
  217. OptGoals.BeamSmoothMax = str2double(handles.BeamSmoothMax.String);
  218. sss_scene_list={[0,0,0]};
  219. Num = sscanf(handles.sss_Y.String, '%g,').';
  220. for i=1:numel(Num)
  221. sss_scene_list{end+1}=[Num(i),0,0];
  222. end
  223. Num = sscanf(handles.sss_X.String, '%g,').';
  224. for i=1:numel(Num)
  225. sss_scene_list{end+1}=[0,Num(i),0];
  226. end
  227. Num = sscanf(handles.sss_Z.String, '%g,').';
  228. for i=1:numel(Num)
  229. sss_scene_list{end+1}=[0,0,Num(i)];
  230. end
  231. OptGoals.sss_scene_list = sss_scene_list;
  232. save( [path, file], 'OptGoals')
  233. end
  234. % --- Executes on button press in Load_goal_button.
  235. function Load_goal_button_Callback(hObject, eventdata, handles)
  236. % hObject handle to Load_goal_button (see GCBO)
  237. % eventdata reserved - to be defined in a future version of MATLAB
  238. % handles structure with handles and user data (see GUIDATA)
  239. [file,path,indx] = uigetfile([handles.Data.Geo_path, '*.mat'],'Choose OptGoals to load');
  240. load( [path, file])
  241. disp('load goal')
  242. handles.uitable1.Data = OptGoals.goals;
  243. handles.maxModulation.String = num2str(OptGoals.maxModulation);
  244. if isfield(OptGoals, 'BeamSmoothMax')
  245. handles.MaxBeamSmooth.String = num2str(OptGoals.BeamSmoothMax);
  246. end
  247. end
  248. % --- Executes when entered data in editable cell(s) in uitable1.
  249. function uitable1_CellEditCallback(hObject, eventdata, handles)
  250. % hObject handle to uitable1 (see GCBO)
  251. % eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
  252. % Indices: row and column indices of the cell(s) edited
  253. % PreviousData: previous data for the cell(s) edited
  254. % EditData: string(s) entered by the user
  255. % NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
  256. % Error: error string when failed to convert EditData to appropriate value for Data
  257. % handles structure with handles and user data (see GUIDATA)
  258. % --- update DP or fixed dose goals
  259. if eventdata.Indices(2) == 3;
  260. mode = handles.uitable1.Data{eventdata.Indices(1), eventdata.Indices(2)}
  261. switch mode
  262. case 'Dose map'
  263. [FileName,PathName,FilterIndex] = uigetfile('F:\021_WiscPlan_data\FET_repeat_005_1\matlab_files\*.mat', 'Select dose reference file');
  264. warning('loading of said profile comes here')
  265. handles.uitable1.Data{eventdata.Indices(1), 4} = [PathName, FileName];
  266. case 'Fixed dose'
  267. answer = inputdlg('Enter desired goal dose');
  268. handles.uitable1.Data{eventdata.Indices(1), 4} = answer{1};
  269. otherwise
  270. error('This doesnt work')
  271. end
  272. end
  273. % --- update DP or fixed dose goals
  274. if eventdata.Indices(2) == 5;
  275. mode = handles.uitable1.Data{eventdata.Indices(1), eventdata.Indices(2)}
  276. switch mode
  277. case 'min_sq_voxwgt'
  278. [FileName,PathName,FilterIndex] = uigetfile('F:\021_WiscPlan_data\FET_repeat_005_1\matlab_files\*.mat', 'Select voxel weight map');
  279. handles.uitable1.Data{eventdata.Indices(1), 7} = [PathName, FileName];
  280. case 'max_sq_voxwgt'
  281. [FileName,PathName,FilterIndex] = uigetfile('F:\021_WiscPlan_data\FET_repeat_005_1\matlab_files\*.mat', 'Select voxel weight map');
  282. handles.uitable1.Data{eventdata.Indices(1), 7} = [PathName, FileName];
  283. otherwise
  284. end
  285. end
  286. % --- update with number of voxels
  287. if eventdata.Indices(2) == 2;
  288. for i = 1:size(handles.Data.Geometry.ROIS,2)
  289. if strcmp(handles.Data.Geometry.ROIS{i}.name, handles.uitable1.Data{eventdata.Indices(1),2})
  290. ROI_idx_num = numel(handles.Data.Geometry.ROIS{i}.ind);
  291. break
  292. end
  293. if i == size(handles.Data.Geometry.ROIS,2)
  294. error(['Invalid ROI name selected in goal ' num2str(i)])
  295. end
  296. end
  297. handles.uitable1.Data{eventdata.Indices(1), 8} = ROI_idx_num;
  298. handles.uitable1.Data{eventdata.Indices(1), 9} = 0;
  299. end
  300. end
  301. function maxModulation_Callback(hObject, eventdata, handles)
  302. % hObject handle to maxModulation (see GCBO)
  303. % eventdata reserved - to be defined in a future version of MATLAB
  304. % handles structure with handles and user data (see GUIDATA)
  305. % Hints: get(hObject,'String') returns contents of maxModulation as text
  306. % str2double(get(hObject,'String')) returns contents of maxModulation as a double
  307. end
  308. % --- Executes during object creation, after setting all properties.
  309. function maxModulation_CreateFcn(hObject, eventdata, handles)
  310. % hObject handle to maxModulation (see GCBO)
  311. % eventdata reserved - to be defined in a future version of MATLAB
  312. % handles empty - handles not created until after all CreateFcns called
  313. % Hint: edit controls usually have a white background on Windows.
  314. % See ISPC and COMPUTER.
  315. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  316. set(hObject,'BackgroundColor','white');
  317. end
  318. end
  319. function sss_Y_Callback(hObject, eventdata, handles)
  320. % hObject handle to sss_Y (see GCBO)
  321. % eventdata reserved - to be defined in a future version of MATLAB
  322. % handles structure with handles and user data (see GUIDATA)
  323. % Hints: get(hObject,'String') returns contents of sss_Y as text
  324. % str2double(get(hObject,'String')) returns contents of sss_Y as a double
  325. end
  326. % --- Executes during object creation, after setting all properties.
  327. function sss_Y_CreateFcn(hObject, eventdata, handles)
  328. % hObject handle to sss_Y (see GCBO)
  329. % eventdata reserved - to be defined in a future version of MATLAB
  330. % handles empty - handles not created until after all CreateFcns called
  331. % Hint: edit controls usually have a white background on Windows.
  332. % See ISPC and COMPUTER.
  333. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  334. set(hObject,'BackgroundColor','white');
  335. end
  336. end
  337. function sss_X_Callback(hObject, eventdata, handles)
  338. % hObject handle to sss_X (see GCBO)
  339. % eventdata reserved - to be defined in a future version of MATLAB
  340. % handles structure with handles and user data (see GUIDATA)
  341. % Hints: get(hObject,'String') returns contents of sss_X as text
  342. % str2double(get(hObject,'String')) returns contents of sss_X as a double
  343. end
  344. % --- Executes during object creation, after setting all properties.
  345. function sss_X_CreateFcn(hObject, eventdata, handles)
  346. % hObject handle to sss_X (see GCBO)
  347. % eventdata reserved - to be defined in a future version of MATLAB
  348. % handles empty - handles not created until after all CreateFcns called
  349. % Hint: edit controls usually have a white background on Windows.
  350. % See ISPC and COMPUTER.
  351. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  352. set(hObject,'BackgroundColor','white');
  353. end
  354. end
  355. function sss_Z_Callback(hObject, eventdata, handles)
  356. % hObject handle to sss_Z (see GCBO)
  357. % eventdata reserved - to be defined in a future version of MATLAB
  358. % handles structure with handles and user data (see GUIDATA)
  359. % Hints: get(hObject,'String') returns contents of sss_Z as text
  360. % str2double(get(hObject,'String')) returns contents of sss_Z as a double
  361. end
  362. % --- Executes during object creation, after setting all properties.
  363. function sss_Z_CreateFcn(hObject, eventdata, handles)
  364. % hObject handle to sss_Z (see GCBO)
  365. % eventdata reserved - to be defined in a future version of MATLAB
  366. % handles empty - handles not created until after all CreateFcns called
  367. % Hint: edit controls usually have a white background on Windows.
  368. % See ISPC and COMPUTER.
  369. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  370. set(hObject,'BackgroundColor','white');
  371. end
  372. end
  373. function BeamSmoothMax_Callback(hObject, eventdata, handles)
  374. % hObject handle to BeamSmoothMax (see GCBO)
  375. % eventdata reserved - to be defined in a future version of MATLAB
  376. % handles structure with handles and user data (see GUIDATA)
  377. % Hints: get(hObject,'String') returns contents of BeamSmoothMax as text
  378. % str2double(get(hObject,'String')) returns contents of BeamSmoothMax as a double
  379. end