testing.doctester

Module: testing.doctester

Inheritance diagram for nipy.testing.doctester:

Inheritance diagram of nipy.testing.doctester

Custom doctester based on Numpy doctester

To run doctests via nose, you’ll need nosetests nipy/testing/doctester.py --doctest-test, because this file will be identified as containing tests.

Classes

NipyDoctest

class nipy.testing.doctester.NipyDoctest

Bases: nipy.fixes.numpy.testing.noseclasses.NumpyDoctest

__init__()
name = 'nipydoctest'
options(parser, env=<class nipy.utils._NoValue>)
out_check_class

alias of NipyOutputChecker

set_test_context(test)

NipyOutputChecker

class nipy.testing.doctester.NipyOutputChecker

Bases: nipy.fixes.numpy.testing.noseclasses.NumpyOutputChecker

check_output(want, got, optionflags)

Functions

nipy.testing.doctester.ignore_dtype(in_str)

Removes dtype=[dtype] from string in_str

Parameters:

in_str : str

String maybe containing dtype specifier

Returns:

out_str : str

String from which the dtype specifier has been removed.

Examples

>>> arr = np.arange(5, dtype='i2')

Here’s the normal repr:

>>> arr
array([0, 1, 2, 3, 4], dtype=int16)

The repr with the dtype bits removed

>>> ignore_dtype(repr(arr))
'array([0, 1, 2, 3, 4])'
>>> ignore_dtype('something(again, dtype=something)')
'something(again)'

Even if there are more closed brackets after the dtype

>>> ignore_dtype('something(again, dtype=something) (1, 2)')
'something(again) (1, 2)'

We need the close brackets to match

>>> ignore_dtype('again, dtype=something')
'again, dtype=something'
nipy.testing.doctester.round_numbers(in_str, precision)

Replace fp numbers in in_str with numbers rounded to precision

Parameters:

in_str : str

string possibly containing floating point numbers

precision : int

number of decimal places to round to

Returns:

out_str : str

in_str with any floating point numbers replaced with same numbers rounded to precision decimal places.

Examples

>>> round_numbers('A=0.234, B=12.345', 2)
'A=0.23, B=12.35'

Rounds the floating point value as it finds it in the string. This is even true for numbers with exponentials. Remember that:

>>> '%.3f' % 0.3339e-10
'0.000'

This routine will recognize an exponential as something to process, but only works on the decimal part (leaving the exponential part is it is):

>>> round_numbers('(0.3339e-10, "string")', 3)
'(0.334e-10, "string")'
nipy.testing.doctester.strip_array_repr(in_str)

Removes array-specific part of repr from string in_str

This parser only works on lines that contain only an array repr (and therefore start with array, and end with a close parenthesis. To remove dtypes in array reprs that may be somewhere within the line, use the IGNORE_DTYPE doctest option.

Parameters:

in_str : str

String maybe containing a repr for an array

Returns:

out_str : str

String from which the array specific parts of the repr have been removed.

Examples

>>> arr = np.arange(5, dtype='i2')

Here’s the normal repr:

>>> arr
array([0, 1, 2, 3, 4], dtype=int16)

The repr with the ‘array’ bits removed:

>>> strip_array_repr(repr(arr))
'[0, 1, 2, 3, 4]'