Changeset 69363c7 in sasview for src/sas/sascalc/fit
- Timestamp:
- Sep 22, 2017 10:29:48 AM (7 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, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- ab0b93f
- Parents:
- 1386b2f (diff), d76c43a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - git-author:
- Paul Kienzle <pkienzle@…> (09/22/17 10:28:48)
- git-committer:
- Paul Kienzle <pkienzle@…> (09/22/17 10:29:48)
- Location:
- src/sas/sascalc/fit
- Files:
-
- 3 edited
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/fit/models.py
r277257f r69363c7 14 14 import py_compile 15 15 import shutil 16 #?? from copy import copy 16 17 17 18 from sasmodels.sasview_model import load_custom_model, load_standard_models 18 19 from sasmodels.sasview_model import MultiplicationModel 20 #?? from sas.sasgui.perspectives.fitting.fitpage import CUSTOM_MODEL 19 21 20 22 # Explicitly import from the pluginmodel module so that py2exe … … 288 290 # Classify models 289 291 structure_factors = [] 292 form_factors = [] 290 293 multiplicity_models = [] 291 294 for model in self.model_dictionary.values(): … … 293 296 if getattr(model, 'is_structure_factor', False): 294 297 structure_factors.append(model) 298 if getattr(model, 'is_form_factor', False): 299 form_factors.append(model) 295 300 if model.is_multiplicity_model: 296 301 multiplicity_models.append(model) … … 299 304 return { 300 305 "Structure Factors": structure_factors, 306 "Form Factors": form_factors, 301 307 "Plugin Models": plugin_models, 302 308 "Multi-Functions": multiplicity_models, -
src/sas/sascalc/fit/AbstractFitEngine.py
ra1b8fee r50fcb09 137 137 that will smear the theory data (slit smearing or resolution 138 138 smearing) when set. 139 139 140 140 The proper way to set the smearing object would be to 141 141 do the following: :: 142 143 from sas.sascalc. data_util.qsmearing import smear_selection142 143 from sas.sascalc.fit.qsmearing import smear_selection 144 144 smearer = smear_selection(some_data) 145 145 fitdata1d = FitData1D( x= [1,3,..,], … … 147 147 dx=None, 148 148 dy=[1,2...], smearer= smearer) 149 149 150 150 :Note: that some_data _HAS_ to be of 151 151 class DataLoader.data_info.Data1D 152 152 Setting it back to None will turn smearing off. 153 153 154 154 """ 155 155 Data1D.__init__(self, x=x, y=y, dx=dx, dy=dy, lam=lam, dlam=dlam) … … 176 176 ## Max Q-value 177 177 self.qmax = max(self.x) 178 178 179 179 # Range used for input to smearing 180 180 self._qmin_unsmeared = self.qmin … … 184 184 self.idx_unsmeared = (self.x >= self._qmin_unsmeared) \ 185 185 & (self.x <= self._qmax_unsmeared) 186 186 187 187 def set_fit_range(self, qmin=None, qmax=None): 188 188 """ to set the fit range""" … … 199 199 self._qmin_unsmeared = self.qmin 200 200 self._qmax_unsmeared = self.qmax 201 201 202 202 self._first_unsmeared_bin = 0 203 203 self._last_unsmeared_bin = len(self.x) - 1 204 204 205 205 if self.smearer is not None: 206 206 self._first_unsmeared_bin, self._last_unsmeared_bin = \ … … 208 208 self._qmin_unsmeared = self.x[self._first_unsmeared_bin] 209 209 self._qmax_unsmeared = self.x[self._last_unsmeared_bin] 210 210 211 211 # Identify the bin range for the unsmeared and smeared spaces 212 212 self.idx = (self.x >= self.qmin) & (self.x <= self.qmax) … … 231 231 """ 232 232 Compute residuals. 233 233 234 234 If self.smearer has been set, use if to smear 235 235 the data before computing chi squared. 236 236 237 237 :param fn: function that return model value 238 238 239 239 :return: residuals 240 240 """ … … 242 242 fx = np.zeros(len(self.x)) 243 243 fx[self.idx_unsmeared] = fn(self.x[self.idx_unsmeared]) 244 244 245 245 ## Smear theory data 246 246 if self.smearer is not None: … … 253 253 raise RuntimeError, msg 254 254 return (self.y[self.idx] - fx[self.idx]) / self.dy[self.idx], fx[self.idx] 255 255 256 256 def residuals_deriv(self, model, pars=[]): 257 257 """ 258 258 :return: residuals derivatives . 259 260 :note: in this case just return empty array 259 260 :note: in this case just return empty array 261 261 """ 262 262 return [] … … 293 293 x_max = max(math.fabs(sas_data2d.xmin), math.fabs(sas_data2d.xmax)) 294 294 y_max = max(math.fabs(sas_data2d.ymin), math.fabs(sas_data2d.ymax)) 295 295 296 296 ## fitting range 297 297 if qmin is None: … … 305 305 self.res_err_data = copy.deepcopy(self.err_data) 306 306 #self.res_err_data[self.res_err_data==0]=1 307 307 308 308 self.radius = np.sqrt(self.qx_data**2 + self.qy_data**2) 309 309 310 310 # Note: mask = True: for MASK while mask = False for NOT to mask 311 311 self.idx = ((self.qmin <= self.radius) &\ … … 368 368 369 369 return res, gn 370 370 371 371 def residuals_deriv(self, model, pars=[]): 372 372 """ 373 373 :return: residuals derivatives . 374 374 375 375 :note: in this case just return empty array 376 376 377 377 """ 378 378 return [] 379 380 379 380 381 381 class FitAbort(Exception): 382 382 """ … … 396 396 self.fit_arrange_dict = {} 397 397 self.fitter_id = None 398 398 399 399 def set_model(self, model, id, pars=[], constraints=[], data=None): 400 400 """ 401 401 set a model on a given in the fit engine. 402 403 :param model: sas.models type 402 403 :param model: sas.models type 404 404 :param id: is the key of the fitArrange dictionary where model is saved as a value 405 :param pars: the list of parameters to fit 406 :param constraints: list of 405 :param pars: the list of parameters to fit 406 :param constraints: list of 407 407 tuple (name of parameter, value of parameters) 408 408 the value of parameter must be a string to constraint 2 different 409 409 parameters. 410 Example: 410 Example: 411 411 we want to fit 2 model M1 and M2 both have parameters A and B. 412 412 constraints can be ``constraints = [(M1.A, M2.B+2), (M1.B= M2.A *5),...,]`` 413 414 413 414 415 415 :note: pars must contains only name of existing model's parameters 416 416 417 417 """ 418 418 if not pars: … … 445 445 in a FitArrange object and adds that object in a dictionary 446 446 with key id. 447 447 448 448 :param data: data added 449 449 :param id: unique key corresponding to a fitArrange object with data … … 456 456 dx=data.dx, dy=data.dy, smearer=smearer) 457 457 fitdata.sas_data = data 458 458 459 459 fitdata.set_fit_range(qmin=qmin, qmax=qmax) 460 460 #A fitArrange is already created but contains model only at id … … 466 466 fitproblem.add_data(fitdata) 467 467 self.fit_arrange_dict[id] = fitproblem 468 468 469 469 def get_model(self, id): 470 470 """ 471 471 :param id: id is key in the dictionary containing the model to return 472 472 473 473 :return: a model at this id or None if no FitArrange element was 474 474 created with this id … … 478 478 else: 479 479 return None 480 480 481 481 def remove_fit_problem(self, id): 482 482 """remove fitarrange in id""" 483 483 if id in self.fit_arrange_dict: 484 484 del self.fit_arrange_dict[id] 485 485 486 486 def select_problem_for_fit(self, id, value): 487 487 """ 488 488 select a couple of model and data at the id position in dictionary 489 489 and set in self.selected value to value 490 490 491 491 :param value: the value to allow fitting. 492 492 can only have the value one or zero … … 494 494 if id in self.fit_arrange_dict: 495 495 self.fit_arrange_dict[id].set_to_fit(value) 496 496 497 497 def get_problem_to_fit(self, id): 498 498 """ 499 499 return the self.selected value of the fit problem of id 500 500 501 501 :param id: the id of the problem 502 502 """ 503 503 if id in self.fit_arrange_dict: 504 504 self.fit_arrange_dict[id].get_to_fit() 505 506 505 506 507 507 class FitArrange: 508 508 def __init__(self): … … 511 511 to perform the Fit.FitArrange must contain exactly one model 512 512 and at least one data for the fit to be performed. 513 513 514 514 model: the model selected by the user 515 515 Ldata: a list of data what the user wants to fit 516 516 517 517 """ 518 518 self.model = None … … 525 525 """ 526 526 set_model save a copy of the model 527 527 528 528 :param model: the model being set 529 529 """ 530 530 self.model = model 531 531 532 532 def add_data(self, data): 533 533 """ 534 534 add_data fill a self.data_list with data to fit 535 535 536 536 :param data: Data to add in the list 537 537 """ 538 538 if not data in self.data_list: 539 539 self.data_list.append(data) 540 540 541 541 def get_model(self): 542 542 """ … … 544 544 """ 545 545 return self.model 546 546 547 547 def get_data(self): 548 548 """ … … 550 550 """ 551 551 return self.data_list[0] 552 552 553 553 def remove_data(self, data): 554 554 """ 555 555 Remove one element from the list 556 556 557 557 :param data: Data to remove from data_list 558 558 """ 559 559 if data in self.data_list: 560 560 self.data_list.remove(data) 561 561 562 562 def set_to_fit(self, value=0): 563 563 """ 564 564 set self.selected to 0 or 1 for other values raise an exception 565 565 566 566 :param value: integer between 0 or 1 567 567 """ 568 568 self.selected = value 569 569 570 570 def get_to_fit(self): 571 571 """ … … 599 599 if self.model is not None and self.data is not None: 600 600 self.inputs = [(self.model, self.data)] 601 601 602 602 def set_model(self, model): 603 603 """ 604 604 """ 605 605 self.model = model 606 606 607 607 def set_fitness(self, fitness): 608 608 """ 609 609 """ 610 610 self.fitness = fitness 611 611 612 612 def __str__(self): 613 613 """ … … 624 624 msg = [msg1, msg3] + msg2 625 625 return "\n".join(msg) 626 626 627 627 def print_summary(self): 628 628 """ -
src/sas/sascalc/fit/BumpsFitting.py
r9a5097c r1386b2f 15 15 def get_fitter(): 16 16 return FIT_CONFIG.selected_fitter, FIT_CONFIG.selected_values 17 except :17 except ImportError: 18 18 # CRUFT: Bumps changed its handling of fit options around 0.7.5.6 19 19 # Default bumps to use the Levenberg-Marquardt optimizer … … 56 56 header = "=== Steps: %s chisq: %s ETA: %s\n"%(step, chisq, time) 57 57 parameters = ["%15s: %-10.3g%s"%(k,v,("\n" if i%3==2 else " | ")) 58 for i, (k,v) in enumerate(zip(pars,history.point[0]))]58 for i, (k, v) in enumerate(zip(pars, history.point[0]))] 59 59 self.msg = "".join([header]+parameters) 60 60 … … 77 77 self.handler.set_result(Progress(history, self.max_step, self.pars, self.dof)) 78 78 self.handler.progress(history.step[0], self.max_step) 79 if len(history.step) >1 and history.step[1] > history.step[0]:79 if len(history.step) > 1 and history.step[1] > history.step[0]: 80 80 self.handler.improvement() 81 81 self.handler.update_fit() … … 97 97 try: 98 98 p = history.population_values[0] 99 n, p = len(p), np.sort(p)100 QI, Qmid, = int(0.2*n),int(0.5*n)101 self.convergence.append((best, p[0], p[QI],p[Qmid],p[-1-QI],p[-1]))102 except :103 self.convergence.append((best, best, best,best,best,best))99 n, p = len(p), np.sort(p) 100 QI, Qmid = int(0.2*n), int(0.5*n) 101 self.convergence.append((best, p[0], p[QI], p[Qmid], p[-1-QI], p[-1])) 102 except Exception: 103 self.convergence.append((best, best, best, best, best, best)) 104 104 105 105 … … 131 131 132 132 def _reset_pars(self, names, values): 133 for k, v in zip(names, values):133 for k, v in zip(names, values): 134 134 self._pars[k].value = v 135 135 … … 137 137 self._pars = {} 138 138 for k in self.model.getParamList(): 139 name = ".".join((self.name, k))139 name = ".".join((self.name, k)) 140 140 value = self.model.getParam(k) 141 bounds = self.model.details.get(k, ["",None,None])[1:3]141 bounds = self.model.details.get(k, ["", None, None])[1:3] 142 142 self._pars[k] = parameter.Parameter(value=value, bounds=bounds, 143 143 fixed=True, name=name) … … 145 145 146 146 def _init_pars(self, kw): 147 for k, v in kw.items():147 for k, v in kw.items(): 148 148 # dispersion parameters initialized with _field instead of .field 149 if k.endswith('_width'): k = k[:-6]+'.width' 150 elif k.endswith('_npts'): k = k[:-5]+'.npts' 151 elif k.endswith('_nsigmas'): k = k[:-7]+'.nsigmas' 152 elif k.endswith('_type'): k = k[:-5]+'.type' 149 if k.endswith('_width'): 150 k = k[:-6]+'.width' 151 elif k.endswith('_npts'): 152 k = k[:-5]+'.npts' 153 elif k.endswith('_nsigmas'): 154 k = k[:-7]+'.nsigmas' 155 elif k.endswith('_type'): 156 k = k[:-5]+'.type' 153 157 if k not in self._pars: 154 158 formatted_pars = ", ".join(sorted(self._pars.keys())) … … 159 163 elif isinstance(v, parameter.BaseParameter): 160 164 self._pars[k] = v 161 elif isinstance(v, (tuple, list)):165 elif isinstance(v, (tuple, list)): 162 166 low, high = v 163 167 self._pars[k].value = (low+high)/2 164 self._pars[k].range(low, high)168 self._pars[k].range(low, high) 165 169 else: 166 170 self._pars[k].value = v … … 170 174 Flag a set of parameters as fitted parameters. 171 175 """ 172 for k, p in self._pars.items():176 for k, p in self._pars.items(): 173 177 p.fixed = (k not in param_list or k in self.constraints) 174 178 self.fitted_par_names = [k for k in param_list if k not in self.constraints] … … 182 186 183 187 def update(self): 184 for k, v in self._pars.items():188 for k, v in self._pars.items(): 185 189 #print "updating",k,v,v.value 186 self.model.setParam(k, v.value)190 self.model.setParam(k, v.value) 187 191 self._dirty = True 188 192 … … 223 227 symtab = dict((".".join((M.name, k)), p) 224 228 for M in self.models 225 for k, p in M.parameters().items())229 for k, p in M.parameters().items()) 226 230 self.update = compile_constraints(symtab, exprs) 227 231 else: … … 300 304 np.NaN*np.ones(len(fitness.computed_pars)))) 301 305 R.pvec = np.hstack((result['value'][fitted_index], 302 306 [p.value for p in fitness.computed_pars])) 303 307 R.fitness = np.sum(R.residuals**2)/(fitness.numpoints() - len(fitted_index)) 304 308 else: 305 309 R.stderr = np.NaN*np.ones(len(param_list)) 306 R.pvec = np.asarray( 310 R.pvec = np.asarray([p.value for p in fitness.fitted_pars+fitness.computed_pars]) 307 311 R.fitness = np.NaN 308 312 R.convergence = result['convergence'] … … 331 335 steps = options.get('steps', 0) 332 336 if steps == 0: 333 pop = options.get('pop', 0)*len(problem._parameters)337 pop = options.get('pop', 0)*len(problem._parameters) 334 338 samples = options.get('samples', 0) 335 339 steps = (samples+pop-1)/pop if pop != 0 else samples … … 343 347 fitdriver = fitters.FitDriver(fitclass, problem=problem, 344 348 abort_test=abort_test, **options) 345 omp_threads = int(os.environ.get('OMP_NUM_THREADS', '0'))349 omp_threads = int(os.environ.get('OMP_NUM_THREADS', '0')) 346 350 mapper = MPMapper if omp_threads == 1 else SerialMapper 347 351 fitdriver.mapper = mapper.start_mapper(problem, None) … … 359 363 convergence_list = options['monitors'][-1].convergence 360 364 convergence = (2*np.asarray(convergence_list)/problem.dof 361 if convergence_list else np.empty((0, 1),'d'))365 if convergence_list else np.empty((0, 1), 'd')) 362 366 363 367 success = best is not None … … 376 380 'errors': '\n'.join(errors), 377 381 } 378 -
src/sas/sascalc/fit/pagestate.py
rda9b239 r277257f 1 1 """ 2 2 Class that holds a fit page state 3 3 """ 4 4 # TODO: Refactor code so we don't need to use getattr/setattr … … 15 15 import os 16 16 import sys 17 import wx18 17 import copy 19 18 import logging … … 23 22 import xml.dom.minidom 24 23 from xml.dom.minidom import parseString 24 from xml.dom.minidom import getDOMImplementation 25 25 from lxml import etree 26 26 27 27 from sasmodels import convert 28 28 import sasmodels.weights 29 30 from sas.sasview import __version__ as SASVIEW_VERSION 29 31 30 32 import sas.sascalc.dataloader … … 38 40 # Information to read/write state as xml 39 41 FITTING_NODE_NAME = 'fitting_plug_in' 40 CANSAS_NS = "cansas1d/1.0"42 CANSAS_NS = {"ns": "cansas1d/1.0"} 41 43 42 44 CUSTOM_MODEL = 'Plugin Models' … … 72 74 ["cb1", "cb1", "bool"], 73 75 ["tcChi", "tcChi", "float"], 74 ["smearer", "smearer", "float"],75 ["smear_type", "smear_type", "string"],76 76 ["dq_l", "dq_l", "float"], 77 77 ["dq_r", "dq_r", "float"], … … 83 83 ["weights", "weights"]] 84 84 85 DISPERSION_LIST = [["disp_obj_dict", " _disp_obj_dict", "string"]]85 DISPERSION_LIST = [["disp_obj_dict", "disp_obj_dict", "string"]] 86 86 87 87 LIST_OF_STATE_PARAMETERS = [["parameters", "parameters"], … … 142 142 Contains information to reconstruct a page of the fitpanel. 143 143 """ 144 def __init__(self, parent=None,model=None, data=None):144 def __init__(self, model=None, data=None): 145 145 """ 146 146 Initialize the current state … … 154 154 self.timestamp = time.time() 155 155 # Data member to store the dispersion object created 156 self. _disp_obj_dict = {}156 self.disp_obj_dict = {} 157 157 # ------------------------ 158 158 # Data used for fitting … … 190 190 # fit page manager 191 191 self.manager = None 192 # Store the parent of this panel parent193 # For this application fitpanel is the parent194 self.parent = parent195 192 # Event_owner is the owner of model event 196 193 self.event_owner = None … … 211 208 # orientation parameters for gaussian dispersity 212 209 self.orientation_params_disp = [] 213 # smearer info214 self.smearer = None215 self.smear_type = None216 210 self.dq_l = None 217 211 self.dq_r = None … … 231 225 # contains link between a model and selected parameters to fit 232 226 self.param_toFit = [] 233 # dictionary of model type and model class234 self.model_list_box = None235 227 # save the state of the context menu 236 228 self.saved_states = {} … … 277 269 # store value of chisqr 278 270 self.tcChi = None 279 self.version = (1, 0,0)271 self.version = (1, 0, 0) 280 272 281 273 def clone(self): … … 287 279 model = self.model.clone() 288 280 model.name = self.model.name 289 obj = PageState( self.parent,model=model)281 obj = PageState(model=model) 290 282 obj.file = copy.deepcopy(self.file) 291 283 obj.data = copy.deepcopy(self.data) … … 294 286 obj.data_name = self.data_name 295 287 obj.is_data = self.is_data 296 obj.model_list_box = copy.deepcopy(self.model_list_box)297 288 298 289 obj.categorycombobox = self.categorycombobox … … 321 312 obj.tcChi = self.tcChi 322 313 323 if len(self. _disp_obj_dict) > 0:324 for k, v in self. _disp_obj_dict.iteritems():325 obj. _disp_obj_dict[k] = v314 if len(self.disp_obj_dict) > 0: 315 for k, v in self.disp_obj_dict.iteritems(): 316 obj.disp_obj_dict[k] = v 326 317 if len(self.disp_cb_dict) > 0: 327 318 for k, v in self.disp_cb_dict.iteritems(): … … 337 328 obj.pinhole_smearer = copy.deepcopy(self.pinhole_smearer) 338 329 obj.slit_smearer = copy.deepcopy(self.slit_smearer) 339 obj.smear_type = copy.deepcopy(self.smear_type)340 330 obj.dI_noweight = copy.deepcopy(self.dI_noweight) 341 331 obj.dI_didata = copy.deepcopy(self.dI_didata) … … 355 345 obj.npts = copy.deepcopy(self.npts) 356 346 obj.cb1 = copy.deepcopy(self.cb1) 357 obj.smearer = copy.deepcopy(self.smearer)358 347 obj.version = copy.deepcopy(self.version) 359 348 … … 519 508 rep = "\nState name: %s\n" % self.file 520 509 t = time.localtime(self.timestamp) 521 time_str = time.strftime("%b %d %Y %H ;%M;%S ", t)510 time_str = time.strftime("%b %d %Y %H:%M:%S ", t) 522 511 523 512 rep += "State created: %s\n" % time_str … … 559 548 rep += "All parameters checkbox selected: %s\n" % self.cb1 560 549 rep += "Value of Chisqr : %s\n" % str(self.tcChi) 561 rep += "Smear object : %s\n" % self.smearer562 rep += "Smear type : %s\n" % self.smear_type563 550 rep += "dq_l : %s\n" % self.dq_l 564 551 rep += "dq_r : %s\n" % self.dq_r … … 596 583 return rep 597 584 598 def set_report_string(self):585 def _get_report_string(self): 599 586 """ 600 587 Get the values (strings) from __str__ for report … … 611 598 q_range = "" 612 599 strings = self.__repr__() 600 fixed_parameter = False 613 601 lines = strings.split('\n') 614 615 602 # get all string values from __str__() 616 603 for line in lines: 617 value = "" 618 content = line.split(":") 619 if line == '' or len(content) == 1: 604 # Skip lines which are not key: value pairs, which includes 605 # blank lines and freeform notes in SASNotes fields. 606 if not ':' in line: 607 #msg = "Report string expected 'name: value' but got %r" % line 608 #logger.error(msg) 620 609 continue 621 name = content[0] 622 try: 623 value = content[1] 624 except Exception: 625 msg = "Report string expected 'name: value' but got %r" % line 626 logger.error(msg) 627 if name.count("State created"): 628 repo_time = "" + value 629 if name.count("parameter name"): 610 611 name, value = [s.strip() for s in line.split(":", 1)] 612 if name == "State created": 613 repo_time = value 614 elif name == "parameter name": 630 615 val_name = value.split(".") 631 616 if len(val_name) > 1: … … 636 621 else: 637 622 param_string += value + ',' 638 if name == "value":623 elif name == "value": 639 624 param_string += value + ',' 640 fixed_parameter = False 641 if name == "selected": 642 if value == u' False': 643 fixed_parameter = True 644 if name == "error value": 625 elif name == "selected": 626 # remember if it is fixed when reporting error value 627 fixed_parameter = (value == u'False') 628 elif name == "error value": 645 629 if fixed_parameter: 646 630 param_string += '(fixed),' 647 631 else: 648 632 param_string += value + ',' 649 if name == "parameter unit":633 elif name == "parameter unit": 650 634 param_string += value + ':' 651 if name == "Value of Chisqr":635 elif name == "Value of Chisqr": 652 636 chi2 = ("Chi2/Npts = " + value) 653 637 chi2_string = CENTRE % chi2 654 if name == "Title":638 elif name == "Title": 655 639 if len(value.strip()) == 0: 656 640 continue 657 641 title = value + " [" + repo_time + "]" 658 642 title_name = HEADER % title 659 if name == "data":643 elif name == "data": 660 644 try: 661 file_value = ("File name:" + content[2]) 645 # parsing "data : File: filename [mmm dd hh:mm]" 646 name = value.split(':', 1)[1].strip() 647 file_value = "File name:" + name 662 648 file_name = CENTRE % file_value 663 649 if len(title) == 0: 664 title = content[2]+ " [" + repo_time + "]"650 title = name + " [" + repo_time + "]" 665 651 title_name = HEADER % title 666 652 except Exception: 667 653 msg = "While parsing 'data: ...'\n" 668 654 logger.error(msg + traceback.format_exc()) 669 if name == "model name":655 elif name == "model name": 670 656 try: 671 modelname = "Model name:" + content[1]672 except :657 modelname = "Model name:" + value 658 except Exception: 673 659 modelname = "Model name:" + " NAN" 674 660 model_name = CENTRE % modelname 675 661 676 if name == "Plotting Range":662 elif name == "Plotting Range": 677 663 try: 678 q_range = content[1] + " = " + content[2] \ 679 + " = " + content[3].split(",")[0] 664 parts = value.split(':') 665 q_range = parts[0] + " = " + parts[1] \ 666 + " = " + parts[2].split(",")[0] 680 667 q_name = ("Q Range: " + q_range) 681 668 q_range = CENTRE % q_name … … 683 670 msg = "While parsing 'Plotting Range: ...'\n" 684 671 logger.error(msg + traceback.format_exc()) 672 685 673 paramval = "" 686 674 for lines in param_string.split(":"): … … 711 699 "\n" + paramval_string + \ 712 700 "\n" + ELINE + \ 713 "\n" + FEET_1 % title + \ 714 "\n" + FEET_2 701 "\n" + FEET_1 % title 715 702 716 703 return html_string, text_string, title … … 725 712 return name 726 713 727 def report(self, fig s=None, canvases=None):714 def report(self, fig_urls): 728 715 """ 729 716 Invoke report dialog panel … … 731 718 : param figs: list of pylab figures [list] 732 719 """ 733 from sas.sasgui.perspectives.fitting.report_dialog import ReportDialog734 720 # get the strings for report 735 html_str, text_str, title = self. set_report_string()721 html_str, text_str, title = self._get_report_string() 736 722 # Allow 2 figures to append 737 if len(figs) == 1: 738 add_str = FEET_3 739 elif len(figs) == 2: 740 add_str = ELINE 741 add_str += FEET_2 % ("%s") 742 add_str += ELINE 743 add_str += FEET_3 744 elif len(figs) > 2: 745 add_str = ELINE 746 add_str += FEET_2 % ("%s") 747 add_str += ELINE 748 add_str += FEET_2 % ("%s") 749 add_str += ELINE 750 add_str += FEET_3 751 else: 752 add_str = "" 723 image_links = [FEET_2%fig for fig in fig_urls] 753 724 754 725 # final report html strings 755 report_str = html_str % ("%s") + add_str 756 757 # make plot image 758 images = self.set_plot_state(figs, canvases) 759 report_list = [report_str, text_str, images] 760 dialog = ReportDialog(report_list, None, wx.ID_ANY, "") 761 dialog.Show() 726 report_str = html_str + ELINE.join(image_links) 727 728 return report_str, text_str 762 729 763 730 def _to_xml_helper(self, thelist, element, newdoc): … … 794 761 :param batch_fit_state: simultaneous fit state 795 762 """ 796 from xml.dom.minidom import getDOMImplementation797 798 763 # Check whether we have to write a standalone XML file 799 764 if doc is None: … … 807 772 try: 808 773 top_element = newdoc.createElement(FITTING_NODE_NAME) 809 except :774 except Exception: 810 775 string = etree.tostring(doc, pretty_print=True) 811 776 newdoc = parseString(string) … … 816 781 try: 817 782 entry_node.appendChild(top_element) 818 except :783 except Exception: 819 784 node_name = entry_node.tag 820 785 node_list = newdoc.getElementsByTagName(node_name) … … 823 788 824 789 attr = newdoc.createAttribute("version") 825 from sas import sasview 826 attr.nodeValue = sasview.__version__ 790 attr.nodeValue = SASVIEW_VERSION 827 791 # attr.nodeValue = '1.0' 828 792 top_element.setAttributeNode(attr) … … 880 844 inputs.appendChild(element) 881 845 882 # Create doc for the dictionary of self. _disp_obj_dic846 # Create doc for the dictionary of self.disp_obj_dic 883 847 for tagname, varname, tagtype in DISPERSION_LIST: 884 848 element = newdoc.createElement(tagname) … … 961 925 """ 962 926 for item in node: 963 try: 964 name = item.get('name') 965 except: 966 name = None 967 try: 968 value = item.get('value') 969 except: 970 value = None 971 try: 972 selected_to_fit = (item.get('selected_to_fit') == "True") 973 except: 974 selected_to_fit = None 975 try: 976 error_displayed = (item.get('error_displayed') == "True") 977 except: 978 error_displayed = None 979 try: 980 error_value = item.get('error_value') 981 except: 982 error_value = None 983 try: 984 minimum_displayed = (item.get('minimum_displayed') == "True") 985 except: 986 minimum_displayed = None 987 try: 988 minimum_value = item.get('minimum_value') 989 except: 990 minimum_value = None 991 try: 992 maximum_displayed = (item.get('maximum_displayed') == "True") 993 except: 994 maximum_displayed = None 995 try: 996 maximum_value = item.get('maximum_value') 997 except: 998 maximum_value = None 999 try: 1000 unit = item.get('unit') 1001 except: 1002 unit = None 927 name = item.get('name') 928 value = item.get('value') 929 selected_to_fit = (item.get('selected_to_fit') == "True") 930 error_displayed = (item.get('error_displayed') == "True") 931 error_value = item.get('error_value') 932 minimum_displayed = (item.get('minimum_displayed') == "True") 933 minimum_value = item.get('minimum_value') 934 maximum_displayed = (item.get('maximum_displayed') == "True") 935 maximum_value = item.get('maximum_value') 936 unit = item.get('unit') 1003 937 list.append([selected_to_fit, name, value, "+/-", 1004 938 [error_displayed, error_value], … … 1030 964 # Get file name 1031 965 entry = get_content('ns:filename', node) 1032 if entry is not None :966 if entry is not None and entry.text: 1033 967 self.file = entry.text.strip() 968 else: 969 self.file = '' 1034 970 1035 971 # Get time stamp … … 1038 974 try: 1039 975 self.timestamp = float(entry.get('epoch')) 1040 except :976 except Exception: 1041 977 msg = "PageState.fromXML: Could not" 1042 978 msg += " read timestamp\n %s" % sys.exc_value … … 1066 1002 list=getattr(self, item[1])) 1067 1003 1068 # Recover _disp_obj_dict from xml file1069 self. _disp_obj_dict = {}1004 # Recover disp_obj_dict from xml file 1005 self.disp_obj_dict = {} 1070 1006 for tagname, varname, tagtype in DISPERSION_LIST: 1071 1007 node = get_content("ns:%s" % tagname, entry) … … 1081 1017 except Exception: 1082 1018 base = "unable to load distribution %r for %s" 1083 logger.error(base % (value, parameter))1019 logger.error(base, value, parameter) 1084 1020 continue 1085 _disp_obj_dict = getattr(self, varname)1086 _disp_obj_dict[parameter] = value1021 disp_obj_dict = getattr(self, varname) 1022 disp_obj_dict[parameter] = value 1087 1023 1088 1024 # get self.values and self.weights dic. if exists … … 1107 1043 setattr(self, varname, dic) 1108 1044 1109 def set_plot_state(self, figs, canvases): 1110 """ 1111 Build image state that wx.html understand 1112 by plotting, putting it into wx.FileSystem image object 1113 1114 """ 1115 images = [] 1116 1117 # Reset memory 1118 self.imgRAM = None 1119 wx.MemoryFSHandler() 1120 1121 # For no figures in the list, prepare empty plot 1122 if figs is None or len(figs) == 0: 1123 figs = [None] 1124 1125 # Loop over the list of figures 1126 # use wx.MemoryFSHandler 1127 self.imgRAM = wx.MemoryFSHandler() 1128 for fig in figs: 1129 if fig is not None: 1130 ind = figs.index(fig) 1131 canvas = canvases[ind] 1132 1133 # store the image in wx.FileSystem Object 1134 wx.FileSystem.AddHandler(wx.MemoryFSHandler()) 1135 1136 # index of the fig 1137 ind = figs.index(fig) 1138 1139 # AddFile, image can be retrieved with 'memory:filename' 1140 self.imgRAM.AddFile('img_fit%s.png' % ind, 1141 canvas.bitmap, wx.BITMAP_TYPE_PNG) 1142 1143 # append figs 1144 images.append(fig) 1145 1146 return images 1147 1045 class SimFitPageState(object): 1046 """ 1047 State of the simultaneous fit page for saving purposes 1048 """ 1049 1050 def __init__(self): 1051 # Sim Fit Page Number 1052 self.fit_page_no = None 1053 # Select all data 1054 self.select_all = False 1055 # Data sets sent to fit page 1056 self.model_list = [] 1057 # Data sets to be fit 1058 self.model_to_fit = [] 1059 # Number of constraints 1060 self.no_constraint = 0 1061 # Dictionary of constraints 1062 self.constraint_dict = {} 1063 # List of constraints 1064 self.constraints_list = [] 1065 1066 def __repr__(self): 1067 # TODO: should use __str__, not __repr__ (similarly for PageState) 1068 # TODO: could use a nicer representation 1069 repr = """\ 1070 fit page number : %(fit_page_no)s 1071 select all : %(select_all)s 1072 model_list : %(model_list)s 1073 model to fit : %(model_to_fit)s 1074 number of construsts : %(no_constraint)s 1075 constraint dict : %(constraint_dict)s 1076 constraints list : %(constraints_list)s 1077 """%self.__dict__ 1078 return repr 1148 1079 1149 1080 class Reader(CansasReader): … … 1204 1135 try: 1205 1136 nodes = entry.xpath('ns:%s' % FITTING_NODE_NAME, 1206 namespaces= {'ns': CANSAS_NS})1137 namespaces=CANSAS_NS) 1207 1138 if nodes: 1208 1139 # Create an empty state … … 1210 1141 state.from_xml(node=nodes[0]) 1211 1142 1212 except :1143 except Exception: 1213 1144 logger.info("XML document does not contain fitting information.\n" 1214 1145 + traceback.format_exc()) 1215 1146 1216 1147 return state … … 1223 1154 """ 1224 1155 nodes = entry.xpath('ns:%s' % FITTING_NODE_NAME, 1225 namespaces= {'ns': CANSAS_NS})1156 namespaces=CANSAS_NS) 1226 1157 if nodes: 1227 1158 simfitstate = nodes[0].xpath('ns:simultaneous_fit', 1228 namespaces= {'ns': CANSAS_NS})1159 namespaces=CANSAS_NS) 1229 1160 if simfitstate: 1230 from simfitpage import SimFitPageState1231 1161 sim_fit_state = SimFitPageState() 1232 1162 simfitstate_0 = simfitstate[0] 1233 1163 all = simfitstate_0.xpath('ns:select_all', 1234 namespaces= {'ns': CANSAS_NS})1164 namespaces=CANSAS_NS) 1235 1165 atts = all[0].attrib 1236 1166 checked = atts.get('checked') 1237 1167 sim_fit_state.select_all = bool(checked) 1238 1168 model_list = simfitstate_0.xpath('ns:model_list', 1239 namespaces= {'ns': CANSAS_NS})1169 namespaces=CANSAS_NS) 1240 1170 model_list_items = model_list[0].xpath('ns:model_list_item', 1241 namespaces={'ns': 1242 CANSAS_NS}) 1171 namespaces=CANSAS_NS) 1243 1172 for model in model_list_items: 1244 1173 attrs = model.attrib … … 1246 1175 1247 1176 constraints = simfitstate_0.xpath('ns:constraints', 1248 namespaces={'ns': CANSAS_NS})1177 namespaces=CANSAS_NS) 1249 1178 constraint_list = constraints[0].xpath('ns:constraint', 1250 namespaces={'ns': CANSAS_NS})1179 namespaces=CANSAS_NS) 1251 1180 for constraint in constraint_list: 1252 1181 attrs = constraint.attrib … … 1266 1195 1267 1196 """ 1268 node = dom.xpath('ns:data_class', namespaces= {'ns': CANSAS_NS})1197 node = dom.xpath('ns:data_class', namespaces=CANSAS_NS) 1269 1198 return_value, _ = self._parse_entry(dom) 1270 1199 return return_value, _ … … 1295 1224 root = tree.getroot() 1296 1225 entry_list = root.xpath('ns:SASentry', 1297 namespaces= {'ns': CANSAS_NS})1226 namespaces=CANSAS_NS) 1298 1227 for entry in entry_list: 1299 1228 try: … … 1319 1248 return None 1320 1249 else: 1321 for ind in range(len(output)):1250 for data in output: 1322 1251 # Call back to post the new state 1323 state = output[ind].meta_data['fitstate']1252 state = data.meta_data['fitstate'] 1324 1253 t = time.localtime(state.timestamp) 1325 1254 time_str = time.strftime("%b %d %H:%M", t) … … 1332 1261 1333 1262 if state is not None and state.is_data is not None: 1334 output[ind].is_data = state.is_data1335 1336 output[ind].filename = state.file1337 state.data = output[ind]1338 state.data.name = output[ind].filename # state.data_name1263 data.is_data = state.is_data 1264 1265 data.filename = state.file 1266 state.data = data 1267 state.data.name = data.filename # state.data_name 1339 1268 state.data.id = state.data_id 1340 1269 if state.is_data is not None: 1341 1270 state.data.is_data = state.is_data 1342 if output[ind].run_name is not None\1343 and len(output[ind].run_name) != 0:1344 if isinstance(output[ind].run_name, dict):1345 name = output[ind].run_name.keys()[0]1271 if data.run_name is not None and len(data.run_name) != 0: 1272 if isinstance(data.run_name, dict): 1273 # Note: key order in dict is not guaranteed, so sort 1274 name = data.run_name.keys()[0] 1346 1275 else: 1347 name = output[ind].run_name1276 name = data.run_name 1348 1277 else: 1349 1278 name = original_fname … … 1351 1280 state.version = fitstate.version 1352 1281 # store state in fitting 1353 self.call_back(state=state, 1354 datainfo=output[ind], format=ext) 1282 self.call_back(state=state, datainfo=data, format=ext) 1355 1283 self.state = state 1356 1284 simfitstate = self._parse_simfit_state(entry) … … 1412 1340 return doc 1413 1341 1414 # Simple html report templ et1342 # Simple html report template 1415 1343 HEADER = "<html>\n" 1416 1344 HEADER += "<head>\n" … … 1440 1368 """ 1441 1369 FEET_2 = \ 1442 """ 1443 <img src="%s" > 1444 </img> 1370 """<img src="%s" ></img> 1445 1371 """ 1446 1372 FEET_3 = \ 1447 """ 1448 </center> 1373 """</center> 1449 1374 </div> 1450 1375 </body> 1451 1376 </html> 1452 1377 """ 1453 ELINE = "<p class=MsoNormal> </p>" 1378 ELINE = """<p class=MsoNormal> </p> 1379 """ -
src/sas/sascalc/fit/qsmearing.py
r8938502 r50fcb09 5 5 #This software was developed by the University of Tennessee as part of the 6 6 #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 7 #project funded by the US National Science Foundation. 7 #project funded by the US National Science Foundation. 8 8 #See the license text in license.txt 9 9 #copyright 2008, University of Tennessee … … 19 19 from sasmodels.sesans import SesansTransform 20 20 from sasmodels.resolution2d import Pinhole2D 21 from .nxsunit import Converter 21 22 from sas.sascalc.data_util.nxsunit import Converter 22 23 23 24 def smear_selection(data, model = None):
Note: See TracChangeset
for help on using the changeset viewer.