Simplistic converter of strings from repr to float NumPy arrays. If the repr representation has ellipsis in it, then this will fail. Parameters ---------- s : str The repr version of a NumPy array. Examples -------- >>> s = "array([ 0.3, inf, nan])"
(s)
| 49 | """ |
| 50 | |
| 51 | def str_to_array(s): |
| 52 | """ |
| 53 | Simplistic converter of strings from repr to float NumPy arrays. |
| 54 | |
| 55 | If the repr representation has ellipsis in it, then this will fail. |
| 56 | |
| 57 | Parameters |
| 58 | ---------- |
| 59 | s : str |
| 60 | The repr version of a NumPy array. |
| 61 | |
| 62 | Examples |
| 63 | -------- |
| 64 | >>> s = "array([ 0.3, inf, nan])" |
| 65 | >>> a = str_to_array(s) |
| 66 | |
| 67 | """ |
| 68 | import numpy as np |
| 69 | |
| 70 | # Need to make sure eval() knows about inf and nan. |
| 71 | # This also assumes default printoptions for NumPy. |
| 72 | from numpy import inf, nan |
| 73 | |
| 74 | if s.startswith(u'array'): |
| 75 | # Remove array( and ) |
| 76 | s = s[6:-1] |
| 77 | |
| 78 | if s.startswith(u'['): |
| 79 | a = np.array(eval(s), dtype=float) |
| 80 | else: |
| 81 | # Assume its a regular float. Force 1D so we can index into it. |
| 82 | a = np.atleast_1d(float(s)) |
| 83 | return a |
| 84 | |
| 85 | def float_doctest(sphinx_shell, args, input_lines, found, submitted): |
| 86 | """ |
no outgoing calls
no test coverage detected
searching dependent graphs…