<commit_before>import numpy as np
import nibabel as nb
import os
import sys
import nighresjava
from ..io import load_volume, save_volume
from ..utils import _output_dir_4saving, _fname_4saving, \
                    _check_topology_lut_dir, _check_atlas_file, \
                    _check_available_memory


def recursive_ridge_diffusion(input_image, ridge_intensities, ridge_filter, 
                              surface_levelset=None, orientation='undefined', 
                              loc_prior=None, 
                              min_scale=0, max_scale=3, 
                              diffusion_factor=1.0, 
                              similarity_scale=0.1,
                              max_iter=100, max_diff=1e-3,
                              save_data=False, overwrite=False, output_dir=None,
                              file_name=None):
    
    """ Recursive Ridge Diffusion

    Extracts planar of tubular structures across multiple scales, with an optional directional bias.

    Parameters
    ----------
    input_image: niimg
        Input image
	ridge_intensities: {'bright','dark','both'}
	    Which intensities to consider for the filtering
	ridge_filter: {'2D','1D','0D'}
	    Whether to filter for 2D ridges, 1D vessels, or 0D holes
	surface_levelset: niimg, optional
	    Level set surface to restrict the orientation of the detected features
	orientation: {'undefined','parallel','orthogonal'}
	    The orientation of features to keep with regard to the surface or its normal
	loc_prior: niimg, optional
	    Location prior image to restrict the search for features
	min_scale: int
	    Minimum scale (in voxels) to look for features (default is 0)
    max_scale: int
	    Maximum scale (in voxels) to look for features (default is 3)
	diffusion_factor: float
	    Scaling factor for the diffusion weighting in [0,1] (default is 1.0)
	similarity_scale: float
	    Scaling of the similarity function as a factor of intensity range
	max_iter: int
	    Maximum number of diffusion iterations
	max_diff:
        Maximum difference to stop the diffusion
    save_data: bool
        Save output data to file (default is False)
    overwrite: bool
        Overwrite existing results (default is False)
    output_dir: str, optional
        Path to desired output directory, will be created if it doesn't exist
    file_name: str, optional
        Desired base name for output files with file extension
        (suffixes will be added)
        
    Returns
    ----------
   	dict
        Dictionary collecting outputs under the following keys
        (suffix of output files in brackets)

        * filter (niimg): raw filter response
        * propagation (niimg): propagated probabilistic response after diffusion
        * scale (niimg): scale of the detection filter 
        * ridge_dir (niimg): estimated local ridge direction
        * ridge_pv (niimg): ridge partial volume map (taking size into account}
        * ridge_size (niimg): estimated size of each detected component

    Notes
    ----------
    Original Java module by <NAME>. Extension of the recursive ridge
    filter in [1]_.

    References
    ----------
    .. [1] Bazin et al (2016), Vessel segmentation from quantitative 
           susceptibility maps for local oxygenation venography, Proc ISBI.

    """

    print('\n Recursive Ridge Diffusion')

    # check atlas_file and set default if not given
    #atlas_file = _check_atlas_file(atlas_file)

    # make sure that saving related parameters are correct
    if save_data:
        output_dir = _output_dir_4saving(output_dir, input_image)

        filter_file = _fname_4saving(file_name=file_name,
                                  rootfile=input_image,
                                  suffix='rrd-filter')

        propagation_file = _fname_4saving(file_name=file_name,
                                   rootfile=input_image,
                                   suffix='rrd-propag')

        scale_file = _fname_4saving(file_name=file_name,
                                   rootfile=input_image,
                                   suffix='rrd-scale')

        ridge_direction_file = _fname_4saving(file_name=file_name,
                                  rootfile=input_image,
                                  suffix='rrd-dir')

        ridge_pv_file = _fname_4saving(file_name=file_name,
                                  rootfile=input_image,
                                  suffix='rrd-pv')

        ridge_size_file = _fname_4saving(file_name=file_name,
                                  rootfile=input_image,
                                  suffix='rrd-size')

        if overwrite is False \
            and os.path.isfile(filter_file) \
            and os.path.isfile(propagation_file) \
            and os.path.isfile(scale_file) \
            and os.path.isfile(ridge_direction_file) \
            and os.path.isfile(ridge_pv_file) \
            and os.path.isfile(ridge_size_file) :
            
            print("skip computation (use existing results)")
            output = {'filter': load_volume(filter_file), 
                      'propagation': load_volume(propagation_file), 
                      'scale': load_volume(scale_file),
                      'ridge_dir': load_volume(ridge_direction_file), 
                      'ridge_pv': load_volume(ridge_pv_file), 
                      'ridge_size': load_volume(ridge_size_file)}
            return output

    # start virtual machine, if not already running
    try:
        mem = _check_available_memory()
        nighresjava.initVM(initialheap=mem['init'], maxheap=mem['max'])
    except ValueError:
        pass
    # create extraction instance
    rrd = nighresjava.FilterRecursiveRidgeDiffusion()

    # set parameters
    rrd.setRidgeIntensities(ridge_intensities)
    rrd.setRidgeFilter(ridge_filter)
    rrd.setOrientationToSurface(orientation)
    rrd.setMinimumScale(min_scale)
    rrd.setMaximumScale(max_scale)
    rrd.setDiffusionFactor(diffusion_factor)
    rrd.setSimilarityScale(similarity_scale)
    rrd.setPropagationModel("none")
    if max_iter>0: rrd.setPropagationModel("diffusion")
    rrd.setMaxIterations(max_iter)
    rrd.setMaxDifference(max_diff)
                     
                                                   
    # load input image and use it to set dimensions and resolution
    img = load_volume(input_image)
    data = img.get_data()
    affine = img.affine
    header = img.header
    resolution = [x.item() for x in header.get_zooms()]
    dimensions = data.shape

    rrd.setDimensions(dimensions[0], dimensions[1], dimensions[2])
    rrd.setResolutions(resolution[0], resolution[1], resolution[2])

    # input input_image
    rrd.setInputImage(nighresjava.JArray('float')((data.flatten('F')).astype(float)))

    # input surface_levelset : dirty fix for the case where surface image not input
    try:
        data = load_volume(surface_levelset).get_data()
        rrd.setSurfaceLevelSet(nighresjava.JArray('float')((data.flatten('F')).astype(float)))
    except:
        print("no surface image")
    
    # input location prior image : loc_prior is optional
    try:
        data = load_volume(loc_prior).get_data()
        rrd.setLocationPrior(nighresjava.JArray('float')((data.flatten('F')).astype(float)))
    except:
        print("no location prior image")
    
    
    # execute Extraction<commit_msg>adding a 2d option to the ridge filter<commit_after>import numpy as np
import nibabel as nb
import os
import sys
import nighresjava
from ..io import load_volume, save_volume
from ..utils import _output_dir_4saving, _fname_4saving, \
                    _check_topology_lut_dir, _check_atlas_file, \
                    _check_available_memory


def recursive_ridge_diffusion(input_image, ridge_intensities, ridge_filter, 
                              surface_levelset=None, orientation='undefined', 
                              loc_prior=None, 
                              min_scale=0, max_scale=3, 
                              diffusion_factor=1.0, 
                              similarity_scale=0.1,
                              max_iter=100, max_diff=1e-3,
                              save_data=False, overwrite=False, output_dir=None,
                              file_name=None):
    
    """ Recursive Ridge Diffusion

    Extracts planar of tubular structures across multiple scales, with an optional directional bias.

    Parameters
    ----------
    input_image: niimg
        Input image
	ridge_intensities: {'bright','dark','both'}
	    Which intensities to consider for the filtering
	ridge_filter: {'2D','1D','0D'}
	    Whether to filter for 2D ridges, 1D vessels, or 0D holes
	surface_levelset: niimg, optional
	    Level set surface to restrict the orientation of the detected features
	orientation: {'undefined','parallel','orthogonal'}
	    The orientation of features to keep with regard to the surface or its normal
	loc_prior: niimg, optional
	    Location prior image to restrict the search for features
	min_scale: int
	    Minimum scale (in voxels) to look for features (default is 0)
    max_scale: int
	    Maximum scale (in voxels) to look for features (default is 3)
	diffusion_factor: float
	    Scaling factor for the diffusion weighting in [0,1] (default is 1.0)
	similarity_scale: float
	    Scaling of the similarity function as a factor of intensity range
	max_iter: int
	    Maximum number of diffusion iterations
	max_diff:
        Maximum difference to stop the diffusion
    save_data: bool
        Save output data to file (default is False)
    overwrite: bool
        Overwrite existing results (default is False)
    output_dir: str, optional
        Path to desired output directory, will be created if it doesn't exist
    file_name: str, optional
        Desired base name for output files with file extension
        (suffixes will be added)
        
    Returns
    ----------
   	dict
        Dictionary collecting outputs under the following keys
        (suffix of output files in brackets)

        * filter (niimg): raw filter response
        * propagation (niimg): propagated probabilistic response after diffusion
        * scale (niimg): scale of the detection filter 
        * ridge_dir (niimg): estimated local ridge direction
        * ridge_pv (niimg): ridge partial volume map (taking size into account}
        * ridge_size (niimg): estimated size of each detected component

    Notes
    ----------
    Original Java module by <NAME>. Extension of the recursive ridge
    filter in [1]_.

    References
    ----------
    .. [1] Bazin et al (2016), Vessel segmentation from quantitative 
           susceptibility maps for local oxygenation venography, Proc ISBI.

    """

    print('\n Recursive Ridge Diffusion')

    # check atlas_file and set default if not given
    #atlas_file = _check_atlas_file(atlas_file)

    # make sure that saving related parameters are correct
    if save_data:
        output_dir = _output_dir_4saving(output_dir, input_image)

        filter_file = _fname_4saving(file_name=file_name,
                                  rootfile=input_image,
                                  suffix='rrd-filter')

        propagation_file = _fname_4saving(file_name=file_name,
                                   rootfile=input_image,
                                   suffix='rrd-propag')

        scale_file = _fname_4saving(file_name=file_name,
                                   rootfile=input_image,
                                   suffix='rrd-scale')

        ridge_direction_file = _fname_4saving(file_name=file_name,
                                  rootfile=input_image,
                                  suffix='rrd-dir')

        ridge_pv_file = _fname_4saving(file_name=file_name,
                                  rootfile=input_image,
                                  suffix='rrd-pv')

        ridge_size_file = _fname_4saving(file_name=file_name,
                                  rootfile=input_image,
                                  suffix='rrd-size')

        if overwrite is False \
            and os.path.isfile(filter_file) \
            and os.path.isfile(propagation_file) \
            and os.path.isfile(scale_file) \
            and os.path.isfile(ridge_direction_file) \
            and os.path.isfile(ridge_pv_file) \
            and os.path.isfile(ridge_size_file) :
            
            print("skip computation (use existing results)")
            output = {'filter': load_volume(filter_file), 
                      'propagation': load_volume(propagation_file), 
                      'scale': load_volume(scale_file),
                      'ridge_dir': load_volume(ridge_direction_file), 
                      'ridge_pv': load_volume(ridge_pv_file), 
                      'ridge_size': load_volume(ridge_size_file)}
            return output

                                                   
    # load input image and use it to set dimensions and resolution
    img = load_volume(input_image)
    data = img.get_data()
    affine = img.affine
    header = img.header
    resolution = [x.item() for x in header.get_zooms()]
    dimensions = data.shape
    if (len(dimensions)<3) dimensions = [dimensions[0], dimensions[1], 1]
    if (len(resolution)<3) resolution = [resolution[0], resolution[1], 1.0]
    
    # start virtual machine, if not already running
    try:
        mem = _check_available_memory()
        nighresjava.initVM(initialheap=mem['init'], maxheap=mem['max'])
    except ValueError:
        pass
    # create extraction instance
    if dimensions[2] is 1 rrd = nighresjava.FilterRecursiveRidgeDiffusion2D()
    else rrd = nighresjava.FilterRecursiveRidgeDiffusion()

    # set parameters
    rrd.setRidgeIntensities(ridge_intensities)
    rrd.setRidgeFilter(ridge_filter)
    rrd.setOrientationToSurface(orientation)
    rrd.setMinimumScale(min_scale)
    rrd.setMaximumScale(max_scale)
    rrd.setDiffusionFactor(diffusion_factor)
    rrd.setSimilarityScale(similarity_scale)
    rrd.setPropagationModel("none")
    if max_iter>0: rrd.setPropagationModel("diffusion")
    rrd.setMaxIterations(max_iter)
    rrd.setMaxDifference(max_diff)
                     
    rrd.setDimensions(dimensions[0], dimensions[1], dimensions[2])
    rrd.setResolutions(resolution[0], resolution[1], resolution[2])

    # input input_image
    rrd.setInputImage(nighresjava.JArray('float')((data.flatten('F')).astype(float)))

    # input surface_levelset : dirty fix for the case where surface image not input
    try:
        data = load_volume(surface_levelset).get_data()
        rrd.setSurfaceLevelSet(nighresjava.JArray('float')((data.flatten('F')).astype(float)))
    except:
        print("no surface image")
    
    # input location prior image : loc_prior is optional
    try:
        data = load_volume(loc_prior).get_data()
        rrd.setLocationPrior(nighresjava.JArray('float')((data.flatten('F')).astype(float)))
    except:
        print("no location prior image")
    
    
    # execute Extraction
{'fraction_non_alphanumeric': 0.07838842426656047, 'fraction_numerical': 0.0068365856896322845, 'mean_word_length': 2.201657458563536, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1781178', 'n_tokens_mistral': 4310, 'n_tokens_neox': 4029, 'n_words': 1085}