0

I have a short matplotlib module where I do some basic image analysis, delete slices, register, sum and save. However, I would like to add a reset button to re-initialise the module, putting the image data back to its initial state.

Basically, if I have a list of 2D images which make up a dynamic data set. I want to be able to delete frames, register the images and finally create a summed image. To do this I manipulate the list raw_dicom_stack. However, if something goes wrong I would like another button reset which resets raw_dicom_stack and restarts the class with the original data. I copy the contents of raw_dicom_stack to reset_stack early on and to reset I try:

I have tried:

def reset(self, event):
    self.__init__(self.reset_stack, self.nframes, self.ds)

but this crashes the module and I get no error message. Where am I going wrong?

def main():
    root = Tk()
    root.withdraw()
    #Default DMSA image directory
    IMAGE_DIR = '/home/nm/Python/DMSA/dmsa_examples/dynamics/'
    filename = tkFileDialog.askopenfilename(parent=root,initialdir=IMAGE_DIR, title='Select dicom image')
    if filename == '':
        quit()

    ds = dicom.read_file(filename)
    pix = ds.pixel_array
    #find out size of pixel array
    #print 'dicom image has x,y,z dimensions %d,%d,%d' %(int(pix.shape[0]), int(pix.shape[1]), int(pix.shape[2]))
    raw_dicom_stack = []

    for x in range(pix.shape[0]/2):
        raw_dicom_stack.append(pix[x,:,:])


    nframes = pix.shape[0]/2;
    # Visualize it
    viewer = VolumeViewer(raw_dicom_stack, nframes, ds)
    viewer.show()

class VolumeViewer(object):
    def __init__(self, raw_dicom_stack, nframes, ds):

        self.raw_dicom_stack = raw_dicom_stack
        self.nframes = nframes
        self.summed_image = np.zeros((self.raw_dicom_stack[0].shape[0],self.raw_dicom_stack[0].shape[1]))
        self.frame = 0
        self.frames_delete = 0
        self.ds = ds
        self.reset_stack = raw_dicom_stack


        # Plot the first slice of the image
        self.fig, self.ax = plt.subplots()
        self.im = self.ax.imshow(np.array(raw_dicom_stack[0]), cmap = cm.gray)


    def update(self, value):
        self.frame = int(self.nframes * value)
        # Update the image data
        dat = np.array(self.raw_dicom_stack[self.frame])
        self.im.set_data(dat)
        self.im.set_clim([dat.min(), dat.max()])
        # Redraw the plot
        self.fig.canvas.draw()      

    def add(self,event):
        if self.query_add == True:
            print "Image series has already summed"
        else:
            for x in range(self.nframes):
                self.summed_image += self.raw_dicom_stack[x]
            del self.raw_dicom_stack[:]
            self.raw_dicom_stack.append(self.summed_image[:,:])
            self.nframes = len(self.raw_dicom_stack)

            # Update the image data
            dat = np.array(self.raw_dicom_stack[0])
            self.im.set_data(dat)
            self.slider.reset()
            self.im.set_clim([dat.min(), dat.max()])
            # Redraw the plot
            self.fig.canvas.draw()

            self.query_add = True
            #rezero summed_image
            #summed_image = np.zeros((self.summed_image.shape[0],self.summed_image.shape[1]))


    def show(self):
        plt.show()

    def reset(self, event):

        #self.summed_image = np.zeros((self.raw_dicom_stack[0].shape[0],self.raw_dicom_stack[0].shape[1]))
        #self.query_register = False
        #self.query_add = False
        self.__init__(self.reset_stack, self.nframes, self.ds)

    def quit(self,event):
        quit()

if __name__ == '__main__':
    main()
3
  • what's the error you get calling reset Commented Feb 1, 2013 at 15:32
  • Also is it possible that you provide a minimal example that illustrates the problem, running that code is a bit difficult not having all the dependencies on hand Commented Feb 1, 2013 at 15:43
  • @MattiLyra I get no error message. I have reworded my question and also removed much of the code unrelated to the problem, hopefully it makes a little more sense now Commented Feb 1, 2013 at 15:55

1 Answer 1

1

Running the following code with Python 2.7.3 works perfectly fine

import numpy as np

class VolumeViewer(object):
    def __init__(self, raw_dicom_stack, nframes, ds):

        self.raw_dicom_stack = raw_dicom_stack
        self.nframes = nframes
        self.summed_image = np.zeros((self.raw_dicom_stack[0].shape[0],self.raw_dicom_stack[0].shape[1]))
        self.frame = 0
        self.frames_delete = 0
        self.ds = ds
        self.reset_stack = raw_dicom_stack


        # Plot the first slice of the image
        self.fig, self.ax = plt.subplots()
        self.im = self.ax.imshow(np.array(raw_dicom_stack[0]), cmap = cm.gray)


    def update(self, value):
        self.frame = int(self.nframes * value)
        # Update the image data
        dat = np.array(self.raw_dicom_stack[self.frame])
        self.im.set_data(dat)
        self.im.set_clim([dat.min(), dat.max()])
        # Redraw the plot
        self.fig.canvas.draw()      

    def add(self,event):
        if self.query_add == True:
            print "Image series has already summed"
        else:
            for x in range(self.nframes):
                self.summed_image += self.raw_dicom_stack[x]
            del self.raw_dicom_stack[:]
            self.raw_dicom_stack.append(self.summed_image[:,:])
            self.nframes = len(self.raw_dicom_stack)

            # Update the image data
            dat = np.array(self.raw_dicom_stack[0])
            self.im.set_data(dat)
            self.slider.reset()
            self.im.set_clim([dat.min(), dat.max()])
            # Redraw the plot
            self.fig.canvas.draw()

            self.query_add = True
            #rezero summed_image
            #summed_image = np.zeros((self.summed_image.shape[0],self.summed_image.shape[1]))


    def show(self):
        plt.show()

    def reset(self, event):

        #self.summed_image = np.zeros((self.raw_dicom_stack[0].shape[0],self.raw_dicom_stack[0].shape[1]))
        #self.query_register = False
        #self.query_add = False
        self.__init__(self.reset_stack, self.nframes, self.ds)

    def quit(self,event):
        quit()

if __name__ == '__main__':
    viewer = VolumeViewer(np.zeros((2,10,10)), 4, None)
    viewer.reset()
Sign up to request clarification or add additional context in comments.

3 Comments

@MattLyra - I get the same problem with the program hanging. Is there a better way to restart the class when the button is selected
Well you can always try separating the code from __init__ into reset, but I don't think that's going to help as it's probably not calling __init__ that fails but something with the actual re initialisation. Have you stepped through the init code in the debugger to see where it hangs?
Problem solved. It was related to the way I was copying raw_dicom_stack

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.