def draw_outliers(ps, outliers, ok, pic_size=(640, 480), is_axis=True, grid=None, filename=None): """ Draw outliers. Arguments: ps -- points, outliers -- list of outliers, ok -- outliers count (leaders), pic_size -- picture size, is_axis -- need to draw axis, grid -- grid lines characteristics, filename -- file name for picture. """ # Points characteristics. min_coords = reduce(lambda p1, p2: (min(p1[0], p2[0]), min(p1[1], p2[1])), ps[1:], ps[0]) max_coords = reduce(lambda p1, p2: (max(p1[0], p2[0]), max(p1[1], p2[1])), ps[1:], ps[0]) # Drawer ini. D = Drawer(draw_area=min_coords + max_coords, pic_size=pic_size) # Axis. if is_axis: D.Axis() # Grid. if grid != None: D.Grid(grid) # Draw points. red_pen = aggdraw.Pen('red', 1.0) red_brush = aggdraw.Brush('red') for p in ps: D.Point(p, 3, red_pen, red_brush) # Draw outliers. black_pen = aggdraw.Pen('black', 2.0) steelblue_brush = aggdraw.Brush('steelblue') for outlier in outliers[ok:]: (r, p) = outlier D.Point(p, 3 * r, black_pen) for outlier in outliers[:ok]: (r, p) = outlier D.Point(p, 3 * r, black_pen, steelblue_brush) D.Point(p, 3, red_pen, red_brush) # Flush save and show. D.FSS(filename=filename)
def draw_data(tree, draw_trajectory=False, draw_clusters=True, pic_size=(640, 480), is_axis=True, grid=None, filename=None): """ Draw data. Arguments: tree -- clustering tree, pic_size -- picture size, is_axis -- need to draw axi, grid -- grid lines characteristics, filename -- file name for picture. """ # Points characteristics. min_coords = reduce(lambda p1, p2: (min(p1[0], p2[0]), min(p1[1], p2[1])), ps[1:], ps[0]) max_coords = reduce(lambda p1, p2: (max(p1[0], p2[0]), max(p1[1], p2[1])), ps[1:], ps[0]) # Drawer ini. D = Drawer(draw_area=min_coords + max_coords, pic_size=pic_size) # Axis. if is_axis: D.Axis() # Grid. if grid != None: D.Grid(grid) # Draw clusters lines. if draw_clusters: leaf1 = tree.LeftLeaf() while leaf1 != None: leaf2 = tree.NextLeafLeftRoRight(leaf1) while leaf2 != None: # Draw. d1 = leaf1.Data d2 = leaf2.Data kn1 = ClusterNumber(leaf1) kn2 = ClusterNumber(leaf2) is_not_outliers = (not leaf1.IsOutlier) and ( not leaf2.IsOutlier) if is_not_outliers and (kn1 != -1) and (kn1 == kn2): D.Line(d1, d2, pen=aggdraw.Pen(pretty_color(kn1), 1.0)) leaf2 = tree.NextLeafLeftRoRight(leaf2) leaf1 = tree.NextLeafLeftRoRight(leaf1) backcolor = D.Backcolor backcolor_pen = aggdraw.Pen(backcolor, 1.0) backcolor_brush = aggdraw.Brush(backcolor) # Draw points. leaf = tree.LeftLeaf() while leaf != None: # Default colors and etc. color = 'red' point_radius = 3 # Define color if cluster number is set. if draw_clusters: kn = ClusterNumber(leaf) if leaf.IsOutlier: color = 'black' point_radius = 5 elif kn != -1: color = pretty_color(kn) # Define pen, brush and draw point. point_pen = aggdraw.Pen(color, 1.0) point_brush = aggdraw.Brush(color) D.Point(leaf.Data, point_radius, pen=point_pen, brush=point_brush) if draw_clusters: if not leaf.IsOutlier: D.Point(leaf.Data, 1, pen=backcolor_pen, brush=backcolor_brush) leaf = tree.NextLeafLeftRoRight(leaf) # Flush save and show. D.FSS(filename=filename)