File: plotspecial.tcl

package info (click to toggle)
tklib 0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,156 kB
  • sloc: tcl: 105,088; sh: 2,573; ansic: 792; pascal: 359; makefile: 69; sed: 53; exp: 21
file content (624 lines) | stat: -rwxr-xr-x 17,447 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# plotspecial.tcl --
#    Facilities to draw specialised plots in a dedicated canvas
#
# Note:
#    It is a companion of "plotchart.tcl"
#

# DrawTargetData --
#    Compute the coordinates for the symbol representing the skill and draw it
#
# Arguments:
#    w           Name of the canvas
#    series      Name of the series of symbols
#    xvalues     List of model results
#    yvalues     List of measurements to which the model results are compared
# Result:
#    None
#
# Side effects:
#    Symbol drawn
#
# Note:
#    The lists of model data and measurements must have the same length
#    Missing data can be represented as {}. Only pairs that have both x and
#    y values are used in the computations.
#
proc ::Plotchart::DrawTargetData { w series xvalues yvalues } {
    variable data_series

    if { [llength $xvalues] != [llength $yvalues] } {
        return -code error "Lists of model data and measurements should have the same length"
    }

    set xn {}
    set yn {}
    set xmean 0.0
    set ymean 0.0
    set count 0

    foreach x $xvalues y $yvalues {
        if { $x != {} && $y != {} } {
            lappend xn $x
            lappend yn $y

            set xmean [expr {$xmean + $x}]
            set ymean [expr {$ymean + $y}]
            incr count
        }
    }

    if { $count <= 1 } {
        return
    }

    set xmean [expr {$xmean/double($count)}]
    set ymean [expr {$ymean/double($count)}]

    set sumx2  0.0
    set sumy2  0.0
    set sumxy  0.0

    foreach x $xn y $yn {
        set sumx2 [expr {$sumx2 + ($x-$xmean)*($x-$xmean)}]
        set sumy2 [expr {$sumy2 + ($y-$ymean)*($y-$ymean)}]
        set sumxy [expr {$sumxy + ($x-$xmean)*($y-$ymean)}]
    }

    set stdevx [expr {sqrt($sumx2 / double($count-1))}]
    set stdevy [expr {sqrt($sumy2 / double($count-1))}]
    set corrxy [expr {$sumxy / $stdevx / $stdevy / double($count-1)}]

    set bstar  [expr {($xmean-$ymean) / $stdevy}]
    set sstar2 [expr {$sumx2 / $sumy2}]
    set rmsd   [expr {sqrt(1.0 + $sstar2 - 2.0 * sqrt($sstar2) * $corrxy)}]

    DataConfig $w $series -type symbol
    DrawData $w $series $rmsd $bstar
}

# DrawTaylorData --
#    Compute the coordinates for the symbol in a Taylor diagram and draw it
#
# Arguments:
#    w           Name of the canvas
#    series      Name of the series of symbols
#    stdev       Standard deviation
#    corr        Correlation
# Result:
#    None
#
# Side effects:
#    Symbol drawn
#
proc ::Plotchart::DrawTaylorData { w series stdev corr } {

    set angle [expr {acos($corr)}]
    set xdata [expr {$stdev * cos($angle)}]
    set ydata [expr {$stdev * sin($angle)}]

    DataConfig $w $series -type symbol
    DrawData $w $series $xdata $ydata
}

# DrawPerformanceData --
#    Compute the coordinates for the performance profiles and draw the lines
#
# Arguments:
#    w                  Name of the canvas
#    profiledata        Names and data for each profiled item
# Result:
#    None
#
# Side effects:
#    Symbol drawn
#
# Note:
#    The lists of model data and measurements must have the same length
#    Missing data can be represented as {}. Only pairs that have both x and
#    y values are used in the computations.
#
proc ::Plotchart::DrawPerformanceData { w profiledata } {
    variable data_series
    variable scaling

    #
    # Determine the minima per solved problem - they function as scale factors
    #
    set scale {}
    set values [lindex $profiledata 1]
    set number [llength $values]
    foreach v $values {
        lappend scale {}
    }

    foreach {series values} $profiledata {
        set idx 0
        foreach s $scale v $values {
            if { $s == {} || $s > $v } {
                lset scale $idx $v
            }
            incr idx
        }
    }

    #
    # Scale the data (remove the missing values)
    # and draw the series
    #
    set plotdata {}
    foreach {series values} $profiledata {
        set newvalues {}
        foreach s $scale v $values {
            if { $s != {} && $v != {} && $s != 0.0 } {
                lappend newvalues [expr {$v / $s}]
            }
        }
        set newvalues [lsort -real $newvalues]

        set count     1

        set yprev     {}
        foreach v $newvalues vn [concat [lrange $newvalues 1 end] 1.0e20] {
            set y [expr {$count/double($number)}]

            #
            # Construct the staircase
            #
            if { $v != $vn } {
                if { $yprev == {} } {
                    DrawData $w $series 1.0 $y
                } else {
                    DrawData $w $series $v $yprev
                }

                DrawData $w $series $v $y
                set  yprev $y
            }
            incr count
        }
    }
}

# DrawDataNormalPlot --
#    Compute the coordinates for the empirical distribution and draw the series
#    in a normal distribution plot
#
# Arguments:
#    w                  Name of the canvas
#    series             Name of the series
#    mean               Estimated mean
#    stdev              Estimated standard deviation
#    data               Actual data
# Result:
#    None
#
# Note:
#    The value of "a" is adopted from the corresponding Wikipedia page,
#    which in turn adopted it from the R "stats" package (qqnorm function)
#
proc ::Plotchart::DrawDataNormalPlot { w series mean stdev data } {
    set n   [llength $data]
    set a   0.375
    if { $n > 10 } {
        set a 0.5
    }

    set idx 1
    foreach x [lsort -real -increasing $data] {
        if { $x != {} } {
            set xn [expr {($x - $mean) / $stdev}]
            set pn [expr {($idx - $a) / ($n + 1 - 2.0 * $a)}]
            set yn [::math::statistics::Inverse-cdf-normal 0.0 1.0 $pn]

            DrawData $w $series $xn $yn
        } else {
            DrawData $w $series {}  {}
        }
        incr idx
    }
}

# DrawDiagonalNormalPlot --
#    Draw the diagonal line in a normal distribution plot
#
# Arguments:
#    w                  Name of the canvas
# Result:
#    None
#
# Note:
#    You can use the "diagonal" series to configure its colour
#
proc ::Plotchart::DrawDiagonalNormalPlot { w } {
    variable scaling

    DrawData $w diagonal $scaling($w,xmin) $scaling($w,ymin)
    DrawData $w diagonal $scaling($w,xmax) $scaling($w,ymax)
}

# DrawCircleOutline --
#    Draw the circle and the labels for a circle plot
#
# Arguments:
#    w                  Name of the canvas
#    labels             Labels for the items
# Result:
#    None
#
proc ::Plotchart::DrawCircleOutline { w labels } {
    variable scaling
    variable data_series

    foreach {xcentre ycentre} [PolarToPixelPriv $w 0.0 0.0] {break}
    foreach {xeast   ycentre} [PolarToPixelPriv $w 1.0 0.0] {break}

    set radius [expr {$xeast - $xcentre}]

    $w create oval [expr {$xcentre - $radius}] [expr {$ycentre - $radius}] [expr {$xcentre + $radius}] [expr {$ycentre + $radius}] \
         -tag circle

    set numberLabels [llength $labels]
    set dangle [expr {360.0/$numberLabels}]

    for {set n 0} {$n < $numberLabels} {incr n} {
        set angle  [expr {$n * $dangle}]
        set anchor w

        set textAngle $angle

        if { $textAngle > 90.0 && $textAngle < 270.0 } {
            set textAngle [expr {$textAngle - 180.0}]
            set anchor e
        }

        #set angle [expr {acos(-1.0)/180.0 * $angle}]

        foreach {x y}   [PolarToPixelPriv $w 1.0  $angle] {break}
        foreach {xt yt} [PolarToPixelPriv $w 1.05 $angle] {break}

        set dotId  [.c create oval [expr {$x - 3}] [expr {$y - 3}] [expr {$x + 3}] [expr {$y + 3}] -fill black -tag circle]
        set textId [.c create text $xt $yt -text [lindex $labels $n] -anchor $anchor -angle $textAngle -tag circle]

        lappend data_series($w,dotIds)  $dotId
        lappend data_series($w,textIds) $textId
        lappend data_series($w,angles)  [expr {acos(-1.0)/180.0 * $angle}]
    }
}

# DrawCircleModify --
#    Modify the label/dot in a circle plot
#
# Arguments:
#    w                  Name of the canvas
#    label              Label for which things should be modified
#    args               Key-value pairs describing the modifications
# Result:
#    None
# Note:
#    The keys should be among the recognised keys
#
proc ::Plotchart::DrawCircleModify { w label args } {
    variable data_series

    set idx [lsearch $data_series($w,labels) $label]

    if { $idx < 0 } {
        return -code error "Unknown label -- $label"
    }

    set textId [lindex $data_series($w,textIds) $idx]
    set dotId  [lindex $data_series($w,dotIds)  $idx]

    foreach {key value} $args {
        switch -- $key {
            "-textcolor" -
            "-textcolour" {
                 $w itemconfig $textId -fill $value
            }
            "-font" {
                 $w itemconfig $textId -font $value
            }
            "-dotcolor" -
            "-dotcolour" {
                 $w itemconfig $dotId -fill $value -outline $value
            }
        }
    }
}

# DrawCircleConnection --
#    Connect the dots, identified by their labels in a circle plot
#
# Arguments:
#    w                  Name of the canvas
#    first              First label
#    second             Second label
#    colour             Colour of the connecting line
#    thicnkess          Thickness of the line
# Result:
#    None
#
proc ::Plotchart::DrawCircleConnection { w first second colour thickness } {
    variable scaling
    variable data_series

    set idx1 [lsearch $data_series($w,labels) $first]
    set idx2 [lsearch $data_series($w,labels) $second]

    if { $idx1 < 0 } {
        return -code error "Unknown label -- $first"
    }
    if { $idx2 < 0 } {
        return -code error "Unknown label -- $second"
    }

    set angle1 [lindex $data_series($w,angles) $idx1]
    set angle2 [lindex $data_series($w,angles) $idx2]

    foreach {xcentre ycentre} [PolarToPixelPriv $w 0.0 0.0] {break}
    foreach {xeast   ycentre} [PolarToPixelPriv $w 1.0 0.0] {break}

    set radius [expr {$xeast - $xcentre}]

    # Create a parabola:
    #
    #    y = a * x**2 + b
    #
    # where a and b are determined such that the parabola
    # intersects the circle in two points such that the
    # tangents to the parabola in these points make an
    # angle 2 * alpha. (alpha is the angle of the tangent to
    # the positive x-axis)
    # Then:
    #    a = 0.5 * sin alpha / cos alpha ** 2
    #    b = 0.5 * sin alpha
    #
    # First create the points for the parabola, then shift
    # and rotate
    #
    set alpha [expr {0.5 * acos(-1.0) - 0.5 * ($angle2 - $angle1)}]
    set beta  [expr {-$alpha + $angle1}]
    #set beta  0.0

    set a [expr {0.5 * sin($alpha) / cos($alpha) ** 2}]
    set b [expr {0.5 * sin($alpha)}]

    set coords [list]

    set dx [expr {cos($alpha)/4.0}]
    for {set i -4} {$i <= 4} {incr i} {
        set x [expr {$dx * $i}]
        set y [expr {$a * $x**2 + $b}]

        set xp [expr {$xcentre + $radius * ($x * cos($beta) - $y * sin($beta))}]
        set yp [expr {$ycentre - $radius * ($x * sin($beta) + $y * cos($beta))}]

        lappend coords $xp $yp
    }

    $w create line $coords -smooth 1 -width $thickness -fill $colour -tag {circle connection}
    $w lower connection
}

# DrawHeatmapOutline --
#    Draw the outline of the heatmap - labels and rectangles
#
# Arguments:
#    w                  Name of the canvas
#    rowlabels          Labels for the rows
#    columnlabels       Labels for the columns
# Result:
#    None
#
proc ::Plotchart::DrawHeatmapOutline { w rowlabels columnlabels } {
    variable scaling
    variable data_series

    #
    # Height of the rows and width of the columns
    #
    set pxmin $scaling($w,pxmin)
    set pxmax $scaling($w,pxmax)
    set pymin $scaling($w,pymin)
    set pymax $scaling($w,pymax)

    set rowheight [expr {min( 30, ($pymax-$pymin) / [llength $rowlabels])}]
    set colwidth  [expr {min(100, ($pxmax-$pxmin) / [llength $columnlabels])}]

    set xt        [expr {$pxmin - 10}]
    set yt        [expr {$pymin - $rowheight / 2}]

    foreach label $rowlabels {
        set yt [expr {$yt + $rowheight}]
        $w create text $xt $yt -text $label -anchor e
    }

    set xt        [expr {$pxmin - $colwidth / 2}]
    set yt        [expr {$pymin - 10}]

    foreach label $columnlabels {
        set xt [expr {$xt + $colwidth}]
        $w create text $xt $yt -text $label -anchor s
    }

    set ye $pymin

    foreach row $rowlabels {
        set yb $ye
        set ye [expr {$ye + $rowheight}]

        set xe $pxmin
        foreach column $columnlabels {
            set xb $xe
            set xe [expr {$xe + $colwidth}]

            set data_series($w,$row,$column) [$w create rectangle $xb $yb $xe $ye]
        }
    }
}

# DrawHeatmapOutline --
#    Draw the outline of the heatmap - labels and rectangles
#
# Arguments:
#    w                  Name of the canvas
#    rowlabels          Labels for the rows
#    columnlabels       Labels for the columns
# Result:
#    None
#
proc ::Plotchart::DrawHeatmapOutline { w rowlabels columnlabels } {
    variable scaling
    variable data_series

    #
    # Height of the rows and width of the columns
    #
    set pxmin $scaling($w,pxmin)
    set pxmax $scaling($w,pxmax)
    set pymin $scaling($w,pymin)
    set pymax $scaling($w,pymax)

    set rowheight [expr {min( 30, ($pymax-$pymin) / [llength $rowlabels])}]
    set colwidth  [expr {min(100, ($pxmax-$pxmin) / [llength $columnlabels])}]

    set xt        [expr {$pxmin - 10}]
    set yt        [expr {$pymin - $rowheight / 2}]

    foreach label $rowlabels {
        set yt [expr {$yt + $rowheight}]
        $w create text $xt $yt -text $label -anchor e
    }

    set xt        [expr {$pxmin - $colwidth / 2}]
    set yt        [expr {$pymin - 10}]

    foreach label $columnlabels {
        set xt [expr {$xt + $colwidth}]
        $w create text $xt $yt -text $label -anchor s
    }

    set ye $pymin

    foreach row $rowlabels {
        set yb $ye
        set ye [expr {$ye + $rowheight}]

        set xe $pxmin
        foreach column $columnlabels {
            set xb $xe
            set xe [expr {$xe + $colwidth}]

            set data_series($w,$row,$column) [$w create rectangle $xb $yb $xe $ye]
        }
    }
}

# DrawHeatmapCells --
#    Fill the rectangle(s) belonging to the selected cells
#
# Arguments:
#    w                  Name of the canvas
#    selection          Type of selected cells
#    args               Arguments - the meaning is determined by "selection"
# Result:
#    None
#
proc ::Plotchart::DrawHeatmapCells { w selection args } {
    variable scaling
    variable data_series

    switch -- $selection {
        "row" {
            set rows    [list [lindex $args 0]]
            set columns $data_series($w,columnlabels)
            set values  [lindex $args 1]
        }
        "column" {
            set columns [list [lindex $args 0]]
            set rows    $data_series($w,rowlabels)
            set values  [lindex $args 1]
        }
        "cell" {
            set rows    [list [lindex $args 0]]
            set columns [list [lindex $args 1]]
            set values  [lindex $args 2]
        }
        default {
            return -code error "Unknown type of selection: $selection"
        }
    }

    set n 0
    foreach row $rows {
        foreach column $columns {
            set item   $data_series($w,$row,$column)
            set colour [HeatmapColour $data_series($w,startcolour) $data_series($w,endcolour) \
                                      $data_series($w,startvalue)  $data_series($w,endvalue)  \
                                      [lindex $values $n]]

            $w itemconfigure $item -fill $colour

            incr n
        }
    }
}

# HeatmapColour --
#    Construct the colour for a heatmap cell
#
# Arguments:
#    startcolour        RGB values for start colour
#    endcolour          RGB values for end colour
#    startvalue         Minimum value for the colour scale
#    endvalue           Maximum value for the colour scale
#    value              Actual value
# Result:
#    Colour to be used
#
proc ::Plotchart::HeatmapColour { startcolour endcolour startvalue endvalue value } {

    set value  [expr {min( $endvalue, max( $startvalue, $value))}]
    set factor [expr {$value / double($endvalue - $startvalue)}]

    set colour [list]
    foreach b $startcolour e $endcolour {
        set comp [expr {int($b * (1.0 - $factor) + $e * $factor)}]
        set comp [expr {min(635535,$comp)}]

        lappend colour $comp
    }

    return [format "#%4.4x%4.4x%4.4x" {*}$colour]
}

# ConfigHeatmapScale --
#    Set the properties of the colour scale
#
# Arguments:
#    w                  Name of the canvas
#    prop               Property (values or colours)
#    start              Minimum value for the property
#    end                Maximum value for the property
#    value              Actual value
# Result:
#    Colour to be used
#
proc ::Plotchart::ConfigHeatmapScale { w prop start end } {
    variable data_series

    switch -- $prop {
        "values" {
            set data_series($w,startvalue) $start
            set data_series($w,endvalue)   $end
        }
        "colours" {
            set data_series($w,startcolour) [winfo rgb . $start]
            set data_series($w,endcolour)   [winfo rgb . $end  ]
        }
        default {
            return -code error "Unknown type of property: $prop"
        }
    }
}