function make_8_branch_func_files_for_dice

project_dir     = pwd;
max_branches    = 8;
%==========================================================================
% min(S)
%==========================================================================

min_pref        = [project_dir '/supplementary_material/dice_coeff_numk_does_not_matter/R_frontal_minS/ap_solution_40.func.gii'];
seed            = min_pref;
affinity_mat    = 'minS_data_hierarchy_F40_P15';

cd([project_dir '/supplementary_material/dice_coeff_numk_does_not_matter/dice_coefficient'])
hemi            = 'R';
load(affinity_mat)

for i_branches = 1:length(max_branches)
    
    hierarchy_data  = data_hierarchy.correlation;
    data_pdist      = pdist(hierarchy_data,'cityblock');
    linkage_ward    = linkage(data_pdist,'ward');
    hierarchy_clusters_idx = cluster(linkage_ward, 'maxclust', max_branches(i_branches));
    
    % Get ROI func
    mask = gifti(seed);
    blank = mask;
    blank.private.data{1}.data = zeros(size(blank.private.data{1}.data));
    % Find non-zero indices
    original_ind = find(mask.private.data{1}.data);
    % Replace indices with branch number
    for i_hierarchical = 1: length(hierarchy_clusters_idx)
        replace_these = find(mask.private.data{1}.data(original_ind)==i_hierarchical);
        blank.private.data{1}.data(original_ind(replace_these)) = hierarchy_clusters_idx(i_hierarchical);
        clear replace_these;
    end
    
    create_primate_func_gii(blank.cdata, [hemi '_frontal_' affinity_mat '_' num2str(max_branches(i_branches)) '_branches' ], hemi);
    
end

%==========================================================================
% min(S)
%==========================================================================
median_pref     = [project_dir '/supplementary_material/dice_coeff_numk_does_not_matter/R_frontal_medianS/ap_solution_67.func.gii'];
seed            = median_pref;
affinity_mat    = 'medianS_data_hierarchy_F67_P15';

cd([project_dir '/supplementary_material/dice_coeff_numk_does_not_matter/dice_coefficient'])
hemi            = 'R';
load(affinity_mat)

for i_branches = 1:length(max_branches)
    
    hierarchy_data  = data_hierarchy.correlation;
    data_pdist      = pdist(hierarchy_data,'cityblock');
    linkage_ward    = linkage(data_pdist,'ward');
    hierarchy_clusters_idx = cluster(linkage_ward, 'maxclust', max_branches(i_branches));
    
    % Get ROI func
    mask = gifti(seed);
    blank = mask;
    blank.private.data{1}.data = zeros(size(blank.private.data{1}.data));
    % Find non-zero indices
    original_ind = find(mask.private.data{1}.data);
    % Replace indices with branch number
    for i_hierarchical = 1: length(hierarchy_clusters_idx)
        replace_these = find(mask.private.data{1}.data(original_ind)==i_hierarchical);
        blank.private.data{1}.data(original_ind(replace_these)) = hierarchy_clusters_idx(i_hierarchical);
        clear replace_these;
    end
    
    create_primate_func_gii(blank.cdata, [hemi '_frontal_' affinity_mat '_' num2str(max_branches(i_branches)) '_branches' ], hemi);
    
end

%==========================================================================
% pref := min(S)-(max(S)-min(S))
%==========================================================================
pref            = [project_dir '/supplementary_material/dice_coeff_numk_does_not_matter/R_frontal_pref/ap_solution_26.func.gii'];
seed            = pref;
affinity_mat    = 'pref_data_hierarchy_F26_P15';

cd([project_dir '/supplementary_material/dice_coeff_numk_does_not_matter/dice_coefficient'])
hemi            = 'R';
load(affinity_mat)

for i_branches = 1:length(max_branches)
    
    hierarchy_data  = data_hierarchy.correlation;
    data_pdist      = pdist(hierarchy_data,'cityblock');
    linkage_ward    = linkage(data_pdist,'ward');
    hierarchy_clusters_idx = cluster(linkage_ward, 'maxclust', max_branches(i_branches));
    
    % Get ROI func
    mask = gifti(seed);
    blank = mask;
    blank.private.data{1}.data = zeros(size(blank.private.data{1}.data));
    % Find non-zero indices
    original_ind = find(mask.private.data{1}.data);
    % Replace indices with branch number
    for i_hierarchical = 1: length(hierarchy_clusters_idx)
        replace_these = find(mask.private.data{1}.data(original_ind)==i_hierarchical);
        blank.private.data{1}.data(original_ind(replace_these)) = hierarchy_clusters_idx(i_hierarchical);
        clear replace_these;
    end
    
    create_primate_func_gii(blank.cdata, [hemi '_frontal_' affinity_mat '_' num2str(max_branches(i_branches)) '_branches' ], hemi);
    
end

cd(project_dir)

end