ParametersRun500
dataset_positions = [{2,0}; {2,1}; {2,2}; {2,3}; {2,5}; {4,0}; {4,1}; {4,2}; {4,3}; {4,4}; {4,5}; {5,0}; {5,1}; {5,2}; {5,3}; {6,0}; {6,1}; {6,2}; {6,3}; {6,4}; {6,5}; {7,0}; {7,1}; {7,2}; {7,3}; {7,4}; {7,5}; {10,0}; {10,1}; {10,2}; {10,3}; {10,4}; {10,5}];

stretch_matrix = [1 0 0 0; 0 1 0 0; 0 0 3.077 0; 0 0 0 1];

target_dataset_directory = 'L:\seg2 5-31\2-4 (cp from E drive 5-30)\Position 4\resized images';
target_run_number = 500;
target_start_time = '15-Nov-2015 12.12.47';
target_position = 4;
target_dataset = 2;
first_target_stack = 20;
last_target_stack = 89;

moving_run_number = 500;


neur_seg_var = optreg.neurite_segmentation_variable;
cb_seg_var = optreg.cell_body_segmentation_variable;
nuclei_seg_var = optreg.nuclei_segmentation_variable;

new_num_slices = optreg.new_frame(3);
img_height = optreg.img_height;
img_width = optreg.img_width;
num_slices = optreg.num_slices;

% for use_offsets = 0:1

use_offsets =0;
    figure
    hold on;

SSE = 0;
SSE_normed = 0;
number_samples = 0;

total_number_samples =0;
total_time_offset= 0;
    
for do_moving = 0:1


    if do_moving
for i = 1:size(dataset_positions,1)

    i
    dataset = dataset_positions{i,1};
    position = dataset_positions{i,2};
    last_stack = calculated_stacks{dataset,position+1}(2);
    
    moving_start_time = moving_start_times{dataset,position+1};
    moving_moments_time = moments_times{dataset,position+1};
    
    time_offset = time_offsets{dataset,position+1};
    total_time_offset = total_time_offset + time_offset;
    
    if (dataset~= 2)&&(dataset~=4)
        moving_dataset_directory =  ['L:\seg2 5-31\Set ' num2str(dataset) '\Stacks\Position ' num2str(position) '\resized images'];
        ilastik_filename = ['Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(last_stack) '_Probabilities.h5'];
    else
        moving_dataset_directory =  ['K:\Laptop F drive copy\new reg 4-8 classifier 6-13-15\' num2str(dataset) '-' num2str(position)];
        ilastik_filename = ['Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(last_stack) '_Probabilities.h5'];
    end
    
    cd(moving_dataset_directory);
    
    number_cells_moving = NumCellsExtractor('',dataset, position,false);
        
    num_time_points = 20;

    number_cells_moving_cut = number_cells_moving(1:20);

    x = 1:num_time_points;
    x_prime = 1:.2:num_time_points;
    
    num_time_points_expanded = (num_time_points-1)*5 + 1;

    number_cells_moving_oversampled = interp1(x,number_cells_moving_cut,x_prime,'pchip');
    
    size(number_cells_moving_oversampled)
        
    if use_offsets
        if time_offset <0
        number_cells_moving_oversampled2 = number_cells_moving_oversampled(1-time_offset:end);
        new_SSE = norm(number_cells_moving_oversampled2 - number_cells_target_oversampled(1:end+time_offset))^2;
        SSE = SSE + new_SSE;
        number_samples = number_samples + length(number_cells_moving_oversampled2);
        SSE_normed = SSE_normed + new_SSE*(length(number_cells_target_oversampled)/length(number_cells_moving_oversampled2));
        ploth = plot(number_cells_moving_oversampled);
        else
        number_cells_moving_oversampled2 = number_cells_moving_oversampled(1:num_time_points_expanded - time_offset);
        ploth = plot(time_offset+1:num_time_points_expanded,number_cells_moving_oversampled(1:num_time_points_expanded - time_offset));
        new_SSE = norm(number_cells_moving_oversampled(1:num_time_points_expanded - time_offset) - number_cells_target_oversampled(1+time_offset:end))^2;
        SSE = SSE + new_SSE;
        SSE_normed = SSE_normed + new_SSE*(length(number_cells_target_oversampled)/length(number_cells_moving_oversampled2));
        number_samples = length(time_offset+1:num_time_points_expanded);
        
        end
    else
        ploth = plot(number_cells_moving_oversampled);
        new_SSE =  norm(number_cells_moving_oversampled - number_cells_target_oversampled)^2;
        SSE = SSE + new_SSE;
        number_samples = number_samples + length(number_cells_moving_oversampled);
    end
    
    size(number_cells_moving_oversampled)
    
    total_number_samples = total_number_samples + number_samples;
%     ploth.Color = [rand .25*rand rand];
end
    elseif ~do_moving
        cd(target_dataset_directory);
        dataset = 2;
        position = 4;
        number_cells_target = NumCellsExtractor('',dataset, position,false);
        
        num_time_points = 20;
        
        number_cells_target_cut = number_cells_target(1:20);
        
        x = 1:num_time_points;
        x_prime = 1:.2:num_time_points;
        
        num_time_points_expanded = (num_time_points-1)*5 + 1;
        
        number_cells_target_oversampled = interp1(x,number_cells_target_cut,x_prime,'pchip');
        plot(number_cells_target_oversampled);
        
        plott = plot(number_cells_target_oversampled);
        plott.Color = [0 .8 0]; plott.LineWidth = 3;
    end
    
end

if use_offsets
    these_axes_offset =gca;
    axis([0 100 0 600]);
    MakeFont;
    these_axes_offset.XTick = [0 20 40 60 80 100];
    these_axes_offset.XTickLabel = {[0 40 80 120 160 200]};
    these_axes_offset.YTick = [0 100 200 300 400 500 600];
    these_axes_offset.YTickLabel = {[0 100 200 300 400 500 600]};
else
    these_axes = gca;
    axis([0 100 0 600]);
    MakeFont;
    these_axes.XTick = [0 20 40 60 80 100];
    these_axes.XTickLabel = {[0 40 80 120 160 200]};
    these_axes.YTick = [0 100 200 300 400 500 600];
    these_axes.YTickLabel = {[0 100 200 300 400 500 600]};
end
