1 | #!/usr/bin/env python |
---|
2 | from pyre.applications.Script import Script |
---|
3 | import sys |
---|
4 | |
---|
5 | #pylint: disable-msg=W0212 |
---|
6 | #pylint: disable-msg=W0613 |
---|
7 | |
---|
8 | |
---|
9 | from pyre.components.Component import Component |
---|
10 | class ToyModel(Component): |
---|
11 | |
---|
12 | has_changed = False |
---|
13 | |
---|
14 | class Inventory(Component.Inventory): |
---|
15 | import pyre.inventory as pyre_inventory |
---|
16 | |
---|
17 | A = pyre_inventory.float( "A", default = 1.0 ) |
---|
18 | B = pyre_inventory.float( "B", default = 2.0 ) |
---|
19 | |
---|
20 | def __call__(self, *args, **kwds): |
---|
21 | """ Evaluate the model. |
---|
22 | For the real component, evaluate will |
---|
23 | be done through a model object. |
---|
24 | """ |
---|
25 | return self.A*10.0*args[0] + self.B |
---|
26 | |
---|
27 | def _configure(self): |
---|
28 | Component._configure(self) |
---|
29 | self.A = self.inventory.A |
---|
30 | self.B = self.inventory.B |
---|
31 | |
---|
32 | def set(self, key, value): |
---|
33 | """ For the real component, we will push the |
---|
34 | values to the underlying model object |
---|
35 | using this method. |
---|
36 | """ |
---|
37 | if hasattr(self, key.upper()): |
---|
38 | setattr(self, key.upper(), value) |
---|
39 | else: |
---|
40 | raise ValueError, "ToyModel does not have %s" % key |
---|
41 | |
---|
42 | def __setattr__(self, key, value): |
---|
43 | self.__dict__["has_changed"] = True |
---|
44 | return Component.__setattr__(self, key, value) |
---|
45 | |
---|
46 | def changed(self): |
---|
47 | tmp = self.has_changed |
---|
48 | self.has_changed = False |
---|
49 | return tmp |
---|
50 | |
---|
51 | def xaxis(self): |
---|
52 | return "time", "t", "s" |
---|
53 | def yaxis(self): |
---|
54 | return "amplitude", "A", "" |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | |
---|
59 | class DemoApp(Script): |
---|
60 | """ Demo application to demonstrate the kind of object manipulations |
---|
61 | our SANS applications will need to do. |
---|
62 | """ |
---|
63 | |
---|
64 | class Inventory(Script.Inventory): |
---|
65 | import pyre.inventory |
---|
66 | ## We need a model object to manipulate |
---|
67 | model = pyre.inventory.facility('model', default = 'sphere') |
---|
68 | |
---|
69 | def demo_plotter(self,graph): |
---|
70 | import wx |
---|
71 | from mplplotter import Plotter |
---|
72 | |
---|
73 | # Make a frame to show it |
---|
74 | frame = wx.Frame(None,-1,'Plottables') |
---|
75 | self.plotter = Plotter(frame) |
---|
76 | frame.Show() |
---|
77 | |
---|
78 | # render the graph to the pylab plotter |
---|
79 | graph.render(self.plotter) |
---|
80 | |
---|
81 | def onIdle(event): |
---|
82 | if graph.changed(): |
---|
83 | graph.render(self.plotter) |
---|
84 | event.RequestMore() |
---|
85 | frame.Bind(wx.EVT_IDLE,onIdle) |
---|
86 | |
---|
87 | |
---|
88 | def main(self, *args, **kwds): |
---|
89 | """ |
---|
90 | Main loop |
---|
91 | """ |
---|
92 | import wx |
---|
93 | app = wx.PySimpleApp() |
---|
94 | |
---|
95 | print "Model output (%s)= %g" % (self.model.name, self.model(.1)) |
---|
96 | |
---|
97 | # Popup graph |
---|
98 | import plottables |
---|
99 | import pyre_plottable |
---|
100 | |
---|
101 | model2 = ToyModel('toymodel','sans') |
---|
102 | model2._configure() |
---|
103 | model2._init() |
---|
104 | model2.set("A", 4.0) |
---|
105 | |
---|
106 | |
---|
107 | m = pyre_plottable.Model(self.model) |
---|
108 | m2 = pyre_plottable.Model(model2) |
---|
109 | self.graph = plottables.Graph() |
---|
110 | self.graph.title('TEST') |
---|
111 | self.graph.add(m) |
---|
112 | self.graph.add(m2) |
---|
113 | |
---|
114 | self.demo_plotter(self.graph) |
---|
115 | |
---|
116 | import thread |
---|
117 | thread.start_new(self.console,()) |
---|
118 | |
---|
119 | app.MainLoop() |
---|
120 | print "Bye" |
---|
121 | return |
---|
122 | |
---|
123 | def newModelTest(self): |
---|
124 | """ |
---|
125 | Create a cylinder model directly |
---|
126 | """ |
---|
127 | ComponentClass = ScatteringIntensityFactory(CylinderModel) |
---|
128 | self.model._fini() |
---|
129 | self.model = ComponentClass('cylinder') |
---|
130 | self.model._configure() |
---|
131 | self.model._init() |
---|
132 | |
---|
133 | def newModel(self, model_name): |
---|
134 | """ |
---|
135 | Instantiate a new model |
---|
136 | @param model_name: name of new model [string] |
---|
137 | """ |
---|
138 | import pyre.inventory |
---|
139 | |
---|
140 | fac = pyre.inventory.facility('model', default = model_name) |
---|
141 | new_model, locator = fac._getDefaultValue(self.inventory) |
---|
142 | new_model._configure() |
---|
143 | new_model._init() |
---|
144 | |
---|
145 | self.model._fini() |
---|
146 | self.model = new_model |
---|
147 | |
---|
148 | def _configure(self): |
---|
149 | """ |
---|
150 | Get the inventory items |
---|
151 | """ |
---|
152 | Script._configure(self) |
---|
153 | self.model = self.inventory.model |
---|
154 | return |
---|
155 | |
---|
156 | def console(self): |
---|
157 | # Read user commands |
---|
158 | continue_flag = True |
---|
159 | while(continue_flag): |
---|
160 | command = raw_input(">").lower() |
---|
161 | |
---|
162 | # Quit the application ###################################### |
---|
163 | if command.count('q')>0: |
---|
164 | continue_flag = False |
---|
165 | |
---|
166 | # Get the value of a parameter ############################## |
---|
167 | elif command.count('get')>0: |
---|
168 | toks = command.split() |
---|
169 | if len(toks)==2: |
---|
170 | try: |
---|
171 | # Get the attribute with the given name |
---|
172 | print "%s = %s" % (toks[1], getattr(self.model, toks[1])) |
---|
173 | except: |
---|
174 | print sys.exc_value |
---|
175 | else: |
---|
176 | print "Usage: Get <name of parameter>" |
---|
177 | |
---|
178 | # Set the value of a parameter ############################## |
---|
179 | elif command.count('set')>0: |
---|
180 | toks = command.split() |
---|
181 | if len(toks)==3: |
---|
182 | try: |
---|
183 | self.model.set(toks[1].lstrip().rstrip(), float(toks[2])) |
---|
184 | print "Model output: ", self.model(1.0) |
---|
185 | except: |
---|
186 | print sys.exc_value |
---|
187 | else: |
---|
188 | print "Usage: Set <name of parameter> <value>" |
---|
189 | |
---|
190 | # Set the active model ###################################### |
---|
191 | elif command.count('model')>0: |
---|
192 | toks = command.split() |
---|
193 | if len(toks)==2: |
---|
194 | try: |
---|
195 | new_name = toks[1].lstrip().rstrip() |
---|
196 | self.newModel(new_name) |
---|
197 | print "Model output (%s) = %g" % (new_name, self.model(1.0)) |
---|
198 | except: |
---|
199 | print sys.exc_value |
---|
200 | else: |
---|
201 | print "Usage: model <name of the model>" |
---|
202 | |
---|
203 | # Help ###################################################### |
---|
204 | else: |
---|
205 | print "Available commands:" |
---|
206 | print " get <parameter>" |
---|
207 | print " set <parameter> <value>" |
---|
208 | print " model <model name>" |
---|
209 | print " q" |
---|
210 | |
---|
211 | if self.graph.changed(): |
---|
212 | self.graph.render(self.plotter) |
---|
213 | |
---|
214 | |
---|
215 | # main |
---|
216 | if __name__ == '__main__': |
---|
217 | app = DemoApp('sans') |
---|
218 | app.run() |
---|
219 | |
---|
220 | |
---|
221 | # version |
---|
222 | __id__ = "$Id$" |
---|
223 | |
---|
224 | # End of file |
---|