Skip to content

Commit

Permalink
added the ability to visualize and modify polygon ROIs for XES flying
Browse files Browse the repository at this point in the history
  • Loading branch information
dleshchev authored and estavitski committed Jul 1, 2024
1 parent 903b92b commit 8d1ecf0
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 21 deletions.
37 changes: 25 additions & 12 deletions pilatus_tools/ui/ui_pilatus.ui
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@
<widget class="QWidget" name="gridLayoutWidget_2">
<property name="geometry">
<rect>
<x>620</x>
<y>430</y>
<x>610</x>
<y>470</y>
<width>381</width>
<height>66</height>
</rect>
Expand Down Expand Up @@ -730,8 +730,8 @@
<widget class="QGroupBox" name="groupBox_2">
<property name="geometry">
<rect>
<x>610</x>
<y>550</y>
<x>600</x>
<y>590</y>
<width>531</width>
<height>151</height>
</rect>
Expand Down Expand Up @@ -869,8 +869,8 @@
<widget class="QCheckBox" name="checkBox_auto_scale">
<property name="geometry">
<rect>
<x>620</x>
<y>820</y>
<x>610</x>
<y>860</y>
<width>171</width>
<height>26</height>
</rect>
Expand All @@ -882,8 +882,8 @@
<widget class="QCheckBox" name="checkBox_detector_settings">
<property name="geometry">
<rect>
<x>620</x>
<y>510</y>
<x>610</x>
<y>550</y>
<width>131</width>
<height>26</height>
</rect>
Expand All @@ -895,8 +895,8 @@
<widget class="QWidget" name="gridLayoutWidget_5">
<property name="geometry">
<rect>
<x>620</x>
<y>720</y>
<x>610</x>
<y>760</y>
<width>161</width>
<height>80</height>
</rect>
Expand Down Expand Up @@ -967,8 +967,8 @@
<widget class="QWidget" name="verticalLayoutWidget_3">
<property name="geometry">
<rect>
<x>800</x>
<y>720</y>
<x>790</x>
<y>760</y>
<width>341</width>
<height>82</height>
</rect>
Expand Down Expand Up @@ -1047,6 +1047,19 @@
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QCheckBox" name="checkBox_show_polygon_rois">
<property name="geometry">
<rect>
<x>630</x>
<y>430</y>
<width>211</width>
<height>26</height>
</rect>
</property>
<property name="text">
<string>Show Polygon ROIs</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
Expand Down
83 changes: 74 additions & 9 deletions pilatus_tools/widgets/widget_pilatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
from isstools.dialogs.BasicDialogs import message_box
from isstools.elements.widget_motors import UIWidgetMotors
from functools import partial
import json
from time import sleep
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
import matplotlib.patches as patches
import time as ttime
import numpy as np

import pyqtgraph as pg
pg.setConfigOption('leftButtonPan', False)
Expand Down Expand Up @@ -46,6 +48,10 @@ def __init__(self,
self.hhm = hhm
self.cur_mouse_coords = None

self.polygon_roi_path = f'/nsls2/data/iss/legacy/xf08id/settings/json/pilatus_polygon_roi.json'
with open(self.polygon_roi_path, 'r') as f:
self.pilatus_polygon_roi = json.loads(f.read())

self.pilatus100k_dict = self.detector_dict['Pilatus 100k']
self.pilatus100k_device = self.detector_dict['Pilatus 100k']['device']
# self.addCanvas()
Expand Down Expand Up @@ -112,13 +118,6 @@ def __init__(self,
self.label_y.setFont(QFont('Arial', 16))
# self.label_x.








self._patches = {}

self.lineEdit_min.returnPressed.connect(self.update_min_range)
Expand Down Expand Up @@ -189,7 +188,7 @@ def __init__(self,
indx = str(i)
self.roi_boxes[indx].sigRegionChangeFinished.connect(partial(self.read_new_pos_n_size, indx))


self.checkBox_show_polygon_rois.toggled.connect(self.add_polygon_rois)

def create_plot_widget(self):
self.window = pg.GraphicsLayoutWidget()
Expand All @@ -208,6 +207,7 @@ def set_n_add_image_properties(self):
self.plot.addItem(self.image)
self.set_n_add_roi_properties()


def set_n_add_roi_properties(self):
self.colors = {'1': 'red',
'2': 'cyan',
Expand All @@ -225,10 +225,42 @@ def set_n_add_roi_properties(self):
self.roi_boxes[indx].addScaleHandle([0.5, 0], [0.5, 1])
self.roi_boxes[indx].addScaleHandle([0, 0.5], [1, 0.5])

def plot_this(self):

self.colors_polygon = {'main': '#1f77b4', # 'tab:blue',
'aux2': '#ff7f0e', # 'tab:orange',
'aux3': '#2ca02c', # 'tab:green',
'aux4': '#d62728', # 'tab:red',
'aux5': '#9467bd', } # 'tab:purple'
self.gui_polygon_roi = {}


for crystal, poly in self.pilatus_polygon_roi.items():
poly_T = [[j, i] for i, j in poly]
polygon_obj = pg.PolyLineROI(poly_T, closed=True,
pen=pg.mkPen(self.colors_polygon[crystal], width=5))
polygon_obj.sigRegionChangeFinished.connect(self.save_polygon_roi_coords)
self.gui_polygon_roi[crystal] = polygon_obj

self.get_polygon_roi_labels()

def get_polygon_roi_labels(self):
self.gui_polygon_label = {}
for crystal, polygon_obj in self.gui_polygon_roi.items():
font = QFont()
font.setBold(True)
font.setPointSize(16)
label = pg.TextItem(crystal, color=self.colors_polygon[crystal], fill='w', anchor=(0.5, 0.5))
label.setFont(font)
self.gui_polygon_label[crystal] = label
self.set_polygon_roi_label_positions()

def set_polygon_roi_label_positions(self, dy=25):
for crystal, polygon_obj in self.gui_polygon_roi.items():
_x, _y, _dx, _dy = polygon_obj.boundingRect().getRect()
self.gui_polygon_label[crystal].setPos(_x + _dx/2, _y + _dy + dy)

def plot_this(self):

_img = self.pilatus100k_device.image.array_data.get()
_img = _img.reshape(195,487) #[:, ::-1]

Expand Down Expand Up @@ -266,6 +298,39 @@ def read_new_pos_n_size(self, roi_indx):

# self.pilatus100k_device.cam.acquire.subscribe(self.update_image_widget)

def add_polygon_rois(self, checked_state):
print(checked_state)
for crystal, obj in self.gui_polygon_roi.items():
if checked_state:
self.plot.addItem(obj)
else:
self.plot.removeItem(obj)

for crystal, obj in self.gui_polygon_label.items():
if checked_state:
self.plot.addItem(obj)
else:
self.plot.removeItem(obj)


@property
def gui_polygon_roi_coords(self):
output = {}
for crystal, obj in self.gui_polygon_roi.items():
points = []
for handle in obj.getHandles():
y, x = handle.pos()
points.append([x, y])
output[crystal] = points
return output


def save_polygon_roi_coords(self):
print('POLYGON ROI COORDINATES HAVE BEEN UPDATED. NOW STORING THEM ON DISC.')
self.set_polygon_roi_label_positions()
with open(self.polygon_roi_path, 'w') as f:
json.dump(self.gui_polygon_roi_coords, f)

def update_counts_n_energy(self):
try:

Expand Down

0 comments on commit 8d1ecf0

Please sign in to comment.