0001 function [nucimg, cellimg] = model2diffeomorphicInstance( model, param )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 options = model.nuclearShapeModel.shape_space_options;
0040
0041 try
0042 verbose = param.verbose;
0043 if isa( verbose, 'logical' )
0044 verbose = false;
0045 end
0046 catch
0047 verbose = false;
0048 end
0049
0050
0051 try
0052 debug = param.debug;
0053 if isa( debug, 'logical' )
0054 debug = false;
0055 end
0056 catch
0057 debug = false;
0058 end
0059
0060
0061 if ~isfield(param,'framefolder')
0062 framefolder = ['.' filesep 'frames' filesep];
0063 else
0064 framefolder = param.framefolder;
0065 end
0066
0067 if ~isdir(framefolder)
0068 mkdir(framefolder);
0069 end
0070
0071
0072 try
0073
0074 if ~isfield(param,'randomwalk')||param.randomwalk==0
0075
0076
0077 number_shapes = size(model.nuclearShapeModel.positions, 1);
0078
0079
0080
0081 random_weights = rand(number_shapes, 1);
0082 random_weights = random_weights ./ sum(random_weights);
0083
0084
0085
0086 random_point = random_weights' * model.nuclearShapeModel.positions;
0087
0088
0089
0090 options.use_compression = false;
0091
0092
0093 elseif param.randomwalk==1
0094
0095
0096
0097
0098 if ~isfield(param,'walksteps')
0099 warning(['No walkstep number specified (param.walksteps).',...
0100 'synthesizing only 1 image']);
0101 maxIter = 1;
0102 else
0103 maxIter = param.walksteps;
0104 end
0105
0106
0107
0108
0109
0110
0111 if isdir([framefolder filesep 'Cellwalk'])
0112 donecells = ml_ls([framefolder filesep 'Cellwalk' filesep '*.tif']);
0113 donenucs = ml_ls([framefolder filesep 'Nucwalk' filesep '*.tif']);
0114 if length(donecells)==param.walksteps
0115
0116
0117
0118
0119 nucimg = ml_readimage(donenucs{1});
0120 cellimg = ml_readimage(donecells{1});
0121 for z = 1:size(cellimg,3)
0122 nucimg(:,:,z) = imfill(nucimg(:,:,z),'holes');
0123 cellimg(:,:,z) = imfill(cellimg(:,:,z),'holes');
0124 end
0125
0126 return
0127 end
0128 end
0129
0130
0131
0132 savepath = [framefolder filesep 'walk'];
0133
0134
0135
0136
0137
0138 if isfield(param,'walk_type')
0139 walk_type = param.walk_type;
0140 else
0141
0142 walk_type = 'willmore';
0143 end
0144
0145 if strcmpi(walk_type,'density')
0146 energies = model.cellShapeModel.density';
0147 elseif strcmpi(walk_type,'willmore')
0148 energies = model.cellShapeModel.Willmore_energy;
0149 else
0150 energies = ones(size(model.cellShapeModel.positions,1),1);
0151 end
0152
0153
0154
0155
0156 if isfield(model.cellShapeModel,'motion')
0157 if isfield(model.cellShapeModel.motion,'Dc')
0158 Dc = model.cellShapeModel.motion.Dc;
0159 else
0160 warning(['No diffusion constant found in motion model,'...
0161 'defaulting to 10e-3']);
0162 Dc = 10e-3;
0163 end
0164 if isfield(model.cellShapeModel.motion,'dt')
0165 dt = model.cellShapeModel.motion.dt;
0166 else
0167 warning(['No time step found in motion model,'...
0168 'defaulting to 1']);
0169 dt = 1;
0170 end
0171 else
0172 warning(['Shape space walk requested with no motion model.'...
0173 'defaulting diffusion = 10e-3, time step = 1']);
0174
0175 Dc = 10e-3;
0176
0177
0178 dt = 1;
0179 end
0180
0181
0182
0183 y2 = model.nuclearShapeModel.positions;
0184 convex_hull = model.nuclearShapeModel.convex_hull;
0185 tes = model.nuclearShapeModel.tesselation;
0186
0187 shape_space = { y2, convex_hull, tes};
0188
0189
0190 [result, success] = generate_walk_path(...
0191 shape_space, walk_type, Dc, dt, maxIter, savepath,energies);
0192
0193 random_point = result.walk_path;
0194
0195 end
0196
0197
0198 try
0199 default_options.method = param.synthesis.diffeomorphic.method;
0200 catch
0201
0202
0203 default_options.method = 'convnfft_fast';
0204 end
0205
0206
0207 try
0208 default_options.convergence_absolute_error = param.synthesis.diffeomorphic.convergence_absolute_error;
0209 catch
0210
0211 end
0212
0213
0214
0215 try
0216 default_options.maximum_iterations = param.synthesis.diffeomorphic.maximum_iterations;
0217 catch
0218 default_options.maximum_iterations = 100;
0219 end
0220
0221
0222 try
0223 default_options.convergence_tolerance = param.synthesis.diffeomorphic.convergence_tolerance;
0224 catch
0225 default_options.convergence_tolerance = 0;
0226
0227 end
0228
0229
0230 try
0231 default_options.convergence_registration_error_scale = param.synthesis.diffeomorphic.convergence_registration_error_scale;
0232 catch
0233 default_options.convergence_registration_error_scale = 5e-3;
0234 end
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245 default_options.alpha = 1;
0246 default_options.gamma = 0.04;
0247 default_options.keep_proportion = 0;
0248 default_options.single_sided = false;
0249 default_options.max_time = 50000;
0250 default_options.step_size = 0.01;
0251 default_options.use_fft = true;
0252 default_options.use_gaussian_kernel = false;
0253 default_options.absolute_distance_tolerance = 0;
0254 default_options.absolute_deformation_tolerance = 0;
0255 default_options.periodic_space = false;
0256 default_options.use_compression = false;
0257 default_options.drop_kernel = true;
0258 default_options.maximum_registration_error_failures = 0;
0259
0260
0261
0262
0263 try
0264 default_options.downsampling_scale = param.synthesis.diffeomorphic.downsampling_scale;
0265 catch
0266 default_options.downsampling_scale = 1;
0267 end
0268
0269 default_options.maximum_deformation_per_step = [4/default_options.downsampling_scale, 4/default_options.downsampling_scale, 0.5];
0270
0271
0272 default_options.window_radius = 192/default_options.downsampling_scale;
0273 default_options.filter_radius = 16/default_options.downsampling_scale;
0274
0275 options = process_options_structure(default_options, options);
0276
0277
0278
0279
0280 frame = cell(1,size(random_point,1));
0281 nucimg = cell(1,size(random_point,1));
0282 cellimg = cell(1,size(random_point,1));
0283
0284 energy = zeros(1,size(random_point,1));
0285 if size(random_point,1)>1 && isfield(param,'templateJob')
0286 mkdir walkscripts
0287 end
0288
0289 param = ml_initparam(param,struct('tempdir',[pwd filesep 'temp' filesep]));
0290 if size(random_point,1)>1
0291 disp(['Random walk detected,',...
0292 'generating and saving frames of the movie to disk. ',...
0293 'The method will only return the final frame to save memory']);
0294
0295 end
0296 for i = 1:size(random_point,1)
0297 framefile = [param.tempdir filesep 'genframe' num2str(i)];
0298 [startimage, ~, ~, framefile] = chunk_start(framefile);
0299
0300 if startimage
0301 try
0302 currframe = i;
0303 curr_point = random_point(i,:);
0304
0305
0306
0307 makeFrame(model,options,curr_point,framefolder,currframe);
0308 chunk_finish(param.tempdir,['genframe' num2str(i)])
0309 catch err
0310 disp(['Skipping image ' num2str(i) ' due to error']);
0311 getReport( err, 'extended')
0312 end
0313 else
0314 disp(['Result file found, Skipping image ' num2str(i) ' ']);
0315 end
0316
0317 chunk_finish(framefile)
0318 end
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330 if ~isempty(dir([param.tempdir filesep '*.tmp']))
0331 warning(['CellOrganizer has detected .tmp files still exist.',...
0332 'Either a process is still working or one or more frame',...
0333 'failed to generate. Returning empty cell and nuclear image.']);
0334 cellimg = [];
0335 nucimg = [];
0336 else
0337 cellimg = ml_readimage([framefolder filesep 'Cellwalk' filesep 'frame1.tif']);
0338 nucimg = ml_readimage([framefolder filesep 'Nucwalk' filesep 'frame1.tif']);
0339 end
0340
0341 catch err
0342 getReport( err, 'extended')
0343 nucimg = [];
0344 cellimg = [];
0345 end
0346 end