def test_vector_pass_through(self): model_value = BasicVector([1., 2, 3]) system = PassThrough(model_value.size()) context = system.CreateDefaultContext() context.FixInputPort(0, model_value) output = system.AllocateOutput(context) input_eval = system.EvalVectorInput(context, 0) compare_value(self, input_eval, model_value) system.CalcOutput(context, output) output_value = output.get_vector_data(0) compare_value(self, output_value, model_value)
def test_abstract_pass_through(self): model_value = AbstractValue.Make("Hello world") system = PassThrough(model_value) context = system.CreateDefaultContext() context.FixInputPort(0, model_value) output = system.AllocateOutput() input_eval = system.EvalAbstractInput(context, 0) compare_value(self, input_eval, model_value) system.CalcOutput(context, output) output_value = output.get_data(0) compare_value(self, output_value, model_value)
def test_abstract_input_port_eval(self): model_value = AbstractValue.Make("Hello World") system = PassThrough(copy.copy(model_value)) context = system.CreateDefaultContext() fixed = context.FixInputPort(0, copy.copy(model_value)) self.assertIsInstance(fixed.GetMutableData(), AbstractValue) input_port = system.get_input_port(0) value = input_port.Eval(context) self.assertEqual(type(value), type(model_value.get_value())) self.assertEqual(value, model_value.get_value()) value_abs = input_port.EvalAbstract(context) self.assertEqual(type(value_abs), type(model_value)) self.assertEqual(value_abs.get_value(), model_value.get_value())
def test_vector_input_port_fix(self): np_zeros = np.array([0.]) model_value = AbstractValue.Make(BasicVector(np_zeros)) system = PassThrough(len(np_zeros)) context = system.CreateDefaultContext() input_port = system.get_input_port(0) # Fix to a scalar. input_port.FixValue(context, 1.) value = input_port.Eval(context) self.assertEqual(type(value), np.ndarray) np.testing.assert_equal(value, np.array([1.])) # Fix to an ndarray. input_port.FixValue(context, np.array([2.])) value = input_port.Eval(context) self.assertEqual(type(value), np.ndarray) np.testing.assert_equal(value, np.array([2.])) # Fix to a BasicVector. input_port.FixValue(context, BasicVector([3.])) value = input_port.Eval(context) self.assertEqual(type(value), np.ndarray) np.testing.assert_equal(value, np.array([3.])) # Fix to a type-erased BasicVector. input_port.FixValue(context, AbstractValue.Make(BasicVector([4.]))) value = input_port.Eval(context) self.assertEqual(type(value), np.ndarray) np.testing.assert_equal(value, np.array([4.])) # Fix to wrong-sized vector. with self.assertRaises(RuntimeError): input_port.FixValue(context, np.array([0., 1.])) with self.assertRaises(RuntimeError): input_port.FixValue( context, AbstractValue.Make(BasicVector([0., 1.]))) # Fix to a non-vector. with self.assertRaises(TypeError): # A TypeError occurs when pybind Value.set_value cannot match any # overload for how to assign the argument into the erased storage. input_port.FixValue(context, "string") with self.assertRaises(RuntimeError): # A RuntimeError occurs when the Context detects that the # type-erased Value objects are incompatible. input_port.FixValue(context, AbstractValue.Make("string"))
def test_vector_input_port_eval(self): np_value = np.array([1., 2., 3.]) model_value = AbstractValue.Make(BasicVector(np_value)) system = PassThrough(len(np_value)) context = system.CreateDefaultContext() system.get_input_port(0).FixValue(context, np_value) input_port = system.get_input_port(0) value = input_port.Eval(context) self.assertEqual(type(value), np.ndarray) np.testing.assert_equal(value, np_value) value_abs = input_port.EvalAbstract(context) self.assertEqual(type(value_abs), type(model_value)) self.assertEqual(type(value_abs.get_value().get_value()), np.ndarray) np.testing.assert_equal(value_abs.get_value().get_value(), np_value) basic = input_port.EvalBasicVector(context) self.assertEqual(type(basic), BasicVector) self.assertEqual(type(basic.get_value()), np.ndarray) np.testing.assert_equal(basic.get_value(), np_value)
def test_abstract_input_port_fix_object(self): # The port type is py::object, not any specific C++ type. model_value = AbstractValue.Make(object()) system = PassThrough(copy.copy(model_value)) context = system.CreateDefaultContext() input_port = system.get_input_port(0) # Fix to a type-erased py::object. input_port.FixValue(context, AbstractValue.Make(object())) # Fix to an int. input_port.FixValue(context, 1) value = input_port.Eval(context) self.assertEqual(type(value), int) self.assertEqual(value, 1) # Fixing to an explicitly-typed Value instantiation is an error ... with self.assertRaises(RuntimeError): input_port.FixValue(context, AbstractValue.Make("string")) # ... but implicit typing works just fine. input_port.FixValue(context, "string") value = input_port.Eval(context) self.assertEqual(type(value), str) self.assertEqual(value, "string")
def test_default_vector_pass_through(self): model_value = [1., 2, 3] system = PassThrough(value=model_value) context = system.CreateDefaultContext() np.testing.assert_array_equal(model_value, system.get_output_port().Eval(context))