| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Project: Fast Azimuthal integration |
| 5 |
# https://github.com/silx-kit/pyFAI |
| 6 |
# |
| 7 |
# |
| 8 |
# Copyright (C) European Synchrotron Radiation Facility, Grenoble, France |
| 9 |
# |
| 10 |
# Authors: Gael Goret <[email protected]> |
| 11 |
# |
| 12 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 |
# of this software and associated documentation files (the "Software"), to deal |
| 14 |
# in the Software without restriction, including without limitation the rights |
| 15 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 16 |
# copies of the Software, and to permit persons to whom the Software is |
| 17 |
# furnished to do so, subject to the following conditions: |
| 18 |
# . |
| 19 |
# The above copyright notice and this permission notice shall be included in |
| 20 |
# all copies or substantial portions of the Software. |
| 21 |
# . |
| 22 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 25 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 27 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 28 |
# THE SOFTWARE. |
| 29 |
""" |
| 30 |
Portable diffraction images viewer/converter |
| 31 |
|
| 32 |
* Written in Python, it combines the functionalities of the I/O library fabIO |
| 33 |
with a user friendly Qt GUI. |
| 34 |
* Image converter is also a light viewer based on the visualization tool |
| 35 |
provided by the module matplotlib. |
| 36 |
""" |
| 37 |
|
| 38 |
__version__ = "1.1" |
| 39 |
__author__ = "Gaël Goret, Jérôme Kieffer" |
| 40 |
__copyright__ = "2015-2026 ESRF" |
| 41 |
__licence__ = "MIT" |
| 42 |
|
| 43 |
import os |
| 44 |
import numpy |
| 45 |
import fabio |
| 46 |
from fabio.nexus import Nexus |
| 47 |
# ---------------------------------------------------------------------- |
| 48 |
# Qt imports via QtPy – this works with PyQt5, PySide2, PySide6, etc. |
| 49 |
# ---------------------------------------------------------------------- |
| 50 |
from qtpy import QtWidgets as qt |
| 51 |
from qtpy import QtCore as qtc |
| 52 |
from qtpy.QtGui import QIcon |
| 53 |
from qtpy.QtWidgets import ( |
| 54 |
QSizePolicy, |
| 55 |
QFileDialog, |
| 56 |
QMessageBox, |
| 57 |
QAction, |
| 58 |
QComboBox, |
| 59 |
QPlainTextEdit, |
| 60 |
QLabel, |
| 61 |
QSplitter, |
| 62 |
QTabWidget, |
| 63 |
QWidget, |
| 64 |
QProgressBar, |
| 65 |
QGroupBox, |
| 66 |
QVBoxLayout, |
| 67 |
QHBoxLayout, |
| 68 |
QPushButton, |
| 69 |
QListWidget, |
| 70 |
QCheckBox, |
| 71 |
QButtonGroup |
| 72 |
) |
| 73 |
# Matplotlib imports (unchanged) |
| 74 |
from matplotlib.figure import Figure |
| 75 |
from .matplotlib import FigureCanvasQTAgg |
| 76 |
from .matplotlib import NavigationToolbar2QT |
| 77 |
from .dialogs import CounterFormatOptionDialog, DownSamplingDialog, BinDialog |
| 78 |
# ---------------------------------------------------------------------- |
| 79 |
# Global configuration |
| 80 |
# ---------------------------------------------------------------------- |
| 81 |
numpy.seterr(divide="ignore") |
| 82 |
|
| 83 |
output_format = [ |
| 84 |
"*.bin", |
| 85 |
"*.cbf", |
| 86 |
"*.edf", |
| 87 |
"*.h5", |
| 88 |
"*.img", |
| 89 |
"*.mar2300", |
| 90 |
"*.mar3450", |
| 91 |
"*.marccd", |
| 92 |
"*.tiff", |
| 93 |
"*.sfrm", |
| 94 |
] |
| 95 |
# ---------------------------------------------------------------------- |
| 96 |
# Main application class |
| 97 |
# ---------------------------------------------------------------------- |
| 98 |
class AppForm(qt.QMainWindow): |
| 99 |
def __init__(self, parent=None): |
| 100 |
super().__init__(parent) |
| 101 |
# ------------------------------------------------------------------ |
| 102 |
# Window set‑up |
| 103 |
# ------------------------------------------------------------------ |
| 104 |
self.setWindowTitle("FabIO Viewer") |
| 105 |
self.setSizePolicy( |
| 106 |
QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) |
| 107 |
) |
| 108 |
# Menu / widgets |
| 109 |
self.create_menu() |
| 110 |
self.create_main_frame() |
| 111 |
self.create_status_bar() |
| 112 |
# ------------------------------------------------------------------ |
| 113 |
# Data containers |
| 114 |
# ------------------------------------------------------------------ |
| 115 |
self.data = numpy.array([]) |
| 116 |
self.header = [] |
| 117 |
|
| 118 |
# Data Series |
| 119 |
self.imgDict = {} |
| 120 |
self.data_series = [] |
| 121 |
self.header_series = [] |
| 122 |
self.sequential_file_list = [] |
| 123 |
self.sequential_file_dict = {} |
| 124 |
|
| 125 |
# Miscellaneous |
| 126 |
self.mask = None |
| 127 |
self.transform_data_series = False |
| 128 |
self.transform_list = [] |
| 129 |
self.sequential_file_mode = False |
| 130 |
self.h5_loaded = False |
| 131 |
self.counter_format = "%03d" |
| 132 |
# ---------------------------------------------------------------------- |
| 133 |
# Helper methods |
| 134 |
# ---------------------------------------------------------------------- |
| 135 |
def format_header(self, d): |
| 136 |
"""Return a nicely formatted header string. |
| 137 |
|
| 138 |
:param d: dict containing headers |
| 139 |
:return: formatted string |
| 140 |
""" |
| 141 |
keys = sorted(d.keys()) |
| 142 |
return " \n".join([f"{k}: {d[k]}" for k in keys]) + " \n" |
| 143 |
|
| 144 |
def _open(self, filename): |
| 145 |
"""Open an image with fabio, falling back to manual binary import.""" |
| 146 |
try: |
| 147 |
img = fabio.open(filename) |
| 148 |
except Exception: |
| 149 |
message = ( |
| 150 |
"Automatic format recognition procedure failed or perhaps you are " |
| 151 |
"trying to open a binary data block...\n\nSwitch to manual procedure." |
| 152 |
) |
| 153 |
QMessageBox.warning(self, "Message", message) |
| 154 |
dim1, dim2, offset, bytecode, endian = BinDialog(self).exec_() |
| 155 |
if dim1 is not None and dim2 is not None: |
| 156 |
endian = "<" if endian == "Short" else ">" |
| 157 |
img = fabio.binaryimage.binaryimage() |
| 158 |
img.read(filename, dim1, dim2, offset, bytecode, endian) |
| 159 |
img.header = { |
| 160 |
"Info": "No header information available in binary data blocks" |
| 161 |
} |
| 162 |
else: |
| 163 |
return None |
| 164 |
return img |
| 165 |
# ---------------------------------------------------------------------- |
| 166 |
# File‑opening / series handling |
| 167 |
# ---------------------------------------------------------------------- |
| 168 |
def open_data_series(self, series=None): |
| 169 |
if not series: |
| 170 |
series = QFileDialog.getOpenFileNames( |
| 171 |
self, "Select and open series of files" |
| 172 |
) |
| 173 |
if isinstance(series, tuple): |
| 174 |
# PyQt5 compatibility – QtPy returns a tuple as well |
| 175 |
series = series[0] |
| 176 |
series = [str(f) for f in list(series)] |
| 177 |
total = len(series) |
| 178 |
if total == 0: |
| 179 |
return |
| 180 |
# Reset UI / internal state |
| 181 |
self.data_series = [] |
| 182 |
self.header_series = [] |
| 183 |
self.sequential_file_list = [] |
| 184 |
self.imgDict = {} |
| 185 |
self.sequential_file_dict = {} |
| 186 |
self.images_list.clear() |
| 187 |
self.imagelistWidget.clear() |
| 188 |
self.headerTextEdit.clear() |
| 189 |
self.axes.clear() |
| 190 |
self.canvas.draw() |
| 191 |
self.h5_loaded = False |
| 192 |
iid = 0 |
| 193 |
for fname in series: |
| 194 |
if not fname: |
| 195 |
continue |
| 196 |
extract_fname = self.extract_fname_from_path(fname) |
| 197 |
if self.sequential_file_mode: |
| 198 |
self.statusBar().showMessage( |
| 199 |
f"Adding path {fname} to batch image list, please wait..." |
| 200 |
) |
| 201 |
self.log.appendPlainText( |
| 202 |
f"Adding path {fname} to batch image list" |
| 203 |
) |
| 204 |
qtc.QCoreApplication.processEvents() |
| 205 |
self.imagelistWidget.addItem(extract_fname) |
| 206 |
self.sequential_file_list.append(extract_fname) |
| 207 |
self.sequential_file_dict[extract_fname] = fname |
| 208 |
iid += 1 |
| 209 |
else: |
| 210 |
self.statusBar().showMessage( |
| 211 |
f"Opening file {fname}, please wait..." |
| 212 |
) |
| 213 |
self.log.appendPlainText(f"Opening file {fname}") |
| 214 |
qtc.QCoreApplication.processEvents() |
| 215 |
img = self._open(fname) |
| 216 |
if img is None: |
| 217 |
continue |
| 218 |
if img.nframes > 1: |
| 219 |
for img_idx in range(img.nframes): |
| 220 |
frame = img.getframe(img_idx) |
| 221 |
self.data_series.append(frame.data[:]) |
| 222 |
self.header_series.append(frame.header.copy()) |
| 223 |
frame_name = f"{extract_fname} # {img_idx}" |
| 224 |
self.images_list.addItem(frame_name) |
| 225 |
self.imagelistWidget.addItem(frame_name) |
| 226 |
self.imgDict[frame_name] = iid |
| 227 |
self.sequential_file_list.append(frame_name) |
| 228 |
self.sequential_file_dict[frame_name] = fname |
| 229 |
iid += 1 |
| 230 |
else: |
| 231 |
self.data_series.append(img.data[:]) |
| 232 |
self.header_series.append(img.header.copy()) |
| 233 |
self.images_list.addItem(extract_fname) |
| 234 |
self.imagelistWidget.addItem(extract_fname) |
| 235 |
self.imgDict[extract_fname] = iid |
| 236 |
self.sequential_file_list.append(extract_fname) |
| 237 |
self.sequential_file_dict[extract_fname] = fname |
| 238 |
iid += 1 |
| 239 |
self.progressBar.setValue(int((iid + 1) / total * 100.0)) |
| 240 |
self.statusBar().clearMessage() |
| 241 |
self.progressBar.setValue(0) |
| 242 |
self.log.appendPlainText("Opening procedure: Complete") |
| 243 |
if self.data_series: |
| 244 |
self.select_new_image(None, imgID=0) |
| 245 |
def open_h5_data_series(self): |
| 246 |
fname = QFileDialog.getOpenFileName(self, "Select and open series of files") |
| 247 |
if isinstance(fname, tuple): |
| 248 |
fname = fname[0] |
| 249 |
fname = str(fname) |
| 250 |
if not fname: |
| 251 |
return |
| 252 |
self.h5_loaded = True |
| 253 |
if self.filecheckBox.checkState(): |
| 254 |
self.filecheckBox.stateChanged.disconnect() |
| 255 |
self.filecheckBox.setCheckState(qtc.Qt.Unchecked) |
| 256 |
self.sequential_file_mode = False |
| 257 |
self.filecheckBox.stateChanged.connect(self.sequential_option) |
| 258 |
QMessageBox.warning( |
| 259 |
self, |
| 260 |
"Message", |
| 261 |
"Sequential file mode is not compatible with hdf5 input file: option removed", |
| 262 |
) |
| 263 |
# Reset UI / state |
| 264 |
self.data_series = [] |
| 265 |
self.header_series = [] |
| 266 |
self.sequential_file_list = [] |
| 267 |
self.sequential_file_dict = {} |
| 268 |
self.imagelistWidget.clear() |
| 269 |
self.headerTextEdit.clear() |
| 270 |
with Nexus(fname, "r") as nxs: |
| 271 |
entry = nxs.get_entries()[0] |
| 272 |
nxdata = nxs.get_class(entry, class_type="NXdata")[0] |
| 273 |
dataset = nxdata.get("data", numpy.zeros(shape=(1, 1, 1))) |
| 274 |
total = dataset.shape[0] |
| 275 |
imgDict = {} |
| 276 |
base_name = os.path.basename(os.path.splitext(fname)[0]) |
| 277 |
self.images_list.clear() |
| 278 |
safeiid = 0 |
| 279 |
for iid in range(total): |
| 280 |
self.progressBar.setValue(int((iid + 1) / total * 100.0)) |
| 281 |
self.log.appendPlainText( |
| 282 |
f"Extracting data from hdf5 archive, image number {iid}" |
| 283 |
) |
| 284 |
qtc.QCoreApplication.processEvents() |
| 285 |
self.data_series.append(dataset[iid]) |
| 286 |
self.header_series.append( |
| 287 |
{"Info": "No header information available in hdf5 Archive"} |
| 288 |
) |
| 289 |
imgDict[f"{base_name}{iid}"] = safeiid |
| 290 |
self.images_list.addItem(f"{base_name}{iid}") |
| 291 |
safeiid += 1 |
| 292 |
self.statusBar().clearMessage() |
| 293 |
self.progressBar.setValue(0) |
| 294 |
self.log.appendPlainText("Hdf5 Extraction: Complete") |
| 295 |
self.imgDict = imgDict.copy() |
| 296 |
if self.data_series: |
| 297 |
self.select_new_image(None, imgID=0) |
| 298 |
# ---------------------------------------------------------------------- |
| 299 |
# UI helpers |
| 300 |
# ---------------------------------------------------------------------- |
| 301 |
def extract_fname_from_path(self, name): |
| 302 |
pos = name.rfind("/") |
| 303 |
return name[pos + 1 :] if pos > -1 else name |
| 304 |
# ---------------------------------------------------------------------- |
| 305 |
# Save dialogs & conversion utilities |
| 306 |
# ---------------------------------------------------------------------- |
| 307 |
defaultSaveFilter = ( |
| 308 |
"binary data block (*.bin);;" |
| 309 |
"cbf image (*.cbf);;" |
| 310 |
"edf image (*.edf);;" |
| 311 |
"oxford diffraction image (*.img);;" |
| 312 |
"mar2300 image(*.mar2300);;" |
| 313 |
"mar3450 image (*.mar3450);;" |
| 314 |
"marccd image (*.marccd));;" |
| 315 |
"tiff image (*.tiff);;" |
| 316 |
"bruker image (*.sfrm)" |
| 317 |
) |
| 318 |
|
| 319 |
def _getSaveFileNameAndFilter(self, parent=None, caption="", directory="", filter=""): |
| 320 |
dialog = QFileDialog(parent, caption=caption, directory=directory) |
| 321 |
dialog.setAcceptMode(QFileDialog.AcceptSave) |
| 322 |
dialog.setNameFilter(filter) |
| 323 |
if not dialog.exec_(): |
| 324 |
return "", "" |
| 325 |
return dialog.selectedFiles()[0], dialog.selectedNameFilter() |
| 326 |
|
| 327 |
def save_as(self): |
| 328 |
info = self._getSaveFileNameAndFilter( |
| 329 |
self, |
| 330 |
"Save active image as", |
| 331 |
qt.QDir.currentPath(), |
| 332 |
filter=self.tr(self.defaultSaveFilter), |
| 333 |
) |
| 334 |
if self.data.any(): |
| 335 |
if info[0] and info[1]: |
| 336 |
fmt = self.extract_format_from_string(str(info[1])) |
| 337 |
fname = self.add_extention_if_absent(str(info[0]), fmt) |
| 338 |
self.convert_and_write(fname, fmt, self.data, self.header) |
| 339 |
else: |
| 340 |
if info[0] and info[1]: |
| 341 |
QMessageBox.warning( |
| 342 |
self, |
| 343 |
"Warning", |
| 344 |
"Could not save image as file if no data have been loaded", |
| 345 |
) |
| 346 |
def save_data_series_as_multiple_file(self): |
| 347 |
info = self._getSaveFileNameAndFilter( |
| 348 |
self, |
| 349 |
"Save data series as multiple files", |
| 350 |
qt.QDir.currentPath(), |
| 351 |
filter=self.tr(self.defaultSaveFilter), |
| 352 |
) |
| 353 |
if self.data_series or self.sequential_file_list: |
| 354 |
if info[0] and info[1]: |
| 355 |
fmt = self.extract_format_from_string(str(info[1])) |
| 356 |
fname = os.path.splitext(str(info[0]))[0] |
| 357 |
self.convert_and_write_multiple_files(fname, fmt) |
| 358 |
else: |
| 359 |
if info[0] and info[1]: |
| 360 |
QMessageBox.warning( |
| 361 |
self, |
| 362 |
"Warning", |
| 363 |
"Could not save image as file if no data have been loaded", |
| 364 |
) |
| 365 |
def save_data_series_as_singlehdf(self): |
| 366 |
info = self._getSaveFileNameAndFilter( |
| 367 |
self, |
| 368 |
"Save data series as single high density file", |
| 369 |
qt.QDir.currentPath(), |
| 370 |
filter=self.tr("HDF5 archive (*.h5)"), |
| 371 |
) |
| 372 |
if self.data_series or self.sequential_file_list: |
| 373 |
if info[0] and info[1]: |
| 374 |
fmt = self.extract_format_from_string(str(info[1])) |
| 375 |
fname = self.add_extention_if_absent(str(info[0]), fmt) |
| 376 |
if fmt == "*.h5": |
| 377 |
self.convert_and_save_to_h5(fname) |
| 378 |
else: |
| 379 |
QMessageBox.warning(self, "Warning", f"Unknown format: {fmt}") |
| 380 |
else: |
| 381 |
if info[0] and info[1]: |
| 382 |
QMessageBox.warning( |
| 383 |
self, |
| 384 |
"Warning", |
| 385 |
"Could not save image as file if no data have been loaded", |
| 386 |
) |
| 387 |
# ---------------------------------------------------------------------- |
| 388 |
# Conversion / I/O core |
| 389 |
# ---------------------------------------------------------------------- |
| 390 |
def convert_and_save_to_h5(self, fname): |
| 391 |
"""Save a stack as a Nexus entry (creates a new entry for each image).""" |
| 392 |
with Nexus(fname) as nxs: |
| 393 |
entry = nxs.new_entry( |
| 394 |
entry="entry", program_name="fabio_viewer", title="FabIO Viewer" |
| 395 |
) |
| 396 |
nxdata = nxs.new_class(entry, "fabio", class_type="NXdata") |
| 397 |
# Determine shape & total number of images |
| 398 |
if self.sequential_file_mode: |
| 399 |
total = len(self.sequential_file_list) |
| 400 |
first_fname = self.sequential_file_dict[self.sequential_file_list[0]] |
| 401 |
img = self._open(first_fname) |
| 402 |
if img is None: |
| 403 |
return |
| 404 |
data = img.data |
| 405 |
else: |
| 406 |
total = len(self.data_series) |
| 407 |
data = self.data_series[0] |
| 408 |
if self.transform_data_series: |
| 409 |
tmpdata = self.apply_queued_transformations(data) |
| 410 |
else: |
| 411 |
tmpdata = data |
| 412 |
shape = tmpdata.shape |
| 413 |
dataset = nxdata.create_dataset( |
| 414 |
"data", |
| 415 |
shape=(total,) + shape, |
| 416 |
dtype=numpy.float32, |
| 417 |
chunks=(1,) + shape, |
| 418 |
compression="gzip", |
| 419 |
) |
| 420 |
dataset.attrs["interpretation"] = "image" |
| 421 |
dataset.attrs["signal"] = "1" |
| 422 |
if self.sequential_file_mode: |
| 423 |
for iid, imgkey in enumerate(self.sequential_file_list): |
| 424 |
tmpfname = self.sequential_file_dict[imgkey] |
| 425 |
img = self._open(tmpfname) |
| 426 |
if img is None: |
| 427 |
continue |
| 428 |
self.progressBar.setValue(int((iid + 1) / total * 100.0)) |
| 429 |
self.log.appendPlainText( |
| 430 |
f"Converting and saving file {tmpfname}. saving file number {iid}" |
| 431 |
) |
| 432 |
qtc.QCoreApplication.processEvents() |
| 433 |
if self.transform_data_series: |
| 434 |
tmpdata = self.apply_queued_transformations(img.data) |
| 435 |
else: |
| 436 |
tmpdata = img.data |
| 437 |
dataset[iid] = tmpdata |
| 438 |
else: |
| 439 |
for iid, data in enumerate(self.data_series): |
| 440 |
self.log.appendPlainText(f"Saving file number {iid}") |
| 441 |
self.progressBar.setValue(int((iid + 1) / total * 100.0)) |
| 442 |
qtc.QCoreApplication.processEvents() |
| 443 |
if self.transform_data_series: |
| 444 |
tmpdata = self.apply_queued_transformations(data) |
| 445 |
else: |
| 446 |
tmpdata = data |
| 447 |
dataset[iid] = tmpdata |
| 448 |
self.statusBar().clear() |
| 449 |
self.progressBar.setValue(0) |
| 450 |
self.log.appendPlainText("Hdf5 Recording: Complete") |
| 451 |
def add_extention_if_absent(self, fname, fmt): |
| 452 |
"""Append the required extension if the filename does not already have one.""" |
| 453 |
slash = fname.rfind("/") |
| 454 |
dot = fname.rfind(".") |
| 455 |
return fname if dot > slash else fname + fmt[1:] |
| 456 |
def convert_and_write(self, fname, fmt, data, header): |
| 457 |
"""Write a single image in the requested format.""" |
| 458 |
if fmt == "*.bin": |
| 459 |
with open(fname, mode="wb") as out: |
| 460 |
data.tofile(out) |
| 461 |
return |
| 462 |
elif fmt == "*.marccd": |
| 463 |
out = fabio.marccdimage.marccdimage(data=data, header=header) |
| 464 |
elif fmt == "*.edf": |
| 465 |
out = fabio.edfimage.edfimage(data=data, header=header) |
| 466 |
elif fmt == "*.tiff": |
| 467 |
out = fabio.tifimage.tifimage(data=data, header=header) |
| 468 |
elif fmt == "*.cbf": |
| 469 |
out = fabio.cbfimage.cbfimage(data=data, header=header) |
| 470 |
elif fmt in ("*.mar3450", "*.mar2300"): |
| 471 |
data = self.padd_mar(data, fmt) |
| 472 |
out = fabio.mar345image.mar345image(data=data, header=header) |
| 473 |
elif fmt == "*.img": |
| 474 |
out = fabio.OXDimage.OXDimage(data=data, header=header) |
| 475 |
elif fmt == "*.sfrm": |
| 476 |
out = fabio.brukerimage.brukerimage(data=data, header=header) |
| 477 |
else: |
| 478 |
raise Warning(f"Unknown format: {fmt}") |
| 479 |
self.statusBar().showMessage( |
| 480 |
f"Writing file {fname} to {fmt[2:]} format, please wait..." |
| 481 |
) |
| 482 |
self.log.appendPlainText(f"Writing file {fname} to {fmt[2:]} format") |
| 483 |
qtc.QCoreApplication.processEvents() |
| 484 |
out.write(fname) |
| 485 |
self.statusBar().clearMessage() |
| 486 |
|
| 487 |
def convert_and_write_multiple_files(self, fname, fmt): |
| 488 |
"""Write a series of images to separate files.""" |
| 489 |
if self.sequential_file_mode: |
| 490 |
total = len(self.sequential_file_list) |
| 491 |
ii = 0 |
| 492 |
for imgkey in self.sequential_file_list: |
| 493 |
tmpfname = self.sequential_file_dict[imgkey] |
| 494 |
img = self._open(tmpfname) |
| 495 |
if img is None: |
| 496 |
continue |
| 497 |
self.progressBar.setValue(int((ii + 1) / total * 100.0)) |
| 498 |
self.log.appendPlainText(f"Converting file {tmpfname}") |
| 499 |
qtc.QCoreApplication.processEvents() |
| 500 |
if self.transform_data_series: |
| 501 |
tmpdata = self.apply_queued_transformations(img.data) |
| 502 |
else: |
| 503 |
tmpdata = img.data |
| 504 |
outname = (f"{fname}_{self.counter_format}{fmt[1:]}") % ii |
| 505 |
self.convert_and_write(outname, fmt, tmpdata, img.header) |
| 506 |
ii += 1 |
| 507 |
else: |
| 508 |
total = len(self.data_series) |
| 509 |
for i in range(total): |
| 510 |
tmpdata = self.data_series[i] |
| 511 |
tmpheader = self.header_series[i] |
| 512 |
outname = (f"{fname}_{self.counter_format}{fmt[1:]}") % i |
| 513 |
self.progressBar.setValue(int((i + 1) / total * 100.0)) |
| 514 |
self.log.appendPlainText(f"Converting file {i}") |
| 515 |
qtc.QCoreApplication.processEvents() |
| 516 |
if self.transform_data_series: |
| 517 |
tmpdata = self.apply_queued_transformations(tmpdata) |
| 518 |
self.convert_and_write(outname, fmt, tmpdata, tmpheader) |
| 519 |
self.progressBar.setValue(0) |
| 520 |
self.log.appendPlainText(f"Convertion to {fmt[2:]}: Complete") |
| 521 |
def extract_format_from_string(self, fmt_long): |
| 522 |
for fmt in output_format: |
| 523 |
if fmt in fmt_long: |
| 524 |
return fmt |
| 525 |
raise Warning(f"Unknown format: {fmt_long}") |
| 526 |
# ---------------------------------------------------------------------- |
| 527 |
# Image transformations |
| 528 |
# ---------------------------------------------------------------------- |
| 529 |
def horizontal_mirror(self): |
| 530 |
if self.transform_data_series: |
| 531 |
if self.sequential_file_mode: |
| 532 |
self.transformation_queue.addItem("horizontal_mirror") |
| 533 |
self.transform_list.append("horizontal_mirror") |
| 534 |
self.log.appendPlainText( |
| 535 |
"Add horizontal mirror to transformations queue" |
| 536 |
) |
| 537 |
qtc.QCoreApplication.processEvents() |
| 538 |
else: |
| 539 |
total = len(self.data_series) |
| 540 |
if not total: |
| 541 |
QMessageBox.warning( |
| 542 |
self, |
| 543 |
"Warning", |
| 544 |
"Could not transform image if no data have been loaded", |
| 545 |
) |
| 546 |
return |
| 547 |
for i in range(total): |
| 548 |
self.data_series[i] = numpy.flipud(self.data_series[i])[:] |
| 549 |
self.progressBar.setValue(int((i + 1) / total * 100.0)) |
| 550 |
self.log.appendPlainText( |
| 551 |
f"Applying horizontal mirror to data series: image {i}" |
| 552 |
) |
| 553 |
qtc.QCoreApplication.processEvents() |
| 554 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 555 |
self.select_new_image(None, imgID=iid) |
| 556 |
else: |
| 557 |
if self.data.any(): |
| 558 |
self.data = numpy.flipud(self.data)[:] |
| 559 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 560 |
self.data_series[iid] = self.data[:] |
| 561 |
self.log.appendPlainText("Applying horizontal mirror to current data") |
| 562 |
self.on_draw() |
| 563 |
else: |
| 564 |
QMessageBox.warning( |
| 565 |
self, |
| 566 |
"Warning", |
| 567 |
"Could not transform image if no data have been loaded", |
| 568 |
) |
| 569 |
self.progressBar.setValue(0) |
| 570 |
|
| 571 |
def vertical_mirror(self): |
| 572 |
if self.transform_data_series: |
| 573 |
if self.sequential_file_mode: |
| 574 |
self.transformation_queue.addItem("vertical_mirror") |
| 575 |
self.transform_list.append("vertical_mirror") |
| 576 |
self.log.appendPlainText( |
| 577 |
"Add vertical mirror to transformations queue" |
| 578 |
) |
| 579 |
qtc.QCoreApplication.processEvents() |
| 580 |
else: |
| 581 |
total = len(self.data_series) |
| 582 |
if not total: |
| 583 |
QMessageBox.warning( |
| 584 |
self, |
| 585 |
"Warning", |
| 586 |
"Could not transform image if no data have been loaded", |
| 587 |
) |
| 588 |
return |
| 589 |
for i in range(total): |
| 590 |
self.data_series[i] = numpy.fliplr(self.data_series[i])[:] |
| 591 |
self.progressBar.setValue(int((i + 1) / total * 100.0)) |
| 592 |
self.log.appendPlainText( |
| 593 |
f"Applying vertical mirror to data series: image {i}" |
| 594 |
) |
| 595 |
qtc.QCoreApplication.processEvents() |
| 596 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 597 |
self.select_new_image(None, imgID=iid) |
| 598 |
else: |
| 599 |
if self.data.any(): |
| 600 |
self.data = numpy.fliplr(self.data)[:] |
| 601 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 602 |
self.data_series[iid] = self.data[:] |
| 603 |
self.log.appendPlainText("Applying vertical mirror to current data") |
| 604 |
self.on_draw() |
| 605 |
else: |
| 606 |
QMessageBox.warning( |
| 607 |
self, |
| 608 |
"Warning", |
| 609 |
"Could not transform image if no data have been loaded", |
| 610 |
) |
| 611 |
self.progressBar.setValue(0) |
| 612 |
|
| 613 |
def transposition(self): |
| 614 |
if self.transform_data_series: |
| 615 |
if self.sequential_file_mode: |
| 616 |
self.transformation_queue.addItem("transposition") |
| 617 |
self.transform_list.append("transposition") |
| 618 |
self.log.appendPlainText( |
| 619 |
"Add transposition to transformations queue" |
| 620 |
) |
| 621 |
qtc.QCoreApplication.processEvents() |
| 622 |
else: |
| 623 |
total = len(self.data_series) |
| 624 |
if not total: |
| 625 |
QMessageBox.warning( |
| 626 |
self, |
| 627 |
"Warning", |
| 628 |
"Could not transform image if no data have been loaded", |
| 629 |
) |
| 630 |
return |
| 631 |
for i in range(total): |
| 632 |
self.data_series[i] = self.data_series[i].transpose()[:] |
| 633 |
self.progressBar.setValue(int((i + 1) / total * 100.0)) |
| 634 |
self.log.appendPlainText( |
| 635 |
f"Applying transposition to data series: image {i}" |
| 636 |
) |
| 637 |
qtc.QCoreApplication.processEvents() |
| 638 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 639 |
self.select_new_image(None, imgID=iid) |
| 640 |
else: |
| 641 |
if self.data.any(): |
| 642 |
self.data = self.data.transpose()[:] |
| 643 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 644 |
self.data_series[iid] = self.data[:] |
| 645 |
self.log.appendPlainText("Applying transposition to current data") |
| 646 |
self.on_draw() |
| 647 |
else: |
| 648 |
QMessageBox.warning( |
| 649 |
self, |
| 650 |
"Warning", |
| 651 |
"Could not transform image if no data have been loaded", |
| 652 |
) |
| 653 |
self.progressBar.setValue(0) |
| 654 |
|
| 655 |
def rotation_90(self): |
| 656 |
if self.transform_data_series: |
| 657 |
if self.sequential_file_mode: |
| 658 |
self.transformation_queue.addItem("rotation(+90)") |
| 659 |
self.transform_list.append("rotation(+90)") |
| 660 |
self.log.appendPlainText("Add + 90 rotation to transformations queue") |
| 661 |
qtc.QCoreApplication.processEvents() |
| 662 |
else: |
| 663 |
total = len(self.data_series) |
| 664 |
if not total: |
| 665 |
QMessageBox.warning( |
| 666 |
self, |
| 667 |
"Warning", |
| 668 |
"Could not transform image if no data have been loaded", |
| 669 |
) |
| 670 |
return |
| 671 |
for i in range(total): |
| 672 |
self.data_series[i] = numpy.rot90(self.data_series[i])[:] |
| 673 |
self.progressBar.setValue(int((i + 1) / total * 100.0)) |
| 674 |
self.log.appendPlainText( |
| 675 |
f"Applying + 90 rotation to data series: image {i}" |
| 676 |
) |
| 677 |
qtc.QCoreApplication.processEvents() |
| 678 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 679 |
self.select_new_image(None, imgID=iid) |
| 680 |
else: |
| 681 |
if self.data.any(): |
| 682 |
self.data = numpy.rot90(self.data)[:] |
| 683 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 684 |
self.data_series[iid] = self.data[:] |
| 685 |
self.log.appendPlainText("Applying + 90 rotation to current data") |
| 686 |
self.on_draw() |
| 687 |
else: |
| 688 |
QMessageBox.warning( |
| 689 |
self, |
| 690 |
"Warning", |
| 691 |
"Could not transform image if no data have been loaded", |
| 692 |
) |
| 693 |
self.progressBar.setValue(0) |
| 694 |
|
| 695 |
def rotation_180(self): |
| 696 |
if self.transform_data_series: |
| 697 |
if self.sequential_file_mode: |
| 698 |
self.transformation_queue.addItem("rotation(+180)") |
| 699 |
self.transform_list.append("rotation(+180)") |
| 700 |
self.log.appendPlainText("Add + 180 rotation to transformations queue") |
| 701 |
qtc.QCoreApplication.processEvents() |
| 702 |
else: |
| 703 |
total = len(self.data_series) |
| 704 |
if not total: |
| 705 |
QMessageBox.warning( |
| 706 |
self, |
| 707 |
"Warning", |
| 708 |
"Could not transform image if no data have been loaded", |
| 709 |
) |
| 710 |
return |
| 711 |
for i in range(total): |
| 712 |
self.data_series[i] = numpy.rot90(self.data_series[i], 2)[:] |
| 713 |
self.progressBar.setValue(int((i + 1) / total * 100.0)) |
| 714 |
self.log.appendPlainText( |
| 715 |
f"Applying + 180 rotation to data series: image {i}" |
| 716 |
) |
| 717 |
qtc.QCoreApplication.processEvents() |
| 718 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 719 |
self.select_new_image(None, imgID=iid) |
| 720 |
else: |
| 721 |
if self.data.any(): |
| 722 |
self.data = numpy.rot90(self.data, 2)[:] |
| 723 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 724 |
self.data_series[iid] = self.data[:] |
| 725 |
self.log.appendPlainText("Applying + 180 rotation to current data") |
| 726 |
self.on_draw() |
| 727 |
else: |
| 728 |
QMessageBox.warning( |
| 729 |
self, |
| 730 |
"Warning", |
| 731 |
"Could not transform image if no data have been loaded", |
| 732 |
) |
| 733 |
self.progressBar.setValue(0) |
| 734 |
|
| 735 |
def rotation_270(self): |
| 736 |
if self.transform_data_series: |
| 737 |
if self.sequential_file_mode: |
| 738 |
self.transformation_queue.addItem("rotation(-90)") |
| 739 |
self.transform_list.append("rotation(-90)") |
| 740 |
self.log.appendPlainText("Add - 90 rotation to transformations queue") |
| 741 |
qtc.QCoreApplication.processEvents() |
| 742 |
else: |
| 743 |
total = len(self.data_series) |
| 744 |
if not total: |
| 745 |
QMessageBox.warning( |
| 746 |
self, |
| 747 |
"Warning", |
| 748 |
"Could not transform image if no data have been loaded", |
| 749 |
) |
| 750 |
return |
| 751 |
for i in range(total): |
| 752 |
self.data_series[i] = numpy.rot90(self.data_series[i], 3)[:] |
| 753 |
self.progressBar.setValue(int((i + 1) / total * 100.0)) |
| 754 |
self.log.appendPlainText( |
| 755 |
f"Applying - 90 rotation to data series: image {i}" |
| 756 |
) |
| 757 |
qtc.QCoreApplication.processEvents() |
| 758 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 759 |
self.select_new_image(None, imgID=iid) |
| 760 |
else: |
| 761 |
if self.data.any(): |
| 762 |
self.data = numpy.rot90(self.data, 3)[:] |
| 763 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 764 |
self.data_series[iid] = self.data[:] |
| 765 |
self.log.appendPlainText("Applying - 90 rotation to current data") |
| 766 |
self.on_draw() |
| 767 |
else: |
| 768 |
QMessageBox.warning( |
| 769 |
self, |
| 770 |
"Warning", |
| 771 |
"Could not transform image if no data have been loaded", |
| 772 |
) |
| 773 |
self.progressBar.setValue(0) |
| 774 |
|
| 775 |
def mask(self): |
| 776 |
fname = QFileDialog.getOpenFileName(self, "Select and import a boolean mask from binary data block file") |
| 777 |
if isinstance(fname, tuple): |
| 778 |
fname = fname[0] |
| 779 |
|
| 780 |
fname = str(fname) |
| 781 |
if not fname: |
| 782 |
return |
| 783 |
dim1, dim2, offset, bytecode, endian = BinDialog(self).exec_() |
| 784 |
if dim1 is None: |
| 785 |
return |
| 786 |
endian = "<" if endian == "Short" else ">" |
| 787 |
img = fabio.binaryimage.binaryimage() |
| 788 |
img.read(fname, dim1, dim2, offset, bytecode, endian) |
| 789 |
self.mask = img.data[:] |
| 790 |
if self.transform_data_series: |
| 791 |
if self.sequential_file_mode: |
| 792 |
self.transformation_queue.addItem("masking") |
| 793 |
self.transform_list.append("masking") |
| 794 |
self.log.appendPlainText("Add masking to transformations queue") |
| 795 |
qtc.QCoreApplication.processEvents() |
| 796 |
else: |
| 797 |
total = len(self.data_series) |
| 798 |
if not total: |
| 799 |
QMessageBox.warning( |
| 800 |
self, |
| 801 |
"Warning", |
| 802 |
"Could not transform image if no data have been loaded", |
| 803 |
) |
| 804 |
return |
| 805 |
for i in range(total): |
| 806 |
if self.data_series[i].shape != self.mask.shape: |
| 807 |
msg = f"Mask and image have different shapes, skipping image {i}" |
| 808 |
QMessageBox.warning(self, "Warning", msg) |
| 809 |
self.log.appendPlainText(msg) |
| 810 |
else: |
| 811 |
self.data_series[i] = self.mask * self.data_series[i] |
| 812 |
self.progressBar.setValue(int((i + 1) / total * 100.0)) |
| 813 |
self.log.appendPlainText( |
| 814 |
f"Applying mask to data series: image {i}" |
| 815 |
) |
| 816 |
qtc.QCoreApplication.processEvents() |
| 817 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 818 |
self.select_new_image(None, imgID=iid) |
| 819 |
else: |
| 820 |
if self.data.any(): |
| 821 |
self.data = self.mask * self.data |
| 822 |
iid = self.imgDict[str(self.images_list.currentText())] |
| 823 |
self.data_series[iid] = self.data[:] |
| 824 |
self.on_draw() |
| 825 |
self.statusBar().showMessage("Binary boolean mask loaded and applied", 2000) |
| 826 |
self.log.appendPlainText("Binary boolean mask loaded and applied") |
| 827 |
else: |
| 828 |
QMessageBox.warning( |
| 829 |
self, |
| 830 |
"Warning", |
| 831 |
"Could not transform image if no data have been loaded", |
| 832 |
) |
| 833 |
self.progressBar.setValue(0) |
| 834 |
def downsample(self): |
| 835 |
thick, start_angle, step_angle = DownSamplingDialog().exec_() |
| 836 |
if thick is None: |
| 837 |
return |
| 838 |
info = self._getSaveFileNameAndFilter( |
| 839 |
self, |
| 840 |
"Save downsampled data series as multiple files", |
| 841 |
qt.QDir.currentPath(), |
| 842 |
filter=self.tr(self.defaultSaveFilter), |
| 843 |
) |
| 844 |
if not (self.data_series or self.sequential_file_list): |
| 845 |
if info[0] and info[1]: |
| 846 |
QMessageBox.warning( |
| 847 |
self, |
| 848 |
"Warning", |
| 849 |
"Could not save image as file if no data have been loaded", |
| 850 |
) |
| 851 |
return |
| 852 |
if not (info[0] and info[1]): |
| 853 |
return |
| 854 |
fmt = self.extract_format_from_string(str(info[1])) |
| 855 |
fname = os.path.splitext(str(info[0]))[0] |
| 856 |
if self.sequential_file_mode: |
| 857 |
total = len(self.sequential_file_list) |
| 858 |
stack = None |
| 859 |
subtotal = (total // thick) * thick |
| 860 |
for i in range(subtotal): |
| 861 |
j = i % thick |
| 862 |
k = i // thick |
| 863 |
imgkey = self.sequential_file_list[i] |
| 864 |
tmpfname = self.sequential_file_dict[imgkey] |
| 865 |
img = self._open(tmpfname) |
| 866 |
if img is None: |
| 867 |
continue |
| 868 |
if stack is None: |
| 869 |
stack = numpy.zeros_like(img.data) |
| 870 |
if img.data.shape != stack.shape: |
| 871 |
self.log.appendPlainText( |
| 872 |
f"Error image shape: {img.data.shape} summed data shape: {stack.shape}" |
| 873 |
) |
| 874 |
continue |
| 875 |
numpy.add(stack, img.data, stack) |
| 876 |
self.progressBar.setValue(int((i + 1) / subtotal * 100.0)) |
| 877 |
self.log.appendPlainText(f"File {imgkey} stacked") |
| 878 |
qtc.QCoreApplication.processEvents() |
| 879 |
if j == thick - 1: |
| 880 |
self.log.appendPlainText(f"stack number {k} summing up") |
| 881 |
qtc.QCoreApplication.processEvents() |
| 882 |
if fmt in ("*.mar3450", "*.mar2300"): |
| 883 |
img.header["PHI_START"] = f"{start_angle + step_angle * (i - thick + 1):.3f}" |
| 884 |
img.header["PHI_END"] = f"{start_angle + step_angle * i:.3f}" |
| 885 |
outname = (f"{fname}_{self.counter_format}{fmt[1:]}") % k |
| 886 |
self.convert_and_write(outname, fmt, stack, img.header) |
| 887 |
stack = numpy.zeros_like(img.data) |
| 888 |
else: |
| 889 |
total = len(self.data_series) |
| 890 |
stack = numpy.zeros_like(self.data_series[0]) |
| 891 |
subtotal = (total // thick) * thick |
| 892 |
for i in range(subtotal): |
| 893 |
j = i % thick |
| 894 |
k = i // thick |
| 895 |
data = self.data_series[i] |
| 896 |
if data.shape != stack.shape: |
| 897 |
self.log.appendPlainText( |
| 898 |
f"Error image shape: {data.shape} summed data shape: {stack.shape}" |
| 899 |
) |
| 900 |
continue |
| 901 |
numpy.add(stack, data, stack) |
| 902 |
self.progressBar.setValue(int((i + 1) / subtotal * 100.0)) |
| 903 |
self.log.appendPlainText(f"File number {i} stacked") |
| 904 |
qtc.QCoreApplication.processEvents() |
| 905 |
if j == thick - 1: |
| 906 |
self.log.appendPlainText(f"stack number {k} summing up") |
| 907 |
qtc.QCoreApplication.processEvents() |
| 908 |
if fmt in ("*.mar3450", "*.mar2300"): |
| 909 |
self.header_series[i]["PHI_START"] = f"{start_angle + step_angle * (i - thick + 1):.3f}" |
| 910 |
self.header_series[i]["PHI_END"] = f"{start_angle + step_angle * i:.3f}" |
| 911 |
outname = (f"{fname}_{self.counter_format}{fmt[1:]}") % k |
| 912 |
self.convert_and_write(outname, fmt, stack, self.header_series[i]) |
| 913 |
stack = numpy.zeros_like(data) |
| 914 |
self.progressBar.setValue(0) |
| 915 |
self.log.appendPlainText("Downsampling: Complete") |
| 916 |
qtc.QCoreApplication.processEvents() |
| 917 |
# ---------------------------------------------------------------------- |
| 918 |
# UI callbacks – image selection, drawing, etc. |
| 919 |
# ---------------------------------------------------------------------- |
| 920 |
def select_new_image(self, name, imgID=None): |
| 921 |
iid = imgID if imgID is not None else self.imgDict[str(name)] |
| 922 |
self.data = self.data_series[iid] |
| 923 |
self.header = self.header_series[iid] |
| 924 |
|
| 925 |
self.headerTextEdit.setPlainText(str(self.format_header(self.header))) |
| 926 |
self.on_draw() |
| 927 |
|
| 928 |
def on_pick(self, event): |
| 929 |
if event.inaxes and self.data.any(): |
| 930 |
x = int(round(event.xdata)) |
| 931 |
y = int(round(event.ydata)) |
| 932 |
if x < self.data.shape[1] and y < self.data.shape[0]: |
| 933 |
i = self.data[y, x] |
| 934 |
self.pix_coords_label.setText( |
| 935 |
f"Pixel coordinates and intensity: x ={x:6d}, y ={y:6d}, i ={i:6g}" |
| 936 |
) |
| 937 |
else: |
| 938 |
self.pix_coords_label.setText( |
| 939 |
"Pixel coordinates and intensity: x = None , y = None , i = None " |
| 940 |
) |
| 941 |
else: |
| 942 |
self.pix_coords_label.setText( |
| 943 |
"Pixel coordinates and intensity: x = None , y = None , i = None " |
| 944 |
) |
| 945 |
|
| 946 |
def on_draw(self): |
| 947 |
"""Redraws the figure""" |
| 948 |
self.statusBar().showMessage("Loading display...") |
| 949 |
qtc.QCoreApplication.processEvents() |
| 950 |
self.axes.clear() |
| 951 |
self.axes.imshow(numpy.arcsinh(self.data), interpolation="nearest") |
| 952 |
|
| 953 |
self.axes.set_visible(True) |
| 954 |
if self.axes.get_ylim()[0] < self.axes.get_ylim()[1]: |
| 955 |
self.axes.set_ylim(self.axes.get_ylim()[::-1]) |
| 956 |
self.canvas.draw() |
| 957 |
self.statusBar().clearMessage() |
| 958 |
|
| 959 |
def batch_to_view(self): |
| 960 |
items = self.imagelistWidget.selectedItems() |
| 961 |
if not items: |
| 962 |
return |
| 963 |
item = str(items[0].text()) |
| 964 |
if any(ext in item.lower() for ext in (".h5", ".hdf", ".hdf5")): |
| 965 |
QMessageBox.warning( |
| 966 |
self, "Message", "Can't display hdf archive from batch mode " |
| 967 |
) |
| 968 |
return |
| 969 |
if self.sequential_file_mode: |
| 970 |
self.data_series = [] |
| 971 |
self.header_series = [] |
| 972 |
self.imgDict = {} |
| 973 |
self.images_list.clear() |
| 974 |
self.headerTextEdit.clear() |
| 975 |
self.axes.clear() |
| 976 |
self.canvas.draw() |
| 977 |
self.statusBar().showMessage( |
| 978 |
f"Import image {item} in the View Mode tab, please wait..." |
| 979 |
) |
| 980 |
self.log.appendPlainText(f"Import image {item} in the View Mode tab") |
| 981 |
qtc.QCoreApplication.processEvents() |
| 982 |
fname = self.sequential_file_dict[item] |
| 983 |
extract_fname = os.path.splitext(os.path.basename(fname))[0] |
| 984 |
img = self._open(fname) |
| 985 |
if img is None: |
| 986 |
return |
| 987 |
|
| 988 |
if img.nframes > 1: |
| 989 |
for img_idx in range(img.nframes): |
| 990 |
frame = img.getframe(img_idx) |
| 991 |
self.data_series.append(frame.data[:]) |
| 992 |
self.header_series.append(frame.header.copy()) |
| 993 |
frame_name = f"{extract_fname} # {img_idx}" |
| 994 |
self.images_list.addItem(frame_name) |
| 995 |
self.imagelistWidget.addItem(frame_name) |
| 996 |
self.imgDict[frame_name] = len(self.data_series) - 1 |
| 997 |
else: |
| 998 |
self.data_series.append(img.data[:]) |
| 999 |
self.header_series.append(img.header.copy()) |
| 1000 |
self.images_list.addItem(extract_fname) |
| 1001 |
self.imagelistWidget.addItem(extract_fname) |
| 1002 |
self.imgDict[extract_fname] = len(self.data_series) - 1 |
| 1003 |
self.statusBar().clearMessage() |
| 1004 |
if self.data_series: |
| 1005 |
self.select_new_image(None, imgID=0) |
| 1006 |
self.tabWidget.setCurrentIndex(0) |
| 1007 |
|
| 1008 |
def set_counter_format_option(self): |
| 1009 |
self.counter_format = CounterFormatOptionDialog(self.counter_format).exec_() |
| 1010 |
def padd_mar(self, data, fmt): |
| 1011 |
"""Pad or crop MAR images to the required 2300×2300 or 3450×3450 size.""" |
| 1012 |
dim1, dim2 = data.shape |
| 1013 |
size = 2300 if fmt == "*.mar2300" else 3450 |
| 1014 |
left = (size - dim1) // 2 |
| 1015 |
right = size - (dim1 + left) |
| 1016 |
up = (size - dim2) // 2 |
| 1017 |
down = size - (dim2 + up) |
| 1018 |
|
| 1019 |
out = numpy.zeros((size, size)) |
| 1020 |
# Compute slice indices (positive → pad, negative → crop) |
| 1021 |
in_lm, out_lm = (0, left) if left > 0 else (-left, 0) |
| 1022 |
in_rm, out_rm = (dim1, size) if right > 0 else (dim1 - right, size) |
| 1023 |
in_um, out_um = (0, up) if up > 0 else (-up, 0) |
| 1024 |
in_dm, out_dm = (dim2, size) if down > 0 else (dim2 - down, size) |
| 1025 |
out[out_lm:out_rm, out_um:out_dm] = data[in_lm:in_rm, in_um:in_dm] |
| 1026 |
return out |
| 1027 |
|
| 1028 |
def sequential_option(self, state): |
| 1029 |
if not self.h5_loaded: |
| 1030 |
self.sequential_file_mode = state == qtc.Qt.Checked |
| 1031 |
else: |
| 1032 |
self.filecheckBox.stateChanged.disconnect() |
| 1033 |
self.filecheckBox.setCheckState(qtc.Qt.Unchecked) |
| 1034 |
self.sequential_file_mode = False |
| 1035 |
self.filecheckBox.stateChanged.connect(self.sequential_option) |
| 1036 |
QMessageBox.warning( |
| 1037 |
self, |
| 1038 |
"Message", |
| 1039 |
"Sequential file mode is not compatible with hdf5 input file: option removed", |
| 1040 |
) |
| 1041 |
# ---------------------------------------------------------------------- |
| 1042 |
# Menu / toolbar creation |
| 1043 |
# ---------------------------------------------------------------------- |
| 1044 |
def create_menu(self): |
| 1045 |
self.file_menu = self.menuBar().addMenu("&File") |
| 1046 |
self.open_menu = self.file_menu.addMenu("&Open") |
| 1047 |
# ---- Open ---- |
| 1048 |
self.add_actions( |
| 1049 |
self.open_menu, |
| 1050 |
( |
| 1051 |
self.create_action( |
| 1052 |
"&Image(s)", |
| 1053 |
slot=self.open_data_series, |
| 1054 |
tip="Load single file and data series (files sequence)", |
| 1055 |
), |
| 1056 |
self.create_action( |
| 1057 |
"&Hdf5 data series", |
| 1058 |
slot=self.open_h5_data_series, |
| 1059 |
tip="Load a series stored in an HDF5 file", |
| 1060 |
), |
| 1061 |
), |
| 1062 |
) |
| 1063 |
# ---- Save ---- |
| 1064 |
self.save_as_menu = self.file_menu.addMenu("&Save") |
| 1065 |
self.add_actions( |
| 1066 |
self.save_as_menu, |
| 1067 |
( |
| 1068 |
self.create_action( |
| 1069 |
"&Active image", |
| 1070 |
slot=self.save_as, |
| 1071 |
tip="Save/Convert the image which is currently displayed", |
| 1072 |
), |
| 1073 |
), |
| 1074 |
) |
| 1075 |
self.save_data_series_menu = self.save_as_menu.addMenu("&Data series as") |
| 1076 |
self.add_actions( |
| 1077 |
self.save_data_series_menu, |
| 1078 |
( |
| 1079 |
self.create_action( |
| 1080 |
"&Multiple files", |
| 1081 |
slot=self.save_data_series_as_multiple_file, |
| 1082 |
tip="Save/Convert the set of images currently loaded into the images list", |
| 1083 |
), |
| 1084 |
self.create_action( |
| 1085 |
"&Hdf5 archive", |
| 1086 |
slot=self.save_data_series_as_singlehdf, |
| 1087 |
tip="Save/Convert the set of images currently loaded into the images list", |
| 1088 |
), |
| 1089 |
), |
| 1090 |
) |
| 1091 |
self.add_actions( |
| 1092 |
self.file_menu, |
| 1093 |
( |
| 1094 |
self.create_action( |
| 1095 |
"&Quit", slot=self.close, shortcut="Ctrl+Q", tip="Close the application" |
| 1096 |
), |
| 1097 |
), |
| 1098 |
) |
| 1099 |
# ---- Transform ---- |
| 1100 |
self.transform_menu = self.menuBar().addMenu("&Transform") |
| 1101 |
self.mirror_menu = self.transform_menu.addMenu("&Mirror") |
| 1102 |
self.add_actions( |
| 1103 |
self.mirror_menu, |
| 1104 |
( |
| 1105 |
self.create_action( |
| 1106 |
"&Horizontal", slot=self.horizontal_mirror, tip="Horizontal mirror" |
| 1107 |
), |
| 1108 |
self.create_action( |
| 1109 |
"&Vertical", slot=self.vertical_mirror, tip="Vertical mirror" |
| 1110 |
), |
| 1111 |
self.create_action( |
| 1112 |
"&Transposition", slot=self.transposition, tip="Transposition" |
| 1113 |
), |
| 1114 |
), |
| 1115 |
) |
| 1116 |
self.rotation_menu = self.transform_menu.addMenu("&Rotation") |
| 1117 |
self.add_actions( |
| 1118 |
self.rotation_menu, |
| 1119 |
( |
| 1120 |
self.create_action( |
| 1121 |
"+90", slot=self.rotation_90, tip="Rotation of +90 degrees (counter‑clockwise)" |
| 1122 |
), |
| 1123 |
self.create_action( |
| 1124 |
"+180", slot=self.rotation_180, tip="Rotation of +180 degrees (counter‑clockwise)" |
| 1125 |
), |
| 1126 |
self.create_action( |
| 1127 |
"- 90", slot=self.rotation_270, tip="Rotation of -90 degrees (counter‑clockwise)" |
| 1128 |
), |
| 1129 |
), |
| 1130 |
) |
| 1131 |
self.add_actions( |
| 1132 |
self.transform_menu, |
| 1133 |
( |
| 1134 |
self.create_action( |
| 1135 |
"&Mask", slot=self.mask, tip="Import a mask from file and apply it to image(s)" |
| 1136 |
), |
| 1137 |
self.create_action( |
| 1138 |
"&Downsample", |
| 1139 |
slot=self.downsample, |
| 1140 |
tip="Summation over groups of images", |
| 1141 |
), |
| 1142 |
self.create_action( |
| 1143 |
"&Apply transform to the whole data series", |
| 1144 |
slot=self.transformation_options, |
| 1145 |
tip=( |
| 1146 |
"If checked, transformations are applied to the whole data series; " |
| 1147 |
"if unchecked, only the active image is affected" |
| 1148 |
), |
| 1149 |
), |
| 1150 |
), |
| 1151 |
) |
| 1152 |
self.transform_option_action = self.transform_menu.actions()[-1] |
| 1153 |
self.transform_option_action.setCheckable(True) |
| 1154 |
# ---- Options ---- |
| 1155 |
self.options = self.menuBar().addMenu("&Options") |
| 1156 |
self.add_actions( |
| 1157 |
self.options, |
| 1158 |
( |
| 1159 |
self.create_action( |
| 1160 |
"&Counter format", |
| 1161 |
slot=self.set_counter_format_option, |
| 1162 |
tip="Define the format for the counter in multiple saving", |
| 1163 |
), |
| 1164 |
), |
| 1165 |
) |
| 1166 |
# ---- Help ---- |
| 1167 |
self.help_menu = self.menuBar().addMenu("&Help") |
| 1168 |
self.add_actions( |
| 1169 |
self.help_menu, |
| 1170 |
( |
| 1171 |
self.create_action( |
| 1172 |
"&About", slot=self.on_about, shortcut="F1", tip="About FabIO Viewer" |
| 1173 |
), |
| 1174 |
), |
| 1175 |
) |
| 1176 |
def add_actions(self, target, actions): |
| 1177 |
for action in actions: |
| 1178 |
if action is None: |
| 1179 |
target.addSeparator() |
| 1180 |
else: |
| 1181 |
target.addAction(action) |
| 1182 |
def create_action( |
| 1183 |
self, |
| 1184 |
text, |
| 1185 |
slot=None, |
| 1186 |
shortcut=None, |
| 1187 |
icon=None, |
| 1188 |
tip=None, |
| 1189 |
checkable=False, |
| 1190 |
signal="triggered", |
| 1191 |
): |
| 1192 |
action = QAction(text, self) |
| 1193 |
if icon is not None: |
| 1194 |
action.setIcon(QIcon(icon)) |
| 1195 |
if shortcut is not None: |
| 1196 |
action.setShortcut(shortcut) |
| 1197 |
if tip is not None: |
| 1198 |
action.setToolTip(tip) |
| 1199 |
action.setStatusTip(tip) |
| 1200 |
if slot is not None: |
| 1201 |
getattr(action, signal).connect(slot) |
| 1202 |
action.setCheckable(checkable) |
| 1203 |
return action |
| 1204 |
# ---------------------------------------------------------------------- |
| 1205 |
# Main UI layout (tabs, canvases, log, etc.) |
| 1206 |
# ---------------------------------------------------------------------- |
| 1207 |
def create_main_frame(self): |
| 1208 |
self.tabWidget = QTabWidget() |
| 1209 |
tab1 = QWidget() |
| 1210 |
self.tabWidget.addTab(tab1, "View Mode") |
| 1211 |
tab2 = QWidget() |
| 1212 |
self.tabWidget.addTab(tab2, "Batch Mode") |
| 1213 |
# ------------------------------------------------------------------ |
| 1214 |
# Matplotlib canvas (View Mode) |
| 1215 |
# ------------------------------------------------------------------ |
| 1216 |
self.dpi = 100 |
| 1217 |
self.fig = Figure(dpi=self.dpi) |
| 1218 |
self.canvas = FigureCanvasQTAgg(self.fig) |
| 1219 |
self.canvas.setParent(tab1) |
| 1220 |
|
| 1221 |
# Since we have only one plot, we can use add_axes |
| 1222 |
# instead of add_subplot, but then the subplot |
| 1223 |
# configuration tool in the navigation toolbar wouldn't |
| 1224 |
# work. |
| 1225 |
# |
| 1226 |
self.axes = self.fig.add_subplot(111) |
| 1227 |
self.axes.set_visible(False) |
| 1228 |
# Bind the 'pick' event for clicking on one of the bars |
| 1229 |
self.canvas.mpl_connect("motion_notify_event", self.on_pick) |
| 1230 |
|
| 1231 |
# Create the navigation toolbar, tied to the canvas |
| 1232 |
self.mpl_toolbar = NavigationToolbar2QT(self.canvas, tab1, coordinates=False) |
| 1233 |
# ------------------------------------------------------------------ |
| 1234 |
# Header display (left side) |
| 1235 |
# ------------------------------------------------------------------ |
| 1236 |
header_vbox = QVBoxLayout() |
| 1237 |
header_label = QLabel("Header Info:", self) |
| 1238 |
header_vbox.addWidget(header_label) |
| 1239 |
self.headerTextEdit = QPlainTextEdit(tab1) |
| 1240 |
self.headerTextEdit.setReadOnly(True) |
| 1241 |
header_vbox.addWidget(self.headerTextEdit) |
| 1242 |
# ------------------------------------------------------------------ |
| 1243 |
# Image selector & viewer (right side) |
| 1244 |
# ------------------------------------------------------------------ |
| 1245 |
selector_label = QLabel("Active Image:", self) |
| 1246 |
self.images_list = QComboBox(self) |
| 1247 |
self.images_list.textActivated.connect(self.select_new_image) |
| 1248 |
viewer_label = QLabel("Images Viewer:", self) |
| 1249 |
self.pix_coords_label = QLabel( |
| 1250 |
"Pixel coordinates and intensity: x = None , y = None , i = None ", self |
| 1251 |
) |
| 1252 |
self.mpl_toolbar.addWidget(self.pix_coords_label) |
| 1253 |
hbox = QHBoxLayout() |
| 1254 |
hbox.addWidget(selector_label, alignment=qtc.Qt.AlignRight) |
| 1255 |
hbox.addWidget(self.images_list) |
| 1256 |
vbox = QVBoxLayout() |
| 1257 |
vbox.addWidget(viewer_label, alignment=qtc.Qt.AlignVCenter) |
| 1258 |
vbox.addWidget(self.canvas, alignment=qtc.Qt.AlignVCenter) |
| 1259 |
vbox.addWidget(self.mpl_toolbar, alignment=qtc.Qt.AlignVCenter) |
| 1260 |
vbox.addLayout(hbox) |
| 1261 |
left = QWidget() |
| 1262 |
right = QWidget() |
| 1263 |
left.setLayout(header_vbox) |
| 1264 |
right.setLayout(vbox) |
| 1265 |
splitter = QSplitter(qtc.Qt.Horizontal) |
| 1266 |
splitter.addWidget(left) |
| 1267 |
splitter.addWidget(right) |
| 1268 |
main_hbox = QHBoxLayout() |
| 1269 |
main_hbox.addWidget(splitter) |
| 1270 |
tab1.setLayout(main_hbox) |
| 1271 |
# ------------------------------------------------------------------ |
| 1272 |
# Batch Mode UI (tab 2) |
| 1273 |
# ------------------------------------------------------------------ |
| 1274 |
imagelistvbox = QVBoxLayout() |
| 1275 |
imagelistlabel = QLabel("Images List:", tab2) |
| 1276 |
self.imagelistWidget = QListWidget(tab2) |
| 1277 |
import_view_button = QPushButton("Export image to View Mode", tab2) |
| 1278 |
import_view_button.clicked.connect(self.batch_to_view) |
| 1279 |
|
| 1280 |
imagelistvbox.addWidget(imagelistlabel) |
| 1281 |
imagelistvbox.addWidget(self.imagelistWidget) |
| 1282 |
imagelistvbox.addWidget(import_view_button) |
| 1283 |
# File‑mode options |
| 1284 |
optiongroupBox = QGroupBox("File Modes:", tab2) |
| 1285 |
optionbox = QVBoxLayout() |
| 1286 |
self.butttonGroup = QButtonGroup() |
| 1287 |
self.filecheckBox = QCheckBox("Sequential access (for large data series)", tab2) |
| 1288 |
self.filecheckBox.stateChanged.connect(self.sequential_option) |
| 1289 |
self.butttonGroup.addButton(self.filecheckBox) |
| 1290 |
self.filecheckBox2 = QCheckBox( |
| 1291 |
"Direct access (all images are stored in memory simultaneously)", tab2 |
| 1292 |
) |
| 1293 |
self.filecheckBox2.setChecked(True) |
| 1294 |
self.butttonGroup.addButton(self.filecheckBox2) |
| 1295 |
|
| 1296 |
self.butttonGroup.setExclusive(True) |
| 1297 |
|
| 1298 |
optionbox.addWidget(self.filecheckBox) |
| 1299 |
optionbox.addWidget(self.filecheckBox2) |
| 1300 |
|
| 1301 |
optiongroupBox.setLayout(optionbox) |
| 1302 |
# Transformations queue |
| 1303 |
queuegroupBox = QGroupBox("Transformations Queue:", tab2) |
| 1304 |
queuebox = QVBoxLayout() |
| 1305 |
self.transformation_queue = QListWidget(tab2) |
| 1306 |
queuebox.addWidget(self.transformation_queue) |
| 1307 |
clear_trans_list_button = QPushButton("Clear Transformation List", tab2) |
| 1308 |
clear_trans_list_button.clicked.connect(self.clear_transform_list) |
| 1309 |
queuebox.addWidget(clear_trans_list_button) |
| 1310 |
|
| 1311 |
queuegroupBox.setLayout(queuebox) |
| 1312 |
# Log view |
| 1313 |
loggroupBox = QGroupBox("Log View:", tab2) |
| 1314 |
logbox = QHBoxLayout() |
| 1315 |
self.log = QPlainTextEdit() |
| 1316 |
logbox.addWidget(self.log) |
| 1317 |
|
| 1318 |
loggroupBox.setLayout(logbox) |
| 1319 |
# Progress bar |
| 1320 |
self.progressBar = QProgressBar(tab2) |
| 1321 |
self.progressBar.setProperty("value", 0) |
| 1322 |
# Assemble right‑hand side of Batch tab |
| 1323 |
rightsidevbox = QVBoxLayout() |
| 1324 |
rightsidevbox.addWidget(optiongroupBox) |
| 1325 |
rightsidevbox.addWidget(queuegroupBox) |
| 1326 |
rightsidevbox.addWidget(loggroupBox) |
| 1327 |
rightsidevbox.addWidget(self.progressBar) |
| 1328 |
left2 = QWidget() |
| 1329 |
right2 = QWidget() |
| 1330 |
left2.setLayout(imagelistvbox) |
| 1331 |
right2.setLayout(rightsidevbox) |
| 1332 |
splitter2 = QSplitter(qtc.Qt.Horizontal) |
| 1333 |
splitter2.addWidget(left2) |
| 1334 |
splitter2.addWidget(right2) |
| 1335 |
splitter2.setStretchFactor(1, 2) |
| 1336 |
batch_hbox = QHBoxLayout() |
| 1337 |
batch_hbox.addWidget(splitter2) |
| 1338 |
tab2.setLayout(batch_hbox) |
| 1339 |
self.setCentralWidget(self.tabWidget) |
| 1340 |
|
| 1341 |
def create_status_bar(self): |
| 1342 |
self.status_text = QLabel("") |
| 1343 |
self.statusBar().addWidget(self.status_text, 1) |
| 1344 |
self.statusBar().showMessage("Thanks for using FabIO viewer.", 5000) |
| 1345 |
|
| 1346 |
def on_about(self): |
| 1347 |
msg = [ |
| 1348 |
__doc__, |
| 1349 |
"", |
| 1350 |
f"Version: \t\t{__version__}", |
| 1351 |
f"FabIO version: \t{fabio.version}", |
| 1352 |
f"Author: \t\t{__author__}", |
| 1353 |
f"Copyright: \t\t{__copyright__}", |
| 1354 |
f"License: \t\t{__licence__}", |
| 1355 |
] |
| 1356 |
QMessageBox.about(self, "About FabIO Viewer", os.linesep.join(msg)) |
| 1357 |
# ---------------------------------------------------------------------- |
| 1358 |
# Miscellaneous UI helpers |
| 1359 |
# ---------------------------------------------------------------------- |
| 1360 |
def clear_transform_list(self): |
| 1361 |
self.transform_list = [] |
| 1362 |
self.transformation_queue.clear() |
| 1363 |
def transformation_options(self): |
| 1364 |
self.transform_data_series = self.transform_option_action.isChecked() |