Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uniform discritization of SO(3) via random rotation matrices #28

Merged
merged 16 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ dependencies:
- pytorch
- pip :
- git+https://github.com/compSPI/simSPI.git
- geomstats

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
from simSPI.transfer import eval_ctf
from geomstats.geometry import special_orthogonal


class IterativeRefinement:
Expand Down Expand Up @@ -290,7 +291,10 @@ def grid_SO3_uniform(n_rotations):
Array describing rotations.
Shape (n_rotations, 3, 3)
"""
rots = np.ones((n_rotations, 3, 3))
geom = special_orthogonal.SpecialOrthogonal(3, "matrix")
rots = geom.random_uniform(n_rotations)
negatives = np.tile(np.random.randint(2, size=n_rotations) * 2 - 1, (3, 3, 1)).T

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are negating every second rotation. A bit opaque in how the tiling works, so I would put a note in the docstring, or the variable name indicating how you are scaling each rotation by +1 or -1, with one random number per rotation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a note to the docstring. The reason for this is that geomstats only rotates on a half-sphere. So, I invert the rotations randomly in order to cover the entire sphere (I validated the functionality in a jupyter notebook and am happy to post images confirming this behaviour)

Copy link

@geoffwoollard geoffwoollard Apr 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I recall this. Great work! Put as much as you can of that in the docstring and tests so that it doesn't go to waste.

The test has to be better than checking for the shape. For sure orthonormality

  • orthogonality: each R.dot(R.T) is close np.eye(3)
  • normality: np.linalg.det(R) is close 1

And I think something for uniformity would be great. If you can come up with some heuristic, would be a nice start. Doesn't have to be rigorous, just protest against a bad fail case. Some ideas (that work with large # of rotations)

  • project to the three coordinates used to plot on sphere. Check roughly same amount above/below, through the three axes (up/down; left/right; front/back)
  • convert to some encoding (Euler angles???) and check stats on those encoding (I think two Euler angles (ZYZ or ZXZ) are uniform and one is not)
  • integrate with an even function (e.g. x2 + y2 + z2) that should be double the integration on half. If the function is odd (e.g. x3 + y3 + z3) it should integrate to roughly one.
    • even: (axis_3_vectors[half_idx]**2).sum() is close 2*axis_3_vectors.sum()
    • odd: (axis_3_vectors[half_idx]**3).sum() is close 0

Made issue here for ref: #39

rots[:] *= negatives
return rots

@staticmethod
Expand Down