<commit_before>__author__ = "<NAME> <<EMAIL>>"
__date__ = "$Aug 01, 2014 14:55:57 EDT$"


import itertools

import numpy

import nanshe.expanded_numpy


def generate_hypersphere_masks(space, centers, radii, include_boundary = False):
    """
        Generate a stack of masks (first index indicates which mask); where, each contains a hypersphere constructed
        using a center and radius provided.

        Args:
            space(tuple of ints):                The size of the mask.

            centers(list of tuples of numbers):  List of centers with one per hypersphere.

            radii(list of numbers):              List of radii with one per hypersphere.

            include_boundary(bool):              Whether the mask should contain the boundary of the hypersphere or not.

        Returns:
            numpy.ndarray:                       A stack of masks (first index indicates which mask) with a filled
                                                 hypersphere using a center and radius for each mask.

        Examples:
            >>> generate_hypersphere_masks((3, 3), (1, 1), 1.25)
            array([[[False,  True, False],
                    [ True,  True,  True],
                    [False,  True, False]]], dtype=bool)

            >>> generate_hypersphere_masks((9, 9), (4, 4), 5)
            array([[[False, False,  True,  True,  True,  True,  True, False, False],
                    [False,  True,  True,  True,  True,  True,  True,  True, False],
                    [ True,  True,  True,  True,  True,  True,  True,  True,  True],
                    [ True,  True,  True,  True,  True,  True,  True,  True,  True],
                    [ True,  True,  True,  True,  True,  True,  True,  True,  True],
                    [ True,  True,  True,  True,  True,  True,  True,  True,  True],
                    [ True,  True,  True,  True,  True,  True,  True,  True,  True],
                    [False,  True,  True,  True,  True,  True,  True,  True, False],
                    [False, False,  True,  True,  True,  True,  True, False, False]]], dtype=bool)
    """

    # Convert to arrays
    space = numpy.array(space)
    centers = numpy.array(centers)
    radii = numpy.array(radii)

    # Add a singleton dimension if there is only one of each.
    if centers.ndim == 1:
        centers = centers[None]

    if radii.ndim == 0:
        radii = radii[None]

    # Validate the dimensions
    assert(space.ndim == 1)
    assert(centers.ndim == 2)
    assert(radii.ndim == 1)

    # Validate the shapes
    assert(space.shape == centers.shape[1:])
    assert(radii.shape == centers.shape[:1])

    # Create a hypersphere mask using a center and a radius.
    hypersphere_mask = numpy.zeros(radii.shape + tuple(space.tolist()), dtype = bool)
    for i, (each_center, each_radius) in enumerate(itertools.izip(centers, radii)):
        space_index = numpy.indices(space)

        each_point_offset = (space_index - nanshe.expanded_numpy.expand_view(each_center, tuple(space.tolist())))

        each_point_offset_sqd_sum = (each_point_offset**2).sum(axis = 0)

        each_point_offset_dist = each_point_offset_sqd_sum.astype(float)**.5

        if include_boundary:
            hypersphere_mask[i] = (each_point_offset_dist <= each_radius)
        else:
            hypersphere_mask[i] = (each_point_offset_dist < each_radius)

    return(hypersphere_mask)
<commit_msg>synthetic_data: Added the function generate_gaussian_images, which generates a stack of gaussian images.<commit_after>__author__ = "<NAME> <<EMAIL>>"
__date__ = "$Aug 01, 2014 14:55:57 EDT$"


import itertools

import numpy

import scipy
import scipy.ndimage
import scipy.ndimage.filters

import nanshe.expanded_numpy


def generate_hypersphere_masks(space, centers, radii, include_boundary = False):
    """
        Generate a stack of masks (first index indicates which mask); where, each contains a hypersphere constructed
        using a center and radius provided.

        Args:
            space(tuple of ints):                The size of the mask.

            centers(list of tuples of numbers):  List of centers with one per hypersphere.

            radii(list of numbers):              List of radii with one per hypersphere.

            include_boundary(bool):              Whether the mask should contain the boundary of the hypersphere or not.

        Returns:
            numpy.ndarray:                       A stack of masks (first index indicates which mask) with a filled
                                                 hypersphere using a center and radius for each mask.

        Examples:
            >>> generate_hypersphere_masks((3, 3), (1, 1), 1.25)
            array([[[False,  True, False],
                    [ True,  True,  True],
                    [False,  True, False]]], dtype=bool)

            >>> generate_hypersphere_masks((9, 9), (4, 4), 5)
            array([[[False, False,  True,  True,  True,  True,  True, False, False],
                    [False,  True,  True,  True,  True,  True,  True,  True, False],
                    [ True,  True,  True,  True,  True,  True,  True,  True,  True],
                    [ True,  True,  True,  True,  True,  True,  True,  True,  True],
                    [ True,  True,  True,  True,  True,  True,  True,  True,  True],
                    [ True,  True,  True,  True,  True,  True,  True,  True,  True],
                    [ True,  True,  True,  True,  True,  True,  True,  True,  True],
                    [False,  True,  True,  True,  True,  True,  True,  True, False],
                    [False, False,  True,  True,  True,  True,  True, False, False]]], dtype=bool)
    """

    # Convert to arrays
    space = numpy.array(space)
    centers = numpy.array(centers)
    radii = numpy.array(radii)

    # Add a singleton dimension if there is only one of each.
    if centers.ndim == 1:
        centers = centers[None]

    if radii.ndim == 0:
        radii = radii[None]

    # Validate the dimensions
    assert(space.ndim == 1)
    assert(centers.ndim == 2)
    assert(radii.ndim == 1)

    # Validate the shapes
    assert(space.shape == centers.shape[1:])
    assert(radii.shape == centers.shape[:1])

    # Create a hypersphere mask using a center and a radius.
    hypersphere_mask = numpy.zeros(radii.shape + tuple(space.tolist()), dtype = bool)
    for i, (each_center, each_radius) in enumerate(itertools.izip(centers, radii)):
        space_index = numpy.indices(space)

        each_point_offset = (space_index - nanshe.expanded_numpy.expand_view(each_center, tuple(space.tolist())))

        each_point_offset_sqd_sum = (each_point_offset**2).sum(axis = 0)

        each_point_offset_dist = each_point_offset_sqd_sum.astype(float)**.5

        if include_boundary:
            hypersphere_mask[i] = (each_point_offset_dist <= each_radius)
        else:
            hypersphere_mask[i] = (each_point_offset_dist < each_radius)

    return(hypersphere_mask)


def generate_gaussian_images(space, means, std_devs, magnitudes):
    """
        Generate a stack of gaussians (first index indicates which image); where, each contains a gaussian
        using a center and radius provided.

        Note:
            This uses a normalized gaussian filter to create the gaussians. So, the sum over any image in the stack will
            be the magnitude given (with some small error).

        Args:
            space(tuple of ints):                The size of the mask.

            means(list of tuples of numbers):    List of means with one per gaussian.

            std_devs(list of numbers):           List of standard deviations with one per gaussian.

            magnitudes(list of numbers):         List of magnitudes for each gaussian.

        Returns:
            numpy.ndarray:                       A stack of masks (first index indicates which mask) with a gaussian
                                                 using a center and radius for each mask.

        Examples:
            >>> generate_gaussian_images((5, 5), (2, 2), 0.5, 1) #doctest: +NORMALIZE_WHITESPACE
            array([[[ 6.96247819e-08, 2.80886418e-05, 2.07548550e-04, 2.80886418e-05, 6.96247819e-08],
                    [ 2.80886418e-05, 1.13317669e-02, 8.37310610e-02, 1.13317669e-02, 2.80886418e-05],
                    [ 2.07548550e-04, 8.37310610e-02, 6.18693507e-01, 8.37310610e-02, 2.07548550e-04],
                    [ 2.80886418e-05, 1.13317669e-02, 8.37310610e-02, 1.13317669e-02, 2.80886418e-05],
                    [ 6.96247819e-08, 2.80886418e-05, 2.07548550e-04, 2.80886418e-05, 6.96247819e-08]]])

            >>> generate_gaussian_images((5, 5), (2, 2), 0.5, 1).sum()
            1.0000000000000002

            >>> generate_gaussian_images((5, 5), (2, 2), 0.25, 1) #doctest: +NORMALIZE_WHITESPACE
            array([[[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
                    [ 0.00000000e+00, 1.12384321e-07, 3.35012940e-04, 1.12384321e-07, 0.00000000e+00],
                    [ 0.00000000e+00, 3.35012940e-04, 9.98659499e-01, 3.35012940e-04, 0.00000000e+00],
                    [ 0.00000000e+00, 1.12384321e-07, 3.35012940e-04, 1.12384321e-07, 0.00000000e+00],
                    [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]]])

            >>> generate_gaussian_images((5, 5), (2, 2), 0.25, 1).sum()
            1.0

            >>> generate_gaussian_images((5, 5), (2, 2), 0.25, 5) #doctest: +NORMALIZE_WHITESPACE
            array([[[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
                    [ 0.00000000e+00, 5.61921606e-07, 1.67506470e-03, 5.61921606e-07, 0.00000000e+00],
                    [ 0.00000000e+00, 1.67506470e-03, 4.99329749e+00, 1.67506470e-03, 0.00000000e+00],
                    [ 0.00000000e+00, 5.61921606e-07, 1.67506470e-03, 5.61921606e-07, 0.00000000e+00],
                    [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]]])

            >>> generate_gaussian_images((5, 5), (2, 2), 0.25, 5).sum()
            5.0000000000000009
    """

    # Convert to arrays
    space = numpy.array(space)
    means = numpy.array(means)
    std_devs = numpy.array(std_devs)
    magnitudes = numpy.array(magnitudes)

    # Add a singleton dimension if there is only one of each.
    if means.ndim == 1:
        means = means[None]

    if std_devs.ndim == 0:
        std_devs = std_devs[None]

    if magnitudes.ndim == 0:
        magnitudes = magnitudes[None]

    # Validate the dimensions
    assert(space.ndim == 1)
    assert(means.ndim == 2)
    assert(std_devs.ndim == 1)
    assert(magnitudes.ndim == 1)

    # Validate the shapes
    assert(space.shape == means.shape[1:])
    assert(magnitudes.shape == means.shape[:1])

    # Create a gaussian from a mean and a standard deviation.
    images = numpy.zeros(magnitudes.shape + tuple(space.tolist()), dtype = float)
    for i, (each_mean, each_std_dev, each_magnitude) in enumerate(itertools.izip(means, std_devs, magnitudes)):
        images[i][tuple(each_mean)] = each_magnitude
        images[i] = scipy.ndimage.filters.gaussian_filter(images[i],
                                                          each_std_dev,
                                                          mode = "nearest")

    return(images)

{'fraction_non_alphanumeric': 0.11282952548330404, 'fraction_numerical': 0.08646748681898067, 'mean_word_length': 2.112964989059081, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 34, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4405209', 'n_tokens_mistral': 4394, 'n_tokens_neox': 3607, 'n_words': 950}