Skip to content

Commit 0732ffa

Browse files
authored
Merge pull request #28 from sezelt/config
Visual updates and bugfixes
2 parents 980ac37 + 1d87955 commit 0732ffa

File tree

5 files changed

+85
-34
lines changed

5 files changed

+85
-34
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "py4D_browser"
7-
version = "1.2.0"
7+
version = "1.2.1"
88
authors = [
99
{ name="Steven Zeltmann", email="[email protected]" },
1010
]

src/py4D_browser/main_window.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ def setup_menus(self):
397397
detector_mode_group.addAction(detector_CoM)
398398
self.detector_menu.addAction(detector_CoM)
399399

400+
detector_CoMx = QAction("CoM &X", self)
401+
detector_CoMx.setCheckable(True)
402+
detector_CoMx.triggered.connect(partial(self.update_real_space_view, True))
403+
detector_mode_group.addAction(detector_CoMx)
404+
self.detector_menu.addAction(detector_CoMx)
405+
406+
detector_CoMy = QAction("CoM &Y", self)
407+
detector_CoMy.setCheckable(True)
408+
detector_CoMy.triggered.connect(partial(self.update_real_space_view, True))
409+
detector_mode_group.addAction(detector_CoMy)
410+
self.detector_menu.addAction(detector_CoMy)
411+
400412
detector_iCoM = QAction("i&CoM", self)
401413
detector_iCoM.setCheckable(True)
402414
detector_iCoM.triggered.connect(partial(self.update_real_space_view, True))
@@ -552,12 +564,7 @@ def setup_views(self):
552564
self.diffraction_space_widget.setMouseTracking(True)
553565

554566
# Create virtual detector ROI selector
555-
self.virtual_detector_point = pg_point_roi(
556-
self.diffraction_space_widget.getView()
557-
)
558-
self.virtual_detector_point.sigRegionChanged.connect(
559-
partial(self.update_real_space_view, False)
560-
)
567+
self.update_diffraction_detector()
561568

562569
# Scalebar
563570
self.diffraction_scale_bar = ScaleBar(pixel_size=1, units="px", width=10)
@@ -574,10 +581,7 @@ def setup_views(self):
574581
self.real_space_widget.setImage(np.zeros((512, 512)))
575582

576583
# Add point selector connected to displayed diffraction pattern
577-
self.real_space_point_selector = pg_point_roi(self.real_space_widget.getView())
578-
self.real_space_point_selector.sigRegionChanged.connect(
579-
partial(self.update_diffraction_space_view, False)
580-
)
584+
self.update_realspace_detector()
581585

582586
# Scalebar, None by default
583587
self.real_space_scale_bar = ScaleBar(pixel_size=1, units="px", width=10)

src/py4D_browser/menu_actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def export_datacube(self, save_format: str):
196196
py4DSTEM.save(filename, self.datacube, mode="o")
197197

198198
elif save_format == "Plain HDF5":
199-
with h5py.File(filename, "o") as f:
199+
with h5py.File(filename, "w") as f:
200200
f["array"] = self.datacube.data
201201

202202

src/py4D_browser/update_views.py

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ def update_real_space_view(self, reset=False):
2929
"Integrating",
3030
"Maximum",
3131
"CoM",
32+
"CoM X",
33+
"CoM Y",
3234
"iCoM",
3335
], detector_mode
3436

@@ -50,7 +52,8 @@ def update_real_space_view(self, reset=False):
5052
if detector_shape == "Rectangular":
5153
# Get slices corresponding to ROI
5254
slices, transforms = self.virtual_detector_roi.getArraySlice(
53-
self.datacube.data[0, 0, :, :], self.diffraction_space_widget.getImageItem()
55+
self.datacube.data[0, 0, :, :].T,
56+
self.diffraction_space_widget.getImageItem(),
5457
)
5558
slice_y, slice_x = slices
5659

@@ -156,6 +159,10 @@ def update_real_space_view(self, reset=False):
156159

157160
if detector_mode == "CoM":
158161
vimg = CoMx + 1.0j * CoMy
162+
elif detector_mode == "CoM X":
163+
vimg = CoMx
164+
elif detector_mode == "CoM Y":
165+
vimg = CoMy
159166
elif detector_mode == "iCoM":
160167
dpc = py4DSTEM.process.phase.DPC(verbose=False)
161168
dpc.preprocess(
@@ -313,7 +320,7 @@ def update_diffraction_space_view(self, reset=False):
313320
elif detector_shape == "Rectangular":
314321
# Get slices corresponding to ROI
315322
slices, _ = self.real_space_rect_selector.getArraySlice(
316-
np.zeros((self.datacube.Rshape)), self.real_space_widget.getImageItem()
323+
np.zeros((self.datacube.Rshape)).T, self.real_space_widget.getImageItem()
317324
)
318325
slice_y, slice_x = slices
319326

@@ -401,12 +408,18 @@ def update_realspace_detector(self):
401408
)
402409
assert detector_shape in ["Point", "Rectangular"], detector_shape
403410

404-
if self.datacube is None:
405-
return
411+
main_pen = {"color": "g", "width": 6}
412+
handle_pen = {"color": "r", "width": 9}
413+
hover_pen = {"color": "c", "width": 6}
414+
hover_handle = {"color": "c", "width": 9}
406415

407-
x, y = self.datacube.data.shape[:2]
408-
x0, y0 = x // 2, y // 2
409-
xr, yr = x / 10, y / 10
416+
if self.datacube is None:
417+
x0, y0 = 0, 0
418+
xr, yr = 4, 4
419+
else:
420+
x, y = self.datacube.data.shape[2:]
421+
y0, x0 = x // 2, y // 2
422+
xr, yr = (np.minimum(x, y) / 10,) * 2
410423

411424
# Remove existing detector
412425
if hasattr(self, "real_space_point_selector"):
@@ -416,19 +429,27 @@ def update_realspace_detector(self):
416429
self.real_space_widget.view.scene().removeItem(self.real_space_rect_selector)
417430
self.real_space_rect_selector = None
418431

419-
# Rectangular detector
432+
# Point detector
420433
if detector_shape == "Point":
421434
self.real_space_point_selector = pg_point_roi(
422435
self.real_space_widget.getView(),
423436
center=(x0 - 0.5, y0 - 0.5),
437+
pen=main_pen,
438+
hoverPen=hover_pen,
424439
)
425440
self.real_space_point_selector.sigRegionChanged.connect(
426441
partial(self.update_diffraction_space_view, False)
427442
)
428443

444+
# Rectangular detector
429445
elif detector_shape == "Rectangular":
430446
self.real_space_rect_selector = pg.RectROI(
431-
[int(x0 - xr / 2), int(y0 - yr / 2)], [int(xr), int(yr)], pen=(3, 9)
447+
[int(x0 - xr / 2), int(y0 - yr / 2)],
448+
[int(xr), int(yr)],
449+
pen=main_pen,
450+
handlePen=handle_pen,
451+
hoverPen=hover_pen,
452+
handleHoverPen=hover_handle,
432453
)
433454
self.real_space_widget.getView().addItem(self.real_space_rect_selector)
434455
self.real_space_rect_selector.sigRegionChangeFinished.connect(
@@ -447,12 +468,18 @@ def update_diffraction_detector(self):
447468
detector_shape = self.detector_shape_group.checkedAction().text().strip("&")
448469
assert detector_shape in ["Point", "Rectangular", "Circle", "Annulus"]
449470

450-
if self.datacube is None:
451-
return
471+
main_pen = {"color": "g", "width": 6}
472+
handle_pen = {"color": "r", "width": 9}
473+
hover_pen = {"color": "c", "width": 6}
474+
hover_handle = {"color": "c", "width": 9}
452475

453-
x, y = self.datacube.data.shape[2:]
454-
x0, y0 = x // 2, y // 2
455-
xr, yr = x / 10, y / 10
476+
if self.datacube is None:
477+
x0, y0 = 0, 0
478+
xr, yr = 4, 4
479+
else:
480+
x, y = self.datacube.data.shape[2:]
481+
y0, x0 = x // 2, y // 2
482+
xr, yr = (np.minimum(x, y) / 10,) * 2
456483

457484
# Remove existing detector
458485
if hasattr(self, "virtual_detector_point"):
@@ -479,6 +506,8 @@ def update_diffraction_detector(self):
479506
self.virtual_detector_point = pg_point_roi(
480507
self.diffraction_space_widget.getView(),
481508
center=(x0 - 0.5, y0 - 0.5),
509+
pen=main_pen,
510+
hoverPen=hover_pen,
482511
)
483512
self.virtual_detector_point.sigRegionChanged.connect(
484513
partial(self.update_real_space_view, False)
@@ -487,7 +516,12 @@ def update_diffraction_detector(self):
487516
# Rectangular detector
488517
elif detector_shape == "Rectangular":
489518
self.virtual_detector_roi = pg.RectROI(
490-
[int(x0 - xr / 2), int(y0 - yr / 2)], [int(xr), int(yr)], pen=(3, 9)
519+
[int(x0 - xr / 2), int(y0 - yr / 2)],
520+
[int(xr), int(yr)],
521+
pen=main_pen,
522+
handlePen=handle_pen,
523+
hoverPen=hover_pen,
524+
handleHoverPen=hover_handle,
491525
)
492526
self.diffraction_space_widget.getView().addItem(self.virtual_detector_roi)
493527
self.virtual_detector_roi.sigRegionChangeFinished.connect(
@@ -497,7 +531,12 @@ def update_diffraction_detector(self):
497531
# Circular detector
498532
elif detector_shape == "Circle":
499533
self.virtual_detector_roi = pg.CircleROI(
500-
[int(x0 - xr / 2), int(y0 - yr / 2)], [int(xr), int(yr)], pen=(3, 9)
534+
[int(x0 - xr / 2), int(y0 - yr / 2)],
535+
[int(xr), int(yr)],
536+
pen=main_pen,
537+
handlePen=handle_pen,
538+
hoverPen=hover_pen,
539+
handleHoverPen=hover_handle,
501540
)
502541
self.diffraction_space_widget.getView().addItem(self.virtual_detector_roi)
503542
self.virtual_detector_roi.sigRegionChangeFinished.connect(
@@ -508,15 +547,23 @@ def update_diffraction_detector(self):
508547
elif detector_shape == "Annulus":
509548
# Make outer detector
510549
self.virtual_detector_roi_outer = pg.CircleROI(
511-
[int(x0 - xr), int(y0 - yr)], [int(2 * xr), int(2 * yr)], pen=(3, 9)
550+
[int(x0 - xr), int(y0 - yr)],
551+
[int(2 * xr), int(2 * yr)],
552+
pen=main_pen,
553+
handlePen=handle_pen,
554+
hoverPen=hover_pen,
555+
handleHoverPen=hover_handle,
512556
)
513557
self.diffraction_space_widget.getView().addItem(self.virtual_detector_roi_outer)
514558

515559
# Make inner detector
516560
self.virtual_detector_roi_inner = pg.CircleROI(
517561
[int(x0 - xr / 2), int(y0 - yr / 2)],
518562
[int(xr), int(yr)],
519-
pen=(4, 9),
563+
pen=main_pen,
564+
hoverPen=hover_pen,
565+
handlePen=handle_pen,
566+
handleHoverPen=hover_handle,
520567
movable=False,
521568
)
522569
self.diffraction_space_widget.getView().addItem(self.virtual_detector_roi_inner)
@@ -623,8 +670,8 @@ def update_tooltip(self):
623670
if scene.getView().rect().contains(pos_in_scene):
624671
pos_in_data = scene.view.mapSceneToView(pos_in_scene)
625672

626-
y = int(np.clip(np.floor(pos_in_data.x()), 0, data.shape[0] - 1))
627-
x = int(np.clip(np.floor(pos_in_data.y()), 0, data.shape[1] - 1))
673+
y = int(np.clip(np.floor(pos_in_data.x()), 0, data.shape[1] - 1))
674+
x = int(np.clip(np.floor(pos_in_data.y()), 0, data.shape[0] - 1))
628675

629676
if np.isrealobj(data):
630677
display_text = f"[{x},{y}]: {data[x,y]:.5g}"

src/py4D_browser/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ def on_click(self, *args):
6666
self.status_bar.showMessage("Shift+click to keep on", 5_000)
6767

6868

69-
def pg_point_roi(view_box, center=(-0.5, -0.5)):
69+
def pg_point_roi(view_box, center=(-0.5, -0.5), pen=(0, 9), hoverPen=None):
7070
"""
7171
Point selection. Based in pyqtgraph, and returns a pyqtgraph CircleROI object.
7272
This object has a sigRegionChanged.connect() signal method to connect to other functions.
7373
"""
74-
circ_roi = pg.CircleROI(center, (2, 2), movable=True, pen=(0, 9))
74+
circ_roi = pg.CircleROI(center, (2, 2), movable=True, pen=pen, hoverPen=hoverPen)
7575
h = circ_roi.addTranslateHandle((0.5, 0.5))
7676
h.pen = pg.mkPen("r")
7777
h.update()

0 commit comments

Comments
 (0)