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

Added new QGRaphicsItems (FieldOfView, Centroid) #6

Merged
merged 21 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
300 changes: 299 additions & 1 deletion aperoll/star_field_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from PyQt5 import QtGui as QtG
from PyQt5 import QtWidgets as QtW

from aperoll import utils

__all__ = [
"Star",
"Catalog",
Expand All @@ -22,6 +24,9 @@
"GuideStar",
"AcqStar",
"MonBox",
"Centroid",
"CameraOutline",
"FieldOfView",
]


Expand Down Expand Up @@ -91,8 +96,20 @@ class Catalog(QtW.QGraphicsItem):
separately for a given attitude.
"""

def __init__(self, catalog, parent=None):
def __init__(self, catalog=None, parent=None):
super().__init__(parent)
self.reset(catalog)
self.setZValue(50)

def reset(self, catalog):
self.starcat = None
for item in self.childItems():
item.setParentItem(None)
item.hide()

if catalog is None:
return

self.starcat = catalog.copy() # will add some columns

cat = Table(self.starcat)
Expand Down Expand Up @@ -150,6 +167,39 @@ def __repr__(self):
return repr(self.starcat)


class Centroid(QtW.QGraphicsEllipseItem):
def __init__(self, imgnum, parent=None):
self.imgnum = imgnum
self.excluded = False
s = 18
w = 3
rect = QtC.QRectF(-s, -s, 2 * s, 2 * s)
super().__init__(rect, parent)
color = QtG.QColor("blue")
pen = QtG.QPen(color, w)
self.setPen(pen)

self._line_1 = QtW.QGraphicsLineItem(-s, 0, s, 0, self)
self._line_1.setPen(pen)
self._line_2 = QtW.QGraphicsLineItem(0, -s, 0, s, self)
self._line_2.setPen(pen)

self._label = QtW.QGraphicsTextItem(f"{imgnum}", self)
self._label.setFont(QtG.QFont("Arial", 30))
self._label.setDefaultTextColor(color)
self._label.setPos(30, -30)

def set_excluded(self, excluded):
self.excluded = excluded
pen = self.pen()
color = pen.color()
color.setAlpha(85 if excluded else 255)
pen.setColor(color)
self.setPen(pen)
self._line_1.setPen(pen)
self._line_2.setPen(pen)


class FidLight(QtW.QGraphicsEllipseItem):
def __init__(self, fid, parent=None):
self.starcat_row = fid
Expand Down Expand Up @@ -218,3 +268,251 @@ def __init__(self, star, parent=None):
super().__init__(-(hw * 2), -(hw * 2), hw * 4, hw * 4, parent)
self.setPen(QtG.QPen(QtG.QColor(255, 165, 0), w))
self.setPos(star["row"], -star["col"])


class FieldOfView(QtW.QGraphicsItem):
def __init__(self, attitude=None, alternate_outline=False):
super().__init__()
self.camera_outline = None
self.alternate_outline = alternate_outline
self.attitude = attitude
self.centroids = [Centroid(i, parent=self) for i in range(8)]
self._centroids = np.array(
[(0, 511, 511, 511, 511, 14, 0, 0, 0, 0) for _ in self.centroids],
dtype=[
("IMGNUM", int),
("IMGROW0_8X8", float),
("IMGCOL0_8X8", float),
("IMGROW0", int),
("IMGCOL0", int),
("AOACMAG", float),
("YAGS", float),
("ZAGS", float),
("IMGFID", bool),
("excluded", bool),
],
)
self._centroids["IMGNUM"] = np.arange(8)
self.show_centroids = True
self.setZValue(80)

def get_attitude(self):
return self._attitude

def set_attitude(self, attitude):
if hasattr(self, "_attitude") and attitude == self._attitude:
return
self._attitude = attitude
if self.camera_outline is None:
self.camera_outline = CameraOutline(
attitude, parent=self, simple=self.alternate_outline
)
else:
self.camera_outline.attitude = attitude
if self.scene() is not None and self.scene().attitude is not None:
self.set_pos_for_attitude(self.scene().attitude)

attitude = property(get_attitude, set_attitude)

def set_pos_for_attitude(self, attitude):
if self.camera_outline is not None:
self.camera_outline.set_pos_for_attitude(attitude)
self._set_centroid_pos_for_attitude(attitude)

def _set_centroid_pos_for_attitude(self, attitude):
yag, zag = self._centroids["YAGS"], self._centroids["ZAGS"]
row0, col0 = yagzag_to_pixels(yag, zag, allow_bad=True)
if attitude != self.attitude:
# the first attitude is the attitude of this frame,
# so we first get the ra/dec pointed to by the centroids
# and then calculate the position in the scene coordinate system
# for those ra/dec values
ra, dec = yagzag_to_radec(yag, zag, self.attitude)
yag, zag = radec_to_yagzag(ra, dec, attitude)
row, col = yagzag_to_pixels(yag, zag, allow_bad=True)

off_ccd = (
(row0 < -511)
| (row0 > 511)
| (col0 < -511)
| (col0 > 511)
| (self._centroids["IMGFID"])
)
for i, centroid in enumerate(self.centroids):
if off_ccd[i]:
centroid.setVisible(False)
else:
centroid.setVisible(self._centroids_visible)
centroid.setPos(row[i], -col[i])

def boundingRect(self):
return QtC.QRectF(0, 0, 1, 1)

def paint(self, _painter, _option, _widget):
# this item draws nothing, it just holds children
pass

def set_centroids(self, centroids):
missing_cols = set(centroids.dtype.names) - set(self._centroids.dtype.names)
if missing_cols:
raise ValueError(f"Missing columns in centroids: {missing_cols}")

cols = list(centroids.dtype.names)
for col in cols:
self._centroids[col] = centroids[col]
self._set_centroid_pos_for_attitude(self.scene().attitude)

def set_show_centroids(self, show=True):
self._centroids_visible = show
row, col = yagzag_to_pixels(
self._centroids["YAGS"], self._centroids["ZAGS"], allow_bad=True
)
off_ccd = (
(row < -511)
| (row > 511)
| (col < -511)
| (col > 511)
| (self._centroids["IMGFID"])
Copy link

Choose a reason for hiding this comment

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

Why are the fid light centroids excluded here? They are a bit less interesting, but if we're just trying to display where on the CCD stuff is happening it might make sense to just include them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, I will add them back. I will use the same symbol but in red.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jeanconn I just pushed this change in this branch (it was in a branch in the other PR). I am going to merge this PR. I have used this extensively and found no issues, and it is getting harder to keep track of things.

Choose a reason for hiding this comment

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

If you want me to review again let me know.

)
for i, centroid in enumerate(self.centroids):
if off_ccd[i]:
centroid.setVisible(False)
else:
centroid.setVisible(show)

def get_show_centroids(self):
return self._centroids_visible

show_centroids = property(get_show_centroids, set_show_centroids)

def get_centroid_table(self):
self._centroids["MAG_ACA"] = [
(centroid.excluded or not centroid.isVisible())
for centroid in self.centroids
]

table = Table()
table["IMGNUM"] = self._centroids["IMGNUM"]
table["MAG_ACA"] = self._centroids["MAG_ACA"]
table["excluded"] = self._centroids["MAG_ACA"]
table["YAG"] = self._centroids["YAGS"]
table["ZAG"] = self._centroids["ZAGS"]

return table


class CameraOutline(QtW.QGraphicsItem):
def __init__(self, attitude, parent=None, simple=False):
super().__init__(parent)
self.simple = simple
self._frame = utils.get_camera_fov_frame()
self.attitude = attitude
self.setZValue(100)

def boundingRect(self):
# roughly half of the CCD size in arcsec (including some margin)
w = 2650
return QtC.QRectF(-w, -w, 2 * w, 2 * w)

def paint(self, painter, _option, _widget):
color = "lightGray" if self.simple else "black"
pen = QtG.QPen(QtG.QColor(color))
pen.setWidth(1)
pen.setCosmetic(True)

# I want to use antialising for these lines regardless of what is set for the scene,
# because they are large and otherwise look hideous. It will be reset at the end.
anti_aliasing_set = painter.testRenderHint(QtG.QPainter.Antialiasing)
painter.setRenderHint(QtG.QPainter.Antialiasing, True)

painter.setPen(pen)
for i in range(len(self._frame["edge_1"]["row"]) - 1):
painter.drawLine(
QtC.QPointF(
self._frame["edge_1"]["row"][i], -self._frame["edge_1"]["col"][i]
),
QtC.QPointF(
self._frame["edge_1"]["row"][i + 1],
-self._frame["edge_1"]["col"][i + 1],
),
)
if self.simple:
painter.setRenderHint(QtG.QPainter.Antialiasing, anti_aliasing_set)
return

for i in range(len(self._frame["edge_2"]["row"]) - 1):
painter.drawLine(
QtC.QPointF(
self._frame["edge_2"]["row"][i], -self._frame["edge_2"]["col"][i]
),
QtC.QPointF(
self._frame["edge_2"]["row"][i + 1],
-self._frame["edge_2"]["col"][i + 1],
),
)

for i in range(len(self._frame["cross_2"]["row"]) - 1):
painter.drawLine(
QtC.QPointF(
self._frame["cross_2"]["row"][i], -self._frame["cross_2"]["col"][i]
),
QtC.QPointF(
self._frame["cross_2"]["row"][i + 1],
-self._frame["cross_2"]["col"][i + 1],
),
)
for i in range(len(self._frame["cross_1"]["row"]) - 1):
painter.drawLine(
QtC.QPointF(
self._frame["cross_1"]["row"][i], -self._frame["cross_1"]["col"][i]
),
QtC.QPointF(
self._frame["cross_1"]["row"][i + 1],
-self._frame["cross_1"]["col"][i + 1],
),
)

painter.setRenderHint(QtG.QPainter.Antialiasing, anti_aliasing_set)

def set_pos_for_attitude(self, attitude):
"""
Set the item position given the scene attitude.

Note that the given attitude is NOT the attitude of the camera corresponding to this frame.
It's the origin of the scene coordinate system.
"""
if self._attitude is None:
raise Exception("FieldOfView attitude is not set. Can't set position.")
for key in self._frame:
# self._frame[key]["yag"] must not be modified
yag2, zag2 = radec_to_yagzag(
self._frame[key]["ra"], self._frame[key]["dec"], attitude
)
self._frame[key]["row"], self._frame[key]["col"] = yagzag_to_pixels(
yag2, zag2, allow_bad=True
)
self.update()

def set_attitude(self, attitude):
"""
Set the attitude of the camera corresponding to this frame.

Note that this is not the attitude of the scene coordinate system.
"""
if hasattr(self, "_attitude") and attitude == self._attitude:
return
self._attitude = attitude
if self._attitude is None:
for key in self._frame:
self._frame[key]["ra"] = None
self._frame[key]["dec"] = None
else:
for key in self._frame:
self._frame[key]["ra"], self._frame[key]["dec"] = yagzag_to_radec(
self._frame[key]["yag"], self._frame[key]["zag"], self._attitude
)

def get_attitude(self):
return self._attitude

attitude = property(get_attitude, set_attitude)
5 changes: 4 additions & 1 deletion aperoll/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import numpy as np
from chandra_aca.transform import pixels_to_yagzag, yagzag_to_pixels
from chandra_aca.transform import (
pixels_to_yagzag,
yagzag_to_pixels,
)
from ska_helpers import logging

logger = logging.basic_logger("aperoll")
Expand Down
9 changes: 9 additions & 0 deletions aperoll/widgets/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def __init__(self, opts=None): # noqa: PLR0915
# self.plot.exclude_star.connect(self.parameters.exclude_star)

self.parameters.do_it.connect(self._run_proseco)
self.plot.update_proseco.connect(self._run_proseco)
self.parameters.run_sparkles.connect(self._run_sparkles)
self.parameters.reset.connect(self._reset)
self.parameters.draw_test.connect(self._draw_test)
Expand Down Expand Up @@ -181,6 +182,11 @@ def __init__(self, opts=None): # noqa: PLR0915
if starcat is not None:
self.plot.set_catalog(starcat)
self.starcat_view.set_catalog(aca)
# make sure the catalog is not overwritten automatically
self.plot.scene.state.auto_proseco = False

if self.plot.scene.state.auto_proseco:
self._run_proseco()

def closeEvent(self, event):
if self.web_page is not None:
Expand All @@ -192,6 +198,8 @@ def _parameters_changed(self):
proseco_args = self.parameters.proseco_args()
self.plot.set_base_attitude(proseco_args["att"])
self._data.reset(proseco_args)
if self.plot.scene.state.auto_proseco and not self.plot.view.moving:
self._run_proseco()

def _init(self):
if self.parameters.values:
Expand All @@ -203,6 +211,7 @@ def _init(self):
)
self.plot.set_base_attitude(aca_attitude)
self.plot.set_time(time)
self.plot.scene.state = "Proseco"

def _reset(self):
self.parameters.set_parameters(**self.opts)
Expand Down
Loading