Skip to content

Cleanup TransferFunction input sanitization. #134

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

Closed
wants to merge 7 commits into from
Closed

Cleanup TransferFunction input sanitization. #134

wants to merge 7 commits into from

Conversation

dapperfu
Copy link
Contributor

This started out to address #112 & #130, but I found a few other small edge cases.

To simplify unit testing I broke the input cleaning out to a separate function and added as many test cases as I could think of.

Deep copy

It performs a deep copy on args before anything else.

Test Code:

num = [[[1], [2]]]
den = [[[1,1], [2,1]]]
print(num)
print(den)
control.TransferFunction(num, den)
print(num)
print(den)

Before:

[[[1], [2]]]
[[[1, 1], [2, 1]]]
[[array([1]), array([2])]]
[[array([1, 1]), array([2, 1])]]

After:

[[[1], [2]]]
[[[1, 1], [2, 1]]]
[[[1], [2]]]
[[[1, 1], [2, 1]]]

Validate All Input Types:

The previous input validation would only check the first element if it was a valid input type. This means you could feed in extra bad values in other positions and it would accept it, only to fail later.

Test Code:

num = [[[1,"a"]]]
den = [[[2,1]]]
print(num)
print(den)
sys = control.TransferFunction(num, den)
print(num)
print(den)

Before:

It would accept the values (and mutate the inputs):

[[[1, 'a']]]
[[[2, 1]]]
[[array(['1', 'a'], 
      dtype='<U21')]]
[[array([2, 1])]]

But fail when you tried to access the system:

   1013 
   1014     for k in range(len(coeffs)):
-> 1015         coefstr = '%.4g' % abs(coeffs[k])
   1016         if coefstr[-4:] == '0000':
   1017             coefstr = coefstr[:-5]

TypeError: bad operand type for abs(): 'numpy.str_'

After

It fails immediately:

TypeError: The numerator and denominator inputs must be scalars or vectors (for
SISO), or lists of lists of vectors (for SISO or MIMO).

Added more valid types.

If you passed an numpy array as an input it would possibly fail depending on which architecture you were using it on. int resolves to int64 on 64-bit systems. It now accepts valid types of int, int8, int16, int32, int64, float, float16, float32, float64, float128. (If for some reason your transfer function parts were generated in another function).

Code:

import control
from numpy import array, int
num = array(1, dtype=int)
den = array([2, 1], dtype=int)
control.TransferFunction(num, den)

Before:

TypeError: The numerator and denominator inputs must be scalars or vectors (for
SISO), or lists of lists of vectors (for SISO or MIMO).

After:

    1
-------
2 s + 1

Added Tuple as valid collection for MIMO systems

It was easy enough to add and a valid ordered Python collection.

Given:

# Input 1 to Output 1
num11=[1]
den11=[2, 3]
# Input 1 to Output 2
num12=[4]
den12=[5, 6]
# Input 2 to Output 1
num21=[7]
den21=[8, 9]
# Input 2 to Output 2
num22=[10]
den22=[11, 12]

Previously this was the only valid MIMO input:

control.TransferFunction([[num11, num21],[num12, num22]], [[den11, den21],[den12, den22]])

Now all of these are also valid:

control.TransferFunction([(num11, num21),(num12, num22)], [(den11, den21),(den12, den22)])
control.TransferFunction(((num11, num21),(num12, num22)), ((den11, den21),(den12, den22)))
control.TransferFunction(([num11, num21],[num12, num22]), ([den11, den21],[den12, den22]))

Jed added 5 commits February 17, 2017 13:57
Added IO cleanup function to xferfcn module.
From .eggs/README.txt:

"This directory contains eggs that were downloaded by setuptools to build, test, and run plug-ins.

This directory caches those eggs to prevent repeated downloads.

However, it is safe to delete this directory."
…function.

Added unit tests to test the cleanup function.

Added deep copy to TransferFunction args.
@coveralls
Copy link

coveralls commented Feb 18, 2017

Coverage Status

Coverage increased (+0.08%) to 77.408% when pulling 509ff8e on jed-frey:master into 5ab74cf on python-control:master.

@coveralls
Copy link

coveralls commented Feb 18, 2017

Coverage Status

Coverage increased (+0.08%) to 77.408% when pulling 761bc2c on jed-frey:master into 5ab74cf on python-control:master.

@coveralls
Copy link

coveralls commented Feb 18, 2017

Coverage Status

Coverage increased (+0.08%) to 77.408% when pulling c373f54 on jed-frey:master into 5ab74cf on python-control:master.

@dapperfu dapperfu closed this Feb 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants