Skip to content

Commit

Permalink
Successful debug
Browse files Browse the repository at this point in the history
  • Loading branch information
DanicaSTFC committed Sep 10, 2024
1 parent 3c34fea commit ce5c672
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 14 deletions.
53 changes: 52 additions & 1 deletion Wrappers/Python/ccpi/viewer/CILViewer2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,11 +1234,17 @@ def setInput3DData(self, imageData):
return self.setInputData(imageData)

def setInputData(self, imageData):
print("start setInputData201")
self.log("setInputData")
print("201")
self.reset()
print("202")
self.img3D = imageData
print("203")
self.installPipeline()
print("204")
self.axes_initialised = True
print("end setInputData")

def setInputData2(self, imageData):
self.image2 = imageData
Expand Down Expand Up @@ -1387,30 +1393,44 @@ def setVisualisationToRectilinearWipe(self):
def installPipeline(self):
if self.vis_mode == CILViewer2D.IMAGE_WITH_OVERLAY:
if self.img3D is not None:
print("300")
self.installImageWithOverlayPipeline()
print("301")
if self.image2 is not None:
self.installPipeline2()
print("302")
elif self.vis_mode == CILViewer2D.RECTILINEAR_WIPE:
self.installRectilinearWipePipeline()
print("303")

if self.getSliderWidgetEnabled():
self.installSliceSliderWidgetPipeline()
print("304")

self.ren.ResetCamera()
print("305")
self.ren.Render()
print("306")

self.camera.SetViewUp(0, -1, 0)
print("306")

if not self.axes_initialised:
self.camera.Azimuth(180)
print("307")

self.AdjustCamera()
print("308")

self.ren.AddViewProp(self.cursorActor)
print("309")
self.cursorActor.VisibilityOn()
print("310")

self.iren.Initialize()
print("311")
self.renWin.Render()
print("312")

def installPipeline2(self):
if self.image2 is not None:
Expand All @@ -1423,31 +1443,60 @@ def installPipeline2(self):

def installImageWithOverlayPipeline(self):
'''Slices a 3D volume and then creates an actor to be rendered'''
print("400")
self.log("installPipeline")
print("401")
self.ren.AddViewProp(self.cornerAnnotation)
print("402")

self.voi.SetInputData(self.img3D)
print("403")
#select one slice in Z
extent = [i for i in self.img3D.GetExtent()]
print("404")
for i in range(len(self.slicenos)):
self.slicenos[i] = round((extent[i * 2 + 1] + extent[i * 2]) / 2)
print("405")

extent[self.sliceOrientation * 2] = self.getActiveSlice()
print("406")
extent[self.sliceOrientation * 2 + 1] = self.getActiveSlice()

print("407")
print("extent is", extent)
self.voi.SetVOI(extent[0], extent[1], extent[2], extent[3], extent[4], extent[5])

self.voi.SetVOI(extent[0], extent[1], extent[2], extent[3], extent[4], extent[5])

self.voi.Update()
print("410")

# set window/level for slice based on values in entire volume:
print(self.voi.GetOutput())
output = self.voi.GetOutput()
print("Extent: ", output.GetExtent())
print("Number of Points: ", output.GetNumberOfPoints())
print("Number of Tuples in Scalars: ", output.GetPointData().GetScalars().GetNumberOfTuples())

point_data = output.GetPointData()

# Print available scalar arrays
for i in range(point_data.GetNumberOfArrays()):
array_name = point_data.GetArrayName(i)
array = point_data.GetArray(i)
print(f"Array {i} name = {array_name}, Number of Tuples = {array.GetNumberOfTuples()}")

#self.voi.GetOutput().GetPointData().SetActiveScalars("vtkarray")
self.ia.SetInputData(self.voi.GetOutput())

print("411")
self.ia.Update()
print("412")
self.style.AutoWindowLevelOnVolumeRange(update_slice=False)
print("413")
self.InitialLevel = self.getSliceColorLevel()
self.InitialWindow = self.getSliceColorWindow()
self.log("level {0} window {1}".format(self.InitialLevel, self.InitialWindow))
print("416")

self.imageSliceMapper.SetInputConnection(self.voi.GetOutputPort())

Expand All @@ -1457,12 +1506,14 @@ def installImageWithOverlayPipeline(self):
self.imageSlice.GetProperty().SetInterpolationTypeToNearest()

self.imageSlice.Update()
print("420")

self.imageTracer.SetProjectionPosition(self.style.image2world([0, 0, 0])[self.getSliceOrientation()])

self.AddActor(self.imageSlice, SLICE_ACTOR)

self.imageTracer.SetViewProp(self.imageSlice)
print("423")

def installImageWithOverlayPipeline2(self):
'''Slices a 3D volume and then creates an actor to be rendered'''
Expand Down
12 changes: 11 additions & 1 deletion Wrappers/Python/ccpi/viewer/ui/main_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,20 +329,25 @@ def displayImage(self, viewers, input_num, reader, image_name, image):
The image to display.
'''
print("start displayImage")
if image is None:
return
if not isinstance(viewers, list):
viewers = [viewers]
for viewer in viewers:
if input_num == 1:
print("101")
viewer.setInputData(image)
print("102")
elif input_num == 2:
if isinstance(viewer, CILViewer2D):
viewer.setInputData2(image)
# If input_num=2 we don't want to update the viewer coords dock widget
# with the name of the image file, as this is the overlay image.
image_name = None
print("103")
self.updateGUIForNewImage(reader, viewer, image_name)
print("end displayImage")

def updateGUIForNewImage(self, reader=None, viewer=None, image_name=None):
'''
Expand All @@ -352,8 +357,10 @@ def updateGUIForNewImage(self, reader=None, viewer=None, image_name=None):
- Updates the viewer coordinates dock widget with the image file name
In subclass, may want to add more functionality.
'''
print("start updateGUIForNewImage")
self.updateViewerCoordsDockWidgetWithCoords(reader)
self.updateViewerCoordsDockWidgetWithImageFileName(image_name)
print("end updateGUIForNewImage")

def updateViewerCoordsDockWidgetWithImageFileName(self, image_name=None):
'''
Expand All @@ -364,13 +371,15 @@ def updateViewerCoordsDockWidgetWithImageFileName(self, image_name=None):
image_file: str
The image file name.
'''
print("start updateViewerCoordsDockWidgetWithImageFileName")
if image_name is None:
return

widgets = self.viewer_coords_dock.getWidgets()
widgets['image_field'].clear()
widgets['image_field'].addItem(image_name)
widgets['image_field'].setCurrentIndex(0)
print("end updateViewerCoordsDockWidgetWithImageFileName")

def updateViewerCoordsDockWidgetWithCoords(self, reader=None):
'''
Expand All @@ -383,7 +392,7 @@ def updateViewerCoordsDockWidgetWithCoords(self, reader=None):
The reader used to read the image. This contains some extra info about the
original image file.
'''

print("start updateViewerCoordsDockWidgetWithCoords")
viewer = self.viewer_coords_dock.viewers[0]

if not isinstance(viewer, (CILViewer2D, CILViewer)):
Expand Down Expand Up @@ -437,6 +446,7 @@ def updateViewerCoordsDockWidgetWithCoords(self, reader=None):

widgets['loaded_image_dims_field'].setText(original_image_dims)
widgets['displayed_image_dims_field'].setText(displayed_image_dims)
print("end updateViewerCoordsDockWidgetWithCoords")

def updateViewerCoords(self):
'''
Expand Down
51 changes: 39 additions & 12 deletions Wrappers/Python/ccpi/viewer/utils/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,29 @@ class Converter(object):
@staticmethod
def numpy2vtkImage(nparray, spacing=(1., 1., 1.), origin=(0, 0, 0), deep=0, output=None):
"""The method converts a numpy array to a vtk image.
The vtk extent is set and needs to differentiate between 3D and 2D images."""
shape = numpy.shape(nparray)
if (nparray.flags["FNC"]):
The vtk extent is set and needs to differentiate between 3D and 2D images.
Flatten the array in the correct order.
Create vtkImageData if output is None, otherwise use the provided empty output.
Add the vtkarray to the image's point data."""

print("active numpy2vtkImage")
shape = numpy.shape(nparray)
num_dims = len(shape)
if num_dims not in [2, 3]:
raise ValueError("Only 2D or 3D numpy arrays are supported.")

if nparray.flags["FNC"]:
order = "F"
i = 0
k = len(shape) - 1
i, k = 0, num_dims - 1
else:
order = "C"
i = len(shape) - 1
k = 0
i, k = num_dims - 1, 0

nparray = nparray.ravel(order)
vtkarray = numpy_support.numpy_to_vtk(num_array=nparray,
deep=deep,
array_type=numpy_support.get_vtk_array_type(nparray.dtype))
vtkarray.SetName('vtkarray')


if output is None:
img_data = vtk.vtkImageData()
Expand All @@ -117,14 +123,35 @@ def numpy2vtkImage(nparray, spacing=(1., 1., 1.), origin=(0, 0, 0), deep=0, outp
else:
img_data = output

img_data.GetPointData().AddArray(vtkarray)
if len(shape) == 3:
print(img_data)
point_data = img_data.GetPointData()
while point_data.GetNumberOfArrays() > 0:
point_data.RemoveArray(0)
vtkarray.SetName('ImageScalars')
point_data.AddArray(vtkarray)
img_data.GetPointData().SetScalars(vtkarray)
if num_dims == 3:
img_data.SetExtent(0, shape[i] - 1, 0, shape[1] - 1, 0, shape[k] - 1)
elif len(shape) == 2:
elif num_dims == 2:
img_data.SetExtent(0, shape[i] - 1, 0, shape[k] - 1, 0, 0)
img_data.GetPointData().SetActiveScalars('vtkarray')
#img_data.GetPointData().SetActiveScalars('vtkarray')
img_data.SetOrigin(origin)
img_data.SetSpacing(spacing)
print("end of numpy2vtkImage")



print("Extent: ", img_data.GetExtent())
print("Number of Points: ", img_data.GetNumberOfPoints())
print("Number of Tuples in Scalars: ", img_data.GetPointData().GetScalars().GetNumberOfTuples())

point_data = img_data.GetPointData()

# Print available scalar arrays
for i in range(point_data.GetNumberOfArrays()):
array_name = point_data.GetArrayName(i)
array = point_data.GetArray(i)
print(f"Array {i} name = {array_name}, Number of Tuples = {array.GetNumberOfTuples()}")

return img_data

Expand Down

0 comments on commit ce5c672

Please sign in to comment.