def plot_region_idft(self, region):
        if self.spectrum_selection != 0:
            y, data = self.reconstruct_tof_from_spectrum(
                region, True, self.spectrum_selection
            )
            y2, _ = self.reconstruct_tof_from_spectrum(
                region, False, self.spectrum_selection
            )
        else:
            y, data = self.reconstruct_tof_from_spectrum(region, True)
            y2, _ = self.reconstruct_tof_from_spectrum(region, False)

        x = np.linspace(0, len(data), len(data))
        xticks = time_ticks(self.minute_interval, self.window_interval, self.periodic_type)
        plt.plot(
            x, y, "-o", color="red", label="Amplitude Addition Reconstruction"
        )
        plt.plot(
            x, y2, "-x", color="blue", label="Best Amplitude Reconstruction"
        )
        plt.plot(x, data, "-", color="green", label="Original Model")

        plt.title("Reconstruction Occurrence Rate for Region %s" % region)
        plt.xticks(x, xticks, rotation="vertical")
        plt.xlabel("One Week Period with %d minutes interval and %d window time" % (self.minute_interval, self.window_interval))
        plt.ylabel("Occurrence rate value")
        plt.ylim(ymin=-5)

        plt.legend()
        plt.show()
Esempio n. 2
0
    def prediction_accuracy(self,
                            region,
                            data,
                            month,
                            year,
                            percentile=0.1,
                            plot=False):
        if self.spectrum_selection != 0:
            reconstruction_tof, original_tof = self.reconstruct_tof_from_spectrum(
                region, self.addition_technique, self.spectrum_selection)
        else:
            reconstruction_tof, original_tof = self.reconstruct_tof_from_spectrum(
                region, self.addition_technique
                # region, False, 26
            )
        test_data = trajectories_full_dates_periodic(
            data, month, year, self.length_of_periodicity,
            self.window_interval, self.minute_interval)
        original_predict = self._get_prediction(original_tof, test_data,
                                                percentile)
        reconstr_predict = self._get_prediction(reconstruction_tof, test_data,
                                                percentile)
        _, clean_ori_pred, clean_recon_pred, indices = self._remove_unobserved_data(
            test_data, reconstr_predict, original_predict)
        if len(clean_ori_pred) and len(clean_recon_pred):
            mse_predict = mse(clean_ori_pred, clean_recon_pred)
        else:
            mse_predict = -1
        rospy.loginfo(
            "Calculated MSE for prediction between original and reconstruction for Region %s: %.2f"
            % (region, mse_predict))
        if plot:
            for index in indices:
                original_predict[index] = -1
                reconstr_predict[index] = -1
            x = np.linspace(0, len(test_data), len(test_data))
            xticks = time_ticks(self.minute_interval, self.window_interval,
                                self.periodic_type)
            plt.plot(x,
                     original_predict,
                     "-o",
                     label="Prediction Original TOF")
            plt.plot(x,
                     reconstr_predict,
                     "-^",
                     label="Prediction Reconstruction TOF")
            plt.title("Prediction for Region %s" % region)
            plt.xticks(x, xticks, rotation="vertical")
            plt.xlabel(
                "One Week Period with %d minutes interval and %d window time" %
                (self.minute_interval, self.window_interval))
            plt.ylabel("Prediction (1=Anomalous, 0=Normal, -1=Unobserved)")
            plt.ylim(ymin=-2, ymax=2)

            plt.legend()
            plt.show()
Esempio n. 3
0
    def plot_region_idft(self, region):
        if self.spectrum_selection != 0:
            y, data = self.reconstruct_tof_from_spectrum(
                region, True, self.spectrum_selection)
            y2, _ = self.reconstruct_tof_from_spectrum(region, False,
                                                       self.spectrum_selection)
        else:
            y, data = self.reconstruct_tof_from_spectrum(region, True)
            y2, _ = self.reconstruct_tof_from_spectrum(region, False)

        x = np.linspace(0, len(data), len(data))
        xticks = time_ticks(self.minute_interval, self.window_interval,
                            self.periodic_type)
        # y = map(lambda x: x / 100.0, y)
        # y2 = map(lambda x: x / 100.0, y2)
        # data = map(lambda x: x / 100.0, data)
        plt.plot(x,
                 y,
                 "-o",
                 color="red",
                 label="Amplitude Addition Model",
                 linewidth=5)
        plt.plot(x,
                 y2,
                 "-x",
                 color="blue",
                 label="Best Amplitude Model",
                 linewidth=5)
        plt.plot(x,
                 data,
                 "-*",
                 color="green",
                 label="Poisson Model",
                 linewidth=5)

        plt.title("Reconstruction Occurrence Rate for Region %s" % region)
        # plt.xticks(x, xticks, rotation="horizontal", fontsize=30)
        plt.xticks(x, xticks, rotation="vertical")
        plt.xlabel(
            "One Week Period with %d minutes interval and %d window time" %
            (self.minute_interval, self.window_interval))
        # plt.xticks([])
        # plt.xticks(fontsize=30)
        # plt.yticks([])
        # plt.yticks(fontsize=30)
        # plt.xlabel("Time", fontsize=40)
        plt.ylabel("Occurrence rate")
        # plt.ylabel("Amplitude", fontsize=40)
        plt.ylim(ymin=-5)

        # plt.legend(prop={'size': 40}, loc=4)
        plt.legend()
        plt.show()
    def prediction_accuracy(self, region, data, month, year, percentile=0.1, plot=False):
        if self.spectrum_selection != 0:
            reconstruction_tof, original_tof = self.reconstruct_tof_from_spectrum(
                region, self.addition_technique, self.spectrum_selection
            )
        else:
            reconstruction_tof, original_tof = self.reconstruct_tof_from_spectrum(
                region, self.addition_technique
                # region, False, 26
            )
        test_data = trajectories_full_dates_periodic(
            data, month, year, self.length_of_periodicity,
            self.window_interval, self.minute_interval
        )
        original_predict = self._get_prediction(original_tof, test_data, percentile)
        reconstr_predict = self._get_prediction(reconstruction_tof, test_data, percentile)
        _, clean_ori_pred, clean_recon_pred, indices = self._remove_unobserved_data(
            test_data, reconstr_predict, original_predict
        )
        if len(clean_ori_pred) and len(clean_recon_pred):
            mse_predict = mse(clean_ori_pred, clean_recon_pred)
        else:
            mse_predict = -1
        rospy.loginfo(
            "Calculated MSE for prediction between original and reconstruction for Region %s: %.2f" % (region, mse_predict)
        )
        if plot:
            for index in indices:
                original_predict[index] = -1
                reconstr_predict[index] = -1
            x = np.linspace(0, len(test_data), len(test_data))
            xticks = time_ticks(self.minute_interval, self.window_interval, self.periodic_type)
            plt.plot(
                x, original_predict, "-o", label="Prediction Original TOF"
            )
            plt.plot(
                x, reconstr_predict, "-^", label="Prediction Reconstruction TOF"
            )
            plt.title("Prediction for Region %s" % region)
            plt.xticks(x, xticks, rotation="vertical")
            plt.xlabel("One Week Period with %d minutes interval and %d window time" % (self.minute_interval, self.window_interval))
            plt.ylabel("Prediction (1=Anomalous, 0=Normal, -1=Unobserved)")
            plt.ylim(ymin=-2, ymax=2)

            plt.legend()
            plt.show()
    def __init__(self, soma_map, soma_config, minute_interval=1, window_interval=10):
        """
            Initialize plotting, it needs the name of the soma map, specific soma configuration,
            the minute interval between two different occurrence rate, and the window interval that
            acts as bins.
        """
        self.tof = TrajectoryOccurrenceFrequencies(soma_map, soma_config, minute_interval, window_interval)
        self.xticks = time_ticks(minute_interval, window_interval, self.tof.periodic_type)
        self.x = np.arange(len(self.xticks))
        self.minute_interval = minute_interval
        self.window_interval = window_interval
        self.periodic_length = len(self.tof.periodic_days)
        self.tof.load_tof()
        self.tof = self.tof.tof
        self.regions = self.tof.keys()
        self.colors = [
            (0., 0., 0.), (0., 0., 1.), (0., 1., 0.), (0., 1., 1.),
            (1., 0., 0.), (1., 0., 1.), (1., 1., 0.), (.75, .75, .75),
            (0., 0., 0.), (0., 0., .5), (0., .5, 0.), (0., .5, .5),
            (.5, 0., 0.), (.5, 0., .5), (.5, .5, 0.), (.25, .25, .25)

        ]