예제 #1
0
    def OnInit(self):

        self.HARDWARE_DEBUG = HARDWARE_DEBUG

        self.frame = MCLStage3DFrame(None)

        self.nanodrive = MCLNanoDrive(debug=self.HARDWARE_DEBUG)

        try:
            self.frame.m_staticText_maxdim.SetLabel(
                "[ %g x %g x %g ]" %
                (self.nanodrive.cal_X, self.nanodrive.cal_Y,
                 self.nanodrive.cal_Z))
        except Exception as e:
            print e
            self.frame.m_staticText_maxdim.SetLabel("[ ? ? ? ]")

        x, y, z = self.nanodrive.get_pos()

        self.frame.m_scrollBar_x.SetScrollbar(100 * x / self.nanodrive.cal_X,
                                              1,
                                              100,
                                              1,
                                              refresh=True)
        self.frame.m_scrollBar_y.SetScrollbar(100 * y / self.nanodrive.cal_Y,
                                              1,
                                              100,
                                              1,
                                              refresh=True)
        self.frame.m_scrollBar_z.SetScrollbar(100 * z / self.nanodrive.cal_Z,
                                              1,
                                              100,
                                              1,
                                              refresh=True)

        self.frame.m_textCtrl_x.SetValue("%02.3f" % x)
        self.frame.m_textCtrl_y.SetValue("%02.3f" % y)
        self.frame.m_textCtrl_z.SetValue("%02.3f" % z)

        self.frame.m_scrollBar_x.Bind(wx.EVT_SCROLL, self.on_scroll)
        self.frame.m_scrollBar_y.Bind(wx.EVT_SCROLL, self.on_scroll)
        self.frame.m_scrollBar_z.Bind(wx.EVT_SCROLL, self.on_scroll)

        # Figure
        self.wxfig = MPLFigureWithToolbarWX(self.frame.m_panel_plot)
        self.fig = self.wxfig.fig
        self.ax = self.fig.add_subplot(111)
        self.ax.set_xlim(0, self.nanodrive.cal_X)
        self.ax.set_ylim(self.nanodrive.cal_Y, 0)

        self.xypos_line, = self.ax.plot([x], [y], 'ro')

        self.frame.Show()
        return True
예제 #2
0
    def __init__(self, **params):

        for p in self.INPUT_PARAM_NAMES:
            setattr(self, p, params[p])

        self.x_array = np.arange(self.x0, self.x1, self.dx, dtype=float)
        self.y_array = np.arange(self.y0, self.y1, self.dy, dtype=float)
        self.z_array = np.arange(self.z0, self.z1, self.dz, dtype=float)

        self.Nx = len(self.x_array)
        self.Ny = len(self.y_array)
        self.Nz = len(self.z_array)

        self.mcl_axis_translation = xyz_tuple(*self.mcl_axis_translation)

        self.HARDWARE_DEBUG = False

        #HARDWARE
        self.nanodrive = MCLNanoDrive(debug=self.HARDWARE_DEBUG)

        self.mono = ActonSpectrometer(port=SPEC_COMM_PORT,
                                      debug=self.HARDWARE_DEBUG,
                                      dummy=False)
        self.mono_grating = self.mono.read_grating()
        self.mono_wl = self.mono.read_wl()

        self.ccd = AndorCCD(debug=self.HARDWARE_DEBUG)
        print "Andor CCD"
        print "%g x %g" % (self.ccd.Nx, self.ccd.Ny)
        self.spectrum_length = self.ccd.Nx
        self.ccd.set_cooler_on()
        self.ccd.get_temperature()

        if SHOW_GRAPH:
            self.fig = pl.figure(1)
            self.ax = self.fig.add_subplot(111)
            self.spec_plotline, = self.ax.plot(np.ones(self.spectrum_length))
            self.fig.show()

        #DATA ARRAYS
        self.integrated_count_map = np.zeros((
            self.Nz,
            self.Ny,
            self.Nx,
        ),
                                             dtype=int)
        self.spec_map = np.zeros(
            (self.Nz, self.Ny, self.Nx, self.spectrum_length), dtype=np.int32)
예제 #3
0
class ConfocalSpectrum3DScan(object):

    INPUT_PARAM_NAMES = """x0 x1 dx y0 y1 dy z0 z1 dz
                    t_exposure emgain ad_chan ad_hs_speed_index bin_row0 bin_row1
                    axis_scan_order mcl_axis_translation""".split()

    def __init__(self, **params):

        for p in self.INPUT_PARAM_NAMES:
            setattr(self, p, params[p])

        self.x_array = np.arange(self.x0, self.x1, self.dx, dtype=float)
        self.y_array = np.arange(self.y0, self.y1, self.dy, dtype=float)
        self.z_array = np.arange(self.z0, self.z1, self.dz, dtype=float)

        self.Nx = len(self.x_array)
        self.Ny = len(self.y_array)
        self.Nz = len(self.z_array)

        self.mcl_axis_translation = xyz_tuple(*self.mcl_axis_translation)

        self.HARDWARE_DEBUG = False

        #HARDWARE
        self.nanodrive = MCLNanoDrive(debug=self.HARDWARE_DEBUG)

        self.mono = ActonSpectrometer(port=SPEC_COMM_PORT,
                                      debug=self.HARDWARE_DEBUG,
                                      dummy=False)
        self.mono_grating = self.mono.read_grating()
        self.mono_wl = self.mono.read_wl()

        self.ccd = AndorCCD(debug=self.HARDWARE_DEBUG)
        print "Andor CCD"
        print "%g x %g" % (self.ccd.Nx, self.ccd.Ny)
        self.spectrum_length = self.ccd.Nx
        self.ccd.set_cooler_on()
        self.ccd.get_temperature()

        if SHOW_GRAPH:
            self.fig = pl.figure(1)
            self.ax = self.fig.add_subplot(111)
            self.spec_plotline, = self.ax.plot(np.ones(self.spectrum_length))
            self.fig.show()

        #DATA ARRAYS
        self.integrated_count_map = np.zeros((
            self.Nz,
            self.Ny,
            self.Nx,
        ),
                                             dtype=int)
        self.spec_map = np.zeros(
            (self.Nz, self.Ny, self.Nx, self.spectrum_length), dtype=np.int32)

    def run_3d_scan(self):

        self.ccd.set_ro_image_mode()
        self.ccd.set_trigger_mode('internal')
        self.ccd.set_image_flip(ANDOR_HFLIP, ANDOR_VFLIP)
        print "flip", self.ccd.get_image_flip()
        self.ccd.set_ad_channel(self.ad_chan)
        self.ccd.set_exposure_time(self.t_exposure)
        self.ccd.set_EMCCD_gain(self.emgain)
        self.ccd.set_cooler_on()
        self.ccd.get_temperature()
        self.ccd.set_shutter_open()
        self.ccd.set_hs_speed(self.ad_hs_speed_index)

        self.set_ijk = (-1, -1, -1)

        self.scanning = True

        time0 = time.time()

        for iii, ijk in enumerate(
                ijk_generator((self.Nx, self.Ny, self.Nz),
                              self.axis_scan_order)):

            #previous ijk
            ip, jp, kp = self.set_ijk

            # new ijk
            i, j, k = ijk

            # move stage
            if i != ip:
                x = self.x_array[i]
                self.nanodrive.set_pos_ax(x, self.mcl_axis_translation.x)
            if j != jp:
                y = self.y_array[j]
                self.nanodrive.set_pos_ax(y, self.mcl_axis_translation.y)
            if k != kp:
                z = self.z_array[k]
                self.nanodrive.set_pos_ax(z, self.mcl_axis_translation.z)

            self.set_ijk = ijk

            #t1 = time.time()
            self.read_from_hardware()
            #print "read_from_hardware (s)", time.time() - t1

            #t1 = time.time()
            self.ccd.start_acquisition()
            stat = "ACQUIRING"
            while stat != "IDLE":
                time.sleep(self.ccd.exposure_time * 0.1)
                stati, stat = self.ccd.get_status()
            self.ccd.get_acquired_data()
            #print "ccd acquire (s)", time.time() - t1

            spectrum = np.sum(self.ccd.buffer[self.bin_row0:self.bin_row1],
                              axis=0)

            self.spec_map[k, j, i, :] = spectrum

            cts = self.integrated_count_map[k, j, i] = sum(spectrum)

            if ((i + j + k) % 10) == 0:
                print ijk, cts
                t_now = time.time()
                pixel_time = (t_now - time0) * 0.1
                print "sec per pixel:", pixel_time, "| time remaining (s)", ((
                    (self.Nx * self.Ny * self.Nz) - iii) * pixel_time)
                time0 = t_now

            #if ((i + j + k) % 2) == 0 :
            if True:
                if SHOW_GRAPH:
                    self.spec_plotline.set_ydata(spectrum)
                    self.ax.relim()
                    self.ax.autoscale_view(tight=None,
                                           scalex=False,
                                           scaley=True)
                    self.fig.canvas.draw()

        #Finish up after scan
        print "saving data..."
        save_params = self.INPUT_PARAM_NAMES + [
            "spec_map", "Nx", "Ny", "Nz", "spectrum_length", "mono_grating",
            "mono_wl", "x_array", "y_array", "z_array", "integrated_count_map"
        ]
        save_dict = dict()
        for key in save_params:
            save_dict[key] = getattr(self, key)

        t0 = time.time()
        save_fname = "%i_spec_map3d.npz" % t0
        np.savez_compressed(save_fname, **save_dict)
        print "data saved as %s" % save_fname

    def read_from_hardware(self):

        pos = self.nanodrive.get_pos()

        self.xpos = pos[self.mcl_axis_translation.x - 1]
        self.ypos = pos[self.mcl_axis_translation.y - 1]
        self.zpos = pos[self.mcl_axis_translation.z - 1]
예제 #4
0
    def OnInit(self):

        print "OnInit"

        self.HARDWARE_DEBUG = HARDWARE_DEBUG

        self.STORED_HISTCHAN = 5000

        self.frame = ScanningTRPLControlFrame(None)

        # Logged Quantities
        self.x_position = LoggedQuantity(
            name='x_position',
            dtype=np.float,
            display_textctrl=self.frame.m_textCtrl_current_x,
            input_textctrl=self.frame.m_textCtrl_set_current_x)
        self.y_position = LoggedQuantity(
            name='x_position',
            dtype=np.float,
            display_textctrl=self.frame.m_textCtrl_current_y,
            input_textctrl=self.frame.m_textCtrl_set_current_y)

        # Figure
        self.wxfig = MPLFigureWithToolbarWX(self.frame.m_panel_plot)
        self.fig = self.wxfig.fig
        self.ax = self.fig.add_subplot(111)

        # Optimization Fig
        self.fig2 = pl.figure(2)
        self.ax2 = self.fig2.add_subplot(111)
        self.c0_hist_line, = self.ax2.plot(np.zeros(HIST_LEN, dtype=float))
        self.c1_hist_line, = self.ax2.plot(np.zeros(HIST_LEN, dtype=float))
        self.hist_vline = self.ax2.axvline(0)
        self.c0_hist = np.zeros(HIST_LEN, dtype=float)
        self.c1_hist = np.zeros(HIST_LEN, dtype=float)
        self.fig2.show()
        self.hist_i = 0

        # hardware
        self.scanning = False

        self.nanodrive = MCLNanoDrive(debug=self.HARDWARE_DEBUG)
        try:
            self.frame.m_staticText_maxdim.SetLabel(
                "max: %g x %g um" %
                (self.nanodrive.cal_Y, self.nanodrive.cal_X))
        except Exception as e:
            print e
            self.frame.m_staticText_maxdim.SetLabel("max: ? x ? um")

        #self.frame.m_staticText_maxdim.SetLabel("max: 75 x 75 um")

        #self.srslockin = SRSlockin(port="COM5", gpibaddr=8)
        #self.lockinstage = LockinStage(srs=self.srslockin,
        #                               POSMIN=0, POSMAX=75, channels={'x':1, 'y':2, 'z':3})

        self.picoharp = PicoHarp300(devnum=0, debug=self.HARDWARE_DEBUG)

        self.read_from_hardware()

        #self.x_position.hardware_set_func = lambda x: self.lockinstage.setx(x)
        #self.y_position.hardware_set_func = lambda y: self.lockinstage.sety(y)

        self.x_position.hardware_set_func = lambda x: self.nanodrive.set_pos_ax(
            x, 2)
        self.y_position.hardware_set_func = lambda y: self.nanodrive.set_pos_ax(
            y, 1)

        # update figure
        self.ax.set_xlim(0, self.nanodrive.cal_Y)
        self.ax.set_ylim(0, self.nanodrive.cal_X)
        #self.ax.set_xlim(0, 75)
        #self.ax.set_ylim(0, 75)

        # events
        self.frame.Bind(wx.EVT_BUTTON, self.on_start_scan,
                        self.frame.m_button_start)
        self.frame.Bind(wx.EVT_BUTTON, self.on_stop_scan,
                        self.frame.m_button_stop)

        self.timer = wx.Timer(id=2001)
        self.timer.Bind(wx.EVT_TIMER, self.on_timer)

        self.timer.Start(2000)

        self.fast_timer = wx.Timer(id=2002)
        self.fast_timer.Bind(wx.EVT_TIMER, self.on_fast_timer)

        self.frame.m_checkBox_picoharp_fastreadout.Bind(
            wx.EVT_CHECKBOX, self.on_fast_timer_checkbox)

        self.update_display()
        self.frame.Show()
        return True
예제 #5
0
class ScanningTRPLHistMapApp(wx.App):
    def OnInit(self):

        print "OnInit"

        self.HARDWARE_DEBUG = HARDWARE_DEBUG

        self.STORED_HISTCHAN = 5000

        self.frame = ScanningTRPLControlFrame(None)

        # Logged Quantities
        self.x_position = LoggedQuantity(
            name='x_position',
            dtype=np.float,
            display_textctrl=self.frame.m_textCtrl_current_x,
            input_textctrl=self.frame.m_textCtrl_set_current_x)
        self.y_position = LoggedQuantity(
            name='x_position',
            dtype=np.float,
            display_textctrl=self.frame.m_textCtrl_current_y,
            input_textctrl=self.frame.m_textCtrl_set_current_y)

        # Figure
        self.wxfig = MPLFigureWithToolbarWX(self.frame.m_panel_plot)
        self.fig = self.wxfig.fig
        self.ax = self.fig.add_subplot(111)

        # Optimization Fig
        self.fig2 = pl.figure(2)
        self.ax2 = self.fig2.add_subplot(111)
        self.c0_hist_line, = self.ax2.plot(np.zeros(HIST_LEN, dtype=float))
        self.c1_hist_line, = self.ax2.plot(np.zeros(HIST_LEN, dtype=float))
        self.hist_vline = self.ax2.axvline(0)
        self.c0_hist = np.zeros(HIST_LEN, dtype=float)
        self.c1_hist = np.zeros(HIST_LEN, dtype=float)
        self.fig2.show()
        self.hist_i = 0

        # hardware
        self.scanning = False

        self.nanodrive = MCLNanoDrive(debug=self.HARDWARE_DEBUG)
        try:
            self.frame.m_staticText_maxdim.SetLabel(
                "max: %g x %g um" %
                (self.nanodrive.cal_Y, self.nanodrive.cal_X))
        except Exception as e:
            print e
            self.frame.m_staticText_maxdim.SetLabel("max: ? x ? um")

        #self.frame.m_staticText_maxdim.SetLabel("max: 75 x 75 um")

        #self.srslockin = SRSlockin(port="COM5", gpibaddr=8)
        #self.lockinstage = LockinStage(srs=self.srslockin,
        #                               POSMIN=0, POSMAX=75, channels={'x':1, 'y':2, 'z':3})

        self.picoharp = PicoHarp300(devnum=0, debug=self.HARDWARE_DEBUG)

        self.read_from_hardware()

        #self.x_position.hardware_set_func = lambda x: self.lockinstage.setx(x)
        #self.y_position.hardware_set_func = lambda y: self.lockinstage.sety(y)

        self.x_position.hardware_set_func = lambda x: self.nanodrive.set_pos_ax(
            x, 2)
        self.y_position.hardware_set_func = lambda y: self.nanodrive.set_pos_ax(
            y, 1)

        # update figure
        self.ax.set_xlim(0, self.nanodrive.cal_Y)
        self.ax.set_ylim(0, self.nanodrive.cal_X)
        #self.ax.set_xlim(0, 75)
        #self.ax.set_ylim(0, 75)

        # events
        self.frame.Bind(wx.EVT_BUTTON, self.on_start_scan,
                        self.frame.m_button_start)
        self.frame.Bind(wx.EVT_BUTTON, self.on_stop_scan,
                        self.frame.m_button_stop)

        self.timer = wx.Timer(id=2001)
        self.timer.Bind(wx.EVT_TIMER, self.on_timer)

        self.timer.Start(2000)

        self.fast_timer = wx.Timer(id=2002)
        self.fast_timer.Bind(wx.EVT_TIMER, self.on_fast_timer)

        self.frame.m_checkBox_picoharp_fastreadout.Bind(
            wx.EVT_CHECKBOX, self.on_fast_timer_checkbox)

        self.update_display()
        self.frame.Show()
        return True

    def on_timer(self, e):
        self.read_from_hardware()
        self.update_display()

    def on_fast_timer(self, e):
        self.picoharp.read_count_rates()
        self.frame.m_textCtrl_count0.SetValue(str(self.picoharp.Countrate0))
        self.frame.m_textCtrl_count1.SetValue(str(self.picoharp.Countrate1))
        self.c0_hist[self.hist_i] = self.picoharp.Countrate0
        self.c1_hist[self.hist_i] = self.picoharp.Countrate1

        #self.c0_hist_line.set_ydata(self.c0_hist)
        self.c1_hist_line.set_ydata(self.c1_hist)
        self.hist_vline.set_xdata([self.hist_i] * 2)

        self.hist_i += 1
        self.hist_i %= HIST_LEN

        if (self.hist_i % 10) == 0:
            self.ax2.relim()
            self.ax2.autoscale_view(scalex=False, scaley=True)
            #self.ax2.autoscale()

        self.fig2.canvas.draw()

    def on_fast_timer_checkbox(self, e):
        fast_timer_enable = self.frame.m_checkBox_picoharp_fastreadout.GetValue(
        )
        if fast_timer_enable:
            self.fast_timer.Start(100)
        else:
            self.fast_timer.Stop()

    def on_start_scan(self, e):
        print "start scan"

        self.scanning = True

        # get scan parameters:
        self.x0 = float(self.frame.m_textCtrl_x0.GetValue())
        self.x1 = float(self.frame.m_textCtrl_x1.GetValue())
        self.y0 = float(self.frame.m_textCtrl_y0.GetValue())
        self.y1 = float(self.frame.m_textCtrl_y1.GetValue())

        self.dx = float(self.frame.m_textCtrl_dx.GetValue()) / 1000.
        self.dy = float(self.frame.m_textCtrl_dy.GetValue()) / 1000.

        self.x_array = np.arange(self.x0, self.x1, self.dx, dtype=float)
        self.y_array = np.arange(self.y0, self.y1, self.dy, dtype=float)

        self.Nx = len(self.x_array)
        self.Ny = len(self.y_array)

        print "Nx, Ny", self.Nx, self.Ny

        self.tacq = int(float(self.frame.m_textCtrl_tacq.GetValue()) * 1000)
        self.phrange = int(self.frame.m_textCtrl_phrange.GetValue())
        self.phoffset = int(self.frame.m_textCtrl_phoffset.GetValue())
        self.syncdiv = int(
            self.frame.m_choice_syncdivider.GetString(
                self.frame.m_choice_syncdivider.GetSelection()))

        self.zerocross0 = int(self.frame.m_spinCtrl_zerocross0.GetValue())
        self.zerocross1 = int(self.frame.m_spinCtrl_zerocross0.GetValue())

        self.level0 = int(self.frame.m_textCtrl_level0.GetValue())
        self.level1 = int(self.frame.m_textCtrl_level1.GetValue())

        # create data arrays
        #self.integrated_count_map_c0 = np.zeros((self.Nx, self.Ny), dtype=int)
        self.integrated_count_map_c1 = np.zeros((self.Ny, self.Nx), dtype=int)
        self.time_trace_map = np.zeros(
            (self.Ny, self.Nx, self.STORED_HISTCHAN), dtype=int)

        print "shape:", self.integrated_count_map_c1.shape, self.time_trace_map.shape

        #update figure
        self.aximg = self.ax.imshow(
            self.integrated_count_map_c1,
            origin='lower',
            vmin=1e4,
            vmax=1e5,
            interpolation='nearest',
            extent=[self.x0, self.x1, self.y0, self.y1])
        self.wxfig.redraw()

        # set up experiment
        self.picoharp.setup_experiment(Range=self.phrange,
                                       Offset=self.phoffset,
                                       Tacq=self.tacq,
                                       SyncDivider=self.syncdiv,
                                       CFDZeroCross0=self.zerocross0,
                                       CFDLevel0=self.level0,
                                       CFDZeroCross1=self.zerocross1,
                                       CFDLevel1=self.level1)

        line_time0 = time.time()
        for jj in range(self.Ny):
            #self.nanodrive.SetY(self.y_array[j])
            if not self.scanning:
                break
            y = self.y_array[jj]
            #self.lockinstage.sety(y)
            #self.read_from_hardware()
            self.nanodrive.set_pos_ax(y, 1)
            self.read_from_hardware()
            y = self.ypos
            print "line time:", time.time() - line_time0
            print "pixel time:", float(time.time() - line_time0) / len(
                self.x_array)
            line_time0 = time.time()
            """xdelta = self.x_array[0] -  self.xpos 
            if abs(xdelta) > 2.0:
                if xdelta > 0:
                    step = +1.0
                elif xdelta < 0:
                    step = -1.0
                for x in np.arange(self.xpos, self.x_array[0], step):
                    self.nanodrive.set_pos_ax(x, 2)
                    wx_yielded_sleep(0.1)
            """
            if jj % 2:  #odd lines
                x_line_indicies = range(self.Nx)
            else:  #even lines -- traverse in opposite direction
                x_line_indicies = range(self.Nx)[::-1]

            for ii in x_line_indicies:
                if not self.scanning:
                    break
                x = self.xpos = self.x_array[ii]
                #print ii,x
                wx.Yield()
                #self.nanodrive.SetX(self.x_array[i])
                #self.nanodrive.set_pos(x, y)
                #print "nanodrive set_pos: ", x, y
                #self.lockinstage.setx(x)
                self.nanodrive.set_pos_ax(x, 2)
                #if self.HARDWARE_DEBUG: print "lockin stage moved to ", x, y

                ph = self.picoharp

                ph.start_histogram(Tacq=self.tacq)
                while not ph.check_done_scanning():
                    wx.Yield()

                ph.stop_histogram()
                hist_data = ph.read_histogram_data()

                self.time_trace_map[jj,
                                    ii, :] = hist_data[0:self.STORED_HISTCHAN]

                self.integrated_count_map_c1[jj, ii] = sum(hist_data)

                #x1, y1 = self.nanodrive.get_pos()
                #print "get pos: ", x1,y1

                # update display
                try:
                    self.update_display()
                except Exception, err:
                    print "Failed to update_display", err

                if not (ii % 5):
                    #self.update_figure()
                    try:
                        #print "updating figure"
                        #self.read_from_hardware()
                        self.aximg.set_data(self.integrated_count_map_c1)
                        try:
                            count_min = np.min(
                                self.integrated_count_map_c1[np.nonzero(
                                    self.integrated_count_map_c1)])
                        except Exception as err:
                            count_min = 0
                        count_max = np.max(self.integrated_count_map_c1)
                        self.aximg.set_clim(count_min, count_max + 1)
                        self.wxfig.redraw()
                    except Exception, err:
                        print "Failed to update figure:", err
예제 #6
0
    def __init__(self):
        ui_loader = QtUiTools.QUiLoader()
        print os.path.join(__file__,"scanning_confocal_mcl_lj.ui")
        ui_file = QtCore.QFile("scanning_confocal_mcl_lj.ui")
        ui_file.open(QtCore.QFile.ReadOnly); 
        self.ui = ui_loader.load(ui_file)
        ui_file.close()

        self.HARDWARE_DEBUG = HARDWARE_DEBUG
        
        self.fig2d = Figure()
        self.ax2d = self.fig2d.add_subplot(111)
        self.ax2d.plot([0,1])
        self.canvas2d = FigureCanvas(self.fig2d)
        self.ui.plot2D_verticalLayout.addWidget(self.canvas2d)
        self.navtoolbar_plot2d = NavigationToolbar2(self.canvas2d, self.ui)
        self.ui.plot2D_verticalLayout.addWidget( self.navtoolbar_plot2d )


        self.fig_opt = Figure()
        self.ax_opt = self.fig_opt.add_subplot(111)
        
        self.canvas_opt = FigureCanvas(self.fig_opt)
        self.ui.plot_optimize_verticalLayout.addWidget(self.canvas_opt)
        self.navtoolbar_plot_opt = NavigationToolbar2(self.canvas_opt, self.ui)
        self.ui.plot_optimize_verticalLayout.addWidget( self.navtoolbar_plot_opt )

        self.optimize_history = np.zeros(OPTIMIZE_HISTORY_LEN, dtype=np.float)
        self.optimize_ii = 0
        self.optimize_line, = self.ax_opt.plot(self.optimize_history)
        self.optimize_current_pos = self.ax_opt.axvline(self.optimize_ii, color='r')

        ##################### hardware #########################################
        
        self.scanning = False

        ######## MCL NanoDrive Stage ###########################################
        self.nanodrive = MCLNanoDrive(debug=self.HARDWARE_DEBUG)
        try:
            self.hmax = self.nanodrive.cal[HAXIS_ID]
            self.vmax = self.nanodrive.cal[VAXIS_ID]
            self.ui.maxdim_label.setText("max: %g x %g um" % (self.hmax, self.vmax) )
        except Exception as e:
            print e
            self.ui.maxdim_label.setText("max: ? x ? um")
        
        
        # Logged Quantities
        self.x_position = LoggedQuantity(name = 'x_position', dtype=np.float)
        self.y_position = LoggedQuantity(name = 'y_position', dtype=np.float)
        self.z_position = LoggedQuantity(name = 'z_position', dtype=np.float)
        
        
        #self.x_position.updated_value.connect(self.ui.cx_lcdNumber.display)
        self.x_position.updated_value.connect(self.ui.cx_doubleSpinBox.setValue)
        self.ui.x_set_lineEdit.returnPressed.connect(self.x_position.update_value)

        #self.y_position.updated_value.connect(self.ui.cy_lcdNumber.display)
        self.y_position.updated_value.connect(self.ui.cy_doubleSpinBox.setValue)
        self.ui.y_set_lineEdit.returnPressed.connect(self.y_position.update_value)

        #self.z_position.updated_value.connect(self.ui.cz_lcdNumber.display)
        self.z_position.updated_value.connect(self.ui.cz_doubleSpinBox.setValue)
        self.ui.z_set_lineEdit.returnPressed.connect(self.z_position.update_value)


        self.x_position.hardware_set_func = lambda x: self.nanodrive.set_pos_ax(x, XAXIS_ID)
        self.y_position.hardware_set_func = lambda y: self.nanodrive.set_pos_ax(y, YAXIS_ID)
        self.z_position.hardware_set_func = lambda z: self.nanodrive.set_pos_ax(z, ZAXIS_ID)
        

        ####### LabJack (apd) counter readout ##################################
        self.lj_counter = labjack_counter.LabJackCounter()

        self.apd_count_rate = LoggedQuantity(name = 'apd_count_rate', dtype=np.float, fmt="%e")

        self.apd_count_rate.updated_text_value.connect(self.ui.apd_counter_output_lineEdit.setText)

        ########################################################################        

        self.read_from_hardware()

        self.update_display()

        # update figure
        self.ax2d.set_xlim(0, self.hmax)
        self.ax2d.set_ylim(0, self.vmax)

        # events

        self.ui.scan_start_pushButton.clicked.connect(self.on_scan_start)
        self.ui.scan_stop_pushButton.clicked.connect(self.on_scan_stop)

        self.ui.fast_update_checkBox.stateChanged.connect(self.on_fast_timer_checkbox)

        self.ui.clearfig_pushButton.clicked.connect(self.on_clearfig)

        ### timers
        
        self.timer = QtCore.QTimer(self.ui)
        self.timer.timeout.connect(self.on_timer)
        self.timer.start(TIMER_MS)
        
        self.fast_timer = QtCore.QTimer(self.ui)
        self.fast_timer.timeout.connect(self.on_fast_timer)
        
        self.display_update_when_scanning_timer = QtCore.QTimer(self.ui)
        self.display_update_when_scanning_timer.timeout.connect(self.on_display_update_when_scanning_timer)
예제 #7
0
class ScanningConfocalUI:
    def __del__ ( self ): 
        self.ui = None

    def show(self): 
        #self.ui.exec_()
        self.ui.show()

    def __init__(self):
        ui_loader = QtUiTools.QUiLoader()
        print os.path.join(__file__,"scanning_confocal_mcl_lj.ui")
        ui_file = QtCore.QFile("scanning_confocal_mcl_lj.ui")
        ui_file.open(QtCore.QFile.ReadOnly); 
        self.ui = ui_loader.load(ui_file)
        ui_file.close()

        self.HARDWARE_DEBUG = HARDWARE_DEBUG
        
        self.fig2d = Figure()
        self.ax2d = self.fig2d.add_subplot(111)
        self.ax2d.plot([0,1])
        self.canvas2d = FigureCanvas(self.fig2d)
        self.ui.plot2D_verticalLayout.addWidget(self.canvas2d)
        self.navtoolbar_plot2d = NavigationToolbar2(self.canvas2d, self.ui)
        self.ui.plot2D_verticalLayout.addWidget( self.navtoolbar_plot2d )


        self.fig_opt = Figure()
        self.ax_opt = self.fig_opt.add_subplot(111)
        
        self.canvas_opt = FigureCanvas(self.fig_opt)
        self.ui.plot_optimize_verticalLayout.addWidget(self.canvas_opt)
        self.navtoolbar_plot_opt = NavigationToolbar2(self.canvas_opt, self.ui)
        self.ui.plot_optimize_verticalLayout.addWidget( self.navtoolbar_plot_opt )

        self.optimize_history = np.zeros(OPTIMIZE_HISTORY_LEN, dtype=np.float)
        self.optimize_ii = 0
        self.optimize_line, = self.ax_opt.plot(self.optimize_history)
        self.optimize_current_pos = self.ax_opt.axvline(self.optimize_ii, color='r')

        ##################### hardware #########################################
        
        self.scanning = False

        ######## MCL NanoDrive Stage ###########################################
        self.nanodrive = MCLNanoDrive(debug=self.HARDWARE_DEBUG)
        try:
            self.hmax = self.nanodrive.cal[HAXIS_ID]
            self.vmax = self.nanodrive.cal[VAXIS_ID]
            self.ui.maxdim_label.setText("max: %g x %g um" % (self.hmax, self.vmax) )
        except Exception as e:
            print e
            self.ui.maxdim_label.setText("max: ? x ? um")
        
        
        # Logged Quantities
        self.x_position = LoggedQuantity(name = 'x_position', dtype=np.float)
        self.y_position = LoggedQuantity(name = 'y_position', dtype=np.float)
        self.z_position = LoggedQuantity(name = 'z_position', dtype=np.float)
        
        
        #self.x_position.updated_value.connect(self.ui.cx_lcdNumber.display)
        self.x_position.updated_value.connect(self.ui.cx_doubleSpinBox.setValue)
        self.ui.x_set_lineEdit.returnPressed.connect(self.x_position.update_value)

        #self.y_position.updated_value.connect(self.ui.cy_lcdNumber.display)
        self.y_position.updated_value.connect(self.ui.cy_doubleSpinBox.setValue)
        self.ui.y_set_lineEdit.returnPressed.connect(self.y_position.update_value)

        #self.z_position.updated_value.connect(self.ui.cz_lcdNumber.display)
        self.z_position.updated_value.connect(self.ui.cz_doubleSpinBox.setValue)
        self.ui.z_set_lineEdit.returnPressed.connect(self.z_position.update_value)


        self.x_position.hardware_set_func = lambda x: self.nanodrive.set_pos_ax(x, XAXIS_ID)
        self.y_position.hardware_set_func = lambda y: self.nanodrive.set_pos_ax(y, YAXIS_ID)
        self.z_position.hardware_set_func = lambda z: self.nanodrive.set_pos_ax(z, ZAXIS_ID)
        

        ####### LabJack (apd) counter readout ##################################
        self.lj_counter = labjack_counter.LabJackCounter()

        self.apd_count_rate = LoggedQuantity(name = 'apd_count_rate', dtype=np.float, fmt="%e")

        self.apd_count_rate.updated_text_value.connect(self.ui.apd_counter_output_lineEdit.setText)

        ########################################################################        

        self.read_from_hardware()

        self.update_display()

        # update figure
        self.ax2d.set_xlim(0, self.hmax)
        self.ax2d.set_ylim(0, self.vmax)

        # events

        self.ui.scan_start_pushButton.clicked.connect(self.on_scan_start)
        self.ui.scan_stop_pushButton.clicked.connect(self.on_scan_stop)

        self.ui.fast_update_checkBox.stateChanged.connect(self.on_fast_timer_checkbox)

        self.ui.clearfig_pushButton.clicked.connect(self.on_clearfig)

        ### timers
        
        self.timer = QtCore.QTimer(self.ui)
        self.timer.timeout.connect(self.on_timer)
        self.timer.start(TIMER_MS)
        
        self.fast_timer = QtCore.QTimer(self.ui)
        self.fast_timer.timeout.connect(self.on_fast_timer)
        
        self.display_update_when_scanning_timer = QtCore.QTimer(self.ui)
        self.display_update_when_scanning_timer.timeout.connect(self.on_display_update_when_scanning_timer)

        
        
    @QtCore.Slot()
    def on_clearfig(self):
        self.fig2d.clf()
        self.ax2d = self.fig2d.add_subplot(111)
        self.ax2d.plot([0,1])    
        # update figure
        self.ax2d.set_xlim(0, self.hmax)
        self.ax2d.set_ylim(0, self.vmax)
        self.fig2d.canvas.draw()

        
    def read_from_hardware(self):
        self.stage_pos = self.nanodrive.get_pos()
        self.c0_rate = self.lj_counter.read_rates()[0]

    def update_display(self):
        self.x_position.update_value(self.stage_pos[XAXIS_ID-1], update_hardware=False)
        self.y_position.update_value(self.stage_pos[YAXIS_ID-1], update_hardware=False)
        self.z_position.update_value(self.stage_pos[ZAXIS_ID-1], update_hardware=False)
        self.apd_count_rate.update_value(self.c0_rate)

    @QtCore.Slot()
    def on_timer(self):
        self.read_from_hardware()
        self.update_display()

    @QtCore.Slot()
    def on_fast_timer(self):
        self.c0_rate = self.lj_counter.read_rates()[0]
        self.apd_count_rate.update_value(self.c0_rate)
        #print self.c0_rate
        
        
        self.optimize_ii += 1
        self.optimize_ii %= OPTIMIZE_HISTORY_LEN
        ii = self.optimize_ii
        
        self.optimize_history[ii] = self.c0_rate
        self.optimize_line.set_ydata(self.optimize_history)
        self.optimize_current_pos.set_xdata((ii,ii))
        if (ii % 10) == 0:
            self.ax_opt.relim()
            self.ax_opt.autoscale_view(scalex=False, scaley=True)
        
        #print "redraw"
        self.fig_opt.canvas.draw()
            
    @QtCore.Slot(bool)
    def on_fast_timer_checkbox(self, fast_timer_enable):
        if fast_timer_enable:
            self.fast_timer.start(100)
            print "fast timer start"
        else:
            self.fast_timer.stop()
            print "fast timer stop"        

    @QtCore.Slot()            
    def on_scan_start(self):
        print "start scan"
        
        self.scanning = True
        
        QtGui.QApplication.processEvents()

        #get scan parameters:
        self.h0 = self.ui.h0_doubleSpinBox.value()
        self.h1 = self.ui.h1_doubleSpinBox.value()
        self.v0 = self.ui.v0_doubleSpinBox.value()
        self.v1 = self.ui.v1_doubleSpinBox.value()
    
        self.dh = 1e-3*self.ui.dh_spinBox.value()
        self.dv = 1e-3*self.ui.dv_spinBox.value()

        self.h_array = np.arange(self.h0, self.h1, self.dh, dtype=float)
        self.v_array = np.arange(self.v0, self.v1, self.dv, dtype=float)
        
        self.Nh = len(self.h_array)
        self.Nv = len(self.v_array)
        
        self.pixel_time = self.ui.pixel_time_doubleSpinBox.value()
        
        
        ### create data arrays
        self.count_rate_map = np.zeros((self.Nv, self.Nh), dtype=np.float)
        
        print "shape:", self.count_rate_map.shape
        
        print "Nh, Nv", self.Nh, self.Nv    

        ### update figure
        self.ax_2d_img = self.ax2d.imshow(self.count_rate_map, 
                                    origin='lower',
                                    vmin=1e4, vmax=1e5, interpolation='nearest', 
                                    extent=[self.h0, self.h1, self.v0, self.v1])
        self.fig2d.canvas.draw()
        
        
        self.timer.stop() #stop the normal timer
        
        #display_timer_update = 0.5*self.pixel_time*1e3
        #if display_timer_update < 200
        
        self.display_update_when_scanning_timer.start(100)
        
        # Scan!            
        line_time0 = time.time()
        for i_v in range(self.Nv):
            if not self.scanning:
                break
            self.v_pos = self.v_array[i_v]
            self.nanodrive.set_pos_ax(self.v_pos, VAXIS_ID)
            self.read_from_hardware()
            
            print "line time:", time.time() - line_time0
            print "pixel time:", float(time.time() - line_time0)/self.Nh
            line_time0 = time.time()
            
            if i_v % 2: #odd lines
                h_line_indicies = range(self.Nh)
            else:       #even lines -- traverse in oposite direction
                h_line_indicies = range(self.Nh)[::-1]
            
            
            for i_h in h_line_indicies:
                if not self.scanning:
                    break
                self.h_pos = self.h_array[i_h]
                self.nanodrive.set_pos_ax(self.h_pos, XAXIS_ID)
                
                
                time0 = time.time()
                while time.time() - time0 < self.pixel_time:
                    QtGui.QApplication.processEvents() #release       
                
                counts = self.lj_counter.read_rates()
                #self.count_rate_map[i_v,i_h] = counts[-1] # grab integration time for now
                self.c0_rate = counts[0]
                self.count_rate_map[i_v,i_h] = self.c0_rate # grab count0 rate
                
                """self.ccd.start_acquisition()
                stat = "ACQUIRING"
                while stat!= "IDLE":
                    wx_yielded_sleep(self.ccd.exposure_time * 0.25)
                    stati, stat = self.ccd.get_status()
                self.ccd.get_acquired_data()
                
                spectrum = np.sum(self.ccd.buffer[ROW0:ROW1], axis=0)
                
                self.spectrum_map[jj,ii,:] = spectrum
                
                self.integrated_count_map[jj,ii] = sum(spectrum)
                """

   
        self.on_scan_stop()
        
        
    @QtCore.Slot()            
    def on_scan_stop(self):
        print "on_scan_stop"
        self.scanning = False
        self.update_display()

        self.timer.start() #restart the normal timer
        self.display_update_when_scanning_timer.stop()

        # clean up after scan
        self.ax_2d_img.set_data(self.count_rate_map)
        self.fig2d.canvas.draw()
        self.update_display()
        self.scanning = False
        print "scanning done"
    
        """
        print "saving data..."
        t0 = time.time()
        #np.savetxt("%i_confocal_scan.csv" % t0, 
        #           self.integrated_count_map, delimiter=',')
        
        save_params = ["spectrum_map", "x0", "x1", "y0", "y1",
                       "Nx", "Ny", 
                       "x_array", "y_array",
                       "dx", "dy", "integrated_count_map"]
        save_dict = dict()
        for key in save_params:
            save_dict[key] = getattr(self, key)
        

        for key in ["XAXIS_ID", "YAXIS_ID","HARDWARE_DEBUG","ANDOR_HFLIP","ANDOR_VFLIP","ANDOR_AD_CHAN","ROW0","ROW1"]:
            save_dict[key] = globals()[key]
        
        for key in ["wl", "gratings", "grating"]:
            save_dict["spec_"+key] = getattr(self.spec, key)
        
        for key in ["exposure_time", "em_gain", "temperature", "ad_chan", "ro_mode", "Nx", "Ny"]:
            save_dict["andor_"+key] = getattr(self.ccd, key)
            
        
        save_dict["time_saved"] = t0
        
        np.savez_compressed("%i_spec_map.npz" % t0, **save_dict)
        print "data saved"
        """


    
    @QtCore.Slot()
    def on_display_update_when_scanning_timer(self):
        # update display
        try:
            self.update_display()
        except Exception, err:
            print "Failed to update_display", err
        
        try:
            #self.spec_plotline.set_ydata(spectrum)
            #self.ax_speclive.relim()
            #self.ax_speclive.autoscale_view(tight=None, scalex=False, scaley=True)
            #self.fig2.canvas.draw()
            pass
        except Exception as err:
            print "Failed to update spectrum plot", err
        
        try:
            #print "updating figure"
            #self.read_from_hardware()
            self.ax_2d_img.set_data(self.count_rate_map)
            try:
                count_min =  np.min(self.count_rate_map[np.nonzero(self.count_rate_map)])
            except Exception as err:
                count_min = 0
            count_max = np.max(self.count_rate_map)
            self.ax_2d_img.set_clim(count_min, count_max + 1)
            self.fig2d.canvas.draw()
        except Exception, err:
            print "Failed to update figure:", err