source: branches/uq/gui/scripts/responseviewer.tcl @ 5743

Last change on this file since 5743 was 5743, checked in by mmh, 9 years ago

fix labels

File size: 6.6 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: ResponseViewer - Response Surface (surogate Model) Viewer
4#
5# Gets response data from PUQ and displayes it using vtk.
6# ======================================================================
7#  Copyright (c) 2004-2014  HUBzero Foundation, LLC
8#
9#  See the file "license.terms" for information on usage and
10#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11# ======================================================================
12package require Itk
13package require BLT
14
15option add *ResponseViewer.width 4i widgetDefault
16option add *ResponseViewer*cursor crosshair widgetDefault
17option add *ResponseViewer.height 4i widgetDefault
18option add *ResponseViewer.foreground black widgetDefault
19option add *ResponseViewer.controlBackground gray widgetDefault
20option add *ResponseViewer.controlDarkBackground #999999 widgetDefault
21option add *ResponseViewer.plotBackground black widgetDefault
22option add *ResponseViewer.plotForeground white widgetDefault
23option add *ResponseViewer.font \
24    -*-helvetica-medium-r-normal-*-12-* widgetDefault
25
26
27itcl::class Rappture::ResponseViewer {
28    inherit itk::Widget
29
30    itk_option define -plotforeground plotForeground Foreground ""
31    itk_option define -plotbackground plotBackground Background ""
32
33    constructor {args} {
34        # defined below
35    }
36    destructor {
37        # defined below
38    }
39
40    public method add {dataobj {settings ""}}
41    public method delete {args}
42    public method download {option args}
43
44    private variable _response
45    private variable _settings
46    private variable _label
47    private variable _dobj ""
48    private method _combobox_cb {}
49    private method _plot_response {var1 var2}
50}
51
52itk::usual ResponseViewer {
53    keep -background -foreground -cursor -font
54    keep -plotbackground -plotforeground
55}
56
57# ----------------------------------------------------------------------
58# CONSTRUCTOR
59# ----------------------------------------------------------------------
60itcl::body Rappture::ResponseViewer::constructor {args} {
61    option add hull.width hull.height
62    pack propagate $itk_component(hull) yes
63
64    itk_component add frame {
65        frame $itk_interior.frame
66    }
67
68    itk_component add frame2 {
69        frame $itk_interior.frame2
70    }
71}
72
73# ----------------------------------------------------------------------
74# DESTRUCTOR
75# ----------------------------------------------------------------------
76itcl::body Rappture::ResponseViewer::destructor {} {
77
78}
79
80# ----------------------------------------------------------------------
81# USAGE: add <dataobj> ?<settings>?
82#
83# Clients use this to add a dataobj to the plot.
84# ----------------------------------------------------------------------
85itcl::body Rappture::ResponseViewer::add {uqobj {settings ""}} {
86    puts "ResponseViewer ADD $uqobj ($settings)"
87    set _response [$uqobj response]
88    set vars [$uqobj variables]
89    set labs [$uqobj vlabels]
90    set _settings $settings
91    set _label [$uqobj label]
92
93    if {[string match "*:UQ:?" $_label]} {
94        set len [string length $_label]
95        set _label [string replace $_label [expr $len-5] end]
96    }
97    puts "LABEL=$_label"
98    puts "VARS=$vars [llength $vars]"
99
100    if {[llength $vars] > 1} {
101        set cb1 [Rappture::Combobox $itk_interior.frame2.cb1 -width 10 -editable no]
102        set cb2 [Rappture::Combobox $itk_interior.frame2.cb2 -width 10 -editable no]
103
104        foreach v $vars l $labs {
105            puts "inserting $v : $l"
106            $cb1 choices insert end $v $l
107            $cb2 choices insert end $v $l
108        }
109        $cb1 value [$cb1 label [lindex $vars 0]]
110        $cb2 value [$cb2 label [lindex $vars 1]]
111        bind $cb1 <<Value>> [itcl::code $this _combobox_cb]
112        bind $cb2 <<Value>> [itcl::code $this _combobox_cb]
113
114        set vs [label $itk_interior.frame2.desc -text "vs"]
115        grid $cb1 $vs $cb2
116    } else {
117        set label [lindex $labs 0]
118        set lab [label $itk_interior.frame2.desc -text "Response for $label"]
119        grid $lab
120    }
121
122    set rmsep [$uqobj rmse]
123    if {$rmsep > 10} {
124        set color red
125    } elseif {$rmsep > 5} {
126        set color orange
127    } elseif {$rmsep > 2} {
128        set color yellow
129    } else {
130        set color black
131    }
132    set rmse [label $itk_interior.frame2.rmse -text "RMSE: $rmsep%" -foreground $color]
133
134    grid $rmse -row 0 -column 4 -sticky e
135    grid $itk_component(frame) -row 0 -column 0 -sticky nsew
136    grid $itk_component(frame2) -row 1 -column 0 -sticky nsew
137    grid columnconfigure $itk_component(frame) 0 -weight 1
138    grid columnconfigure $itk_component(frame2) 3 -weight 1
139    grid rowconfigure $itk_component(frame) 0 -weight 1
140    grid columnconfigure $itk_component(hull) 0 -weight 1
141    grid rowconfigure $itk_component(hull) 0 -weight 1
142
143    if {[llength $vars] > 1} {
144        _combobox_cb
145    } else {
146        if {$_dobj != ""} {
147            destroy $itk_component(frame).field
148            destroy _dobj
149            set _dobj ""
150        }
151        set vname [lindex $vars 0]
152        after idle [itcl::code $this _plot_response $vname $vname]
153    }
154}
155
156itcl::body Rappture::ResponseViewer::_plot_response {var1 var2} {
157    puts "plotting $var1 vs $var2"
158
159    set fname response_result.xml
160
161    set rs $_response
162    if {[catch {exec get_response.py $fname $rs $var1 $var2 $_label} res]} {
163        set fp [open "response.err" r]
164        set rdata [read $fp]
165        close $fp
166        puts "Surrogate Model failed: $res\n$rdata"
167        error "Sampling of Surrogate Model failed: $res\n$rdata"
168        # FIXME: Maybe put in text box instead of error()?
169        return
170    }
171
172    set xmlobj [Rappture::library $fname]
173    puts "xmlobj=$xmlobj"
174    set w $itk_component(frame).field
175
176    if {$var1 == $var2} {
177        set path "output.curve(response)"
178        set _dobj [Rappture::Curve ::#auto $xmlobj $path]
179        puts "dobj=$_dobj"
180        Rappture::XyResult $w
181    } else {
182        set path "output.field(f2d)"
183        set _dobj [Rappture::Field ::#auto $xmlobj $path]
184        puts "dobj=$_dobj"
185        Rappture::FieldResult $w -mode heightmap
186    }
187    $w add $_dobj $_settings
188    $w scale $_dobj
189    puts "added _dobj"
190    puts "w=$w"
191    pack $w -expand yes -fill both
192    puts "DONE"
193}
194
195itcl::body Rappture::ResponseViewer::_combobox_cb {} {
196    if {$_dobj != ""} {
197        destroy $itk_component(frame).field
198        destroy _dobj
199        set _dobj ""
200    }
201    set var1 [$itk_component(frame2).cb1 current]
202    set var2 [$itk_component(frame2).cb2 current]
203    _plot_response $var1 $var2
204}
205
206
207itcl::body Rappture::ResponseViewer::download {option args} {
208    $itk_component(frame).field download $option $args
209}
Note: See TracBrowser for help on using the repository browser.