コード例 #1
0
    def __init__(self, img_path):
        super().__init__()

        #
        # Load image data
        #
        base_path = os.path.splitext(img_path)[0]
        lenslet_path = base_path + '-lenslet.txt'
        optics_path = base_path + '-optics.txt'

        with open(lenslet_path) as f:
            tmp = eval(f.readline())
            x_offset, y_offset, right_dx, right_dy, down_dx, down_dy = \
              np.array(tmp, dtype=np.float32)

        with open(optics_path) as f:
            for line in f:
                name, val = line.strip().split()
                try:
                    setattr(self, name, np.float32(val))
                except:
                    pass

        max_angle = math.atan(self.pitch/2/self.flen)

        #
        # Prepare image
        #
        im_pil = Image.open(img_path)
        if im_pil.mode == 'RGB':
            self.NCHANNELS = 3
            w, h = im_pil.size
            im = np.zeros((h, w, 4), dtype=np.float32)
            im[:, :, :3] = np.array(im_pil).astype(np.float32)
            self.LF_dim = (ceil(h/down_dy), ceil(w/right_dx), 3)
        else:
            self.NCHANNELS = 1
            im = np.array(im_pil.getdata()).reshape(im_pil.size[::-1]).astype(np.float32)
            h, w = im.shape
            self.LF_dim = (ceil(h/down_dy), ceil(w/right_dx))

        x_start = x_offset - int(x_offset / right_dx) * right_dx
        y_start = y_offset - int(y_offset / down_dy) * down_dy
        x_ratio = self.flen * right_dx / self.pitch
        y_ratio = self.flen * down_dy / self.pitch

        #
        # Generate the cuda kernel
        #
        mod_LFview = pycuda.compiler.SourceModule(
            _kernel_tpl.render(
                newiw=self.LF_dim[1],
                newih=self.LF_dim[0],
                oldiw=w,
                oldih=h,
                x_start=x_start,
                y_start=y_start,
                x_ratio=x_ratio,
                y_ratio=y_ratio,
                x_step=right_dx,
                y_step=down_dy,
                NCHANNELS=self.NCHANNELS
                )
            )
        
        self.LFview_func = mod_LFview.get_function("LFview_kernel")
        self.texref = mod_LFview.get_texref("tex")
        
        #
        # Now generate the cuda texture
        #
        if self.NCHANNELS == 3:
            cuda.bind_array_to_texref(
                cuda.make_multichannel_2d_array(im, order="C"),
                self.texref
                )
        else:
            cuda.matrix_to_texref(im, self.texref, order="C")
            
        #
        # We could set the next if we wanted to address the image
        # in normalized coordinates ( 0 <= coordinate < 1.)
        # texref.set_flags(cuda.TRSF_NORMALIZED_COORDINATES)
        #
        self.texref.set_filter_mode(cuda.filter_mode.LINEAR)

        #
        # Prepare the traits
        #
        self.add_trait('X_angle', Range(-max_angle, max_angle, 0.0))
        self.add_trait('Y_angle', Range(-max_angle, max_angle, 0.0))
        
        self.plotdata = ArrayPlotData(LF_img=self.sampleLF())
        self.LF_img = Plot(self.plotdata)
        if self.NCHANNELS == 3:
            self.LF_img.img_plot("LF_img")
        else:
            self.LF_img.img_plot("LF_img", colormap=gray)
コード例 #2
0
    def _create_plot_component(self):
        self.data = ArrayPlotData()
        self.data["frequency"] = np.linspace(0.,
                                             SAMPLING_RATE / 2.0,
                                             num=NUM_SAMPLES / 2)
        for i in xrange(NUM_LINES):
            self.data['amplitude%d' % i] = np.zeros(NUM_SAMPLES / 2)
        self.data["time"] = np.linspace(0.,
                                        float(NUM_SAMPLES) / SAMPLING_RATE,
                                        num=NUM_SAMPLES)
        self.data['time_amplitude'] = np.zeros(NUM_SAMPLES)
        self.data['imagedata'] = np.zeros(
            (NUM_SAMPLES / 2, SPECTROGRAM_LENGTH))

        spectrum_plot = Plot(self.data)
        for i in xrange(NUM_LINES):
            if i == NUM_LINES - 1:
                linewidth = 2
                color = (1, 0, 0)
            else:
                linewidth = 1
                c = (NUM_LINES - i - 1) / float(NUM_LINES)
                color = (1, 0.5 + c / 2, c)
            spectrum_plot.plot(("frequency", "amplitude%d" % i),
                               name="Spectrum%d" % i,
                               color=color,
                               line_width=linewidth)
        spectrum_plot.padding_bottom = 20
        spectrum_plot.padding_top = 20
        spec_range = spectrum_plot.plots.values()[0][0].value_mapper.range
        spec_range.low = -90
        spec_range.high = 0.0
        spectrum_plot.index_axis.title = 'Frequency(Hz)'
        spectrum_plot.value_axis.title = 'Amplitude(dB)'

        time_plot = Plot(self.data)
        time_plot.plot(("time", "time_amplitude"), name="Time", color="blue")
        time_plot.padding_top = 20
        time_plot.padding_bottom = 20
        time_plot.index_axis.title = 'Time (seconds)'
        time_plot.value_axis.title = 'Amplitude'
        time_range = time_plot.plots.values()[0][0].value_mapper.range
        time_range.low = -1.5
        time_range.high = 1.5

        spectrogram_plot = Plot(self.data)
        spectrogram_time = (0.0, SPECTROGRAM_LENGTH * NUM_SAMPLES /
                            float(SAMPLING_RATE))
        spectrogram_freq = (0.0, SAMPLING_RATE / 2.0)
        spectrogram_plot.img_plot(
            'imagedata',
            name='Spectrogram',
            xbounds=spectrogram_time,
            ybounds=spectrogram_freq,
            colormap=cm.reverse(cm.Blues),
        )
        range_obj = spectrogram_plot.plots['Spectrogram'][0].value_mapper.range
        range_obj.high = -20
        range_obj.low = -60
        spectrogram_plot.padding_bottom = 20
        spectrogram_plot.padding_top = 20

        container = VPlotContainer()
        container.add(time_plot)
        container.add(spectrum_plot)
        container.add(spectrogram_plot)

        return container
コード例 #3
0
    def _create_plot_window(self):
        # Create the model
        min_value = 350
        max_value = self.max_data
        image_value_range = DataRange1D(low=min_value, high=max_value)
        self.cmap = jet(range=image_value_range)
        self._update_model()
        datacube = self.colorcube

        # Create the plot
        self.plotdata = ArrayPlotData()
        self.plotdataVoxel = ArrayPlotData()
        self.plotdataSlices = ArrayPlotData()
        self.plotdataVoxelFFT = ArrayPlotData()
        self.plotdataPC = ArrayPlotData()
        self._update_images()

        # Top Left plot
        centerplot = Plot(self.plotdata,
                          resizable='hv',
                          padding=20,
                          title="Slice_X")
        imgplot = centerplot.img_plot("yz",
                                      xbounds=None,
                                      ybounds=None,
                                      colormap=self.cmap)[0]

        centerplot.x_axis.title = "Y"
        centerplot.y_axis.title = "Z"
        self._add_plot_tools(imgplot, "yz")
        self.cursorYZ = CursorTool(imgplot, drag_button='left', color='white')
        self.cursorYZ.current_position = self.slice_y, self.slice_z
        imgplot.overlays.append(self.cursorYZ)
        self.center = imgplot

        # Top Right Plot
        rightplot = Plot(self.plotdata,
                         resizable='hv',
                         padding=20,
                         title="Slice_Y")
        rightplot.x_axis.title = "X"
        rightplot.y_axis.title = "Z"
        imgplot = rightplot.img_plot("xz",
                                     xbounds=None,
                                     ybounds=None,
                                     colormap=self.cmap)[0]

        self._add_plot_tools(imgplot, "xz")
        self.cursorXZ = CursorTool(imgplot, drag_button='left', color='white')
        self.cursorXZ.current_position = self.slice_x, self.slice_z
        imgplot.overlays.append(self.cursorXZ)
        self.right = imgplot

        # Bottom  LeftPlot
        bottomplot = Plot(self.plotdata,
                          resizable='hv',
                          padding=20,
                          title="Slice_Z")
        bottomplot.x_axis.title = "Y"
        bottomplot.y_axis.title = "X"
        imgplot = bottomplot.img_plot("xy",
                                      xbounds=None,
                                      ybounds=None,
                                      colormap=self.cmap)[0]
        """bottomplot.contour_plot("xy", 
                          type="poly",
                          xbounds=None,
                          ybounds=None)[0]"""

        self._add_plot_tools(imgplot, "xy")
        self.cursorXY = CursorTool(imgplot, drag_button='left', color='white')
        self.cursorXY.current_position = self.slice_y, self.slice_x
        imgplot.overlays.append(self.cursorXY)
        self.bottom = imgplot
        """ # Create a colorbar
        cbar_index_mapper = LinearMapper(range=image_value_range)
        self.colorbar = ColorBar(index_mapper=cbar_index_mapper,
                                 plot=centerplot,
                                 padding_top=centerplot.padding_top,
                                 padding_bottom=centerplot.padding_bottom,
                                 padding_right=40,
                                 resizable='v',
                                 width=30, height = 100)"""

        # Create data series to plot
        timeplot = Plot(self.plotdataVoxel, resizable='hv', padding=20)
        timeplot.x_axis.title = "Frames"
        timeplot.plot("TimeVoxel",
                      color='lightblue',
                      line_width=1.0,
                      bgcolor="white",
                      name="Time")[0]
        # for i in range(len(self.tasks)):
        #         timeplot.plot(self.timingNames[i+2],color=tuple(COLOR_PALETTE[i]),
        #         line_width=1.0, bgcolor = "white", border_visible=True, name = self.timingNames[i+2])[0]

        timeplot.legend.visible = True
        timeplot.plot("time",
                      type="scatter",
                      color=tuple(COLOR_PALETTE[2]),
                      line_width=1,
                      bgcolor="white",
                      border_visible=True,
                      name="time")[0]
        self.timePlot = timeplot
        # Create data series to plot
        timeplotBig = Plot(self.plotdataVoxel, resizable='hv', padding=20)
        timeplotBig.x_axis.title = "Frames"
        timeplotBig.plot("TimeVoxel",
                         color='lightblue',
                         line_width=1.5,
                         bgcolor="white",
                         name="Time")[0]
        timeplotBig.legend.visible = True
        timeplotBig.plot("time",
                         type="scatter",
                         color=tuple(COLOR_PALETTE[2]),
                         line_width=1,
                         bgcolor="white",
                         border_visible=True,
                         name="time")[0]
        self.timePlotBig = timeplotBig

        # Create data series to plot
        freqplotBig = Plot(self.plotdataVoxelFFT, resizable='hv', padding=20)
        freqplotBig.x_axis.title = "Frequency (Hz)"
        freqplotBig.plot("FreqVoxel",
                         color='lightblue',
                         line_width=1.5,
                         bgcolor="white",
                         name="Abs(Y)")[0]
        freqplotBig.legend.visible = True
        freqplotBig.plot("peaks",
                         type="scatter",
                         color=tuple(COLOR_PALETTE[2]),
                         line_width=1,
                         bgcolor="white",
                         border_visible=True,
                         name="peaks")[0]
        self.freqPlotBig = freqplotBig

        # Create data series to plot
        PCplotBig = Plot(self.plotdataPC, resizable='hv', padding=20)
        PCplotBig.x_axis.title = "Frames"
        PCplotBig.plot("Principal Component",
                       color='lightblue',
                       line_width=1.5,
                       bgcolor="white",
                       name="Principal Component")[0]
        PCplotBig.legend.visible = True
        PCplotBig.plot("time",
                       type="scatter",
                       color=tuple(COLOR_PALETTE[2]),
                       line_width=1,
                       bgcolor="white",
                       border_visible=True,
                       name="time")[0]
        self.PCplotBig = PCplotBig

        #self.time = time
        # Create a GridContainer to hold all of our plots
        container = GridContainer(padding=10,
                                  fill_padding=True,
                                  bgcolor="white",
                                  use_backbuffer=True,
                                  shape=(2, 2),
                                  spacing=(10, 10))
        containerTime = GridContainer(padding=10,
                                      fill_padding=True,
                                      bgcolor="white",
                                      use_backbuffer=True,
                                      shape=(1, 1),
                                      spacing=(5, 5))

        containerFreq = GridContainer(padding=10,
                                      fill_padding=True,
                                      bgcolor="white",
                                      use_backbuffer=True,
                                      shape=(1, 1),
                                      spacing=(5, 5))
        containerPC = GridContainer(padding=10,
                                    fill_padding=True,
                                    bgcolor="white",
                                    use_backbuffer=True,
                                    shape=(1, 1),
                                    spacing=(5, 5))

        container.add(centerplot)
        container.add(rightplot)
        container.add(bottomplot)
        container.add(timeplot)
        containerTime.add(timeplotBig)
        containerFreq.add(freqplotBig)
        containerPC.add(PCplotBig)
        """container = GridContainer(padding=10, fill_padding=True,
                              bgcolor="white", use_backbuffer=True,
                              shape=(3,3), spacing=(10,10))
       
        for i in range(14,23):
             slicePlot = Plot(self.plotdataSlices, resizable= 'hv', padding=20,title = "slice " + str(i),bgcolor = "white")
             slicePlot.img_plot("slice " + str(i),xbounds= None, ybounds= None, colormap=self.cmap,bgcolor = "white")[0]
             container.add(slicePlot)"""

        self.container = container
        self.nb.DeleteAllPages()
        self.window = Window(self.nb, -1, component=container)
        self.windowTime = Window(self.nb, -1, component=containerTime)
        self.windowFreq = Window(self.nb, -1, component=containerFreq)
        self.windowPC = Window(self.nb, -1, component=containerPC)
        self.sizer.Detach(self.topsizer)
        self.sizer.Detach(self.pnl2)
        self.topsizer.Clear()
        self.topsizer.Add(self.pnl3, 0, wx.ALL, 10)
        self.nb.AddPage(self.window.control, "fMRI Slices")
        self.nb.AddPage(self.windowTime.control, "Time Voxel")
        self.nb.AddPage(self.windowFreq.control, "Frequency Voxel")
        self.nb.AddPage(self.windowPC.control, "Principal Component")
        self.topsizer.Add(self.nb, 1, wx.EXPAND)
        self.sizer.Add(self.topsizer, 1, wx.EXPAND)
        self.sizer.Add(self.pnl2,
                       flag=wx.EXPAND | wx.BOTTOM | wx.TOP,
                       border=10)

        self.SetSizer(self.sizer)
        self.Centre()
        self.Show(True)