Skip to content

Commit

Permalink
Replace affine6p transformation with numpy solution (#579)
Browse files Browse the repository at this point in the history
* Replace affine6p transformation with numpy solution

* Remove affine6p dependency

* Update changelog and remove pip from conda-recipe
  • Loading branch information
chkim-usgs authored Nov 20, 2023
1 parent b9a28e6 commit cf11ad7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ release.
### Fixed
- Fixed LRO MiniRF drivers naif keywords focal to pixel and pixel to focal translations to be correct. [#569](https://github.com/DOI-USGS/ale/pull/569)

### Changed
- Removed the affine6p library and replaced affine6p's affine transformation with a numpy solution [#579](https://github.com/DOI-USGS/ale/pull/579)


## [0.9.1] - 2023-06-05

### Changed
Expand Down
19 changes: 13 additions & 6 deletions ale/drivers/lo_drivers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import spiceypy as spice
import numpy as np
import affine6p
from ale.base.data_naif import NaifSpice
from ale.base.label_isis import IsisLabel
from ale.base.type_sensor import Framer
Expand Down Expand Up @@ -156,17 +155,25 @@ def naif_keywords(self):
p_fidYCoords = self.label['IsisCube']['Instrument']['FiducialYCoordinates'].value

# Create Affine Transformation

p_src = [p_fidSamples, p_fidLines]
p_dst = [p_fidXCoords, p_fidYCoords]

# format the fiducial coordinatens as [ [x, y], [x, y]...]
# Format the fiducial coordinates as [ [x, y], [x, y]...]
p_src = np.rot90(np.array([p_fidSamples, p_fidLines]))
p_dst = np.rot90(np.array([p_fidXCoords, p_fidYCoords]))

# find a best match for the transformation based on source and destination coordinates
tr_mat = affine6p.estimate(p_src, p_dst).get_matrix()
# Pad data with ones so that the transformation allows translations
pad = lambda x: np.hstack([x, np.ones((x.shape[0], 1))])
X = pad(p_src)
Y = pad(p_dst)

# Solve the least squares problem X * A = Y to find our transformation matrix A
A, res, rank, s = np.linalg.lstsq(X, Y)

# Transpose matrix and convert to list
tr_mat = np.transpose(A).tolist()

# Compute inverse of transformation matrix
tr_mat_inv = np.linalg.inv(tr_mat)

# X and Y, Inverse S and L components of transformation
Expand All @@ -175,7 +182,7 @@ def naif_keywords(self):
itranss = tr_mat_inv[0]
itransl = tr_mat_inv[1]

# move the last item to the front to get the ordering standard in ISIS
# Move the last item to the front to get the ordering standard in ISIS
transx.insert(0, transx.pop())
transy.insert(0, transy.pop())
itranss = np.roll(itranss, 1).tolist()
Expand Down
3 changes: 0 additions & 3 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,3 @@ dependencies:
- pytest-cov
- networkx
- breathe
- pip
- pip:
- affine6p
1 change: 0 additions & 1 deletion recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ requirements:
- cmake>=3.15
- eigen
host:
- pip
- python
- nlohmann_json
run:
Expand Down
19 changes: 17 additions & 2 deletions tests/pytests/test_lo_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def test_focal2pixel_lines(self):
namfrm.assert_called_with("LO3_HIGH_RESOLUTION_CAMERA")

def test_naif_keywords(self):

with patch('ale.drivers.lo_drivers.LoHighCameraIsisLabelNaifSpiceDriver.ikid', new_callable=PropertyMock) as ikid, \
patch('ale.base.data_naif.spice.bodvrd', return_value=[1737.4, 1737.4, 1737.4]) as bodvrd:

Expand All @@ -114,6 +113,22 @@ def test_naif_keywords(self):
"INS-533001_ITRANSL" : [4541.692430539061, -0.05845617762411283, 143.95514969883214]
}

assert self.driver.naif_keywords == naif_keywords
assert self.driver.naif_keywords["BODY_CODE"] == naif_keywords["BODY_CODE"]
assert self.driver.naif_keywords["BODY301_RADII"] == naif_keywords["BODY301_RADII"]
assert self.driver.naif_keywords["BODY_FRAME_CODE"] == naif_keywords["BODY_FRAME_CODE"]

np.testing.assert_almost_equal(np.asarray(self.driver.naif_keywords["INS-533001_TRANSX"]),
np.asarray(naif_keywords["INS-533001_TRANSX"]),
decimal=10)
np.testing.assert_almost_equal(np.asarray(self.driver.naif_keywords["INS-533001_TRANSY"]),
np.asarray(naif_keywords["INS-533001_TRANSY"]),
decimal=10)
np.testing.assert_almost_equal(np.asarray(self.driver.naif_keywords["INS-533001_ITRANSS"]),
np.asarray(naif_keywords["INS-533001_ITRANSS"]),
decimal=10)
np.testing.assert_almost_equal(np.asarray(self.driver.naif_keywords["INS-533001_ITRANSL"]),
np.asarray(naif_keywords["INS-533001_ITRANSL"]),
decimal=10)

bodvrd.assert_called_with('Moon', 'RADII', 3)

0 comments on commit cf11ad7

Please sign in to comment.