mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-04 00:31:27 +08:00
ENH: Add inspect() function to adjusted_array.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
Tests for chunked adjustments.
|
||||
"""
|
||||
from textwrap import dedent
|
||||
from unittest import TestCase
|
||||
|
||||
from nose_parameterized import parameterized
|
||||
@@ -323,3 +324,29 @@ class AdjustedArrayTestCase(TestCase):
|
||||
|
||||
with self.assertRaisesRegexp(ValueError, msg):
|
||||
adjusted_array(data, bad_mask, {})
|
||||
|
||||
def test_inspect(self):
|
||||
data = arange(15, dtype=float).reshape(5, 3)
|
||||
adj_array = adjusted_array(
|
||||
data,
|
||||
NOMASK,
|
||||
{4: [Float64Multiply(2, 3, 0, 0, 4.0)]},
|
||||
)
|
||||
|
||||
expected = dedent(
|
||||
"""\
|
||||
Adjusted Array:
|
||||
|
||||
Data:
|
||||
array([[ 0., 1., 2.],
|
||||
[ 3., 4., 5.],
|
||||
[ 6., 7., 8.],
|
||||
[ 9., 10., 11.],
|
||||
[ 12., 13., 14.]])
|
||||
|
||||
Adjustments:
|
||||
{4: [Float64Multiply(first_row=2, last_row=3, first_col=0, \
|
||||
last_col=0, value=4.000000)]}
|
||||
"""
|
||||
)
|
||||
self.assertEqual(expected, adj_array.inspect())
|
||||
|
||||
@@ -5,6 +5,8 @@ from cpython cimport (
|
||||
Py_EQ,
|
||||
PyObject_RichCompare,
|
||||
)
|
||||
from pprint import pformat
|
||||
|
||||
from numpy import (
|
||||
asarray,
|
||||
bool_,
|
||||
@@ -128,6 +130,15 @@ cdef class Float64AdjustedArray(AdjustedArray):
|
||||
self._data = data
|
||||
self.adjustments = adjustments
|
||||
|
||||
def inspect(self):
|
||||
return (
|
||||
"Adjusted Array:\n\nData:\n"
|
||||
"{data}\n\nAdjustments:\n{adjustments}\n".format(
|
||||
data=repr(asarray(self._data)),
|
||||
adjustments=pformat(self.adjustments),
|
||||
)
|
||||
)
|
||||
|
||||
property dtype:
|
||||
def __get__(self):
|
||||
return float64
|
||||
@@ -216,10 +227,17 @@ cdef class _Float64AdjustedArrayWindow:
|
||||
self.anchor += 1
|
||||
return out
|
||||
|
||||
def __repr__(self):
|
||||
return "%s(window_length=%d, anchor=%d, max_anchor=%d)" % (
|
||||
type(self).__name__,
|
||||
self.window_length,
|
||||
self.anchor,
|
||||
self.max_anchor,
|
||||
def inspect(self):
|
||||
return (
|
||||
"{type_}\n"
|
||||
"Window Length: {window_length}\n"
|
||||
"Current Buffer:\n"
|
||||
"{data}\n"
|
||||
"Remaining Adjustments:\n"
|
||||
"{adjustments}\n"
|
||||
).format(
|
||||
type_=type(self).__name__,
|
||||
window_length=self.window_length,
|
||||
data=asarray(self.data[self.anchor - self.window_length:self.anchor]),
|
||||
adjustments=pformat(self.adjustments),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user