Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit43746b1

Browse files
committed
NF: add utils module for code support functions
Add to_scalar function with tests.
1 parent3d59146 commit43746b1

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

‎nibabel/tests/test_utils.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
""" Test for utils module
2+
"""
3+
4+
importnumpyasnp
5+
6+
fromnibabel.utilsimportto_scalar
7+
8+
fromnose.toolsimportassert_equal,assert_true,assert_false,assert_raises
9+
10+
11+
deftest_to_scalar():
12+
forpass_thruin (2,2.3,'foo',b'foo', [], (), [2], (2,),object()):
13+
assert_true(to_scalar(pass_thru)ispass_thru)
14+
forarr_contentsin (2,2.3,'foo',b'foo'):
15+
arr=np.array(arr_contents)
16+
out=to_scalar(arr)
17+
assert_false(to_scalar(arr)isarr)
18+
assert_equal(out,arr_contents)
19+
# Promote to 1 and 2D and check contents
20+
assert_equal(to_scalar(np.atleast_1d(arr)),arr_contents)
21+
assert_equal(to_scalar(np.atleast_2d(arr)),arr_contents)
22+
assert_raises(ValueError,to_scalar,np.array([1,2]))

‎nibabel/utils.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
""" Code support routines, not otherwise classified
2+
"""
3+
4+
5+
defto_scalar(val):
6+
""" Return scalar representation of `val`
7+
8+
Return scalar value from numpy array, or pass through value if not numpy
9+
array.
10+
11+
Parameters
12+
----------
13+
val : object
14+
numpy array or other object.
15+
16+
Returns
17+
-------
18+
out : object
19+
Result of ``val.item()`` if `val` has an ``item`` method, else `val`.
20+
"""
21+
returnval.item()ifhasattr(val,'item')elseval

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp