def update(self,i=0):
                # Regenerate GMs
                merged_gauss_2d = GaussianMixture(weights, means, covariances,
                                                  max_num_mixands=self.num_mixands)
                matlab_merged_gauss_2d = self.parent.matlab_gm(weights,
                                                   means,
                                                   covariances,
                                                   max_num_mixands=self.num_mixands)

                # Replot GMs
                title = 'Python Merged GM ({} mixands)'.format(merged_gauss_2d.weights.size)
                if hasattr(self,'old_contour'):
                    merged_gauss_2d.contourf = self.old_contour
                    merged_gauss_2d.plot_remove()
                self.old_contour = merged_gauss_2d.plot(ax=self.axes[1], levels=self.levels, title=title)

                title = 'Matlab Merged GM ({} mixands)'.format(matlab_merged_gauss_2d.weights.size)
                if hasattr(self,'old_matlab_contour'):
                    matlab_merged_gauss_2d.contourf = self.old_matlab_contour
                    matlab_merged_gauss_2d.plot_remove()
                self.old_matlab_contour = matlab_merged_gauss_2d.plot(ax=self.axes[2], levels=self.levels, title=title)

                # Decrement mixands (with wrapping)
                if self.num_mixands == self.max_num_mixands:
                    self.num_mixands = 1
                elif np.int(self.num_mixands * self.mixand_rate) < self.max_num_mixands:
                    self.num_mixands = np.int(self.num_mixands * self.mixand_rate)
                else:
                    self.num_mixands = self.max_num_mixands
Exemple #2
0
    def _detect_probability(self, prior):

        mu, sigma, beta = self.vb.update(measurement='No Detection',
                                         likelihood=self.detection_model,
                                         prior=prior,
                                         use_LWIS=True,
                                         poly=self.detection_model.poly
                                         )
        gm = GaussianMixture(beta, mu, sigma)
        gm.camera_viewcone = self.detection_model.poly  # for plotting
        return gm
Exemple #3
0
def test_voi():
    from cops_and_robots.robo_tools.robber import Robber
    from cops_and_robots.map_tools.map import Map
    from cops_and_robots.fusion.human import Human
    from cops_and_robots.fusion.gaussian_mixture import GaussianMixture
    import matplotlib.pyplot as plt
    from matplotlib.colors import cnames

    m = Map()
    pris = Robber("Pris")
    pris.map_obj.color = cnames["cornflowerblue"]
    m.add_robber(pris.map_obj)
    zhora = Robber("Zhora")
    zhora.map_obj.color = cnames["cornflowerblue"]
    m.add_robber(zhora.map_obj)
    roy = Robber("Roy")
    m.add_robber(roy.map_obj)
    h = Human(map_=m)
    m.add_human_sensor(h)

    prior = GaussianMixture(
        [0.1, 0.7, 0.2],
        [[3, -2], [-6, -2], [0, 0]],
        [[[1.5, 1.0], [1.0, 1.5]], [[2.5, -0.3], [-0.3, 2.5]], [[0.5, -0.3], [-0.3, 0.5]]],
    )
    prior._discretize(bounds=m.bounds, grid_spacing=0.1)
    q = Questioner(human_sensor=h, target_order=["Pris", "Roy"], target_weights=[11.0, 10.0])

    m.setup_plot(show_human_interface=False)
    m.update()
    ax = m.axes["combined"]

    # prior.plot(bounds=m.bounds, alpha=0.5, ax=ax)
    # m.update()
    # plt.show()
    q.weigh_questions({"Roy": prior})
    for qu in q.weighted_questions:
        print qu
    def test_merging(self, num_mixands=100, max_num_mixands=10, spread=4,
                     speak=False):
        if max_num_mixands is None:
            animate = True
            max_num_mixands = num_mixands
        else:
            animate = False

        # Generate the unmerged and merged gaussians
        weights, means, covariances = generate_random_params(num_mixands,
                                                             ndims=2,
                                                             spread=spread)

        unmerged_gauss_2d = GaussianMixture(weights.copy(),
                                            means.copy(),
                                            covariances.copy(),
                                            max_num_mixands=len(weights))
        merged_gauss_2d = GaussianMixture(weights.copy(),
                                          means.copy(),
                                          covariances.copy(),
                                          max_num_mixands=max_num_mixands)
        matlab_merged_gauss_2d = self.matlab_gm(weights.copy(),
                                                means.copy(),
                                                covariances.copy(),
                                                max_num_mixands)
        mixtures = {'unmerged': unmerged_gauss_2d,
                    'merged': merged_gauss_2d,
                    'matlab merged': matlab_merged_gauss_2d
                    }

        # Setup figure and levels
        fig = plt.figure(figsize=(18,6))
        axes = []
        _, max_1 = unmerged_gauss_2d.max_point_by_grid()
        _, max_2 = merged_gauss_2d.max_point_by_grid()
        _, max_3 = matlab_merged_gauss_2d.max_point_by_grid()
        max_prob = np.max((max_1, max_2, max_3))
        levels = np.linspace(0, max_prob * 1.2, 50)

        # Plot all three
        ax = fig.add_subplot(131)
        axes.append(ax)
        title = 'Original GM ({} mixands)'\
                .format(unmerged_gauss_2d.weights.size)
        unmerged_gauss_2d.plot(ax=ax, levels=levels, title=title)

        ax = fig.add_subplot(132)
        axes.append(ax)
        title = 'Python Merged GM ({} mixands)'\
                .format(merged_gauss_2d.weights.size)
        merged_gauss_2d.plot(ax=ax, levels=levels, title=title)

        ax = fig.add_subplot(133)
        axes.append(ax)
        title = 'Matlab Merged GM ({} mixands)'\
                .format(matlab_merged_gauss_2d.weights.size)
        matlab_merged_gauss_2d.plot(ax=ax, levels=levels, title=title)

        # Add a colorbar
        fig.subplots_adjust(right=0.85)
        cbar_ax = fig.add_axes([0.875, 0.1, 0.025, 0.8])
        fig.colorbar(unmerged_gauss_2d.contourf, cax=cbar_ax)

        class GMAnimation(object):
            """docstring for merged_gm"""
            def __init__(self, parent, mixand_rate=2, levels=None, axes=None):
                self.max_num_mixands = mixtures['unmerged'].weights.size
                self.num_mixands = 1
                self.mixand_rate = mixand_rate
                self.levels = levels
                self.axes = axes
                self.parent = parent

            def update(self,i=0):
                # Regenerate GMs
                merged_gauss_2d = GaussianMixture(weights, means, covariances,
                                                  max_num_mixands=self.num_mixands)
                matlab_merged_gauss_2d = self.parent.matlab_gm(weights,
                                                   means,
                                                   covariances,
                                                   max_num_mixands=self.num_mixands)

                # Replot GMs
                title = 'Python Merged GM ({} mixands)'.format(merged_gauss_2d.weights.size)
                if hasattr(self,'old_contour'):
                    merged_gauss_2d.contourf = self.old_contour
                    merged_gauss_2d.plot_remove()
                self.old_contour = merged_gauss_2d.plot(ax=self.axes[1], levels=self.levels, title=title)

                title = 'Matlab Merged GM ({} mixands)'.format(matlab_merged_gauss_2d.weights.size)
                if hasattr(self,'old_matlab_contour'):
                    matlab_merged_gauss_2d.contourf = self.old_matlab_contour
                    matlab_merged_gauss_2d.plot_remove()
                self.old_matlab_contour = matlab_merged_gauss_2d.plot(ax=self.axes[2], levels=self.levels, title=title)

                # Decrement mixands (with wrapping)
                if self.num_mixands == self.max_num_mixands:
                    self.num_mixands = 1
                elif np.int(self.num_mixands * self.mixand_rate) < self.max_num_mixands:
                    self.num_mixands = np.int(self.num_mixands * self.mixand_rate)
                else:
                    self.num_mixands = self.max_num_mixands

            def compare_results():
                pass

        if animate:
            gm_ani = GMAnimation(self, mixand_rate=2, levels=levels, axes=axes)
            ani = animation.FuncAnimation(fig, gm_ani.update, 
                interval=100,
                repeat=True,
                blit=False,
                )
        else:
            self.diff(merged_gauss_2d, matlab_merged_gauss_2d)
        plt.show()
        self.check_diff()