Ejemplo n.º 1
0
    def _setup_graph(self):
        self.plot_container = HPlotContainer()
        g = self.stream_graph
        g.clear()
        g.new_plot(padding_left=70, padding_right=10)
        g.new_series(plotid=0)
        g.set_y_title('Lumens', plotid=0)
        g.new_plot(padding_left=70, padding_right=10)
        g.new_series(plotid=1)
        g.set_y_title('Error', plotid=1)
        g.new_plot(padding_left=70, padding_right=10)
        g.new_series(plotid=2)
        g.set_y_title('Output', plotid=2)

        g = self.img_graph
        g.clear()
        imgplot = g.new_plot(padding_right=10)
        imgplot.aspect_ratio = 1.0
        imgplot.x_axis.visible = False
        imgplot.y_axis.visible = False
        imgplot.x_grid.visible = False
        imgplot.y_grid.visible = False

        imgplot.data.set_data('imagedata', zeros((150, 150, 3), dtype=uint8))
        imgplot.img_plot('imagedata', origin='top left')

        self.plot_container.add(self.stream_graph.plotcontainer)
        self.plot_container.add(self.img_graph.plotcontainer)
Ejemplo n.º 2
0
    def _setup_graph(self):
        self.plot_container = HPlotContainer()
        g = self.stream_graph
        g.clear()
        g.new_plot(padding_left=70, padding_right=10)
        g.new_series(plotid=0)
        g.set_y_title('Lumens', plotid=0)
        g.new_plot(padding_left=70, padding_right=10)
        g.new_series(plotid=1)
        g.set_y_title('Error', plotid=1)
        g.new_plot(padding_left=70, padding_right=10)
        g.new_series(plotid=2)
        g.set_y_title('Output', plotid=2)

        g = self.img_graph
        g.clear()
        imgplot = g.new_plot(padding_right=10)
        imgplot.aspect_ratio = 1.0
        imgplot.x_axis.visible = False
        imgplot.y_axis.visible = False
        imgplot.x_grid.visible = False
        imgplot.y_grid.visible = False

        imgplot.data.set_data('imagedata', zeros((150, 150, 3), dtype=uint8))
        imgplot.img_plot('imagedata', origin='top left')

        self.plot_container.add(self.stream_graph.plotcontainer)
        self.plot_container.add(self.img_graph.plotcontainer)
Ejemplo n.º 3
0
    def setup_graph(self, an):

        container = HPlotContainer()

        sg = StackedGraph()
        sg.plotcontainer.spacing = 5
        sg.plotcontainer.stack_order = 'top_to_bottom'

        isos = an.sorted_values(reverse=False)
        for i, iso in enumerate(isos):
            sniff = iso.sniff
            sg.new_plot(ytitle=iso.name, xtitle='Time (s)', title='Equilibration')
            if sniff.xs.shape[0]:
                sg.new_series(sniff.offset_xs, sniff.ys, marker='circle', type='scatter')
            sg.set_y_limits(pad='0.1', plotid=i)

        bg = StackedRegressionGraph()
        bg.plotcontainer.spacing = 5
        bg.plotcontainer.stack_order = 'top_to_bottom'

        for i, iso in enumerate(isos):
            baseline = iso.baseline
            bg.new_plot(ytitle=baseline.detector, xtitle='Time (s)', title='Baseline')
            if baseline.xs.shape[0]:
                bg.new_series(baseline.offset_xs, baseline.ys,
                              color='red', type='scatter', fit=baseline.fit)
            bg.set_y_limits(pad='0.1', plotid=i)

        ig = StackedRegressionGraph()
        ig.plotcontainer.spacing = 5
        ig.plotcontainer.stack_order = 'top_to_bottom'

        for i, iso in enumerate(isos):
            ig.new_plot(ytitle=iso.name, xtitle='Time (s)', title='Isotope')
            if iso.xs.shape[0]:
                ig.new_series(iso.offset_xs, iso.ys,
                              color='blue', type='scatter', fit=iso.fit)
            ig.set_y_limits(pad='0.1', plotid=i)

        container.add(sg.plotcontainer)
        container.add(ig.plotcontainer)
        container.add(bg.plotcontainer)

        self.container = container
Ejemplo n.º 4
0
    def _matrix_plot_container_default(self):
        matrix = np.nan_to_num(self.matrix)
        width = matrix.shape[0]

        # create a plot data object and give it this data
        plot_data = ArrayPlotData()
        plot_data.set_data("values", matrix)

        # create the plot
        plot = Plot(plot_data, origin=self.origin)

        img_plot = plot.img_plot("values",
                                 interpolation='nearest',
                                 xbounds=(0, width),
                                 ybounds=(0, width),
                                 colormap=self._create_colormap())[0]

        #### fix axes
        self._remove_grid_and_axes(plot)

        axis = self._create_increment_one_axis(plot, 0.5, width, 'bottom')
        self._add_value_axis(plot, axis)

        axis = self._create_increment_one_axis(
            plot,
            0.5,
            width,
            'left',
            ticks=[str(i) for i in range(width - 1, -1, -1)])
        self._add_index_axis(plot, axis)

        #### tweak plot attributes
        self._set_title(plot)
        plot.aspect_ratio = 1.
        # padding [left, right, up, down]
        plot.padding = [0, 0, 25, 25]

        # create the colorbar, handing in the appropriate range and colormap
        colormap = img_plot.color_mapper
        colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range),
                            color_mapper=colormap,
                            plot=img_plot,
                            orientation='v',
                            resizable='v',
                            width=20,
                            padding=[0, 20, 0, 0])
        colorbar.padding_top = plot.padding_top
        colorbar.padding_bottom = plot.padding_bottom

        # create a container to position the plot and the colorbar side-by-side
        container = HPlotContainer(use_backbuffer=True)
        container.add(plot)
        container.add(colorbar)
        container.bgcolor = 0xFFFFFF

        self.decorate_plot(container, self.matrix)
        return container
Ejemplo n.º 5
0
    def _plot_default(self):
        distr_len = len(self.data)

        # PolygonPlot holding the circles of the Hinton diagram
        polyplot = Plot(self.plot_data)
        for idx in range(distr_len):
            p = polyplot.plot(('x%d' % idx, 'y%d' % idx),
                              type="polygon",
                              face_color=get_class_color(idx),
                              edge_color='black')

        self._set_title(polyplot)
        self._remove_grid_and_axes(polyplot)

        # create x axis for labels
        axis = self._create_increment_one_axis(polyplot, 1., distr_len,
                                               'bottom')
        self._add_index_axis(polyplot, axis)

        # create y axis for probability density
        #prob_axis = self._create_probability_axis(polyplot)
        #polyplot.value_axis = prob_axis
        #polyplot.underlays.append(prob_axis)

        # tweak some of the plot properties
        range2d = DataRange2D(low=(0.5, 0.), high=(distr_len + 0.5, 1.))
        polyplot.range2d = range2d
        polyplot.aspect_ratio = ((range2d.x_range.high - range2d.x_range.low) /
                                 (range2d.y_range.high - range2d.y_range.low))

        polyplot.border_visible = False
        polyplot.padding = [0, 0, 25, 25]

        # create a container to position the plot and the colorbar side-by-side
        container = HPlotContainer(use_backbuffer=True, valign='center')
        container.add(polyplot)
        container.bgcolor = 0xFFFFFF  # light gray: 0xEEEEEE

        self.decorate_plot(container, self.data)
        return container
Ejemplo n.º 6
0
    def _plot_default(self):
        distr_len = len(self.data)

        # PolygonPlot holding the circles of the Hinton diagram
        polyplot = Plot(self.plot_data)
        for idx in range(distr_len):
            p = polyplot.plot(('x%d' % idx, 'y%d' % idx),
                          type="polygon",
                          face_color=get_class_color(idx),
                          edge_color='black')

        self._set_title(polyplot)
        self._remove_grid_and_axes(polyplot)

        # create x axis for labels
        axis = self._create_increment_one_axis(polyplot, 1., distr_len, 'bottom')
        self._add_index_axis(polyplot, axis)

        # create y axis for probability density
        #prob_axis = self._create_probability_axis(polyplot)
        #polyplot.value_axis = prob_axis
        #polyplot.underlays.append(prob_axis)

        # tweak some of the plot properties
        range2d = DataRange2D(low=(0.5, 0.), high=(distr_len+0.5, 1.))
        polyplot.range2d = range2d
        polyplot.aspect_ratio = ((range2d.x_range.high - range2d.x_range.low)
                                 / (range2d.y_range.high - range2d.y_range.low))

        polyplot.border_visible = False
        polyplot.padding = [0, 0, 25, 25]

        # create a container to position the plot and the colorbar side-by-side
        container = HPlotContainer(use_backbuffer=True, valign='center')
        container.add(polyplot)
        container.bgcolor = 0xFFFFFF # light gray: 0xEEEEEE

        self.decorate_plot(container, self.data)
        return container
Ejemplo n.º 7
0
    def make_component(self, padding):
        cg = ContourGraph()

        cg.new_plot(title='Beam Space',
                    xtitle='X mm',
                    ytitle='Y mm',
                    aspect_ratio=1
                    )

        g = Graph()
        g.new_plot(title='Motor Space',
                     xtitle='X mm',
                     ytitle='Power',
                     )
        g.new_series(
                     )

        self.graph = g
        self.contour_graph = cg
        c = HPlotContainer()
        c.add(g.plotcontainer)
        c.add(cg.plotcontainer)

        return c
Ejemplo n.º 8
0
    def _matrix_plot_container_default(self):
        matrix = np.nan_to_num(self.matrix)
        width = matrix.shape[0]

        # create a plot data object and give it this data
        plot_data = ArrayPlotData()
        plot_data.set_data("values", matrix)

        # create the plot
        plot = Plot(plot_data, origin=self.origin)

        img_plot = plot.img_plot("values",
                                 interpolation='nearest',

                                 xbounds=(0, width),
                                 ybounds=(0, width),
                                 colormap=self._create_colormap())[0]

        #### fix axes
        self._remove_grid_and_axes(plot)

        axis = self._create_increment_one_axis(plot, 0.5, width, 'bottom')
        self._add_value_axis(plot, axis)

        axis = self._create_increment_one_axis(
            plot, 0.5, width, 'left',
            ticks=[str(i) for i in range(width-1, -1, -1)])
        self._add_index_axis(plot, axis)

        #### tweak plot attributes
        self._set_title(plot)
        plot.aspect_ratio = 1.
        # padding [left, right, up, down]
        plot.padding = [0, 0, 25, 25]

        # create the colorbar, handing in the appropriate range and colormap
        colormap = img_plot.color_mapper
        colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range),
                            color_mapper=colormap,
                            plot=img_plot,
                            orientation='v',
                            resizable='v',
                            width=20,
                            padding=[0, 20, 0, 0])
        colorbar.padding_top = plot.padding_top
        colorbar.padding_bottom = plot.padding_bottom

        # create a container to position the plot and the colorbar side-by-side
        container = HPlotContainer(use_backbuffer=True)
        container.add(plot)
        container.add(colorbar)
        container.bgcolor = 0xFFFFFF

        self.decorate_plot(container, self.matrix)
        return container
Ejemplo n.º 9
0
    def make_component(self, padding):
        cg = ContourGraph()

        cg.new_plot(title='Beam Space',
                    xtitle='X mm',
                    ytitle='Y mm',
                    aspect_ratio=1)

        g = Graph()
        g.new_plot(
            title='Motor Space',
            xtitle='X mm',
            ytitle='Power',
        )
        g.new_series()

        self.graph = g
        self.contour_graph = cg
        c = HPlotContainer()
        c.add(g.plotcontainer)
        c.add(cg.plotcontainer)

        return c
Ejemplo n.º 10
0
 def _container_factory(self):
     pc = HPlotContainer(padding=[5, 5, 5, 20])
     return pc
Ejemplo n.º 11
0
    def _plot_container_default(self):
        data = self.posterior
        nannotations, nclasses = data.shape

        # create a plot data object
        plot_data = ArrayPlotData()
        plot_data.set_data("values", data)

        # create the plot
        plot = Plot(plot_data, origin=self.origin)

        img_plot = plot.img_plot("values",
                                 interpolation='nearest',
                                 xbounds=(0, nclasses),
                                 ybounds=(0, nannotations),
                                 colormap=self._create_colormap())[0]
        ndisp = 55
        img_plot.y_mapper.range.high = ndisp
        img_plot.y_mapper.domain_limits=((0, nannotations))

        self._set_title(plot)
        plot.padding_top = 80

        # create x axis for labels
        label_axis = self._create_increment_one_axis(plot, 0.5, nclasses, 'top')
        label_axis.title = 'classes'
        self._add_index_axis(plot, label_axis)

        plot.y_axis.title = 'items'

        # tweak plot aspect
        goal_aspect_ratio = 2.0
        plot_width = (goal_aspect_ratio * self.plot_height
                      * nclasses / ndisp)
        self.plot_width = min(max(plot_width, 200), 400)
        plot.aspect_ratio = self.plot_width / self.plot_height

        # add colorbar
        colormap = img_plot.color_mapper
        colorbar = ColorBar(index_mapper = LinearMapper(range=colormap.range),
                            color_mapper = colormap,
                            plot = img_plot,
                            orientation = 'v',
                            resizable = '',
                            width = 15,
                            height = 250)
        colorbar.padding_top = plot.padding_top
        colorbar.padding_bottom = int(self.plot_height - colorbar.height -
                                      plot.padding_top)
        colorbar.padding_left = 0
        colorbar.padding_right = 30


        # create a container to position the plot and the colorbar side-by-side
        container = HPlotContainer(use_backbuffer=True)
        container.add(plot)
        container.add(colorbar)
        container.bgcolor = 0xFFFFFF # light gray: 0xEEEEEE

        # add pan tools
        img_plot.tools.append(PanTool(img_plot, constrain=True,
                                      constrain_direction="y", speed=7.))

        self.decorate_plot(container, self.posterior)
        self.plot_posterior = plot
        return container
Ejemplo n.º 12
0
 def _container_default(self):
     hp = HPlotContainer(padding=0, spacing=0)
     return hp
Ejemplo n.º 13
0
    def _plot_container_default(self):
        data = self.posterior
        nannotations, nclasses = data.shape

        # create a plot data object
        plot_data = ArrayPlotData()
        plot_data.set_data("values", data)

        # create the plot
        plot = Plot(plot_data, origin=self.origin)

        img_plot = plot.img_plot("values",
                                 interpolation='nearest',
                                 xbounds=(0, nclasses),
                                 ybounds=(0, nannotations),
                                 colormap=self._create_colormap())[0]
        ndisp = 55
        img_plot.y_mapper.range.high = ndisp
        img_plot.y_mapper.domain_limits = ((0, nannotations))

        self._set_title(plot)
        plot.padding_top = 80

        # create x axis for labels
        label_axis = self._create_increment_one_axis(plot, 0.5, nclasses,
                                                     'top')
        label_axis.title = 'classes'
        self._add_index_axis(plot, label_axis)

        plot.y_axis.title = 'items'

        # tweak plot aspect
        goal_aspect_ratio = 2.0
        plot_width = (goal_aspect_ratio * self.plot_height * nclasses / ndisp)
        self.plot_width = min(max(plot_width, 200), 400)
        plot.aspect_ratio = self.plot_width / self.plot_height

        # add colorbar
        colormap = img_plot.color_mapper
        colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range),
                            color_mapper=colormap,
                            plot=img_plot,
                            orientation='v',
                            resizable='',
                            width=15,
                            height=250)
        colorbar.padding_top = plot.padding_top
        colorbar.padding_bottom = int(self.plot_height - colorbar.height -
                                      plot.padding_top)
        colorbar.padding_left = 0
        colorbar.padding_right = 30

        # create a container to position the plot and the colorbar side-by-side
        container = HPlotContainer(use_backbuffer=True)
        container.add(plot)
        container.add(colorbar)
        container.bgcolor = 0xFFFFFF  # light gray: 0xEEEEEE

        # add pan tools
        img_plot.tools.append(
            PanTool(img_plot,
                    constrain=True,
                    constrain_direction="y",
                    speed=7.))

        self.decorate_plot(container, self.posterior)
        self.plot_posterior = plot
        return container
Ejemplo n.º 14
0
class Degasser(Loggable):
    laser_manager = None

    lumens = Float(50)

    pid = Instance(PID)
    stream_graph = Instance(StreamStackedGraph, ())
    img_graph = Instance(Graph, ())
    plot_container = Instance(HPlotContainer, ())

    threshold = Range(0, 100, 25)
    test = Button
    edit_pid_button = Button
    save_button = Button

    _lum_thread = None
    _lum_evt = None
    _luminosity_value = 0
    _testing = False
    _info = None

    def stop(self):
        self.debug('stop')
        self.dump()
        if self._lum_evt:
            self._lum_evt.set()

        if self._info:
            invoke_in_main_thread(self._info.dispose, abort=True)

    @property
    def persistence_path(self):
        return os.path.join(paths.setup_dir, 'pid_degasser.yaml')

    def load(self):
        self.debug('loading')
        self.pid = PID()
        p = self.persistence_path
        if not os.path.isfile(p):
            self.warning('No PID degasser file located at {}'.format(p))
            return

        with open(p, 'rb') as rfile:
            jd = yaml.load(rfile)
            if jd:
                self.threshold = jd['threshold']
                self.pid.load_from_obj(jd['pid'])

    def dump(self):
        self.debug('dump')
        obj = self.pid.get_dump_obj()
        jd = {'pid': obj, 'threshold': self.threshold}
        with open(self.persistence_path, 'wb') as wfile:
            yaml.dump(jd, wfile, encoding='utf-8')

    def degas(self, lumens=None, autostart=True):
        self.load()

        if lumens is None:
            lumens = self.lumens

        self.lumens = lumens
        self._setup_graph()

        # def _open():
        #     self._info = self.edit_traits()
        #
        # invoke_in_main_thread(_open)
        if autostart:
            self.start()

    def start(self):
        self.pid.reset()
        self._lum_evt = Event()
        self._lum_thread = Thread(target=self._degas,
                                  args=(self.lumens, self.pid))
        self._lum_thread.start()

    def _edit_pid_button_fired(self):
        info = self.pid.edit_traits(kind='livemodal')
        if info.result:
            self.dump()

    def _save_button_fired(self):
        self.dump()

    def _test_fired(self):
        if self._testing:
            self.stop()
            self.laser_manager.disable_laser()
            self.stream_graph.export_data(
                path='/Users/argonlab3/Desktop/degas.csv')
        else:
            self.laser_manager.enable_laser()
            self.start()

        self._testing = not self._testing

    def _setup_graph(self):
        self.plot_container = HPlotContainer()
        g = self.stream_graph
        g.clear()
        g.new_plot(padding_left=70, padding_right=10)
        g.new_series(plotid=0)
        g.set_y_title('Lumens', plotid=0)
        g.new_plot(padding_left=70, padding_right=10)
        g.new_series(plotid=1)
        g.set_y_title('Error', plotid=1)
        g.new_plot(padding_left=70, padding_right=10)
        g.new_series(plotid=2)
        g.set_y_title('Output', plotid=2)

        g = self.img_graph
        g.clear()
        imgplot = g.new_plot(padding_right=10)
        imgplot.aspect_ratio = 1.0
        imgplot.x_axis.visible = False
        imgplot.y_axis.visible = False
        imgplot.x_grid.visible = False
        imgplot.y_grid.visible = False

        imgplot.data.set_data('imagedata', zeros((150, 150, 3), dtype=uint8))
        imgplot.img_plot('imagedata', origin='top left')

        self.plot_container.add(self.stream_graph.plotcontainer)
        self.plot_container.add(self.img_graph.plotcontainer)

    def _degas(self, lumens, pid):

        self.lumens = lumens
        g = self.stream_graph
        img = self.img_graph.plots[0]
        ld = self.laser_manager.stage_manager.lumen_detector

        def update(c, e, o, src, targets):
            g.record(c, plotid=0)
            g.record(e, plotid=1)
            g.record(o, plotid=2)

            if src.dtype == uint16:
                src = src.astype('uint32')
                src = src / 4095 * 255
                src = src.astype('uint8')

            imgdata = gray2rgb(src)
            ld.draw_targets(imgdata, targets)

            img.data.set_data('imagedata', imgdata)

        evt = self._lum_evt
        set_laser_power = self.laser_manager.set_laser_power_hook

        ld.reset()

        get_brightness = self.laser_manager.get_brightness
        target = self.lumens
        prev = 0

        sst = time.time()
        while not evt.is_set():
            dt = pid.kdt
            st = time.time()
            src, current, targets = get_brightness(threshold=self.threshold)

            err = target - current
            out = pid.get_value(err) or 0

            invoke_in_main_thread(update, current, err, out, src, targets)

            if abs(prev - out) > 0.02:
                self.debug('set power output={}'.format(out))
                set_laser_power(out)
                prev = out

            if time.time() - sst > 10:
                sst = time.time()
                ld.reset()

            et = time.time() - st
            t = dt - et
            if t > 0:
                evt.wait(dt)

    def traits_view(self):
        v = View(
            VGroup(
                Item('pid', style='custom'), Item('threshold', label='T'),
                Item('test'),
                UItem('plot_container',
                      style='custom',
                      editor=ComponentEditor())))
        return v
Ejemplo n.º 15
0
class Degasser(Loggable):
    laser_manager = None

    lumens = Float(50)

    pid = Instance(PID)
    stream_graph = Instance(StreamStackedGraph, ())
    img_graph = Instance(Graph, ())
    plot_container = Instance(HPlotContainer, ())

    threshold = Range(0,100, 25)
    test = Button
    edit_pid_button = Button
    save_button = Button

    _lum_thread = None
    _lum_evt = None
    _luminosity_value = 0
    _testing = False
    _info = None

    def stop(self):
        self.debug('stop')
        self.dump()
        if self._lum_evt:
            self._lum_evt.set()

        if self._info:
            invoke_in_main_thread(self._info.dispose, abort=True)

    @property
    def persistence_path(self):
        return os.path.join(paths.setup_dir, 'pid_degasser.yaml')

    def load(self):
        self.debug('loading')
        self.pid = PID()
        p = self.persistence_path
        if not os.path.isfile(p):
            self.warning('No PID degasser file located at {}'.format(p))
            return

        with open(p, 'rb') as rfile:
            jd = yaml.load(rfile)
            if jd:
                self.threshold = jd['threshold']
                self.pid.load_from_obj(jd['pid'])

    def dump(self):
        self.debug('dump')
        obj = self.pid.get_dump_obj()
        jd = {'pid': obj, 'threshold': self.threshold}
        with open(self.persistence_path, 'wb') as wfile:
            yaml.dump(jd, wfile, encoding='utf-8')

    def degas(self, lumens=None, autostart=True):
        self.load()

        if lumens is None:
            lumens = self.lumens

        self.lumens = lumens
        self._setup_graph()

        # def _open():
        #     self._info = self.edit_traits()
        #
        # invoke_in_main_thread(_open)
        if autostart:
            self.start()

    def start(self):
        self.pid.reset()
        self._lum_evt = Event()
        self._lum_thread = Thread(target=self._degas, args=(self.lumens, self.pid))
        self._lum_thread.start()

    def _edit_pid_button_fired(self):
        info = self.pid.edit_traits(kind='livemodal')
        if info.result:
            self.dump()

    def _save_button_fired(self):
        self.dump()

    def _test_fired(self):
        if self._testing:
            self.stop()
            self.laser_manager.disable_laser()
            self.stream_graph.export_data(path='/Users/argonlab3/Desktop/degas.csv')
        else:
            self.laser_manager.enable_laser()
            self.start()

        self._testing = not self._testing

    def _setup_graph(self):
        self.plot_container = HPlotContainer()
        g = self.stream_graph
        g.clear()
        g.new_plot(padding_left=70, padding_right=10)
        g.new_series(plotid=0)
        g.set_y_title('Lumens', plotid=0)
        g.new_plot(padding_left=70, padding_right=10)
        g.new_series(plotid=1)
        g.set_y_title('Error', plotid=1)
        g.new_plot(padding_left=70, padding_right=10)
        g.new_series(plotid=2)
        g.set_y_title('Output', plotid=2)

        g = self.img_graph
        g.clear()
        imgplot = g.new_plot(padding_right=10)
        imgplot.aspect_ratio = 1.0
        imgplot.x_axis.visible = False
        imgplot.y_axis.visible = False
        imgplot.x_grid.visible = False
        imgplot.y_grid.visible = False

        imgplot.data.set_data('imagedata', zeros((150, 150, 3), dtype=uint8))
        imgplot.img_plot('imagedata', origin='top left')

        self.plot_container.add(self.stream_graph.plotcontainer)
        self.plot_container.add(self.img_graph.plotcontainer)

    def _degas(self, lumens, pid):

        self.lumens = lumens
        g = self.stream_graph
        img = self.img_graph.plots[0]

        def update(c, e, o, src):
            g.record(c, plotid=0)
            g.record(e, plotid=1)
            g.record(o, plotid=2)

            if src.dtype == uint16:
                src = src.astype('uint32')
                src = src/4095 * 255
                src = src.astype('uint8')

            imgdata = gray2rgb(src)
            img.data.set_data('imagedata', imgdata)

        evt = self._lum_evt
        set_laser_power = self.laser_manager.set_laser_power_hook

        get_brightness = self.laser_manager.get_brightness
        target = self.lumens
        prev = 0

        while not evt.is_set():
            dt = pid.kdt
            st = time.time()
            src, current = get_brightness(threshold=self.threshold)

            err = target - current
            out = pid.get_value(err) or 0

            invoke_in_main_thread(update, current, err, out, src)

            if abs(prev - out) > 0.02:
                self.debug('set power output={}'.format(out))
                set_laser_power(out)
                prev = out

            et = time.time() - st
            t = dt - et
            if t > 0:
                evt.wait(dt)

    def traits_view(self):
        v = View(VGroup(Item('pid', style='custom'),
                        Item('threshold', label='T'),
                        Item('test'),
                        UItem('plot_container', style='custom', editor=ComponentEditor())))
        return v
Ejemplo n.º 16
0
    def setup_graph(self, an):

        container = HPlotContainer()

        container_dict = {'spacing': 5, 'stack_order': 'top_to_bottom'}
        sg = StackedGraph(container_dict=container_dict)
        sg.plotcontainer.spacing = 5
        sg.plotcontainer.stack_order = 'top_to_bottom'

        isos = an.sorted_values(reverse=False)
        add_sniff = True

        sisos = [iso for iso in isos if iso.sniff.offset_xs.shape[0]]
        for i, iso in enumerate(sisos):
            sniff = iso.sniff
            sg.new_plot(ytitle=iso.name,
                        xtitle='Time (s)',
                        title='Equilibration')
            sg.new_series(sniff.offset_xs,
                          sniff.ys,
                          marker='circle',
                          type='scatter')
            sg.set_y_limits(pad='0.1', plotid=i)
            sg.set_x_limits(min_=0, max_=max(sniff.offset_xs) * 1.05, plotid=i)

        bg = StackedRegressionGraph(container_dict=container_dict)
        add_baseline = True

        ig = StackedRegressionGraph(container_dict=container_dict)

        iisos = [iso for iso in isos if iso.offset_xs.shape[0]]
        baselines = []
        for i, iso in enumerate(iisos):
            if iso.baseline.offset_xs.shape[0]:
                baselines.append(iso.baseline)
            ig.new_plot(ytitle='{}({})'.format(iso.name, iso.detector),
                        xtitle='Time (s)',
                        title='Isotope')
            ig.new_series(iso.offset_xs,
                          iso.ys,
                          display_filter_bounds=True,
                          filter_outliers_dict=iso.filter_outliers_dict,
                          color='blue',
                          type='scatter',
                          fit=iso.efit)
            ig.set_regressor(iso.regressor, i)
            ig.set_y_limits(pad='0.1', plotid=i)
            ig.set_x_limits(min_=0, max_=max(iso.offset_xs) * 1.05, plotid=i)

        ig.refresh()

        # bisos = [iso for iso in isos if iso.baseline.offset_xs.shape[0]]
        # plotted_baselines = []
        # for i, iso in enumerate(bisos):
        # baseline = iso.baseline
        # if baseline.detector in plotted_baselines:
        #     continue
        # plotted_baselines.append(baseline.detector)

        # for iso in bisos:
        for i, baseline in enumerate(baselines):

            bg.new_plot(ytitle=baseline.detector,
                        xtitle='Time (s)',
                        title='Baseline')
            bg.new_series(baseline.offset_xs,
                          baseline.ys,
                          filter_outliers_dict=baseline.filter_outliers_dict,
                          display_filter_bounds=True,
                          color='red',
                          type='scatter',
                          fit=baseline.efit)
            bg.set_regressor(baseline.regressor, i)
            bg.set_y_limits(pad='0.1', plotid=i)
            bg.set_x_limits(pad='0.025', plotid=i)

        bg.refresh()

        container.add(sg.plotcontainer)
        container.add(ig.plotcontainer)
        container.add(bg.plotcontainer)

        self.container = container