goal_def_UI.m 18 KB

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