refactor: consolidate numpy geometry helpers under unilab.utils#677
Closed
TATP-233 wants to merge 2 commits into
Closed
refactor: consolidate numpy geometry helpers under unilab.utils#677TATP-233 wants to merge 2 commits into
TATP-233 wants to merge 2 commits into
Conversation
Move the shared numeric helpers out of unilab.envs.common into unilab.utils
and extract per-env private geometry/coordinate helpers (tracking, stewart,
allegro, sharpa, go2_arm) into a new unilab.utils.geometry module. Each
extraction preserves the caller-visible dtype cast (e.g. get_global_dtype
for allegro rotation axis) so refactored code is bit-identical to the
pre-refactor implementation across random inputs.
Notable moves:
- envs/common/{rotation,math}.py -> utils/{rotation,math}.py, then math.py's
lone np_sample_uniform folded into utils/geometry.py so utils/ no longer
hosts a single-function module.
- utils/geometry.py picks up the frame-transform, roll/pitch, gravity-in-body,
quat-normalize, orientation-error, angvel-from-pair, uniform-quat-sample,
and sphere/cart conversions that were duplicated across envs.
- Dead helpers _normalize_quat and _orientation_error_local removed from
go2_arm/base.py; call sites now hit the shared util directly.
- All 30+ import sites (src/tests/scripts/docs/AGENTS.md) updated; utils
package whitelist and sphinx toctree adjusted.
Collaborator
Author
|
关闭本 PR:本次改动已直接合并进 合并过程中解决了两处冲突:
合并后 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
unilab.envs.common.{rotation,math}→unilab.utils.{rotation,math}so the shared numeric helpers live inutils/alongsidedevice,tensor,nan_guard. Delete the now-emptyunilab.envs.commonpackage.math.py's lonenp_sample_uniformintounilab.utils.geometrysoutils/no longer hosts a single-function module.motion_tracking/g1/tracking.py,manipulation/stewart/balance.py,manipulation/allegro_inhand/rotation.py,manipulation/sharpa_inhand/rotation.py,locomotion/go2_arm/base.py,locomotion/go2_arm/manip_loco.py) into a newunilab.utils.geometrymodule: quaternion normalize, roll/pitch, gravity-z-in-body, orientation-error-local, angular-velocity-from-pair, uniform quaternion sampling, spherical↔cartesian, and a fused frame-transform + 6D-flatten writer._normalize_quatand_orientation_error_localingo2_arm/base.pyno longer have callers after the util replaces them.src/,tests/,scripts/, sphinx docs,AGENTS.mdpointer, and the utils allowlist test.Numerical equivalence
Each replaced helper was probed against its pre-refactor implementation on random inputs (batched float32 quaternions/positions, various rotation axes, sphere↔cart samples). All 14 checked functions produce bit-identical output:
np_gravity_z_in_body_from_quatnp_roll_pitch_from_quat(+ stewart float32 wrapper)normalize_rotation_axis(allegro)compute_ball_angvel(allegro)sample_random_quaternion(sharpa)np_quat_orientation_error_local(go2_arm)_sphere2cart/_cart2sphere(go2_arm aliases)np_write_relative_anchor_transform_pos_rot6d(tracking, pos + rot6d)np_sample_uniform(moved)normalize_rotation_axisinitially drifted by 1 ULP (~6e-08) for non-unit input tuples becausenp_normalize_axiscomputes the norm as a Python float (float64). Fixed by pre-casting the input tuple toget_global_dtype()before delegating, so the norm and division happen at the training dtype exactly as before.Validation
make test-all→ 1410 passed, 27 skipped, 258 deselected (43.15s, coverage 73%).G1MotionTrackingEnv,StewartBalanceEnv,AllegroRotationPPO,SharpaInhandRotationEnv,Go2ArmBaseEnv,Go2ArmManipLocoEnv,G1BoxTrackingEnv).Test plan
make test-allpassessample_random_quaternionmonkeypatch test intests/test_sharpa.pystill works (import at module scope preserved)_cart2spheredirect import intests/envs/locomotion/go2_arm/test_manip_loco_contract.pystill works (aliased at import)unilab.utilsallowlist test updated withgeometry,rotation(and no longer requiresmath)