Skip to content

Commit fedd147

Browse files
committed
DOC+TST - docstrings + doctests for arraywriters
Doctests for the supporting functions.
1 parent bda026e commit fedd147

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

nibabel/arraywriters.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ def __init__(self, array, out_dtype=None)
66
77
and methods
88
9+
* scaling_needed() - returns True if array requires scaling for write
10+
*.finite_range() - returns min, max of self.array
911
* to_fileobj(fileobj, offset=None, order='F')
1012
11-
They do have attributes:
13+
They have attributes:
1214
1315
* array
1416
* out_dtype
@@ -178,6 +180,20 @@ def to_fileobj(self, fileobj, order='F', nan2zero=True):
178180

179181

180182
class SlopeArrayWriter(ArrayWriter):
183+
""" ArrayWriter that can use scalefactor for writing arrays
184+
185+
The scalefactor allows the array writer to write floats to int output types,
186+
and rescale larger ints to smaller. It can therefore lose precision.
187+
188+
It extends the ArrayWriter class with attribute:
189+
190+
* slope
191+
192+
and methods:
193+
194+
* reset() - reset slope to default (not adapted to self.array)
195+
* calc_scale() - calculate slope to best write self.array
196+
"""
181197

182198
def __init__(self, array, out_dtype=None, calc_scale=True,
183199
scaler_dtype=np.float32):
@@ -339,6 +355,22 @@ def _range_scale(self):
339355

340356

341357
class SlopeInterArrayWriter(SlopeArrayWriter):
358+
""" Array writer that can use slope and intercept to scale array
359+
360+
The writer can subtract an intercept, and divided by a slope, in order to
361+
be able to convert floating point values into a (u)int range, or to convert
362+
larger (u)ints to smaller.
363+
364+
It extends the ArrayWriter class with attributes:
365+
366+
* inter
367+
* slope
368+
369+
and methods:
370+
371+
* reset() - reset inter, slope to default (not adapted to self.array)
372+
* calc_scale() - calculate inter, slope to best write self.array
373+
"""
342374

343375
def __init__(self, array, out_dtype=None, calc_scale=True,
344376
scaler_dtype=np.float32):
@@ -357,7 +389,7 @@ def __init__(self, array, out_dtype=None, calc_scale=True,
357389
If False, then you can calculate this scaling with
358390
``obj.calc_scale()`` - see examples
359391
scaler_dtype : dtype-like, optional
360-
specifier for numpy dtype for scaling
392+
specifier for numpy dtype for slope, intercept
361393
362394
Examples
363395
--------
@@ -480,6 +512,16 @@ def get_slope_inter(writer):
480512
slope in `writer` or 1.0 if not present
481513
inter : scalar
482514
intercept in `writer` or 0.0 if not present
515+
516+
Examples
517+
--------
518+
>>> arr = np.arange(10)
519+
>>> get_slope_inter(ArrayWriter(arr))
520+
(1.0, 0.0)
521+
>>> get_slope_inter(SlopeArrayWriter(arr))
522+
(1.0, 0.0)
523+
>>> get_slope_inter(SlopeInterArrayWriter(arr))
524+
(1.0, 0.0)
483525
"""
484526
try:
485527
slope = writer.slope
@@ -514,6 +556,18 @@ def make_array_writer(data, out_type, has_intercept=True, has_slope=True,
514556
writer : arraywriter instance
515557
Instance of array writer, with class adapted to `has_intercept` and
516558
`has_slope`.
559+
560+
Examples
561+
--------
562+
>>> aw = make_array_writer(np.arange(10), np.uint8, True, True)
563+
>>> type(aw) == SlopeInterArrayWriter
564+
True
565+
>>> aw = make_array_writer(np.arange(10), np.uint8, False, True)
566+
>>> type(aw) == SlopeArrayWriter
567+
True
568+
>>> aw = make_array_writer(np.arange(10), np.uint8, False, False)
569+
>>> type(aw) == ArrayWriter
570+
True
517571
"""
518572
data = np.asarray(data)
519573
if has_intercept == True and has_slope == False:

0 commit comments

Comments
 (0)