Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
FSL
funpack
Commits
43d2827e
Commit
43d2827e
authored
Jul 08, 2019
by
Paul McCarthy
🚵
Browse files
TEST: Some unit tests checking type handling
parent
256d4585
Changes
1
Hide whitespace changes
Inline
Side-by-side
funpack/tests/test_types.py
0 → 100644
View file @
43d2827e
#!/usr/bin/env python
import
textwrap
as
tw
import
numpy
as
np
import
pandas
as
pd
import
pytest
from
.
import
tempdir
import
funpack.main
as
main
import
funpack.custom
as
custom
import
funpack.config
as
config
def
test_InternalType_trustTypes
():
_test_InternalType
(
True
)
def
test_InternalType_no_trustTypes
():
_test_InternalType
(
False
)
def
_test_InternalType
(
trustTypes
):
data
=
tw
.
dedent
(
"""
eid
\t
1-0.0
\t
2-0.0
\t
3-0.0
\t
4-0.0
\t
5-0.0
1
\t
1000
\t
2
\t
3
\t
4
\t
5
2
\t
1
\t
2
\t
3
\t
4
\t
5
3
\t
1
\t
2
\t
3
\t
4
\t
5
4
\t
1
\t
2
\t
3
\t
4
\t
5
5
\t
1
\t
2
\t
3
\t
4
\t
5
"""
).
strip
()
vartable
=
tw
.
dedent
(
"""
ID
\t
InternalType
1
\t
int8
2
\t
float64
3
\t
uint32
4
\t
float32
5
\t
int16
"""
).
strip
()
with
tempdir
():
with
open
(
'data.txt'
,
'wt'
)
as
f
:
f
.
write
(
data
)
with
open
(
'vartable.txt'
,
'wt'
)
as
f
:
f
.
write
(
vartable
)
custom
.
registerBuiltIns
()
args
=
'-nb -vf vartable.txt out.txt data.txt'
if
trustTypes
:
args
+=
' -tt'
args
=
config
.
parseArgs
(
args
.
split
())[
0
]
dtable
=
main
.
doImport
(
args
,
None
,
None
)[
0
]
assert
dtable
[:,
'1-0.0'
].
dtype
==
np
.
int8
assert
dtable
[:,
'2-0.0'
].
dtype
==
np
.
float64
assert
dtable
[:,
'3-0.0'
].
dtype
==
np
.
uint32
assert
dtable
[:,
'4-0.0'
].
dtype
==
np
.
float32
assert
dtable
[:,
'5-0.0'
].
dtype
==
np
.
int16
# The low 8 bits of 1000 == -24
# when interpreted as int8
assert
dtable
[
1
,
'1-0.0'
]
==
-
24
def
test_trustTypes
():
data
=
tw
.
dedent
(
"""
eid
\t
1-0.0
1
\t
1
2
\t
1
3
\t
badval
"""
).
strip
()
vartable
=
tw
.
dedent
(
"""
ID
\t
InternalType
1
\t
float32
"""
).
strip
()
with
tempdir
():
with
open
(
'data.txt'
,
'wt'
)
as
f
:
f
.
write
(
data
)
with
open
(
'vartable.txt'
,
'wt'
)
as
f
:
f
.
write
(
vartable
)
custom
.
registerBuiltIns
()
args
=
'-nb -tt -vf vartable.txt out.txt data.txt'
args
=
config
.
parseArgs
(
args
.
split
())[
0
]
with
pytest
.
raises
(
ValueError
):
dtable
=
main
.
doImport
(
args
,
None
,
None
)[
0
]
args
=
'-nb -vf vartable.txt out.txt data.txt'
args
=
config
.
parseArgs
(
args
.
split
())[
0
]
dtable
=
main
.
doImport
(
args
,
None
,
None
)[
0
]
assert
dtable
[
1
,
'1-0.0'
]
==
1
assert
dtable
[
2
,
'1-0.0'
]
==
1
assert
pd
.
isna
(
dtable
[
3
,
'1-0.0'
])
def
test_largevals
():
# Some random values which cannot be
# represented with float32 data type
vals
=
np
.
array
([
10001249182
,
48192124975
,
14328901242
])
data
=
'eid
\t
1-0.0
\n
'
for
i
,
v
in
enumerate
(
vals
):
data
+=
'{}
\t
{}
\n
'
.
format
(
i
,
v
)
with
tempdir
():
with
open
(
'data.txt'
,
'wt'
)
as
f
:
f
.
write
(
data
)
# If we specify the variable as an integer,
# funpack will default to float32, and the
# values will be misinterpreted
vartable
=
'ID
\t
Type
\n
1
\t
integer'
with
open
(
'vartable.txt'
,
'wt'
)
as
f
:
f
.
write
(
vartable
)
custom
.
registerBuiltIns
()
args
=
'-nb -vf vartable.txt out.txt data.txt'
args
=
config
.
parseArgs
(
args
.
split
())[
0
]
dtable
=
main
.
doImport
(
args
,
None
,
None
)[
0
]
assert
not
np
.
any
(
dtable
[:,
'1-0.0'
]
==
vals
)
# But we can use InternalType to
# specify a suitable storage type
vartable
=
'ID
\t
Type
\t
InternalType
\n
1
\t
integer
\t
float64'
with
open
(
'vartable.txt'
,
'wt'
)
as
f
:
f
.
write
(
vartable
)
args
=
'-nb -vf vartable.txt out.txt data.txt'
args
=
config
.
parseArgs
(
args
.
split
())[
0
]
dtable
=
main
.
doImport
(
args
,
None
,
None
)[
0
]
assert
np
.
all
(
dtable
[:,
'1-0.0'
]
==
vals
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment