function build_1d_model_2_bump
% (Unused)

%% SEEMS UNFINISHED... MAYBE JUST SUBROUTINE.. KATE SAYS IT MAY BE REDUNDANT
%% AND IS MOSTLY MADE OF CUT-AND-PASTE

% make images and points
n_images = 10;
n_iterations = 1;
n_sets = 10;
image_width = 100;
spline_type = 'multi_point';
placement_type = 'random';
n_points = 5;
do_plot = 0;

white_ctr = 0;
ref_shift = 0.2;
max_shift = 0.2;
step = 0.1;
los = zeros(n_images,floor((max_shift-ref_shift)/step));
his = zeros(n_images,floor((max_shift-ref_shift)/step));
factor_step = 0.1;
factor_min = 0;
factor_max = 0.4;
blurred = 0;
spec_iters = 25;
gen_iters = 25;
min_error = 0;
max_error = 1;
error_step = 0.1;
n_error_steps = (max_error-min_error)/error_step;

tic;
for this_set = 1:n_sets
  tic;
  ['calculating set ', num2str(this_set)]
  % for each set of n_sets images, build a model
  % calculate obj fn values from these models, plot the mean with error
  % bars given by 1 stddev away
  for white_width = ref_shift:step:max_shift
    white_ctr = white_ctr+1;

    [imagelist1 images1 points1 his1(:,white_ctr) los1(:,white_ctr)] = make_1d_images(n_images, image_width/2, white_width);
    [imagelist2 images2 points2 his2(:,white_ctr) los2(:,white_ctr)] = make_1d_images(n_images, image_width/2, white_width);
    images = vertcat(images1, images2);
    points = vertcat(points1, points2+image_width/2);
    his = vertcat(his1, his2);
    los = vertcat(los1, los2);
    % smooth images
    window = 9;
%    images = average_smooth(images, window); blurred = 1;
%    images = gaussian_smooth(images, window); blurred = 1;

    % normalise points from -1 to 1
    points = -1 + 2*(points-1)/(image_width-1);

    keep = 0.999999;
    ref_points_vec = points(:,1);
    ref_image_vec = images(:,1);
    ref_hi = his(1);
    ref_lo = los(1);
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Warping images, then modelling warped images
% attempt to register images by warping.
subplot_fig = figure;
figure(subplot_fig),for i=1:n_images,subplot(n_images,2,(2*i)-1),plot(images(:,i)),title('Unwarped images'); end;
warped_images = images;
warped_points = points;
% correctly_warped_points = points;
% correctly_warped_images = images;
% for i=1:n_images
%   % now bung in the right deformations to get them back
%   correctly_warped_points(:,i) = linear_warp(ref_points_vec, los(i,white_ctr), his(i,white_ctr), ref_lo, ref_hi);
%   correctly_warped_points_vec = correctly_warped_points(:,i);
%   image_vec = images(:,i);
%   % resample at the warped points - point on reg. grid has value of point at same index in warpy grid
%   % need to interpolate
%   correctly_warped_images(:,i) = interp1(ref_points_vec, image_vec, correctly_warped_points_vec);
%   correct_model = build_model(correctly_warped_images,correctly_warped_points,1,'','edge',0);
%   converging_score(i) = measure_model(correct_model.variances,50);
% end
% figure,plot(log(converging_score)),title('Log of score as model converges');
%     

  for n=1:n_iterations
  disp(['iter ',num2str(n)]);
    % correct registration
    % attempt to register
    for i=1:n_images
      disp(['Warping image ',num2str(i),' of ',num2str(n_images)]);
      image_vec = warped_images(:,i);
      points_vec = warped_points(:,i);
%      [param_list, warped_point, warped_image, score] = optimise_warps_model([warped_images(:,1:i-1),warped_images(:,i+1:n_images)], [warped_points(:,1:i-1),warped_points(:,i+1:n_images)], image_vec, points_vec,placement_type,spline_type,subplot_fig,i*2, n_points);
      [param_list, warped_point, warped_image, score] = optimise_all_warps(ref_image_vec, ref_points_vec, image_vec, points_vec, spline_type, placement_type,subplot_fig,i*2, n_points);
%      [param_list, warped_point, warped_image, score] = optimise_warps(ref_image_vec, ref_points_vec, image_vec, points_vec, spline_type, placement_type, n_points);

      warped_images(:,i) = warped_image;
      warped_points(:,i) = warped_point;

%      figure(subplot_fig),title(['Warped images, after image ',num2str(i),' iteration ',num2str(n)]);
      %      dummy = waitforbuttonpress;
      if(n==n_iterations)
        final_score(i, this_set) = score;
      end
    end
  end

    % build model from warped images
    w_c_model = build_model(warped_images, warped_points, keep,'Optimised warp', 'variance');
%    [w_c_model.mean_specificity, w_c_model.std_specificity] = find_specificity(w_c_model, images, spec_iters, ref_points_vec);
%    show_combined_model(w_c_model,ref_points_vec, 2, 0.2, 'Combined model built from optimised warps');

  fig_title = 'Automatically aligned: ';
%  show_shape_model(w_shape_model, ref_points_vec, ref_image_vec, 2, white_width)
%  show_intensity_model(w_intensity_model, 2, white_width, fig_title)
%  show_combined_model(w_c_model, ref_points_vec, 2, white_width, fig_title)

    w_intensity_total_vars(white_ctr) = w_c_model.intensity_model.total_var;
    w_shape_total_vars(white_ctr) = w_c_model.shape_model.total_var;

    warped_score(white_ctr, this_set) = measure_model(w_c_model.variances, 20);
    shape_modes(white_ctr, this_set) = w_c_model.n_shape_modes;
    intensity_modes(white_ctr, this_set) = size(w_c_model.intensity_model.pcs,2);
    shape_variance(white_ctr, this_set) = sum(w_c_model.shape_model.variances);
    intensity_variance(white_ctr, this_set) = sum(w_c_model.intensity_model.variances);
    white_ctr = 1;
  end % white width
  t = toc;
  disp(['Time for set ',num2str(this_set),': ',num2str(t)]);
end % set
disp('Mean match score:');
mean(final_score(:))
disp('Std match score:');
std(final_score(:))

disp('Mean model score:');
mean(warped_score(:))
disp('Std model score:');
std(warped_score(:))

disp('Mean shape modes:');
mean(shape_modes(:))
disp('Std shape modes:');
std(shape_modes(:))

disp('Mean intensity modes:');
mean(intensity_modes(:))
disp('Std intensity modes:');
std(intensity_modes(:))

disp('Mean shape variance:');
mean(shape_variance(:))
disp('Std shape variance:');
std(shape_variance(:))

disp('Mean intensity variance:');
mean(intensity_variance(:))
disp('Std intensity variance:');
std(intensity_variance(:))

t = toc;
disp(['Total time: ',num2str(t)]);
