def add_polygon(self, vertices, polygon_type):

        self.all_polygons_vertex_circles.append(
            self.curr_polygon_vertex_circles)

        if polygon_type == PolygonType.CLOSED:
            polygon = Polygon(vertices,
                              closed=True,
                              fill=False,
                              edgecolor=self.colors[self.curr_label + 1],
                              linewidth=2)
        elif polygon_type == PolygonType.OPEN:
            polygon = Polygon(vertices,
                              closed=False,
                              fill=False,
                              edgecolor=self.colors[self.curr_label + 1],
                              linewidth=2)
        elif polygon_type == PolygonType.TEXTURE:
            polygon = Polygon(vertices,
                              closed=True,
                              fill=False,
                              edgecolor=self.colors[self.curr_label + 1],
                              linewidth=2,
                              hatch='/')
        elif polygon_type == PolygonType.TEXTURE_WITH_CONTOUR:
            polygon = Polygon(vertices,
                              closed=True,
                              fill=False,
                              edgecolor=self.colors[self.curr_label + 1],
                              linewidth=2,
                              hatch='x')
        elif polygon_type == PolygonType.DIRECTION:
            polygon = Polygon(vertices,
                              closed=False,
                              fill=False,
                              edgecolor=self.colors[self.curr_label + 1],
                              linewidth=2,
                              linestyle='dashed')
        else:
            raise 'polygon_type must be one of enum closed, open, texture'

        xys = polygon.get_xy()
        x0_y0_x1_y1 = np.r_[xys.min(axis=0), xys.max(axis=0)]

        self.axis.add_patch(polygon)

        polygon.set_picker(True)

        self.polygon_list.append(polygon)
        self.polygon_bbox_list.append(x0_y0_x1_y1)
        self.polygon_labels.append(self.curr_label)
        self.polygon_types.append(polygon_type)

        self.curr_polygon_vertices = []
        self.curr_polygon_vertex_circles = []
    def initialize_brain_labeling_gui(self):

        self.menu = QMenu()
        self.endDraw_Action = self.menu.addAction("Confirm closed contour")
        self.endDrawOpen_Action = self.menu.addAction("Confirm open boundary")
        self.confirmTexture_Action = self.menu.addAction(
            "Confirm textured region without contour")
        self.confirmTextureWithContour_Action = self.menu.addAction(
            "Confirm textured region with contour")
        self.confirmDirectionality_Action = self.menu.addAction(
            "Confirm striated region")

        self.deletePolygon_Action = self.menu.addAction("Delete polygon")
        self.deleteVertex_Action = self.menu.addAction("Delete vertex")
        self.addVertex_Action = self.menu.addAction("Add vertex")

        self.crossReference_Action = self.menu.addAction("Cross reference")

        # A set of high-contrast colors proposed by Green-Armytage
        self.colors = np.loadtxt('100colors.txt', skiprows=1)
        self.label_cmap = ListedColormap(self.colors, name='label_cmap')

        self.curr_label = -1

        self.setupUi(self)

        self.fig = self.canvaswidget.fig
        self.canvas = self.canvaswidget.canvas

        self.canvas.mpl_connect('scroll_event', self.zoom_fun)
        self.bpe_id = self.canvas.mpl_connect('button_press_event',
                                              self.press_fun)
        self.bre_id = self.canvas.mpl_connect('button_release_event',
                                              self.release_fun)
        self.canvas.mpl_connect('motion_notify_event', self.motion_fun)

        self.display_buttons = [
            self.img_radioButton, self.textonmap_radioButton,
            self.dirmap_radioButton, self.labeling_radioButton
        ]
        self.img_radioButton.setChecked(True)

        for b in self.display_buttons:
            b.toggled.connect(self.display_option_changed)

        self.spOnOffSlider.valueChanged.connect(self.display_option_changed)

        self.canvas.mpl_connect('pick_event', self.on_pick)

        ########## Label buttons #############

        self.n_labelbuttons = 0

        self.labelbuttons = []
        self.labeledits = []

        if self.parent_labeling is not None:
            for n in self.parent_labeling['labelnames']:
                self._add_labelbutton(desc=n)
        else:
            for n in self.dm.labelnames:
                self._add_labelbutton(desc=n)

        # self.loadButton.clicked.connect(self.load_callback)
        self.saveButton.clicked.connect(self.save_callback)
        self.newLabelButton.clicked.connect(self.newlabel_callback)
        # self.newLabelButton.clicked.connect(self.sigboost_callback)
        self.quitButton.clicked.connect(self.close)
        self.buttonParams.clicked.connect(self.paramSettings_clicked)

        self.setWindowTitle(self.windowTitle() + ', parent_labeling = %s' %
                            (self.parent_labeling_name))

        # self.statusBar().showMessage()

        self.fig.clear()
        self.fig.set_facecolor('white')

        self.axis = self.fig.add_subplot(111)
        self.axis.axis('off')

        self.axis.imshow(self.masked_img, cmap=plt.cm.Greys_r, aspect='equal')

        if self.curr_labeling['initial_polygons'] is not None:
            for label, typed_polygons in self.curr_labeling[
                    'initial_polygons'].iteritems():
                for polygon_type, vertices in typed_polygons:
                    if polygon_type == PolygonType.CLOSED:
                        polygon = Polygon(vertices,
                                          closed=True,
                                          fill=False,
                                          edgecolor=self.colors[label + 1],
                                          linewidth=2)
                    elif polygon_type == PolygonType.OPEN:
                        polygon = Polygon(vertices,
                                          closed=False,
                                          fill=False,
                                          edgecolor=self.colors[label + 1],
                                          linewidth=2)
                    elif polygon_type == PolygonType.TEXTURE:
                        polygon = Polygon(vertices,
                                          closed=True,
                                          fill=False,
                                          edgecolor=self.colors[label + 1],
                                          linewidth=2,
                                          hatch='/')
                    elif polygon_type == PolygonType.TEXTURE_WITH_CONTOUR:
                        polygon = Polygon(vertices,
                                          closed=True,
                                          fill=False,
                                          edgecolor=self.colors[label + 1],
                                          linewidth=2,
                                          hatch='x')
                    elif polygon_type == PolygonType.DIRECTION:
                        polygon = Polygon(vertices,
                                          closed=False,
                                          fill=False,
                                          edgecolor=self.colors[label + 1],
                                          linewidth=2,
                                          linestyle='dashed')
                    else:
                        raise 'polygon_type must be one of enum closed, open, texture'

                    xys = polygon.get_xy()
                    x0_y0_x1_y1 = np.r_[xys.min(axis=0), xys.max(axis=0)]

                    polygon.set_picker(10.)

                    self.axis.add_patch(polygon)
                    self.polygon_list.append(polygon)
                    self.polygon_bbox_list.append(x0_y0_x1_y1)
                    self.polygon_labels.append(label)
                    self.polygon_types.append(polygon_type)

                    curr_polygon_vertex_circles = []
                    for x, y in vertices:
                        curr_vertex_circle = plt.Circle(
                            (x, y),
                            radius=10,
                            color=self.colors[label + 1],
                            alpha=.8)
                        curr_vertex_circle.set_picker(True)
                        self.axis.add_patch(curr_vertex_circle)
                        curr_polygon_vertex_circles.append(curr_vertex_circle)

                    self.all_polygons_vertex_circles.append(
                        curr_polygon_vertex_circles)

        # self.curr_polygon_vertices = []

        self.fig.subplots_adjust(left=0, bottom=0, right=1, top=1)

        self.newxmin, self.newxmax = self.axis.get_xlim()
        self.newymin, self.newymax = self.axis.get_ylim()

        self.canvas.draw()
        self.show()