Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
12 kB
1
Indexable
Never
import numpy as np
import h5py
import cv2 as cv
import algotom.rec.reconstruction as rec
import algotom.prep.filtering as filt
import h5py
import pvaccess as pva


channel_name = 'test'
import os
from superqt import QLabeledRangeSlider, QLabeledDoubleSlider, QLabeledSlider

#filepath2x = r"/raid-ssd/CT/2024/2024_03/Markoetter/W-sphere/00009_W-sphere_8___Z155_Y3500_35000eV_2x_100ms/240326_111109_00001.h5"
#filepath10x = r"/raid-ssd/CT/2024/2024_03/Markoetter/W-sphere/00011_W-sphere_8___Z155_Y3500_35000eV_10x_100ms/240326_112852_00001.h5"
#file = h5py.File(filepath2x,'r')

import sys

from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QSlider, QGridLayout, QWidget, QLabel, QFileDialog, QCheckBox, QDoubleSpinBox, QSpinBox
from PyQt5 import QtCore, QtGui

# Subclass QMainWindow to customize your application's main window
class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.setWindowTitle("My App")
        layout = QGridLayout()

        self.load_button = QPushButton("Load hdf5")
        self.usedprojslider = QLabeledRangeSlider(QtCore.Qt.Orientation.Horizontal)
        self.usedprojslider.setMinimum(0)

        self.labelproj = QLabel('Proj')
        self.usedFFslider = QLabeledRangeSlider(QtCore.Qt.Orientation.Horizontal)
        self.usedFFslider.setMinimum(0)

        self.usedFFslider.setValue((0,20))
        self.labelFF = QLabel('FF')
        self.CORslider = QLabeledDoubleSlider(QtCore.Qt.Orientation.Horizontal)
        self.CORslider.setMaximum(2000)
        self.CORslider.setValue(1247)
        self.labelCOR = QLabel('COR')
        self.sliceslider = QLabeledSlider(QtCore.Qt.Orientation.Vertical)

        self.filtering_checkbox = QCheckBox("Fresnel filter")
        self.filtering_ratio = QDoubleSpinBox()
        self.filtering_ratio.setMaximum(1)
        self.filtering_ratio.setMinimum(0)

        self.downsample_checkbox = QCheckBox("Down-sampling")
        self.downsample_slider = QLabeledRangeSlider(QtCore.Qt.Orientation.Horizontal)
        self.downsample_spinbox_high = QSpinBox()
        self.labeldown_high = QLabel('High sampling')
        self.downsample_spinbox_low = QSpinBox()
        self.labeldown_low = QLabel('Low sampling')





        # Set the central widget of the Window.
        self.button = QPushButton("Send Reco")

        layout.addWidget(self.usedprojslider,0,0)
        layout.addWidget(self.labelproj,0,1)
        layout.addWidget(self.usedFFslider,1,0)
        layout.addWidget(self.labelFF, 1, 1)
        layout.addWidget(self.CORslider,2,0)
        layout.addWidget(self.labelCOR, 2, 1)
        layout.addWidget(self.button,3,0)
        layout.addWidget(self.load_button,3,1)
        layout.addWidget(self.sliceslider,4,0,1,2)
        layout.addWidget(self.filtering_checkbox,5,0)
        layout.addWidget(self.filtering_ratio,5,1)
        layout.addWidget(self.downsample_checkbox,6,0)
        layout.addWidget(self.downsample_slider,7,0,1,2)
        layout.addWidget(self.labeldown_high, 8, 0)
        layout.addWidget(self.downsample_spinbox_high, 8, 1)
        layout.addWidget(self.labeldown_low, 8, 2)
        layout.addWidget(self.downsample_spinbox_low, 8,3)



        self.filtering_ratio.hide()
        self.downsample_slider.hide()
        self.labeldown_low.hide()
        self.labeldown_high.hide()
        self.downsample_spinbox_high.hide()
        self.downsample_spinbox_low.hide()

        widget = QWidget()

        widget.setLayout(layout)
        self.setCentralWidget(widget)

        pva_image_dict = {'value': ({'booleanValue': [pva.pvaccess.ScalarType.BOOLEAN], 'byteValue':
            [pva.pvaccess.ScalarType.BYTE], 'shortValue': [pva.pvaccess.ScalarType.SHORT], 'intValue':
                                         [pva.pvaccess.ScalarType.INT], 'longValue': [pva.pvaccess.ScalarType.LONG],
                                     'ubyteValue':
                                         [pva.pvaccess.ScalarType.UBYTE],
                                     'ushortValue': [pva.pvaccess.ScalarType.USHORT], 'uintValue':
                                         [pva.pvaccess.ScalarType.UINT], 'ulongValue': [pva.pvaccess.ScalarType.ULONG],
                                     'floatValue':
                                         [pva.pvaccess.ScalarType.FLOAT],
                                     'doubleValue': [pva.pvaccess.ScalarType.DOUBLE]},), 'codec':
                              {'name': pva.pvaccess.ScalarType.STRING, 'parameters': ()}, 'compressedSize':
                              pva.pvaccess.ScalarType.LONG, 'uncompressedSize': pva.pvaccess.ScalarType.LONG,
                          'dimension':
                              [{'size': pva.pvaccess.ScalarType.INT, 'offset': pva.pvaccess.ScalarType.INT, 'fullSize':
                                  pva.pvaccess.ScalarType.INT, 'binning': pva.pvaccess.ScalarType.INT, 'reverse':
                                    pva.pvaccess.ScalarType.BOOLEAN}], 'uniqueId': pva.pvaccess.ScalarType.INT,
                          'dataTimeStamp':
                              {'secondsPastEpoch': pva.pvaccess.ScalarType.LONG,
                               'nanoseconds': pva.pvaccess.ScalarType.INT,
                               'userTag': pva.pvaccess.ScalarType.INT}, 'attribute':
                              [{'name': pva.pvaccess.ScalarType.STRING, 'value': (),
                                'descriptor': pva.pvaccess.ScalarType.STRING,
                                'sourceType': pva.pvaccess.ScalarType.INT, 'source': pva.pvaccess.ScalarType.STRING}],
                          'descriptor':
                              pva.pvaccess.ScalarType.STRING,
                          'alarm': {'severity': pva.pvaccess.ScalarType.INT, 'status':
                              pva.pvaccess.ScalarType.INT, 'message': pva.pvaccess.ScalarType.STRING}, 'timeStamp':
                              {'secondsPastEpoch': pva.pvaccess.ScalarType.LONG,
                               'nanoseconds': pva.pvaccess.ScalarType.INT, 'userTag':
                                   pva.pvaccess.ScalarType.INT},
                          'display': {'limitLow': pva.pvaccess.ScalarType.DOUBLE, 'limitHigh':
                              pva.pvaccess.ScalarType.DOUBLE, 'description': pva.pvaccess.ScalarType.STRING, 'format':
                                          pva.pvaccess.ScalarType.STRING, 'units': pva.pvaccess.ScalarType.STRING}}

        self.pv_rec = pva.PvObject(pva_image_dict)
        self.pvaServer = pva.PvaServer(channel_name, self.pv_rec)
        self.pvaServer.start()
        self.new = True
        self.usedprojslider.valueChanged.connect(self.slice_changed)
        self.usedFFslider.valueChanged.connect(self.slice_changed)
        self.button.clicked.connect(self.send_reco)
        self.sliceslider.valueChanged.connect(self.slice_changed)
        self.load_button.clicked.connect(self.load_file)
        self.filtering_checkbox.toggled.connect(self.filter_show)
        self.downsample_checkbox.toggled.connect(self.downsample_show)
        self.downsample_slider.valueChanged.connect(self.slice_changed)


    def load_file(self):
        path_klick = QFileDialog.getOpenFileName(self, 'Select hdf5-file, please.', r"/raid-ssd/CT/2024/2024_03/Markoetter/ISM_F/")
        if path_klick[0]:
            print(path_klick[0])
            self.file = h5py.File(path_klick[0],'r')
            self.usedprojslider.setMaximum(self.file['entry/data/data'].shape[0])
            self.usedprojslider.setValue((80, self.file['entry/data/data'].shape[0]))
            self.usedFFslider.setMaximum(self.file['entry/data/data'].shape[0])
            self.sliceslider.setMaximum(self.file['entry/data/data'].shape[1])
            print('file loaded')
        else:
            print("User cancelled the dialog.")

    def filter_show(self):
        if self.filtering_checkbox.isChecked():
            self.filtering_ratio.show()
        else:
            self.filtering_ratio.hide()


    def downsample_show(self):
        if self.downsample_checkbox.isChecked():
            self.downsample_slider.show()
            self.labeldown_low.show()
            self.labeldown_high.show()
            self.downsample_spinbox_high.show()
            self.downsample_spinbox_low.show()
        else:
            self.downsample_slider.hide()
            self.labeldown_low.hide()
            self.labeldown_high.hide()
            self.downsample_spinbox_high.hide()
            self.downsample_spinbox_low.hide()

    def slice_changed(self):
        self.new = True
        self.downsample_slider.setMinimum(self.usedprojslider.value()[0])
        self.downsample_slider.setMaximum(self.usedprojslider.value()[1])

    def create_indices_list(self):
        self.selected_indices = []

        for i in range(self.usedprojslider.value()[0], self.usedprojslider.value()[1] + 1):
            if self.downsample_slider.value()[0] <= i <= self.downsample_slider.value()[1]:
                # Inside slider values, select every xth index
                if (i - self.downsample_slider.value()[0]) % self.downsample_spinbox_high.value() == 0:
                    self.selected_indices.append(i)
            else:
                # Outside slider values, select every yth index
                if i < self.downsample_slider.value()[0]:
                    if (self.downsample_slider.value()[0] - i) % self.downsample_spinbox_low.value()  == 0:
                        self.selected_indices.append(i)
                else:
                    if (i - self.downsample_slider.value()[1]) % self.downsample_spinbox_low.value()  == 0:
                        self.selected_indices.append(i)

    def send_reco(self):
        if self.new:
            print('loading new data')
            if self.downsample_checkbox.isChecked():
                print('using downsampling')
                self.create_indices_list()
                self.proj = self.file['entry/data/data'][self.selected_indices,
                            self.sliceslider.value(), :]
                self.angles = np.radians(self.file['entry/instrument/NDAttributes/SAMPLE_MICOS_W2'][self.selected_indices])
                print('using ', self.angles.shape, ' angles')
            else:
                print('loading everything')
                self.proj = self.file['entry/data/data'][self.usedprojslider.value()[0]:self.usedprojslider.value()[1],
                        self.sliceslider.value(), :]
                self.angles = np.radians(self.file['entry/instrument/NDAttributes/SAMPLE_MICOS_W2']
                           [self.usedprojslider.value()[0]:self.usedprojslider.value()[1]])
                print('using ', self.angles.shape, ' angles')
            self.ff = np.mean(self.file['entry/data/data'][self.usedFFslider.value()[0]:self.usedFFslider.value()[1],self.sliceslider.value(),:],axis=0)
            self.norm = np.divide(self.proj,self.ff)
            self.new = False

        if self.filtering_checkbox.isChecked():
            print('filtering')
            self.norm_final = filt.fresnel_filter(self.norm, self.filtering_ratio.value(), dim=1)
        else:
            self.norm_final = self.norm

        print('reconstructing')
        self.original_reconstruction = rec.fbp_reconstruction(self.norm_final, self.CORslider.value(),angles=self.angles,
                                                              gpu=True,
                                                              apply_log=True)
        self.pv_rec['dimension'] = [
            {'size': self.original_reconstruction.shape[1], 'fullSize': self.original_reconstruction.shape[1], 'binning': 1},
            {'size': self.original_reconstruction.shape[0], 'fullSize': self.original_reconstruction.shape[0], 'binning': 1}]
        self.pv_rec['value'] = ({'floatValue': self.original_reconstruction.flatten()},)
        print('reco done')


app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec()
Leave a Comment