@@ -29,6 +29,12 @@ class ChiSqStats:
2929 pair of attributes. The class is also used for ranking.
3030 """
3131 def __init__ (self , data , attr1 , attr2 ):
32+ attr1 = data .domain [attr1 ]
33+ attr2 = data .domain [attr2 ]
34+ if attr1 .is_discrete and not attr1 .values or \
35+ attr2 .is_discrete and not attr2 .values :
36+ self .p = np .nan
37+ return
3238 self .observed = get_contingency (data , attr1 , attr2 )
3339 self .n = np .sum (self .observed )
3440 self .probs_x = self .observed .sum (axis = 0 ) / self .n
@@ -403,15 +409,22 @@ def _oper(attr, txt):
403409 view = self .canvasView
404410
405411 chi = ChiSqStats (self .discrete_data , disc_x , disc_y )
406- n = chi .n
407412 max_ylabel_w = max ((width (val ) for val in disc_y .values ), default = 0 )
408413 max_ylabel_w = min (max_ylabel_w , 200 )
409414 x_off = width (attr_x .name ) + max_ylabel_w
410415 y_off = 15
411- square_size = min (view .width () - x_off - 35 , view .height () - y_off - 50 )
416+ square_size = min (view .width () - x_off - 35 , view .height () - y_off - 80 )
412417 square_size = max (square_size , 10 )
413418 self .canvasView .setSceneRect (0 , 0 , view .width (), view .height ())
414-
419+ if not disc_x .values or not disc_y .values :
420+ text_ = "Features {} and {} have no values" .format (disc_x , disc_y ) \
421+ if not disc_x .values and not disc_y .values and \
422+ disc_x != disc_y else "Feature {} has no values" .format (
423+ disc_x if not disc_x .values else disc_y )
424+ text (text_ , view .width () / 2 + 70 , view .height () / 2 ,
425+ Qt .AlignRight | Qt .AlignVCenter )
426+ return
427+ n = chi .n
415428 curr_x = x_off
416429 max_xlabel_h = 0
417430 self .areas = []
@@ -452,6 +465,7 @@ def _oper(attr, txt):
452465 Qt .AlignLeft | Qt .AlignVCenter , bold = True , vertical = True )
453466 text (attr_x .name , x_off + square_size / 2 , bottom ,
454467 Qt .AlignHCenter | Qt .AlignTop , bold = True )
468+ bottom += 30
455469 xl = text ("χ²={:.2f}, p={:.3f}" .format (chi .chisq , chi .p ),
456470 0 , bottom )
457471 # Assume similar height for both lines
0 commit comments