Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Apr 3, 2024
1 parent ce4a6fb commit 6ab0a7e
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 100 deletions.
134 changes: 81 additions & 53 deletions romancal/patch_match/patch_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@

import os
import os.path
import numpy as np

import asdf
import spherical_geometry.vector as sgv
import spherical_geometry.polygon as sgp
import gwcs.wcs as wcs
import numpy as np
import spherical_geometry.polygon as sgp
import spherical_geometry.vector as sgv
from matplotlib import pyplot as plt

RAD_TO_ARCSEC = 180. / np.pi * 3600.
RAD_TO_ARCSEC = 180.0 / np.pi * 3600.0

plt.ion()

PATCH_TABLE = None

print(os.environ)


def load_patch_table(tablepath=None):
"""
Load the patch table. If no tablepath is supplied the path is obtained
Expand All @@ -29,12 +31,12 @@ def load_patch_table(tablepath=None):
global PATCH_TABLE
if tablepath is None:
try:
tablepath = os.environ['PATCH_TABLE_PATH']
tablepath = os.environ["PATCH_TABLE_PATH"]
except KeyError:
raise KeyError("PATCH_TABLE_PATH environmental variable not found")
try:
with asdf.open(tablepath) as af:
PATCH_TABLE = af.tree['patches'].copy()
PATCH_TABLE = af.tree["patches"].copy()
except FileNotFoundError:
raise FileNotFoundError("Specified patch table file path not found")

Check warning on line 41 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L40-L41

Added lines #L40 - L41 were not covered by tests

Expand All @@ -55,6 +57,7 @@ def image_coords_to_vec(image_corners):
vec_im_corners = np.array(sgv.lonlat_to_vector(image_corners[0], image_corners[1]))
return vec_im_corners

Check warning on line 58 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L57-L58

Added lines #L57 - L58 were not covered by tests


def find_patch_matches(image_corners, image_shape=None):
"""Find patches that the image overlaps with
Expand All @@ -81,13 +84,16 @@ def find_patch_matches(image_corners, image_shape=None):
iwcs = image_corners

Check warning on line 84 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L83-L84

Added lines #L83 - L84 were not covered by tests
# Now must find size of correspinding image, with three possible
# sources of that information.
if ((not hasattr(iwcs, "bounding_box") or iwcs.bounding_box is None)
and (not hasattr(iwcs, "pixel_shape") or iwcs.pixel_shape is None)
and image_shape is None):
if (

Check warning on line 87 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L87

Added line #L87 was not covered by tests
(not hasattr(iwcs, "bounding_box") or iwcs.bounding_box is None)
and (not hasattr(iwcs, "pixel_shape") or iwcs.pixel_shape is None)
and image_shape is None
):
raise ValueError(

Check warning on line 92 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L92

Added line #L92 was not covered by tests
"Use of a wcs object requires at least one of the bounding_box"
" or pixel_shape attributes be set to the image shape or that the"
"image_shape argument be set")
"image_shape argument be set"
)
if image_shape is not None:
pass

Check warning on line 98 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L97-L98

Added lines #L97 - L98 were not covered by tests
else:
Expand All @@ -99,12 +105,14 @@ def find_patch_matches(image_corners, image_shape=None):
elif hasattr(iwcs, "pixel_shape") and iwcs.pixel_shape is not None:
image_shape = iwcs.pixel_shape

Check warning on line 106 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L104-L106

Added lines #L104 - L106 were not covered by tests
# Compute the image corners ra, dec from the wcs
(cxm, cxp), (cym, cyp) = ((-0.5, image_shape[1] - 0.5),
(-0.5, image_shape[0] - 0.5))
(cxm, cxp), (cym, cyp) = (

Check warning on line 108 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L108

Added line #L108 was not covered by tests
(-0.5, image_shape[1] - 0.5),
(-0.5, image_shape[0] - 0.5),
)
image_corners = (iwcs(cxp, cyp), iwcs(cxm, cyp), iwcs(cxm, cym), iwcs(cxp, cym))
ptab = PATCH_TABLE
ra = ptab[:]['ra_center']
dec = ptab[:]['dec_center']
ra = ptab[:]["ra_center"]
dec = ptab[:]["dec_center"]

Check warning on line 115 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L112-L115

Added lines #L112 - L115 were not covered by tests
# # Convert all celestial coordinates to cartesion coordinates.
vec_centers = np.array(sgv.lonlat_to_vector(ra, dec)).transpose()

Check warning on line 117 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L117

Added line #L117 was not covered by tests
# # Organize corners into two ra, dec lists
Expand All @@ -120,14 +128,14 @@ def find_patch_matches(image_corners, image_shape=None):
ncandidates = len(match[0])

Check warning on line 128 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L125-L128

Added lines #L125 - L128 were not covered by tests
# Now see which of these that are close actually overlap the supplied image.
# (Is it necessary to check that the corners are in a sensible order?)
mra1 = ptab[match]['ra_corn1']
mra2 = ptab[match]['ra_corn2']
mra3 = ptab[match]['ra_corn3']
mra4 = ptab[match]['ra_corn4']
mdec1 = ptab[match]['dec_corn1']
mdec2 = ptab[match]['dec_corn2']
mdec3 = ptab[match]['dec_corn3']
mdec4 = ptab[match]['dec_corn4']
mra1 = ptab[match]["ra_corn1"]
mra2 = ptab[match]["ra_corn2"]
mra3 = ptab[match]["ra_corn3"]
mra4 = ptab[match]["ra_corn4"]
mdec1 = ptab[match]["dec_corn1"]
mdec2 = ptab[match]["dec_corn2"]
mdec3 = ptab[match]["dec_corn3"]
mdec4 = ptab[match]["dec_corn4"]
mcenters = vec_centers[match]
mra = np.vstack([mra1, mra2, mra3, mra4, mra1])
mdec = np.vstack([mdec1, mdec2, mdec3, mdec4, mdec1])
Expand All @@ -148,26 +156,28 @@ def find_patch_matches(image_corners, image_shape=None):
realmatch.append(i)
return match[0][realmatch], match[0]

Check warning on line 157 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L156-L157

Added lines #L156 - L157 were not covered by tests


def get_corners(patch):
"""
Construct a the vertex coordinates for a patch from a patch definition suitable
for plotting the defined region (x coordinates, y coordinates).
"""
p = patch
corners = ((p['dec_corn1'],
p['dec_corn2'],
p['dec_corn3'],
p['dec_corn4'],
p['dec_corn1']),
(p['ra_corn1'],
p['ra_corn2'],
p['ra_corn3'],
p['ra_corn4'],
p['ra_corn1']))
corners = (

Check warning on line 166 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L165-L166

Added lines #L165 - L166 were not covered by tests
(
p["dec_corn1"],
p["dec_corn2"],
p["dec_corn3"],
p["dec_corn4"],
p["dec_corn1"],
),
(p["ra_corn1"], p["ra_corn2"], p["ra_corn3"], p["ra_corn4"], p["ra_corn1"]),
)
corners = np.array(corners)
vec_corners = sgv.lonlat_to_vector(corners[0], corners[1])
return vec_corners

Check warning on line 178 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L176-L178

Added lines #L176 - L178 were not covered by tests


def find_closest_tangent_point(patches, image_corners):
"""
Out of all listed patches, find the closest tangent point to the center
Expand All @@ -179,18 +189,22 @@ def find_closest_tangent_point(patches, image_corners):
vec_im_corners = image_coords_to_vec(image_corners)
im_center = np.array(normalize_vector(vec_im_corners.mean(axis=1)))
tangent_point_set = set()
patch_tangent_points = [(patch['dec_projection_center'],
patch['ra_projection_center'])
for patch in patches]
patch_tangent_points = [

Check warning on line 192 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L189-L192

Added lines #L189 - L192 were not covered by tests
(patch["dec_projection_center"], patch["ra_projection_center"])
for patch in patches
]
for tangent_point in patch_tangent_points:
tangent_point_set.add(tangent_point)
unique_tangent_points = list(tangent_point_set)

Check warning on line 198 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L196-L198

Added lines #L196 - L198 were not covered by tests
# Compute distance for each tangent point from im_center
dist = [((im_center - np.array(sgv.lonlat_to_vector(*tangent_point)))**2).sum()
for tangent_point in unique_tangent_points]
dist = [

Check warning on line 200 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L200

Added line #L200 was not covered by tests
((im_center - np.array(sgv.lonlat_to_vector(*tangent_point))) ** 2).sum()
for tangent_point in unique_tangent_points
]
sorted_dist_indices = sorted(zip(dist, range(len(dist))))
sorted_tangent_points = [unique_tangent_points[sorted_dist[1]]
for sorted_dist in sorted_dist_indices]
sorted_tangent_points = [

Check warning on line 205 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L204-L205

Added lines #L204 - L205 were not covered by tests
unique_tangent_points[sorted_dist[1]] for sorted_dist in sorted_dist_indices
]
closest_tangent_point = np.array(sgv.lonlat_to_vector(*sorted_tangent_points[0]))

Check warning on line 208 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L208

Added line #L208 was not covered by tests
# Now associate index of sorted_tangent_points with that of all patches
patch_tp_id = []
Expand All @@ -200,11 +214,12 @@ def find_closest_tangent_point(patches, image_corners):
patch_tp_id.append(i)
return closest_tangent_point, patch_tp_id

Check warning on line 215 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L210-L215

Added lines #L210 - L215 were not covered by tests


def normalize_vector(vec):
"""
Normalize a 3d vector to have length 1. Only works on 1d arrays.
"""
return vec/np.sqrt((vec**2).sum())
return vec / np.sqrt((vec**2).sum())

Check warning on line 222 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L222

Added line #L222 was not covered by tests


def veccoords_to_tangent_plane(vertices, tangent_point_vec):
Expand All @@ -217,47 +232,60 @@ def veccoords_to_tangent_plane(vertices, tangent_point_vec):
"""
# First compute the tangent plane axis vectors.
x_axis = normalize_vector(np.cross([0, 0, 1], tangent_point_vec))
y_axis = normalize_vector(np.array([0, 0, 1])
- np.array(tangent_point_vec) * np.dot(np.array([0, 0, 1]),
np.array(tangent_point_vec)))
y_axis = normalize_vector(

Check warning on line 235 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L234-L235

Added lines #L234 - L235 were not covered by tests
np.array([0, 0, 1])
- np.array(tangent_point_vec)
* np.dot(np.array([0, 0, 1]), np.array(tangent_point_vec))
)
avertices = np.vstack(vertices)
x_coords = np.dot(x_axis, avertices) * RAD_TO_ARCSEC
y_coords = np.dot(y_axis, avertices) * RAD_TO_ARCSEC
return x_coords, y_coords

Check warning on line 243 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L240-L243

Added lines #L240 - L243 were not covered by tests

def plot_field(corners, id='', fill=None, color=None):

def plot_field(corners, id="", fill=None, color=None):
plt.fill(corners[0], corners[1], color=fill, edgecolor=color)

Check warning on line 247 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L247

Added line #L247 was not covered by tests

def plot_patch(corners, id='', color=None):

def plot_patch(corners, id="", color=None):
plt.plot(corners[0], corners[1], color=color)
if id:
center = (corners[0][:-1].mean(), corners[1][:-1].mean())
plt.annotate(str(id), center, va='center', ha='center', size=10)
plt.annotate(str(id), center, va="center", ha="center", size=10)

Check warning on line 254 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L251-L254

Added lines #L251 - L254 were not covered by tests


def plot(image_corners, patches_touched_ids, patches_candidate_ids):
"""
This plots a list of patches
"""
plt.clf()
plt.gca().invert_xaxis()
plt.plot(0, 0, '*', markersize=10)
plt.plot(0, 0, "*", markersize=10)
patches_touched = [PATCH_TABLE[index] for index in patches_touched_ids]
patches_candidate = [PATCH_TABLE[index] for index in patches_candidate_ids]
tangent_point, patch_tp_id_touched = find_closest_tangent_point(

Check warning on line 266 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L261-L266

Added lines #L261 - L266 were not covered by tests
patches_touched, image_corners)
patches_touched, image_corners
)
ra, dec = sgv.vector_to_lonlat(*tangent_point)
dummy, patch_tp_id = find_closest_tangent_point(patches_candidate, image_corners)
vec_image_corners = image_coords_to_vec(image_corners)
tp_image_corners = veccoords_to_tangent_plane(vec_image_corners, tangent_point)
plot_field(tp_image_corners, fill='lightgrey', color='black')
plot_field(tp_image_corners, fill="lightgrey", color="black")
for patch, id in zip(patches_candidate, patches_candidate_ids):
plot_patch(veccoords_to_tangent_plane(get_corners(patch), tangent_point),
id=id, color='lightgray')
plot_patch(

Check warning on line 275 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L269-L275

Added lines #L269 - L275 were not covered by tests
veccoords_to_tangent_plane(get_corners(patch), tangent_point),
id=id,
color="lightgray",
)
for patch, id in zip(patches_touched, patches_touched_ids):
plot_patch(veccoords_to_tangent_plane(get_corners(patch), tangent_point),
id=id, color='blue')
plot_patch(

Check warning on line 281 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L280-L281

Added lines #L280 - L281 were not covered by tests
veccoords_to_tangent_plane(get_corners(patch), tangent_point),
id=id,
color="blue",
)
plt.xlabel("Offset from nearest tangent point in arcsec")
plt.ylabel("Offset from nearest tangent point in arcsec")
plt.title(f"RA: {ra} Dec: {dec} of tangent point in degrees")

Check warning on line 288 in romancal/patch_match/patch_match.py

View check run for this annotation

Codecov / codecov/patch

romancal/patch_match/patch_match.py#L286-L288

Added lines #L286 - L288 were not covered by tests


load_patch_table()
Binary file modified romancal/patch_match/test/patches_subset.asdf
Binary file not shown.
Loading

0 comments on commit 6ab0a7e

Please sign in to comment.