<commit_before>"""
Pythonic utilities ported to C [Cython] for speedup.
"""

from __future__ import division

import numpy as np
cimport numpy as np
DTYPE = np.double
ctypedef np.double_t DTYPE_t
ctypedef np.uint_t UINT_t
ctypedef np.int_t INT_t

import cython

cdef extern from "math.h":
    double sqrt(double x)
    double exp(double x)

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.embedsignature(True)
def disperse_grism_object(np.ndarray[DTYPE_t, ndim=2] flux, unsigned int id, np.ndarray[UINT_t, ndim=2] segm, np.ndarray[INT_t, ndim=1] xord, np.ndarray[INT_t, ndim=1] yord, np.ndarray[DTYPE_t, ndim=1] ford, np.ndarray[DTYPE_t, ndim=2] object):
    """
    disperse_grism_object(flux, id, segm, xord, yord, ford, object)
    
    The orders of the grism spectrum are defined in xord, yord, ford.  For a given object, `id`, loop through the pixels within the `segm` segmentation image scaling the dispersed spectrum by the `flux` value in each pixel.  
    
    The result is updated in place to the `object` image.
    
    """
    cdef unsigned int NX, NY, x, y, Nord, iord
    cdef int xxi, yyi
    #cdef np.ndarray[DTYPE_t, ndim=2] object
    cdef double flux_i
    
    NY, NX = np.shape(segm)
    Nord = np.shape(xord)[0]
    
    #object = np.zeros((NY,NX), dtype=DTYPE)
    
    for x in range(NX):
        for y in range(NY):
            if segm[y,x] == id:
                flux_i = flux[y,x]
                for iord in range(Nord):
                    xxi = x+xord[iord]
                    yyi = y+yord[iord]
                    if (xxi >= 0) & (xxi < NX) & (yyi >= 0) & (yyi < NY):
                        object[yyi,xxi] += ford[iord]*flux_i
                        
    return True

@cython.boundscheck(False)
@cython.embedsignature(True)
def total_flux(np.ndarray[DTYPE_t, ndim=2] flux, np.ndarray[UINT_t, ndim=2] segm):
    """
    total_flux(flux, segm)
    
    `segm` is a segmentation image with intensities defined in the `flux` image`.  
    
    Return an array, `total_flux`, where
        total_flux[i] = sum(flux[where(segm == i)])
    
    Note that there will be zero-valued entries in the output arrays where
    there were no pixels with that index number in the segmentation image.
    """
    cdef unsigned int NX, NY, x, y
    
    cdef unsigned int id_max
    cdef np.ndarray[DTYPE_t, ndim=1] total_fluxes
    
    id_max = segm.max()<commit_msg>utils_c/reduce_c: reduce array lookups in "disperse_grism_object".<commit_after>"""
Pythonic utilities ported to C [Cython] for speedup.
"""

from __future__ import division

import numpy as np
cimport numpy as np
DTYPE = np.double
ctypedef np.double_t DTYPE_t
ctypedef np.uint_t UINT_t
ctypedef np.int_t INT_t

import cython

cdef extern from "math.h":
    double sqrt(double x)
    double exp(double x)

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.embedsignature(True)
def disperse_grism_object(np.ndarray[DTYPE_t, ndim=2] flux, unsigned int id, np.ndarray[UINT_t, ndim=2] segm, np.ndarray[INT_t, ndim=1] xord, np.ndarray[INT_t, ndim=1] yord, np.ndarray[DTYPE_t, ndim=1] ford, np.ndarray[DTYPE_t, ndim=2] object, xpix=None, ypix=None):
    """
    disperse_grism_object(flux, id, segm, xord, yord, ford, object)
    
    The orders of the grism spectrum are defined in xord, yord, ford.  For a given object, `id`, loop through the pixels within the `segm` segmentation image scaling the dispersed spectrum by the `flux` value in each pixel.  
    
    The result is updated in place to the `object` image.
    
    """
    cdef unsigned int NX, NY, x, y, Nord, iord, x0, x1, y0, y1
    cdef int xxi, yyi
    #cdef np.ndarray[DTYPE_t, ndim=2] object
    cdef double flux_i
            
    NY, NX = np.shape(segm)
    Nord = np.shape(xord)[0]
    
    if xpix is not None:
        x0, x1 = xpix
    else:
        x0, x1 = 0, NX
        
    if ypix is not None:
        y0, y1 = ypix
    else:
        y0, y1 = 0, NY
    
    #object = np.zeros((NY,NX), dtype=DTYPE)
    
    for x in range(x0, x1):
        for y in range(y0, y1):
            if segm[y,x] == id:
                flux_i = flux[y,x]
                for iord in range(Nord):
                    xxi = x+xord[iord]
                    yyi = y+yord[iord]
                    if (xxi >= 0) & (xxi < NX) & (yyi >= 0) & (yyi < NY):
                        object[yyi,xxi] += ford[iord]*flux_i
                        
    return True

@cython.boundscheck(False)
@cython.embedsignature(True)
def total_flux(np.ndarray[DTYPE_t, ndim=2] flux, np.ndarray[UINT_t, ndim=2] segm):
    """
    total_flux(flux, segm)
    
    `segm` is a segmentation image with intensities defined in the `flux` image`.  
    
    Return an array, `total_flux`, where
        total_flux[i] = sum(flux[where(segm == i)])
    
    Note that there will be zero-valued entries in the output arrays where
    there were no pixels with that index number in the segmentation image.
    """
    cdef unsigned int NX, NY, x, y
    
    cdef unsigned int id_max
    cdef np.ndarray[DTYPE_t, ndim=1] total_fluxes
    
    id_max = segm.max()
{'fraction_non_alphanumeric': 0.10674267741301356, 'fraction_numerical': 0.008649498722233143, 'mean_word_length': 2.5730337078651684, 'pattern_counts': {'":': 2, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5860780', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1859, 'n_words': 552}