Skip to content

Commit

Permalink
Merge branch 'raw_input_preview' of github.com:paskino/CILViewer into…
Browse files Browse the repository at this point in the history
… raw_input_preview
  • Loading branch information
paskino committed Apr 15, 2024
2 parents daa4fce + 38801bd commit cc854ba
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 58 deletions.
133 changes: 77 additions & 56 deletions Wrappers/Python/ccpi/viewer/ui/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import tempfile
import logging

logger = logging.getLogger(__name__)

class ViewerSettingsDialog(AppSettingsDialog):
''' This is a dialog window which allows the user to set:
Expand Down Expand Up @@ -76,13 +77,12 @@ class RawInputDialog(FormDialog):
>>> dialog.setSupportedTypes(['float32', 'float64'])
'''

supported_types = [ np.dtype(f) for f in ['int8', 'uint8', 'int16', 'uint16', \
'float16', 'float32', 'float64'] ]
supported_types = [np.dtype(f) for f in Converter.dtype_name_to_vtkType.keys()]

def __init__(self, parent, fname):
super(RawInputDialog, self).__init__(parent, fname)
self.setFileName(fname)
self.setWindowFlag(QtCore.Qt.WindowContextHelpButtonHint, False)
fw = self.formWidget

# dimensionality:
Expand All @@ -108,7 +108,7 @@ def __init__(self, parent, fname):
self.supported_types = RawInputDialog.supported_types.copy()
dtypeLabel = QLabel("Data Type")
dtypeValue = QComboBox()
dtypeValue.addItems([np.dtype(dt).name for dt in self.supported_types])
dtypeValue.addItems([dt.name for dt in self.supported_types])
dtypeValue.setCurrentIndex(1)
fw.addWidget(dtypeValue, dtypeLabel, 'dtype')

Expand Down Expand Up @@ -138,7 +138,7 @@ def __init__(self, parent, fname):
self.Cancel.clicked.connect(self.close)

def setFileName(self, filename):
'''Set the filename used in the dialog'''
'''Set the filename used in the dialog and the dialog title.'''
self.fname = os.path.abspath(filename)
title = "Config for " + os.path.basename(filename)
self.setWindowTitle(title)
Expand All @@ -154,7 +154,7 @@ def setSupportedTypes(self, types):
'''
self.supported_types = types
self.getWidget('dtype').clear()
self.getWidget('dtype').addItems([np.dtype(dt).name for dt in self.supported_types])
self.getWidget('dtype').addItems([dt.name for dt in self.supported_types])

def getRawAttrs(self):
'''
Expand Down Expand Up @@ -210,8 +210,10 @@ def preview(self):
if dimensionality == 3:
if isFortran:
shape = (dimX, dimY, dimZ)
central_slice = 'z'
else:
shape = (dimZ, dimY, dimX)
central_slice = 'x'

# Construct a data type
dt = np.dtype(typecode)
Expand Down Expand Up @@ -253,60 +255,68 @@ def preview(self):
slice_size = shape[1]*shape[0]
offset = shape[2]*slice_size//2

# use the cilRawCroppedReader to read the slice
reader2 = cilRawCroppedReader()
reader2.SetFileName(self.fname)
reader2.SetTargetZExtent((shape[2]//2, shape[2]//2))
reader2.SetBigEndian(isBigEndian)
reader2.SetIsFortran(isFortran)
reader2.SetTypeCodeName(dt.name)
reader2.SetStoredArrayShape(shape)
reader2.Update()
# image = reader2.GetOutput()

# rawfname = os.path.join(tempfile.gettempdir(),"test.raw")

# offset = offset * bytes_per_element
# slices_to_read = 1
# if shape[2] > 1:
# slices_to_read = 2
# with open(self.fname, 'br') as f:
# f.seek(offset)
# raw_data = f.read(slice_size*bytes_per_element* slices_to_read)
# with open(rawfname, 'wb') as f2:
# f2.write(raw_data)

# reader2 = vtk.vtkImageReader2()
# reader2.SetFileName(rawfname)

# vtktype = Converter.dtype_name_to_vtkType[dt.name]
# reader2.SetDataScalarType(vtktype)

# if isBigEndian:
# reader2.SetDataByteOrderToBigEndian()
# else:
# reader2.SetDataByteOrderToLittleEndian()

# reader2.SetFileDimensionality(len(shape))
# vtkshape = shape[:]
# if not isFortran:
# # need to reverse the shape (again)
# vtkshape = shape[::-1]
# # vtkshape = shape[:]
# slice_idx = 0
# if dimensionality == 3:
# slice_idx = vtkshape[2]//2
# reader2.SetDataExtent(0, vtkshape[0]-1, 0, vtkshape[1]-1, slice_idx, slice_idx+slices_to_read-1)
# # DataSpacing and DataOrigin should be added to the interface
# reader2.SetDataSpacing(1, 1, 1)
# reader2.SetDataOrigin(0, 0, 0)

# print("reading")
# reader2.Update()
if True:
# use the cilRawCroppedReader to read the slice
reader2 = cilRawCroppedReader()
reader2.SetFileName(self.fname)
reader2.SetTargetZExtent((shape[2]//2-5, shape[2]//2+5))
reader2.SetBigEndian(isBigEndian)
reader2.SetIsFortran(isFortran)
reader2.SetTypeCodeName(dt.name)
reader2.SetStoredArrayShape(shape)
reader2.Update()
else:

rawfname = os.path.join(tempfile.gettempdir(),"test.raw")

offset = offset * bytes_per_element
slices_to_read = 1
if shape[2] > 1:
slices_to_read = 2
with open(self.fname, 'br') as f:
f.seek(offset)
raw_data = f.read(slice_size*bytes_per_element* slices_to_read)
with open(rawfname, 'wb') as f2:
f2.write(raw_data)

reader2 = vtk.vtkImageReader2()
reader2.SetFileName(rawfname)

vtktype = Converter.dtype_name_to_vtkType[dt.name]
reader2.SetDataScalarType(vtktype)

if isBigEndian:
reader2.SetDataByteOrderToBigEndian()
else:
reader2.SetDataByteOrderToLittleEndian()

reader2.SetFileDimensionality(len(shape))
vtkshape = shape[:]
if not isFortran:
# need to reverse the shape (again)
vtkshape = shape[::-1]
# vtkshape = shape[:]
slice_idx = 0
if dimensionality == 3:
slice_idx = vtkshape[2]//2
reader2.SetDataExtent(0, vtkshape[0]-1, 0, vtkshape[1]-1, slice_idx, slice_idx+slices_to_read-1)
# DataSpacing and DataOrigin should be added to the interface
reader2.SetDataSpacing(1, 1, 1)
reader2.SetDataOrigin(0, 0, 0)

logger.info("reading")
reader2.Update()
# read one slice in the middle and display it in a viewer in a modal dialog

diag = QtWidgets.QDialog(parent=self)
diag.setModal(True)
if dimensionality == 3:
diag.setWindowTitle(f'Preview slice {central_slice} = {shape[2]//2}')
else:
diag.setWindowTitle(f'Preview data')
diag.setWindowFlag(QtCore.Qt.WindowContextHelpButtonHint, False)
diag.setWindowFlag(QtCore.Qt.WindowMaximizeButtonHint)

# add a layout
verticalLayout = QtWidgets.QVBoxLayout(diag)
verticalLayout.setContentsMargins(10, 10, 10, 10)
Expand All @@ -315,6 +325,17 @@ def preview(self):
sc = QCILViewerWidget(diag, viewer=viewer2D)
sc.viewer.setInputData(reader2.GetOutput())

# swap the labels of the orientation marker if the 3D data is not in fortran order
if dimensionality == 3:
if not isFortran:
logger.info("Swapping orientation marker XZ labels")
# sc.viewer.orientation_marker.GetOrientationMarker().RotateWXYZ(90, 0, 1, 0)
om = sc.viewer.orientation_marker
xlabel = om.GetXAxisLabelText()
zlabel = om.GetZAxisLabelText()
om.SetZAxisLabelText(xlabel)
om.SetXAxisLabelText(zlabel)

# add it to the layout of the dialog
verticalLayout.addWidget(sc)
# add the layout to the dialog
Expand Down
2 changes: 0 additions & 2 deletions Wrappers/Python/ccpi/viewer/ui/main_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,6 @@ def setViewersInputFromDialog(self, viewers, input_num=1):
if image_file is not None:
self.setViewersInput(image_file, viewers, input_num=input_num)

import pysnooper
@pysnooper.snoop()
def setViewersInput(self, image, viewers, input_num=1, image_name=None):
'''
Displays an image in the viewer/s.
Expand Down

0 comments on commit cc854ba

Please sign in to comment.