goal_def_UI.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 23-Oct-2020 12:55:21
  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. handles.uitable1.ColumnFormat{2}{numel(Geometry.ROIS)} = '-||-';
  65. % == populate function options ==
  66. handles.uitable1.ColumnFormat{5}{1} = 'min';
  67. handles.uitable1.ColumnFormat{5}{2} = 'max';
  68. handles.uitable1.ColumnFormat{5}{3} = 'min_sq';
  69. handles.uitable1.ColumnFormat{5}{4} = 'max_sq';
  70. handles.uitable1.ColumnFormat{5}{5} = 'min_step';
  71. handles.uitable1.ColumnFormat{5}{6} = 'max_step';
  72. handles.uitable1.ColumnFormat{5}{7} = 'LeastSquare';
  73. handles.uitable1.ColumnFormat{5}{8} = 'min_perc_Volume';
  74. handles.uitable1.ColumnFormat{5}{9} = 'max_perc_Volume';
  75. handles.uitable1.ColumnFormat{5}{8} = 'min_sq_voxwgt';
  76. handles.uitable1.ColumnFormat{5}{9} = 'max_sq_voxwgt';
  77. % == populate the first entry ==
  78. handles.uitable1.Data = handles.uitable1.Data(1,:);
  79. handles.uitable1.Data{1} = 'Goal 1';
  80. handles.uitable1.Data{2} = 'Target';
  81. handles.uitable1.Data{3} = 'Fixed dose';
  82. handles.uitable1.Data{4} = '60';
  83. handles.uitable1.Data{5} = 'min_sq';
  84. handles.uitable1.Data{6} = 100;
  85. handles.uitable1.Data{7} = 'null';
  86. handles.uitable1.Data{8} = 0;
  87. handles.uitable1.Data{9} = 0;
  88. % populate the drop-down
  89. % handles.popupmenu2.String = {"test 1","test 2"};
  90. set(handles.optSelect_popup, 'String', {"RO max", "RO mean", "RO objective-based", "Classical"});
  91. % Update handles structure
  92. guidata(hObject, handles);
  93. % UIWAIT makes goal_def_test wait for user response (see UIRESUME)
  94. % uiwait(handles.figure1);
  95. end
  96. % --- Outputs from this function are returned to the command line.
  97. function varargout = goal_def_test_OutputFcn(hObject, eventdata, handles)
  98. % varargout cell array for returning output args (see VARARGOUT);
  99. % hObject handle to figure
  100. % eventdata reserved - to be defined in a future version of MATLAB
  101. % handles structure with handles and user data (see GUIDATA)
  102. % Get default command line output from handles structure
  103. varargout{1} = handles.output;
  104. end
  105. % --- Executes on button press in addRow.
  106. function addRow_Callback(hObject, eventdata, handles)
  107. % adds new empty row to table
  108. handles.uitable1.Data{end+1, 1} = ['Goal ' num2str(size(handles.uitable1.Data,1)+1)];
  109. end
  110. % --- Executes on button press in removeRow.
  111. function removeRow_Callback(hObject, eventdata, handles)
  112. % Removes row number selected by Remove_row_N
  113. newTable = handles.uitable1.Data;
  114. rowToDelete = str2double(handles.Remove_row_N.String);
  115. if rowToDelete>size(newTable, 1)
  116. disp(['No row ' num2str(rowToDelete)])
  117. else
  118. newTable(rowToDelete,:) = [];
  119. handles.uitable1.Data = newTable;
  120. end
  121. end
  122. function Remove_row_N_Callback(hObject, eventdata, handles)
  123. % Identifies what is the number for removing a row and checks validty of
  124. % input.
  125. num = str2double(handles.Remove_row_N.String);
  126. if isnan(num)
  127. disp('You''re a silly goose. Enter a number.')
  128. elseif num<1
  129. disp('Row number must be greater than 0')
  130. num = 1;
  131. handles.Remove_row_N.String = 1;
  132. else
  133. r = rem(num, 1);
  134. if r ~= 0
  135. disp('Enter an integer, duh.')
  136. num = round(num);
  137. handles.Remove_row_N.String = round(num);
  138. end
  139. disp(num)
  140. end
  141. end
  142. % --- Executes on button press in Save_goal_button.
  143. function Save_goal_button_Callback(hObject, eventdata, handles)
  144. % hObject handle to Save_goal_button (see GCBO)
  145. % eventdata reserved - to be defined in a future version of MATLAB
  146. % handles structure with handles and user data (see GUIDATA)
  147. Save_goal_function(handles);
  148. end
  149. function Save_goal_function(handles)
  150. % this function creates and outputs the OptGoals file
  151. [file,path,indx] = uiputfile([handles.Data.Geo_path, '*.mat'],'Choose where to save OptGoals');
  152. OptGoals.geometryPath = [handles.Data.Geo_path handles.Data.Geo_fileName];
  153. disp('Loading Geometry ...')
  154. load([OptGoals.geometryPath])
  155. % disp('Loading beamlets ...')
  156. % [beamlets, beamlets_joined, numBeamlet, numBeam, beam_i_list] = get_beam_lets(Geometry, Geometry.patient_dir);
  157. OptGoals.data={};
  158. for i = 1 : size(handles.uitable1.Data, 1)
  159. % -- START DEFINITION OF GOAL --
  160. % == goal name
  161. goal_i.name = handles.uitable1.Data{i,1};
  162. % == ROI name
  163. goal_i.ROI_name = handles.uitable1.Data{i,2};
  164. % == ROI index
  165. for j = 1:size(Geometry.ROIS,2)
  166. if strcmp(Geometry.ROIS{j}.name, handles.uitable1.Data{i,2})
  167. goal_i.ROI_idx = Geometry.ROIS{j}.ind;
  168. break
  169. end
  170. if strcmp('-||-', handles.uitable1.Data{i,2})
  171. goal_i.ROI_idx = OptGoals.data{end}.ROI_idx;
  172. break
  173. end
  174. if j == size(Geometry.ROIS,2)
  175. error(['Invalid ROI name selected in goal ' num2str(i)])
  176. end
  177. end
  178. % == image dimensions
  179. goal_i.imgDim = size(Geometry.data);
  180. % == goal dose
  181. if strcmp (handles.uitable1.Data{i,3}, 'Fixed dose')
  182. goal_i.D_final = str2double(handles.uitable1.Data{i,4}) * ones(size(goal_i.ROI_idx));
  183. elseif strcmp (handles.uitable1.Data{i,3}, 'Dose map')
  184. % warning('Works only for NRRD (for now)')
  185. dose_in = nrrdread(handles.uitable1.Data{i,4});
  186. colorwash(Geometry.data-1000, dose_in, [-500, 500], [0, 1.0*(max(dose_in(:)))], 1, goal_i.name)
  187. goal_i.D_final = dose_in(goal_i.ROI_idx);
  188. % loading of data comes here
  189. % matrix size checks
  190. end
  191. % == goal penalty function
  192. goal_i.function = handles.uitable1.Data{i,5};
  193. % == goal penalty function
  194. if strcmp (handles.uitable1.Data{i,5}, 'min_sq_voxwgt')
  195. handles.uitable1.Data{i,2};
  196. voxwgt_in = nrrdread(handles.uitable1.Data{i, 7});
  197. % load(handles.uitable1.Data{i, 7});
  198. % voxwgt_in = miTLM_3;
  199. goal_i.wgt_map = voxwgt_in(goal_i.ROI_idx);
  200. goal_i.wgt_map = goal_i.wgt_map / mean(goal_i.wgt_map(:)); % normalize
  201. elseif strcmp (handles.uitable1.Data{i,5}, 'max_sq_voxwgt')
  202. voxwgt_in = nrrdread(handles.uitable1.Data{i, 7});
  203. % load(handles.uitable1.Data{i, 7});
  204. % voxwgt_in = miTLM_3;
  205. goal_i.wgt_map = voxwgt_in(goal_i.ROI_idx);
  206. goal_i.wgt_map = goal_i.wgt_map / mean(goal_i.wgt_map(:)); % normalize
  207. else
  208. if isfield (goal_i, 'wgt_map')
  209. goal_i = rmfield(goal_i, 'wgt_map');
  210. end
  211. end
  212. % == goal weight
  213. goal_i.opt_weight = handles.uitable1.Data{i,6} / numel(goal_i.ROI_idx); % normalize to volume of target area
  214. % == optional
  215. goal_i.optionalParam = handles.uitable1.Data{i,7};
  216. % == Number of supervoxels
  217. goal_i.SupVox_num = handles.uitable1.Data{i,9};
  218. if strcmp('-||-', handles.uitable1.Data{i,2})
  219. goal_i.SupVox_num = handles.uitable1.Data{i-1,9};
  220. end
  221. % -- END DEFINITION OF GOAL --
  222. % assign target
  223. OptGoals.data{end+1}=goal_i;
  224. end
  225. disp('-- all OptGoals exported!')
  226. OptGoals.goals = [handles.uitable1.Data];
  227. OptGoals.maxModulation = str2double(handles.maxModulation.String);
  228. OptGoals.BeamSmoothMax = str2double(handles.BeamSmoothMax.String);
  229. OptGoals.optFunc = handles.optSelect_popup.String{handles.optSelect_popup.Value};
  230. switch OptGoals.optFunc
  231. case "RO max"
  232. OptGoals.optFuncNum = 1;
  233. case "RO mean"
  234. OptGoals.optFuncNum = 2;
  235. case "RO objective-based"
  236. OptGoals.optFuncNum = 3;
  237. case "Classical"
  238. OptGoals.optFuncNum = 4;
  239. end
  240. OptGoals.sss.Y = handles.sss_Y.String;
  241. OptGoals.sss.X = handles.sss_X.String;
  242. OptGoals.sss.Z = handles.sss_Z.String;
  243. sss_scene_list={[0,0,0]};
  244. Num = sscanf(handles.sss_Y.String, '%g,').';
  245. for i=1:numel(Num)
  246. sss_scene_list{end+1}=[Num(i),0,0];
  247. end
  248. Num = sscanf(handles.sss_X.String, '%g,').';
  249. for i=1:numel(Num)
  250. sss_scene_list{end+1}=[0,Num(i),0];
  251. end
  252. Num = sscanf(handles.sss_Z.String, '%g,').';
  253. for i=1:numel(Num)
  254. sss_scene_list{end+1}=[0,0,Num(i)];
  255. end
  256. OptGoals.sss_scene_list = sss_scene_list;
  257. save( [path, file], 'OptGoals')
  258. end
  259. % --- Executes on button press in Load_goal_button.
  260. function Load_goal_button_Callback(hObject, eventdata, handles)
  261. % hObject handle to Load_goal_button (see GCBO)
  262. % eventdata reserved - to be defined in a future version of MATLAB
  263. % handles structure with handles and user data (see GUIDATA)
  264. [file,path,indx] = uigetfile([handles.Data.Geo_path, '*.mat'],'Choose OptGoals to load');
  265. load( [path, file])
  266. disp('load goal')
  267. handles.uitable1.Data = OptGoals.goals;
  268. handles.maxModulation.String = num2str(OptGoals.maxModulation);
  269. handles.sss_Y.String = OptGoals.sss.Y;
  270. handles.sss_X.String = OptGoals.sss.X;
  271. handles.sss_Z.String = OptGoals.sss.Z;
  272. if isfield(OptGoals, 'BeamSmoothMax')
  273. handles.BeamSmoothMax.String = num2str(OptGoals.BeamSmoothMax);
  274. end
  275. end
  276. % --- Executes when entered data in editable cell(s) in uitable1.
  277. function uitable1_CellEditCallback(hObject, eventdata, handles)
  278. % hObject handle to uitable1 (see GCBO)
  279. % eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
  280. % Indices: row and column indices of the cell(s) edited
  281. % PreviousData: previous data for the cell(s) edited
  282. % EditData: string(s) entered by the user
  283. % NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
  284. % Error: error string when failed to convert EditData to appropriate value for Data
  285. % handles structure with handles and user data (see GUIDATA)
  286. % --- update DP or fixed dose goals
  287. if eventdata.Indices(2) == 3;
  288. mode = handles.uitable1.Data{eventdata.Indices(1), eventdata.Indices(2)}
  289. switch mode
  290. case 'Dose map'
  291. [FileName,PathName,FilterIndex] = uigetfile('F:\021_WiscPlan_data\FET_repeat_005_1\matlab_files\*.mat', 'Select dose reference file');
  292. warning('loading of said profile comes here')
  293. handles.uitable1.Data{eventdata.Indices(1), 4} = [PathName, FileName];
  294. case 'Fixed dose'
  295. answer = inputdlg('Enter desired goal dose');
  296. handles.uitable1.Data{eventdata.Indices(1), 4} = answer{1};
  297. otherwise
  298. error('This doesnt work')
  299. end
  300. end
  301. % --- update DP or fixed dose goals
  302. if eventdata.Indices(2) == 5;
  303. mode = handles.uitable1.Data{eventdata.Indices(1), eventdata.Indices(2)}
  304. switch mode
  305. case 'min_sq_voxwgt'
  306. [FileName,PathName,FilterIndex] = uigetfile('F:\021_WiscPlan_data\FET_repeat_005_1\matlab_files\*.mat', 'Select voxel weight map');
  307. handles.uitable1.Data{eventdata.Indices(1), 7} = [PathName, FileName];
  308. case 'max_sq_voxwgt'
  309. [FileName,PathName,FilterIndex] = uigetfile('F:\021_WiscPlan_data\FET_repeat_005_1\matlab_files\*.mat', 'Select voxel weight map');
  310. handles.uitable1.Data{eventdata.Indices(1), 7} = [PathName, FileName];
  311. otherwise
  312. end
  313. end
  314. % --- update with number of voxels
  315. if eventdata.Indices(2) == 2;
  316. for i = 1:size(handles.Data.Geometry.ROIS,2)
  317. if strcmp(handles.Data.Geometry.ROIS{i}.name, handles.uitable1.Data{eventdata.Indices(1),2})
  318. ROI_idx_num = numel(handles.Data.Geometry.ROIS{i}.ind);
  319. break
  320. end
  321. if strcmp('-||-', handles.uitable1.Data{eventdata.Indices(1),2})
  322. ROI_idx_num = 0;
  323. break
  324. end
  325. if i == size(handles.Data.Geometry.ROIS,2)
  326. error(['Invalid ROI name selected in goal ' num2str(i)])
  327. end
  328. end
  329. handles.uitable1.Data{eventdata.Indices(1), 8} = ROI_idx_num;
  330. handles.uitable1.Data{eventdata.Indices(1), 9} = 0;
  331. end
  332. end
  333. function maxModulation_Callback(hObject, eventdata, handles)
  334. % hObject handle to maxModulation (see GCBO)
  335. % eventdata reserved - to be defined in a future version of MATLAB
  336. % handles structure with handles and user data (see GUIDATA)
  337. % Hints: get(hObject,'String') returns contents of maxModulation as text
  338. % str2double(get(hObject,'String')) returns contents of maxModulation as a double
  339. end
  340. % --- Executes during object creation, after setting all properties.
  341. function maxModulation_CreateFcn(hObject, eventdata, handles)
  342. % hObject handle to maxModulation (see GCBO)
  343. % eventdata reserved - to be defined in a future version of MATLAB
  344. % handles empty - handles not created until after all CreateFcns called
  345. % Hint: edit controls usually have a white background on Windows.
  346. % See ISPC and COMPUTER.
  347. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  348. set(hObject,'BackgroundColor','white');
  349. end
  350. end
  351. function sss_Y_Callback(hObject, eventdata, handles)
  352. % hObject handle to sss_Y (see GCBO)
  353. % eventdata reserved - to be defined in a future version of MATLAB
  354. % handles structure with handles and user data (see GUIDATA)
  355. % Hints: get(hObject,'String') returns contents of sss_Y as text
  356. % str2double(get(hObject,'String')) returns contents of sss_Y as a double
  357. end
  358. % --- Executes during object creation, after setting all properties.
  359. function sss_Y_CreateFcn(hObject, eventdata, handles)
  360. % hObject handle to sss_Y (see GCBO)
  361. % eventdata reserved - to be defined in a future version of MATLAB
  362. % handles empty - handles not created until after all CreateFcns called
  363. % Hint: edit controls usually have a white background on Windows.
  364. % See ISPC and COMPUTER.
  365. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  366. set(hObject,'BackgroundColor','white');
  367. end
  368. end
  369. function sss_X_Callback(hObject, eventdata, handles)
  370. % hObject handle to sss_X (see GCBO)
  371. % eventdata reserved - to be defined in a future version of MATLAB
  372. % handles structure with handles and user data (see GUIDATA)
  373. % Hints: get(hObject,'String') returns contents of sss_X as text
  374. % str2double(get(hObject,'String')) returns contents of sss_X as a double
  375. end
  376. % --- Executes during object creation, after setting all properties.
  377. function sss_X_CreateFcn(hObject, eventdata, handles)
  378. % hObject handle to sss_X (see GCBO)
  379. % eventdata reserved - to be defined in a future version of MATLAB
  380. % handles empty - handles not created until after all CreateFcns called
  381. % Hint: edit controls usually have a white background on Windows.
  382. % See ISPC and COMPUTER.
  383. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  384. set(hObject,'BackgroundColor','white');
  385. end
  386. end
  387. function sss_Z_Callback(hObject, eventdata, handles)
  388. % hObject handle to sss_Z (see GCBO)
  389. % eventdata reserved - to be defined in a future version of MATLAB
  390. % handles structure with handles and user data (see GUIDATA)
  391. % Hints: get(hObject,'String') returns contents of sss_Z as text
  392. % str2double(get(hObject,'String')) returns contents of sss_Z as a double
  393. end
  394. % --- Executes during object creation, after setting all properties.
  395. function sss_Z_CreateFcn(hObject, eventdata, handles)
  396. % hObject handle to sss_Z (see GCBO)
  397. % eventdata reserved - to be defined in a future version of MATLAB
  398. % handles empty - handles not created until after all CreateFcns called
  399. % Hint: edit controls usually have a white background on Windows.
  400. % See ISPC and COMPUTER.
  401. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  402. set(hObject,'BackgroundColor','white');
  403. end
  404. end
  405. function BeamSmoothMax_Callback(hObject, eventdata, handles)
  406. % hObject handle to BeamSmoothMax (see GCBO)
  407. % eventdata reserved - to be defined in a future version of MATLAB
  408. % handles structure with handles and user data (see GUIDATA)
  409. % Hints: get(hObject,'String') returns contents of BeamSmoothMax as text
  410. % str2double(get(hObject,'String')) returns contents of BeamSmoothMax as a double
  411. end
  412. % --- Executes on selection change in optSelect_popup.
  413. function optSelect_popup_Callback(hObject, eventdata, handles)
  414. % hObject handle to optSelect_popup (see GCBO)
  415. % eventdata reserved - to be defined in a future version of MATLAB
  416. % handles structure with handles and user data (see GUIDATA)
  417. % Hints: contents = cellstr(get(hObject,'String')) returns optSelect_popup contents as cell array
  418. % contents{get(hObject,'Value')} returns selected item from optSelect_popup
  419. end
  420. % --- Executes during object creation, after setting all properties.
  421. function optSelect_popup_CreateFcn(hObject, eventdata, handles)
  422. % hObject handle to optSelect_popup (see GCBO)
  423. % eventdata reserved - to be defined in a future version of MATLAB
  424. % handles empty - handles not created until after all CreateFcns called
  425. % Hint: popupmenu controls usually have a white background on Windows.
  426. % See ISPC and COMPUTER.
  427. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  428. set(hObject,'BackgroundColor','white');
  429. end
  430. end