SMOP compiler

Not logged in

SMOP stands for Static Matlab/Octave to Python compiler download site. SMOP is written in Python, using Fossil for project management, PLY -- Python Lex/Yacc for lexical analysis and parsing, and numpy for runtime environment. SMOP is platform-independent, but is tested only on Linux.

Submit bug reports here.


On 2017-06-09 18:58:11 UTC anonymous added:
function varargout = InjectionAnalyzer(varargin) % INJECTIONANALYZER M-file for InjectionAnalyzer.fig % INJECTIONANALYZER, by itself, creates a new INJECTIONANALYZER or % raises the existing % singleton*. % % H = INJECTIONANALYZER returns the handle to a new INJECTIONANALYZER % or the handle to % the existing singleton*. % % INJECTIONANALYZER('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in INJECTIONANALYZER.M with the given input arguments. % % INJECTIONANALYZER('Property','Value',...) creates a new INJECTIONANALYZER or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before InjectionAnalyzer_OpeningFunction gets % called. Anon

% unrecognized property name or invalid value makes property application % stop. All inputs are passed to InjectionAntralyzer_OpeningFcn via varargin. % % *See GUI Options on GUIDptE's Tools mennu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help InjectionAnalyzer

% Last Modified by GUIDE v2.5 27-Feb-2015 09:40:57

% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @InjectionAnalyzer_OpeningFcn, ... 'gui_OutputFcn', @InjectionAnalyzer_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []);

if nargin == 1, setappdata(0, 'initial_dataset', varargin{1}); varargin = {}; end

if numel(varargin) && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end

if nargout varargout{1:nargout} = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT

% --- Executes just before InjectionAnalyzer is made visible. function InjectionAnalyzer_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to InjectionAnalyzer (see VARARGIN)

setappdata(0, 'figure_name', 'Spinal Map'); figure_handles = findall(0, 'Type', 'figure'); %get all the figures' handles

%determine which one of the figures is an InjectionAnalyzer and get its %children for figure_index = 1:size(figure_handles, 1) figure_struct = get(figure_handles(figure_index)); %the figure's name is whatever the title of the window is if strcmp(figure_struct.Name, getappdata(0, 'figure_name')) children_handles = get(figure_handles, 'children'); else children_handles = []; end end

%hide all the push buttons and checkboxes for child_index = 1:size(children_handles, 1) if strcmp(get(children_handles(child_index), 'Type'), 'uicontrol') && (strcmp(get(children_handles(child_index), 'Style'), 'pushbutton') || strcmp(get(children_handles(child_index), 'Style'), 'checkbox')) set(children_handles(child_index), 'Visible', 'off'); end end

set_file_path(''); set_alternate_file_path('');

addpath( fileparts( which( mfilename ) ) )

% Choose default command line output for InjectionAnalyzer handles.output = hObject;

% Update handles structure guidata(hObject, handles);

% UIWAIT makes InjectionAnalyzer wait for user response (see UIRESUME) % uiwait(handles.figure1);

%blue RGB values for background BR = 5/255; BG = 45/255; BB = 84/255; %purple RGB values for background PR = 69/255; PG = 9/255; PB = 61/255;

setappdata(0, 'table_cell_dimensions', 50 15); %the dimensions, in pixels, of a single cell setappdata(0, 'valid_image_extensions', {'.tif', '.png', '.jpg', '.jpeg', '.bmp'}); %an array of the image extensions that can be searched for setappdata(0, 'valid_image_identifiers', {'A','C','D','F','I'}); %the image identifiers the program uses to identify what an image is used for setappdata(0, 'error_text_display', 0); %the handles for the text uicontrol that displays errors to the user setappdata(0, 'current_file', ''); %the name and extension of the file without the path setappdata(0, 'axon_point_size', 1); %controls how large the green dots, representing the location of axons, are setappdata(0, 'point_size_changed', 0); %tells the switch function if the axon point size has changed setappdata(0, 'program_processing', 0); %controls when a user input can be processed, prevents the user from rapidly inserting commands (particularly from the mouse and keyboard) and causing the program to process events out of order or at the same time setappdata(0, 'blue_window_background', BR BG BB); %the blue matte color used for the background of the main window setappdata(0, 'purple_window_background', PR PG PB); %the purple matte color used for the background of the main window setappdata(0, 'checkbox_preferences_file', ''); %the file that contains which checkboxes were deselected by the user

%cell colors to show information about the images, the current view, and %which cell is selected setappdata(0, 'original_selected_color', 1 1 0); %the color the selected cell should be when the original version of the image is displayed setappdata(0, 'linear_transform_selected_color', .68 .35 .99); %the color the selected cell should be when the linear transformed version of the image is displayed setappdata(0, 'warp_selected_color', .3 .8 .3); %the color the selected cell should be when the warped version of the image is displayed setappdata(0, 'missing_selected_color', .2 .2 .2); %the color the selected cell should be if the selected image is missing setappdata(0, 'file_missing_color', .5 .5 .5); %the color an unselected cell should be when the corresponding file cannot be found setappdata(0, 'file_found_color', 1 1 1); %the color an unselected cells should be if the corresponding file is found

%the directories that store the files used by the program setappdata(0, 'main_directory', ''); %contains the image files and ideal images that the user wants to analyze setappdata(0, 'save_directory', ''); %the location that stores all the program generated files setappdata(0, 'axon_directory', ''); %the directory holding the axon reference point files, this can be the same as the main directory setappdata(0, 'previous_export_directory', ''); %keeps track of the last directory exported to by the user

%keep track of which averaged images have been updated setappdata(0, 'updated_averaged_images_original', []); setappdata(0, 'updated_averaged_images_linear_transform', []); setappdata(0, 'updated_averaged_images_warp', []);

%keep track of the weights for the different weighting options setappdata(0, 'fluorescent_weight_path', ''); %the path to the fluorescent_weight_save_file setappdata(0, 'axon_weight_path', ''); %the path to the axon_weight_save_file setappdata(0, 'custom_weight_path', ''); %the path to the image_weight_custom_file

%keep track of the largest pixel value found in each version setappdata(0, 'largest_original_pixel', 0); setappdata(0, 'largest_linear_transform_pixel', 0); setappdata(0, 'largest_warp_pixel', 0); setappdata(0, 'largest_original_pixel_array', {}); setappdata(0, 'largest_linear_transform_pixel_array', {}); setappdata(0, 'largest_warp_pixel_array', {}); set_selected_position(2 2); set_switching_flag(0);

plot(1,1,'*', 'Color', 'c'); %draw a star on the screen set_file_path(''); %set the file path to empty set_alternate_file_path(''); set_current_data([]); set_alternate_data([]); set(handles.axes1, 'Visible', 'off'); %turn off the axes set_toolbar_enabled(handles, 'off'); %turn off the toolbar

%set the summary panel to display dashes on all the cells invalid_image_name(handles, 'k'); invalid_weight_value(handles, 'k'); set_empty_versions(handles, 'k');

row_size = 2; %set the default number of rows in the table column_size = 4; %set the default number of columns in the table table_cells = zeros(row_size, column_size); %create an array to eventually hold the handles for the table cells checkboxes = zeros(1,row_size-1); %create an array to eventually hold the handles for the check boxes

%setup the table cells for row_index = 1:row_size for column_index = 1:column_size table_cells(row_index, column_index) = uicontrol('style', 'pushbutton', 'string', '', 'background', getappdata(0, 'file_missing_color'), 'Enable', 'off'); %create the cells and store their handles end end set(table_cells(1,1), 'Visible', 'off'); %set the top left corner cell to invisible

%create a column of check boxes that can be used to select images for row_index = 2:row_size checkboxes(row_index-1) = uicontrol('style', 'checkbox', 'Enable', 'off'); end

set_table_cells(table_cells); %save the handles of the table cells set_table_checks(checkboxes); %save the handles of the check boxes

blue_color = getappdata(0, 'blue_window_background'); set(handles.figure1, 'Color', blue_color); %set the background color of the main figure figure_position = get(handles.figure1, 'Position'); set(handles.figure1, 'Position', figure_position(1), figure_position(2), 560, figure_position(4)); %set the figure size to fit all the toggle buttons in the toolbar set(handles.figure1, 'ResizeFcn', @(s,e) window_size_changed(handles)); %determine what happens when the window resizes window_size_changed(handles); %set objects in the window to their proper positions set(handles.generator_button, 'Callback', @(s,e) generate_image_Callback(handles)); %setup the button to accept button presses set(handles.generator_button, 'Enable', 'off', 'Visible', 'off'); %disable and hide the button until it is needed set(handles.auto_align, 'Callback', @(s,e) linear_transform_Callback(handles)); set(handles.auto_warp, 'Callback', @(s,e) warp_Callback(handles)); set(handles.transform_all_option, 'Callback', @(s,e) transform_all_Callback(handles)); set(handles.weight_value, 'Callback', @(s,e) weight_button_pressed(handles)); %set(handles.export_all, 'Callback', @(s,e) weight_button_pressed(handles));

%set the default weight selections set(handles.flat_weight_toggle, 'State', 'on'); set(handles.automatic_weight_toggle, 'State', 'off'); set(handles.custom_weight_toggle, 'State', 'off'); set(handles.weight_value, 'Style', 'text', 'Visible', 'on'); set(table_cells, 'Enable', 'off');

%set the default heatmap selections set(handles.normal_view_toggle, 'State', 'on'); set(handles.range_view_toggle, 'State', 'off'); set(handles.normalized_view_toggle, 'State', 'off');

initial_dataset = getappdata(0, 'initial_dataset'); if ~isempty(initial_dataset) open_option_Callback(hObject, eventdata, handles, initial_dataset) end

%colormap(gray(256)); colormap(jet(256)); %set the default colormap set(handles.figure1, 'Name', 'Spinal Map', 'NumberTitle', 'off'); %set the name of the main window set_toolbar_enabled(handles, 'off');

handles.HeatMapColor = 1; guidata(hObject, handles);

function Toggle_HeatMap_Callback(hObject,eventdata,handles)

handles.HeatMapColor = 1 - handles.HeatMapColor; %Toggles between 1 and 0

if(handles.HeatMapColor) colormap(jet(256)); else colormap((gray(256))); A = getimage(handles.figure1);

B = double(A); B = B./max(max(B)); C = contourc(B,3);
figure;imshow(A); hold on; plot(C(1,:),C(2,:),'k.','MarkerSize',3); %figure; imcontour(handles.figure1,3); end

guidata(hObject, handles);

% --- Outputs from this function are returned to the command line. function varargout = InjectionAnalyzer_OutputFcn(~, ~, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure varargout{1} = handles.output;

% -------------------------------------------------------------------- % -------------------------------------------------------------------- %Global Variables' Set And Get Functions % -------------------------------------------------------------------- % --------------------------------------------------------------------

% -------------------------------------------------------------------- function set_stored_largest(column, largest_pixel, image_version) if column == 0 if strcmp(image_version, 'O') setappdata(0, 'largest_original_pixel', largest_pixel); elseif strcmp(image_version, 'T') setappdata(0, 'largest_linear_transform_pixel', largest_pixel); else setappdata(0, 'largest_warp_pixel', largest_pixel); end else if strcmp(image_version, 'O') largest_array = getappdata(0, 'largest_original_pixel_array'); array_name = 'largest_original_pixel_array'; elseif strcmp(image_version, 'T') largest_array = getappdata(0, 'largest_linear_transform_pixel_array'); array_name = 'largest_linear_transform_pixel_array'; else largest_array = getappdata(0, 'largest_warp_pixel_array'); array_name = 'largest_warp_pixel_array'; end

largest_array{column} = largest_pixel; setappdata(0, array_name, largest_array); end

% -------------------------------------------------------------------- function stored_largest = get_stored_largest(column, image_version) %if column == 0, then the program is using the largest pixel out of all the %images in the directory, otherwise it is only comparing for a single %column

if column == 0 if strcmp(image_version, 'O') stored_largest = getappdata(0, 'largest_original_pixel'); elseif strcmp(image_version, 'T') stored_largest = getappdata(0, 'largest_linear_transform_pixel'); else stored_largest = getappdata(0, 'largest_warp_pixel'); end else if strcmp(image_version, 'O') largest_array = getappdata(0, 'largest_original_pixel_array'); elseif strcmp(image_version, 'T') largest_array = getappdata(0, 'largest_linear_transform_pixel_array'); else largest_array = getappdata(0, 'largest_warp_pixel_array'); end

if isempty(largest_array) || size(largest_array, 2) < column stored_largest = 0; else if isempty(largest_array{column}) stored_largest = 0; else stored_largest = largest_array{column}; end end end

% -------------------------------------------------------------------- function reset_stored_largest(image_version) if nargin < 1, image_version = []; end

if strcmp(image_version, 'O') setappdata(0, 'largest_original_pixel', 0); setappdata(0, 'largest_original_pixel_array', {}); elseif strcmp(image_version, 'T') setappdata(0, 'largest_linear_transform_pixel', 0); setappdata(0, 'largest_linear_transform_pixel_array', {}); elseif strcmp(image_version, 'W') setappdata(0, 'largest_warp_pixel', 0); setappdata(0, 'largest_warp_pixel_array', {}); else setappdata(0, 'largest_original_pixel', 0); setappdata(0, 'largest_linear_transform_pixel', 0); setappdata(0, 'largest_warp_pixel', 0); setappdata(0, 'largest_original_pixel_array', {}); setappdata(0, 'largest_linear_transform_pixel_array', {}); setappdata(0, 'largest_warp_pixel_array', {}); end

setappdata(0, 'updated_averaged_images_original', {}); setappdata(0, 'updated_averaged_images_linear_transform', {}); setappdata(0, 'updated_averaged_images_warp', {});

% -------------------------------------------------------------------- function set_table_cells(table_handle) %sets the array of handles for uicontrol objects, used to display the cells of a %table. The table displays all of the aviailable images global images_table_cells_handles; images_table_cells_handles = table_handle;

% -------------------------------------------------------------------- function table_cells = get_table_cells() %returns an array of handles for uicontrol objects, used to display the cells of a %table. The table displays all of the aviailable images global images_table_cells_handles; table_cells = images_table_cells_handles;

% -------------------------------------------------------------------- function set_table_checks(table_checks) %sets the array of handles for the check boxes next to the table global table_checkboxes; table_checkboxes = table_checks;

% -------------------------------------------------------------------- function table_checks = get_table_checks() %returns the array containing the handles of the table's check boxes global table_checkboxes; table_checks = table_checkboxes;

% -------------------------------------------------------------------- function set_x_marker(image_text) %set the handle of the x_marker global x_marker; x_marker = image_text;

% -------------------------------------------------------------------- function marker_handle = get_x_marker() %get the handle of the x_marker global x_marker; marker_handle = x_marker;

% -------------------------------------------------------------------- function set_all_marker_positions(struct_of_marker_positions) %save an entire array to the global variable global text_position_markers; text_position_markers = struct_of_marker_positions;

% -------------------------------------------------------------------- function set_marker_positions(marker_index, new_x, new_y) %change the position of a single marker global text_position_markers; %stores the letter, x, and y positions of the markers

%set the marker's x and y position text_position_markers(marker_index).x_position = new_x; text_position_markers(marker_index).y_position = new_y;

text_position_markers = get_cleaned_references(text_position_markers);

% -------------------------------------------------------------------- function marker_labels = get_marker_labels() marker_labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';

% -------------------------------------------------------------------- function marker_positions = get_marker_positions() %return a struct array consisting of fields: letter(character), %x_position(double), and y_position(double) global text_position_markers; %stores the letter, x, and y positions of the markers marker_positions = text_position_markers;

% -------------------------------------------------------------------- function set_file_name(file_to_convert) %get the name and extension of the file and save it ~, file_name, file_extension = fileparts(file_to_convert); file_name = strcat(file_name, file_extension); setappdata(0, 'current_file', file_name);

% -------------------------------------------------------------------- function set_file_path(current_file) %sets the path, name, and extension of the current file global current_file_path; current_file_path = current_file; set_file_name(current_file);

% -------------------------------------------------------------------- function current_file = get_file_path() %returns the path, name, and extension of the current file global current_file_path; current_file = current_file_path;

% -------------------------------------------------------------------- function set_alternate_file_path(alternate_file) %string containing the path, name, and extension of the alternate file global alternate_file_path; alternate_file_path = alternate_file;

% -------------------------------------------------------------------- function alternate_file = get_alternate_file_path() %string containing the path, name, and extension of the alternate file global alternate_file_path; alternate_file = alternate_file_path;

% -------------------------------------------------------------------- function set_alternate_reference_view(alternate_reference_view) %stores whether the reference view of the alternate image is on or off global view_alternate_reference_points; view_alternate_reference_points = alternate_reference_view;

% -------------------------------------------------------------------- function alternate_reference_view = get_alternate_reference_view() %stores whether the reference view of the alternate image is on or off global view_alternate_reference_points; alternate_reference_view = view_alternate_reference_points;

% -------------------------------------------------------------------- function set_alternate_reference_mover(alternate_reference_mover) %stores whether the reference mover of the alternate image is on or off global move_alternate_reference_points; move_alternate_reference_points = alternate_reference_mover;

% -------------------------------------------------------------------- function alternate_reference_move = get_alternate_reference_mover() %stores whether the reference mover of the alternate image is on or off global move_alternate_reference_points; alternate_reference_move = move_alternate_reference_points;

% -------------------------------------------------------------------- function set_alternate_reference_deleter(alternate_reference_deleter) %stores whether the reference deleter of the alternate image is on or off global delete_alternate_reference_points; delete_alternate_reference_points = alternate_reference_deleter;

% -------------------------------------------------------------------- function alternate_reference_deleter = get_alternate_reference_deleter() %stores whether the reference deleter of the alternate image is on or off global delete_alternate_reference_points; alternate_reference_deleter = delete_alternate_reference_points;

% -------------------------------------------------------------------- function set_alternate_version(image_version) %stores whether the alternate image is the original('O'), linear transform('T'), or %warped('W') version global version_of_the_alternate_image; version_of_the_alternate_image = image_version;

% -------------------------------------------------------------------- function alternate_version = get_alternate_version() %stores whether the alternate image is the original('O'), linear transform('T'), or %warped('W') version global version_of_the_alternate_image; alternate_version = version_of_the_alternate_image;

% -------------------------------------------------------------------- function set_alternate_type(image_type) %stores whether the alternate image is a composite('C'), dark('D'), %fluorescent('F'), heatmap('H'), or axon('A') image global type_of_the_alternate_image; type_of_the_alternate_image = image_type;

% -------------------------------------------------------------------- function alternate_type = get_alternate_type() %stores whether the alternate image is a composite('C'), dark('D'), %fluorescent('F'), heatmap('H'), or axon('A') image global type_of_the_alternate_image; alternate_type = type_of_the_alternate_image;

% -------------------------------------------------------------------- function set_alternate_position(selected_position) %set the selected cell position for the alternate image global alternate_cell_selected_position; alternate_cell_selected_position = selected_position;

% -------------------------------------------------------------------- function alternate_position = get_alternate_position() %return the selected cell position for the alternate image global alternate_cell_selected_position; alternate_position = alternate_cell_selected_position;

% -------------------------------------------------------------------- function set_alternate_data(image_data) %returns an MxNx3 array of image data of the alternate image global alternate_image_data; alternate_image_data = image_data;

% -------------------------------------------------------------------- function set_alternate_ideal(toggle_state) %set the state of the alternate ideal_view state global ideal_view_toggle_state; ideal_view_toggle_state = toggle_state;

% -------------------------------------------------------------------- function alternate_ideal = get_alternate_ideal() %return the state of the alternate ideal_view state global ideal_view_toggle_state; alternate_ideal = ideal_view_toggle_state;

% -------------------------------------------------------------------- function set_alternate_mask(toggle_state) %set the state of the alternate view_mask state global view_mask_toggle_state; view_mask_toggle_state = toggle_state;

% -------------------------------------------------------------------- function alternate_mask = get_alternate_mask() %set the state of the alternate view_mask state global view_mask_toggle_state; alternate_mask = view_mask_toggle_state;

% -------------------------------------------------------------------- function alternate_data = get_alternate_data() %returns an MxNx3 array of image data of the alternate image global alternate_image_data; alternate_data = alternate_image_data;

% -------------------------------------------------------------------- function set_current_data(current_data) %save an MxNx3 array of image data of the currently displayed image global current_image_data; current_image_data = current_data;

% -------------------------------------------------------------------- function current_data = get_current_data() %save an MxNx3 array of image data of the currently displayed image global current_image_data; current_data = current_image_data;

% -------------------------------------------------------------------- function set_alternate_references(alternate_references) %save the reference points of the alternate image global reference_point_positions; reference_point_positions = alternate_references;

% -------------------------------------------------------------------- function alternate_references = get_alternate_references() %get the reference points of the alternate image global reference_point_positions alternate_references = reference_point_positions;

% -------------------------------------------------------------------- function set_alternate_heatmap(alternate_heatmap) global heatmap_image_view; heatmap_image_view = alternate_heatmap;

% -------------------------------------------------------------------- function alternate_heatmap = get_alternate_heatmap() global heatmap_image_view; alternate_heatmap = heatmap_image_view;

% -------------------------------------------------------------------- function set_switching_flag(switching_flag) %if the program is currently switching images, set to 1 %else the switching_flag should be 0 %prevents the program from using update_image to display an image global is_program_switching; is_program_switching = switching_flag;

% -------------------------------------------------------------------- function switching_flag = get_switching_flag() %if the program is currently switching images, return 1 %else the switching_flag should be 0 %prevents the program from using update_image to display an image global is_program_switching; if isempty(is_program_switching) is_program_switching = 0; end switching_flag = is_program_switching;

% -------------------------------------------------------------------- function set_selected_position(cell_position) %set the 2x1 array containing the row and column number of the selected cell global selected_cell_position; selected_cell_position = cell_position;

% -------------------------------------------------------------------- function cell_position = get_selected_position() %get the 2x1 array containing the row and column number global selected_cell_position; cell_position = selected_cell_position;

% -------------------------------------------------------------------- % -------------------------------------------------------------------- %Menu Bar Functions % -------------------------------------------------------------------- % --------------------------------------------------------------------

% -------------------------------------------------------------------- function open_option_Callback(~, ~, handles, directory_name) % hObject handle to open_option (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

%code for retrieving only files from a selected directory and populating the listbox if nargin < 4, directory_name = ''; end if isempty( directory_name ) directory_name = uigetdir('*.*'); %opens a dialog window that allows the user to select a desired directory to load images from. end

%check that a directory was selected properly, then proceed to setup the %GUI and display the first image if directory_name ~= 0 set(handles.generator_button, 'Visible', 'on', 'Enable', 'off', 'String', 'Loading...'); drawnow setappdata(0, 'main_directory', directory_name);

%reset some variables and buttons reset_alternate(); setappdata(0, 'checkbox_preferences_file', ''); set(handles.reference_viewer, 'State', 'off'); set(handles.view_ideal, 'State', 'off'); set(handles.export_option, 'Enable', 'on'); set(handles.import_axon_positions_option, 'Enable', 'on'); set(handles.composite_view_toggle, 'State', 'on'); %reset the view type set(handles.original_view_toggle, 'State', 'on'); %reset the view version set(handles.flat_weight_toggle, 'State', 'on'); %reset the weighting to flat set(handles.normal_view_toggle, 'State', 'on'); %reset the heatmap view
path_to_files = strcat(directory_name, filesep); %the filepath including the end file separator available_files = get_image_files(path_to_files, getappdata(0, 'valid_image_extensions'), getappdata(0, 'valid_image_identifiers')); available_files = {available_files.name}; %removes the path of the files in the list
specimen_files = cellfun(@(x)regexp(x, '_', 'split'), available_files, 'UniformOutput', false); %split the file name into its 3 parts number_of_files = size(specimen_files, 2); %get the array size of specimen_files for use in seperating the nested array
specimen_names = {number_of_files}; %an array to hold the nested array of specimen_files bone_names = {number_of_files}; %an array to hold the names of the bones list_of_bones = []; %hold the unique names of the bones image_identifiers_found = {number_of_files};
list_of_malformations = get_incorrect_file_names(directory_name); if ~isempty(list_of_malformations) error_message = 'Malformed file names found in the directory, file names should be formatted in one of the following manners. "Animal_LevelIdentifer.extension" or "Animal_X_LevelIdentifier.extension". The animal and level may be any combination of letters, numbers, and/or capitalizations. The Identifier is a single capitalized letter. The identifier must be A, C, D, or F. The extension can be any image extension and can contain capitalizations. The X represents any combination of letters, numbers, or underscores the user wishes to add to the file name. The program does not use the information in X to discern between files. It is for user information only. The animal, level, and identifier are used by the program to organize and find image files. If you wish to use these files in the program, please correct the errors and re-open the directory.'; error_window = errordlg(error_message, 'Malformed Names'); set(error_window, 'WindowStyle', 'modal'); uiwait(error_window); %wait for the dialog box to close before running to completion end
%get all of the specimen_names found in the directory for x = 1:number_of_files specimen_names{x} = specimen_files{x}{1};
~, bone_names{x}, ~ = fileparts(specimen_files{x}{end}); image_identifiers_found{x} = bone_names{x}(length(bone_names{x})); bone_names{x}(length(bone_names{x})) = ''; end
if ~isempty(specimen_names) && ~isempty(bone_names) && iscellstr(specimen_names) && iscellstr(bone_names) %remove duplicates in each array specimen_names = unique(specimen_names); specimen_names, ~ = sort_nat(specimen_names, 'ascend'); specimen_names{end + 1} = 'Avg'; %put 'Avg' into the array of specimens, it will not be found otherwise list_of_bones = unique(bone_names); list_of_bones, ~ = sort_nat(list_of_bones, 'ascend'); number_of_bones = size(list_of_bones, 2); image_identifiers_found = unique(image_identifiers_found);
%reorder the bones so that empty bone names will appear on the %right of the table for bone_index = 1:number_of_bones if isempty(list_of_bones{bone_index}) for array_index = bone_index:number_of_bones - 1 list_of_bones{array_index} = list_of_bones{array_index+1}; list_of_bones{array_index+1} = '---'; end end end else number_of_files = 0; end
if number_of_files > 0 table_cells = get_table_cells(); checkboxes = get_table_checks(); number_of_specimens = size(specimen_names, 2); %get the number of specimens found in the directory
save_directory_name = 'program_save_files'; subdirectory_location = strcat(directory_name, filesep, save_directory_name);
%if the save directory does not exist, create it if ~exist(subdirectory_location, 'dir') mkdir(directory_name, save_directory_name); end
setappdata(0, 'save_directory', subdirectory_location);
path_to_saves = strcat(subdirectory_location, filesep); %the filepath including the end file separator save_files = get_image_files(path_to_saves, getappdata(0, 'valid_image_extensions'), getappdata(0, 'valid_image_identifiers')); save_files = {save_files.name}; %removes the path of the files in the list
save_names = cellfun(@(x)regexp(x, '_', 'split'), save_files, 'UniformOutput', false); %split the file name into its 3 parts number_of_saves = size(save_names, 2); %get the array size of specimen_files for use in seperating the nested array
list_of_averages = {number_of_saves}; average_count = 0;
%get all of the specimen_names found in the directory for x = 1:number_of_saves if strcmp(save_names{x}, 'Avg') average_count =+ 1; list_of_averages(average_count) = save_files{x}; end end
total_averages = size(image_identifiers_found, 2); %how many averaged images should be in the directory
if ismember('I', image_identifiers_found) total_averages = total_averages - 1; end
total_averages = total_averages * number_of_bones;
%if Avg is not a specimen name, create it and add it to the array of %specimen_names if average_count ~= total_averages && total_averages > 0 image_identifiers = 'C', 'D', 'F'; %create an array of the valid image_identifiers to search for image_names = []; %create an empty array that will hold the combined bone names and image_identifiers
for id_index = 1:size(image_identifiers, 2) temp_names = strcat(list_of_bones, image_identifiers(id_index)); %combine the bone names and image_identifiers and store the result in a temporary array image_names = cat(2, image_names, temp_names); %add the new names to the array of image_names end
%cycle through the available_files and get the index of %the first image found for image_index = 1:size(image_names, 2) averaged_image_name = strcat(path_to_saves, 'Avg_', image_names{image_index}, '.png'); %create an averaged_images name
if ~exist(averaged_image_name, 'file') for file_index = 1:number_of_files if strfind(available_files{file_index}, image_names{image_index}) found_image = strcat(path_to_files, available_files{file_index}); %combine the file path and the file name image_data = imread(found_image); %get the data from the image imwrite(image_data, averaged_image_name); %put the image data from the fluorescent image into a new image file average_count = average_count + 1; break; end end end if average_count >= total_averages break; end end end
%resize the table and checkboxes, if necessary column_size = number_of_bones + 1; row_size = number_of_specimens + 1; if row_size ~= size(table_cells, 1) || column_size ~= size(table_cells, 2) set(table_cells(:,:), 'Visible', 'off', 'Enable', 'off'); set(checkboxes(:), 'Visible', 'off', 'Enable', 'off'); table_cells = zeros(row_size, column_size); checkboxes = zeros(1, row_size - 1);
%create new checkboxes for row_index = 1:row_size-1 checkboxes(row_index) = uicontrol('style', 'checkbox', 'Visible', 'off', 'Enable', 'off', 'Value', 1); end
%create the table cells for row_index = 1:row_size for column_index = 1:column_size table_cells(row_index, column_index) = uicontrol('style', 'pushbutton', 'string', '', 'background', getappdata(0, 'file_found_color'), 'Visible', 'on', 'Enable', 'on'); end end
set_table_cells(table_cells); set_table_checks(checkboxes); end
%cycle through the list_of_bones and add them to the table for column_index = 1:size(list_of_bones, 2) set(table_cells(1, column_index+1), 'String', list_of_bones{column_index}, 'Visible', 'on'); end
set(table_cells(:,:), 'Visible', 'on', 'Enable', 'on', 'Fontweight', 'bold'); set(table_cells(1,1), 'Visible', 'off'); %hide the cell in the top left corner
set_selected_position(2 2); %set the defualt selected cell set(table_cells(2:end, 2:end), 'String', '', 'Enable', 'on'); %remove all text from the center cells, the ones that do not display specimen or bone names set(table_cells(2, 2), 'backgroundcolor', getappdata(0, 'original_selected_color'), 'String', get_cell_text(handles)); %set the color of the first cell
%move the Avg and Ideal names to the bottom of the table set(table_cells(end, 1), 'String', 'Avg'); for specimen_index = size(specimen_names, 2):-1:1 if strcmp(specimen_names{specimen_index}, 'Avg') specimen_names(specimen_index) = ''; elseif strcmp(specimen_names{specimen_index}, 'Ideal') specimen_names(specimen_index) = ''; set(table_cells(end-1, 1), 'String', 'Ideal'); end end
%fill the table rows in the first column with the specimen names %and set the appropriate number of checkboxes to visible for row_index = 2:size(specimen_names, 2) + 1 set(table_cells(row_index, 1), 'String', specimen_names{row_index - 1}); set(checkboxes(row_index-1), 'Visible', 'on', 'Enable', 'on', 'Value', 1); end
%get the first image specimen_name = get(table_cells(2, 1), 'String'); bone_name = get(table_cells(1, 2), 'String'), get_image_type(handles); image_file_name = get_image_file(handles, specimen_name, bone_name, 'O');
%display the first image if isempty(image_file_name) find_image_file(handles); else set_file_path(image_file_name); %store the first image, this is needed for find_image_file to work properly end
update_image(handles); set(handles.figure1, 'WindowKeyPressFcn', @(s,e) key_press_detected(handles.figure1, handles)); %set the window to register key presses set(handles.figure1, 'DeleteFcn', @(s,e) window_close_detected(handles.figure1, handles));
set(checkboxes(1:end), 'Callback', @(s,e)checkbox_clicked_detected(handles.figure1, handles)); set_toolbar_enabled(handles, 'on'); checkbox_save_file = strcat(subdirectory_location, filesep, 'user_checkbox_preferences.mat'); %the file that saves which check boxes were not selected setappdata(0, 'checkbox_preferences_file', checkbox_save_file);
%setup each table cell to the same callback, but pass its own %handle for row_index = 1:size(table_cells, 1) for column_index = 1:size(table_cells, 2) set(table_cells(row_index, column_index), 'Callback', @(s,e) table_clicked_detected(table_cells(row_index, column_index), handles)); end end
%if the checkbox preferences save file does not exist, create it and save a structure %array to it if ~exist(checkbox_save_file, 'file') checkbox_preferences = struct('specimen_name', {}); %create an empty structure array to hold information about the undesired image save(checkbox_save_file, 'checkbox_preferences'); %save the structure array to the file, the file will automatically be created end
%set all the cells in the first row and column to file found color %and prevent the user from clicking on them set(table_cells(1, 1:end), 'backgroundcolor', getappdata(0, 'file_found_color'), 'Enable', 'inactive'); set(table_cells(1:end, 1), 'backgroundcolor', getappdata(0, 'file_found_color'), 'Enable', 'inactive');
update_table_cells(handles); window_size_changed(handles); %properly adjust the table to its new size
%if the weight values save file does not exist, create it and save a structure array to it calculated_file = strcat(subdirectory_location, filesep, 'fluorescent_weight.mat'); setappdata(0, 'fluorescent_weight_path', calculated_file); custom_file = strcat(subdirectory_location, filesep, 'custom_weights.mat'); setappdata(0, 'custom_weight_path', custom_file);
if ~exist(calculated_file, 'file') original_weights, linear_transform_weights, warp_weights = get_default_weights(0); save(calculated_file, 'original_weights', 'linear_transform_weights', 'warp_weights'); end
if ~exist(custom_file, 'file') original_weights, linear_transform_weights, warp_weights = get_default_weights(1); save(custom_file, 'original_weights', 'linear_transform_weights', 'warp_weights'); end
update_summary(handles);
list_of_mismatches = 'Some images do not match the size of their associated ideal image, please make the images the same size: '; mismatch_count = 0; level_names{1} = ''; missing_count = 0;
%cycle through all the images and determine if any do not match the %size of the ideal image for column_index = 2:size(table_cells, 2) bone_name = get(table_cells(1, column_index), 'String');
for row_index = 2:size(table_cells, 1) specimen_name = get(table_cells(row_index, 1), 'String'); if ~is_ideal_or_avg(specimen_name) complete_bone = bone_name, get_image_type(handles); specimen_file = get_image_file(handles, specimen_name, complete_bone); ideal_image = get_image_file(handles, 'Ideal', bone_name, 'I', 'O');
if ~isempty(specimen_file) & ~is_valid_size(handles, specimen_file) & ~isempty(ideal_image) mismatch_count = mismatch_count + 1; ~, specimen_name, ~ = fileparts(specimen_file); list_of_mismatches = list_of_mismatches, specimen_name, ', '; elseif isempty(ideal_image) missing_count = missing_count + 1; level_names{missing_count} = bone_name; end end end end
%display an error message with a list of mismatched ideal images if mismatch_count list_of_mismatches(end:-1:end-1) = ''; %remove the last ', ' from the array errordlg(list_of_mismatches); end
if missing_count level_names = unique(level_names); error_message = 'Some bone levels do not have a corresponding ideal image: '; for missing_index = 1:size(level_names, 2) - 1 error_message = error_message, level_names{missing_index}, ', '; end error_message = error_message, level_names{end}; errordlg(error_message); end else table_cells = get_table_cells(); checkboxes = get_table_checks(); set(table_cells(1:2, 1:end), 'Visible', 'on', 'String', '', 'background', getappdata(0, 'file_missing_color')); if size(table_cells, 1) > 2 set(table_cells(3:end, 1:end), 'Visible', 'off', 'String', ''); end
set(checkboxes(1:end), 'Visible', 'off'); set(checkboxes(1), 'Value' , 0, 'Enable', 'off', 'Visible', 'on');
setappdata(0, 'main_directory', ''); set_file_path(''); %set the file path to empty title(''); %set the axes title to empty plot(1,1,'*', 'Color', 'c'); %draw a blue star on the screen set(handles.axes1, 'Visible', 'off'); %turn off the axes numbers set_toolbar_enabled(handles, 'off'); %turn off the toolbar buttons update_summary(handles);
if isempty(list_of_malformations) errordlg('No image files found'); end end set(handles.generator_button, 'Visible', 'off'); end

% -------------------------------------------------------------------- function incorrect_file_names = get_incorrect_file_names(directory_name) list_of_files = {}; file_extensions = getappdata(0, 'valid_image_extensions'); image_identifiers = getappdata(0, 'valid_image_identifiers');

for extension_index = 1:size(file_extensions, 2) search_string = strcat(directory_name, filesep, '*', file_extensions{extension_index}); found_files = dir(search_string); found_files = {found_files.name};

list_of_files = cat(2, list_of_files, found_files); end

missing_underscores = {}; missing_identifiers = {}; missing_animal_names = {}; missing_level_names = {};

for file_index = 1:size(list_of_files, 2) ~, file_name, ~ = fileparts(list_of_files{file_index});

if isempty(strfind(file_name, '_')) missing_underscores{end+1} = list_of_files{file_index}; else name_parts = regexp(file_name, '_', 'Split'); if isempty(name_parts(1)) missing_animal_names{end+1} = list_of_files{file_index}; end
if isempty(name_parts{end}) || size(name_parts, 2) < 2 missing_level_names{end+1} = list_of_files{file_index}; else bone_name = name_parts{end}; image_identifier = bone_name(end); identifier_found = 0;
for identifier_index = 1:size(image_identifiers, 2) if strcmp(image_identifier, image_identifiers{identifier_index}) identifier_found = 1; break; end end
if ~identifier_found missing_identifiers{end+1} = list_of_files{file_index}; end end end end

%construct the array of strings that contain the misconfigured file %names error_strings = cell(1); error_strings(:,:) = [];

error_strings = construct_incomplete_files_list('*** Missing Underscores ***', missing_underscores, error_strings); error_strings = construct_incomplete_files_list('*** Missing Identifiers ***', missing_identifiers, error_strings); error_strings = construct_incomplete_files_list('*** Missing Animal Names ***', missing_animal_names, error_strings); error_strings = construct_incomplete_files_list('*** Missing Level Names ***', missing_level_names, error_strings);

incorrect_file_names = cell(size(error_strings));

if ~isempty(error_strings) for error_index = 1:size(error_strings, 2) incorrect_file_names{error_index} = char(error_strings{error_index}); end end

% -------------------------------------------------------------------- function incomplete_files_list = construct_incomplete_files_list(error_title, name_array, files_list) if ~isempty(name_array) files_list{1, end+1} = char(error_title); incomplete_files_list = cat(2, files_list, name_array); else incomplete_files_list = files_list; end

% -------------------------------------------------------------------- function import_axon_positions_option_Callback(~, ~, handles) import_directory = getappdata(0, 'main_directory'); %use the main directory as a starting point for the open directory gui

if ~isempty(import_directory) && exist(import_directory, 'dir') directory_name = uigetdir(import_directory, 'Import axons');

if directory_name ~= 0 set(handles.generator_button, 'Visible', 'on', 'Enable', 'off', 'String', 'Loading...'); drawnow axon_files = strcat(directory_name, filesep, '*_*A.csv'); available_files = dir(axon_files);
if ~isempty(available_files) setappdata(0, 'axon_directory', directory_name); axon_file = strcat(getappdata(0, 'save_directory'), filesep, 'axon_weights.mat'); setappdata(0, 'axon_weight_path', axon_file); if ~exist(axon_file, 'file') original_weights, linear_transform_weights, warp_weights = get_default_weights(0); save(axon_file, 'original_weights', 'linear_transform_weights', 'warp_weights'); end
%find all the axon averaged images averaged_files = strcat(getappdata(0, 'save_directory'), filesep, 'Avg_*A.*'); averaged_images = dir(averaged_files); averaged_images = {averaged_images.name};
dark_files = strcat(getappdata(0, 'main_directory'), filesep, '*D.*'); dark_images = dir(dark_files); dark_images = {dark_images.name};
table_cells = get_table_cells();
for column_index = 2:size(table_cells, 2) if ~strcmp('---', get(table_cells(1, column_index), 'String')) average_found = 0;
%check that there exists an averaged image for each %bone in the columns for file_index = 1:size(averaged_images, 2) ~, file_name, ~ = fileparts(averaged_images{file_index}); name_parts = regexp(file_name, '_', 'Split'); bone_name = name_parts{end}(1:end-1); %remove the image identifier
if strcmp(bone_name, get(table_cells(1, column_index), 'String')); average_found = 1; break; end end
%if an average does not exist, create a file using the %first valid dark image found if ~average_found valid_extensions = getappdata(0, 'valid_image_extensions'); averaged_name = strcat(getappdata(0, 'save_directory'), filesep, 'Avg_', get(table_cells(1, column_index), 'String'), 'A.png');
for file_index = 1:size(dark_images, 2) ~, ~, file_extension = fileparts(dark_images{file_index}); dark_image_found = 0;
%if the file has a valid image extension, use %it to create the averaged file for extension_index = 1:size(valid_extensions) if strcmp(file_extension, valid_extensions{extension_index}) found_file = strcat(getappdata(0, 'main_directory'), filesep, dark_images{file_index}); image_data = imread(found_file); imwrite(image_data, averaged_name); dark_image_found = 1; break; end end
if dark_image_found, break; end end end end end else errordlg('No axon files found'); end
set(handles.axon_view_toggle, 'Enable', 'on'); set(handles.generator_button, 'Visible', 'off'); end end

% -------------------------------------------------------------------- function export_option_Callback(~, ~, handles) export_viewed_image(handles);

function print_image(I,fstr)

f = figure; imshow(I); print(f,'-dpng','-r600',fstr); close(f);

% -------------------------------------------------------------------- function export_viewed_image(handles) if exist(get_file_path(), 'file') && ~isempty(get_current_data()) %return the user to the directory of their last save or to their image %directory if ~isempty(getappdata(0, 'previous_export_directory')) export_directory = getappdata(0, 'previous_export_directory'); else export_directory = strcat(getappdata(0, 'main_directory'), filesep); end

%check that the export directory is not empty if ~isempty(export_directory) export_file, export_path, ~ = uiputfile('.png', 'Export Image', export_directory); else export_file, export_path, ~ = uiputfile('.png', 'Export Image'); end
if export_file & ~isempty(export_file) setappdata(0, 'previous_export_directory', export_path); ~, export_name, export_extension = fileparts(export_file);
%make sure the image is saved as a png if (isempty(export_extension) || ~strcmp(export_extension, '.png')) export_file = strcat(export_name, '.png'); end
export_file = strcat(export_path, export_file);
%if the user is in heatmap view, the file needs to be saved with %the colormap if strcmp(get(handles.normal_view_toggle, 'State'), 'on') %imwrite(get_current_data(), export_file,'png','XResolution',2000); print_image(get_current_data(), export_file); else rgb_image = ind2rgb(get_current_data(), colormap);
%get the ideal image ~, bone_name = parse_image_name(get_file_path()); bone_name(end) = 'I'; ideal_image = get_image_file(handles, 'Ideal', bone_name, 'O');
%if the ideal image exists, set the image's background to white if exist(ideal_image, 'file') ideal_data = imread(ideal_image); ideal_data = double(ideal_data);
mask = ideal_data(:, :, 1); mask = cat(3, mask, mask, mask);
%rgb_image = imtranslate(rgb_image,0 -50); % TEMPORARY!!! REMOVE ASAP %rgb_image = flipud(rgb_image);
rgb_image = rgb_image + mask; end
%imwrite(rgb_image, export_file,'png','XResolution',2000); print_image(get_current_data(), export_file); end end end

% -------------------------------------------------------------------- function modify_heatmap_option_Callback(~, ~, ~) colormapeditor; %opens the colormap editor which allows the user to customize the figure's colormap

% -------------------------------------------------------------------- function linear_transform_Callback(handles, image_file) if nargin < 2, image_file = []; end if isempty(image_file), image_file = get_file_path(); end set_gui_enabled(handles, 'off'); specimen_name, bone_name = parse_image_name(image_file); valid_transform = is_valid_transform(handles, specimen_name, bone_name);

%check that the image_references are valid. If they are not, display the %appropriate warning if ~isstruct(valid_transform) set( handles.generator_button, 'string', 'Computing...', 'enable', 'off', 'visible', 'on' ), drawnow ~, image_references, ideal_references = is_different(handles, image_file, 'O'); array_of_files = get_bone_images(image_file); %get an array of paths to bone images

%transpose the reference points for the linear transform function image_references = image_references'; ideal_references = ideal_references';
selected = ~any( ideal_references;image_references == -1, 1 ); extra = get_axon_positions(handles, image_file, 'O'); extra = extra'; ~, converted_image, ~, ~, new_references, extra = estimate_transform_to_reference(ideal_references(:, selected), image_references(:, selected), array_of_files, [], [], extra); %transform the current image to align, as best it can, with the ideal image new_references = get_merged_references(image_references, new_references); new_references = new_references'; extra = extra';
for image_index = 1:size(array_of_files, 1) image_name = array_of_files(image_index, :); %get the path, name, and extension of the file image_name = save_data_and_get_image_name(image_name, converted_image(:,:,:,image_index), extra, 'T'); save_converted_image(image_name, new_references, 'TR'); %save the adjusted reference points to the associated save file end
set_outdated_weight(handles, image_file, 'T'); set_outdated_weight(handles, image_file, 'W'); set(handles.linear_transform_view_toggle, 'State', 'on'); remove_from_updated(handles, image_file); %remove the associated averaged image from the udpated averages list reset_stored_largest('T'); reset_stored_largest('W'); else display_transform_error(handles, valid_transform); end set(handles.linear_transform_view_toggle, 'State', 'on'); set_gui_enabled(handles, 'on');

% -------------------------------------------------------------------- function warp_Callback(handles, image_file) if nargin < 2, image_file = []; end if isempty(image_file), image_file = get_file_path(); end

set_gui_enabled(handles, 'off');

~, image_references, ideal_references = is_different(handles, image_file, 'T');

set( handles.generator_button, 'string', 'Computing...', 'enable', 'off', 'visible', 'on' ), drawnow

array_of_files = get_bone_images(image_file); %get an array of paths to bone images aligned_images = []; %an array that holds image data checked_array = []; %an array of verified files

%change the directory from the main directory to the save directory temp_array = char([]); for image_index = 1:size(array_of_files, 1) image_name = array_of_files(image_index, :); ~, file_name, file_extension = fileparts(image_name); temp_array{image_index} = strcat(getappdata(0, 'save_directory'), filesep, file_name, file_extension); end array_of_files = char(temp_array);

%cycle through the files and determine if each has associated linear %transform data for image_index = 1:size(array_of_files, 1) aligned_data = load_converted_image(array_of_files(image_index, :), 'T'); %attempt to load the linear transform image if ~isempty(aligned_data) aligned_images = cat(4, aligned_images, aligned_data); %save the data to aligned_images checked_array = cat(1, checked_array, array_of_files(image_index,:)); %if the image file has associated linear transform data, save it to the checked_array end end

specimen_name, bone_name = parse_image_name(image_file); valid_transform = is_valid_transform(handles, specimen_name, bone_name);

%check that the image_references are valid. If they are not, display the %appropriate warning if ~isstruct(valid_transform) %clear some of the unused variables to free up space clearvars array_of_files temp_array specimen_name bone_name image_name; clearvars valid_transform file_name file_extension;

extra = get_axon_positions(handles, image_file, 'T');
%set the reference points to a 2xN format for the auto_warp_image %function image_references = image_references'; ideal_references = ideal_references'; extra = extra'; selected = ~any( ideal_references;image_references == -1, 1 );
~, converted_image, ~, ~, extra = auto_warp_image(ideal_references(:, selected), image_references(:, selected), aligned_images, [], [], extra); %warp the current image to fit the ideal image extra = extra'; %switch back to an Nx2 format for storage
%cycle through the image data and save it in the appropriate places for image_index = 1:size(aligned_images, 4) image_name = checked_array(image_index, :); save_data_and_get_image_name(image_name, converted_image(:,:,:,image_index), extra, 'W'); end
set_outdated_weight(handles, image_file, 'W'); set(handles.warp_view_toggle, 'State', 'on'); remove_from_updated(handles, image_file); %remove the associated averaged image from the udpated averages list reset_stored_largest('W'); else display_transform_error(handles, valid_transform); end set(handles.warp_view_toggle, 'State', 'on'); set_gui_enabled(handles, 'on');

% -------------------------------------------------------------------- function image_name = save_data_and_get_image_name(image_name, image_data, extra, image_version) %save the new image's information to the mat file

~, file_name, file_extension = fileparts(image_name); image_name = strcat(getappdata(0, 'save_directory'), filesep, file_name, file_extension); save_converted_image(image_name, image_data, image_version);

axon_code = strcat(image_version, 'A'); save_converted_image(image_name, extra, axon_code);

if strcmp(image_version, 'T') save_converted_image(image_name, 0, 'TO'); save_converted_image(image_name, 1, 'WO'); else save_converted_image(image_name, 0, 'WO'); end

% -------------------------------------------------------------------- function is_different, image_references, ideal_references = is_different(handles, image_file, image_version) %function different = is_different(handles, image_file, image_version) %check that the reference points in each array are not identical

is_different = 0; image_references = []; ideal_references = []; %determine if the image references are different from the ideal references if exist(image_file, 'file') ~, file_name, file_extension = fileparts(image_file); main_path = getappdata(0, 'main_directory'), filesep, file_name, file_extension;

marker_positions = get_image_references(handles, main_path, image_version); %get the image's reference points ideal_image = get_ideal_image(main_path); %get the name of the associated ideal image as a string ideal_references = get_image_references(handles, ideal_image, 'O');
image_references = convert_references_double(marker_positions); ideal_references = convert_references_double(ideal_references);
if ~isequal(size(image_references), size(ideal_references)) | find(~(image_references == ideal_references), 2) is_different = 1; %the two arrays are different end end

% -------------------------------------------------------------------- function bone_images = get_bone_images(image_file) %return an array of file paths to a bone's images %used in auto_align_Callback and auto_warp_Callback functions

if nargin < 1, image_file = ''; end if isempty(image_file), image_file = get_file_path(); end

bone_images = []; %setup an empty array to store all the image files file_path, file_name, ~ = fileparts(image_file); file_name(length(file_name)) = ''; %remove the image identifier

%get all the files that match the image_file file_to_search = strcat(file_path, filesep, file_name, '*'); %the file to search for, get all the files that match the specimen and bone name available_files = dir(file_to_search); %get a list of files from the directory available_files = {available_files.name}; %get only the names and file extensions of the available_files

file_types = getappdata(0, 'valid_image_extensions'); %valid file extensions used to filter out unwanted files

%cycle through the availabe_files to find image files to transform for file_index = 1:size(available_files, 2) ~, file_name, file_extension = fileparts(available_files{file_index}); %if the file_extension matches one of the valid files, determine which %type of image it is and assign it to the appropriate variable if find(ismember(file_types, lower(file_extension))) if find(ismember('C', 'D', 'F', file_name(end))) image_file = file_path, filesep, file_name, file_extension; end

bone_images = cat(1, bone_images, image_file); end end

% -------------------------------------------------------------------- function transform_all_Callback(handles) problem_images = []; image_version = 'T'; table_cells = get_table_cells(); row_size = size(table_cells, 1); column_size = size(table_cells, 2); checkboxes = get_table_checks(); image_types = {'C', 'D', 'F'}; start_position = get_selected_position(); %used to move the selected cell back to its original position previous_existed = 1;

%cycle through all the rows and determine if the image can be transformed for row_index = 2:row_size specimen_name = get(table_cells(row_index, 1), 'String');

if ~strcmp(specimen_name, 'Avg') && ~strcmp(specimen_name, 'Ideal') & ~is_specimen_in_preferences(specimen_name) for column_index = 2:column_size bone_name = get(table_cells(1, column_index), 'String');
if strcmp(get(checkboxes(row_index-1), 'Visible'), 'on') for type_index = 1:size(image_types, 2) complete_bone = bone_name, image_types{type_index}; valid_transform = is_valid_transform(handles, specimen_name, complete_bone, image_version);
if isstruct(valid_transform) & isempty(strfind(valid_transform.error_message, 'was not found in directory')) problem_images{end+1} = strcat(specimen_name, '_', bone_name); end end end end end end

if isempty(problem_images) set_gui_enabled(handles, 'off');

for row_index = 2:row_size for column_index = 2:column_size specimen_name = get(table_cells(row_index, 1), 'String'); bone_name = get(table_cells(1, column_index), 'String') get_image_type(handles); image_file = get_image_file(handles, specimen_name, bone_name, 'O'); if ~is_ideal_or_avg(specimen_name) bone_images = get_bone_images(image_file); else bone_images = []; end
linear_transform_outdated = 0; warp_outdated = 0;
%check that there is at least one file to process if ~isempty(bone_images) for image_index = 1:size(bone_images, 1) image_name = bone_images(image_index, :);
if is_image_outdated(handles, image_name, 'T') linear_transform_outdated = 1; elseif is_image_outdated(handles, image_name, 'W') warp_outdated = 1; end
if linear_transform_outdated & warp_outdated break; end end end
%set the color of the previous cell previous_position = get_selected_position(); if previous_existed set(table_cells(previous_position(1), previous_position(2)), 'backgroundcolor', getappdata(0, 'file_found_color'), 'String', ''); else set(table_cells(previous_position(1), previous_position(2)), 'backgroundcolor', getappdata(0, 'file_missing_color'), 'String', ''); end
%set the new selected position in the table selected_position = row_index, column_index; set_selected_position(selected_position); set(table_cells(selected_position(1), selected_position(2)), 'backgroundcolor', get_selected_cell_color(handles), 'String', get_cell_text(handles));
if ~isempty(image_file) && ~strcmp(specimen_name, 'Avg') && ~strcmp(specimen_name, 'Ideal') previous_existed = 1; mat_file = get_image_file(handles, specimen_name, bone_name, 'T');
%check which images are outdated and perform the appropriate transformations if isempty(mat_file) || linear_transform_outdated set(handles.linear_transform_view_toggle, 'State', 'on'); linear_transform_Callback(handles, image_file); warp_Callback(handles, image_file); elseif warp_outdated set(handles.warp_view_toggle, 'State', 'on'); warp_Callback(handles, image_file); end elseif isempty(image_file) previous_existed = 0; end end end
set(handles.warp_view_toggle, 'State', 'on'); %switch to the warp view and update the image message_handle = msgbox('Program has finished transforming all images', 'Finished'); set(message_handle, 'WindowStyle', 'modal'); %set the message window always on top of the main figure, and prevent the user from using the main window while the message is displayed else problem_images = unique(problem_images); image_list = []; for problem_index = 1:size(problem_images, 2) image_list = image_list, problem_images{problem_index}, ', '; end image_list(end-1:end) = ''; error_message = 'Some images are not ready for transforming: ', image_list;
errordlg(error_message); end

set(table_cells(end, end), 'String', ''); %when the function is finished, the last cell in the table is selected set_selected_position(start_position); update_table_cells(handles); set(handles.generator_button, 'Visible', 'off', 'Enable', 'off');

% -------------------------------------------------------------------- function merged_references = get_merged_references(original_references, new_references) %put the new_references into the original positions

merged_references = original_references; new_reference_count = 0; for reference_index = 1:size(original_references, 2) if original_references(1, reference_index) < 0 || original_references(2, reference_index) < 0 merged_references(:, reference_index) = -1; else new_reference_count = new_reference_count + 1; merged_references(1, reference_index) = new_references(1, new_reference_count); merged_references(2, reference_index) = new_references(2, new_reference_count); end end

% -------------------------------------------------------------------- function set_gui_enabled(handles, gui_enabled) checkboxes = get_table_checks(); table_cells = get_table_cells();

if strcmpi(gui_enabled, 'on') || strcmpi(gui_enabled, 'off') set(handles.file_menu, 'Enable', gui_enabled); set(handles.help_menu, 'Enable', gui_enabled); set_toolbar_enabled(handles, gui_enabled); set(checkboxes(1:end), 'Enable', gui_enabled);

if ~isempty(getappdata(0, 'main_directory')) set(table_cells(2:end, 2:end), 'Enable', gui_enabled); else set(table_cells(2:end, 2:end), 'Enable', 'off'); end
%turn off the zoom and pan to allow the WindowKeyPressFcn to be set %correctly zoom off pan off
%determine if the gui should register key presses if strcmp(gui_enabled, 'off') set(handles.figure1, 'WindowKeyPressFcn', ''); else set(handles.figure1, 'WindowKeyPressFcn', @(s,e) key_press_detected(handles.figure1, handles)); end drawnow end

% -------------------------------------------------------------------- function change_point_size_option_Callback(~, ~, handles) dialog_handle = dialog('Position',300 300 250 150,'Name','Point size'); dialog_message = uicontrol('Parent', dialog_handle,... 'Style','text',... 'ForegroundColor', 'k', ... 'Position',20 80 210 40,... 'String','Enter a size value');

text_field = uicontrol('Parent', dialog_handle,... 'String', getappdata(0, 'axon_point_size'),... 'Style','edit',... 'Position',75 70 100 25);

ok_button = uicontrol('Parent', dialog_handle,... 'Position',50 20 70 25,... 'String','OK',... 'Callback', @(s,e)accept_size_change(handles, text_field, dialog_message, dialog_handle));

cancel_button = uicontrol('Parent', dialog_handle, ... 'Position', 125 20 70 25, ... 'String', 'Cancel', ... 'Callback', @(s,e)cancel_size_change(dialog_handle));

uiwait(dialog_handle); %wait for the dialog box to close before running to completion

% -------------------------------------------------------------------- function accept_size_change(handles, text_field, dialog_message, dialog_handle) user_input = get(text_field, 'String'); valid_number = 1;

try user_input = eval( user_input ); end

if ~isnumeric( user_input ) || numel( user_input ) ~= 1 || ~isreal( user_input ) || isnan( user_input ) || isinf( user_input ) || ~(user_input > 0) valid_number = 0; end

if ~isempty(get_file_path()) && exist(get_file_path(), 'file') && valid_number setappdata(0, 'axon_point_size', user_input);

setappdata(0, 'updated_averaged_images_original', {}); setappdata(0, 'updated_averaged_images_linear_transform', {}); setappdata(0, 'updated_averaged_images_warp', {});
delete(dialog_handle);
if strcmp(get(handles.axon_view_toggle, 'State'), 'on') setappdata(0, 'point_size_changed', 1); update_image(handles); end

elseif ~isnumeric(user_input) || isnan(user_input) || numel(user_input) ~= 1 set(dialog_message, 'ForegroundColor', 'r', 'String', 'Please enter a number'); elseif ~isreal(user_input) set(dialog_message, 'ForegroundColor', 'r', 'String', 'Please enter a real number'); elseif isinf(user_input) set(dialog_message, 'ForegroundColor', 'r', 'String', 'Please don''t enter infinite numbers'); elseif ~(user_input > 0) set(dialog_message, 'ForegroundColor', 'r', 'String', 'The number must be greater than 0'); end

% -------------------------------------------------------------------- function cancel_size_change(dialog_handle) delete(dialog_handle);

% -------------------------------------------------------------------- % -------------------------------------------------------------------- %Toolbar Functions % -------------------------------------------------------------------- % --------------------------------------------------------------------

% -------------------------------------------------------------------- function reference_viewer_OnCallback(~, ~, handles) %toggle button %when on, displays the reference points on the image

if ~strcmp(get_file_path(), '') update_image(handles); end

% -------------------------------------------------------------------- function reference_viewer_OffCallback(~, ~, handles) %toggle button %when off, hides the reference points

set(handles.reference_deleter, 'State', 'off'); %the user should not be able to delete the refernce points if they can't be seen set(handles.reference_mover, 'State', 'off'); %the user should not be able to move the reference points if they can't be seen update_image(handles);

% -------------------------------------------------------------------- function reference_mover_OnCallback(~, ~, handles) %toggle button %when on, allows the user to move reference points by clicking a location %with the mouse and then pressing an letter key on the keyboard

set(handles.reference_deleter, 'State', 'off'); %prevent the user from deleting the reference points when trying to move them set(handles.reference_viewer, 'State', 'on'); %the user should be able to see the reference points if they can be moved

% -------------------------------------------------------------------- function reference_mover_OffCallback(~, ~, ~) %toggle button %when off, prevents the user from moving reference points

%if there is an x marker, remove it if ~isempty(get_x_marker()) && ishandle(get_x_marker()) set(get_x_marker(), 'Position', 100 100, 'String', '', 'Color', getappdata(0, 'missing_selected_color')); %set the text object to empty set_x_marker(''); %remove the x marker handle end

% -------------------------------------------------------------------- function reference_deleter_OnCallback(~, ~, handles) %toggle button %when on, allows the user to delete a reference point by pressing the %computer key associated with the letter on the reference point

set(handles.reference_mover, 'State', 'off'); %prevent the user from moving the reference points when trying to delete them set(handles.reference_viewer, 'State', 'on'); %the user should be able to see the reference points if they can be deleted

% -------------------------------------------------------------------- function view_ideal_OnCallback(~, ~, handles) %toggle button %view the outline of the ideal image on the current image if ~get_switching_flag() update_image(handles); end

% -------------------------------------------------------------------- function view_ideal_OffCallback(~, ~, handles) %toggle button %hide the ideal image if ~get_switching_flag() update_image(handles); end

% -------------------------------------------------------------------- function mask_view_toggle_OnCallback(~, ~, handles) %cover everything except the area marked by the ideal image if ~get_switching_flag() update_image(handles); end

% -------------------------------------------------------------------- function mask_view_toggle_OffCallback(~, ~, handles) %hide the mask if ~get_switching_flag() update_image(handles); end

% -------------------------------------------------------------------- function switch_images_Callback(~, ~, handles) switch_images(handles);

% -------------------------------------------------------------------- function switch_images(handles) set_switching_flag(1); %turn on the switching flag so the toggle buttons don't perform any unnecessary actions

%if the alternate file has not been set, set it to the current file if strcmp(get_alternate_file_path(), '') || ~exist(get_alternate_file_path(), 'file') set_alternate_file_path(get_file_path()); set_alternate_position(get_selected_position()); set_alternate_data(get_current_data()); set_alternate_references(get_marker_positions()); end

%check that the string is not empty and that the file exists if ~strcmp(get_alternate_file_path(), '') && exist(get_alternate_file_path(), 'file') alt_version = get_alternate_version(); %get the image version of the alternate image set_alternate_version(get_image_version(handles)); alt_type = get_alternate_type(); %get the image type of the alternate image alt_heatmap = get_alternate_heatmap(); %get the heatmap view of the alternate image ideal_state = get_alternate_ideal(); %get the state of the alternate view_ideal toggle button set_alternate_ideal(get(handles.view_ideal, 'State')); %save the current state of the view_ideal toggle button mask_state = get_alternate_mask(); %get the state of the alternate mask_view toggle button set_alternate_mask(get(handles.mask_view, 'State')); %save the current state of the mask_view toggle button

%get and set the alternate reference point toggle buttons reference_view_state = get_alternate_reference_view(); set_alternate_reference_view(get(handles.reference_viewer, 'State')); reference_mover_state = get_alternate_reference_mover(); set_alternate_reference_mover(get(handles.reference_mover, 'State')); reference_deleter_state = get_alternate_reference_deleter(); set_alternate_reference_deleter(get(handles.reference_deleter, 'State'));
%switch the alternate file and the current file previous_file = get_file_path(); %temporarily store the current file set_file_path(get_alternate_file_path()); set_alternate_file_path(previous_file);
%switch the alternate position and the current positon previous_position = get_selected_position(); set_selected_position(get_alternate_position()); set_alternate_position(previous_position); selected_cell = get_selected_position();
%switch the alternate references and the current references previous_references = get_marker_positions(); set_all_marker_positions(get_alternate_references); set_alternate_references(previous_references);
%switch the alternate image data and the current image data previous_data = get_current_data(); set_current_data(get_alternate_data()); set_alternate_data(previous_data);
clearvars previous_file previous_references previous_data
%change the background color of the window to indicate the change if isequal(get(handles.figure1, 'Color'), getappdata(0, 'blue_window_background')) set(handles.figure1, 'Color', getappdata(0, 'purple_window_background')); else set(handles.figure1, 'Color', getappdata(0, 'blue_window_background')); end
%determine the appropriate image type and save it if strcmp(get(handles.axon_view_toggle, 'State'), 'on') image_type = 'A'; else image_type = get_image_type(handles); end set_alternate_type(image_type);
%adjust the toggle buttons to display the appropriate type if strcmp(alt_type, 'C') set(handles.composite_view_toggle, 'State', 'on'); elseif strcmp(alt_type, 'D') set(handles.dark_view_toggle, 'State', 'on'); elseif strcmp(alt_type, 'F') set(handles.fluorescent_view_toggle, 'State', 'on'); elseif strcmp(alt_type, 'A') set(handles.axon_view_toggle, 'State', 'on'); end
%update the alternate heatmap if strcmp(get(handles.normal_view_toggle, 'State'), 'on') set_alternate_heatmap('N'); elseif strcmp(get(handles.range_view_toggle, 'State'), 'on') set_alternate_heatmap('R'); else set_alternate_heatmap('E'); end
%adjust the heatmap view buttons if strcmp(alt_heatmap, 'N') set(handles.normal_view_toggle, 'State', 'on'); elseif strcmp(alt_heatmap, 'R') set(handles.range_view_toggle, 'State', 'on'); elseif strcmp(alt_heatmap, 'E') set(handles.normalized_view_toggle, 'State', 'on'); end
%adjust the ideal view button, the elseif will handle an empty %ideal_state if strcmp(ideal_state, 'on') set(handles.view_ideal, 'State', 'on'); elseif strcmp(ideal_state, 'off') set(handles.view_ideal, 'State', 'off'); end
%adjust the mask view button, the elseif will handles an empty %mask_state if strcmp(mask_state, 'on') set(handles.mask_view, 'State', 'on'); elseif strcmp(mask_state, 'off') set(handles.mask_view, 'State', 'off'); end
%switch to the appropriate version toggle button if strcmp(alt_version, 'T') set(handles.linear_transform_view_toggle, 'State', 'on'); elseif strcmp(alt_version, 'W') set(handles.warp_view_toggle, 'State', 'on'); elseif strcmp(alt_version, 'O') set(handles.original_view_toggle, 'State', 'on'); end
update_summary(handles); update_table_cells(handles); table_cells = get_table_cells(); set(table_cells(previous_position(1), previous_position(2)), 'String', ''); %set the string of the unselected cell to empty set(table_cells(selected_cell(1), selected_cell(2)), 'backgroundcolor', get_selected_cell_color(handles), 'String', get_cell_text(handles)); %setup the selected cell
%check if the image is an average that needs updating if strcmp(alt_version, 'T') updated_string = 'updated_averaged_images_linear_transform'; elseif strcmp(alt_version, 'W') updated_string = 'updated_averaged_images_warp'; else updated_string = 'updated_averaged_images_original'; end
old_updated = getappdata(0, updated_string); updated_images = get_updated_averages(handles, old_updated); %check if the averaged image needs updating setappdata(0, updated_string, updated_images); %update the array
%check if there was any change to the array of updated images %if there was, update the averaged image if size(old_updated) ~= size(updated_images) | getappdata(0, 'point_size_changed') setappdata(0, 'point_size_changed', 0); update_image(handles); else draw_image(handles); end
%set the reference view toggle button if ~isempty(reference_view_state) set(handles.reference_viewer, 'State', reference_view_state); end
%set the reference move and delete toggle buttons if strcmp(alt_version, 'T') || strcmp(alt_version, 'W') %the user cannot move or delete reference points while in these %views set(handles.reference_mover, 'State', 'off', 'Enable', 'off') set(handles.reference_deleter, 'State', 'off', 'Enable', 'off'); elseif strcmp(alt_version, 'O') if ~isempty(reference_mover_state) set(handles.reference_mover, 'State', reference_mover_state); end
if ~isempty(reference_deleter_state) set(handles.reference_deleter, 'State', reference_deleter_state); end end
if ~is_ideal_or_avg() set(handles.auto_manipulate, 'Enable', 'on'); end elseif ~exist(get_alternate_file_path(), 'file') %if the image file does not exist, set the alternate variables to empty set_alternate_file_path(''); set_alternate_version(''); set_alternate_type(''); set_alternate_heatmap(''); set_alternate_position([]); end set_switching_flag(0); %turn off the switching flag to resume normal toggle button operation

% -------------------------------------------------------------------- function original_view_toggle_OffCallback(~, ~, handles) if ~get_versions_on(handles) set(handles.original_view_toggle, 'State', 'on'); end

% -------------------------------------------------------------------- function original_view_toggle_OnCallback(~, ~, handles) set_version_toggle(handles, 'original');

% -------------------------------------------------------------------- function linear_transform_view_toggle_OffCallback(~, ~, handles) if ~get_versions_on(handles) set(handles.linear_transform_view_toggle, 'State', 'on'); end

% -------------------------------------------------------------------- function linear_transform_view_toggle_OnCallback(~, ~, handles) set_version_toggle(handles, 'linear');

% -------------------------------------------------------------------- function warp_view_toggle_OffCallback(~, ~, handles) if ~get_versions_on(handles) set(handles.warp_view_toggle, 'State', 'on'); end

% -------------------------------------------------------------------- function warp_view_toggle_OnCallback(~, ~, handles) set_version_toggle(handles, 'warp');

% -------------------------------------------------------------------- function set_version_toggle(handles, version_name) if ~strcmp(version_name, 'original') set(handles.original_view_toggle, 'State', 'off'); set(handles.auto_align, 'Enable', 'off'); else set(handles.auto_align, 'Enable', 'on'); end

if ~strcmp(version_name, 'linear') set(handles.linear_transform_view_toggle, 'State', 'off'); set(handles.auto_warp, 'Enable', 'off'); else set(handles.auto_warp, 'Enable', 'on'); end

if ~strcmp(version_name, 'warp') set(handles.warp_view_toggle, 'State', 'off'); end

%check if the program is switching images %if it is, don't perform these actions, the switch_images function will %handle them if ~get_switching_flag() update_image(handles); update_table_cells(handles); end

% -------------------------------------------------------------------- function versions_on = get_versions_on(handles) versions_on = 1; if strcmp(get(handles.original_view_toggle, 'State'), 'off') & strcmp(get(handles.linear_transform_view_toggle, 'State'), 'off') & strcmp(get(handles.warp_view_toggle, 'State'), 'off') versions_on = 0; end

% -------------------------------------------------------------------- function composite_view_toggle_OnCallback(~, ~, handles) set_type_toggle(handles, 'composite');

% -------------------------------------------------------------------- function composite_view_toggle_OffCallback(~, ~, handles) reset_type_toggle(handles, 'composite');

% -------------------------------------------------------------------- function dark_view_toggle_OnCallback(~, ~, handles) set_type_toggle(handles, 'dark');

% -------------------------------------------------------------------- function dark_view_toggle_OffCallback(~, ~, handles) reset_type_toggle(handles, 'dark');

% -------------------------------------------------------------------- function fluorescent_view_toggle_OnCallback(~, ~, handles) set_type_toggle(handles, 'fluorescent');

% -------------------------------------------------------------------- function fluorescent_view_toggle_OffCallback(~, ~, handles) reset_type_toggle(handles, 'fluorescent');

% -------------------------------------------------------------------- function axon_view_toggle_OnCallback(~, ~, handles) set_type_toggle(handles, 'axon');

% -------------------------------------------------------------------- function axon_view_toggle_OffCallback(~, ~, handles) reset_type_toggle(handles, 'axon');

% -------------------------------------------------------------------- function reset_type_toggle(handles, type_name) if ~get_types_on(handles) & ~get_switching_flag() set_switching_flag(1); %prevent the program from reloading the image data

if strcmp(type_name, 'composite') set(handles.composite_view_toggle, 'State', 'on'); elseif strcmp(type_name, 'dark') set(handles.dark_view_toggle, 'State', 'on'); elseif strcmp(type_name, 'fluorescent') set(handles.fluorescent_view_toggle, 'State', 'on'); elseif strcmp(type_name, 'axon') set(handles.axon_view_toggle, 'State', 'on'); end
set_switching_flag(0); end

% -------------------------------------------------------------------- function set_type_toggle(handles, type_name) if ~strcmp(type_name, 'composite') set(handles.composite_view_toggle, 'State', 'off'); end

if ~strcmp(type_name, 'dark') set(handles.dark_view_toggle, 'State', 'off'); end

if ~strcmp(type_name, 'fluorescent') set(handles.fluorescent_view_toggle, 'State', 'off'); end

if ~strcmp(type_name, 'axon') set(handles.axon_view_toggle, 'State', 'off'); end

if ~get_switching_flag() reset_stored_largest;

find_image_file(handles); update_table_cells(handles); update_image(handles); end

% -------------------------------------------------------------------- function types_on = get_types_on(handles) types_on = 1; if strcmp(get(handles.composite_view_toggle, 'State'), 'off') & strcmp(get(handles.dark_view_toggle, 'State'), 'off') & strcmp(get(handles.fluorescent_view_toggle, 'State'), 'off') & strcmp(get(handles.axon_view_toggle, 'State'), 'off') types_on = 0; end

% -------------------------------------------------------------------- function set_toolbar_enabled(handles, enable_toolbar) %Summary: %The function is used to prevent the user from manipulating an image that %does not exist. This can occur if no image data was found or during the %start of the program when no image has been loaded yet

%check that enable_toolbar is either 'on' or 'off' if strcmpi(enable_toolbar, 'on') || strcmpi(enable_toolbar, 'off') set(handles.uitoggletool1, 'Enable', enable_toolbar); %zoom in tool set(handles.uitoggletool2, 'Enable', enable_toolbar); %zoom out tool set(handles.uitoggletool3, 'Enable', enable_toolbar); %pan tool set(handles.reference_viewer, 'Enable', enable_toolbar); %show/hide references tool set(handles.view_ideal, 'Enable', enable_toolbar); %view the ideal image over the current image set(handles.mask_view, 'Enable', enable_toolbar); %cover everything except an area marked as blue on the ideal image set(handles.switch_images, 'Enable', enable_toolbar); %switch between two images set(handles.original_view_toggle, 'Enable', enable_toolbar); %view original version of image set(handles.linear_transform_view_toggle, 'Enable', enable_toolbar); %view linear transform version of image set(handles.warp_view_toggle, 'Enable', enable_toolbar); %view warped version of image set(handles.composite_view_toggle, 'Enable', enable_toolbar); %view composite image version of bone set(handles.dark_view_toggle, 'Enable', enable_toolbar); %view dark image version of bone set(handles.fluorescent_view_toggle, 'Enable', enable_toolbar); %view fluorescent version of bone set(handles.flat_weight_toggle, 'Enable', enable_toolbar); set(handles.automatic_weight_toggle, 'Enable', enable_toolbar); set(handles.custom_weight_toggle, 'Enable', enable_toolbar); set(handles.normal_view_toggle, 'Enable', enable_toolbar); set(handles.range_view_toggle, 'Enable', enable_toolbar); set(handles.normalized_view_toggle, 'Enable', enable_toolbar);

%functions not part of the toolbar, but rely on image data none the %less set(handles.edit_menu, 'Enable', enable_toolbar); %menu item that leads to Auto align and Auto warp set(handles.export_option, 'Enable', enable_toolbar); %menu item that allows the user to save the currently viewed image as a png set(handles.import_axon_positions_option, 'Enable', enable_toolbar); %menu item that the user can use to select the directory containing the axon reference points set(handles.options_menu, 'Enable', enable_toolbar);
%if the axon_directory is empty, it means there are no axon points to %display if ~isempty(getappdata(0, 'axon_directory')) set(handles.axon_view_toggle, 'Enable', enable_toolbar); %view the axon points as an overlay else set(handles.axon_view_toggle, 'Enable', 'off'); end
image_version = get_image_version(handles); if strcmp(image_version, 'T') || strcmp(image_version, 'W') set(handles.reference_mover, 'Enable', 'off'); %move references tool set(handles.reference_deleter, 'Enable', 'off'); %delete references tool else set(handles.reference_mover, 'Enable', enable_toolbar); %move references tool set(handles.reference_deleter, 'Enable', enable_toolbar); %delete references tool end end

% -------------------------------------------------------------------- function flat_weight_toggle_OnCallback(~, ~, handles) set_weight_toggle(handles, 'flat');

% -------------------------------------------------------------------- function flat_weight_toggle_OffCallback(~, ~, handles) if ~get_weights_on(handles) set(handles.flat_weight_toggle, 'State', 'on'); end

% -------------------------------------------------------------------- function automatic_weight_toggle_OnCallback(~, ~, handles) set_weight_toggle(handles, 'automatic');

% -------------------------------------------------------------------- function automatic_weight_toggle_OffCallback(~, ~, handles) if ~get_weights_on(handles) set(handles.automatic_weight_toggle, 'State', 'on'); end

% -------------------------------------------------------------------- function custom_weight_toggle_OnCallback(~, ~, handles) set_weight_toggle(handles, 'custom');

% -------------------------------------------------------------------- function custom_weight_toggle_OffCallback(~, ~, handles) if ~get_weights_on(handles) set(handles.custom_weight_toggle, 'State', 'on'); end

% -------------------------------------------------------------------- function set_weight_toggle(handles, weight_name) %the largest pixels are now outdated reset_stored_largest;

%all averaged images are now outdated setappdata(0, 'updated_averaged_images_original', {}); setappdata(0, 'updated_averaged_images_linear_transform', {}); setappdata(0, 'updated_averaged_images_warp', {});

if ~strcmp(weight_name, 'flat') set(handles.flat_weight_toggle, 'State', 'off'); end

if ~strcmp(weight_name, 'automatic') set(handles.automatic_weight_toggle, 'State', 'off'); end

if ~strcmp(weight_name, 'custom') set(handles.custom_weight_toggle, 'State', 'off'); end

%if the image is an averaged image, update it if is_ideal_or_avg() == 2 || strcmp(get(handles.normal_view_toggle, 'State'), 'off') update_image(handles); end

update_summary(handles);

% -------------------------------------------------------------------- function weights_on = get_weights_on(handles) weights_on = 1; if strcmp(get(handles.flat_weight_toggle, 'State'), 'off') & strcmp(get(handles.automatic_weight_toggle, 'State'), 'off') & strcmp(get(handles.custom_weight_toggle, 'State'), 'off') weights_on = 0; end

% -------------------------------------------------------------------- function normal_view_toggle_OnCallback(~, ~, handles) set_heatmap_view_toggle(handles, 'normal');

% -------------------------------------------------------------------- function normal_view_toggle_OffCallback(~, ~, handles) if ~get_heatmaps_on(handles) set(handles.normal_view_toggle, 'State', 'on'); end

% -------------------------------------------------------------------- function range_view_toggle_OnCallback(~, ~, handles) set_heatmap_view_toggle(handles, 'range');

% -------------------------------------------------------------------- function range_view_toggle_OffCallback(~, ~, handles) if ~get_heatmaps_on(handles) set(handles.range_view_toggle, 'State', 'on'); end

% -------------------------------------------------------------------- function normalized_view_toggle_OnCallback(~, ~, handles) set_heatmap_view_toggle(handles, 'normalized');

% -------------------------------------------------------------------- function normalized_view_toggle_OffCallback(~, ~, handles) if ~get_heatmaps_on(handles) set(handles.normalized_view_toggle, 'State', 'on'); end

% -------------------------------------------------------------------- function set_heatmap_view_toggle(handles, button_name) table_cells = get_table_cells(); reset_stored_largest;

if ~strcmp(button_name, 'normal') set(handles.normal_view_toggle, 'State', 'off'); end

if ~strcmp(button_name, 'range') set(handles.range_view_toggle, 'State', 'off'); end

if ~strcmp(button_name, 'normalized') set(handles.normalized_view_toggle, 'State', 'off'); end

if ~get_switching_flag() update_image(handles); end

% -------------------------------------------------------------------- function heatmaps_on = get_heatmaps_on(handles) heatmaps_on = 1; if strcmp(get(handles.normal_view_toggle, 'State'), 'off') & strcmp(get(handles.range_view_toggle, 'State'), 'off') & strcmp(get(handles.normalized_view_toggle, 'State'), 'off') heatmaps_on = 0; end

function button_export_all_Callback(~,~,handles)

pack_path = uigetdir('','CREATE NEW DIRECTORY FOR CURRENT GROUP'); directory_name = getappdata(0, 'main_directory');

path_to_files = strcat(directory_name, filesep); %the filepath including the end file separator available_files = get_image_files(path_to_files, getappdata(0, 'valid_image_extensions'), getappdata(0, 'valid_image_identifiers')); available_files = {available_files.name}; %removes the path of the files in the list

specimen_files = cellfun(@(x)regexp(x, '_', 'split'), available_files, 'UniformOutput', false); %split the file name into its 3 parts number_of_files = size(specimen_files, 2); %get the array size of specimen_files for use in seperating the nested array

specimen_names = {number_of_files}; %an array to hold the nested array of specimen_files bone_names = {number_of_files}; %an array to hold the names of the bones

for x = 1:number_of_files specimen_names{x} = specimen_files{x}{1};

~, bone_names{x}, ~ = fileparts(specimen_files{x}{end}); image_identifiers_found{x} = bone_names{x}(length(bone_names{x})); bone_names{x}(length(bone_names{x})) = ''; end

list_of_bones = unique(bone_names); list_of_bones, ~ = sort_nat(list_of_bones, 'ascend'); image_identifiers_found = unique(image_identifiers_found);

animal_names = unique(specimen_names); group_name = specimen_files{1}{2}; select_type = 'F'; image_version = 'W';

%mkdir(pack_path filesep 'Consolidated_Data'); %mkdir(pack_path filesep 'Consolidated_Data' filesep group_name);

weight_file = getappdata(0, 'fluorescent_weight_path'); warp_weights = load(weight_file, 'warp_weights'); warp_weights = warp_weights.warp_weights;

warp_bones = {warp_weights(:).bone_name}; warp_weight_values = {warp_weights(:).value};

for j = 1:numel(list_of_bones)

if(exist(pack_path filesep list_of_bones{j})~=7) mkdir(pack_path filesep list_of_bones{j}); end
for i = 1:numel(animal_names) curr_animal = animal_names{i}; if(~strcmp(curr_animal,'Ideal'))
proc_mat_file = [directory_name filesep 'program_save_files' filesep curr_animal '_' group_name '_' list_of_bones{j} select_type '.mat']directory_name filesep 'program_save_files' filesep curr_animal '_' group_name '_' list_of_bones{j} select_type '.mat'; image_to_display = double(load_converted_image( proc_mat_file, image_version));
% image_to_display = flipud(image_to_display); %TEMPORARY FLIP!!!! PLEASE REMOVE ASAP image_to_display = imtranslate(image_to_display,20 0); %TEMPORARY TRANSLATION!!!! PLEASE REMOVE ASAP
bone_name_ideal = list_of_bones{j} 'I';
ideal_filename = get_image_file(handles, 'Ideal', bone_name_ideal, 'O'); overlay = imread(ideal_filename, 'BackgroundColor', .5, .5, .5); maskAlpha = double(overlay(:, :, 1)) / 255; idealAlpha = 1.0 - double(overlay(:, :, 2)) / 255; alphaData = 0;
overlay = setchannels( overlay, maskAlpha > 0, [ 0, 0, 0] 0, 0, 0 ); % <---------- Major change for mask overlay, used to be 255 255 255 alphaData = max( alphaData, maskAlpha );
nchannels = size(image_to_display, 3); alphaData = repmat( alphaData, 1, 1, nchannels ); overlay = overlay(:, :, 1:nchannels); cdata = image_to_display .* (1 - alphaData) + double(overlay) .* alphaData; cdata = uint8(round(cdata));
%h = figure;imshow(cdata);
imwrite(cdata,pack_path filesep list_of_bones{j} filesep curr_animal '.png','BitDepth',16);
%print('-dpng','-r600',[pack_path filesep 'Consolidated_Data' filesep group_name filesep list_of_bones{j} filesep curr_animal '.png']); %close(h); %keyboard end end
warp_bone_indices = find(strcmp(warp_bones(:),list_of_bones{j})); warp_bone_weights = warp_weight_values(warp_bone_indices); % keyboard save(pack_path filesep list_of_bones{j} filesep 'weight_list.mat','warp_bone_weights'); end

%keyboard

% -------------------------------------------------------------------- % -------------------------------------------------------------------- %User Input Functions % -------------------------------------------------------------------- % --------------------------------------------------------------------

% -------------------------------------------------------------------- function window_close_detected(~, ~) %just before the program closes, clean up a little

set_file_path(''); reset_alternate(); table_cells = get_table_cells(); checkboxes = get_table_checks(); set(table_cells(1:end, 1:end), 'Visible', 'off'); set(checkboxes(1:end), 'Visible', 'off'); set_table_cells([]); set_table_checks([]);

% -------------------------------------------------------------------- function window_size_changed(handles) %Adjust window components based on window size %All components have pixel based units

fig_dim = get(handles.figure1, 'Position'); %get the figure's dimensions text_dimensions = get(handles.animal_title, 'Position'); %get the dimensions of the text in the summary panel text_separation = 10; cell_dimensions = getappdata(0, 'table_cell_dimensions'); %get the dimensions of the table cells table_cells = get_table_cells(); if ~isempty(table_cells) table_from_right = fig_dim(3) - (cell_dimensions(1) * (size(table_cells, 2) + 1)); else table_from_right = fig_dim(3) - (cell_dimensions(1) * 5); end

%equations used to center the generator_button and the error_text_display %over the image error_width = 210; vertical_center = (fig_dim(4)/2)-40; horizontal_center = text_dimensions(3) + ((table_from_right - text_dimensions(3))/2) - (error_width/2);

%setup the dimensions and positions of the figure's components %Position = left, bottom, width, height set(handles.axes1, 'Position', text_dimensions(3), 20, fig_dim(3)-(text_dimensions(3)+(cell_dimensions(1)*5)), fig_dim(4)-60); %resize the image set(handles.generator_button, 'Position', horizontal_center, vertical_center, error_width, 50); %place the button in the center of the image set(handles.error_text_display, 'Position', horizontal_center, vertical_center, error_width, 100); %place the error text in the center of the image

%set the dimensions of the summary panel set(handles.animal_title, 'Position', 0, get_summary_cell_height(handles, text_separation, 1),text_dimensions(3), text_dimensions(4)); set(handles.animal_name, 'Position', 0, get_summary_cell_height(handles, text_separation, 2), text_dimensions(3), text_dimensions(4)); set(handles.level_title, 'Position', 0, get_summary_cell_height(handles, text_separation, 3), text_dimensions(3), text_dimensions(4)); set(handles.level_name, 'Position', 0, get_summary_cell_height(handles, text_separation, 4), text_dimensions(3), text_dimensions(4)); set(handles.linear_transform_title, 'Position', 0, get_summary_cell_height(handles, text_separation, 5), text_dimensions(3), text_dimensions(4)); set(handles.linear_transform_status, 'Position', 0, get_summary_cell_height(handles, text_separation, 6), text_dimensions(3), text_dimensions(4)); set(handles.warp_title, 'Position', 0, get_summary_cell_height(handles, text_separation, 7), text_dimensions(3), text_dimensions(4)); set(handles.warp_status, 'Position', 0, get_summary_cell_height(handles, text_separation, 8), text_dimensions(3), text_dimensions(4)); set(handles.weight_title, 'Position', 0, get_summary_cell_height(handles, text_separation, 9), text_dimensions(3), text_dimensions(4)); set(handles.weight_value, 'Position', 0, get_summary_cell_height(handles, text_separation, 10), text_dimensions(3), text_dimensions(4)); set(handles.current_weight, 'Position', 0, fig_dim(4)-(11*text_dimensions(4))-(text_separation*4), text_dimensions(3), text_dimensions(4));

%check that the table_cells have been initialized if ~isempty(table_cells) row_size = size(table_cells, 1); column_size = size(table_cells, 2);

valid_table_handles = ishandle(table_cells(1:end, 1:end)); %check which elements are handles
%only continue if all elements are handles if valid_table_handles %setup the table cells for row_index = 1:row_size for column_index = 1:column_size set(table_cells(row_index, column_index), 'position', [table_from_right + column_index * cell_dimensions(1), fig_dim(4) - row_index * cell_dimensions(2), cell_dimensions * 0.9]table_from_right + column_index * cell_dimensions(1), fig_dim(4) - row_index * cell_dimensions(2), cell_dimensions * 0.9); end end end end

%if at least one check box exists, update its position if ~isempty(get_table_checks()) checkboxes = get_table_checks(); row_size = size(checkboxes, 2);

valid_check_handles = ishandle(checkboxes(1:end)); %check that all the elements of the array are handles
%if all the elements are handles, adjust the position of the check boxes if valid_check_handles for row_index = 2:row_size+1 set(checkboxes(row_index-1), 'position', [table_from_right + (cell_dimensions(2) * 0.9), fig_dim(4) - row_index * cell_dimensions(2), cell_dimensions(2) * 0.9, cell_dimensions(2) * 0.9]table_from_right + (cell_dimensions(2) * 0.9), fig_dim(4) - row_index * cell_dimensions(2), cell_dimensions(2) * 0.9, cell_dimensions(2) * 0.9); end end end

% -------------------------------------------------------------------- function summary_cell_height = get_summary_cell_height(handles, text_separation, number) fig_dim = get(handles.figure1, 'Position'); %get the figure's dimensions text_dimensions = get(handles.animal_title, 'Position'); separation_modifier = ceil((number/2)-1); summary_cell_height = fig_dim(4)-(number * text_dimensions(4)) - (separation_modifier * text_separation);

% -------------------------------------------------------------------- function generate_image_Callback(handles) if strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') linear_transform_Callback(handles); elseif strcmp(get(handles.warp_view_toggle, 'State'), 'on') warp_Callback(handles); end

update_image(handles); update_table_cells(handles);

% -------------------------------------------------------------------- function key_press_detected(hObject, handles) save_path = getappdata(0, 'save_directory'), filesep, getappdata(0, 'current_file');

%check that the x marker exists character = lower(get(hObject, 'CurrentCharacter')); %get the key that was pressed and convert it to lowercase

pressed_modifier = get(gcf, 'currentmodifier'); pressed_key = get(gcf, 'currentkey');

%if the modifier is control, find the associated shortcut and check that %the toggle button is enabled if strcmp(pressed_modifier, 'control') if strcmp(pressed_key, 'i') && strcmp(get(handles.view_ideal, 'Enable'), 'on') if strcmp(get(handles.view_ideal, 'State'), 'on') set(handles.view_ideal, 'State', 'off'); else set(handles.view_ideal, 'State', 'on'); end elseif strcmp(pressed_key, 'm') && strcmp(get(handles.mask_view, 'Enable'), 'on') if strcmp(get(handles.mask_view, 'State'), 'on') set(handles.mask_view, 'State', 'off'); else set(handles.mask_view, 'State', 'on'); end elseif strcmp(pressed_key, 's') && strcmp(get(handles.switch_images, 'Enable'), 'on') switch_images(handles); elseif strcmp(pressed_key, '0') && strcmp(get(handles.original_view_toggle, 'Enable'), 'on') set(handles.original_view_toggle, 'State', 'on'); elseif strcmp(pressed_key, 'l') && strcmp(get(handles.linear_transform_view_toggle, 'Enable'), 'on') set(handles.linear_transform_view_toggle, 'State', 'on'); elseif strcmp(pressed_key, 'w') && strcmp(get(handles.warp_view_toggle, 'Enable'), 'on') set(handles.warp_view_toggle, 'State', 'on'); elseif strcmp(pressed_key, 'c') && strcmp(get(handles.composite_view_toggle, 'Enable'), 'on') set(handles.composite_view_toggle, 'State', 'on'); elseif strcmp(pressed_key, 'd') && strcmp(get(handles.dark_view_toggle, 'Enable'), 'on') set(handles.dark_view_toggle, 'State', 'on'); elseif strcmp(pressed_key, 'f') && strcmp(get(handles.fluorescent_view_toggle, 'Enable'), 'on') set(handles.fluorescent_view_toggle, 'State', 'on'); elseif strcmp(pressed_key, 'a') && strcmp(get(handles.axon_view_toggle, 'Enable'), 'on') set(handles.axon_view_toggle, 'State', 'on') elseif strcmp(pressed_key, 'e') && ~isempty(get_current_data()) export_viewed_image(handles); elseif strcmp(pressed_key, 't') %recalculate the weight of the current_image if Automatic weight image %is checked if strcmp(get(handles.automatic_weight_toggle, 'State'), 'on') image_version = 'O'; if strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') image_version = 'T'; elseif strcmp(get(handles.warp_view_toggle, 'State'), 'on') image_version = 'W'; end set_outdated_weight(handles, get_file_path(), image_version); update_image(handles); end elseif strcmp(pressed_key, 'x') if ~isempty(getappdata(0, 'axon_directory')) 'xml2image test start' xml_file = strcat(getappdata(0, 'axon_directory'), filesep, 'blah', '.xml'); xml2images(xml_file, [], 'test', 'T5', getappdata(0, 'axon_directory')); 'xml2image test end' end elseif strcmp(pressed_key, 'q') 'contour test start' coordinates = cell(1,4,2); coordinates{1,1,1} = 100; coordinates{1,1,2} = 100; coordinates{1,2,1} = 1000; coordinates{1,2,2} = 100; coordinates{1,3,1} = 1000; coordinates{1,3,2} = 1000; coordinates{1,4,1} = -1; coordinates{1,4,2} = -1;

markers = cell(3,2); markers{1,1} = 200; markers{1,2} = 200; markers{2,1} = 500; markers{2,2} = 500; markers{3,1} = 400; markers{3,2} = 400;
destination_directory = getappdata(0, 'axon_directory'), filesep; draw_contour_image(coordinates, markers, 1600 1200, 'resize', 'rt', destination_directory); 'contour test end' end end

%add or delete reference points marker_labels = get_marker_labels(); character_index = []; if numel(character) == 1, character_index = find( upper( character ) == marker_labels ); end

if ~isempty( character_index ) & character_index > 0 &(strcmp(get(handles.reference_mover, 'State'), 'on') || strcmp(get(handles.reference_deleter, 'State'), 'on')) files_outdated = 0;

if strcmp(get(handles.reference_deleter, 'State'), 'off') & ~isempty(get_x_marker()) & ishandle(get_x_marker()) x_mark_position = get(get_x_marker(), 'Position'); %x and y position of the x marker set_marker_positions(character_index, x_mark_position(1), x_mark_position(2)); files_outdated = 1; elseif strcmp(get(handles.reference_deleter, 'State'), 'on') set_marker_positions(character_index, -1, -1); %set the position of the reference point to -1, -1 files_outdated = 1; end
if files_outdated display_loading(handles); remove_from_updated(handles); save_image_references(save_path, get_marker_positions()); %save the positions of the reference points
row_count = 2; table_cells = get_table_cells(); specimen_name, bone_name = get_selected_names(); image_types = {'C','D','F'};
%if the image is an ideal image, all the images in the column are %outdated if is_ideal_or_avg() == 1 row_count = size(table_cells, 1); end
%outdate the appropriate files for row_index = 2:row_count if is_ideal_or_avg() == 1 specimen_name = get(table_cells(row_index, 1), 'String'); end
for type_index = 1:size(image_types, 2) %find the associated .mat file for the image if ~is_ideal_or_avg(save_path) bone_and_type = bone_name, image_types{type_index}; save_path = get_image_file(handles, specimen_name, bone_and_type, 'T');
if ~isempty(save_path) if strcmp(get(handles.original_view_toggle, 'State'), 'on') save_converted_image(save_path, 1, 'TO'); %the linear transform image is now outdated end
save_converted_image(save_path, 1, 'WO'); %the warp image is now outdated end else break; %don't process ideal or averaged images end end end end update_image(handles); %update the image after changing the reference point end

%if the character is a number representing the arrow keys, move the %selection character_code = double( character ); if character_code <= 31 & character_code >= 28 & ~isempty(get_file_path()) & ~getappdata(0, 'program_processing') selected_position = get_selected_position(); %get the currently selected cell previous_position = selected_position; %remember the selected cell table_cells = get_table_cells(); %get the handles of each cell in the table

%if left, right, up, or down arrow keys were pressed, adjust the %selected cell accordingly if character_code == 28 & selected_position(2) > 2 %if the column the selection is on, is greater than 2, move the %cursor left selected_position(2) = selected_position(2) - 1; elseif character_code == 29 & selected_position(2) < size(table_cells, 2) %if the column the selection is on, is fewer than the number of columns, move the %cursor right selected_position(2) = selected_position(2) + 1; elseif character_code == 30 & selected_position(1) > 2 %if the row the selection is on, is greater than 2, move the %cursor up selected_position(1) = selected_position(1) - 1; elseif character_code == 31 & selected_position(1) < size(table_cells, 1) %if the row the selection is on is fewer than the number of rows, move the %cursor down selected_position(1) = selected_position(1) + 1; else previous_position = 0; %if the cell being selected is invalid, set the previous_position to 1,1 as an error check end
%check that the cell is visible %if it's not, don't move the selection if strcmp(get(table_cells(selected_position(1), selected_position(2)), 'Visible'), 'on') %determine the image version to display and set the appropriate color if strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on'); set(table_cells(selected_position(1), selected_position(2)), 'backgroundcolor', getappdata(0, 'linear_transform_selected_color')); elseif strcmp(get(handles.warp_view_toggle, 'State'), 'on') set(table_cells(selected_position(1), selected_position(2)), 'backgroundcolor', getappdata(0, 'warp_selected_color')); else set(table_cells(selected_position(1), selected_position(2)), 'backgroundcolor', getappdata(0, 'original_selected_color')); end
%check that there was a previous_position, as in the selection %moved to another cell if previous_position %check that the previous file existed and set the color accordingly if exist(get_file_path(), 'file') image_data = []; %setup image_data to store the information read from a .mat or image file
~, file_name, ~ = fileparts(get_file_path()); save_file = strcat(getappdata(0, 'save_directory'), filesep, file_name, '.mat'); name_parts = regexp(file_name, '_', 'Split');
if strcmp(get(handles.axon_view_toggle, 'State'), 'off') || strcmp(name_parts{1}, 'Avg') || strcmp(name_parts{1}, 'Ideal') %determine if the .mat file has the associated information if strcmp(name_parts{1}, 'Ideal') image_data = 1; elseif strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') & exist(save_file, 'file') image_data = whos('-file', save_file, 'linear_transform'); elseif strcmp(get(handles.warp_view_toggle, 'State'), 'on') & exist(save_file, 'file') image_data = whos('-file', save_file, 'warp'); elseif strcmp(get(handles.original_view_toggle, 'State'), 'on') image_data = 1; end else image_data = get_axon_positions(handles); end
%check if there's information in image_data and set the color %accordingly if ~isempty(image_data) set(table_cells(previous_position(1), previous_position(2)), 'backgroundcolor', getappdata(0, 'file_found_color'), 'String', ''); else set(table_cells(previous_position(1), previous_position(2)), 'backgroundcolor', getappdata(0, 'file_missing_color'), 'String', ''); end else set(table_cells(previous_position(1), previous_position(2)), 'backgroundcolor', getappdata(0, 'file_missing_color'), 'String', ''); end end
set_selected_position(selected_position); %update the selection's position find_image_file(handles);
if exist(get_file_path(), 'file') set(table_cells(selected_position(1), selected_position(2)), 'backgroundcolor', get_selected_cell_color(handles), 'String', get_cell_text(handles)); else set(table_cells(selected_position(1), selected_position(2)), 'backgroundcolor', getappdata(0, 'missing_selected_color'), 'String', get_cell_text(handles)); end
update_checkboxes(handles); if previous_position update_image(handles); end end end

% -------------------------------------------------------------------- function table_clicked_detected(hObject, handles) if ~isempty(get_file_path()) & ~getappdata(0, 'program_processing') table_cells = get_table_cells(); %get the 2 dimensional array of table cell handles search_finished = 0; %used to stop the loops when the cell has been found

%cycle through each cell and check if has the same handle as the calling %object for row_index = 1:size(table_cells, 1) for column_index = 1:size(table_cells, 2)
%if the handles match, move the selected cell to the new position, %update the table, and update the image if table_cells(row_index, column_index) == hObject %set the currently selected cell to empty selected_position = get_selected_position(); set(table_cells(selected_position(1), selected_position(2)), 'backgroundcolor', getappdata(0, 'file_found_color'), 'String', '');
set_selected_position(row_index column_index); %set the selected_position to the clicked cell find_image_file(handles); %get the new image based on where the selected cell is set(table_cells(row_index, column_index), 'String', get_cell_text(handles)); %update the clicked cell update_table_cells(handles); update_image(handles); %update to the new image update_summary(handles); search_finished = 1; %declare the search_finished
break; %stop the column loop end end
%if the search is finished, stop the row loop if search_finished %remove the dotted selection box from the button set(table_cells(row_index, column_index), 'Enable', 'off'); drawnow set(table_cells(row_index, column_index), 'Enable', 'on'); break; end end end

% -------------------------------------------------------------------- function button_down_detected(hObject, handles) position = get(hObject, 'CurrentPoint'); %get the position of the cursor. position(1) and position(3) are x and y respectively

%check that the reference_mover is on if strcmp(get(handles.reference_mover, 'State'), 'on') & strcmp(get(handles.reference_mover, 'Enable'), 'on') %if the x marker exists, change its position. If it does not exist, create %it in the clicked position if ~isempty(get_x_marker()) && ishandle(get_x_marker()) set(get_x_marker(), 'Position', position(1) position(3), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle', 'HitTest', 'off'); %sets the global variable that keeps track of the x's position else image_text = text(position(1), position(3), '+', 'Color', 'y', 'FontSize', 15, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle', 'HitTest', 'off'); %creates a new text object set_x_marker(image_text); %sets the global variable for the x marker end end

% -------------------------------------------------------------------- function checkbox_clicked_detected(~, handles) %when a user clicks a checkbox, update the save file

checkbox_file = getappdata(0, 'checkbox_preferences_file'); %get the file name

%check that the file name isn't empty and that the file exists if ~isempty(get_file_path()) && ~isempty(checkbox_file) && exist(checkbox_file, 'file') table_cells = get_table_cells(); checkboxes = get_table_checks();

file_data = load(checkbox_file); %load the user's preferences for the checkboxes checkbox_preferences = file_data.checkbox_preferences; %get the struct array containing the preferences
%cycle through all the checkboxes, determine which are selected and %which aren't and adjust the save file accordingly for check_index = 1:size(checkboxes, 2) specimen_name = get(table_cells(check_index+1, 1), 'String'); %get the specimen name from the first column
%if the checkbox is disabled, disregard it if ~isempty(checkbox_preferences) && ~strcmp(specimen_name, 'Ideal') && ~strcmp(specimen_name, 'Avg') && strcmp(get(checkboxes(check_index), 'Enable'), 'on') file_found = 0; %remember if the file already exists in the array
%cycle through the preferences and check if a specimen is listed for file_index = 1:size(checkbox_preferences, 2)
%if the image appears in the array and the checkbox is %ticked, remove the image from the array and set file_found %to 1 if strcmp(checkbox_preferences(file_index).specimen_name, specimen_name) file_found = 1; if get(checkboxes(check_index), 'Value') == 1 checkbox_preferences(file_index) = []; %remove the image from the array break; end end end
%if the file was not found and the checkbox is unticked, add %the image to the array if ~file_found && get(checkboxes(check_index), 'Value') == 0 end_index = size(checkbox_preferences, 2) + 1; %extends the checkbox_preferences size by 1
%save the undesired image to the array checkbox_preferences(end_index).specimen_name = specimen_name; end
elseif get(checkboxes(check_index), 'Value') == 0 && isempty(checkbox_preferences) && ~strcmp(specimen_name, 'Ideal') && ~strcmp(specimen_name, 'Avg') && strcmp(get(checkboxes(check_index), 'Enable'), 'on') end_index = size(checkbox_preferences, 2) + 1;
%save the undesired image to the array checkbox_preferences(end_index).specimen_name = specimen_name; break; end end
save(checkbox_file, 'checkbox_preferences'); %save the struct array to the file else warndlg('User preferences file not found'); end

setappdata(0, 'updated_averaged_images_original', {}); setappdata(0, 'updated_averaged_images_linear_transform', {}); setappdata(0, 'updated_averaged_images_warp', {});

if is_ideal_or_avg() == 2 update_image(handles); else update_summary(handles); end

% -------------------------------------------------------------------- function weight_button_pressed(handles) dialog_handle = dialog('Position',300 300 250 150,'Name','Change weight'); dialog_message = uicontrol('Parent', dialog_handle,... 'Style','text',... 'ForegroundColor', 'k', ... 'Position',20 80 210 40,... 'String','Enter a weight value');

text_field = uicontrol('Parent', dialog_handle,... 'Style','edit',... 'Position',75 70 100 25);

ok_button = uicontrol('Parent', dialog_handle,... 'Position',50 20 70 25,... 'String','OK',... 'Callback', @(s,e)accept_weight_modification(handles, text_field, dialog_message, dialog_handle));

cancel_button = uicontrol('Parent', dialog_handle, ... 'Position', 125 20 70 25, ... 'String', 'Cancel', ... 'Callback', @(s,e)cancel_weight_modification(dialog_handle));

uiwait(dialog_handle); %wait for the dialog box to close before running to completion

update_summary(handles);

% -------------------------------------------------------------------- function accept_weight_modification(handles, text_field, dialog_message, dialog_handle) user_input = get(text_field, 'String'); valid_number = 1;

try user_input = eval( user_input ); end

if ~isnumeric( user_input ) || numel( user_input ) ~= 1 || ~isreal( user_input ) || isnan( user_input ) || isinf( user_input ) || ~(user_input > 0) valid_number = 0; end

if ~is_ideal_or_avg() && ~isempty(get_file_path()) && exist(get_file_path(), 'file') && valid_number %remove the proper averaged image from the list of updated averages if strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') remove_name(getappdata(0, 'updated_averaged_images_linear_transform'), 'updated_averaged_images_linear_transform'); reset_stored_largest('T') elseif strcmp(get(handles.warp_view_toggle, 'State'), 'on') remove_name(getappdata(0, 'updated_averaged_images_warp'), 'updated_averaged_images_warp'); reset_stored_largest('W'); else remove_name(getappdata(0, 'updated_averaged_images_original'), 'updated_averaged_images_original'); reset_stored_largest('O'); end

modify_weight(handles, user_input); delete(dialog_handle); update_image(handles); elseif ~isnumeric(user_input) || isnan(user_input) || numel(user_input) ~= 1 set(dialog_message, 'ForegroundColor', 'r', 'String', 'Please enter a number'); elseif ~isreal(user_input) set(dialog_message, 'ForegroundColor', 'r', 'String', 'Please enter a real number'); elseif isinf(user_input) set(dialog_message, 'ForegroundColor', 'r', 'String', 'Please don''t enter infinite numbers'); elseif ~(user_input > 0) set(dialog_message, 'ForegroundColor', 'r', 'String', 'The number must be greater than 0'); end

% -------------------------------------------------------------------- function cancel_weight_modification(dialog_handle) delete(dialog_handle);

% -------------------------------------------------------------------- function modify_weight(handles, weight_value) if ~is_ideal_or_avg() & ~isempty(get_file_path()) & exist(get_file_path(), 'file') & ~isnan(weight_value) specimen_name, bone_name = parse_image_name(get_file_path()); bone_name(end) = ''; version = get_image_version(handles);

set_image_weight(handles, specimen_name, bone_name, version, weight_value); end

% -------------------------------------------------------------------- % -------------------------------------------------------------------- %Image Functions % -------------------------------------------------------------------- % --------------------------------------------------------------------

% -------------------------------------------------------------------- function update_image(handles) if ~getappdata(0, 'program_processing') setappdata(0, 'program_processing', 1); %prevent the user from calling the function again while it is processing set(handles.error_text_display, 'Visible', 'off'); display_loading(handles);

image_name = get_file_path(); image_to_display = []; %set up an empty array for other if statements to use if the file is not found display_references = 1; %remember if the references should be displayed or not image_version = get_image_version(handles); updated_string = ''; %used to keep track of the variable name for the updated averaged images specimen_name, bone_name = parse_image_name(image_name); checkboxes = get_table_checks(); set_current_data([]); %set the current data to empty to free up memory
%if the file exists, get the image data and load the image references into %memory if exist(image_name, 'file') image_references = get_image_references(handles, image_name, image_version); image_references = get_cleaned_references(image_references); set_all_marker_positions(image_references); clearvars image_references; end
%if the averaged file does not exist, create it if strcmp(specimen_name(1:end-1), 'Avg') || strcmp(specimen_name, 'Avg') if ~exist(image_name, 'file') && strcmp(image_version, 'O') update_averaged_image(handles); image_name = get_file_path(); elseif ~strcmp(image_version, 'O') average_name = strcat(getappdata(0, 'save_directory'), filesep, specimen_name, '_', bone_name, '.mat'); if ~exist(average_name, 'file') update_averaged_image(handles); elseif strcmp(image_version, 'T') & isempty(whos('-file', average_name, 'linear_transform')) update_averaged_image(handles); elseif strcmp(image_version, 'W') & isempty(whos('-file', average_name, 'warp')) update_averaged_image(handles); end end end
%determine the save variable for the associated averaged image if strcmp(image_version, 'T') updated_string = 'updated_averaged_images_linear_transform'; display_references = 0; elseif strcmp(image_version, 'W'); updated_string = 'updated_averaged_images_warp'; display_references = 0; elseif exist(image_name, 'file') updated_string = 'updated_averaged_images_original'; elseif strcmp(specimen_name, 'Avg') updated_string = 'updated_averaged_images_original'; image_name = get_file_path(); end
%update the averaged image and get the image data from the appropriate %source if ~isempty(updated_string) updated_images = get_updated_averages(handles, getappdata(0, updated_string)); %check if the averaged image needs updating setappdata(0, updated_string, updated_images);
if strcmp(image_version, 'T') || strcmp(image_version, 'W') ~, file_name, ~ = fileparts(image_name); save_path = getappdata(0, 'save_directory') filesep file_name '.mat'; image_to_display = load_converted_image(save_path, image_version); elseif exist(image_name, 'file') image_to_display = imread(image_name); end end clearvars updated_string;
%if the image is an ideal image, load the data from the original file if is_ideal_or_avg() == 1 ~, ideal_bone = parse_image_name(image_name); image_name = get_image_file(handles, 'Ideal', ideal_bone, image_version);
if ~isempty(image_name) image_to_display = imread(image_name); set_file_path(image_name); end elseif strcmp(get(handles.axon_view_toggle, 'State'), 'on') & ~is_ideal_or_avg() axon_positions = get_axon_positions(handles);
if ~isempty(axon_positions) image_to_display = render_points(image_to_display, axon_positions, getappdata(0, 'axon_point_size')); %apply the dots to the image else image_to_display = []; end
clearvars axon_positions; end
%if there is a valid image to display, display it, else, display a blue %star if ~isempty(image_to_display) & is_valid_size(handles, image_name) set_current_data(image_to_display); %set the current image data if ~isempty(get_current_data()) & exist(get_file_path(), 'file') if strcmp(get(handles.normal_view_toggle, 'State'), 'off') selected_position = get_selected_position(); table_cells = get_table_cells(); one_column = 1; %developer boolean to toggle between normalizing against one column or all columns
image_version = get_image_version(handles); image_identifier = get_image_type(handles);
if one_column largest_pixel = get_stored_largest(selected_position(2), image_version); column_start = selected_position(2); column_end = column_start; else largest_pixel = get_stored_largest(0, image_version); column_start = 2; column_end = size(table_cells, 2); end
if ~is_ideal_or_avg() & ~largest_pixel %cycle through all the rows and determine if the image should be added to %the array for row_index = 2:size(table_cells, 1) specimen_name = get(table_cells(row_index, 1), 'String');
if ~strcmp(specimen_name, 'Avg') && ~strcmp(specimen_name, 'Ideal') for column_index = column_start:column_end bone_name = get(table_cells(1, column_index), 'String');
if strcmp(get(checkboxes(row_index-1), 'Visible'), 'on') image_file = get_image_file(handles, specimen_name, bone_name, image_identifier, image_version); if is_valid_size(handles, image_file) largest_pixel = get_largest_pixel(handles, image_file, image_version, largest_pixel); end end end end end
if one_column set_stored_largest(column_start, largest_pixel, image_version); else set_stored_largest(0, largest_pixel, image_version); end elseif ~(is_ideal_or_avg() == 1) && strcmp(get(handles.range_view_toggle, 'State'), 'on') largest_pixel = 1; elseif is_ideal_or_avg() == 2 image_version = get_image_version(handles); image_type = get_image_type(handles); largest_pixel = 0;
if strcmp(image_version, 'O') average_string = 'updated_averaged_images_original'; elseif strcmp(image_version, 'T') average_string = 'updated_averaged_images_linear_transform'; else average_string = 'updated_averaged_images_warp'; end
setappdata(0, average_string, {}); updated_averages = getappdata(0, average_string);
for column_index = 2:size(table_cells, 2) bone_name = get(table_cells(1, column_index), 'String'), image_type; image_file = get_image_file(handles, 'Avg', bone_name, image_version);
if isempty(image_file) if strcmp(image_version, 'O') file_extension = '.png'; else file_extension = '.mat'; end image_file = getappdata(0, 'save_directory'), filesep, 'Avg_', bone_name, file_extension; end
updated_averages = get_updated_averages(handles, updated_averages, image_file, column_index); largest_pixel = get_largest_pixel(handles, image_file, image_version, largest_pixel); end
setappdata(0, average_string, updated_averages); %get the average image data from the appropriate source if strcmp(image_version, 'T') || strcmp(image_version, 'W') specimen_name, bone_name = parse_image_name(image_name); averaged_file = get_image_file(handles, specimen_name, bone_name, image_version); average_data = load_converted_image(averaged_file, image_version); else average_data = imread(get_file_path()); end
set_current_data(average_data); end
if largest_pixel && ~(is_ideal_or_avg() == 1) original_image = double(get_current_data()); original_image = mean(original_image, 3);
if strcmp(get(handles.normalized_view_toggle, 'State'), 'on') specimen_name, bone_name = parse_image_name(get_file_path()); original_image = apply_weight(handles, original_image, specimen_name, bone_name); elseif strcmp(get(handles.range_view_toggle, 'State'), 'on') largest_pixel = max(original_image(:)); end
image_data = original_image * 255 / largest_pixel;
%pixel_sum = round(sum(original_image(:)))
image_data = uint8(round(image_data)); set_current_data(image_data); end end
draw_image(handles); %draw the image to the axes in the window draw_overlays(handles); set_toolbar_enabled(handles, 'on'); %enable the toggle buttons on the toolbar
%if the image is an ideal or averaged image, don't allow the %user to transform the image if is_ideal_or_avg() set(handles.auto_manipulate, 'Enable', 'off'); else set(handles.auto_manipulate, 'Enable', 'on'); end
%don't display references if the image is an averaged image if is_ideal_or_avg() == 2 display_references = 0; set(handles.reference_viewer, 'Enable', 'off'); end
%only display reference points if the image is not a warp version %otherwise, turn off the reference_viewer if ~display_references set(handles.reference_mover, 'Enable', 'off'); set(handles.reference_deleter, 'Enable', 'off'); end else display_no_data(handles); end else display_no_data(handles); end
update_table_cells(handles); update_summary(handles); %update the status of the linear transformed and warped text in the summary panel setappdata(0, 'program_processing', 0); %allow the function to be called again set_gui_enabled(handles, 'on'); end

% -------------------------------------------------------------------- function image_data = apply_weight(handles, image_data, specimen_name, bone_name) weight = get_image_weight(handles, specimen_name, bone_name); image_data = image_data * weight;

% -------------------------------------------------------------------- function largest_pixel = get_largest_pixel(handles, image_file, image_version, largest_pixel) if nargin < 4, largest_pixel = []; end if nargin < 3, image_version = []; end if nargin < 2, image_file = []; end if isempty(largest_pixel), largest_pixel = 0; end if isempty(image_version), image_version = get_image_version(handles); end if isempty(image_file), image_file = get_file_path(); end

image_data = []; specimen_name, bone_name = parse_image_name(image_file); image_file = get_image_file(handles, specimen_name, bone_name, image_version);

%if the file exists, get the appropriate image_data if ~isempty(image_file) if strcmp(image_version, 'T') || strcmp(image_version, 'W') if strcmp(image_version, 'T') transform_type = 'linear_transform'; else transform_type = 'warp'; end

if ~isempty(whos('-file', image_file, transform_type)) image_data = load_converted_image(image_file, image_version); end else image_data = imread(image_file); end end

%get the largest pixel of the image and compare it to the current largest if ~isempty(image_data) image_data = double(image_data); image_data = mean(image_data, 3);

if strcmp(get(handles.normalized_view_toggle, 'State'), 'on') image_data = apply_weight(handles, image_data, specimen_name, bone_name); end
image_max = max(image_data(:)); largest_pixel = max(image_max, largest_pixel); end

% -------------------------------------------------------------------- function draw_image(handles) set(handles.generator_button, 'Enable', 'off', 'Visible', 'off'); set(handles.error_text_display, 'Visible', 'off');

if ~isempty(get_current_data()) set_toolbar_enabled(handles, 'on'); image_version = get_image_version(handles); image_name = get_file_path();

%determine which image version the title should display if strcmp(image_version, 'T') image_view = 'Linear Transform: '; elseif strcmp(image_version, 'W') image_view = 'Warp: '; else image_view = 'Original: '; end
image_displayed = image(get_current_data()); %displays the image in the window set(image_displayed, 'ButtonDownFcn', @(s,e) button_down_detected(handles.axes1, handles)); %set image to register button presses
%set the axis size to the image axis('image'); axis('tight');
axis('off'); %turns off the axis numbers ~, file_name, ~ = fileparts(image_name); %get the name of the file for use by the title file_name = image_view, file_name;
%when testing is completed, this code block should be removed and %replaced with the commented out code just below it image_data = mean(double(get(image_displayed, 'cdata')), 3); minpix = min(min(image_data)); maxpix = max(max(image_data)); titlestr = sprintf( '%s (%d-%d)', file_name, round(minpix), round(maxpix)); title(titlestr, 'interpreter', 'none', 'Color', 'w'); %sets the title of the image, setting interpreter to none allows the underscore characters to be displayed correctly
%{ title(file_name, 'interpreter', 'none', 'Color', 'w'); %sets the title of the image, setting interpreter to none allows the underscore characters to be displayed correctly %} clearvars image_data minpix maxpix %matlab does not appear to clear these when the function finishes
%if the data is outdated, set the image to half opacity if is_image_outdated(handles, image_name, image_version) display_generator_button(handles); %display the image generator button end
draw_references(handles); else display_no_data(handles); end

% -------------------------------------------------------------------- function draw_references(handles) %if reference_viewer is on, show the reference points if strcmp(get(handles.reference_viewer, 'State'), 'on') && ~strcmp(get_file_path(), '') && is_ideal_or_avg() ~= 2 marker_positions = get_image_references(handles, get_file_path(), get_image_version(handles));

if ~isempty(marker_positions) hold on %keep the original image while the program plots points scaling = get( 0, 'screensize' ); scaling = min( scaling( 3:4 ) ); for q = 1:size(marker_positions, 2) %display a yellow circle behind the reference point's letter if marker_positions(q).x_position > -1 && marker_positions(q).y_position > -1 plot(marker_positions(q).x_position, marker_positions(q).y_position,'o',... 'LineWidth', 1,... 'MarkerSize', scaling/64,... 'MarkerEdgeColor', 1.0, 0, 0,... 'MarkerFaceColor', 0.6, 0, 0,... 'HitTest', 'off'); %if the circle is clicked on, don't do anything
%display the associated letter with the reference point text(marker_positions(q).x_position, marker_positions(q).y_position,... upper(marker_positions(q).letter),... 'Color', 'w',... 'FontUnits', 'pixels', 'FontSize', scaling/75, ... 'HorizontalAlignment', 'Center',... 'VerticalAlignment', 'Middle',... 'FontWeight', 'Bold',... 'HitTest', 'off'); %if the text is clicked on, don't do anything end end hold off %allow the image to change end end

% -------------------------------------------------------------------- function draw_overlays( handles ) %display the appropriate ideal image and/or mask view_mask = strcmp(get(handles.mask_view, 'State'), 'on'); view_ideal = strcmp(get(handles.view_ideal, 'State'), 'on'); image_displayed = findobj(handles.axes1, 'type', 'image');

if ~view_mask && ~view_ideal, return, end if isempty(get_current_data()), return, end if isempty(image_displayed), return, end

%get the ideal image file ~, bone_name = parse_image_name(); bone_name(end) = 'I'; ideal_filename = get_image_file(handles, 'Ideal', bone_name, 'O');

hold on if exist(ideal_filename, 'file') && ~isempty(ideal_filename) %draw the ideal image as semi transparent over the specimen's image overlay = imread(ideal_filename, 'BackgroundColor', .5, .5, .5); maskAlpha = double(overlay(:, :, 1)) / 255; idealAlpha = 1.0 - double(overlay(:, :, 2)) / 255;

alphaData = 0; if view_mask overlay = setchannels( overlay, maskAlpha > 0, [ 0, 0, 0] 0, 0, 0 ); % <---------- Major change for mask overlay, used to be 255 255 255
alphaData = max( alphaData, maskAlpha ); end if view_ideal overlay = setchannels( overlay, idealAlpha > 0, [ 255, 255, 0 ] 255, 255, 0 ); idealAlpha = max( idealAlpha, 0.1 ); idealAlpha = min( idealAlpha, 0.6 ); alphaData = max( alphaData, idealAlpha ); end
cdata = double(get(image_displayed, 'cdata'));
nchannels = size(cdata, 3); alphaData = repmat( alphaData, 1, 1, nchannels ); overlay = overlay(:, :, 1:nchannels); cdata = cdata .* (1 - alphaData) + double(overlay) .* alphaData; cdata = uint8(round(cdata));
set(image_displayed, 'cdata', cdata) set_current_data(cdata); %set the current image data else set(handles.mask_view, 'State', 'off'); set(handles.view_ideal, 'State', 'off'); warndlg('Ideal image does not exist', 'Image missing'); end hold off

% -------------------------------------------------------------------- function img = setchannels(img, mask, rgb) for i = 1:3 ch = img(:, :, i); ch( mask ) = rgb(i); img(:, :, i) = ch; end

% -------------------------------------------------------------------- function display_generator_button(handles) ~, file_name, ~ = fileparts(get_file_path()); name_parts = regexp(file_name, '_', 'Split'); specimen_name = name_parts{1}; specimen_name = regexprep(specimen_name, '*', '');

if ~strcmp(specimen_name, 'Avg') && ~strcmp(specimen_name, 'Ideal') && ~isempty(getappdata(0, 'main_directory')) valid_transform = is_valid_transform(handles, name_parts{1}, name_parts{end});

if ~isstruct(valid_transform) && valid_transform == 1 %determine if the button should be displayed and what it should say if strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') set(handles.generator_button, 'Enable', 'on', 'Visible', 'on', 'String', 'Make Linear Transformed Image'); elseif strcmp(get(handles.warp_view_toggle, 'State'), 'on') set(handles.generator_button, 'Enable', 'on', 'Visible', 'on', 'String', 'Make Warped Image'); end else display_transform_error(handles, valid_transform); end end

% -------------------------------------------------------------------- function display_no_data(handles) %if the image is empty, display a blue star and disable buttons and options %that can't be used properly when there is no data

plot(1,1,'*', 'Color', 'c'); %draw a cyan star on the screen

%correctly size the axis to the image axis('image'); axis('tight');

axis('off'); %turns off the axis numbers name = ''; %if the file is not found, don't display anything title(name, 'Interpreter', 'none'); set_toolbar_enabled(handles, 'off'); %these options should not be disabled, otherwise the user could get trapped %in a view that does not have any images set(handles.switch_images, 'Enable', 'on'); set(handles.original_view_toggle, 'Enable', 'on'); set(handles.linear_transform_view_toggle, 'Enable', 'on'); set(handles.warp_view_toggle, 'Enable', 'on'); set(handles.composite_view_toggle, 'Enable', 'on'); set(handles.dark_view_toggle, 'Enable', 'on'); set(handles.fluorescent_view_toggle, 'Enable', 'on'); set(handles.flat_weight_toggle, 'Enable', 'on'); set(handles.automatic_weight_toggle, 'Enable', 'on'); set(handles.custom_weight_toggle, 'Enable', 'on'); set(handles.normal_view_toggle, 'Enable', 'on'); set(handles.range_view_toggle, 'Enable', 'on'); set(handles.normalized_view_toggle, 'Enable', 'on');

if ~isempty(getappdata(0, 'axon_directory')) set(handles.axon_view_toggle, 'Enable', 'on'); end

display_generator_button(handles);

% -------------------------------------------------------------------- % -------------------------------------------------------------------- %Table Cell Functions % -------------------------------------------------------------------- % --------------------------------------------------------------------

% -------------------------------------------------------------------- function update_table_cells(handles) table_cells = get_table_cells(); image_identifier = get_image_type(handles); image_version = get_image_version(handles);

%cycle through every cell and determine if it corresponds to an %existing file for row_index = 2:size(table_cells, 1) for column_index = 2:size(table_cells, 2) specimen_name = get(table_cells(row_index, 1), 'String'); %get the specimen name from the first column bone_name = get(table_cells(1, column_index), 'String'); %get the bone name from the first row image_data_info = [];

if strcmp(specimen_name, 'Ideal') complete_bone = bone_name, 'I'; else complete_bone = bone_name, image_identifier; end image_file = get_image_file(handles, specimen_name, complete_bone, image_version);
if ~isempty(image_file) if (strcmp(image_version, 'T') || strcmp(image_version, 'W')) && ~strcmp(specimen_name, 'Ideal') if strcmp(image_version, 'T') version_string = 'linear_transform'; else version_string = 'warp'; end
if strcmp(get(handles.axon_view_toggle, 'State'), 'on') image_data_info = get_axon_positions(handles, image_file); else image_data_info = whos('-file', image_file, version_string); end
else if strcmp(get(handles.axon_view_toggle, 'State'), 'on') & ~is_ideal_or_avg(image_file) image_data_info = get_axon_positions(handles, image_file); else image_data_info = 1; end end end
%if the file exists, indicate it with the appropriate color %else, mark it as missing if ~isempty(image_data_info) set(table_cells(row_index, column_index), 'backgroundcolor', getappdata(0, 'file_found_color')); else set(table_cells(row_index, column_index), 'backgroundcolor', getappdata(0, 'file_missing_color')); end end end

selected_position = get_selected_position(); set(table_cells(selected_position(1), selected_position(2)), 'backgroundcolor', get_selected_cell_color(handles), 'String', get_cell_text(handles)); %set the color of the selected cell update_checkboxes(handles);

% -------------------------------------------------------------------- function cell_color = get_selected_cell_color(handles)

%if the file exists, return the appropriate color %else, return the color for missing if exist(get_file_path(), 'file') cell_color = getappdata(0, 'original_selected_color'); %set the default color image_data = [];

if strcmp(get(handles.original_view_toggle, 'State'), 'on') image_data = 1 1 1; else ~, file_name, ~ = fileparts(get_file_path()); name_parts = regexp(file_name, '_', 'Split'); image_file = strcat(getappdata(0, 'save_directory'), filesep, file_name, '.mat');
%if the .mat file exists and is not an ideal image, determine if it contains data and set the %color %else if the file is an ideal image, set image data to true and %set the color if exist(image_file, 'file') && ~strcmp(name_parts{1}, 'Ideal')
%determine which version is being viewed and set the color %accordingly, also determine if the file contains the %associated data if strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') image_data = whos('-file', image_file, 'linear_transform'); cell_color = getappdata(0 ,'linear_transform_selected_color'); elseif strcmp(get(handles.warp_view_toggle, 'State'), 'on') image_data = whos('-file', image_file, 'warp'); cell_color = getappdata(0, 'warp_selected_color'); end elseif strcmp(name_parts{1}, 'Ideal') image_data = 1 1 1;
%determine the color to set if strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') cell_color = getappdata(0, 'linear_transform_selected_color'); elseif strcmp(get(handles.warp_view_toggle, 'State'), 'on') cell_color = getappdata(0, 'warp_selected_color'); else cell_color = getappdata(0, 'original_selected_color'); end end end
%if image_data is empty, return the color for missing if isempty(image_data) cell_color = getappdata(0, 'missing_selected_color'); end else cell_color = getappdata(0, 'missing_selected_color'); end

% -------------------------------------------------------------------- function cell_text = get_cell_text(handles) %Returns two letters that signify which image is being viewed. The letters %returned letters should then be placed in the currently selected cell

image_type = 'C';

if strcmp(get(handles.dark_view_toggle, 'State'), 'on') image_type = 'D'; elseif strcmp(get(handles.fluorescent_view_toggle, 'State'), 'on') image_type = 'F'; elseif strcmp(get(handles.axon_view_toggle, 'State'), 'on') image_type = 'A'; end

image_version = get_image_version(handles); if strcmp(image_version, 'T') image_version = 'L'; end cell_text = strcat(image_version, image_type); %combine the image_version and image_type

% -------------------------------------------------------------------- function update_checkboxes(handles) %update the checkboxes based on which images in the column have up to date data

image_data_info = []; table_cells = get_table_cells(); checkboxes = get_table_checks();

selected_position = get_selected_position(); column_index = selected_position(2);

image_version = get_image_version(handles); image_identifier = get_image_type(handles);

bone_name = get(table_cells(1, column_index), 'String'); %get the bone name from the first row

%cycle through every cell and determine if it corresponds to an %existing file for row_index = 2:size(table_cells, 1) specimen_name = get(table_cells(row_index, 1), 'String'); %get the specimen name from the first column

if ~strcmp(specimen_name, 'Ideal') && ~strcmp(specimen_name, 'Avg') image_outdated = 0; %keeps track of whether or not the image is up to date complete_bone = bone_name, image_identifier; image_file = get_image_file(handles, specimen_name, complete_bone, image_version);
if strcmp(image_version, 'T') || strcmp(image_version, 'W') outdated_string = image_version, 'O'; if strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') version_string = 'linear_transform'; else version_string = 'warp'; end
if ~isempty(image_file) image_data_info = whos('-file', image_file, version_string); image_outdated = load_converted_image(image_file, outdated_string); end elseif strcmp(image_version, 'O') if ~isempty(image_file) image_data_info = 1; image_outdated = 0; end end
%if the image is an axon image, check if it has an axon file if strcmp(get(handles.axon_view_toggle, 'State'), 'on') & ~isempty(image_data_info) & ~is_ideal_or_avg(image_file) image_data_info = get_axon_positions(handles, image_file); end
%if the file exists, indicate it with the appropriate color if ~isempty(image_data_info) && ~image_outdated if ~is_specimen_in_preferences(specimen_name) set(checkboxes(row_index-1), 'Value', 1, 'Enable', 'on'); else set(checkboxes(row_index-1), 'Value', 0, 'Enable', 'on'); end else set(checkboxes(row_index-1), 'Value', 0, 'Enable', 'off'); end else set(checkboxes(row_index-1:end), 'Value', 0, 'Enable', 'off', 'Visible', 'off'); %the Ideal and Avg images are last in the array, set these and every checkbox after it to invisible break; end end

% -------------------------------------------------------------------- % -------------------------------------------------------------------- %Averaged Image Functions % -------------------------------------------------------------------- % --------------------------------------------------------------------

% -------------------------------------------------------------------- function updated_averages = get_updated_averages(handles, updated_averages, image_name, column_index) %check if the averaged image was already updated

if nargin < 4, column_index = []; end if nargin < 3, image_name = []; end

if isempty(image_name) image_name = get_file_path(); end

specimen_name, ~ = parse_image_name(image_name); specimen_name = regexprep(specimen_name, '*', ''); %remove any '*' characters from the specimen's name

if strcmp(specimen_name, 'Avg') if strcmp(get(handles.axon_view_toggle, 'State'), 'on') ~, file_name, ~ = fileparts(image_name); file_name(end) = 'A'; image_name = strcat(getappdata(0, 'save_directory'), filesep, file_name, '.png'); end

%if the array is empty or does not contain the name of the averaged %image, update image and the array if isempty(updated_averages) || ~ismember(image_name, updated_averages) update_averaged_image(handles, column_index); updated_averages{end+1} = image_name; %add the averaged image to the array update_table_cells(handles); end end

% -------------------------------------------------------------------- function remove_from_updated(handles, image_file) if nargin < 2, image_file = []; end updated_warp_averages = getappdata(0,'updated_averaged_images_warp'); %get the list of updated warped averages

%if linear transform view or warp view are selected, get the associated %data if strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') || strcmp(get(handles.original_view_toggle, 'State'), 'on') remove_name(getappdata(0, 'updated_averaged_images_linear_transform'), 'updated_averaged_images_linear_transform'); end

remove_name(updated_warp_averages, 'updated_averaged_images_warp', image_file);

% -------------------------------------------------------------------- function remove_name(updated_averages, appdata_string, image_file) if nargin < 3, image_file = []; end if isempty(image_file), image_file = get_file_path(); end

%if the array containing the updated averages is not empty, find the %average that is connected to the image_file and remove it %from the updated list if ~isempty(updated_averages) updated_names = char(updated_averages(1:end)); %convert the updated_averages array to a character array ~, bone_name = parse_image_name(image_file);

for name_index = 1:size(updated_names, 1) if strfind(updated_names(name_index, :), bone_name) %find the average associated with the currently selected bone and save its index updated_averages(name_index) = []; %remove the average from the array setappdata(0, appdata_string, updated_averages); %update the appropriate list of updated averages end end end

% -------------------------------------------------------------------- function update_averaged_image(handles, column_index) if nargin < 2, column_index = []; end set(handles.generator_button, 'Visible', 'on', 'Enable', 'off', 'String', 'Loading...'); drawnow

table_cells = get_table_cells(); checkboxes = get_table_checks();

%if there is no column_index data, use the currently displayed image if isempty(column_index) || column_index < 1 || column_index > size(table_cells, 2) selected_cell = get_selected_position(); column_index = selected_cell(2); end

path_to_saves = strcat(getappdata(0, 'save_directory'), filesep);

%get the number of specimens that will be used, so the program won't have %to expand the array for every image found number_of_specimens = 0; for check_index = 1:size(checkboxes, 2) %only get images with selected and visible checkboxes if strcmp(get(checkboxes(check_index), 'Visible'), 'on') && get(checkboxes(check_index), 'Value') == 1 && strcmp(get(checkboxes(check_index), 'Enable'), 'on') number_of_specimens = number_of_specimens + 1; end end

denominator = 0; %keeps track of the denominator used for the average averaged_image = 0;

image_version = get_image_version(handles); image_identifier = get_image_type(handles);

%if the axon view is selected, get the dark field image if strcmp(image_identifier, 'A') image_identifier = 'D'; end

bone_name = get(table_cells(1, column_index), 'String'); bone_name = bone_name, image_identifier;

previous_info = []; size_mismatched = 0; for row_index = 2:size(table_cells, 1)-2 specimen_name = get(table_cells(row_index, 1), 'String'); image_file = get_image_file(handles, specimen_name, bone_name, image_version); image_data = [];

valid_size = is_valid_size(handles, image_file); %{row_index, specimen_name, image_file, valid_size}
if ~isempty(image_file) & ~is_image_outdated(handles, image_file, image_version) & strcmp(get(checkboxes(row_index-1), 'Visible'), 'on') & ~is_specimen_in_preferences(specimen_name) & valid_size
%check that the image size is the same as the previous available %image if valid_size == -1 image_information = imfinfo(image_file); if ~isempty(previous_info) && ~isempty(image_information) if image_information.Height ~= previous_info.Height || image_information.Widht ~= previous_info.Width size_mismatched = 1; break; end end previous_info = imfinfo(image_file); end
if strcmp(image_version, 'T') || strcmp(image_version, 'W') image_data = load_converted_image(image_file, image_version);
if strcmp(get(handles.axon_view_toggle, 'State'), 'on') if strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') file_variable = 'transform_axons'; load_input = 'TA'; else file_variable = 'warp_axons'; load_input = 'WA'; end
if ~isempty(whos('-file', image_file, file_variable)) axon_points = load_converted_image(image_file, load_input);
if ~isempty(axon_points) image_data = render_points(image_data, axon_points, getappdata(0, 'axon_point_sized')); end end end else image_data = imread(image_file);
if strcmp(get(handles.axon_view_toggle, 'State'), 'on') ~, file_name, ~ = fileparts(image_file); file_name(end) = 'A'; axon_file = strcat(getappdata(0, 'axon_directory'), filesep, file_name, '.csv'); if exist(axon_file, 'file') axon_points = csvread(axon_file); image_data = render_points(image_data, axon_points, getappdata(0, 'axon_point_size')); end end end elseif ~valid_size size_mismatched = 2; end
if ~isempty(image_data) weight = get_image_weight(handles, specimen_name, bone_name); averaged_image = averaged_image + weight * double(image_data); denominator = denominator + weight; end end

if denominator averaged_image = averaged_image / denominator; averaged_image = uint8( round( averaged_image ) );

table_outdated = 0; %check if a .mat file existed for the averaged image if strcmp(image_version, 'T') || strcmp(image_version, 'W') averaged_image_file = path_to_saves, 'Avg_', bone_name, '.mat'; %create the Avg file path ~,file_name, ~ = fileparts(averaged_image_file); mat_file = path_to_saves, file_name, '.mat';
%if the file did not exist before now, set table_outdated = 1 if ~exist(mat_file, 'file') table_outdated = 1; end save_converted_image(averaged_image_file, averaged_image, image_version); %write the averaged information to the file else averaged_image_file = strcat(path_to_saves, 'Avg_', bone_name, '.png'); %create the Avg file path if ~exist(averaged_image_file, 'file') table_outdated = 1; end
imwrite(averaged_image, averaged_image_file); %write the averaged information to the file end
%if the table is outdated, update it to show the new averaged file as a %found file if table_outdated ~, current_bone = parse_image_name(get_file_path()); if strcmp(bone_name, current_bone) averaged_image_file = strcat(path_to_saves, 'Avg_', bone_name, '.png'); %create the Avg file path set_file_path(averaged_image_file); end update_table_cells(handles); end end

if size_mismatched if size_mismatched == 1 size_error = errordlg('Some images are not the same size'); else size_error = errordlg('Some images do not match the size of the ideal image'); end

uiwait(size_error); end

set(handles.generator_button, 'Visible', 'off', 'Enable', 'off', 'String', 'Loading...');

% -------------------------------------------------------------------- function ideal_or_avg = is_ideal_or_avg(image_file) %check if the file is an ideal or average image and return a value %representing the answer

if nargin < 1, image_file = []; end if isempty(image_file), image_file = get_file_path(); end

specimen_name, ~ = parse_image_name(image_file); specimen_name = regexprep(specimen_name, '*', ''); %remove any '*' characters from the specimen's name

if strcmp(specimen_name, 'Avg') ideal_or_avg = 2; elseif strcmp(specimen_name, 'Ideal') ideal_or_avg = 1; else ideal_or_avg = 0; end

% -------------------------------------------------------------------- function update_summary(handles) if strcmp(get(handles.automatic_weight_toggle, 'State'), 'on') set(handles.current_weight, 'String', 'Automatic'); elseif strcmp(get(handles.custom_weight_toggle, 'State'), 'on') set(handles.current_weight, 'String', 'Custom'); else set(handles.current_weight, 'String', 'Flat'); end

specimen_name, bone_name = parse_image_name(get_file_path());

if ~isempty(get_file_path()) && exist(get_file_path(), 'file') && ~isempty(getappdata(0, 'main_directory')) save_file = get_image_file(handles, specimen_name, bone_name);

bone_name(end) = ''; set(handles.animal_name, 'String', specimen_name, 'ForegroundColor', 'k'); %set the animal_name set(handles.level_name, 'String', bone_name); %set the bone_name
%allow the user to input numbers if the custom_weight_toggle is %selected if strcmp(get(handles.custom_weight_toggle, 'State'), 'on') set(handles.weight_value, 'Style', 'pushbutton'); else set(handles.weight_value, 'Style', 'text'); end
if ~is_ideal_or_avg() weight_bone = bone_name, get_image_type(handles); image_weight = get_image_weight(handles, specimen_name, weight_bone);
if image_weight set(handles.weight_value, 'String', image_weight, 'ForegroundColor', 'k'); else invalid_weight_value(handles, 'r'); end else invalid_weight_value(handles, 'r'); end
%if the specimen is listed in the checkbox preferences, mark it as %excluded if is_specimen_in_preferences(specimen_name) set(handles.animal_name, 'String', specimen_name, 'ForegroundColor', 'r'); %if the user has deselected the specimen, display the animal's name in bold red set(handles.weight_value, 'Style', 'text'); set(handles.weight_value, 'String', 'Excluded', 'ForegroundColor', 'r'); end
%if the file exists and is not an ideal or avg image, display the status information %else display red '---' for each status if ~isempty(save_file) && ~is_ideal_or_avg() %determine if the linear transformed image is outdated and update the %summary panel if ~is_image_outdated(handles, save_file, 'T') set(handles.linear_transform_status, 'String', 'Ready', 'ForegroundColor', 0 .5 0); else set(handles.linear_transform_status, 'String', 'Outdated', 'ForegroundColor', 'r');
if strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') invalid_weight_value(handles, 'r'); end end
%determine if the warped image is outdated and update the summary panel if ~is_image_outdated(handles, save_file, 'W') set(handles.warp_status, 'String', 'Ready', 'ForegroundColor', 0 .5 0); else set(handles.warp_status, 'String', 'Outdated','ForegroundColor', 'r');
if strcmp(get(handles.warp_view_toggle, 'State'), 'on') invalid_weight_value(handles, 'r'); end end else set_empty_versions(handles, 'r');
if ~strcmp(get(handles.original_view_toggle, 'State'), 'on') invalid_weight_value(handles, 'r'); end end elseif isempty(getappdata(0, 'main_directory')) %adjust the summary panel invalid_image_name(handles, 'k'); set_empty_versions(handles, 'k'); set(handles.export_option, 'Enable', 'off'); set(handles.import_axon_positions_option, 'Enable', 'off'); elseif ~isempty(get_file_path()) %if the file does not exists, display the expected specimen and level name and red dashes %in the status text
%remove the *'s from the specimen name specimen_name = regexprep(specimen_name, '*', ''); %remove any '*' characters from the specimen's name set(handles.animal_name, 'String', specimen_name);
%remove the * and image identifier from the bone name bone_name = regexprep(bone_name, '*', ''); %remove any '*' characters from the bone's name set(handles.level_name, 'String', bone_name);
set(handles.animal_name, 'String', specimen_name, 'ForegroundColor', 'r'); %set the animal_name set(handles.level_name, 'String', bone_name); %set the bone_name set_empty_versions(handles, 'r'); invalid_weight_value(handles, 'r'); end

% -------------------------------------------------------------------- function invalid_image_name(handles, foreground_color) set(handles.animal_name, 'String', '---', 'ForegroundColor', foreground_color); set(handles.level_name, 'String', '---', 'ForegroundColor', foreground_color);

% -------------------------------------------------------------------- function invalid_weight_value(handles, foreground_color) %set the weight value in the summary panel to display red dashes set(handles.weight_value, 'Style', 'text'); set(handles.weight_value, 'String', '---', 'ForegroundColor', foreground_color);

% -------------------------------------------------------------------- function set_empty_versions(handles, foreground_color) set(handles.linear_transform_status, 'String', '---', 'ForegroundColor', foreground_color); set(handles.warp_status, 'String', '---', 'ForegroundColor', foreground_color);

% -------------------------------------------------------------------- % -------------------------------------------------------------------- %Image Weight Functions % -------------------------------------------------------------------- % --------------------------------------------------------------------

% -------------------------------------------------------------------- function image_weight = get_image_weight(handles, specimen_name, bone_name) %get the image's weight from the weight file, if possible

image_weight = 0; weight_found = 0; %keep track if a weight was found weight_values = []; image_version = get_image_version(handles); bone_name(end) = '';

if strcmp(get(handles.flat_weight_toggle, 'State'), 'off') %get the appropriate weight file and its default values if strcmp(get(handles.custom_weight_toggle, 'State'), 'on') weight_file = getappdata(0, 'custom_weight_path'); default_weight = 1; elseif strcmp(get(handles.axon_view_toggle, 'State'), 'off') weight_file = getappdata(0, 'fluorescent_weight_path'); default_weight = 0; else weight_file = getappdata(0, 'axon_weight_path'); default_weight = 0; end

%if the file does not exist, create it and fill it with default values if ~exist(weight_file, 'file') original_weights, linear_transform_weights, warp_weights = get_default_weights(default_weight); save(weight_file, 'original_weights', 'linear_transform_weights', 'warp_weights'); end
%get the information from the appropriate version if exist(get_file_path(), 'file') && exist(weight_file, 'file') && ~isempty(get_file_path()) && ~isempty(weight_file) if strcmp(image_version, 'T') && ~isempty(whos('-file', weight_file, 'linear_transform_weights')) weight_values = load(weight_file, 'linear_transform_weights'); weight_values = weight_values.linear_transform_weights; elseif strcmp(image_version, 'W') && ~isempty(whos('-file', weight_file, 'warp_weights')) weight_values = load(weight_file, 'warp_weights'); weight_values = weight_values.warp_weights; elseif ~isempty(whos('-file', weight_file, 'original_weights')) weight_values = load(weight_file, 'original_weights'); weight_values = weight_values.original_weights; end end
if ~isempty(weight_values) %find the matching weight value, then exit the loop for weight_index = 1:size(weight_values, 2) if strcmp(specimen_name, weight_values(weight_index).specimen_name) && strcmp(bone_name, weight_values(weight_index).bone_name) image_weight = weight_values(weight_index).value; weight_found = 1;
if (image_weight == 0 | strcmp(specimen_name, 'Avg')) & ~isempty(get_current_data()) image_weight = get_calculated_weight(handles, specimen_name, bone_name, image_version); set_image_weight(handles, specimen_name, bone_name, image_version, image_weight); end break; end end end else image_weight = 1; %set the flat weight weight_found = 1; end

%if the image is not listed in the weights, create a new entry for it if ~weight_found if strcmp(get(handles.automatic_weight_toggle, 'State'), 'off') image_weight = 1; else image_weight = 0; end

new_size = size(weight_values, 2) + 1; weight_values(new_size).specimen_name = specimen_name; weight_values(new_size).bone_name = bone_name; weight_values(new_size).value = image_weight;
save_weight_values(image_version, weight_values, weight_file); end

% -------------------------------------------------------------------- function calculated_weight = get_calculated_weight(handles, specimen_name, bone_name, image_version) %get the necessary variables and determine which function to use to the %weight calculations

image_data = []; calculated_weight = 0;

bone_name = bone_name, 'F'; image_file = get_image_file(handles, specimen_name, bone_name, image_version);

%get the image data through the appropriate method if ~isempty(image_file) if strcmp(image_version, 'T') || strcmp(image_version, 'W') image_data = load_converted_image(image_file, image_version); else image_data = imread(image_file); end end

bone_name(end) = 'I'; ideal_file = get_image_file(handles, 'Ideal', bone_name, 'O');

if ~isempty(ideal_file) && ~isempty(image_data) ideal_image = imread(ideal_file);

if strcmp(get(handles.axon_view_toggle, 'State'), 'on') ~, image_name, ~ = fileparts(image_file); calculated_weight = calculate_axon_weight(handles, image_data, image_name, ideal_image); else calculated_weight = calculate_fluorescent_weight(image_data, ideal_image); end end

% -------------------------------------------------------------------- function axon_weight = calculate_axon_weight(handles, image_data, image_name, ideal_image) axon_weight = 0; image_name(end) = 'D'; axon_positions = get_axon_positions(handles, image_name);

if ~isempty(ideal_image) && isequal( size(ideal_image), size(image_data) ) && ~isempty(axon_positions) %weight calculations mask = 255 - ideal_image( :, :, 1 ); %any location missing the red channel will become 255

row = round( axon_positions( :, 2 ) ) + 1; col = round( axon_positions( :, 1 ) ) + 1; rm = row > size( mask, 1 ) | row < 1 | col > size( mask, 2 ) | col < 1; row( rm ) = []; col( rm ) = []; ind = sub2ind( size( mask ), row, col ); axon_counter = sum( mask( ind ) > 0 ); if axon_counter > 0 axon_weight = 1 / axon_counter; end elseif isempty(ideal_image) errordlg('No Ideal image data available'); elseif ~isequal(size(ideal_image), size(image_data)) errordlg('Ideal and image sizes are different, please make them the same size'); elseif isempty(axon_positions) %errordlg('No axon positions found'); end

% -------------------------------------------------------------------- function fluorescent_weight = calculate_fluorescent_weight(image_data, ideal_image) fluorescent_weight = 0; if ~isempty(ideal_image) && isequal( size(ideal_image), size(image_data) ) %weight calculations mask = 255 - ideal_image( :, :, 1 ); mask = mask(:)';

keyboard %mask(:) = 1.0; % for sanity-checking weighting/scaling only - remove mask = double( mask ) / sum( mask ); image_data = mean( double( image_data ), 3 ) / 255; fluorescence = mask * image_data(:); fluorescent_weight = 1 ./ fluorescence;

elseif isempty(ideal_image) errordlg('No Ideal image data available'); elseif ~isequal(size(ideal_image), size(image_data)) errordlg('Ideal and image sizes are different, please make them the same size'); end

% -------------------------------------------------------------------- function set_image_weight(handles, specimen_name, bone_name, image_version, new_value) %set the weight of a single image

if strcmp(get(handles.custom_weight_toggle, 'State'), 'on') weight_file = getappdata(0, 'custom_weight_path'); elseif strcmp(get(handles.axon_view_toggle, 'State'), 'off') weight_file = getappdata(0, 'fluorescent_weight_path'); else weight_file = getappdata(0, 'axon_weight_path'); end

%determine which version of the image is being displayed and load the %appropriate weight information if strcmp(image_version, 'O') original_weights = load(weight_file, 'original_weights'); weight_values = original_weights.original_weights; elseif strcmp(image_version, 'T') linear_transform_weights = load(weight_file, 'linear_transform_weights'); weight_values = linear_transform_weights.linear_transform_weights; else warp_weights = load(weight_file, 'warp_weights'); weight_values = warp_weights.warp_weights; end

if ~isempty(weight_values) && ~isempty(weight_file) %find the matching image and change its weight value for weight_index = 1:size(weight_values, 2) if strcmp(specimen_name, weight_values(weight_index).specimen_name) && strcmp(bone_name, weight_values(weight_index).bone_name) weight_values(weight_index).value = new_value; save_weight_values(image_version, weight_values, weight_file); break; end end end

% -------------------------------------------------------------------- function original_weights, linear_transform_weights, warp_weights = get_default_weights(default_value) %function default_weights = get_default_weights(default_value) %since this function is hard to use 'find' with, I put some search terms %below

table_cells = get_table_cells(); weight_data = struct('specimen_name', {}, 'bone_name', {}, 'weight_value', {}); weight_index = 1;

%cycle through all the specimen_names, bone_names, and %version_types and add them to each structure array for specimen_index = 2:size(table_cells, 1) for bone_index = 2:size(table_cells, 2) weight_data(weight_index).specimen_name = get(table_cells(specimen_index, 1), 'string'); weight_data(weight_index).bone_name = get(table_cells(1, bone_index), 'String'); weight_data(weight_index).value = default_value;

weight_index = weight_index + 1; end end

original_weights = weight_data; linear_transform_weights = weight_data; warp_weights = weight_data;

% -------------------------------------------------------------------- function set_outdated_weight(handles, image_file ,image_version) if nargin < 3, image_version = []; end if nargin < 2, image_file = []; end if isempty(image_version), image_version = get_image_version(handles); end if isempty(image_file), image_file = get_file_path(); end

if strcmp(get(handles.automatic_weight_toggle, 'State'), 'on') && ~isempty(image_file) && exist(image_file, 'file') specimen_name, bone_name = parse_image_name(image_file); bone_name(end) = ''; set_image_weight(handles, specimen_name, bone_name, image_version, 0); end

% -------------------------------------------------------------------- function save_weight_values(image_version, weight_values, weight_file) %save the image weights to the appropriate mat file if strcmp(image_version, 'O') original_weights = weight_values; save(weight_file, 'original_weights', '-append'); elseif strcmp(image_version, 'T') linear_transform_weights = weight_values; save(weight_file, 'linear_transform_weights', '-append'); elseif strcmp(image_version, 'W') warp_weights = weight_values; save(weight_file, 'warp_weights', '-append'); end

% -------------------------------------------------------------------- % -------------------------------------------------------------------- %Helper Functions % -------------------------------------------------------------------- % --------------------------------------------------------------------

% -------------------------------------------------------------------- function find_image_file(handles) %determine the current file based on the position of the selected cell, its %row's specimen name, column's bone name, and the selected image type

specimen_name, bone_name = get_selected_names();

if strcmp(specimen_name, 'Avg') image_path = getappdata(0, 'save_directory'); elseif strcmp(get(handles.axon_view_toggle, 'State'), 'on') image_path = getappdata(0, 'axon_directory'); else image_path = getappdata(0, 'main_directory'); end

bone = bone_name, get_image_type(handles); image_file = get_image_file(handles, specimen_name, bone, 'O');

if isempty(image_file) image_identifier = get_image_identifier(handles, specimen_name); image_file = image_path, filesep, specimen_name, '*_*', bone_name, image_identifier; %build the image string to search for end

set_file_path(image_file);

% -------------------------------------------------------------------- function image_identifier = get_image_identifier(handles, specimen_name) if nargin < 2, specimen_name = []; end

if isempty(specimen_name) specimen_name, ~ = parse_image_name(get_file_path()); end

%determine the type of image to display if strcmp(specimen_name, 'Ideal') image_identifier = 'I.*'; else image_identifier = get_image_type(handles); image_identifier = strcat(image_identifier, '.*'); end

% -------------------------------------------------------------------- function image_version = get_image_version(handles) if strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') image_version = 'T'; elseif strcmp(get(handles.warp_view_toggle, 'State'), 'on') image_version = 'W'; else image_version = 'O'; end

% -------------------------------------------------------------------- function image_type = get_image_type(handles) if strcmp(get(handles.composite_view_toggle, 'State'), 'on') image_type = 'C'; elseif strcmp(get(handles.fluorescent_view_toggle, 'State'), 'on') image_type = 'F'; else image_type = 'D'; end

% -------------------------------------------------------------------- function axon_positions = get_axon_positions(handles, image_file, image_version) if nargin < 3, image_version = []; end if nargin < 2, image_file = []; end if isempty(image_version), image_version = get_image_version(handles); end if isempty(image_file), image_file = get_file_path(); end

~, file_name, ~ = fileparts(image_file); axon_positions = [];

if ~isempty(getappdata(0, 'axon_directory')) if strcmp(image_version, 'O') file_name(end) = 'A'; axon_path = getappdata(0, 'axon_directory'), filesep, file_name, '.csv';

if exist(axon_path, 'file') axon_positions = csvread(axon_path); end else axon_path = strcat(getappdata(0, 'save_directory'), filesep, file_name, '.mat');
if strcmp(image_version, 'T') axon_positions = load_converted_image(axon_path, 'TA'); elseif strcmp(image_version, 'W') axon_positions = load_converted_image(axon_path, 'WA'); end end end

%remove any invalid positions if ~isempty(axon_positions) axon_positions(axon_positions < 0) = []; end

% -------------------------------------------------------------------- function specimen_in_preferences = is_specimen_in_preferences(specimen_name) specimen_in_preferences = 0; checkbox_file = getappdata(0, 'checkbox_preferences_file'); checkbox_preferences = [];

%check that the file name for the checkbox preferences is not empty and %that it exists if ~isempty(checkbox_file) && exist(checkbox_file, 'file') file_data = load(checkbox_file); %load the user's preferences for the checkboxes checkbox_preferences = file_data.checkbox_preferences; %get the struct array containing the preferences end

%cycle through the preferences and check if a specimen is listed if ~isempty(checkbox_preferences) for file_index = 1:size(checkbox_preferences, 2) if strcmp(checkbox_preferences(file_index).specimen_name, specimen_name) specimen_in_preferences = 1; break; end end end

% -------------------------------------------------------------------- function valid_transform = is_valid_transform(handles, specimen_name, bone_name, image_version) %determine if a transform can be done on the specified image %if the transform can be done, return 1 %else return a struct array with information on what to display and what %the message should say

if nargin < 4, image_version = []; end if nargin < 3, bone_name = []; end if nargin < 2, specimen_name = []; end

if isempty(image_version) image_version = get_image_version(handles);

if strcmp(image_version, 'O') image_version = 'T'; end end

if isempty(bone_name) ~, bone_name = parse_image_name(get_file_path()); end

if isempty(specimen_name) specimen_name, ~ = parse_image_name(get_file_path()); end

valid_transform = struct('display_button', 1, 'error_message', ''); image_name = get_image_file(handles, specimen_name, bone_name, 'O');

if ~isempty(image_name) && ~strcmp(specimen_name, 'Avg') && ~strcmp(specimen_name, 'Ideal') ~, file_name, file_extension = fileparts(image_name); save_path = strcat(getappdata(0, 'save_directory'), filesep, file_name); main_path = strcat(getappdata(0, 'main_directory'), filesep, file_name, file_extension);

if exist(image_name, 'file') %determine if the button should be displayed and what it should say if strcmp(image_version, 'T') && strcmp(get(handles.linear_transform_view_toggle, 'State'), 'on') set(handles.generator_button, 'Enable', 'on', 'Visible', 'on', 'String', 'Make Linear Transformed Image'); set_outdated_weight(handles, 'T'); set_outdated_weight(handles, 'W'); elseif strcmp(image_version, 'W') && strcmp(get(handles.warp_view_toggle, 'State'), 'on') set(handles.generator_button, 'Enable', 'on', 'Visible', 'on', 'String', 'Make Warped Image'); set_outdated_weight(handles, 'W'); end
valid_axons = 1; image_references = []; number_of_references = 0; outdated_linear_transform_check = 1; %keep track of whether or not the linear transform image is outdated
%if the current image is a warped image, check whether or not the %associated linear transformed image is outdated if strcmp(image_version, 'W') & is_image_outdated(handles, save_path, 'T') outdated_linear_transform_check = 0; end
%lower the image version by one to get the appropriate references if strcmp(image_version, 'T') image_version = 'O'; elseif strcmp(image_version, 'W') image_version = 'T'; end
marker_positions = get_image_references(handles, main_path, image_version); ideal_bone = bone_name(1:end-1), 'I'; %remove the image identifier and replace it with 'I' ideal_image = get_image_file(handles, 'Ideal', ideal_bone); ideal_references = get_image_references(handles, ideal_image, 'O');
is_array_valid = 0; %holds the boolean value for if valid_image_references matches valid_ideal_references
%check that the ideal_reference_file is not empty if ~isempty(ideal_references) ideal_references = convert_references_double(ideal_references); image_references = convert_references_double(marker_positions);
%if the image_references match the ideal_references, set the linear %transform and warp images to outdated if isequal(ideal_references, image_references) save_converted_image(save_path, 1, 'TO'); save_converted_image(save_path, 1, 'WO'); end
is_array_valid, number_of_references = get_is_array_valid(image_references, ideal_references); end
if strcmp(get(handles.axon_view_toggle, 'State'), 'on') if isempty(get_axon_positions(handles, get_file_path(), image_version)) valid_axons = 0; end end
valid_size = is_valid_size(handles, image_name);
if valid_axons && ~isempty(image_references) && ~isempty(ideal_references) && ~isequal(ideal_references, image_references) && is_array_valid && outdated_linear_transform_check && (number_of_references >= 3) && valid_size valid_transform = 1; elseif ~valid_size valid_transform.display_button = 0; valid_transform.error_message = 'Image size does not match ideal image'; elseif ~outdated_linear_transform_check valid_transform.display_button = 1; valid_transform.error_message = 'Linear transform image is outdated'; elseif ~valid_axons %alert the user if the axon reference point file is missing ~, file_name, ~ = fileparts(save_path); error_message = ['Image ', file_name, ' did not have an associated axon reference file in: ', getappdata(0, 'axon_directory')]'Image ', file_name, ' did not have an associated axon reference file in: ', getappdata(0, 'axon_directory'); %strcat removes spaces, using a character array resolves this problem
valid_transform.display_button = 0; valid_transform.error_message = error_message; elseif isempty(ideal_references) valid_transform.display_button = 1; valid_transform.error_message = 'Ideal references are missing'; elseif isempty(image_references) valid_transform.display_button = 1; valid_transform.error_message = 'Image references are missing'; elseif isequal(image_references, ideal_references) valid_transform.display_button = 1; valid_transform.error_message = 'Image and Ideal references are identical'; elseif ~is_array_valid missing_references = (number_of_references - size(image_references, 1) * 2) + 1; %an array to hold a list of letters, representing mismatched references points, the spaces in between them, and one space in front of the string missing_references(1) = ' '; missing_count = 2; %used to iterate through the missing references largest_size = max(size(image_references, 1), size(ideal_references, 1)); smallest_size = min(size(image_references, 1), size(ideal_references, 1)); marker_labels = get_marker_labels();
%save the largest set of references in a new variable if size(image_references, 1) > size(ideal_references, 1) largest_references = image_references; else largest_references = ideal_references; end
for reference_index = 1:largest_size %if the size of the reference points do not match, find the %valid references in the largest reference array and mark %them as mismatched %elseif the index is less than or equal to the smallest %array and the reference points are neither both positive %nor negative, mark the point as mismatched if reference_index > smallest_size && largest_references(reference_index, 1) >= 0 && largest_references(reference_index, 2) >= 0 missing_references, missing_count = get_missing_references_and_count(missing_references, missing_count, marker_labels(reference_index)); elseif reference_index <= smallest_size if((image_references(reference_index, 1) < 0) ~= (ideal_references(reference_index, 1) < 0)) || ((image_references(reference_index, 2) < 0) ~= (ideal_references(reference_index, 2) < 0)) missing_references, missing_count = get_missing_references_and_count(missing_references, missing_count, marker_labels(reference_index)); end end end
button_message = strcat('Ideal and image references are mismatched:', char(missing_references)); %construct the error message to display to the user
valid_transform.display_button = 0; valid_transform.error_message = button_message; elseif number_of_references < 3 valid_transform.display_button = 1; valid_transform.error_message = 'The Ideal image must have 3 or more reference points'; end end else %if the file is not found, display the button with a message telling %the user which file is missing and which directory it is looking in file_name = specimen_name '_' bone_name '.*'; if strcmp(image_version, 'T') || strcmp(image_version, 'W') file_path = getappdata(0, 'save_directory'); else file_path = getappdata(0, 'main_directory'); end
error_message = 'Image ', file_name, ' was not found in directory: ', file_path; %strcat removes spaces, using a character array resolves this problem
valid_transform.display_button = 0; valid_transform.error_message = error_message; end

% -------------------------------------------------------------------- function missing_references missing_count = get_missing_references_and_count(missing_references, missing_count, reference_letter) missing_references(missing_count) = reference_letter; missing_count = missing_count + 1; missing_references(missing_count) = ' '; missing_count = missing_count + 1;

% -------------------------------------------------------------------- function specimen_name, bone_name = parse_image_name(image_file) if nargin < 1, image_file = get_file_path(); end ~, file_name, ~ = fileparts(image_file); name_parts = regexp(file_name, '_', 'Split'); specimen_name = name_parts{1}; bone_name = name_parts{end};

% -------------------------------------------------------------------- function image_references = get_image_references(handles, image_file, image_version) %return a struct array containing the reference points if nargin < 3, image_version = []; end if nargin < 2, image_file = []; end if isempty(image_version), image_version = get_image_version(handles); end if isempty(image_file), image_file = get_file_path(); end

marker_labels = get_marker_labels(); image_references = struct('letter', {}, 'x_position', {}, 'y_position', {}); specimen_name, bone_name = parse_image_name(image_file);

if exist(image_file, 'file') ~, file_name, ~ = fileparts(image_file); reference_points = [];

%the ideal reference points can only be found in the associated csv file name_parts = regexp(file_name, '_', 'Split'); if strcmp(name_parts(1), 'Ideal') image_version = 'O'; end
if strcmp(image_version, 'O') || strcmp(image_version, 'W') if strcmp(image_version, 'W') || strcmp(specimen_name, 'Ideal') bone_name(end) = 'I'; ideal_file = get_image_file(handles, 'Ideal', bone_name, 'O'); ~, file_name, ~ = fileparts(ideal_file); end
if ~isempty(file_name) file_name(end) = 'R'; csv_file = getappdata(0, 'save_directory') filesep file_name '.csv';
if exist(csv_file, 'file') reference_file = fopen(csv_file, 'r'); reference_points = csvread(csv_file); fclose(reference_file); end end elseif strcmp(image_version, 'T') mat_file = getappdata(0, 'save_directory') filesep file_name '.mat'; reference_points = load_converted_image(mat_file, 'TR'); end
if ~isempty(reference_points) for reference_index = 1:size(reference_points, 1) image_references(reference_index).letter = marker_labels(reference_index); image_references(reference_index).x_position = reference_points(reference_index, 1); image_references(reference_index).y_position = reference_points(reference_index, 2); end end end

% -------------------------------------------------------------------- function references_double = convert_references_double(reference_points) %convert the struct array of the reference_points to an Nx2 double array

references_double = []; if isstruct(reference_points) references_double = zeros(size(reference_points, 2), 2); for reference_index = 1:size(reference_points, 2) references_double(reference_index, 1) = reference_points(reference_index).x_position; references_double(reference_index, 2) = reference_points(reference_index).y_position; end end

% -------------------------------------------------------------------- function cleaned_references = get_cleaned_references(image_references) %remove improper values from the image's reference points

invalid_positions = 0; %remove all the trailing -1's in the array for position_index = size(image_references, 2):-1:1 if image_references(position_index).x_position < 0 | image_references(position_index).y_position < 0 | isempty(image_references(position_index)) image_references(position_index) = ''; invalid_positions = 1; elseif isempty(image_references(position_index)) | isempty(image_references(position_index).x_position) | isempty(image_references(position_index).x_position) image_references(position_index) = ''; invalid_positions = 1; else break; end end

%remove any empty elements in the array for position_index = 1:size(image_references, 2) if isempty(image_references(position_index).x_position) | isempty(image_references(position_index).y_position) image_references(position_index).x_position = -1; image_references(position_index).y_position = -1; invalid_positions = 1; end end

%save the corrected array to the file if invalid_positions save_path = getappdata(0, 'save_directory') filesep getappdata(0, 'current_file'); save_image_references(save_path, image_references); %save the positions of the reference points end

cleaned_references = image_references;

% -------------------------------------------------------------------- function is_array_valid, number_of_references = get_is_array_valid(image_references, ideal_references) %function is_array_valid = get_is_array_valid(image_references, ideal_references) %determine if each reference point has a partner in the other array

valid_image_references = all( image_references >= 0, 2 ); valid_ideal_references = all( ideal_references >= 0, 2 ); is_array_valid = isequal(valid_image_references, valid_ideal_references); %compare arrays to determine if they are the same number_of_references = sum(valid_image_references); %if a reference point is valid the element equals 1. Adding all the one's together states how many valid reference points there are

% -------------------------------------------------------------------- function display_transform_error(handles, valid_transform) if isstruct(valid_transform) if valid_transform.display_button set(handles.error_text_display, 'String', '', 'Visible', 'off'); set(handles.generator_button, 'Enable', 'off', 'Visible', 'on', 'String', valid_transform.error_message); else set(handles.error_text_display, 'Visible', 'on', 'String', valid_transform.error_message); set(handles.generator_button, 'Enable', 'off', 'Visible', 'off'); end end

% -------------------------------------------------------------------- function image_file = get_image_file(handles, specimen_name, bone_name, image_version) %if the file is found, return a string with the directory, name, and extension of the file %else return empty

if nargin < 4, image_version = ''; end if nargin < 3, bone_name = ''; end if nargin < 2, specimen_name = ''; end

if isempty(image_version), image_version = get_image_version(handles); end if isempty(bone_name), ~, bone_name = parse_image_name(get_file_path()); end if isempty(specimen_name), specimen_name, ~ = parse_image_name(get_file_path()); end

if strcmp(specimen_name, 'Ideal') image_version = 'O'; end

%get the appropriate extension and directory if strcmp(image_version, 'T') || strcmp(image_version, 'W') file_types = {'.mat'}; image_path = getappdata(0, 'save_directory'); elseif strcmp(specimen_name, 'Avg') file_types = getappdata(0, 'valid_image_extensions'); image_path = getappdata(0, 'save_directory'); else file_types = getappdata(0, 'valid_image_extensions'); image_path = getappdata(0, 'main_directory'); end

image_file = '';

%remove any '*' from the specimen and bone names specimen_name = regexprep(specimen_name, '*', ''); bone_name = regexprep(bone_name, '*', '');

search_string = strcat(image_path, filesep, specimen_name, '*_*', bone_name, '.*'); %build the image string to search for available_files = dir(search_string); %get a list of files from the directory available_files = {available_files.name}; %get only the names and file extensions of the available_files

%cycle through the files and compare their file extensions with the %array for file_index = 1:size(available_files, 2) ~,~,file_extension = fileparts(available_files{file_index}); %get the file extension from one of the available_files for type_index = 1:size(file_types, 2) %if the file extensions match, set the image_file to the %matching available file if strcmpi(file_extension, file_types{type_index}) image_file = image_path, filesep, available_files{file_index}; %build the string to create the image_file break; end end end

% -------------------------------------------------------------------- function image_outdated = is_image_outdated(handles, image_name, image_version) if nargin < 3, image_version = []; end if nargin < 2, image_name = []; end if isempty(image_version), image_version = get_image_version(handles); end if isempty(image_name), image_name = get_file_path(); end image_outdated = 1;

specimen_name, bone_name = parse_image_name(image_name); image_name = get_image_file(handles, specimen_name, bone_name, image_version);

if strcmp(image_version, 'O') image_outdated = 0; elseif exist(image_name, 'file') keyword = image_version, 'O'; image_outdated = load_converted_image(image_name, keyword);

if isempty(image_outdated) image_outdated = 1; end end

% -------------------------------------------------------------------- function valid_size = is_valid_size(handles, image_file) if nargin < 2, image_file = []; end if isempty(image_file), image_file = get_file_path(); end

valid_size = 0; specimen_name, bone_name = parse_image_name(image_file);

if ~isempty(specimen_name) & ~isempty(bone_name) image_file = get_image_file(handles, specimen_name, bone_name, 'O'); end

if ~isempty(image_file) bone_name(end) = 'I'; ideal_file = get_image_file(handles, 'Ideal', bone_name);

if ~isempty(ideal_file) ideal_information = imfinfo(ideal_file); image_information = imfinfo(image_file);
if image_information.Height == ideal_information.Height && image_information.Width == ideal_information.Width valid_size = 1; end else valid_size = -1; %the ideal image was not found end end

% -------------------------------------------------------------------- function specimen_name, bone_name = get_selected_names() %function selected_names = get_selected_position_names() %return the specimen and bone name based on the selected cell position table_cells = get_table_cells(); selected_position = get_selected_position(); specimen_name = get(table_cells(selected_position(1), 1), 'String'); bone_name = get(table_cells(1, selected_position(2)), 'String');

% -------------------------------------------------------------------- function display_loading(handles) %prevent 'loading...' from being displayed at startup if ~isempty(getappdata(0, 'main_directory')) set(handles.generator_button, 'Visible', 'on', 'Enable', 'off', 'String', 'Loading...'); set_gui_enabled(handles, 'off'); drawnow end

% -------------------------------------------------------------------- function reset_alternate() %clear all the alternate variables set_alternate_file_path(''); set_alternate_type(''); set_alternate_version(''); set_alternate_heatmap(''); set_alternate_reference_view(''); set_alternate_reference_mover(''); set_alternate_reference_deleter(''); set_alternate_ideal(''); set_alternate_mask('');