function [norm_ratemap, firingrate, firing_position, occupancy_per_cell, stability, spatial_info, best_direction] = ca_imag_to_ratemap_v5_halves(filetoprocess)

mousedata = table2array(readtable(filetoprocess));
mouse_posn = mousedata(:,4);
num_neurons = size(mousedata,2) - 5;
total_frames = size(mousedata,1);

% General Firing Rate (Hz)
firingrate = (sum(mousedata(:,6:end),1) / (total_frames/20))';

% --- Directionality ---
sm_mouse_posn = smooth(mouse_posn, 15);
direction = ones(total_frames, 1);
for nm = 2:total_frames
    if sm_mouse_posn(nm) > sm_mouse_posn(nm-1),     direction(nm) = 1;
    elseif sm_mouse_posn(nm) < sm_mouse_posn(nm-1), direction(nm) = 2;
    else,                                           direction(nm) = 0;
    end
end
for ia = 2:numel(direction)
    if direction(ia) == 0, direction(ia) = direction(ia-1); end
end

forward_idx  = find(direction == 1);
backward_idx = find(direction == 2);

% --- CHRONOLOGICAL HALF SPLIT ---
midpoint = floor(total_frames / 2);
first_half_idx  = 1:midpoint;
second_half_idx = (midpoint + 1):total_frames;

% Intersect directions with session halves
fwd_h1 = intersect(forward_idx, first_half_idx);
fwd_h2 = intersect(forward_idx, second_half_idx);
bwd_h1 = intersect(backward_idx, first_half_idx);
bwd_h2 = intersect(backward_idx, second_half_idx);

% --- OCCUPANCY ---
[occ_f,~]  = histcounts(mouse_posn(forward_idx),  1:73);
[occ_b,~]  = histcounts(mouse_posn(backward_idx), 1:73);
[occ_fh1] = histcounts(mouse_posn(fwd_h1), 1:73); [occ_fh2] = histcounts(mouse_posn(fwd_h2), 1:73);
[occ_bh1] = histcounts(mouse_posn(bwd_h1), 1:73); [occ_bh2] = histcounts(mouse_posn(bwd_h2), 1:73);

% Preallocate
norm_ratemap = zeros(72, num_neurons);
stability = zeros(num_neurons, 1);
spatial_info = zeros(num_neurons, 1);
firing_position = zeros(num_neurons, 1);
best_direction = zeros(num_neurons, 1);
occupancy_per_cell = zeros(72, num_neurons);

for nm = 1:num_neurons
    neuron_data = mousedata(:, nm+5);
    
    % --- FORWARD METRICS ---
    f_rm_h1 = histcounts(mouse_posn(fwd_h1(neuron_data(fwd_h1)>0)), 1:73) ./ (occ_fh1 + 1e-10);
    f_rm_h2 = histcounts(mouse_posn(fwd_h2(neuron_data(fwd_h2)>0)), 1:73) ./ (occ_fh2 + 1e-10);
    f_full  = histcounts(mouse_posn(forward_idx(neuron_data(forward_idx)>0)), 1:73) ./ (occ_f + 1e-10);
    
    if std(f_rm_h1) > 0 && std(f_rm_h2) > 0
        f_stab = corr(f_rm_h1', f_rm_h2', 'Rows', 'complete');
    else
        f_stab = 0;
    end
    
    % --- BACKWARD METRICS ---
    b_rm_h1 = histcounts(mouse_posn(bwd_h1(neuron_data(bwd_h1)>0)), 1:73) ./ (occ_bh1 + 1e-10);
    b_rm_h2 = histcounts(mouse_posn(bwd_h2(neuron_data(bwd_h2)>0)), 1:73) ./ (occ_bh2 + 1e-10);
    b_full  = histcounts(mouse_posn(backward_idx(neuron_data(backward_idx)>0)), 1:73) ./ (occ_b + 1e-10);
    
    if std(b_rm_h1) > 0 && std(b_rm_h2) > 0
        b_stab = corr(b_rm_h1', b_rm_h2', 'Rows', 'complete');
    else
        b_stab = 0;
    end

    % --- SELECTION LOGIC ---
    if max(f_full) > max(b_full) * 2 && f_stab > 0.1
        chosen = 1;
    elseif max(b_full) > max(f_full) * 2 && b_stab > 0.1
        chosen = 2;
    else
        [~, chosen] = max([f_stab, b_stab]);
    end
    
    % Final Storage
    best_direction(nm) = chosen;
    if chosen == 1
        norm_ratemap(:,nm) = f_full;
        stability(nm) = f_stab;
        occupancy_per_cell(:,nm) = occ_f;
    else
        norm_ratemap(:,nm) = b_full;
        stability(nm) = b_stab;
        occupancy_per_cell(:,nm) = occ_b;
    end
    
    spatial_info(nm) = calculate_si(norm_ratemap(:,nm), occupancy_per_cell(:,nm));
    [~, firing_position(nm)] = max(norm_ratemap(:,nm));
end
end

function si = calculate_si(ratemap, occupancy)
    P_i = occupancy / sum(occupancy);
    mean_rate = sum(ratemap .* P_i);
    if mean_rate <= 0, si = 0; return; end
    term = (ratemap ./ mean_rate) .* log2((ratemap ./ mean_rate) + eps);
    si = sum(P_i .* term);
end