44from math import sqrt , log
55from operator import mul , attrgetter
66
7+ from scipy .stats import distributions
78from AnyQt .QtCore import Qt , QSize , pyqtSignal as Signal
89from AnyQt .QtGui import QColor , QPainter , QPen , QStandardItem
910from AnyQt .QtWidgets import QGraphicsScene , QGraphicsLineItem
2425from Orange .widgets .visualize .utils import (
2526 CanvasText , CanvasRectangle , ViewWithPress , VizRankDialog )
2627from Orange .widgets .widget import OWWidget , Default , Msg
27- from scipy .stats import distributions
2828
2929
3030class MosaicVizRank (VizRankDialog , OWComponent ):
@@ -229,22 +229,22 @@ def __init__(self):
229229 box = gui .vBox (self .controlArea , box = True )
230230 self .attr_combos = [
231231 gui .comboBox (
232- box , self , value = "variable{}" .format (i ),
233- orientation = Qt .Horizontal , contentsLength = 12 ,
234- callback = self .reset_graph ,
235- sendSelectedValue = True , valueType = str )
232+ box , self , value = "variable{}" .format (i ),
233+ orientation = Qt .Horizontal , contentsLength = 12 ,
234+ callback = self .reset_graph ,
235+ sendSelectedValue = True , valueType = str )
236236 for i in range (1 , 5 )]
237237 self .vizrank , self .vizrank_button = MosaicVizRank .add_vizrank (
238238 box , self , "Find Informative Mosaics" , self .set_attr )
239239
240240 self .rb_colors = gui .radioButtonsInBox (
241- self .controlArea , self , "interior_coloring" ,
242- self .interior_coloring_opts , box = "Interior Coloring" ,
243- callback = self .update_graph )
241+ self .controlArea , self , "interior_coloring" ,
242+ self .interior_coloring_opts , box = "Interior Coloring" ,
243+ callback = self .update_graph )
244244 self .bar_button = gui .checkBox (
245- gui .indentedBox (self .rb_colors ),
246- self , 'use_boxes' , label = 'Compare with total' ,
247- callback = self ._compare_with_total )
245+ gui .indentedBox (self .rb_colors ),
246+ self , 'use_boxes' , label = 'Compare with total' ,
247+ callback = self ._compare_with_total )
248248 gui .rubber (self .controlArea )
249249
250250 def sizeHint (self ):
@@ -272,7 +272,7 @@ def init_combos(self, data):
272272 if self .attr_combos [0 ].count () > 0 :
273273 self .variable1 = self .attr_combos [0 ].itemText (0 )
274274 self .variable2 = self .attr_combos [1 ].itemText (
275- 2 * (self .attr_combos [1 ].count () > 2 ))
275+ 2 * (self .attr_combos [1 ].count () > 2 ))
276276 self .variable3 = self .attr_combos [2 ].itemText (0 )
277277 self .variable4 = self .attr_combos [3 ].itemText (0 )
278278
@@ -456,7 +456,7 @@ def draw_data(attr_list, x0_x1, y0_y1, side, condition,
456456 valrange = list (range (len (values )))
457457 if len (attr_list + used_attrs ) == 4 and len (used_attrs ) == 2 :
458458 attr1values = get_variable_values_sorted (
459- data .domain [used_attrs [0 ]])
459+ data .domain [used_attrs [0 ]])
460460 if used_vals [0 ] == attr1values [- 1 ]:
461461 valrange = valrange [::- 1 ]
462462
@@ -547,8 +547,7 @@ def draw_text(side, attr, x0_x1, y0_y1,
547547 Qt .AlignBottom | Qt .AlignHCenter ,
548548 Qt .AlignLeft | Qt .AlignVCenter ]
549549 align = aligns [side ]
550- for i in range (len (values )):
551- val = values [i ]
550+ for i , val in enumerate (values ):
552551 perc = counts [i ] / float (total )
553552 if distributiondict [val ] != 0 :
554553 if side == 0 :
@@ -575,30 +574,28 @@ def draw_text(side, attr, x0_x1, y0_y1,
575574
576575 if side == 0 :
577576 CanvasText (
578- self .canvas , attr ,
579- x0 + (x1 - x0 ) / 2 ,
580- y1 + self .ATTR_VAL_OFFSET +
581- self .ATTR_NAME_OFFSET ,
582- align , bold = 1 )
577+ self .canvas , attr ,
578+ x0 + (x1 - x0 ) / 2 ,
579+ y1 + self .ATTR_VAL_OFFSET + self .ATTR_NAME_OFFSET ,
580+ align , bold = 1 )
583581 elif side == 1 :
584582 CanvasText (
585- self .canvas , attr ,
586- x0 - max_ylabel_w1 - self .ATTR_VAL_OFFSET ,
587- y0 + (y1 - y0 ) / 2 ,
588- align , bold = 1 , vertical = True )
583+ self .canvas , attr ,
584+ x0 - max_ylabel_w1 - self .ATTR_VAL_OFFSET ,
585+ y0 + (y1 - y0 ) / 2 ,
586+ align , bold = 1 , vertical = True )
589587 elif side == 2 :
590588 CanvasText (
591- self .canvas , attr ,
592- x0 + (x1 - x0 ) / 2 ,
593- y0 - self .ATTR_VAL_OFFSET -
594- self .ATTR_NAME_OFFSET ,
595- align , bold = 1 )
589+ self .canvas , attr ,
590+ x0 + (x1 - x0 ) / 2 ,
591+ y0 - self .ATTR_VAL_OFFSET - self .ATTR_NAME_OFFSET ,
592+ align , bold = 1 )
596593 else :
597594 CanvasText (
598- self .canvas , attr ,
599- x1 + max_ylabel_w2 + self .ATTR_VAL_OFFSET ,
600- y0 + (y1 - y0 ) / 2 ,
601- align , bold = 1 , vertical = True )
595+ self .canvas , attr ,
596+ x1 + max_ylabel_w2 + self .ATTR_VAL_OFFSET ,
597+ y0 + (y1 - y0 ) / 2 ,
598+ align , bold = 1 , vertical = True )
602599
603600 def add_rect (x0 , x1 , y0 , y1 , condition = "" ,
604601 used_attrs = [], used_vals = [], attr_vals = "" ):
@@ -623,13 +620,13 @@ def select_area(_, ev):
623620 def rect (x , y , w , h , z , pen_color = None , brush_color = None , ** args ):
624621 if pen_color is None :
625622 return CanvasRectangle (
626- self .canvas , x , y , w , h , z = z , onclick = select_area ,
627- ** args )
623+ self .canvas , x , y , w , h , z = z , onclick = select_area ,
624+ ** args )
628625 if brush_color is None :
629626 brush_color = pen_color
630627 return CanvasRectangle (
631- self .canvas , x , y , w , h , pen_color , brush_color , z = z ,
632- onclick = select_area , ** args )
628+ self .canvas , x , y , w , h , pen_color , brush_color , z = z ,
629+ onclick = select_area , ** args )
633630
634631 def line (x1 , y1 , x2 , y2 ):
635632 r = QGraphicsLineItem (x1 , y1 , x2 , y2 , None )
@@ -645,9 +642,9 @@ def line(x1, y1, x2, y2):
645642 if self .interior_coloring == self .PEARSON :
646643 s = sum (apriori_dists [0 ])
647644 expected = s * reduce (
648- mul ,
649- (apriori_dists [i ][used_vals [i ]] / float (s )
650- for i in range (len (used_vals ))))
645+ mul ,
646+ (apriori_dists [i ][used_vals [i ]] / float (s )
647+ for i in range (len (used_vals ))))
651648 actual = conditionaldict [attr_vals ]
652649 pearson = (actual - expected ) / sqrt (expected )
653650 if pearson == 0 :
@@ -657,11 +654,11 @@ def line(x1, y1, x2, y2):
657654 color = [self .RED_COLORS , self .BLUE_COLORS ][pearson > 0 ][ind ]
658655 rect (x0 , y0 , x1 - x0 , y1 - y0 , - 20 , color )
659656 outer_rect .setToolTip (
660- condition + "<hr/>" +
661- "Expected instances: %.1f<br>"
662- "Actual instances: %d<br>"
663- "Standardized (Pearson) residual: %.1f" %
664- (expected , conditionaldict [attr_vals ], pearson ))
657+ condition + "<hr/>" +
658+ "Expected instances: %.1f<br>"
659+ "Actual instances: %d<br>"
660+ "Standardized (Pearson) residual: %.1f" %
661+ (expected , conditionaldict [attr_vals ], pearson ))
665662 else :
666663 cls_values = get_variable_values_sorted (class_var )
667664 prior = get_distribution (data , class_var .name )
@@ -725,17 +722,16 @@ def line(x1, y1, x2, y2):
725722 apriori = [prior [key ] for key in cls_values ]
726723 n_apriori = sum (apriori )
727724 text = "<br/>" .join (
728- "<b>%s</b>: %d / %.1f%% (Expected %.1f / %.1f%%)" %
729- (cls , act , 100.0 * act / n_actual ,
730- apr / n_apriori * n_actual , 100.0 * apr / n_apriori
731- )
732- for cls , act , apr in zip (cls_values , actual , apriori
733- ))
725+ "<b>%s</b>: %d / %.1f%% (Expected %.1f / %.1f%%)" %
726+ (cls , act , 100.0 * act / n_actual ,
727+ apr / n_apriori * n_actual , 100.0 * apr / n_apriori
728+ )
729+ for cls , act , apr in zip (cls_values , actual , apriori ))
734730 else :
735731 text = ""
736732 outer_rect .setToolTip (
737- "{}<hr>Instances: {}<br><br>{}" .format (
738- condition , n_actual , text [:- 4 ]))
733+ "{}<hr>Instances: {}<br><br>{}" .format (
734+ condition , n_actual , text [:- 4 ]))
739735
740736 def draw_legend (x0_x1 , y0_y1 ):
741737 x0 , x1 = x0_x1
@@ -839,7 +835,7 @@ def get_max_label_width(attr):
839835 if square_size < 0 :
840836 return # canvas is too small to draw rectangles
841837 self .canvas_view .setSceneRect (
842- 0 , 0 , self .canvas_view .width (), self .canvas_view .height ())
838+ 0 , 0 , self .canvas_view .width (), self .canvas_view .height ())
843839
844840 drawn_sides = set ()
845841 draw_positions = {}
0 commit comments