<commit_before>cimport cython
cimport numpy as np

cdef extern from "dpy_math.h" nogil:
    int dpy_rint(double)

from .interpolation cimport(trilinear_interpolate4d,
                            _trilinear_interpolate_c_4d)

import numpy as np

cdef class TissueClassifier:
    cpdef TissueClass check_point(self, double[::1] point) except PYERROR:
        pass


cdef class BinaryTissueClassifier(TissueClassifier):
    """
    cdef:
        unsigned char[:, :, :] mask
    """

    def __cinit__(self, mask):
        self.interp_out_view = self.interp_out_double
        self.mask = (mask > 0).astype('uint8')

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.initializedcheck(False)
    cpdef TissueClass check_point(self, double[::1] point) except PYERROR:
        cdef:
            unsigned char result
            int err
            int voxel[3]

        if point.shape[0] != 3:
            raise ValueError("Point has wrong shape")

        voxel[0] = int(dpy_rint(point[0]))
        voxel[1] = int(dpy_rint(point[1]))
        voxel[2] = int(dpy_rint(point[2]))

        if (voxel[0] < 0 or voxel[0] > self.mask.shape[0]
                or voxel[1] < 0 or voxel[1] > self.mask.shape[1]
                or voxel[2] < 0 or voxel[2] > self.mask.shape[2]):
            return OUTSIDEIMAGE

        result = self.mask[voxel[0], voxel[1], voxel[2]]

        if result > 0:
            return TRACKPOINT
        else:
            return ENDPOINT


cdef class ThresholdTissueClassifier(TissueClassifier):
    """
    # Declarations from tissue_classifier.pxd bellow
    cdef:
        double threshold, interp_out_double[1]
        double[:]  interp_out_view = interp_out_view
        double[:, :, :] metric_map
    """

    def __cinit__(self, metric_map, threshold):
        self.interp_out_view = self.interp_out_double
        self.metric_map = np.asarray(metric_map, 'float64')
        self.threshold = threshold

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.initializedcheck(False)<commit_msg>Fix out of bounds point not being classified OUTSIDEIMAGE (binary classifier)<commit_after>cimport cython
cimport numpy as np

cdef extern from "dpy_math.h" nogil:
    int dpy_rint(double)

from .interpolation cimport(trilinear_interpolate4d,
                            _trilinear_interpolate_c_4d)

import numpy as np

cdef class TissueClassifier:
    cpdef TissueClass check_point(self, double[::1] point) except PYERROR:
        pass


cdef class BinaryTissueClassifier(TissueClassifier):
    """
    cdef:
        unsigned char[:, :, :] mask
    """

    def __cinit__(self, mask):
        self.interp_out_view = self.interp_out_double
        self.mask = (mask > 0).astype('uint8')

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.initializedcheck(False)
    cpdef TissueClass check_point(self, double[::1] point) except PYERROR:
        cdef:
            unsigned char result
            int err
            int voxel[3]

        if point.shape[0] != 3:
            raise ValueError("Point has wrong shape")

        voxel[0] = int(dpy_rint(point[0]))
        voxel[1] = int(dpy_rint(point[1]))
        voxel[2] = int(dpy_rint(point[2]))

        if (voxel[0] < 0 or voxel[0] >= self.mask.shape[0]
                or voxel[1] < 0 or voxel[1] >= self.mask.shape[1]
                or voxel[2] < 0 or voxel[2] >= self.mask.shape[2]):
            return OUTSIDEIMAGE

        result = self.mask[voxel[0], voxel[1], voxel[2]]

        if result > 0:
            return TRACKPOINT
        else:
            return ENDPOINT


cdef class ThresholdTissueClassifier(TissueClassifier):
    """
    # Declarations from tissue_classifier.pxd bellow
    cdef:
        double threshold, interp_out_double[1]
        double[:]  interp_out_view = interp_out_view
        double[:, :, :] metric_map
    """

    def __cinit__(self, metric_map, threshold):
        self.interp_out_view = self.interp_out_double
        self.metric_map = np.asarray(metric_map, 'float64')
        self.threshold = threshold

    @cython.boundscheck(False)
    @cython.wraparound(False)
    @cython.initializedcheck(False)
{'fraction_non_alphanumeric': 0.10679145668346532, 'fraction_numerical': 0.016318694504439644, 'mean_word_length': 2.5593509820666096, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '7379218', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1385, 'n_words': 291}