goal_def_UI.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 28-Mar-2019 16:16:14
  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_exp';
  70. handles.uitable1.ColumnFormat{5}{6} = 'max_exp';
  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. % == populate the first entry ==
  75. handles.uitable1.Data = handles.uitable1.Data(1,:);
  76. handles.uitable1.Data{1} = 'Goal 1';
  77. handles.uitable1.Data{2} = 'Target';
  78. handles.uitable1.Data{3} = 'Fixed dose';
  79. handles.uitable1.Data{4} = '60';
  80. handles.uitable1.Data{5} = 'min_sq';
  81. handles.uitable1.Data{6} = 100;
  82. handles.uitable1.Data{7} = 'null';
  83. handles.uitable1.Data{8} = 0;
  84. handles.uitable1.Data{9} = 0;
  85. % Update handles structure
  86. guidata(hObject, handles);
  87. % UIWAIT makes goal_def_test wait for user response (see UIRESUME)
  88. % uiwait(handles.figure1);
  89. end
  90. % --- Outputs from this function are returned to the command line.
  91. function varargout = goal_def_test_OutputFcn(hObject, eventdata, handles)
  92. % varargout cell array for returning output args (see VARARGOUT);
  93. % hObject handle to figure
  94. % eventdata reserved - to be defined in a future version of MATLAB
  95. % handles structure with handles and user data (see GUIDATA)
  96. % Get default command line output from handles structure
  97. varargout{1} = handles.output;
  98. end
  99. % --- Executes on button press in addRow.
  100. function addRow_Callback(hObject, eventdata, handles)
  101. % adds new empty row to table
  102. handles.uitable1.Data{end+1, 1} = ['Goal ' num2str(size(handles.uitable1.Data,1)+1)];
  103. end
  104. % --- Executes on button press in removeRow.
  105. function removeRow_Callback(hObject, eventdata, handles)
  106. % Removes row number selected by Remove_row_N
  107. newTable = handles.uitable1.Data;
  108. rowToDelete = str2double(handles.Remove_row_N.String);
  109. if rowToDelete>size(newTable, 2)
  110. disp(['No row ' num2str(rowToDelete)])
  111. else
  112. newTable(rowToDelete,:) = [];
  113. handles.uitable1.Data = newTable;
  114. end
  115. end
  116. function Remove_row_N_Callback(hObject, eventdata, handles)
  117. % Identifies what is the number for removing a row and checks validty of
  118. % input.
  119. num = str2double(handles.Remove_row_N.String);
  120. if isnan(num)
  121. disp('You''re a silly goose. Enter a number.')
  122. elseif num<1
  123. disp('Row number must be greater than 0')
  124. num = 1;
  125. handles.Remove_row_N.String = 1;
  126. else
  127. r = rem(num, 1);
  128. if r ~= 0
  129. disp('Enter an integer, duh.')
  130. num = round(num);
  131. handles.Remove_row_N.String = round(num);
  132. end
  133. disp(num)
  134. end
  135. end
  136. % --- Executes on button press in Save_goal_button.
  137. function Save_goal_button_Callback(hObject, eventdata, handles)
  138. % hObject handle to Save_goal_button (see GCBO)
  139. % eventdata reserved - to be defined in a future version of MATLAB
  140. % handles structure with handles and user data (see GUIDATA)
  141. Save_goal_function(handles);
  142. end
  143. function Save_goal_function(handles)
  144. % this function creates and outputs the OptGoals file
  145. [file,path,indx] = uiputfile([handles.Data.Geo_path, '*.mat'],'Choose where to save OptGoals');
  146. OptGoals.geometryPath = [handles.Data.Geo_path handles.Data.Geo_fileName];
  147. disp('Loading Geometry ...')
  148. load([OptGoals.geometryPath])
  149. % disp('Loading beamlets ...')
  150. % [beamlets, beamlets_joined, numBeamlet, numBeam, beam_i_list] = get_beam_lets(Geometry, Geometry.patient_dir);
  151. OptGoals.data={};
  152. for i = 1 : size(handles.uitable1.Data, 1)
  153. % -- START DEFINITION OF GOAL --
  154. % == goal name
  155. goal_i.name = handles.uitable1.Data{i,1};
  156. % == ROI name
  157. goal_i.ROI_name = handles.uitable1.Data{i,2};
  158. % == ROI index
  159. for j = 1:size(Geometry.ROIS,2)
  160. if strcmp(Geometry.ROIS{j}.name, handles.uitable1.Data{i,2})
  161. goal_i.ROI_idx = Geometry.ROIS{j}.ind;
  162. break
  163. end
  164. if j == size(Geometry.ROIS,2)
  165. error(['Invalid ROI name selected in goal ' num2str(i)])
  166. end
  167. end
  168. % == image dimensions
  169. goal_i.imgDim = size(Geometry.data);
  170. % == goal dose
  171. if strcmp (handles.uitable1.Data{i,3}, 'Fixed dose')
  172. goal_i.D_final = str2double(handles.uitable1.Data{i,4}) * ones(size(Geometry.data));
  173. elseif strcmp (handles.uitable1.Data{i,3}, 'Dose map')
  174. % warning('Works only for NRRD (for now)')
  175. dose_in = nrrdread(handles.uitable1.Data{i,4});
  176. colorwash(Geometry.data-1000, dose_in, [-500, 500], [0, 1.5*(max(dose_in(:)))], 1, goal_i.name)
  177. goal_i.D_final = dose_in(goal_i.ROI_idx);
  178. % loading of data comes here
  179. % matrix size checks
  180. end
  181. % == goal penalty function
  182. goal_i.function = handles.uitable1.Data{i,5};
  183. % == goal weight
  184. goal_i.opt_weight = handles.uitable1.Data{i,6} / numel(goal_i.ROI_idx); % normalize to volume of target area
  185. % == optional
  186. goal_i.optionalParam = handles.uitable1.Data{i,7};
  187. % == Number of supervoxels
  188. goal_i.SupVox_num = handles.uitable1.Data{i,9};
  189. % -- END DEFINITION OF GOAL --
  190. % assign target
  191. OptGoals.data{end+1}=goal_i;
  192. end
  193. disp('-- all OptGoals exported!')
  194. OptGoals.goals = [handles.uitable1.Data];
  195. save( [path, file], 'OptGoals')
  196. end
  197. % --- Executes on button press in Load_goal_button.
  198. function Load_goal_button_Callback(hObject, eventdata, handles)
  199. % hObject handle to Load_goal_button (see GCBO)
  200. % eventdata reserved - to be defined in a future version of MATLAB
  201. % handles structure with handles and user data (see GUIDATA)
  202. [file,path,indx] = uigetfile([handles.Data.Geo_path, '*.mat'],'Choose OptGoals to load');
  203. load( [path, file])
  204. disp('load goal')
  205. handles.uitable1.Data = OptGoals.goals;
  206. end
  207. % --- Executes when entered data in editable cell(s) in uitable1.
  208. function uitable1_CellEditCallback(hObject, eventdata, handles)
  209. % hObject handle to uitable1 (see GCBO)
  210. % eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
  211. % Indices: row and column indices of the cell(s) edited
  212. % PreviousData: previous data for the cell(s) edited
  213. % EditData: string(s) entered by the user
  214. % NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
  215. % Error: error string when failed to convert EditData to appropriate value for Data
  216. % handles structure with handles and user data (see GUIDATA)
  217. % --- update DP or fixed dose goals
  218. if eventdata.Indices(2) == 3;
  219. mode = handles.uitable1.Data{eventdata.Indices(1), eventdata.Indices(2)}
  220. switch mode
  221. case 'Dose map'
  222. [FileName,PathName,FilterIndex] = uigetfile('F:\021_WiscPlan_data\FET_repeat_005_1\matlab_files\*.mat', 'Select dose reference file');
  223. warning('loading of said profile comes here')
  224. handles.uitable1.Data{eventdata.Indices(1), 4} = [PathName, FileName];
  225. case 'Fixed dose'
  226. answer = inputdlg('Enter desired goal dose');
  227. handles.uitable1.Data{eventdata.Indices(1), 4} = answer{1};
  228. otherwise
  229. error('This doesnt work')
  230. end
  231. end
  232. % --- update with number of voxels
  233. if eventdata.Indices(2) == 2;
  234. for i = 1:size(handles.Data.Geometry.ROIS,2)
  235. if strcmp(handles.Data.Geometry.ROIS{i}.name, handles.uitable1.Data{eventdata.Indices(1),2})
  236. ROI_idx_num = numel(handles.Data.Geometry.ROIS{i}.ind);
  237. break
  238. end
  239. if i == size(handles.Data.Geometry.ROIS,2)
  240. error(['Invalid ROI name selected in goal ' num2str(i)])
  241. end
  242. end
  243. handles.uitable1.Data{eventdata.Indices(1), 8} = ROI_idx_num;
  244. handles.uitable1.Data{eventdata.Indices(1), 9} = 0;
  245. end
  246. end