signal_base.py
Go to the documentation of this file.
1 # Copyright (C) 2020 CNRS
2 #
3 # Author: Joseph Mirabel
4 
5 from __future__ import print_function
6 
7 # I kept what follows for backward compatibility but I think it should be
8 # removed
9 import re
10 
11 from .wrap import SignalBase # noqa
12 from .wrap import create_signal_wrapper as SignalWrapper # noqa
13 
14 
15 def stringToTuple(vector):
16  """
17  Transform a string of format '[n](x_1,x_2,...,x_n)' into a tuple of numbers.
18  """
19  # Find vector length
20  a = re.match(r"\[(\d+)\]", vector)
21  size = int(a.group(1))
22  # remove '[n]' prefix
23  vector = vector[len(a.group(0)) :]
24  # remove '(' and ')' at beginning and end
25  vector = vector.lstrip("(").rstrip(")\n")
26  # split string by ','
27  vector = vector.split(",")
28  # check size
29  if len(vector) != size:
30  raise TypeError(
31  "displayed size "
32  + str(size)
33  + " of vector does not fit actual size: "
34  + str(len(vector))
35  )
36  res = map(float, vector)
37  return tuple(res)
38 
39 
40 def tupleToString(vector):
41  """
42  Transform a tuple of numbers into a string of format
43  '[n](x_1, x_2, ..., x_n)'
44  """
45  string = "[%d](" % len(vector)
46  for x in vector[:-1]:
47  string += "%f," % x
48  string += "%f)" % vector[-1]
49  return string
50 
51 
52 def stringToMatrix(string):
53  """
54  Transform a string of format
55  '[n,m]((x_11,x_12,...,x_1m),...,(x_n1,x_n2,...,x_nm))' into a tuple
56  of tuple of numbers.
57  """
58  # Find matrix size
59  a = re.search(r"\[(\d+),(\d+)]", string)
60  nRows = int(a.group(1))
61  nCols = int(a.group(2))
62  # Remove '[n,m]' prefix
63  string = string[len(a.group(0)) :]
64  rows = string.split("),(")
65  if len(rows) != nRows:
66  raise TypeError(
67  "displayed nb rows "
68  + nRows
69  + " of matrix does not fit actual nb rows: "
70  + str(len(rows))
71  )
72  m = []
73  for rstr in rows:
74  rstr = rstr.lstrip("(").rstrip(")\n")
75  r = map(float, rstr.split(","))
76  if len(r) != nCols:
77  raise TypeError(
78  "one row length "
79  + len(r)
80  + " of matrix does not fit displayed nb cols: "
81  + nCols
82  )
83  m.append(tuple(r))
84  return tuple(m)
85 
86 
87 def matrixToString(matrix):
88  """
89  Transform a tuple of tuple of numbers into a string of format
90  '[n,m]((x_11,x_12,...,x_1m),...,(x_n1,x_n2,...,x_nm))'.
91  """
92  nRows = len(matrix)
93  if nRows == 0:
94  return "[0,0](())"
95  nCols = len(matrix[0])
96  string = "[%d,%d](" % (nRows, nCols)
97  for r in range(nRows):
98  string += "("
99  for c in range(nCols):
100  string += str(float(matrix[r][c]))
101  if c != nCols - 1:
102  string += ","
103  string += ")"
104  if r != nRows - 1:
105  string += ","
106  string += ")"
107  return string
108 
109 
110 def objectToString(obj):
111  """
112  Transform an object to a string. Object is either
113  - an entity (more precisely a sub-class named Feature)
114  - a matrix
115  - a vector or
116  - a floating point number,
117  - an integer,
118  - a boolean,
119  """
120  if hasattr(obj, "__iter__"):
121  # matrix or vector
122  if len(obj) == 0:
123  return ""
124  else:
125  if hasattr(obj[0], "__iter__"):
126  # matrix
127  return matrixToString(obj)
128  else:
129  # vector
130  return tupleToString(obj)
131  elif hasattr(obj, "name"):
132  return obj.name
133  else:
134  return str(obj)
135 
136 
137 def stringToObject(string):
138  """
139  Convert a string into one of the following types
140  - a matrix (tuple of tuple),
141  - a vector,
142  - an integer,
143  - a floating point number.
144  Successively attempts conversion in the above order and return
145  on success. If no conversion fits, the string is returned.
146  """
147  if isinstance(string, float):
148  return string
149  if isinstance(string, int):
150  return string
151  if isinstance(string, tuple):
152  return string
153  try:
154  return stringToMatrix(string)
155  except Exception:
156  pass
157  try:
158  return stringToTuple(string)
159  except Exception:
160  pass
161  try:
162  return int(string)
163  except Exception:
164  pass
165  try:
166  return float(string)
167  except Exception:
168  return string
def matrixToString(matrix)
Definition: signal_base.py:87
def stringToMatrix(string)
Definition: signal_base.py:52


dynamic-graph-python
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:55:50