-
Notifications
You must be signed in to change notification settings - Fork 438
Cleanup TransferFunction Input Sanitization #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
37b06f4
Added notebook for doing development work on IO-Cleanup.
feb68f5
Added .eggs directory to gitignore.
5e2d95d
Broke out the numerator/denominator cleanup function into a separate …
cd7f596
Merge remote-tracking branch 'upstream/master'
509ff8e
Removed test notebook
d44489f
Renamed cleanPart to _cleanPart since it is an internal function, as …
b1437f0
unit test from kangwonlee commit facfe9c49d79227dd3bdcd22af9d0aa917ac…
murrayrm 10cdb7a
TST: renamed unit test from kangwonlee
murrayrm af8837e
Merge branch 'InputSanitization' of https://github.com/jed-frey/pytho…
murrayrm b697a08
Merge branch 'jed-frey-InputSanitization' into sstf_input_sanitization
murrayrm 68b5dd1
address additional comments from roryyorke in PR #135
murrayrm 11a6264
Merge pull request #1 from murrayrm/sstf_input_sanitization
bbb902f
Merge branch 'master' into InputSanitization
murrayrm 8c997ff
Merge branch 'master' into InputSanitization
murrayrm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# input_element_int_test.py | ||
# | ||
# Author: Kangwon Lee (kangwonlee) | ||
# Date: 22 Oct 2017 | ||
# | ||
# Unit tests contributed as part of PR #158, "SISO tf() may not work | ||
# with numpy arrays with numpy.int elements" | ||
# | ||
# Modified: | ||
# * 29 Dec 2017, RMM - updated file name and added header | ||
|
||
import unittest | ||
import numpy as np | ||
import control as ctl | ||
|
||
class TestTfInputIntElement(unittest.TestCase): | ||
# currently these do not pass | ||
def test_tf_den_with_numpy_int_element(self): | ||
num = 1 | ||
den = np.convolve([1, 2, 1], [1, 1, 1]) | ||
|
||
sys = ctl.tf(num, den) | ||
|
||
self.assertAlmostEqual(1.0, ctl.dcgain(sys)) | ||
|
||
def test_tf_num_with_numpy_int_element(self): | ||
num = np.convolve([1], [1, 1]) | ||
den = np.convolve([1, 2, 1], [1, 1, 1]) | ||
|
||
sys = ctl.tf(num, den) | ||
|
||
self.assertAlmostEqual(1.0, ctl.dcgain(sys)) | ||
|
||
# currently these pass | ||
def test_tf_input_with_int_element_works(self): | ||
num = 1 | ||
den = np.convolve([1.0, 2, 1], [1, 1, 1]) | ||
|
||
sys = ctl.tf(num, den) | ||
|
||
self.assertAlmostEqual(1.0, ctl.dcgain(sys)) | ||
|
||
def test_ss_input_with_int_element(self): | ||
ident = np.matrix(np.identity(2), dtype=int) | ||
a = np.matrix([[0, 1], | ||
[-1, -2]], dtype=int) * ident | ||
b = np.matrix([[0], | ||
[1]], dtype=int) | ||
c = np.matrix([[0, 1]], dtype=int) | ||
d = 0 | ||
|
||
sys = ctl.ss(a, b, c, d) | ||
sys2 = ctl.ss2tf(sys) | ||
self.assertAlmostEqual(ctl.dcgain(sys), ctl.dcgain(sys2)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,261 @@ | ||
#!/usr/bin/env python | ||
# | ||
# xferfcn_input_test.py - test inputs to TransferFunction class | ||
# jed-frey, 18 Feb 2017 (based on xferfcn_test.py) | ||
|
||
import unittest | ||
import numpy as np | ||
|
||
from numpy import int, int8, int16, int32, int64 | ||
from numpy import float, float16, float32, float64, float128 | ||
from numpy import all, ndarray, array | ||
|
||
from control.xferfcn import _cleanPart | ||
|
||
class TestXferFcnInput(unittest.TestCase): | ||
"""These are tests for functionality of cleaning and validating | ||
XferFucnInput.""" | ||
|
||
# Tests for raising exceptions. | ||
def testBadInputType(self): | ||
"""Give the part cleaner invalid input type.""" | ||
|
||
self.assertRaises(TypeError, _cleanPart, [[0., 1.], [2., 3.]]) | ||
|
||
def testBadInputType2(self): | ||
"""Give the part cleaner another invalid input type.""" | ||
self.assertRaises(TypeError, _cleanPart, [1,"a"]) | ||
|
||
def testScalar(self): | ||
"""Test single scalar value.""" | ||
num = 1 | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float)) | ||
|
||
def testListScalar(self): | ||
"""Test single scalar value in list.""" | ||
num = [1] | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float)) | ||
|
||
def testTupleScalar(self): | ||
"""Test single scalar value in tuple.""" | ||
num = (1) | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float)) | ||
|
||
def testList(self): | ||
"""Test multiple values in a list.""" | ||
num = [1, 2] | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float)) | ||
|
||
def testTuple(self): | ||
"""Test multiple values in tuple.""" | ||
num = (1, 2) | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float)) | ||
|
||
def testAllScalarTypes(self): | ||
"""Test single scalar value for all valid data types.""" | ||
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]: | ||
num = dtype(1) | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float)) | ||
|
||
def testNpArray(self): | ||
"""Test multiple values in numpy array.""" | ||
num = np.array([1, 2]) | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float)) | ||
|
||
def testAllNumpyArrayTypes(self): | ||
"""Test scalar value in numpy array of ndim=0 for all data types.""" | ||
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]: | ||
num = np.array(1, dtype=dtype) | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float)) | ||
|
||
def testAllNumpyArrayTypes2(self): | ||
"""Test numpy array for all types.""" | ||
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]: | ||
num = np.array([1, 2], dtype=dtype) | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float)) | ||
|
||
def testListAllTypes(self): | ||
"""Test list of a single value for all data types.""" | ||
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]: | ||
num = [dtype(1)] | ||
num_ = _cleanPart(num) | ||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float)) | ||
|
||
def testListAllTypes2(self): | ||
"""List of list of numbers of all data types.""" | ||
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]: | ||
num = [dtype(1), dtype(2)] | ||
num_ = _cleanPart(num) | ||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float)) | ||
|
||
def testTupleAllTypes(self): | ||
"""Test tuple of a single value for all data types.""" | ||
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]: | ||
num = (dtype(1),) | ||
num_ = _cleanPart(num) | ||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float)) | ||
|
||
def testTupleAllTypes2(self): | ||
"""Test tuple of a single value for all data types.""" | ||
for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, float128]: | ||
num = (dtype(1), dtype(2)) | ||
num_ = _cleanPart(num) | ||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1, 2], dtype=float)) | ||
|
||
def testListListListInt(self): | ||
""" Test an int in a list of a list of a list.""" | ||
num = [[[1]]] | ||
num_ = _cleanPart(num) | ||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float)) | ||
|
||
def testListListListFloat(self): | ||
""" Test a float in a list of a list of a list.""" | ||
num = [[[1.0]]] | ||
num_ = _cleanPart(num) | ||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float)) | ||
|
||
def testListListListInts(self): | ||
"""Test 2 lists of ints in a list in a list.""" | ||
num = [[[1,1],[2,2]]] | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float)) | ||
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float)) | ||
|
||
def testListListListFloats(self): | ||
"""Test 2 lists of ints in a list in a list.""" | ||
num = [[[1.0,1.0],[2.0,2.0]]] | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float)) | ||
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float)) | ||
|
||
def testListListArray(self): | ||
"""List of list of numpy arrays for all valid types.""" | ||
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, float128: | ||
num = [[array([1,1], dtype=dtype),array([2,2], dtype=dtype)]] | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float)) | ||
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float)) | ||
|
||
def testTupleListArray(self): | ||
"""Tuple of list of numpy arrays for all valid types.""" | ||
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, float128: | ||
num = ([array([1,1], dtype=dtype),array([2,2], dtype=dtype)],) | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float)) | ||
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float)) | ||
|
||
def testListTupleArray(self): | ||
"""List of tuple of numpy array for all valid types.""" | ||
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, float128: | ||
num = [(array([1,1], dtype=dtype),array([2,2], dtype=dtype))] | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float)) | ||
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float)) | ||
|
||
def testTupleTuplesArrays(self): | ||
"""Tuple of tuples of numpy arrays for all valid types.""" | ||
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, float128: | ||
num = ((array([1,1], dtype=dtype),array([2,2], dtype=dtype)), | ||
(array([3,4], dtype=dtype),array([4,4], dtype=dtype))) | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float)) | ||
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float)) | ||
|
||
def testListTuplesArrays(self): | ||
"""List of tuples of numpy arrays for all valid types.""" | ||
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, float128: | ||
num = [(array([1,1], dtype=dtype),array([2,2], dtype=dtype)), | ||
(array([3,4], dtype=dtype),array([4,4], dtype=dtype))] | ||
num_ = _cleanPart(num) | ||
|
||
assert isinstance(num_, list) | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float)) | ||
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float)) | ||
|
||
def testListListArrays(self): | ||
"""List of list of numpy arrays for all valid types.""" | ||
for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, float128: | ||
num = [[array([1,1], dtype=dtype),array([2,2], dtype=dtype)], | ||
[array([3,3], dtype=dtype),array([4,4], dtype=dtype)]] | ||
num_ = _cleanPart(num) | ||
|
||
assert len(num_) == 2 | ||
assert np.all([isinstance(part, list) for part in num_]) | ||
assert np.all([len(part) == 2 for part in num_]) | ||
np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float)) | ||
np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float)) | ||
np.testing.assert_array_equal(num_[1][0], array([3.0, 3.0], dtype=float)) | ||
np.testing.assert_array_equal(num_[1][1], array([4.0, 4.0], dtype=float)) | ||
|
||
def suite(): | ||
return unittest.TestLoader().loadTestsFromTestCase(TestXferFcnInput) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
floats, not ints?