- Timestamp:
- Sep 1, 2017 4:42:57 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, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- c351bf1
- Parents:
- f9ba422
- git-author:
- Lewis O'Driscoll <lewis.o'driscoll@…> (08/30/17 10:32:35)
- git-committer:
- Lewis O'Driscoll <lewis.o'driscoll@…> (09/01/17 04:42:57)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/perspectives/calculator/model_editor.py
r07ec714 r9d80623 106 106 self.model2_string = "cylinder" 107 107 self.name = 'Sum' + M_NAME 108 self.factor = 'scale_factor'109 108 self._notes = '' 110 109 self._operator = '+' … … 133 132 self.model2_name = str(self.model2.GetValue()) 134 133 self.good_name = True 135 self.fill_op rator_combox()134 self.fill_operator_combox() 136 135 137 136 def _layout_name(self): … … 491 490 a sum or multiply model then create the appropriate string 492 491 """ 493 494 492 name = '' 495 496 493 if operator == '*': 497 494 name = 'Multi' 498 factor = 'BackGround' 499 f_oper = '+' 495 factor = 'background' 500 496 else: 501 497 name = 'Sum' 502 498 factor = 'scale_factor' 503 f_oper = '*' 504 505 self.factor = factor 499 506 500 self._operator = operator 507 self.explanation = " Plugin Model = %s %s (model1 %s model2)\n" % \508 (self.factor, f_oper, self._operator)501 self.explanation = (" Plugin_model = scale_factor * (model_1 {} " 502 "model_2) + background").format(operator) 509 503 self.explanationctr.SetLabel(self.explanation) 510 504 self.name = name + M_NAME 511 505 512 506 513 def fill_op rator_combox(self):507 def fill_operator_combox(self): 514 508 """ 515 509 fill the current combobox with the operator … … 535 529 if description == '': 536 530 description = name1 + self._operator + name2 537 text = self._operator_choice.GetValue() 538 if text.count('+') > 0: 539 factor = 'scale_factor' 540 f_oper = '*' 541 default_val = '1.0' 542 else: 543 factor = 'BackGround' 544 f_oper = '+' 545 default_val = '0.0' 531 operator_text = self._operator_choice.GetValue() 532 f_oper = '*' if '+' in operator_text else '+' 546 533 path = self.fname 547 try: 548 out_f = open(path, 'w') 549 except: 550 raise 551 lines = SUM_TEMPLATE.split('\n') 552 for line in lines: 553 try: 554 if line.count("scale_factor"): 555 line = line.replace('scale_factor', factor) 556 #print "scale_factor", line 557 if line.count("= %s"): 558 out_f.write(line % (default_val) + "\n") 559 elif line.count("import Model as P1"): 560 if self.is_p1_custom: 561 line = line.replace('#', '') 562 out_f.write(line % name1 + "\n") 563 else: 564 out_f.write(line + "\n") 565 elif line.count("import %s as P1"): 566 if not self.is_p1_custom: 567 line = line.replace('#', '') 568 out_f.write(line % (name1) + "\n") 569 else: 570 out_f.write(line + "\n") 571 elif line.count("import Model as P2"): 572 if self.is_p2_custom: 573 line = line.replace('#', '') 574 out_f.write(line % name2 + "\n") 575 else: 576 out_f.write(line + "\n") 577 elif line.count("import %s as P2"): 578 if not self.is_p2_custom: 579 line = line.replace('#', '') 580 out_f.write(line % (name2) + "\n") 581 else: 582 out_f.write(line + "\n") 583 elif line.count("P1 = find_model"): 584 out_f.write(line % (name1) + "\n") 585 elif line.count("P2 = find_model"): 586 out_f.write(line % (name2) + "\n") 587 588 elif line.count("self.description = '%s'"): 589 out_f.write(line % description + "\n") 590 #elif line.count("run") and line.count("%s"): 591 # out_f.write(line % self._operator + "\n") 592 #elif line.count("evalDistribution") and line.count("%s"): 593 # out_f.write(line % self._operator + "\n") 594 elif line.count("return") and line.count("%s") == 2: 595 #print "line return", line 596 out_f.write(line % (f_oper, self._operator) + "\n") 597 elif line.count("out2")and line.count("%s"): 598 out_f.write(line % self._operator + "\n") 599 else: 600 out_f.write(line + "\n") 601 except: 602 raise 603 out_f.close() 604 #else: 605 # msg = "Name exists already." 534 output = SUM_TEMPLATE.format(model1=name1, model2=name2, 535 scale_factor_default=1.0, background_default=0.001, 536 factor_operator=f_oper, operator=self._operator, 537 description=description) 538 if self._operator == '*': 539 # Multiplication models only have 1 overall scale factor. Don't use 540 # sub-models' individual scales as fitting params 541 output = output.replace("if name == 'background'", 542 "if name == 'background' or name == 'scale'") 543 with open(self.fname, 'w') as out_f: 544 out_f.write(output + "\n") 606 545 607 546 def compile_file(self, path): … … 1294 1233 def __init__(self, multiplicity=1): 1295 1234 Model1DPlugin.__init__(self, name='') 1296 P1 = find_model(' %s')1297 P2 = find_model(' %s')1235 P1 = find_model('{model1}') 1236 P2 = find_model('{model2}') 1298 1237 p_model1 = P1() 1299 1238 p_model2 = P2() 1300 1239 ## Setting model name model description 1301 self.description = ' %s'1240 self.description = '{description}' 1302 1241 if self.name.rstrip().lstrip() == '': 1303 1242 self.name = self._get_name(p_model1.name, p_model2.name) … … 1311 1250 1312 1251 ## Parameter details [units, min, max] 1313 self.details = { }1252 self.details = {{}} 1314 1253 ## Magnetic Panrameters 1315 1254 self.magnetic_params = [] … … 1328 1267 self._set_params() 1329 1268 ## New parameter:scaling_factor 1330 self.params['scale_factor'] = %s 1269 self.params['scale_factor'] = {scale_factor_default} 1270 # Set each model's background to 0, and define our own background param 1271 if 'background' in self.p_model1.params: 1272 self.p_model1.setParam('background', 0.0) 1273 if 'background' in self.p_model2.params: 1274 self.p_model2.setParam('background', 0.0) 1275 self.params['background'] = {background_default} 1331 1276 1332 1277 ## Parameter details [units, min, max] 1333 1278 self._set_details() 1334 1279 self.details['scale_factor'] = ['', 0.0, numpy.inf] 1335 1280 self.details['background'] = ['1/cm', 0.0, numpy.inf] 1336 1281 1337 1282 #list of parameter that can be fitted … … 1434 1379 def _set_params(self): 1435 1380 for name , value in self.p_model1.params.iteritems(): 1436 # No 2D-supported 1437 #if name not in self.p_model1.orientation_params: 1381 # Don't use the model's background param - we've defined our own 1382 if name == 'background': 1383 continue 1438 1384 new_name = "p1_" + name 1439 self.params[new_name] = value1385 self.params[new_name] = value 1440 1386 1441 1387 for name , value in self.p_model2.params.iteritems(): 1442 # No 2D-supported 1443 #if name not in self.p_model2.orientation_params: 1388 # Don't use the model's background param - we've defined our own 1389 if name == 'background': 1390 continue 1444 1391 new_name = "p2_" + name 1445 self.params[new_name] = value1392 self.params[new_name] = value 1446 1393 1447 1394 # Set "scale" as initializing … … 1451 1398 def _set_details(self): 1452 1399 for name ,detail in self.p_model1.details.iteritems(): 1400 if name == 'background': 1401 continue 1453 1402 new_name = "p1_" + name 1454 1403 #if new_name not in self.orientation_params: … … 1456 1405 1457 1406 for name ,detail in self.p_model2.details.iteritems(): 1407 if name == 'background': 1408 continue 1458 1409 new_name = "p2_" + name 1459 1410 #if new_name not in self.orientation_params: … … 1481 1432 if new_name in self.p_model2.getParamList(): 1482 1433 self.p_model2.setParam(new_name, value) 1483 elif name == 'scale_factor' :1484 self.params[ 'scale_factor'] = value1434 elif name == 'scale_factor' or name == 'background': 1435 self.params[name] = value 1485 1436 else: 1486 1437 raise ValueError, "Model does not contain parameter %s" % name … … 1538 1489 def run(self, x = 0.0): 1539 1490 self._set_scale_factor() 1540 return self.params['scale_factor'] %s\1541 (self.p_model1.run(x) %s self.p_model2.run(x))1491 return self.params['scale_factor'] {factor_operator} \ 1492 (self.p_model1.run(x) {operator} self.p_model2.run(x)) + self.params['background'] 1542 1493 1543 1494 def runXY(self, x = 0.0): 1544 1495 self._set_scale_factor() 1545 return self.params['scale_factor'] %s\1546 (self.p_model1.runXY(x) %s self.p_model2.runXY(x))1496 return self.params['scale_factor'] {factor_operator} \ 1497 (self.p_model1.runXY(x) {operator} self.p_model2.runXY(x)) + self.params['background'] 1547 1498 1548 1499 ## Now (May27,10) directly uses the model eval function … … 1550 1501 def evalDistribution(self, x = []): 1551 1502 self._set_scale_factor() 1552 return self.params['scale_factor'] %s\1553 (self.p_model1.evalDistribution(x) %s\1554 self.p_model2.evalDistribution(x)) 1503 return self.params['scale_factor'] {factor_operator} \ 1504 (self.p_model1.evalDistribution(x) {operator} \ 1505 self.p_model2.evalDistribution(x)) + self.params['background'] 1555 1506 1556 1507 def set_dispersion(self, parameter, dispersion): … … 1589 1540 #m2.p_model2.setParam("scale", 100) 1590 1541 #m2.p_model2.setParam("rg", 100) 1591 out2 = m2.p_model1.runXY(0.01) %sm2.p_model2.runXY(0.01)\n1542 out2 = m2.p_model1.runXY(0.01) {operator} m2.p_model2.runXY(0.01)\n 1592 1543 print "My name is %s."% m1.name 1593 1544 print out1, " = ", out2
Note: See TracChangeset
for help on using the changeset viewer.