<commit_before># depth_statistics.py: compute depth statistics
# Requirements: Spatial Analyst
# Author: <NAME>
# Date: 9/1/2012

# Import system modules
from __future__ import absolute_import
import os
import sys
import numpy as np
try:
    import scipy.stats
except ImportError:
    pass    # error generated inline
import arcpy
from arcpy.sa import FocalStatistics, Power

# local imports
from . import utils
from . import config

# Check out any necessary licenses
arcpy.CheckOutExtension("Spatial")


def iqr(in_array, overlap):
    s = in_array.shape
    nbh_list = []
    for r in range(0, (overlap * 2) + 1):
        for c in range(0, (overlap * 2) + 1):
            nbh_list.append(in_array[r:(s[0] - (overlap * 2) + r),
                                     c:(s[1] - (overlap * 2) + c)])
    iqr_array = np.array(nbh_list)
    iqr_array = (np.percentile(iqr_array, 75, axis=0) -
                 np.percentile(iqr_array, 25, axis=0))
    return iqr_array


def kurtosis(in_array, overlap):
    s = in_array.shape
    nbh_list = []
    for r in range(0, (overlap * 2) + 1):
        for c in range(0, (overlap * 2) + 1):
            nbh_list.append(in_array[r:(s[0] - (overlap * 2) + r),
                                     c:(s[1] - (overlap * 2) + c)])
    kurt_array = np.array(nbh_list)
    kurt_array = scipy.stats.kurtosis(kurt_array, axis=0)
    return kurt_array


def output_parts(in_raster, out_workspace, n_size):
    """return a prefix and suffix for naming our outputs"""
    in_base = os.path.splitext(os.path.basename(in_raster))[0]
    prefix = os.path.join(out_workspace, in_base)

    ws = arcpy.Describe(out_workspace)
    ws_type = set([ws.workspaceType])
    db_types = ['LocalDatabase', 'RemoteDatabase']
    if ws_type.intersection(db_types):
        ext = ""
    else:
        ext = ".tif"

    n_label = "{:03d}".format(n_size)
    suffix = "{}{}".format(n_label, ext)

    return (prefix, suffix)


def output_name(parts, label):
    """Generate a final output name, assumes prefix contains pathing."""
    (prefix, suffix) = parts
    return "_".join((prefix, label, suffix))


def main(in_raster=None, neighborhood_size=None, out_workspace=None,
         out_stats_raw=None, verbose=True, window_type='Rectangle'):
    """
    Compute depth statisitcs, averaging values over a defined neighborhood
    of cells. Can compute mean, standard deviation, and variance.
    """
    out_stats = out_stats_raw.replace("'", '').split(";")
    out_stats = list(set(out_stats) - set(['Terrain Ruggedness (VRM)']))
    arcpy.env.rasterStatistics = "STATISTICS"
    arcpy.env.compression = 'LZW'  # compress output rasters

    # neighborhood is integer
    n_size = int(neighborhood_size)

    # convert our data to sets for easy comparison
    mean_set = set(['Mean Depth', 'Difference to Mean'])
    std_dev_set = set(['Standard Deviation', 'Variance'])
    iqr_set = set(['Interquartile Range'])
    kurt_set = set(['Kurtosis'])

    # list stats to be computed
    if verbose:
        utils.msg("The following stats will be computed: " +
                  "{}".format(";".join(out_stats)))

    # these two tools both use block processing which requires NetCDF4
    if 'Interquartile Range' in out_stats or 'Kurtosis' in out_stats:
        if not utils.NETCDF4_EXISTS:
            utils.msg("The interquartile range and kurtosis tools require "
                      "the NetCDF4 Python library is installed. NetCDF4 "
                      "is included in ArcGIS 10.3 and later.", "error")
            return

        if 'Kurtosis' in out_stats and not utils.SCIPY_EXISTS:
            utils.msg("The kurtosis calculation requires the SciPy library "
                      "is installed. SciPy is included in ArcGIS 10.4 and "
                      "later versions.", "error")
            return

    # get output name prefix and suffix
    parts = output_parts(in_raster, out_workspace, n_size)

    utils.workspace_exists(out_workspace)
    # set geoprocessing environments
    arcpy.env.scratchWorkspace = out_workspace
    arcpy.env.workspace = out_workspace

    # validate nbr type
    if window_type not in ('Rectangle', 'Cirle'):
        utils.msg("Unknown window type.", "error")

    try:
        # initialize our neighborhood
        if verbose:
            utils.msg("Calculating neighborhood...")<commit_msg>include window type in error<commit_after># depth_statistics.py: compute depth statistics
# Requirements: Spatial Analyst
# Author: <NAME>
# Date: 9/1/2012

# Import system modules
from __future__ import absolute_import
import os
import sys
import numpy as np
try:
    import scipy.stats
except ImportError:
    pass    # error generated inline
import arcpy
from arcpy.sa import FocalStatistics, Power

# local imports
from . import utils
from . import config

# Check out any necessary licenses
arcpy.CheckOutExtension("Spatial")


def iqr(in_array, overlap):
    s = in_array.shape
    nbh_list = []
    for r in range(0, (overlap * 2) + 1):
        for c in range(0, (overlap * 2) + 1):
            nbh_list.append(in_array[r:(s[0] - (overlap * 2) + r),
                                     c:(s[1] - (overlap * 2) + c)])
    iqr_array = np.array(nbh_list)
    iqr_array = (np.percentile(iqr_array, 75, axis=0) -
                 np.percentile(iqr_array, 25, axis=0))
    return iqr_array


def kurtosis(in_array, overlap):
    s = in_array.shape
    nbh_list = []
    for r in range(0, (overlap * 2) + 1):
        for c in range(0, (overlap * 2) + 1):
            nbh_list.append(in_array[r:(s[0] - (overlap * 2) + r),
                                     c:(s[1] - (overlap * 2) + c)])
    kurt_array = np.array(nbh_list)
    kurt_array = scipy.stats.kurtosis(kurt_array, axis=0)
    return kurt_array


def output_parts(in_raster, out_workspace, n_size):
    """return a prefix and suffix for naming our outputs"""
    in_base = os.path.splitext(os.path.basename(in_raster))[0]
    prefix = os.path.join(out_workspace, in_base)

    ws = arcpy.Describe(out_workspace)
    ws_type = set([ws.workspaceType])
    db_types = ['LocalDatabase', 'RemoteDatabase']
    if ws_type.intersection(db_types):
        ext = ""
    else:
        ext = ".tif"

    n_label = "{:03d}".format(n_size)
    suffix = "{}{}".format(n_label, ext)

    return (prefix, suffix)


def output_name(parts, label):
    """Generate a final output name, assumes prefix contains pathing."""
    (prefix, suffix) = parts
    return "_".join((prefix, label, suffix))


def main(in_raster=None, neighborhood_size=None, out_workspace=None,
         out_stats_raw=None, verbose=True, window_type='Rectangle'):
    """
    Compute depth statisitcs, averaging values over a defined neighborhood
    of cells. Can compute mean, standard deviation, and variance.
    """
    out_stats = out_stats_raw.replace("'", '').split(";")
    out_stats = list(set(out_stats) - set(['Terrain Ruggedness (VRM)']))
    arcpy.env.rasterStatistics = "STATISTICS"
    arcpy.env.compression = 'LZW'  # compress output rasters

    # neighborhood is integer
    n_size = int(neighborhood_size)

    # convert our data to sets for easy comparison
    mean_set = set(['Mean Depth', 'Difference to Mean'])
    std_dev_set = set(['Standard Deviation', 'Variance'])
    iqr_set = set(['Interquartile Range'])
    kurt_set = set(['Kurtosis'])

    # list stats to be computed
    if verbose:
        utils.msg("The following stats will be computed: " +
                  "{}".format(";".join(out_stats)))

    # these two tools both use block processing which requires NetCDF4
    if 'Interquartile Range' in out_stats or 'Kurtosis' in out_stats:
        if not utils.NETCDF4_EXISTS:
            utils.msg("The interquartile range and kurtosis tools require "
                      "the NetCDF4 Python library is installed. NetCDF4 "
                      "is included in ArcGIS 10.3 and later.", "error")
            return

        if 'Kurtosis' in out_stats and not utils.SCIPY_EXISTS:
            utils.msg("The kurtosis calculation requires the SciPy library "
                      "is installed. SciPy is included in ArcGIS 10.4 and "
                      "later versions.", "error")
            return

    # get output name prefix and suffix
    parts = output_parts(in_raster, out_workspace, n_size)

    utils.workspace_exists(out_workspace)
    # set geoprocessing environments
    arcpy.env.scratchWorkspace = out_workspace
    arcpy.env.workspace = out_workspace

    # validate nbr type
    if window_type not in ('Rectangle', 'Circle'):
        utils.msg("Unknown window type `{}`".format(window_type), "error")

    try:
        # initialize our neighborhood
        if verbose:
            utils.msg("Calculating neighborhood...")
{'fraction_non_alphanumeric': 0.11212814645308924, 'fraction_numerical': 0.010526315789473684, 'mean_word_length': 2.774179620034542, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3006303', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2742, 'n_words': 768}