<commit_before>
'''
General skeleton analysis
'''

import numpy as np

from fil_finder.pixel_ident import pix_identify, \
    make_final_skeletons, recombine_skeletons, isolateregions
from fil_finder.length import init_lengths, pre_graph, \
    longest_path, prune_graph, main_length


def analyze_skeletons(skeleton, relintens_thresh=0.2, imgscale=1.,
                      skel_thresh=None, branch_thresh=None,
                      verbose=False, save_png=False, save_name=None):
    '''

    This function wraps most of the skeleton analysis. Several steps are
    completed here:
    *   isolatefilaments is run to separate each skeleton into its own
        array. If the skeletons are under the threshold set by
        size_thresh, the region is removed. An updated mask is
        also returned.
    *   pix_identify classifies each of the pixels in a skeleton as a
        body, end, or intersection point. See the documentation on find_filpix
        for a complete explanation. The function labels the branches and
        intersections of each skeletons.
    *   init_lengths finds the length of each branch in each skeleton and
        also returns the coordinates of each of these branches for use in
        the graph representation.
    *   pre_graph turns the skeleton structures into a graphing format
        compatible with networkx. Hubs in the graph are the intersections
        and end points, labeled as letters and numbers respectively. Edges
        define the connectivity of the hubs and they are weighted by their
        length.
    *   longest_path utilizes networkx.shortest_path_length to find the
        overall length of each of the filaments. The returned path is the
        longest path through the skeleton. If loops exist in the skeleton,
        the longest path is chosen (this shortest path algorithm fails when
        used on loops).
    *   extremum_pts returns the locations of the longest path's extent
        so its performance can be evaluated.
    *   final_lengths takes the path returned from longest_path and
        calculates the overall length of the filament. This step also acts
        as to prune the skeletons.
    *   final_analysis combines the outputs and returns the results for
        further analysis.

    Parameters
    ----------
    verbose : bool, optional
        Enables plotting.
    relintens_thresh : float, optional
        Relative intensity threshold for pruning. Sets the importance
        a branch must have in intensity relative to all other branches
        in the skeleton. Must be between (0.0, 1.0].
    skel_thresh : float, optional
        Manually set the minimum skeleton threshold. Overrides all
        previous settings.
    branch_thresh : float, optional
        Manually set the minimum branch length threshold. Overrides all
        previous settings.
    save_png : bool, optional
        Saves the plot made in verbose mode. Disabled by default.

    Returns
    -------
    lengths : np.ndarray
        Longest path lengths through the skeletons.
    skeleton : np.ndarray
        Pruned skeletons
    skeleton_longpath : np.ndarray
        Longest path skeletons

    '''

    if relintens_thresh > 1.0 or relintens_thresh <= 0.0:
        raise ValueError(
            "relintens_thresh must be set between (0.0, 1.0].")

    # Minimum skeleton length
    if skel_thresh is None:
        skel_thresh = 0

    # Minimum branch length
    if branch_thresh is None:
        branch_thresh = 0

    isolated_filaments, num, offsets = \
        isolateregions(skeleton, size_threshold=skel_thresh,
                       pad_size=2, fill_hole=False)

    # Check to make sure there is at least one valid skeleton to work with
    if not bool(isolateregions):
        raise IndexError("Could not identify any valid skeletons based on"
                         " the given skel_thresh.")

    interpts, hubs, ends, filbranches, labeled_fil_arrays =  \
        pix_identify(isolated_filaments, num)<commit_msg>Fix the empty check<commit_after>
'''
General skeleton analysis
'''

import numpy as np

from fil_finder.pixel_ident import pix_identify, \
    make_final_skeletons, recombine_skeletons, isolateregions
from fil_finder.length import init_lengths, pre_graph, \
    longest_path, prune_graph, main_length


def analyze_skeletons(skeleton, relintens_thresh=0.2, imgscale=1.,
                      skel_thresh=None, branch_thresh=None,
                      verbose=False, save_png=False, save_name=None):
    '''

    This function wraps most of the skeleton analysis. Several steps are
    completed here:
    *   isolatefilaments is run to separate each skeleton into its own
        array. If the skeletons are under the threshold set by
        size_thresh, the region is removed. An updated mask is
        also returned.
    *   pix_identify classifies each of the pixels in a skeleton as a
        body, end, or intersection point. See the documentation on find_filpix
        for a complete explanation. The function labels the branches and
        intersections of each skeletons.
    *   init_lengths finds the length of each branch in each skeleton and
        also returns the coordinates of each of these branches for use in
        the graph representation.
    *   pre_graph turns the skeleton structures into a graphing format
        compatible with networkx. Hubs in the graph are the intersections
        and end points, labeled as letters and numbers respectively. Edges
        define the connectivity of the hubs and they are weighted by their
        length.
    *   longest_path utilizes networkx.shortest_path_length to find the
        overall length of each of the filaments. The returned path is the
        longest path through the skeleton. If loops exist in the skeleton,
        the longest path is chosen (this shortest path algorithm fails when
        used on loops).
    *   extremum_pts returns the locations of the longest path's extent
        so its performance can be evaluated.
    *   final_lengths takes the path returned from longest_path and
        calculates the overall length of the filament. This step also acts
        as to prune the skeletons.
    *   final_analysis combines the outputs and returns the results for
        further analysis.

    Parameters
    ----------
    verbose : bool, optional
        Enables plotting.
    relintens_thresh : float, optional
        Relative intensity threshold for pruning. Sets the importance
        a branch must have in intensity relative to all other branches
        in the skeleton. Must be between (0.0, 1.0].
    skel_thresh : float, optional
        Manually set the minimum skeleton threshold. Overrides all
        previous settings.
    branch_thresh : float, optional
        Manually set the minimum branch length threshold. Overrides all
        previous settings.
    save_png : bool, optional
        Saves the plot made in verbose mode. Disabled by default.

    Returns
    -------
    lengths : np.ndarray
        Longest path lengths through the skeletons.
    skeleton : np.ndarray
        Pruned skeletons
    skeleton_longpath : np.ndarray
        Longest path skeletons

    '''

    if relintens_thresh > 1.0 or relintens_thresh <= 0.0:
        raise ValueError(
            "relintens_thresh must be set between (0.0, 1.0].")

    # Minimum skeleton length
    if skel_thresh is None:
        skel_thresh = 0

    # Minimum branch length
    if branch_thresh is None:
        branch_thresh = 0

    isolated_filaments, num, offsets = \
        isolateregions(skeleton, size_threshold=skel_thresh,
                       pad_size=2, fill_hole=False)

    # Check to make sure there is at least one valid skeleton to work with
    if not isolated_filaments:
        raise IndexError("Could not identify any valid skeletons based on"
                         " the given skel_thresh.")

    interpts, hubs, ends, filbranches, labeled_fil_arrays =  \
        pix_identify(isolated_filaments, num)
{'fraction_non_alphanumeric': 0.04561316051844467, 'fraction_numerical': 0.00448654037886341, 'mean_word_length': 2.5698398576512456, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3266888', 'n_tokens_mistral': 2151, 'n_tokens_neox': 2027, 'n_words': 886}