Exemple #1
0
    def plot2dataframe(self, xaxes=Utils.X, yaxes=Utils.Y):
        set_white_chart()
        path = Utils.BUILD_CHART2D_PATH(self.dataset_name, self.info[Utils.NAME], self.info[Utils.SURNAME],
                                        self.info[Utils.WORD_NUMBER], self.info[Utils.HANDWRITING], self.label)
        chrono = chronometer.Chrono("Plotting 2D Chart for: {}...".format(self.title))
        if os.path.isfile(path):
            chrono.end("already exixst")
            return

        ax = None
        colors = itertools.cycle(plt.rcParams['axes.prop_cycle'])
        for i, component in enumerate(g for _, g in self.word_dataframe.groupby(Utils.COMPONENT)):
            ax = component[["x", "y", Utils.TIME]].plot(x=xaxes, y=yaxes, kind="scatter", c=next(colors)['color'],
                                                        ax=ax if ax else None)

        ax.set_xlim(0, self.width)
        ax.set_ylim(0, self.height)

        ax.set_xticklabels([])
        ax.set_yticklabels([])

        ax.xaxis.label.set_visible(False)
        ax.yaxis.label.set_visible(False)

        # plt.title(self.title)
        plt.axes().set_aspect('equal')
        plt.axes().invert_yaxis()

        Utils.mkdir(Utils.BUILD_CHART2D_FOLDER_PATH(self.dataset_name))
        plt.savefig(path, dpi=400)
        chrono.end()
Exemple #2
0
    def _generate_animation(self):
        chrono = chronometer.Chrono("Generating gif for: {}...".format(self.title))
        if os.path.isfile(self.gif_path):
            chrono.end("already exixst")
            return

        time_millis_per_frame = self.max_time / (self.frames - 1)

        fig = plt.figure()
        ax = fig.add_subplot(111)

        ax.set_xlim(0, self.width)
        ax.set_ylim(0, self.height)

        ax.set_xticklabels([])
        ax.set_yticklabels([])

        ax.xaxis.label.set_visible(False)
        ax.yaxis.label.set_visible(False)

        # plt.title(self.title)
        plt.axes().set_aspect('equal')
        plt.axes().invert_yaxis()

        ani = animation.FuncAnimation(fig, self._update_plot,
                                      fargs=(self, time_millis_per_frame,),
                                      frames=self.frames,
                                      interval=time_millis_per_frame,
                                      repeat=True,
                                      repeat_delay=self.repeat_delay,
                                      blit=False)

        ani.save(self.gif_path, writer='imagemagick')
        plt.close(fig)
        chrono.end()
Exemple #3
0
    def plot3dataframe(self, scaling_rates=None):
        set_white_chart()
        chrono = chronometer.Chrono("Plotting 3D Charts for: {}...".format(self.title))
        maxv = max(self.word_dataframe[Utils.TIME])
        if not scaling_rates:
            scaling_rates = range(0, maxv + 1, 50)
        wrote_something = False
        for scaling in scaling_rates:
            path = Utils.BUILD_CHART3D_PATH(self.dataset_name, self.info[Utils.NAME], self.info[Utils.SURNAME],
                                            self.info[Utils.WORD_NUMBER], self.info[Utils.HANDWRITING], scaling,
                                            self.label)
            if os.path.isfile(path):
                continue

            wrote_something = True

            fig = plt.figure()
            ax = fig.add_subplot(111, projection='3d')

            colors = itertools.cycle(plt.rcParams['axes.prop_cycle'])
            for i, component in enumerate(g for _, g in self.word_dataframe.groupby(Utils.COMPONENT)):
                x = component[Utils.X]
                y = component[Utils.Y]
                z = component[Utils.TIME] / maxv * scaling

                ax.scatter(y, x, z, c=next(colors)['color'])

            ax.w_xaxis.set_pane_color((1, 1, 1, 0))
            ax.w_yaxis.set_pane_color((1, 1, 1, 0))
            ax.w_zaxis.set_pane_color((1, 1, 1, 0))

            ax.set_xticklabels([])
            ax.set_yticklabels([])
            ax.set_zticklabels([])


            # ax.xaxis.set_ticks_position('none')  # tick markers
            # ax.yaxis.set_ticks_position('none')


            # plt.title(self.title)
            ax.set_xlim(0, self.height)
            ax.set_ylim(0, self.width)
            ax.set_zlim(0, maxv)
            ax.set_zlabel('\ntime', linespacing=-4)

            # ChartCreator.set_axes_equal(ax)

            Utils.mkdir(Utils.BUILD_CHART3D_FOLDER_PATH(self.dataset_name, self.info[Utils.NAME], self.info[Utils.SURNAME],
                                            self.info[Utils.WORD_NUMBER], self.info[Utils.HANDWRITING], self.label ))
            plt.savefig(path, dpi=400, bbox_inches='tight')
            plt.close(fig)

        if wrote_something:
            chrono.end()

        else:
            chrono.end("already exixst")
Exemple #4
0
 def _extract_features_from_dataframes(self):
     for label in Utils.TIMED_POINTS_SERIES_TYPE:
         chrono = Chronom.Chrono(
             "Extracting features from {}...".format(label), True)
         local_features = {
             label:
             self.extract_features_from_dataframe(
                 self.data_frames[label],
                 self.data_frames[Utils.WORDID_USERID])
         }
         chrono.end()
         self._save_feature(local_features)
         del local_features
Exemple #5
0
    def fit(self):
        self._initialize_svm()
        chrono = Chronom.Chrono("Fitting svms...")
        for label in LEARNING_FROM:
            self.svms[label].fit(self.X_train[label], self.y_train)

        # todo: check consistenza ordinamento, forse si può togliere
        for l1 in LEARNING_FROM:
            for l2 in LEARNING_FROM:
                assert (self.svms[l1].classes_ == self.svms[l2].classes_).all()
        chrono.end()
        if self.check_inconsistency:
            self.check_inconsistencies()
Exemple #6
0
def save_dataframes(dataset_name, dataframes_dict, dataframe_type, message,
                    to_csv, frames_to_add_column, csv_column):
    mkdir(BUILD_GENERATED_FOLDER(dataset_name))
    chrono = Chronometer.Chrono(message)
    for label, v in dataframes_dict.items():
        v.to_pickle(PATHS_FUN[dataframe_type][PICKLE_EXTENSION](dataset_name,
                                                                label))
        if to_csv:
            if frames_to_add_column and csv_column is not None and label in frames_to_add_column:
                v = add_column(v, csv_column)
            dataframe_to_csv(
                v, dataset_name,
                PATHS_FUN[dataframe_type][CSV_EXTENSION](dataset_name, label))
    chrono.end()
Exemple #7
0
 def check_inconsistencies(self):
     chrono = Chronom.Chrono("Checking consistency...")
     counter = 0
     inc = []
     # print(self.get_classes_())
     for svm in SVM_LIST:
         predicted = self.predict(svm, self.get_testdata()[0])
         predicted_proba = self.predict_proba(svm, self.get_testdata()[0])
         for i, (a, b) in enumerate(zip(predicted, predicted_proba)):
             if a != self.prob_to_class(b):
                 counter += 1
                 inc.append(({"predicted": a}, {self.index_to_class(i): a for i, a in enumerate(b)},
                             {"class with max proba": self.prob_to_class(b)},
                             {"correct one": list(self.get_testdata()[1])[i]}))
                 # print(a,b,self.prob_to_class((b)))
     chrono.end("found {} inconsistencies".format(counter))
     return inc
Exemple #8
0
 def _read_pickles(self):
     chrono = Chronom.Chrono("Reading features...")
     for label in Utils.TIMED_POINTS_SERIES_TYPE:
         self.data_features[label] = pandas.read_pickle(
             Utils.BUILD_FEATURE_PICKLE_PATH(self.dataset_name, label))
     chrono.end()
Exemple #9
0
SVM_LIST_COMP2 = [
    lr.MOVEMENT, lr.ALL_MAJORITY, lr.ALL_AVERAGE, lr.ALL_WEIGHTED_AVERAGE
]

TO_DO_TOGHETER = [(SVM_LIST_NOSHIFT, "setnoshift"),
                  (SVM_LIST_SHIFT, "setshift"), (SVM_LIST_COMP1, "origshift"),
                  (SVM_LIST_COMP2, "setall")]

# todo: ottimizza evitando la ripetizione di calcoli
if __name__ == '__main__':
    p = plotter.Plotter(Utils.DATASET_NAME_0)

    for handwriting in [Utils.ITALIC, Utils.BLOCK_LETTER]:
        classifier = lr.WordClassifier(Utils.DATASET_NAME_0, handwriting)

        chrono = cr.Chrono("Generating verification outputs...")
        ver = VerificationEvaluator(classifier)
        for balanced in [True, False]:
            names, fprs, tprs, ts, aucs = ver.plots_info_weights(
                lr.WEIGHTED_AVERAGE, balanced, np.arange(0, 1.01, 0.2))
            p.plotRocs(names, fprs, tprs, aucs, handwriting, balanced,
                       "weights")

            for svm_list, name in TO_DO_TOGHETER:
                names, fprs, tprs, ts, aucs = ver.plots_info_names(
                    svm_list, balanced)
                p.plotRocs(names, fprs, tprs, aucs, handwriting, balanced,
                           name)

            for svm in SVM_LIST:
                name, fpr, tpr, t, auc = ver.plot_info(svm, balanced)