Skip to content

Instantly share code, notes, and snippets.

@kmader
Forked from kmader/mandelbrot.condor
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kmader/c96640bdbd8961ef9ec7 to your computer and use it in GitHub Desktop.
Save kmader/c96640bdbd8961ef9ec7 to your computer and use it in GitHub Desktop.
A condor script for filtering and thresholding a given image using a number of different parameters
##################################################################
##
## Run a parameter sweep with threshold and filter using condor
## Filename: batchimage.condor
##
##################################################################
universe = vanilla
getenv = True # MATLAB needs local environment
#initialdir = /home/foo/src/matlab
my_prefix = FilterTest
#
# Seek max floating point performance
#
Rank = Kflops
#
# Filter and Threshold Settings
#
thresh_count= 30
filter_count = 1
job_count = $(thresh_count) * $(filter_count)
threshold_start = 0.25
threshold_end = 0.75
#
# MATLAB does all the math there,
# Condor just does string substitution
#
cur_threshold = ( $(threshold_start) + ($(threshold_end) - $(threshold_start))*$(Process)/$(thresh_count) )
cur_filter = 1
#
# The name of the image file we want MATLAB to write
#
my_file = $(my_prefix).t.$(Process).csv
#
# For MATLAB and other SEPP packages, the executable must be a script wrapper.
#
executable = filterandthreshold.sh
arguments = saveresults('input_image.jpg',$(cur_threshold), '$(my_file)')
#
# To redirect stdout and/or stderr to /dev/null, comment these out.
#
log = $(my_prefix).log
output = $(my_prefix).$(Process).out
error = $(my_prefix).$(Process).err
#
# Lastly, tell condor how many jobs to queue up.
#
queue $(job_count)
% this function (will) performs the ellipsoid based shape analysis on a 3D image
% the input is a labeled image probably from the bwlabel function
% the output is an array formatted like such
% labelId, volume, centerX, centerY, centerZ, extentsX, extentsY, extentsZ, pca1X, pca1Y, pca1Z, score1, score2, score3
function [out_table]=ellipsoid_analysis(in_image,out_file)
% create an array the same size as the image of x,y, and z points. This way we can calculate
% center of volume and other parameters by using multiplication
[xgrid,ygrid,zgrid]=meshgrid(1:size(in_image,1),1:size(in_image,2),1:size(in_image,3));
num_of_obj=max(in_image(:));
% assign the columns to the output table
col_names={'label','volume','cov_x','cov_y','cov_z','ext_x','ext_y','ext_z','pca1_x','pca1_y','pca1_z','score_1','score_2','score_3'};
num_of_outputs=length(col_names);
% initialize the output array
out_table=zeros(num_of_obj,num_of_outputs);
for cur_obj = 1 : num_of_obj
% save the label and the volume
out_table(cur_obj,getnameidx(col_names,'label'))=cur_obj;
out_table(cur_obj,getnameidx(col_names,'volume'))=sum(in_image(:)==cur_obj);
% get the x,y, and z positions of each voxel
xpts=xgrid(in_image==cur_obj);
ypts=ygrid(in_image==cur_obj);
zpts=zgrid(in_image==cur_obj);
out_table(cur_obj,getnameidx(col_names,'cov_x'))=mean(xpts);
out_table(cur_obj,getnameidx(col_names,'cov_y'))=mean(ypts);
out_table(cur_obj,getnameidx(col_names,'cov_z'))=mean(zpts);
% extents analysis
out_table(cur_obj,getnameidx(col_names,'ext_x'))=range(xpts);
out_table(cur_obj,getnameidx(col_names,'ext_y'))=range(ypts);
out_table(cur_obj,getnameidx(col_names,'ext_z'))=range(zpts);
% shape tensor analysis
covar_pts=cov([xpts(:) ypts(:) zpts(:)]);
% in case it is only one point
if (prod(size(covar_pts))<9)
covar_pts=ones(3);
end
% save the first (largest) eigenvector as pca1,
% and the scores (eigenvalues) as score1...
[evec,evals]=eig(covar_pts);
evals=diag(evals); % scores are in a diagonal matrix
first_comp=evec(:,3);
out_table(cur_obj,getnameidx(col_names,'pca1_x'))=first_comp(1);
out_table(cur_obj,getnameidx(col_names,'pca1_y'))=first_comp(2);
out_table(cur_obj,getnameidx(col_names,'pca1_z'))=first_comp(3);
out_table(cur_obj,getnameidx(col_names,'score_1'))=evals(3);
out_table(cur_obj,getnameidx(col_names,'score_2'))=evals(2);
out_table(cur_obj,getnameidx(col_names,'score_3'))=evals(1);
end
% make and write the header
header_txt=sprintf('%s,',col_names{:});
header_txt(end)=''; % delete the last comma
dlmwrite(out_file,header_txt,'');
% write the table
dlmwrite(out_file,out_table,'-append','delimiter',',');
%
% Filename: filterandthreshold.m
% Performs the filtering, thresholding and component labeling
%
function [label_img]=filterandthreshold(in_image,threshold_value)
filt_image=medfilt2(in_image,[3,3]);
thresh_img=filt_image<=threshold_value;
label_img=bwlabel(thresh_img);
#!/bin/sh
#
# Filename: mandelbrot.sh
#
# We use a shell wrapper for two reasons:
#
# 1) By using "$*" we ensure that the matlab command string is
# passed as a single argument even if it contains spaces.
#
# 2) Condor changes argv[0], which causes problems for SEPP. Hence,
# whenever we run a program from /usr/sepp/bin/* we must use a
# shell script wrapper.
#
exec /usr/sepp/bin/matlab -nojvm -nodisplay -nodesktop -nosplash -r "$*"
%
% Filename: saveresults.m
%
function saveresults(im_name,threshold_value,filename)
gray_img=imread(im_name);
% run the filter / threshold
lab_img=filterandthreshold(gray_img,threshold_value);
% run shape analysis
ellipsoid_analysis(lab_img,filename);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment