From d802255eb3fb4302b408c6f8422d5cc4764d3565 Mon Sep 17 00:00:00 2001 From: Duy-Nguyen Ta Date: Tue, 16 Oct 2018 07:06:39 -0400 Subject: [PATCH] support pythonic print for gtsam objects. --- cython/gtsam/examples/OdometryExample.py | 6 +++--- cython/gtsam/examples/PlanarSLAMExample.py | 6 +++--- cython/gtsam/examples/Pose2SLAMExample.py | 6 +++--- cython/gtsam/examples/README.md | 4 ++-- wrap/Method.cpp | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cython/gtsam/examples/OdometryExample.py b/cython/gtsam/examples/OdometryExample.py index 0357aebb3..5d2190d56 100644 --- a/cython/gtsam/examples/OdometryExample.py +++ b/cython/gtsam/examples/OdometryExample.py @@ -35,7 +35,7 @@ odometry = gtsam.Pose2(2.0, 0.0, 0.0) # Create odometry (Between) factors between consecutive poses graph.add(gtsam.BetweenFactorPose2(1, 2, odometry, ODOMETRY_NOISE)) graph.add(gtsam.BetweenFactorPose2(2, 3, odometry, ODOMETRY_NOISE)) -graph.print_("\nFactor Graph:\n") +print("\nFactor Graph:\n{}".format(graph)) # Create the data structure to hold the initialEstimate estimate to the solution # For illustrative purposes, these have been deliberately set to incorrect values @@ -43,10 +43,10 @@ initial = gtsam.Values() initial.insert(1, gtsam.Pose2(0.5, 0.0, 0.2)) initial.insert(2, gtsam.Pose2(2.3, 0.1, -0.2)) initial.insert(3, gtsam.Pose2(4.1, 0.1, 0.1)) -initial.print_("\nInitial Estimate:\n") +print("\nInitial Estimate:\n{}".format(initial)) # optimize using Levenberg-Marquardt optimization params = gtsam.LevenbergMarquardtParams() optimizer = gtsam.LevenbergMarquardtOptimizer(graph, initial, params) result = optimizer.optimize() -result.print_("\nFinal Result:\n") +print("\nFinal Result:\n{}".format(result)) diff --git a/cython/gtsam/examples/PlanarSLAMExample.py b/cython/gtsam/examples/PlanarSLAMExample.py index 785faaea6..fb038d043 100644 --- a/cython/gtsam/examples/PlanarSLAMExample.py +++ b/cython/gtsam/examples/PlanarSLAMExample.py @@ -50,7 +50,7 @@ graph.add(gtsam.BearingRangeFactor2D( X3, L2, gtsam.Rot2.fromDegrees(90), 2.0, MEASUREMENT_NOISE)) # Print graph -graph.print_("Factor Graph:\n") +print("Factor Graph:\n{}".format(graph)) # Create (deliberately inaccurate) initial estimate initial_estimate = gtsam.Values() @@ -61,7 +61,7 @@ initial_estimate.insert(L1, gtsam.Point2(1.80, 2.10)) initial_estimate.insert(L2, gtsam.Point2(4.10, 1.80)) # Print -initial_estimate.print_("Initial Estimate:\n") +print("Initial Estimate:\n{}".format(initial_estimate)) # Optimize using Levenberg-Marquardt optimization. The optimizer # accepts an optional set of configuration parameters, controlling @@ -72,7 +72,7 @@ initial_estimate.print_("Initial Estimate:\n") params = gtsam.LevenbergMarquardtParams() optimizer = gtsam.LevenbergMarquardtOptimizer(graph, initial_estimate, params) result = optimizer.optimize() -result.print_("\nFinal Result:\n") +print("\nFinal Result:\n{}".format(result)) # Calculate and print marginal covariances for all variables marginals = gtsam.Marginals(graph, result) diff --git a/cython/gtsam/examples/Pose2SLAMExample.py b/cython/gtsam/examples/Pose2SLAMExample.py index a9bb9d74b..b15b90864 100644 --- a/cython/gtsam/examples/Pose2SLAMExample.py +++ b/cython/gtsam/examples/Pose2SLAMExample.py @@ -52,7 +52,7 @@ graph.add(gtsam.BetweenFactorPose2( # techniques with camera images. We will use another Between Factor to enforce this constraint: graph.add(gtsam.BetweenFactorPose2( 5, 2, gtsam.Pose2(2, 0, math.pi / 2), ODOMETRY_NOISE)) -graph.print_("\nFactor Graph:\n") # print +print("\nFactor Graph:\n{}".format(graph)) # print # 3. Create the data structure to hold the initial_estimate estimate to the # solution. For illustrative purposes, these have been deliberately set to incorrect values @@ -62,7 +62,7 @@ initial_estimate.insert(2, gtsam.Pose2(2.3, 0.1, -0.2)) initial_estimate.insert(3, gtsam.Pose2(4.1, 0.1, math.pi / 2)) initial_estimate.insert(4, gtsam.Pose2(4.0, 2.0, math.pi)) initial_estimate.insert(5, gtsam.Pose2(2.1, 2.1, -math.pi / 2)) -initial_estimate.print_("\nInitial Estimate:\n") # print +print("\nInitial Estimate:\n{}".format(initial_estimate)) # print # 4. Optimize the initial values using a Gauss-Newton nonlinear optimizer # The optimizer accepts an optional set of configuration parameters, @@ -79,7 +79,7 @@ parameters.setMaxIterations(100) optimizer = gtsam.GaussNewtonOptimizer(graph, initial_estimate, parameters) # ... and optimize result = optimizer.optimize() -result.print_("Final Result:\n") +print("Final Result:\n{}".format(result)) # 5. Calculate and print marginal covariances for all variables marginals = gtsam.Marginals(graph, result) diff --git a/cython/gtsam/examples/README.md b/cython/gtsam/examples/README.md index 4474da488..067929a20 100644 --- a/cython/gtsam/examples/README.md +++ b/cython/gtsam/examples/README.md @@ -1,4 +1,4 @@ These examples are almost identical to the old handwritten python wrapper -examples. However, there are just some slight name changes, for example .print -becomes .print_, and noiseModel.Diagonal becomes noiseModel_Diagonal etc... +examples. However, there are just some slight name changes, for example +noiseModel.Diagonal becomes noiseModel_Diagonal etc... Also, annoyingly, instead of gtsam.Symbol('b',0) we now need to say gtsam.symbol(ord('b'), 0)) diff --git a/wrap/Method.cpp b/wrap/Method.cpp index 2637275d1..1a58692b6 100644 --- a/wrap/Method.cpp +++ b/wrap/Method.cpp @@ -106,7 +106,7 @@ void Method::emit_cython_pyx_no_overload(FileWriter& file, // leverage python's special treatment for print if (funcName == "print_") { - file.oss << " def __str__(self):\n"; + file.oss << " def __repr__(self):\n"; file.oss << " strBuf = RedirectCout()\n"; file.oss << " self.print_('')\n"; file.oss << " return strBuf.str()\n";