Changeset f93dfcb in sasview for sansview/perspectives
- Timestamp:
- Mar 6, 2009 11:19:04 AM (16 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 925a30e
- Parents:
- d72cee0
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/fitting.py
r54905de rf93dfcb 86 86 #menu for model 87 87 menu2 = wx.Menu() 88 89 88 self.menu_mng.populate_menu(menu2, owner) 90 89 id2 = wx.NewId() 91 90 owner.Bind(models.EVT_MODEL,self._on_model_menu) 92 91 ## Allow fitpanel to access list of models 93 92 self.fit_panel.set_owner(owner) 94 93 self.fit_panel.set_model_list(self.menu_mng.get_model_list()) … … 98 97 return [(id, self.menu1, "Fitting"),(id2, menu2, "Model")] 99 98 99 100 100 def on_add_sim_page(self, event): 101 101 """ 102 Create a page to access simultaneous fit option 102 103 """ 103 104 self.sim_page= self.fit_panel.add_sim_page() … … 110 111 TODO: replace the text with a nice image 111 112 """ 112 #from helpDialog import HelpWindow113 #dialog = HelpWindow(None, -1, 'HelpWindow')114 #if dialog.ShowModal() == wx.ID_OK:115 # pass116 #dialog.Destroy()117 113 from helpPanel import HelpWindow 118 114 frame = HelpWindow(None, -1, 'HelpWindow') … … 132 128 "Dialog with fitting parameters ", self._onSelect]] 133 129 else: 134 #if item.name==graph.selected_plottable and\ 135 # item.__class__.__name__ is "Data1D": 130 136 131 if item.name==graph.selected_plottable : 137 132 return [["Select data for Fitting", \ … … 147 142 # Creation of the fit panel 148 143 self.fit_panel = FitPanel(self.parent, -1) 149 #Set the manager for the main panel144 #Set the manager for the main panel 150 145 self.fit_panel.set_manager(self) 151 146 # List of windows used for the perspective … … 160 155 self.parent.Bind( ERR_DATA, self._on_data_error) 161 156 162 #create the fitting panel 163 #return [self.fit_panel] 164 157 #Send the fitting panel to guiframe 165 158 self.mypanels.append(self.fit_panel) 166 159 return self.mypanels … … 169 162 def _on_slicer_event(self, event): 170 163 """ 171 """ 172 #print "fitting:slicer event ", event.panel 164 Receive a panel as event and send it to guiframe 165 @param event: event containing a panel 166 """ 173 167 if event.panel!=None: 174 168 new_panel = event.panel … … 214 208 def copy_data(self, item, dy): 215 209 """ 210 receive a data 1D and the list of errors on dy 211 and create a new data1D data 212 @param return 216 213 """ 217 214 detector=None … … 243 240 for item in self.panel.graph.plottables: 244 241 if item.name == self.panel.graph.selected_plottable: 242 ## reset the error back to data 245 243 if len(self.err_dy)>0: 246 244 if item.name in self.err_dy.iterkeys(): … … 258 256 else: 259 257 data= item 260 261 258 ## create anew page 262 259 if item.name == self.panel.graph.selected_plottable or\ 263 260 item.__class__.__name__ is "Data2D": 264 #find a name for the page created for notebook265 #print "fitting", self.panel266 261 try: 267 268 #page = self.fit_panel.add_fit_page(item)269 262 page = self.fit_panel.add_fit_page(data) 270 #page, model_name = self.fit_panel.add_fit_page(item)271 263 # add data associated to the page created 272 273 264 if page !=None: 274 265 #create a fitproblem storing all link to data,model,page creation 275 266 self.page_finder[page]= FitProblem() 276 #self.page_finder[page].save_model_name(model_name) 267 ## item is almost the same as data but contains 268 ## axis info for plotting 277 269 self.page_finder[page].add_plotted_data(item) 278 270 self.page_finder[page].add_fit_data(data) 279 280 271 281 272 wx.PostEvent(self.parent, StatusEvent(status="Page Created")) 282 273 else: 283 274 wx.PostEvent(self.parent, StatusEvent(status="Page was already Created")) 284 275 except: 285 #raise286 276 wx.PostEvent(self.parent, StatusEvent(status="Creating Fit page: %s"\ 287 277 %sys.exc_value)) … … 291 281 def schedule_for_fit(self,value=0,fitproblem =None): 292 282 """ 293 283 Set the fit problem field to 0 or 1 to schedule that problem to fit. 284 Schedule the specified fitproblem or get the fit problem related to 285 the current page and set value. 286 @param value : integer 0 or 1 287 @param fitproblem: fitproblem to schedule or not to fit 294 288 """ 295 289 if fitproblem !=None: … … 316 310 @note: expecting park used for fit. 317 311 """ 318 #sim_page=self.fit_panel.GetPage(1)319 312 sim_page= self.sim_page 320 313 for page, value in self.page_finder.iteritems(): 321 314 if page != sim_page: 322 315 list=value.get_model() 323 model=list[0] 324 #print "fitting",model.name,modelname, values 316 model = list[0] 325 317 if model.name== modelname: 326 318 value.set_model_param(names,values) … … 354 346 355 347 """ 356 357 #print "single fit ", pars,result.pvec,result.stderr,result.fitness358 348 wx.PostEvent(self.parent, StatusEvent(status="Single fit \ 359 complete " , type="stop"))349 complete! " , type="stop")) 360 350 try: 361 351 for page, value in self.page_finder.iteritems(): 362 352 if page==cpage : 363 #fitdata = value.get_data()364 353 list = value.get_model() 365 354 model= list[0] 366 355 break 356 ## reset the current model with new fitted parameters 367 357 i = 0 368 369 358 for name in pars: 370 359 if result.pvec.__class__==numpy.float64: 371 360 model.setParam(name,result.pvec) 372 361 else: 373 #print "fitting result : chisqr",result.fitness374 #print "fitting result : pvec",result.pvec375 #print "fitting result : stderr",result.stderr376 362 model.setParam(name,result.pvec[i]) 377 #print "fitting: single fit", name, result.pvec[i]378 363 i += 1 379 380 364 ## Reset values of the current page to fit result 381 365 cpage.onsetValues(result.fitness, result.pvec,result.stderr) 382 # title="Fitted model 2D "366 ## plot the current model with new param 383 367 self.plot_helper(currpage=cpage,qmin=qmin,qmax=qmax, 384 368 ymin=ymin, ymax=ymax, 385 369 xmin=xmin, xmax=xmax,title=None) 386 370 except: 387 #raise388 wx.PostEvent(self.parent, StatusEvent(status=" Fitting error: %s" % sys.exc_value))371 msg= "Single Fit completed but Following error occurred:" 372 wx.PostEvent(self.parent, StatusEvent(status="%s %s" % (msg, sys.exc_value))) 389 373 return 390 374 … … 399 383 """ 400 384 if cpage!=None: 385 ## the fit perform was a single fit but selected from the 386 ## simultaneous fit page 401 387 self._single_fit_completed(result=result,pars=pars,cpage=cpage, 402 388 qmin=qmin,qmax=qmax, … … 406 392 wx.PostEvent(self.parent, StatusEvent(status="Simultaneous fit \ 407 393 complete ", type="stop")) 408 #print "result",cpage,str(result),result.parameters,result.fitness,result.stderr 409 394 ## fit more than 1 model at the same time 410 395 try: 411 396 for page, value in self.page_finder.iteritems(): 412 397 if value.get_scheduled()==1: 413 #fitdata = value.get_data()414 398 list = value.get_model() 415 399 model= list[0] … … 427 411 small_cov.append(p.stderr) 428 412 model.setParam(param_name,p.value) 413 429 414 # Display result on each page 430 #print "mmmm",p.value,p.stderr431 415 page.onsetValues(result.fitness, small_out,small_cov) 432 #print "out,err",small_out,small_cov 433 #Replot model 416 #Replot models 417 msg= "Single Fit completed plotting %s:"%model.name 418 wx.PostEvent(self.parent, StatusEvent(status="%s " % msg)) 434 419 self.plot_helper(currpage= page,qmin= qmin,qmax= qmax, 435 420 xmin=xmin, xmax=xmax, 436 421 ymin=ymin, ymax=ymax) 437 422 except: 438 wx.PostEvent(self.parent, StatusEvent(status="Simultaneous Fitting error: %s" % sys.exc_value)) 439 return 440 441 423 msg= "Simultaneous Fit completed but Following error occurred: " 424 wx.PostEvent(self.parent, StatusEvent(status="%s%s" %(msg,sys.exc_value))) 425 return 426 427 442 428 def stop_fit(self): 443 429 """ 430 Stop the fit engine 444 431 """ 445 432 if self.calc_thread != None and self.calc_thread.isrunning(): 446 #self.calc_thread.interrupt()447 433 self.calc_thread.stop() 448 434 wx.PostEvent(self.parent, StatusEvent(status="Fitting \ … … 458 444 459 445 """ 460 #print "in single fitting"461 446 #set an engine to perform fit 462 447 from sans.fit.Fitting import Fit 463 self.fitter = Fit(self._fit_engine)448 self.fitter = Fit(self._fit_engine) 464 449 #Setting an id to store model and data in fit engine 465 self.fit_id = 0450 self.fit_id = 0 466 451 if id!=None: 467 self.fit_id = id468 469 page_fitted =None470 fit_problem =None452 self.fit_id = id 453 454 page_fitted = None 455 fit_problem = None 471 456 #Get information (model , data) related to the page on 472 457 #with the fit will be perform 473 current_pg=self.fit_panel.get_current_page() 474 #simul_pg=self.fit_panel.GetPage(1 ) 475 simul_pg=self.sim_page 458 current_pg= self.fit_panel.get_current_page() 459 simul_pg= self.sim_page 476 460 pars=[] 477 #for page, value in self.page_finder.iteritems(): 478 if current_pg!= simul_pg: 479 # if value.get_scheduled() ==1 : 461 ## Check that the current page is different from self.sim_page 462 if current_pg != simul_pg: 480 463 value = self.page_finder[current_pg] 481 464 metadata = value.get_fit_data() 482 465 list = value.get_model() 483 466 model = list[0] 484 smearer = value.get_smearer()467 smearer = value.get_smearer() 485 468 486 487 469 #Create list of parameters for fitting used 488 489 470 templist=[] 490 471 try: 491 # templist=current_pg.get_param_list()492 templist =current_pg.get_param_list()472 ## get the list of parameter names to fit 473 templist = current_pg.get_param_list() 493 474 for element in templist: 494 475 pars.append(str(element[0].GetLabelText())) 495 476 pars.sort() 477 496 478 #Do the single fit 497 self.fitter.set_model(Model(model), self.fit_id, pars) 498 if self._fit_engine=="scipy": 499 wx.PostEvent(self.parent, StatusEvent(status="Parameters range will\ 500 be ignored for %s fitting engine"%str(self._fit_engine))) 501 #print "args...:",metadata,self.fit_id,smearer,qmin,qmax,ymin,ymax 479 self.fitter.set_model(Model(model), self.fit_id, pars) 502 480 dy=[] 503 481 x=[] 504 482 y=[] 483 ## checking the validity of error 505 484 if metadata.__class__ in ["Data1D","Theory1D"]: 506 485 for i in range(len(metadata.dy)): … … 516 495 metadata.x=numpy.zeros(len(x)) 517 496 metadata.x=x 497 518 498 self.fitter.set_data(data=metadata,Uid=self.fit_id, 519 499 smearer=smearer,qmin= qmin,qmax=qmax, 520 500 ymin=ymin,ymax=ymax) 521 self.fitter.select_problem_for_fit(Uid=self.fit_id,value=value.get_scheduled()) 501 502 self.fitter.select_problem_for_fit(Uid= self.fit_id, 503 value= value.get_scheduled()) 522 504 page_fitted=current_pg 523 #self.fit_id+=1 524 #self.schedule_for_fit( 0,value) 505 525 506 except: 526 wx.PostEvent(self.parent, StatusEvent(status="Single Fit error: %s" % sys.exc_value)) 507 msg= "Single Fit error: %s" % sys.exc_value 508 wx.PostEvent(self.parent, StatusEvent(status= msg )) 527 509 return 528 510 # make sure to keep an alphabetic order 529 511 #of parameter names in the list 530 512 try: 531 # If a thread is already started, stop it513 ## If a thread is already started, stop it 532 514 if self.calc_thread != None and self.calc_thread.isrunning(): 533 515 self.calc_thread.stop() … … 566 548 self.fit_id= id 567 549 568 569 550 for page, value in self.page_finder.iteritems(): 570 551 try: … … 573 554 list = value.get_model() 574 555 model= list[0] 575 # Create dictionary of parameters for fitting used556 ## store page 576 557 cpage= page 577 print ""558 #Get list of parameters name to fit 578 559 pars = [] 579 560 templist = [] … … 586 567 wx.PostEvent(self.parent, StatusEvent(status="Simultaneous Fit error: %s" % sys.exc_value)) 587 568 return 588 # need to check this print "new model "589 569 ## create a park model and reset parameter value if constraint 570 ## is given 590 571 new_model=Model(model) 591 572 param=value.get_model_param() … … 595 576 param_value = item[1] 596 577 param_name = item[0] 597 598 578 new_model.parameterset[ param_name].set( param_value ) 599 579 600 580 self.fitter.set_model(new_model, self.fit_id, pars) 601 # print "sim-->model",metadata,model,self.fit_id, pars581 ## check that non -zero value are send as dy in the fit engine 602 582 dy=[] 603 583 x=[] … … 616 596 metadata.x=numpy.zeros(len(x)) 617 597 metadata.x=x 598 618 599 self.fitter.set_data(metadata,self.fit_id,qmin,qmax,ymin,ymax) 619 #print "sim---->value of problem",value.get_scheduled()620 self.fitter.select_problem_for_fit(Uid=self.fit_id,value=value.get_scheduled())600 self.fitter.select_problem_for_fit(Uid= self.fit_id, 601 value= value.get_scheduled()) 621 602 self.fit_id += 1 622 603 value.clear_model_param() 623 #self.schedule_for_fit( 0,value)604 624 605 except: 625 wx.PostEvent(self.parent, StatusEvent(status="Simultaneous Fit error: %s" % sys.exc_value)) 606 msg= "Simultaneous Fit error: %s" % sys.exc_value 607 wx.PostEvent(self.parent, StatusEvent(status= msg )) 626 608 return 609 627 610 #Do the simultaneous fit 628 611 try: 629 # If a thread is already started, stop it612 ## If a thread is already started, stop it 630 613 if self.calc_thread != None and self.calc_thread.isrunning(): 631 614 self.calc_thread.stop() 615 ## Perform a single fit , then need to page 632 616 if self.fit_id==1: 633 617 self.calc_thread =FitThread(parent =self.parent, … … 643 627 644 628 else: 629 ## Perform more than 1 fit at the time 645 630 self.calc_thread =FitThread(parent =self.parent, 646 631 fn= self.fitter, … … 655 640 656 641 except: 657 wx.PostEvent(self.parent, StatusEvent(status="Simultaneous Fit error: %s" % sys.exc_value)) 642 msg= "Simultaneous Fit error: %s" % sys.exc_value 643 wx.PostEvent(self.parent, StatusEvent(status= msg )) 658 644 return 659 645 … … 661 647 def _onset_engine(self,event): 662 648 """ 663 set engine to scipy 649 Toggle engine name from "park" to "scipy" or reverse if event received 650 @param event: wx.menu item checked 664 651 """ 665 652 if self._fit_engine== 'park': … … 667 654 else: 668 655 self._on_change_engine('park') 669 wx.PostEvent(self.parent, StatusEvent(status="Engine set to: %s" % self._fit_engine)) 656 657 msg= "Engine set to: %s" % self._fit_engine 658 wx.PostEvent(self.parent, StatusEvent(status= msg )) 670 659 671 660 … … 681 670 """ 682 671 react to model selection on any combo box or model menu.plot the model 672 @param evt: wx.combobox event 683 673 """ 684 674 685 675 model = evt.model 686 676 name = evt.name 687 688 #print "name fitting", name 689 #sim_page=self.fit_panel.GetPage(1) 677 690 678 sim_page=self.sim_page 691 679 current_pg = self.fit_panel.get_current_page() 680 ## make sure nothing is done on self.sim_page 681 ## example trying to call set_panel on self.sim_page 692 682 if current_pg != sim_page: 693 683 current_pg.set_panel(model) 694 #model.name = self.page_finder[current_pg].get_name() 695 #print "model name", model.name 684 696 685 model.name="M"+str(self.index_model) 697 686 try: … … 702 691 self.index_model += 1 703 692 704 705 693 # save model name 706 707 694 # save the name containing the data name with the appropriate model 708 695 self.page_finder[current_pg].set_model(model,M_name) 709 696 self.plot_helper(currpage= current_pg,qmin= None,qmax= None) 697 ## draw self.sim_page and store information on the selected model 710 698 if self.sim_page!=None: 711 699 self.sim_page.add_model(self.page_finder) 712 700 701 702 713 703 def set_smearer(self,smearer): 714 704 """ 715 705 Get a smear object and store it to a fit problem 706 @param smearer: smear object to allow smearing data 716 707 """ 717 708 current_pg=self.fit_panel.get_current_page() 718 709 self.page_finder[current_pg].set_smearer(smearer) 710 719 711 720 712 def redraw_model(self,qmin= None,qmax= None): … … 730 722 self.plot_helper(currpage=page,qmin= qmin,qmax= qmax) 731 723 732 def plot_helper(self,currpage, fitModel=None, qmin=None,qmax=None, 724 725 726 def plot_helper(self,currpage, qmin=None,qmax=None, 733 727 ymin=None,ymax=None, xmin=None, xmax=None,title=None ): 734 728 """ … … 740 734 for page in self.page_finder.iterkeys(): 741 735 if page==currpage : 742 data=self.page_finder[page].get_plotted_data() 743 list=self.page_finder[page].get_model() 736 data= self.page_finder[page].get_plotted_data() 737 list= self.page_finder[page].get_model() 738 smearer= self.page_finder[page].get_smearer() 744 739 model=list[0] 745 740 break 746 # print "model in fitting",model741 ## create Model 1D 747 742 if data!=None and data.__class__.__name__ != 'Data2D': 748 theory = Theory1D(x=[], y=[]) 743 x= numpy.zeros(len(data.x)) 744 y= numpy.zeros(len(data.x)) 745 746 theory = Theory1D(x=x, y=y) 749 747 theory.name = model.name 750 748 theory.group_id = data.group_id 751 749 theory.id = "Model" 752 750 751 752 753 753 x_name, x_units = data.get_xaxis() 754 754 y_name, y_units = data.get_yaxis() … … 759 759 if qmax == None : 760 760 qmax = max(data.x) 761 j=0 761 762 try: 762 763 tempx = qmin 763 764 tempy = model.run(qmin) 764 theory.x.append(tempx) 765 theory.y.append(tempy) 765 theory.x[j]=tempx 766 theory.y[j]=tempy 767 j+=1 766 768 except : 767 769 wx.PostEvent(self.parent, StatusEvent(status="fitting \ … … 773 775 tempx = data.x[i] 774 776 tempy = model.run(tempx) 775 theory.x.append(tempx) 776 theory.y.append(tempy) 777 778 if j< i: 779 theory.x[j]=tempx 780 theory.y[j]=tempy 781 j+=1 777 782 778 783 except: … … 782 787 tempx = qmax 783 788 tempy = model.run(qmax) 784 theory.x.append(tempx) 785 theory.y.append(tempy) 789 790 theory.x[j]=tempx 791 theory.y[j]=tempy 792 j+=1 786 793 except: 787 794 wx.PostEvent(self.parent, StatusEvent(status="fitting \ 788 795 skipping point x %g %s" %(qmin, sys.exc_value)) ) 789 796 797 if smearer!=None: 798 theory.y= smearer(theory.y) 790 799 wx.PostEvent(self.parent, NewPlotEvent(plot=theory, 791 800 title=str(data.name))) 792 801 else: 793 802 ## create Model 2D 794 803 theory=Data2D(data.data, data.err_data) 795 804 theory.name= model.name … … 805 814 theory.y_bins= data.y_bins 806 815 tempy=[] 807 #print "max x,y",max(data.xmax,data.xmin),max(data.ymax,data.ymin)816 808 817 if qmin==None: 809 qmin=0 #min(math.fabs(data.xmax),math.fabs(data.ymin))818 qmin=0 810 819 if qmax==None: 811 qmax=math.sqrt(math.pow(max(math.fabs(data.xmax),math.fabs(data.xmin)),2)/ 812 +math.pow(max(math.fabs(data.ymax),math.fabs(data.ymin)),2)) 820 x= math.pow(max(math.fabs(data.xmax),math.fabs(data.xmin)),2) 821 y= math.pow(max(math.fabs(data.ymax),math.fabs(data.ymin)),2) 822 qmax=math.sqrt( x+y ) 823 813 824 if ymin==None: 814 825 ymin=data.ymin … … 823 834 for j in range(len(data.y_bins)): 824 835 for i in range(len(data.x_bins)): 825 tempqij=math.sqrt((math.pow(data.y_bins[j],2)+math.pow(data.x_bins[i],2))) 836 tempqij=math.sqrt(( math.pow(data.y_bins[j],2)\ 837 +math.pow(data.x_bins[i],2) )) 838 826 839 if tempqij>= qmin and tempqij<= qmax: 827 theory.data[j][i]=model.runXY([data.y_bins[j],data.x_bins[i]]) 840 theory.data[j][i] = model.runXY([data.y_bins[j], 841 data.x_bins[i]] ) 828 842 else: 829 theory.data[j][i]=0 #None # Later, We need to decide which of 0 and None is better. 830 #print "len(data.x_bins),len(data.y_bins);",len(data.x_bins),len(data.y_bins),qmax 831 #print "fitting : plot_helper:", theory.image 832 #print "fitting : plot_helper:",theory.image 843 ## Later, We need to decide which of 0 and None is better. 844 theory.data[j][i]=0 845 833 846 theory.detector= data.detector 834 847 theory.source= data.source 835 848 ## qmin, qmax will used later to define q range. 836 849 theory.qmin= qmin 837 850 theory.qmax= qmax 851 ## plot boundaries 838 852 theory.ymin= ymin 839 853 theory.ymax= ymax … … 848 862 """ 849 863 Plot a theory from a model selected from the menu 864 @param evt: wx.menu event 850 865 """ 851 866 name = evt.model.__name__ 852 867 if hasattr(evt.model, "name"): 853 868 name = evt.model.name 854 #print "on model menu", name855 869 model=evt.model() 856 870 description=model.description … … 861 875 self.fit_panel.add_model_page(model,description,name,topmenu=True) 862 876 863 def draw_model(self,model,name ,data=None,description=None,enable1D=True, enable2D=False, 864 qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): 865 """ 866 draw model with default data value 867 """ 877 878 def draw_model(self, model, name, data= None, description= None, 879 enable1D= True, enable2D= False, 880 qmin= DEFAULT_QMIN, qmax= DEFAULT_QMAX, qstep= DEFAULT_NPTS): 881 """ 882 Draw model. 883 @param model: the model to draw 884 @param name: the name of the model to draw 885 @param data: the data on which the model is based to be drawn 886 @param description: model's description 887 @param enable1D: if true enable drawing model 1D 888 @param enable2D: if true enable drawing model 2D 889 @param qmin: Range's minimum value to draw model 890 @param qmax: Range's maximum value to draw model 891 @param qstep: number of step to divide the x and y-axis 892 893 """ 894 ## draw model based on loaded data 868 895 if data !=None: 869 896 self.redraw_model(qmin,qmax) 870 897 return 898 ## draw model 1D with no loaded data 871 899 self._draw_model1D(model,name,model.description, enable1D,qmin,qmax, qstep) 900 ## draw model 2D with no initial data 872 901 self._draw_model2D(model=model, 873 902 description=model.description, … … 878 907 879 908 880 def _draw_model1D(self,model,name,description=None, enable1D=True, 881 qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): 882 """ 883 884 """ 885 886 if enable1D: 887 x= numpy.linspace(start= qmin, 888 stop= qmax, 889 num= qstep, 890 endpoint=True 891 ) 892 xlen= len(x) 893 y = numpy.zeros(xlen) 894 if not enable1D: 895 for i in range(xlen): 896 y[i] = model.run(x[i]) 909 def _draw_model1D(self, model, name, description= None, enable1D= True, 910 qmin= DEFAULT_QMIN, qmax= DEFAULT_QMAX, qstep= DEFAULT_NPTS): 911 """ 912 Draw model 1D between qmin and qmax value 913 @param model: the model to draw 914 @param name: the name of the model 915 @param description: descripion of the model as string 916 @param enable1D: allow to draw model 1D if True else False 917 @param qmin: the minimum value of the range to draw the model 918 @param qmax: the maximum value of the range to draw the model 919 @param qstep: the number of points to draw 920 """ 921 x= numpy.linspace(start= qmin, 922 stop= qmax, 923 num= qstep, 924 endpoint=True 925 ) 926 xlen= len(x) 927 y = numpy.zeros(xlen) 928 929 if enable1D: 930 for i in range(xlen): 931 y[i] = model.run(x[i]) 932 try: 933 new_plot = Theory1D(x, y) 934 new_plot.name = name 935 new_plot.xaxis("\\rm{Q}", 'A^{-1}') 936 new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") 937 new_plot.id ="Model" 938 new_plot.group_id ="Model" 939 # Pass the reset flag to let the plotting event handler 940 # know that we are replacing the whole plot 941 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, 942 title="Analytical model 1D ", reset=True )) 897 943 898 try: 899 new_plot = Theory1D(x, y) 900 901 new_plot.name = name 902 new_plot.xaxis("\\rm{Q}", 'A^{-1}') 903 new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") 904 new_plot.id = "Model" 905 new_plot.group_id ="Model" 906 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title="Analytical model 1D")) 907 except: 908 wx.PostEvent(self.parent, StatusEvent(status="Draw model 1D error: %s" % sys.exc_value)) 909 #raise 910 return 911 else: 912 for i in range(xlen): 913 y[i] = model.run(x[i]) 914 try: 915 new_plot = Theory1D(x, y) 916 #print "draw model 1D", name 917 new_plot.name = name 918 new_plot.xaxis("\\rm{Q}", 'A^{-1}') 919 new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") 920 new_plot.id ="Model" 921 new_plot.group_id ="Model" 922 923 # Pass the reset flag to let the plotting event handler 924 # know that we are replacing the whole plot 925 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, 926 title="Analytical model 1D ", reset=True )) 927 928 except: 929 #raise 930 wx.PostEvent(self.parent, StatusEvent(status="Draw model 1D error: %s" % sys.exc_value)) 931 return 932 933 944 except: 945 msg= " Error occurred when drawing %s Model 1D: "%new_plot.name 946 msg+= " %s"%sys.exc_value 947 wx.PostEvent( self.parent, StatusEvent(status= msg )) 948 return 949 950 934 951 935 952 def update(self, output,time): 936 953 """ 954 Update the output of plotting model 937 955 """ 938 956 pass … … 940 958 def complete(self, output, elapsed, model, qmin, qmax,qstep=DEFAULT_NPTS): 941 959 """ 942 """ 943 wx.PostEvent(self.parent, StatusEvent(status="Calc \ 944 complete !" , type="stop")) 945 #print "complete",output, model,qmin, qmax 960 Complete get the result of modelthread and create model 2D 961 that can be plot. 962 """ 963 msg = "Calc complete !" 964 wx.PostEvent( self.parent, StatusEvent( status= msg , type="stop" )) 965 946 966 data = output 947 967 temp= numpy.zeros(numpy.shape(data)) … … 950 970 951 971 from DataLoader.data_info import Detector, Source 952 953 972 detector = Detector() 954 973 theory.detector=[] 955 974 theory.detector.append(detector) 956 975 957 theory.detector[0].distance=1e+32 #13705.0976 theory.detector[0].distance=1e+32 958 977 theory.source= Source() 959 theory.source.wavelength=2*math.pi/1e+32 #8.4978 theory.source.wavelength=2*math.pi/1e+32 960 979 theory.x_bins =[] 961 980 theory.y_bins =[] 962 #Now qmax is xmax. 963 xmax=2*theory.detector[0].distance*math.atan(qmax/(4*math.pi/theory.source.wavelength)) 964 theory.detector[0].pixel_size.x= xmax/(qstep/2-0.5)#5.0 965 theory.detector[0].pixel_size.y= xmax/(qstep/2-0.5)#5.0 981 ## Create detector for Model 2D 982 xmax=2*theory.detector[0].distance*math.atan(\ 983 qmax/(4*math.pi/theory.source.wavelength)) 984 985 theory.detector[0].pixel_size.x= xmax/(qstep/2-0.5) 986 theory.detector[0].pixel_size.y= xmax/(qstep/2-0.5) 966 987 theory.detector[0].beam_center.x= qmax 967 988 theory.detector[0].beam_center.y= qmax 968 #print "xmax,qmax",xmax,qmax 969 # compute x_bins and y_bins 970 # Qx and Qy vectors 971 989 ## create x_bins and y_bins of the model 2D 972 990 distance = theory.detector[0].distance 973 991 pixel = qstep/2-1 … … 981 999 982 1000 size_x, size_y= numpy.shape(theory.data) 983 #print "size_x,size_y",size_x, size_y984 #This case, q and x are equivalent to each other.985 1001 for i_x in range(size_x): 986 theta = (i_x-center_x)*pixel_width_x / distance #/ size_x#100.01002 theta = (i_x-center_x)*pixel_width_x / distance 987 1003 qx = 4.0*math.pi/wavelength * math.tan(theta/2.0) 988 1004 theory.x_bins.append(qx) 989 1005 for i_y in range(size_y): 990 theta = (i_y-center_y)*pixel_width_y / distance #/ size_y#100.01006 theta = (i_y-center_y)*pixel_width_y / distance 991 1007 qy =4.0*math.pi/wavelength * math.tan(theta/2.0) 992 1008 theory.y_bins.append(qy) 993 1009 994 #theory.err_data= numpy.995 1010 theory.name= model.name 996 1011 theory.group_id ="Model" 997 1012 theory.id ="Model" 998 999 1000 1001 theory.xmin= -qmax#/math.sqrt(2)#You can not do sqrt(2), that changes axis scale not data itself 1002 theory.xmax= qmax#/math.sqrt(2) 1003 theory.ymin= -qmax#/math.sqrt(2) 1004 theory.ymax= qmax#/math.sqrt(2) 1005 1006 #print "model draw comptele xmax",theory.xmax,model.name 1013 ## determine plot boundaries 1014 theory.xmin= -qmax 1015 theory.xmax= qmax 1016 theory.ymin= -qmax 1017 theory.ymax= qmax 1018 ## plot 1007 1019 wx.PostEvent(self.parent, NewPlotEvent(plot=theory, 1008 1020 title="Analytical model 2D ", reset=True )) … … 1012 1024 def _draw_model2D(self,model,description=None, enable2D=False, 1013 1025 qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): 1014 1026 """ 1027 draw model in 2D 1028 @param model: instance of the model to draw 1029 @param description: the description of the model 1030 @param enable2D: when True allows to draw model 2D 1031 @param qmin: the minimum value to draw model 2D 1032 @param qmax: the maximum value to draw model 2D 1033 @param qstep: the number of division of Qx and Qy of the model to draw 1034 1035 """ 1015 1036 x= numpy.linspace(start= -1*qmax, 1016 1037 stop= qmax, … … 1022 1043 endpoint=True ) 1023 1044 1024 lx = len(x)1025 #print x1026 #data=numpy.zeros([len(x),len(y)])1027 1045 self.model= model 1028 1046 if enable2D: 1029 1047 try: 1030 1048 from sans.guiframe.model_thread import Calc2D 1031 # print "in draw model 2d ",self.model1032 1049 self.calc_thread = Calc2D(parent =self.parent,x=x, 1033 1050 y=y,model= self.model, … … 1040 1057 self.calc_thread.ready(2.5) 1041 1058 except: 1042 raise 1059 msg= "Draw model 2D error: " 1060 wx.PostEvent(self.parent, StatusEvent(status="%s %s" % sys.exc_value)) 1061 return 1043 1062 1044 def show_panel2D(self, id=None ): 1045 self.parent.show_panel(self.model2D_id) 1063 1046 1064 1047 1065
Note: See TracChangeset
for help on using the changeset viewer.