function [mesh, idx] = fix_meshes(myconn, emesh, selection) % [idx] = fix_meshes(conn, myconn, emesh, term, selection) % flips meshes of selection if misannotated % conn = pg conn handle % myconn = mysql conn handle % emesh % selection = cell array of filenames % or index in emesh % or struct with idx and stain % stain will replace the entries in emesh % returns flipped meshes and index in emesh % % example: % fix_meshes(conn,myconn,emesh,{'insitu19957.jpe', 'insitu19936.jpe', 'insitu19912.jpe', 'insitu19891.jpe'}) v_flip = flipmesh(emesh.p, emesh.t, 'vertical'); h_flip = flipmesh(emesh.p, emesh.t, 'horizontal'); if iscell(selection), sel_files = selection; sel_idx = zeros(length(selection),1); for i=1:length(sel_files) match = strmatch(sel_files{i}, emesh.files); sel_idx(i) = match; end else if isstruct(selection), sel_idx = selection.idx; for i=1:length(selection.idx), emesh.stain(:,selection.idx(i)) = selection.stain(:,i); end else sel_idx = selection; end for i=1:length(sel_idx), sel_files{i} = emesh.files{sel_idx(i)}; end end j=1; mesh = zeros(size(emesh.stain,1), length(sel_idx)); idx = zeros(length(sel_idx),1); for i=1:length(sel_idx), im_orient = pg_query(myconn, 0, my_insitus('image_orientation'), myinsitu2fn(sel_files{i})); % fprintf('%s: %s, %s\n', sel_files{i}, im_orient{1}, im_orient{2}); fprintf('No. %d, idx %d: ', i, j); if strcmp(im_orient{1}, 'image_reject') || strcmp(im_orient{1}, 'mesh_reject'), fprintf('Reject: %s\n', sel_files{i}); continue; else idx(j) = sel_idx(i); end if length(im_orient) < 2, fprintf('%s data missing\n', sel_files{i}); mesh(:,j) = emesh.stain(:, sel_idx(i)); elseif strcmp(im_orient{2}, 'AP_inverted'), fprintf('AP: %s\n', sel_files{i}); mesh(:,j) = emesh.stain(h_flip, sel_idx(i)); elseif strcmp(im_orient{2}, 'DV_inverted'), fprintf('DV: %s\n', sel_files{i}); mesh(:,j) = emesh.stain(v_flip, sel_idx(i)); elseif strcmp(im_orient{2}, 'AP_DV_inverted') fprintf('AP/DV: %s\n', sel_files{i}); mesh(:,j) = emesh.stain(h_flip, sel_idx(i)); mesh(:,j) = mesh(v_flip, j); else fprintf('OK: %s\n', sel_files{i}); mesh(:,j) = emesh.stain(:, sel_idx(i)); end j = j + 1; end mesh = mesh(:,1:j-1); idx = idx(1:j-1); end