source: branches/1.4/gui/scripts/responseviewer.tcl @ 5937

Last change on this file since 5937 was 5937, checked in by mmh, 7 years ago

for 1d response show sample points

File size: 6.4 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 {[llength $vars] > 1} {
94        set cb1 [Rappture::Combobox $itk_interior.frame2.cb1 -width 10 -editable no]
95        set cb2 [Rappture::Combobox $itk_interior.frame2.cb2 -width 10 -editable no]
96
97        foreach v $vars l $labs {
98            $cb1 choices insert end $v $l
99            $cb2 choices insert end $v $l
100        }
101        $cb1 value [$cb1 label [lindex $vars 0]]
102        $cb2 value [$cb2 label [lindex $vars 1]]
103        bind $cb1 <<Value>> [itcl::code $this _combobox_cb]
104        bind $cb2 <<Value>> [itcl::code $this _combobox_cb]
105
106        set vs [label $itk_interior.frame2.desc -text "vs"]
107        grid $cb1 $vs $cb2
108    } else {
109        set label [lindex $labs 0]
110        set lab [label $itk_interior.frame2.desc -text "Response for $label"]
111        grid $lab
112    }
113
114    set rmsep [$uqobj rmse]
115    if {$rmsep > 10} {
116        set color red
117    } elseif {$rmsep > 5} {
118        set color orange
119    } elseif {$rmsep > 2} {
120        set color yellow
121    } else {
122        set color black
123    }
124    set rmse [label $itk_interior.frame2.rmse -text "RMSE: $rmsep%" -foreground $color]
125
126    grid $rmse -row 0 -column 4 -sticky e
127    grid $itk_component(frame) -row 0 -column 0 -sticky nsew
128    grid $itk_component(frame2) -row 1 -column 0 -sticky nsew
129    grid columnconfigure $itk_component(frame) 0 -weight 1
130    grid columnconfigure $itk_component(frame2) 3 -weight 1
131    grid rowconfigure $itk_component(frame) 0 -weight 1
132    grid columnconfigure $itk_component(hull) 0 -weight 1
133    grid rowconfigure $itk_component(hull) 0 -weight 1
134
135    if {[llength $vars] > 1} {
136        _combobox_cb
137    } else {
138        if {$_dobj != ""} {
139            destroy $itk_component(frame).field
140            destroy _dobj
141            set _dobj ""
142        }
143        set vname [lindex $vars 0]
144        after idle [itcl::code $this _plot_response $vname $vname]
145    }
146}
147
148itcl::body Rappture::ResponseViewer::_plot_response {var1 var2} {
149    #puts "plotting $var1 vs $var2"
150
151    set fname response_result.xml
152
153    set rs $_response
154    if {[catch {exec get_response.py $fname $rs $var1 $var2 $_label} res]} {
155        set fp [open "response.err" r]
156        set rdata [read $fp]
157        close $fp
158        puts "Surrogate Model failed: $res\n$rdata"
159        error "Sampling of Surrogate Model failed: $res\n$rdata"
160        # FIXME: Maybe put in text box instead of error()?
161        return
162    }
163
164    set xmlobj [Rappture::library $fname]
165    #puts "xmlobj=$xmlobj"
166    set w $itk_component(frame).field
167
168    if {$var1 == $var2} {
169        set path "output.curve(scatter)"
170        set _dobj [Rappture::Curve ::#auto $xmlobj $path]
171        Rappture::XyResult $w
172        $w add $_dobj {-color red}
173
174        set path "output.curve(response)"
175        set _dobj [Rappture::Curve ::#auto $xmlobj $path]
176
177    } else {
178        set path "output.field(f2d)"
179        set _dobj [Rappture::Field ::#auto $xmlobj $path]
180        Rappture::FieldResult $w -mode heightmap
181    }
182    $w add $_dobj $_settings
183    $w scale $_dobj
184    #puts "added _dobj"
185    #puts "w=$w"
186    pack $w -expand yes -fill both
187}
188
189itcl::body Rappture::ResponseViewer::_combobox_cb {} {
190    if {$_dobj != ""} {
191        destroy $itk_component(frame).field
192        destroy _dobj
193        set _dobj ""
194    }
195    set var1 [$itk_component(frame2).cb1 current]
196    set var2 [$itk_component(frame2).cb2 current]
197    _plot_response $var1 $var2
198}
199
200
201itcl::body Rappture::ResponseViewer::download {option args} {
202    $itk_component(frame).field download $option $args
203}
Note: See TracBrowser for help on using the repository browser.