Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dad8714

Browse files
committed
Finish adding and checking remaining tests for clip (and choose). All tests now pass for new clip method.
1 parent 701a913 commit dad8714

3 files changed

Lines changed: 98 additions & 100 deletions

File tree

THANKS.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ Albert Strasheim for documentation, bug-fixes, regression tests and
3434
Valgrind expertise.
3535
Stefan van der Walt for documentation, bug-fixes and regression-tests.
3636
Andrew Straw for help with http://www.scipy.org, documentation, and testing.
37-
David Cournapeau for documentation, bug-fixes, and code contributions.
37+
David Cournapeau for documentation, bug-fixes, and code contributions including fast_clipping.

numpy/core/src/multiarraymodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,7 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
12911291
else
12921292
oflags = NPY_CARRAY;
12931293
oflags |= NPY_UPDATEIFCOPY | NPY_FORCECAST;
1294+
Py_INCREF(indescr);
12941295
newout = (NPY_AO*)PyArray_FromArray(out, indescr, oflags);
12951296
if (newout == NULL) goto fail;
12961297
}

numpy/core/tests/test_numeric.py

Lines changed: 96 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -568,105 +568,102 @@ def test_type_cast_09(self):
568568
ac = self.fastclip(a, m , M, out = b)
569569
assert_array_strict_equal(ac, act)
570570

571-
## # This looks like a data-type descriptor is not reference
572-
## # counted right.
573-
## print "=== OUT arg: testing non native with native scalar, min/max, " + \
574-
## "out non native==="
575-
## a = self._generate_non_native_data(self.nr, self.nc)
576-
## b = a.copy()
577-
## b = b.astype(b.dtype.newbyteorder('>'))
578-
## bt = b.copy()
579-
## m = -0.5
580-
## M = 1.
581-
## self.fastclip(a, m , M, out = b)
582-
## self.clip(a, m, M, out = bt)
583-
## assert_array_strict_equal(b, bt)
584-
585-
## print "=== OUT arg: testing native int32 input and min/max and float out ==="
586-
## print "\t NOT TESTED (current clip is buggy in this case)"
587-
## a = self._generate_int_data(self.nr, self.nc)
588-
## b = zeros(a.shape, dtype = float32)
589-
## m = int32(0)
590-
## M = int32(1)
591-
## act = self.clip(a, m, M, out = b)
592-
## ac = self.fastclip(a, m , M, out = b)
593-
## assert_array_strict_equal(ac, act)
594-
595-
## def test_clip_with_out_simple(self):
596-
## print "=== WITH OUT testing native double input with scalar min/max ==="
597-
## a = self._generate_data(self.nr, self.nc)
598-
## m = -0.5
599-
## M = 0.6
600-
## ac = zeros(a.shape)
601-
## act = zeros(a.shape)
602-
## self.fastclip(a, m, M, ac)
603-
## self.clip(a, m, M, act)
604-
## assert_array_strict_equal(ac, act)
605-
606-
## def test_clip_with_out_simple2(self):
607-
## print "=== WITH OUT testing native int32 input "+\
608-
## "with double min/max and int32 out ==="
609-
## a = self._generate_int32_data(self.nr, self.nc)
610-
## m = float64(0)
611-
## M = float64(2)
612-
## ac = zeros(a.shape, dtype = int32)
613-
## act = ac.copy()
614-
## self.fastclip(a, m, M, ac)
615-
## self.clip(a, m, M, act)
616-
## assert_array_strict_equal(ac, act)
617-
618-
## def test_clip_with_out_simple_int32(self):
619-
## print "=== WITH OUT testing native int32 input with int32 scalar min/max and int64 out ==="
620-
## a = self._generate_int32_data(self.nr, self.nc)
621-
## m = int32(-1)
622-
## M = int32(1)
623-
## ac = zeros(a.shape, dtype = int64)
624-
## act = ac.copy()
625-
## self.fastclip(a, m, M, ac)
626-
## self.clip(a, m, M, act)
627-
## assert_array_strict_equal(ac, act)
628-
629-
## def test_clip_with_out_array_int32(self):
630-
## print "=== WITH OUT testing native int32 input with double array min/max and int32 out ==="
631-
## a = self._generate_int32_data(self.nr, self.nc)
632-
## m = zeros(a.shape, float64)
633-
## M = float64(1)
634-
## ac = zeros(a.shape, dtype = int32)
635-
## act = ac.copy()
636-
## self.fastclip(a, m, M, ac)
637-
## self.clip(a, m, M, act)
638-
## assert_array_strict_equal(ac, act)
639-
640-
## def test_clip_with_out_array_outint32(self):
641-
## print "=== WITH OUT testing native double input with scalar min/max and int out ==="
642-
## a = self._generate_data(self.nr, self.nc)
643-
## m = -1.0
644-
## M = 2.0
645-
## ac = zeros(a.shape, dtype = int32)
646-
## act = ac.copy()
647-
## self.fastclip(a, m, M, ac)
648-
## self.clip(a, m, M, act)
649-
## assert_array_strict_equal(ac, act)
650-
651-
## def test_clip_inplace_array(self):
652-
## print "=== INPLACE: testing native double input with array min/max ==="
653-
## a = self._generate_data(self.nr, self.nc)
654-
## ac = a.copy()
655-
## m = zeros(a.shape)
656-
## M = 1.0
657-
## self.fastclip(a, m, M, a)
658-
## self.clip(a, m, M, ac)
659-
## assert_array_strict_equal(a, ac)
660-
661-
## def test_clip_inplace_simple(self):
662-
## print "=== INPLACE: testing native double input with scalar min/max ==="
663-
## a = self._generate_data(self.nr, self.nc)
664-
## ac = a.copy()
665-
## m = -0.5
666-
## M = 0.6
667-
## self.fastclip(a, m, M, a)
668-
## self.clip(a, m, M, ac)
669-
## assert_array_strict_equal(a, ac)
571+
def test_type_cast_10(self):
572+
"Test non native with native scalar, min/max, out non native"
573+
a = self._generate_non_native_data(self.nr, self.nc)
574+
b = a.copy()
575+
b = b.astype(b.dtype.newbyteorder('>'))
576+
bt = b.copy()
577+
m = -0.5
578+
M = 1.
579+
self.fastclip(a, m , M, out = b)
580+
self.clip(a, m, M, out = bt)
581+
assert_array_strict_equal(b, bt)
582+
583+
def test_type_cast_11(self):
584+
"Test native int32 input and min/max and float out"
585+
a = self._generate_int_data(self.nr, self.nc)
586+
b = zeros(a.shape, dtype = float32)
587+
m = int32(0)
588+
M = int32(1)
589+
act = self.clip(a, m, M, out = b)
590+
ac = self.fastclip(a, m , M, out = b)
591+
assert_array_strict_equal(ac, act)
592+
593+
def test_clip_with_out_simple(self):
594+
"Test native double input with scalar min/max"
595+
a = self._generate_data(self.nr, self.nc)
596+
m = -0.5
597+
M = 0.6
598+
ac = zeros(a.shape)
599+
act = zeros(a.shape)
600+
self.fastclip(a, m, M, ac)
601+
self.clip(a, m, M, act)
602+
assert_array_strict_equal(ac, act)
603+
604+
def test_clip_with_out_simple2(self):
605+
"Test native int32 input with double min/max and int32 out"
606+
a = self._generate_int32_data(self.nr, self.nc)
607+
m = float64(0)
608+
M = float64(2)
609+
ac = zeros(a.shape, dtype = int32)
610+
act = ac.copy()
611+
self.fastclip(a, m, M, ac)
612+
self.clip(a, m, M, act)
613+
assert_array_strict_equal(ac, act)
614+
615+
def test_clip_with_out_simple_int32(self):
616+
"Test native int32 input with int32 scalar min/max and int64 out"
617+
a = self._generate_int32_data(self.nr, self.nc)
618+
m = int32(-1)
619+
M = int32(1)
620+
ac = zeros(a.shape, dtype = int64)
621+
act = ac.copy()
622+
self.fastclip(a, m, M, ac)
623+
self.clip(a, m, M, act)
624+
assert_array_strict_equal(ac, act)
625+
626+
def test_clip_with_out_array_int32(self):
627+
"Test native int32 input with double array min/max and int32 out"
628+
a = self._generate_int32_data(self.nr, self.nc)
629+
m = zeros(a.shape, float64)
630+
M = float64(1)
631+
ac = zeros(a.shape, dtype = int32)
632+
act = ac.copy()
633+
self.fastclip(a, m, M, ac)
634+
self.clip(a, m, M, act)
635+
assert_array_strict_equal(ac, act)
636+
637+
def test_clip_with_out_array_outint32(self):
638+
"Test native double input with scalar min/max and int out"
639+
a = self._generate_data(self.nr, self.nc)
640+
m = -1.0
641+
M = 2.0
642+
ac = zeros(a.shape, dtype = int32)
643+
act = ac.copy()
644+
self.fastclip(a, m, M, ac)
645+
self.clip(a, m, M, act)
646+
assert_array_strict_equal(ac, act)
647+
648+
def test_clip_inplace_array(self):
649+
"Test native double input with array min/max"
650+
a = self._generate_data(self.nr, self.nc)
651+
ac = a.copy()
652+
m = zeros(a.shape)
653+
M = 1.0
654+
self.fastclip(a, m, M, a)
655+
self.clip(a, m, M, ac)
656+
assert_array_strict_equal(a, ac)
657+
658+
def test_clip_inplace_simple(self):
659+
"Test native double input with scalar min/max"
660+
a = self._generate_data(self.nr, self.nc)
661+
ac = a.copy()
662+
m = -0.5
663+
M = 0.6
664+
self.fastclip(a, m, M, a)
665+
self.clip(a, m, M, ac)
666+
assert_array_strict_equal(a, ac)
670667

671668

672669
import sys

0 commit comments

Comments
 (0)