-
Notifications
You must be signed in to change notification settings - Fork 2
/
XTChannelArithmetic.py
executable file
·858 lines (732 loc) · 35.8 KB
/
XTChannelArithmetic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
# =========================================================================
#
# Copyright Ziv Yaniv
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# =========================================================================
#
# Channel arithmetic using SimpleITK. Also allows for use of comparitve operators (e.g. >=) to
# create masks for selecting subregions in channels.
# <CustomTools>
# <Menu>
# <Submenu name="SimpleITK Algorithms">
# <Item name="Channel Arithmetic" icon="Python3" tooltip="Perform arithmetic operations using channels.">
# <Command>Python3XT::XTChannelArithmetic(%i)</Command>
# </Item>
# </Submenu>
# </Menu>
# </CustomTools>
import re
import inspect
import traceback
import os
from PySide6.QtWidgets import (
QStackedWidget,
QWidget,
QApplication,
QFileDialog,
QTextEdit,
QLabel,
QPushButton,
QMessageBox,
QLineEdit,
QVBoxLayout,
QHBoxLayout,
QGridLayout,
QCheckBox,
QProgressBar,
QColorDialog,
QScrollArea,
)
from PySide6.QtCore import Qt, QObject, QRunnable, Signal, QThreadPool
import qdarkstyle
import SimpleITK as sitk
import sitk_ims_file_io as sio
import imaris_extension_base as ieb
from help_dialog import HelpDialog
def XTChannelArithmetic(imaris_id=None):
app = QApplication([])
app.setStyle(ieb.style) # Consistent setting of style for all applications
app.setStyleSheet(qdarkstyle.load_stylesheet(qt_api="pyside6"))
channel_arithmetic_dialog = ChannelArithmeticDialog() # noqa: F841
app.exec()
class ChannelArithmeticDialog(ieb.ImarisExtensionBase):
"""
Channel Arithmetic and Beyond
=============================
`View on GitHub <https://github.com/niaid/imaris_extensions>`_
This program enables one to specify arithmetic expressions which are used to
create new channels. The basic arithmetic operations are supported: +,-,*,/,**.
More advanced operations that run short `SimpleITK <https://simpleitk.org/>`_
code snippets are also supported. SimpleITK provides `hundreds of filters
<https://simpleitk.readthedocs.io/en/master/filters.html>`_ that
can be used via this program.
Channels are referenced using square brackets and the channel index, starting
at **zero**. To apply an expression to all channels, use the channel index 'i'.
When creating a single new channel, the arithmetic expression consists of literal
channel numbers, one can select a name and color for the new channel. When
creating multiple new channels, the arithmetic expression is applied to all channels,
the postfix '_modified' is appended to the original channel names and the original
color is copied over. Note that for all channels created by the program the
channel description will include the arithmetic expression used to create that
channel. This transparently supports your efforts to conduct reproducible
research.
Because an Imaris image has a specific pixel type (8, 16, 32 bit unsigned integer
and 32 bit floating point) all computations are performed using a 32 bit floating
point representation and then clamped to the range of the image's pixel type.
The program allows you to use the same expression on multiple files. In this
case literal channel values are limited by the number of shared channels. Thus,
if working with two files one with three channels and one with four channels,
the valid literal channel values are limited to 0, 1, 2. We cannot use 3 as it does not
exist in all files. On the other hand, if our autofluorescence channel is one
of these channels, e.g. channel 0, we can subtract it from all channels in
both files, `[i]-[0]`.
Basic Examples
--------------
#. Multiply channels zero and three:
.. code-block:: Python
[0]*[3]
#. Multiply channels zero and three and subtract the result from channel four:
.. code-block:: Python
[4] - ([0]*[3])
#. Duplicate the second channel (indexes are zero based, so second channel index is 1):
.. code-block:: Python
[1]
#. Duplicate all channels:
.. code-block:: Python
[i]
#. Subtract channel zero from all channels:
.. code-block:: Python
[i]-[0]
#. Invert channel intensity (new intensity values are the result of subtracting the current intensity
value from the maximal possible value):
.. code-block:: Python
sitk.InvertIntensity([1])
#. Threshold channel one using a value of 100, resulting image is binary with
values in {0,1}:
.. code-block:: Python
[1]>100
#. Threshold a specific channel to create a binary result using the Otsu filter,
resulting image is binary with values in {0,1}:
.. code-block:: Python
sitk.OtsuThreshold([1], 0, 1)
#. Gaussian blurring (variance specified in metric units, e.g. nm):
.. code-block:: Python
# blur channel 3 using a Gaussian with variance of 0.245 for x, y and z
sitk.DiscreteGaussian([3], 0.245)
# blur channel 3 using a Gaussian with different variance per x=0.245, y=0.1, z=0.3
sitk.DiscreteGaussian([3], [0.245, 0.1, 0.3])
#. Unsharp masking (variance specified in metric units, e.g. nm). This is actually a filter:
that emphasizes edges, sharpens the image using a blurred version of the image.
.. code-block:: Python
# sharpen channel 0 using an unsharp Gaussian mask with variance of 0.245 and
# sharpening increase of 0.5
[0]+([0]-sitk.DiscreteGaussian([0], 0.245))*0.5
#. Median filtering using a radius of x=1, y=2 and z=3 (radius in which to compute the
median per dimension, pixel units):
.. code-block:: Python
sitk.Median([3], [1, 2, 3])
#. Linearly map the intensity values in the interval [windowMinimum, windowMaximum] to the interval
[outputMinimum, outputMaximum]. Values lower than windowMinimum are mapped to outputMinimum. Values
higher than windowMaximum are mapped to outputMaximum.
.. code-block:: Python
sitk.IntensityWindowing([1], windowMinimum=20, windowMaximum=200, outputMinimum=0, outputMaximum=255)
Advanced Examples
-----------------
#. Threshold a specific channel retaining the values above the threshold and
setting all values equal or lower to zero:
.. code-block:: Python
sitk.Cast([1]>100, sitk.sitkFloat32)*[1]
#. Threshold a specific channel retaining the values above the threshold and
setting all values equal or lower to the threshold to an arbitrary value
(20 in this example):
.. code-block:: Python
sitk.Cast([1]>100, sitk.sitkFloat32)*[1] + sitk.Cast([1]<=100, sitk.sitkFloat32)*20
#. Threshold a specific channel, get all connected components, then
sort the components according to size, discarding those smaller than a minimum
size and create a binary mask corresponding to the largest component, which is
the first label(second largest component label is 2 etc.):
.. code-block:: Python
sitk.RelabelComponent(sitk.ConnectedComponent([1]>100), minimumObjectSize = 50)==1
#. Create a binary mask representing the colocalization of two channels,
intensity values below 20 are considred noise:
.. code-block:: Python
([1]>20)*([2]>20)
#. Create a binary mask representing the colocalization of two channels.
We are interested in all pixels in channel 2 that have a value above 20
and that are less than 1.0um away from pixels in channel 1 that have a value
above 100 (**note**: this operation yields different results when run using
a slice-by-slice approach vs. a volumetric approach):
.. code-block:: Python
(sitk.Cast([2]>20, sitk.sitkFloat32) *
sitk.Abs(sitk.SignedMaurerDistanceMap([1]>100, insideIsPositive=False, squaredDistance=False, useImageSpacing=True)))<=1.0
#. Create a binary mask using thresholding and then perform morphological
closing (dilation followed by erosion) with distance maps, useful
for filling holes:
.. code-block:: Python
sitk.SignedMaurerDistanceMap(sitk.SignedMaurerDistanceMap([1]>100, insideIsPositive=False, squaredDistance=False, useImageSpacing=True) < 1.0, insideIsPositive=False, squaredDistance=False, useImageSpacing=True)<-1.0
#. Create a binary mask using thresholding and then perform morphological
opening (erosion followed by dilation) with distance maps, useful
for removing small islands:
.. code-block:: Python
sitk.SignedMaurerDistanceMap(sitk.SignedMaurerDistanceMap([1]>100, insideIsPositive=False, squaredDistance=False, useImageSpacing=True) < -0.2, insideIsPositive=False, squaredDistance=False, useImageSpacing=True)<0.2
""" # noqa
def __init__(self):
super(ChannelArithmeticDialog, self).__init__()
# Channel indexes in the arithmetic calculator are denoted using a
# regular expression: one or more digits in square brackets (e.g. [1234]).
# First digit is zero and nothing afterwards or first digit is in [1-9] and
# there are possibly more digits afterwards.
# Starting index is zero.
self.channel_pattern = re.compile(r"\[(0|[1-9]\d*)\]")
# Use QT's global threadpool, documentation says: "This global thread pool
# automatically maintains an optimal number of threads based on the
# number of cores in the CPU."
self.threadpool = QThreadPool.globalInstance()
# Configure the help dialog.
self.help_dialog = HelpDialog(w=700, h=500)
self.help_dialog.setWindowTitle("Channel Arithmetic Help")
self.help_dialog.set_rst_text(
inspect.getdoc(self), pygments_css_file_name="pygments_dark.css"
)
self.__create_gui()
self.setWindowTitle("Channel Arithmetic")
self.processing_error = False
self.show()
def __create_gui(self):
menu_bar = self.menuBar()
# Force menubar to be displayed in the application on OSX/Linux, otherwise it
# is displayed in the system menubar
menu_bar.setNativeMenuBar(False)
self.help_button = QPushButton("Help")
self.help_button.clicked.connect(self.help_dialog.show)
menu_bar.setCornerWidget(self.help_button, Qt.TopLeftCorner)
central_widget = QWidget(self)
gui_layout = QVBoxLayout()
central_widget.setLayout(gui_layout)
self.setCentralWidget(central_widget)
select_files_widget = self.__create_select_files_widget()
arithmetic_widget = self.__create_arithmetic_widget()
self.stack = QStackedWidget(self)
self.stack.addWidget(select_files_widget)
self.stack.addWidget(arithmetic_widget)
gui_layout.addWidget(self.stack)
self.status_bar = self.statusBar()
def closeEvent(self, event):
"""
Override the closeEvent method so that clicking the 'x' button also
closes all of the dialogs.
"""
self.help_dialog.close()
event.accept()
def __create_arithmetic_widget(self):
wid = QWidget(self)
arithmetic_layout = QVBoxLayout()
wid.setLayout(arithmetic_layout)
self.valid_indexes_label = QLabel("")
arithmetic_layout.addWidget(self.valid_indexes_label)
layout = QHBoxLayout()
layout.setAlignment(Qt.AlignLeft)
layout.addWidget(QLabel("Enter new channel arithmetic expression:"))
arithmetic_layout.addLayout(layout)
self.arithmetic_expression_text_edit = QTextEdit()
arithmetic_layout.addWidget(self.arithmetic_expression_text_edit)
self.slice_by_slice_checkbox = QCheckBox(
"Slice by slice (smaller memory footprint)."
)
arithmetic_layout.addWidget(self.slice_by_slice_checkbox)
layout = QHBoxLayout()
layout.addWidget(QLabel("New channel name:"))
self.new_channel_name_line_edit = QLineEdit()
layout.addWidget(self.new_channel_name_line_edit)
arithmetic_layout.addLayout(layout)
layout = QHBoxLayout()
layout.addWidget(QLabel("New channel color:"))
self.new_channel_color_button = QPushButton()
self.new_channel_color_button.clicked.connect(self.__select_color_callback)
layout.addWidget(self.new_channel_color_button)
arithmetic_layout.addLayout(layout)
self.apply_button = QPushButton("Apply")
self.apply_button.clicked.connect(self.__channel_arithmetic_wrapper)
arithmetic_layout.addWidget(self.apply_button)
progress_wid = QWidget()
self.progress_grid_layout = QGridLayout()
progress_wid.setLayout(self.progress_grid_layout)
scroll_area = QScrollArea()
scroll_area.setWidget(progress_wid)
scroll_area.setWidgetResizable(True)
arithmetic_layout.addWidget(scroll_area)
layout = QHBoxLayout()
layout.setAlignment(Qt.AlignLeft)
self.processing_prev_button = QPushButton("Prev")
self.processing_prev_button.clicked.connect(
lambda: self.stack.setCurrentIndex(0)
)
layout.addWidget(self.processing_prev_button)
arithmetic_layout.addLayout(layout)
return wid
def __configure_and_show_arithmetic_widget(self):
file_names = self.input_files_edit.toPlainText().split("\n")
num_channels = []
problematic_images = []
for file_name in file_names:
try:
meta_data = sio.read_metadata(file_name)
num_channels.append(len(meta_data["channels_information"]))
except Exception:
problematic_images.append(file_name)
if problematic_images:
self._error_function(
"Problem encountered reading the following file(s):\n"
+ "\n".join(problematic_images)
)
return
self.max_channel_index = min(num_channels) - 1
self.valid_indexes_label.setText(
f"Valid channel indexes: 0...{self.max_channel_index}, i"
)
self.arithmetic_expression_text_edit.clear()
self.slice_by_slice_checkbox.setChecked(False)
self.new_channel_name_line_edit.clear()
# Remove all widgets from layout, done in reverse order because
# removing from the begining shifts the rest of the items
for i in reversed(range(self.progress_grid_layout.count())):
self.progress_grid_layout.itemAt(i).widget().setParent(None)
for i, file_name in enumerate(file_names):
self.progress_grid_layout.addWidget(
QLabel(os.path.basename(file_name)), i, 0
)
progress_bar = QProgressBar()
progress_bar.setMaximum(100)
self.progress_grid_layout.addWidget(progress_bar, i, 1)
self.stack.setCurrentIndex(1)
def __create_select_files_widget(self):
wid = QWidget()
input_layout = QVBoxLayout()
wid.setLayout(input_layout)
layout = QHBoxLayout()
layout.addWidget(QLabel("File names:"))
layout.setAlignment(Qt.AlignLeft)
button = QPushButton("Browse")
button.setToolTip("Select input files for arithmetic operation.")
button.clicked.connect(self.__browse_select_input_callback)
layout.addWidget(button)
input_layout.addLayout(layout)
self.input_files_edit = QTextEdit()
self.input_files_edit.setReadOnly(True)
input_layout.addWidget(self.input_files_edit)
layout = QHBoxLayout()
layout.setAlignment(Qt.AlignRight)
self.input_files_next_button = QPushButton("Next")
self.input_files_next_button.setEnabled(False)
self.input_files_next_button.clicked.connect(
self.__configure_and_show_arithmetic_widget
)
layout.addWidget(self.input_files_next_button)
input_layout.addLayout(layout)
return wid
def __browse_select_input_callback(self):
file_names, _ = QFileDialog.getOpenFileNames(
self,
"QFileDialog.getOpenFileNames()",
"",
"Imaris Images (*.ims);;All Files (*)",
)
if file_names:
self.input_files_edit.setText("\n".join(file_names))
self.input_files_next_button.setEnabled(True)
def __select_color_callback(self):
color = QColorDialog.getColor()
if color.isValid():
self.new_channel_color_button.setStyleSheet(
f"background-color :rgb({color.red()},{color.green()},{color.blue()})"
)
def __channel_arithmetic_wrapper(self):
# Get the arithmetic expression after removing all whitespace
arithmetic_expression = "".join(
self.arithmetic_expression_text_edit.toPlainText().split()
)
color = self.new_channel_color_button.palette().button().color()
if arithmetic_expression:
# Get the explicit channel indexes that appear in the expression and
# check that they are in the valid range.
channel_indexes = re.findall(self.channel_pattern, arithmetic_expression)
invalid_channels = [
ci
for ci in channel_indexes
if int(ci) not in range(self.max_channel_index + 1)
]
if invalid_channels:
self._error_function(
"The following channels specified in the arithmetic expression"
+ f" are outside the valid range [0,{self.max_channel_index}]: "
+ ", ".join(invalid_channels)
)
return
# Disable the UI interaction during computation
self.arithmetic_expression_text_edit.setReadOnly(True)
self.slice_by_slice_checkbox.setEnabled(False)
self.new_channel_name_line_edit.setReadOnly(True)
self.new_channel_color_button.setEnabled(False)
self.apply_button.setEnabled(False)
self.processing_prev_button.setEnabled(False)
QApplication.setOverrideCursor(Qt.WaitCursor)
file_names = self.input_files_edit.toPlainText().split("\n")
self.num_threads_left = len(file_names)
for i, input_file_name in enumerate(file_names):
# Configure and perform computation in another thread.
arithmetic_calculator = ArithmeticCalculator(self.channel_pattern)
arithmetic_calculator.signals.finished.connect(
self.__arithmetic_finished
)
arithmetic_calculator.signals.processing_error.connect(
self._processing_error_function
)
arithmetic_calculator.signals.progress_signal.connect(
self.progress_grid_layout.itemAtPosition(i, 1).widget().setValue
)
arithmetic_calculator.signals.update_state_signal.connect(
self.status_bar.showMessage
)
arithmetic_calculator.input_file_name = input_file_name
arithmetic_calculator.arithmetic_expression = arithmetic_expression
arithmetic_calculator.new_channel_color = [
color.red() / 255.0,
color.green() / 255.0,
color.blue() / 255.0,
]
arithmetic_calculator.new_channel_alpha = color.alpha() / 255.0
arithmetic_calculator.new_channel_name = (
self.new_channel_name_line_edit.text().strip()
)
arithmetic_calculator.slice_by_slice = (
self.slice_by_slice_checkbox.isChecked()
)
self.threadpool.start(arithmetic_calculator)
else:
self._error_function("No action taken: arithmetic expression not set.")
def __arithmetic_finished(self):
self.num_threads_left = self.num_threads_left - 1
if self.num_threads_left == 0:
QApplication.restoreOverrideCursor()
self.status_bar.clearMessage()
for i in range(self.progress_grid_layout.rowCount()):
self.progress_grid_layout.itemAtPosition(i, 1).widget().setValue(0)
# Enable the UI interaction after computation
self.arithmetic_expression_text_edit.setReadOnly(False)
self.slice_by_slice_checkbox.setEnabled(True)
self.new_channel_name_line_edit.setReadOnly(False)
self.new_channel_color_button.setEnabled(True)
self.apply_button.setEnabled(True)
self.processing_prev_button.setEnabled(True)
# Inform the user that the calculations completed. If processing errors
# occured then the desired operation may not have happened, but the
# calculation was completed.
QMessageBox().information(self, "Message", "Calculation completed.")
self.processing_error = False
class ArithmeticCalculatorSignals(QObject):
progress_signal = Signal(int)
update_state_signal = Signal(str)
processing_error = Signal(str)
finished = Signal()
class ArithmeticCalculator(QRunnable):
def __init__(self, channel_pattern):
super(ArithmeticCalculator, self).__init__()
self.channel_pattern = channel_pattern
self.signals = ArithmeticCalculatorSignals()
def reset(self):
self.input_file_name = ""
# The arithmetic expression is expected to contain no white space. It is
# the callers responsibility to ensure this.
self.arithmetic_expression = ""
self.new_channel_color = None
self.new_channel_alpha = None
self.new_channel_name = ""
self.slice_by_slice = None
def run(self):
try:
if self.slice_by_slice:
self.process_slice_by_slice()
else:
self.process_vol_by_vol()
self.signals.finished.emit()
# Use the stack trace as the error message to provide enough
# detailes for debugging.
except Exception:
self.signals.processing_error.emit(
"Exception occurred during computation:\n" + traceback.format_exc()
)
self.signals.finished.emit()
def process_vol_by_vol(self):
meta_data = sio.read_metadata(self.input_file_name)
message_fname = os.path.basename(self.input_file_name)
# Read a single pixel image to get the original pixel type
original_pixel_type = sio.read(
self.input_file_name,
channel_index=0,
sub_ranges=[range(0, 1), range(0, 1), range(0, 1)],
).GetPixelID()
using_all_channels = False
# Expression is applied to all channels.
if "[i]" in self.arithmetic_expression:
using_all_channels = True
total_work = len(meta_data["times"]) * (
len(meta_data["channels_information"]) if using_all_channels else 1
)
# Channel meta data.
channel_description = (
"SimpleITK generated channel from arithmetic expression: "
+ self.arithmetic_expression
)
channel_info = {}
channel_info["color"] = self.new_channel_color
channel_info["name"] = (
self.new_channel_name if self.new_channel_name else " "
) # Name has to have a value no matter what, so set to space
channel_info["range"] = [0, 255]
channel_info["alpha"] = self.new_channel_alpha
if using_all_channels:
time_entries = len(meta_data["times"])
for i in range(len(meta_data["channels_information"])):
for time_index in range(time_entries):
self.signals.update_state_signal.emit(
f"Evaluating arithmetic expression ({message_fname})..."
)
read_float32_command_str = (
f'sitk.Cast(sio.read(file_name="{self.input_file_name}", time_index={time_index}, resolution_index=0, ' # noqa: E501
+ "channel_index=\\1), sitk.sitkFloat32)"
)
read_float32_command_str_any_channel = (
f'sitk.Cast(sio.read(file_name="{self.input_file_name}", time_index={time_index}, resolution_index=0, ' # noqa: E501
+ f"channel_index={i}), sitk.sitkFloat32)"
)
new_channel = sitk.Clamp(
eval(
self.channel_pattern.sub(
read_float32_command_str, self.arithmetic_expression
).replace("[i]", read_float32_command_str_any_channel)
),
original_pixel_type,
)
self.signals.progress_signal.emit(
int(100 * (i * time_entries + time_index + 1) / total_work)
)
channel_info["name"] = (
meta_data["channels_information"][i][1]["name"] + "_modified"
)
channel_info["description"] = channel_description + f", i={i}"
if "color" in meta_data["channels_information"][i][1]:
channel_info["color"] = meta_data["channels_information"][i][1][
"color"
]
elif "color_table" in meta_data["channels_information"][i][1]:
channel_info["color_table"] = meta_data["channels_information"][
i
][1]["color_table"]
if "color" in channel_info:
del channel_info["color"]
channel_info["range"] = meta_data["channels_information"][i][1][
"range"
]
channel_info["alpha"] = meta_data["channels_information"][i][1][
"alpha"
]
new_channel.SetMetaData(
sio.channels_metadata_key,
sio.channels_information_list2xmlstr([(0, channel_info)]),
)
self.signals.update_state_signal.emit(
f"Saving channel ({message_fname})..."
)
sio.append_channels(
new_channel, self.input_file_name, time_index=time_index
)
else:
channel_info["description"] = channel_description
for time_index in range(len(meta_data["times"])):
self.signals.update_state_signal.emit(
f"Evaluating arithmetic expression ({message_fname})..."
)
read_float32_command_str = (
f'sitk.Cast(sio.read(file_name="{self.input_file_name}", time_index={time_index}, resolution_index=0, ' # noqa: E501
+ "channel_index=\\1), sitk.sitkFloat32)"
)
new_channel = sitk.Clamp(
eval(
self.channel_pattern.sub(
read_float32_command_str, self.arithmetic_expression
)
),
original_pixel_type,
)
self.signals.progress_signal.emit(
int(100 * (time_index + 1) / total_work)
)
new_channel.SetMetaData(
sio.channels_metadata_key,
sio.channels_information_list2xmlstr([(0, channel_info)]),
)
self.signals.update_state_signal.emit(
f"Saving channel ({message_fname})..."
)
sio.append_channels(
new_channel, self.input_file_name, time_index=time_index
)
def process_slice_by_slice(self):
meta_data = sio.read_metadata(self.input_file_name)
message_fname = os.path.basename(self.input_file_name)
# Read a single pixel image to get the original pixel type
original_pixel_type = sio.read(
self.input_file_name,
channel_index=0,
sub_ranges=[range(0, 1), range(0, 1), range(0, 1)],
).GetPixelID()
using_all_channels = False
# Expression is applied to all channels.
if "[i]" in self.arithmetic_expression:
using_all_channels = True
img_size = meta_data["sizes"][0]
total_work = (
len(meta_data["times"])
* img_size[2]
* (len(meta_data["channels_information"]) if using_all_channels else 1)
)
# Channel meta data.
channel_description = (
"SimpleITK generated channel from arithmetic expression: "
+ self.arithmetic_expression
)
channel_info = {}
channel_info["color"] = self.new_channel_color
channel_info["name"] = (
self.new_channel_name if self.new_channel_name else " "
) # Name has to have a value no matter what, so set to space
channel_info["range"] = [0, 255]
channel_info["alpha"] = self.new_channel_alpha
if using_all_channels:
time_entries = len(meta_data["times"])
slice_entries = img_size[2]
for i in range(len(meta_data["channels_information"])):
for time_index in range(time_entries):
self.signals.update_state_signal.emit(
f"Evaluating arithmetic expression ({message_fname})..."
)
z_slices = []
for z_index in range(slice_entries):
read_float32_command_str = (
f'sitk.Cast(sio.read(file_name="{self.input_file_name}", time_index={time_index}, resolution_index=0, ' # noqa: E501
+ f"channel_index=\\1, sub_ranges=[range(0,{img_size[0]}), range(0,{img_size[1]}), range({z_index},{z_index+1})])[:,:,0], sitk.sitkFloat32)" # noqa: E501
)
read_float32_command_str_any_channel = (
f'sitk.Cast(sio.read(file_name="{self.input_file_name}", time_index={time_index}, resolution_index=0, ' # noqa: E501
+ f"channel_index={i}, sub_ranges=[range(0,{img_size[0]}), range(0,{img_size[1]}), range({z_index},{z_index+1})])[:,:,0], sitk.sitkFloat32)" # noqa: E501
)
z_slices.append(
sitk.Clamp(
eval(
self.channel_pattern.sub(
read_float32_command_str,
self.arithmetic_expression,
).replace(
"[i]", read_float32_command_str_any_channel
)
),
original_pixel_type,
)
)
self.signals.progress_signal.emit(
int(
100
* (
i * time_entries * slice_entries
+ time_index * slice_entries
+ z_index
+ 1
)
/ total_work
)
)
new_channel = sitk.JoinSeries(z_slices)
new_channel.SetOrigin(meta_data["origin"])
new_channel.SetSpacing(meta_data["spacings"][0])
channel_info["name"] = (
meta_data["channels_information"][i][1]["name"] + "_modified"
)
channel_info["description"] = channel_description + f", i={i}"
if "color" in meta_data["channels_information"][i][1]:
channel_info["color"] = meta_data["channels_information"][i][1][
"color"
]
elif "color_table" in meta_data["channels_information"][i][1]:
channel_info["color_table"] = meta_data["channels_information"][
i
][1]["color_table"]
if "color" in channel_info:
del channel_info["color"]
channel_info["range"] = meta_data["channels_information"][i][1][
"range"
]
channel_info["alpha"] = meta_data["channels_information"][i][1][
"alpha"
]
new_channel.SetMetaData(
sio.channels_metadata_key,
sio.channels_information_list2xmlstr([(0, channel_info)]),
)
self.signals.update_state_signal.emit(
f"Saving channel ({message_fname})..."
)
sio.append_channels(
new_channel, self.input_file_name, time_index=time_index
)
else:
channel_info["description"] = channel_description
for time_index in range(len(meta_data["times"])):
self.signals.update_state_signal.emit(
f"Evaluating arithmetic expression ({message_fname})..."
)
z_slices = []
for z_index in range(img_size[2]):
read_float32_command_str = (
f'sitk.Cast(sio.read(file_name="{self.input_file_name}", time_index={time_index}, resolution_index=0, ' # noqa: E501
+ f"channel_index=\\1, sub_ranges=[range(0,{img_size[0]}), range(0,{img_size[1]}), range({z_index},{z_index+1})])[:,:,0], sitk.sitkFloat32)" # noqa: E501
)
z_slices.append(
sitk.Clamp(
eval(
self.channel_pattern.sub(
read_float32_command_str, self.arithmetic_expression
)
),
original_pixel_type,
)
)
self.signals.progress_signal.emit(
int((100 * time_index * img_size[2] + z_index + 1) / total_work)
)
new_channel = sitk.JoinSeries(z_slices)
new_channel.SetOrigin(meta_data["origin"])
new_channel.SetSpacing(meta_data["spacings"][0])
new_channel.SetMetaData(
sio.channels_metadata_key,
sio.channels_information_list2xmlstr([(0, channel_info)]),
)
self.signals.update_state_signal.emit(
f"Saving channel ({message_fname})..."
)
sio.append_channels(
new_channel, self.input_file_name, time_index=time_index
)
if __name__ == "__main__":
XTChannelArithmetic()