[f3d51f6] | 1 | #TODO: Use simview to generate P(r) and I(q) pairs in sansview. |
---|
| 2 | # Make sure the option of saving each curve is available |
---|
| 3 | # Use the I(q) curve as input and compare the output to P(r) |
---|
| 4 | |
---|
| 5 | import os |
---|
[4a5de6f] | 6 | import sys |
---|
[f3d51f6] | 7 | import wx |
---|
[aa4b8379] | 8 | import logging |
---|
[f3d51f6] | 9 | from sans.guitools.plottables import Data1D, Theory1D |
---|
| 10 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
| 11 | import math, numpy |
---|
| 12 | from sans.pr.invertor import Invertor |
---|
| 13 | |
---|
[aa4b8379] | 14 | PR_FIT_LABEL = "P_{fit}(r)" |
---|
| 15 | PR_LOADED_LABEL = "P_{loaded}(r)" |
---|
| 16 | IQ_DATA_LABEL = "I_{obs}(q)" |
---|
| 17 | |
---|
| 18 | import wx.lib |
---|
| 19 | (NewPrFileEvent, EVT_PR_FILE) = wx.lib.newevent.NewEvent() |
---|
| 20 | |
---|
| 21 | |
---|
[f3d51f6] | 22 | class Plugin: |
---|
| 23 | |
---|
[b659551] | 24 | DEFAULT_ALPHA = 0.0001 |
---|
| 25 | DEFAULT_NFUNC = 10 |
---|
| 26 | DEFAULT_DMAX = 140.0 |
---|
| 27 | |
---|
[f3d51f6] | 28 | def __init__(self): |
---|
| 29 | ## Plug-in name |
---|
| 30 | self.sub_menu = "Pr inversion" |
---|
| 31 | |
---|
| 32 | ## Reference to the parent window |
---|
| 33 | self.parent = None |
---|
| 34 | |
---|
| 35 | ## Simulation window manager |
---|
| 36 | self.simview = None |
---|
| 37 | |
---|
| 38 | ## List of panels for the simulation perspective (names) |
---|
| 39 | self.perspective = [] |
---|
| 40 | |
---|
| 41 | ## State data |
---|
[b659551] | 42 | self.alpha = self.DEFAULT_ALPHA |
---|
| 43 | self.nfunc = self.DEFAULT_NFUNC |
---|
| 44 | self.max_length = self.DEFAULT_DMAX |
---|
[634f1cf] | 45 | self.q_min = None |
---|
| 46 | self.q_max = None |
---|
[f3d51f6] | 47 | ## Remember last plottable processed |
---|
| 48 | self.last_data = "sphere_60_q0_2.txt" |
---|
| 49 | ## Time elapsed for last computation [sec] |
---|
| 50 | # Start with a good default |
---|
| 51 | self.elapsed = 0.022 |
---|
[aa4b8379] | 52 | self.iq_data_shown = False |
---|
[f3d51f6] | 53 | |
---|
| 54 | ## Current invertor |
---|
| 55 | self.invertor = None |
---|
[3fd1ebc] | 56 | self.pr = None |
---|
[f3d51f6] | 57 | ## Calculation thread |
---|
| 58 | self.calc_thread = None |
---|
| 59 | ## Estimation thread |
---|
| 60 | self.estimation_thread = None |
---|
| 61 | ## Result panel |
---|
| 62 | self.control_panel = None |
---|
| 63 | ## Currently views plottable |
---|
| 64 | self.current_plottable = None |
---|
[b659551] | 65 | ## Number of P(r) points to display on the output plot |
---|
| 66 | self._pr_npts = 51 |
---|
[aa4b8379] | 67 | ## Flag to let the plug-in know that it is running standalone |
---|
| 68 | self.standalone = True |
---|
| 69 | |
---|
| 70 | # Log startup |
---|
| 71 | logging.info("Pr(r) plug-in started") |
---|
| 72 | |
---|
| 73 | |
---|
[f3d51f6] | 74 | |
---|
| 75 | def populate_menu(self, id, owner): |
---|
| 76 | """ |
---|
| 77 | Create a menu for the plug-in |
---|
| 78 | """ |
---|
[7cb0353] | 79 | return [] |
---|
[f3d51f6] | 80 | import wx |
---|
| 81 | shapes = wx.Menu() |
---|
| 82 | |
---|
| 83 | id = wx.NewId() |
---|
| 84 | shapes.Append(id, '&Sphere test') |
---|
| 85 | wx.EVT_MENU(owner, id, self._fit_pr) |
---|
[119a11d] | 86 | |
---|
[f3d51f6] | 87 | return [(id, shapes, "P(r)")] |
---|
| 88 | |
---|
[119a11d] | 89 | def help(self, evt): |
---|
| 90 | """ |
---|
| 91 | Show a general help dialog. |
---|
| 92 | TODO: replace the text with a nice image |
---|
| 93 | """ |
---|
| 94 | from inversion_panel import HelpDialog |
---|
| 95 | dialog = HelpDialog(None, -1) |
---|
| 96 | if dialog.ShowModal() == wx.ID_OK: |
---|
| 97 | dialog.Destroy() |
---|
| 98 | else: |
---|
| 99 | dialog.Destroy() |
---|
| 100 | |
---|
[f3d51f6] | 101 | def _fit_pr(self, evt): |
---|
| 102 | from sans.pr.invertor import Invertor |
---|
| 103 | import numpy |
---|
| 104 | import pylab |
---|
| 105 | import math |
---|
| 106 | from sans.guicomm.events import NewPlotEvent |
---|
| 107 | from sans.guitools.plottables import Data1D, Theory1D |
---|
| 108 | |
---|
| 109 | # Generate P(r) for sphere |
---|
| 110 | radius = 60.0 |
---|
| 111 | d_max = 2*radius |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | r = pylab.arange(0.01, d_max, d_max/51.0) |
---|
| 115 | M = len(r) |
---|
| 116 | y = numpy.zeros(M) |
---|
| 117 | pr_err = numpy.zeros(M) |
---|
| 118 | |
---|
| 119 | sum = 0.0 |
---|
| 120 | for j in range(M): |
---|
| 121 | value = self.pr_theory(r[j], radius) |
---|
| 122 | sum += value |
---|
| 123 | y[j] = value |
---|
| 124 | pr_err[j] = math.sqrt(y[j]) |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | y = y/sum*d_max/len(r) |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | # Perform fit |
---|
| 132 | pr = Invertor() |
---|
| 133 | pr.d_max = d_max |
---|
| 134 | pr.alpha = 0 |
---|
| 135 | pr.x = r |
---|
| 136 | pr.y = y |
---|
| 137 | pr.err = pr_err |
---|
| 138 | out, cov = pr.pr_fit() |
---|
| 139 | for i in range(len(out)): |
---|
| 140 | print "%g +- %g" % (out[i], math.sqrt(cov[i][i])) |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | # Show input P(r) |
---|
[b659551] | 144 | new_plot = Data1D(pr.x, pr.y, dy=pr.err) |
---|
[f3d51f6] | 145 | new_plot.name = "P_{obs}(r)" |
---|
| 146 | new_plot.xaxis("\\rm{r}", 'A') |
---|
| 147 | new_plot.yaxis("\\rm{P(r)} ","cm^{-3}") |
---|
| 148 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Pr")) |
---|
| 149 | |
---|
| 150 | # Show P(r) fit |
---|
| 151 | self.show_pr(out, pr) |
---|
| 152 | |
---|
| 153 | # Show I(q) fit |
---|
| 154 | q = pylab.arange(0.001, 0.1, 0.01/51.0) |
---|
| 155 | self.show_iq(out, pr, q) |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | def show_shpere(self, x, radius=70.0, x_range=70.0): |
---|
| 159 | import numpy |
---|
| 160 | import pylab |
---|
| 161 | import math |
---|
| 162 | from sans.guicomm.events import NewPlotEvent |
---|
| 163 | from sans.guitools.plottables import Data1D, Theory1D |
---|
| 164 | # Show P(r) |
---|
| 165 | y_true = numpy.zeros(len(x)) |
---|
| 166 | |
---|
| 167 | sum_true = 0.0 |
---|
| 168 | for i in range(len(x)): |
---|
| 169 | y_true[i] = self.pr_theory(x[i], radius) |
---|
| 170 | sum_true += y_true[i] |
---|
| 171 | |
---|
| 172 | y_true = y_true/sum_true*x_range/len(x) |
---|
| 173 | |
---|
| 174 | # Show the theory P(r) |
---|
| 175 | new_plot = Theory1D(x, y_true) |
---|
| 176 | new_plot.name = "P_{true}(r)" |
---|
| 177 | new_plot.xaxis("\\rm{r}", 'A') |
---|
| 178 | new_plot.yaxis("\\rm{P(r)} ","cm^{-3}") |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | #Put this call in plottables/guitools |
---|
| 182 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Sphere P(r)")) |
---|
| 183 | |
---|
[b659551] | 184 | def get_npts(self): |
---|
| 185 | """ |
---|
| 186 | Returns the number of points in the I(q) data |
---|
| 187 | """ |
---|
| 188 | try: |
---|
| 189 | return len(self.pr.x) |
---|
| 190 | except: |
---|
| 191 | return 0 |
---|
| 192 | |
---|
[f3d51f6] | 193 | def show_iq(self, out, pr, q=None): |
---|
| 194 | import numpy |
---|
| 195 | import pylab |
---|
| 196 | import math |
---|
| 197 | from sans.guicomm.events import NewPlotEvent |
---|
| 198 | from sans.guitools.plottables import Data1D, Theory1D |
---|
| 199 | |
---|
| 200 | qtemp = pr.x |
---|
| 201 | if not q==None: |
---|
| 202 | qtemp = q |
---|
| 203 | |
---|
| 204 | # Make a plot |
---|
| 205 | maxq = -1 |
---|
| 206 | for q_i in qtemp: |
---|
| 207 | if q_i>maxq: |
---|
| 208 | maxq=q_i |
---|
| 209 | |
---|
[634f1cf] | 210 | minq = 0.001 |
---|
| 211 | |
---|
| 212 | # Check for user min/max |
---|
| 213 | if not pr.q_min==None: |
---|
| 214 | minq = pr.q_min |
---|
| 215 | if not pr.q_max==None: |
---|
| 216 | maxq = pr.q_max |
---|
| 217 | |
---|
| 218 | x = pylab.arange(minq, maxq, maxq/301.0) |
---|
[f3d51f6] | 219 | y = numpy.zeros(len(x)) |
---|
| 220 | err = numpy.zeros(len(x)) |
---|
| 221 | for i in range(len(x)): |
---|
| 222 | value = pr.iq(out, x[i]) |
---|
| 223 | y[i] = value |
---|
| 224 | try: |
---|
| 225 | err[i] = math.sqrt(math.fabs(value)) |
---|
| 226 | except: |
---|
| 227 | err[i] = 1.0 |
---|
| 228 | print "Error getting error", value, x[i] |
---|
| 229 | |
---|
| 230 | new_plot = Theory1D(x, y) |
---|
| 231 | new_plot.name = "I_{fit}(q)" |
---|
| 232 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 233 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
| 234 | #new_plot.group_id = "test group" |
---|
[9b85e93] | 235 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="I(q)")) |
---|
[f3d51f6] | 236 | |
---|
| 237 | |
---|
[b659551] | 238 | def _on_pr_npts(self, evt): |
---|
| 239 | """ |
---|
| 240 | Redisplay P(r) with a different number of points |
---|
| 241 | """ |
---|
| 242 | from inversion_panel import PrDistDialog |
---|
| 243 | dialog = PrDistDialog(None, -1) |
---|
| 244 | dialog.set_content(self._pr_npts) |
---|
| 245 | if dialog.ShowModal() == wx.ID_OK: |
---|
| 246 | self._pr_npts= dialog.get_content() |
---|
| 247 | dialog.Destroy() |
---|
| 248 | self.show_pr(self.pr.out, self.pr, self.pr.cov) |
---|
| 249 | else: |
---|
| 250 | dialog.Destroy() |
---|
[f3d51f6] | 251 | |
---|
| 252 | |
---|
| 253 | def show_pr(self, out, pr, cov=None): |
---|
| 254 | import numpy |
---|
| 255 | import pylab |
---|
| 256 | import math |
---|
| 257 | from sans.guicomm.events import NewPlotEvent |
---|
| 258 | from sans.guitools.plottables import Data1D, Theory1D |
---|
| 259 | |
---|
| 260 | # Show P(r) |
---|
[b659551] | 261 | x = pylab.arange(0.0, pr.d_max, pr.d_max/self._pr_npts) |
---|
[f3d51f6] | 262 | |
---|
| 263 | y = numpy.zeros(len(x)) |
---|
| 264 | dy = numpy.zeros(len(x)) |
---|
| 265 | y_true = numpy.zeros(len(x)) |
---|
| 266 | |
---|
| 267 | sum = 0.0 |
---|
[dfb58f8] | 268 | cov2 = numpy.ascontiguousarray(cov) |
---|
| 269 | |
---|
[f3d51f6] | 270 | for i in range(len(x)): |
---|
[dfb58f8] | 271 | if cov2==None: |
---|
[f3d51f6] | 272 | value = pr.pr(out, x[i]) |
---|
| 273 | else: |
---|
[dfb58f8] | 274 | (value, dy[i]) = pr.pr_err(out, cov2, x[i]) |
---|
[660b1e6] | 275 | sum += value*pr.d_max/len(x) |
---|
[f3d51f6] | 276 | y[i] = value |
---|
| 277 | |
---|
[660b1e6] | 278 | y = y/sum |
---|
| 279 | dy = dy/sum |
---|
[f3d51f6] | 280 | |
---|
[dfb58f8] | 281 | if cov2==None: |
---|
[f3d51f6] | 282 | new_plot = Theory1D(x, y) |
---|
| 283 | else: |
---|
| 284 | new_plot = Data1D(x, y, dy=dy) |
---|
| 285 | new_plot.name = "P_{fit}(r)" |
---|
| 286 | new_plot.xaxis("\\rm{r}", 'A') |
---|
| 287 | new_plot.yaxis("\\rm{P(r)} ","cm^{-3}") |
---|
| 288 | |
---|
| 289 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="P(r) fit")) |
---|
| 290 | |
---|
| 291 | return x, pr.d_max |
---|
| 292 | |
---|
| 293 | |
---|
| 294 | def choose_file(self): |
---|
| 295 | """ |
---|
| 296 | |
---|
| 297 | """ |
---|
| 298 | #TODO: this should be in a common module |
---|
| 299 | return self.parent.choose_file() |
---|
| 300 | |
---|
[b659551] | 301 | def load(self, path = "sphere_60_q0_2.txt"): |
---|
[f3d51f6] | 302 | import numpy, math, sys |
---|
| 303 | # Read the data from the data file |
---|
| 304 | data_x = numpy.zeros(0) |
---|
| 305 | data_y = numpy.zeros(0) |
---|
| 306 | data_err = numpy.zeros(0) |
---|
[b659551] | 307 | scale = None |
---|
| 308 | min_err = 0.0 |
---|
[f3d51f6] | 309 | if not path == None: |
---|
| 310 | input_f = open(path,'r') |
---|
| 311 | buff = input_f.read() |
---|
| 312 | lines = buff.split('\n') |
---|
| 313 | for line in lines: |
---|
| 314 | try: |
---|
| 315 | toks = line.split() |
---|
| 316 | x = float(toks[0]) |
---|
| 317 | y = float(toks[1]) |
---|
[b659551] | 318 | if len(toks)>2: |
---|
| 319 | err = float(toks[2]) |
---|
| 320 | else: |
---|
| 321 | if scale==None: |
---|
| 322 | scale = 0.05*math.sqrt(y) |
---|
| 323 | #scale = 0.05/math.sqrt(y) |
---|
| 324 | min_err = 0.01*y |
---|
| 325 | err = scale*math.sqrt(y)+min_err |
---|
| 326 | #err = 0 |
---|
| 327 | |
---|
[4a5de6f] | 328 | data_x = numpy.append(data_x, x) |
---|
| 329 | data_y = numpy.append(data_y, y) |
---|
[b659551] | 330 | data_err = numpy.append(data_err, err) |
---|
[f3d51f6] | 331 | except: |
---|
[b659551] | 332 | pass |
---|
[f3d51f6] | 333 | |
---|
[b659551] | 334 | return data_x, data_y, data_err |
---|
[f3d51f6] | 335 | |
---|
| 336 | def pr_theory(self, r, R): |
---|
| 337 | """ |
---|
| 338 | |
---|
| 339 | """ |
---|
| 340 | if r<=2*R: |
---|
| 341 | return 12.0* ((0.5*r/R)**2) * ((1.0-0.5*r/R)**2) * ( 2.0 + 0.5*r/R ) |
---|
| 342 | else: |
---|
| 343 | return 0.0 |
---|
| 344 | |
---|
[660b1e6] | 345 | def get_context_menu(self, graph=None): |
---|
[f3d51f6] | 346 | """ |
---|
| 347 | Get the context menu items available for P(r) |
---|
[660b1e6] | 348 | @param graph: the Graph object to which we attach the context menu |
---|
[f3d51f6] | 349 | @return: a list of menu items with call-back function |
---|
| 350 | """ |
---|
[660b1e6] | 351 | # Look whether this Graph contains P(r) data |
---|
[aa4b8379] | 352 | #if graph.selected_plottable==IQ_DATA_LABEL: |
---|
[660b1e6] | 353 | for item in graph.plottables: |
---|
[aa4b8379] | 354 | if item.name==PR_FIT_LABEL: |
---|
| 355 | return [["Add P(r) data", "Load a data file and display it on this plot", self._on_add_data], |
---|
[b659551] | 356 | ["Change number of P(r) points", "Change the number of points on the P(r) output", self._on_pr_npts]] |
---|
[aa4b8379] | 357 | |
---|
| 358 | elif item.name==graph.selected_plottable: |
---|
| 359 | return [["Compute P(r)", "Compute P(r) from distribution", self._on_context_inversion]] |
---|
[660b1e6] | 360 | |
---|
[aa4b8379] | 361 | return [] |
---|
[660b1e6] | 362 | |
---|
| 363 | def _on_add_data(self, evt): |
---|
| 364 | """ |
---|
| 365 | Add a data curve to the plot |
---|
| 366 | WARNING: this will be removed once guiframe.plotting has its full functionality |
---|
| 367 | """ |
---|
| 368 | path = self.choose_file() |
---|
| 369 | if path==None: |
---|
| 370 | return |
---|
| 371 | |
---|
| 372 | x, y, err = self.parent.load_ascii_1D(path) |
---|
| 373 | |
---|
[b659551] | 374 | #new_plot = Data1D(x, y, dy=err) |
---|
| 375 | new_plot = Theory1D(x, y) |
---|
[660b1e6] | 376 | new_plot.name = "P_{loaded}(r)" |
---|
| 377 | new_plot.xaxis("\\rm{r}", 'A') |
---|
| 378 | new_plot.yaxis("\\rm{P(r)} ","cm^{-3}") |
---|
| 379 | |
---|
| 380 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="P(r) fit")) |
---|
| 381 | |
---|
| 382 | |
---|
[f3d51f6] | 383 | |
---|
| 384 | def start_thread(self): |
---|
| 385 | from pr_thread import CalcPr |
---|
| 386 | from copy import deepcopy |
---|
| 387 | |
---|
| 388 | # If a thread is already started, stop it |
---|
| 389 | if self.calc_thread != None and self.calc_thread.isrunning(): |
---|
| 390 | self.calc_thread.stop() |
---|
| 391 | |
---|
| 392 | pr = self.pr.clone() |
---|
| 393 | self.calc_thread = CalcPr(pr, self.nfunc, error_func=self._thread_error, completefn=self._completed, updatefn=None) |
---|
| 394 | self.calc_thread.queue() |
---|
| 395 | self.calc_thread.ready(2.5) |
---|
| 396 | |
---|
| 397 | def _thread_error(self, error): |
---|
| 398 | wx.PostEvent(self.parent, StatusEvent(status=error)) |
---|
| 399 | |
---|
[32dffae4] | 400 | def _estimate_completed(self, alpha, message, elapsed): |
---|
[f3d51f6] | 401 | """ |
---|
| 402 | Parameter estimation completed, |
---|
| 403 | display the results to the user |
---|
| 404 | @param alpha: estimated best alpha |
---|
| 405 | @param elapsed: computation time |
---|
| 406 | """ |
---|
| 407 | # Save useful info |
---|
| 408 | self.elapsed = elapsed |
---|
| 409 | self.control_panel.alpha_estimate = alpha |
---|
[32dffae4] | 410 | if not message==None: |
---|
| 411 | wx.PostEvent(self.parent, StatusEvent(status=str(message))) |
---|
[f3d51f6] | 412 | |
---|
| 413 | def _completed(self, out, cov, pr, elapsed): |
---|
| 414 | """ |
---|
| 415 | Method called with the results when the inversion |
---|
| 416 | is done |
---|
| 417 | |
---|
| 418 | @param out: output coefficient for the base functions |
---|
| 419 | @param cov: covariance matrix |
---|
| 420 | @param pr: Invertor instance |
---|
| 421 | @param elapsed: time spent computing |
---|
| 422 | """ |
---|
[dfb58f8] | 423 | from copy import deepcopy |
---|
[f3d51f6] | 424 | # Save useful info |
---|
| 425 | self.elapsed = elapsed |
---|
[b659551] | 426 | # Save Pr invertor |
---|
| 427 | self.pr = pr |
---|
| 428 | |
---|
[f3d51f6] | 429 | message = "Computation completed in %g seconds [chi2=%g]" % (elapsed, pr.chi2) |
---|
| 430 | wx.PostEvent(self.parent, StatusEvent(status=message)) |
---|
| 431 | |
---|
[dfb58f8] | 432 | cov = numpy.ascontiguousarray(cov) |
---|
| 433 | |
---|
[f3d51f6] | 434 | # Show result on control panel |
---|
| 435 | self.control_panel.chi2 = pr.chi2 |
---|
| 436 | self.control_panel.elapsed = elapsed |
---|
| 437 | self.control_panel.oscillation = pr.oscillations(out) |
---|
| 438 | #print "OSCILL", pr.oscillations(out) |
---|
[dfb58f8] | 439 | print "PEAKS:", pr.get_peaks(out) |
---|
| 440 | self.control_panel.positive = pr.get_positive(out) |
---|
| 441 | self.control_panel.pos_err = pr.get_pos_err(out, cov) |
---|
[f3d51f6] | 442 | |
---|
| 443 | for i in range(len(out)): |
---|
| 444 | try: |
---|
| 445 | print "%d: %g +- %g" % (i, out[i], math.sqrt(math.fabs(cov[i][i]))) |
---|
| 446 | except: |
---|
[b659551] | 447 | print sys.exc_value |
---|
[f3d51f6] | 448 | print "%d: %g +- ?" % (i, out[i]) |
---|
| 449 | |
---|
| 450 | # Make a plot of I(q) data |
---|
[aa4b8379] | 451 | if False: |
---|
| 452 | new_plot = Data1D(self.pr.x, self.pr.y, dy=self.pr.err) |
---|
| 453 | new_plot.name = "I_{obs}(q)" |
---|
| 454 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 455 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
| 456 | #new_plot.group_id = "test group" |
---|
| 457 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Iq")) |
---|
[f3d51f6] | 458 | |
---|
| 459 | # Show I(q) fit |
---|
| 460 | self.show_iq(out, self.pr) |
---|
| 461 | |
---|
| 462 | # Show P(r) fit |
---|
[dfb58f8] | 463 | x_values, x_range = self.show_pr(out, self.pr, cov) |
---|
[f3d51f6] | 464 | |
---|
| 465 | # Popup result panel |
---|
| 466 | #result_panel = InversionResults(self.parent, -1, style=wx.RAISED_BORDER) |
---|
| 467 | |
---|
[660b1e6] | 468 | def show_data(self, path=None): |
---|
| 469 | if not path==None: |
---|
| 470 | self._create_file_pr(path) |
---|
| 471 | |
---|
| 472 | # Make a plot of I(q) data |
---|
[b659551] | 473 | new_plot = Data1D(self.pr.x, self.pr.y, dy=self.pr.err) |
---|
[660b1e6] | 474 | new_plot.name = "I_{obs}(q)" |
---|
| 475 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 476 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
[aa4b8379] | 477 | new_plot.interactive = True |
---|
[660b1e6] | 478 | #new_plot.group_id = "test group" |
---|
[9b85e93] | 479 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="I(q)")) |
---|
[660b1e6] | 480 | |
---|
[aa4b8379] | 481 | # Get Q range |
---|
| 482 | self.control_panel.q_min = self.pr.x.min() |
---|
| 483 | self.control_panel.q_max = self.pr.x.max() |
---|
| 484 | |
---|
| 485 | |
---|
[660b1e6] | 486 | |
---|
[634f1cf] | 487 | def setup_plot_inversion(self, alpha, nfunc, d_max, q_min=None, q_max=None): |
---|
[f3d51f6] | 488 | self.alpha = alpha |
---|
| 489 | self.nfunc = nfunc |
---|
| 490 | self.max_length = d_max |
---|
[634f1cf] | 491 | self.q_min = q_min |
---|
| 492 | self.q_max = q_max |
---|
[f3d51f6] | 493 | |
---|
[4a5de6f] | 494 | try: |
---|
| 495 | self._create_plot_pr() |
---|
| 496 | self.perform_inversion() |
---|
| 497 | except: |
---|
| 498 | wx.PostEvent(self.parent, StatusEvent(status=sys.exc_value)) |
---|
[f3d51f6] | 499 | |
---|
[634f1cf] | 500 | def estimate_plot_inversion(self, alpha, nfunc, d_max, q_min=None, q_max=None): |
---|
[f3d51f6] | 501 | self.alpha = alpha |
---|
| 502 | self.nfunc = nfunc |
---|
| 503 | self.max_length = d_max |
---|
[634f1cf] | 504 | self.q_min = q_min |
---|
| 505 | self.q_max = q_max |
---|
[f3d51f6] | 506 | |
---|
[4a5de6f] | 507 | try: |
---|
| 508 | self._create_plot_pr() |
---|
| 509 | self.perform_estimate() |
---|
| 510 | except: |
---|
| 511 | wx.PostEvent(self.parent, StatusEvent(status=sys.exc_value)) |
---|
[f3d51f6] | 512 | |
---|
| 513 | def _create_plot_pr(self): |
---|
| 514 | """ |
---|
| 515 | Create and prepare invertor instance from |
---|
| 516 | a plottable data set. |
---|
| 517 | @param path: path of the file to read in |
---|
| 518 | """ |
---|
| 519 | # Get the data from the chosen data set and perform inversion |
---|
| 520 | pr = Invertor() |
---|
| 521 | pr.d_max = self.max_length |
---|
| 522 | pr.alpha = self.alpha |
---|
[634f1cf] | 523 | pr.q_min = self.q_min |
---|
| 524 | pr.q_max = self.q_max |
---|
[f3d51f6] | 525 | pr.x = self.current_plottable.x |
---|
| 526 | pr.y = self.current_plottable.y |
---|
| 527 | |
---|
| 528 | # Fill in errors if none were provided |
---|
| 529 | if self.current_plottable.dy == None: |
---|
| 530 | print "no error", self.current_plottable.name |
---|
| 531 | y = numpy.zeros(len(pr.y)) |
---|
| 532 | for i in range(len(pr.y)): |
---|
| 533 | y[i] = math.sqrt(pr.y[i]) |
---|
| 534 | pr.err = y |
---|
| 535 | else: |
---|
| 536 | pr.err = self.current_plottable.dy |
---|
| 537 | |
---|
| 538 | self.pr = pr |
---|
[aa4b8379] | 539 | self.iq_data_shown = True |
---|
[f3d51f6] | 540 | |
---|
| 541 | |
---|
[634f1cf] | 542 | def setup_file_inversion(self, alpha, nfunc, d_max, path, q_min=None, q_max=None): |
---|
[f3d51f6] | 543 | self.alpha = alpha |
---|
| 544 | self.nfunc = nfunc |
---|
| 545 | self.max_length = d_max |
---|
[634f1cf] | 546 | self.q_min = q_min |
---|
| 547 | self.q_max = q_max |
---|
[f3d51f6] | 548 | |
---|
[4a5de6f] | 549 | try: |
---|
| 550 | if self._create_file_pr(path): |
---|
| 551 | self.perform_inversion() |
---|
| 552 | except: |
---|
| 553 | wx.PostEvent(self.parent, StatusEvent(status=sys.exc_value)) |
---|
[f3d51f6] | 554 | |
---|
[634f1cf] | 555 | def estimate_file_inversion(self, alpha, nfunc, d_max, path, q_min=None, q_max=None): |
---|
[f3d51f6] | 556 | self.alpha = alpha |
---|
| 557 | self.nfunc = nfunc |
---|
| 558 | self.max_length = d_max |
---|
[634f1cf] | 559 | self.q_min = q_min |
---|
| 560 | self.q_max = q_max |
---|
[f3d51f6] | 561 | |
---|
[4a5de6f] | 562 | try: |
---|
| 563 | if self._create_file_pr(path): |
---|
| 564 | self.perform_estimate() |
---|
| 565 | except: |
---|
| 566 | wx.PostEvent(self.parent, StatusEvent(status=sys.exc_value)) |
---|
| 567 | |
---|
[f3d51f6] | 568 | |
---|
| 569 | def _create_file_pr(self, path): |
---|
| 570 | """ |
---|
| 571 | Create and prepare invertor instance from |
---|
| 572 | a file data set. |
---|
| 573 | @param path: path of the file to read in |
---|
| 574 | """ |
---|
| 575 | # Load data |
---|
| 576 | if os.path.isfile(path): |
---|
| 577 | x, y, err = self.load(path) |
---|
| 578 | |
---|
| 579 | # Get the data from the chosen data set and perform inversion |
---|
| 580 | pr = Invertor() |
---|
| 581 | pr.d_max = self.max_length |
---|
| 582 | pr.alpha = self.alpha |
---|
[634f1cf] | 583 | pr.q_min = self.q_min |
---|
| 584 | pr.q_max = self.q_max |
---|
[f3d51f6] | 585 | pr.x = x |
---|
| 586 | pr.y = y |
---|
| 587 | pr.err = err |
---|
| 588 | |
---|
| 589 | self.pr = pr |
---|
| 590 | return True |
---|
| 591 | return False |
---|
| 592 | |
---|
| 593 | def perform_estimate(self): |
---|
| 594 | from pr_thread import EstimatePr |
---|
| 595 | from copy import deepcopy |
---|
| 596 | |
---|
[32dffae4] | 597 | wx.PostEvent(self.parent, StatusEvent(status='')) |
---|
[f3d51f6] | 598 | # If a thread is already started, stop it |
---|
| 599 | if self.estimation_thread != None and self.estimation_thread.isrunning(): |
---|
| 600 | self.estimation_thread.stop() |
---|
| 601 | |
---|
| 602 | pr = self.pr.clone() |
---|
| 603 | self.estimation_thread = EstimatePr(pr, self.nfunc, error_func=self._thread_error, |
---|
| 604 | completefn = self._estimate_completed, |
---|
| 605 | updatefn = None) |
---|
| 606 | self.estimation_thread.queue() |
---|
| 607 | self.estimation_thread.ready(2.5) |
---|
| 608 | |
---|
| 609 | def perform_inversion(self): |
---|
| 610 | |
---|
| 611 | # Time estimate |
---|
| 612 | #estimated = self.elapsed*self.nfunc**2 |
---|
| 613 | message = "Computation time may take up to %g seconds" % self.elapsed |
---|
| 614 | wx.PostEvent(self.parent, StatusEvent(status=message)) |
---|
| 615 | |
---|
| 616 | # Start inversion thread |
---|
| 617 | self.start_thread() |
---|
| 618 | return |
---|
| 619 | |
---|
| 620 | out, cov = self.pr.lstsq(self.nfunc) |
---|
| 621 | |
---|
| 622 | # Save useful info |
---|
| 623 | self.elapsed = self.pr.elapsed |
---|
| 624 | |
---|
| 625 | for i in range(len(out)): |
---|
| 626 | try: |
---|
| 627 | print "%d: %g +- %g" % (i, out[i], math.sqrt(math.fabs(cov[i][i]))) |
---|
| 628 | except: |
---|
| 629 | print "%d: %g +- ?" % (i, out[i]) |
---|
| 630 | |
---|
| 631 | |
---|
| 632 | |
---|
| 633 | # Make a plot of I(q) data |
---|
| 634 | new_plot = Data1D(self.pr.x, self.pr.y, dy=self.pr.err) |
---|
| 635 | new_plot.name = "I_{obs}(q)" |
---|
| 636 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 637 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
| 638 | wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Iq")) |
---|
| 639 | |
---|
| 640 | # Show I(q) fit |
---|
| 641 | self.show_iq(out, self.pr) |
---|
| 642 | |
---|
| 643 | # Show P(r) fit |
---|
| 644 | x_values, x_range = self.show_pr(out, self.pr, cov=cov) |
---|
| 645 | |
---|
| 646 | |
---|
| 647 | |
---|
| 648 | def _on_context_inversion(self, event): |
---|
| 649 | panel = event.GetEventObject() |
---|
| 650 | |
---|
| 651 | from inversion_panel import InversionDlg |
---|
| 652 | |
---|
| 653 | # If we have more than one displayed plot, make the user choose |
---|
[aa4b8379] | 654 | if len(panel.plots)>1 and panel.graph.selected_plottable in panel.plots: |
---|
| 655 | dataset = panel.graph.selected_plottable |
---|
| 656 | if False: |
---|
| 657 | dialog = InversionDlg(None, -1, "P(r) Inversion", panel.plots, pars=False) |
---|
| 658 | dialog.set_content(self.last_data, self.nfunc, self.alpha, self.max_length) |
---|
| 659 | if dialog.ShowModal() == wx.ID_OK: |
---|
| 660 | dataset = dialog.get_content() |
---|
| 661 | dialog.Destroy() |
---|
| 662 | else: |
---|
| 663 | dialog.Destroy() |
---|
| 664 | return |
---|
[f3d51f6] | 665 | elif len(panel.plots)==1: |
---|
| 666 | dataset = panel.plots.keys()[0] |
---|
| 667 | else: |
---|
| 668 | print "Error: No data is available" |
---|
| 669 | return |
---|
| 670 | |
---|
| 671 | # Store a reference to the current plottable |
---|
[3fd1ebc] | 672 | # If we have a suggested value, use it. |
---|
| 673 | try: |
---|
| 674 | estimate = float(self.control_panel.alpha_estimate) |
---|
| 675 | self.control_panel.alpha = estimate |
---|
| 676 | except: |
---|
| 677 | self.control_panel.alpha = self.alpha |
---|
| 678 | print "No estimate yet" |
---|
| 679 | pass |
---|
| 680 | |
---|
[f3d51f6] | 681 | self.current_plottable = panel.plots[dataset] |
---|
| 682 | self.control_panel.plotname = dataset |
---|
| 683 | self.control_panel.nfunc = self.nfunc |
---|
| 684 | self.control_panel.d_max = self.max_length |
---|
| 685 | self.parent.set_perspective(self.perspective) |
---|
[aa4b8379] | 686 | self.control_panel._on_invert(None) |
---|
[f3d51f6] | 687 | |
---|
| 688 | def get_panels(self, parent): |
---|
| 689 | """ |
---|
| 690 | Create and return a list of panel objects |
---|
| 691 | """ |
---|
| 692 | from inversion_panel import InversionControl |
---|
| 693 | |
---|
| 694 | self.parent = parent |
---|
[aa4b8379] | 695 | self.control_panel = InversionControl(self.parent, -1, |
---|
| 696 | style=wx.RAISED_BORDER, |
---|
| 697 | standalone=self.standalone) |
---|
[f3d51f6] | 698 | self.control_panel.set_manager(self) |
---|
| 699 | self.control_panel.nfunc = self.nfunc |
---|
| 700 | self.control_panel.d_max = self.max_length |
---|
| 701 | self.control_panel.alpha = self.alpha |
---|
| 702 | |
---|
| 703 | self.perspective = [] |
---|
| 704 | self.perspective.append(self.control_panel.window_name) |
---|
[aa4b8379] | 705 | |
---|
| 706 | self.parent.Bind(EVT_PR_FILE, self._on_new_file) |
---|
| 707 | |
---|
[f3d51f6] | 708 | return [self.control_panel] |
---|
| 709 | |
---|
[aa4b8379] | 710 | def _on_new_file(self, evt): |
---|
| 711 | """ |
---|
| 712 | Called when the application manager posted an |
---|
| 713 | EVT_PR_FILE event. Just prompt the control |
---|
| 714 | panel to load a new data file. |
---|
| 715 | """ |
---|
| 716 | self.control_panel._change_file(None) |
---|
| 717 | |
---|
[f3d51f6] | 718 | def get_perspective(self): |
---|
| 719 | """ |
---|
| 720 | Get the list of panel names for this perspective |
---|
| 721 | """ |
---|
| 722 | return self.perspective |
---|
| 723 | |
---|
| 724 | def on_perspective(self, event): |
---|
| 725 | """ |
---|
| 726 | Call back function for the perspective menu item. |
---|
| 727 | We notify the parent window that the perspective |
---|
| 728 | has changed. |
---|
| 729 | """ |
---|
| 730 | self.parent.set_perspective(self.perspective) |
---|
| 731 | |
---|
| 732 | def post_init(self): |
---|
| 733 | """ |
---|
| 734 | Post initialization call back to close the loose ends |
---|
| 735 | [Somehow openGL needs this call] |
---|
| 736 | """ |
---|
[32dffae4] | 737 | self.parent.set_perspective(self.perspective) |
---|
[660b1e6] | 738 | |
---|