- Timestamp:
- Aug 9, 2017 2:41:58 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branch/2frame/GSASIIscriptable.py
r2979 r2984 14 14 Routines for reading, writing, modifying and creating GSAS-II project (.gpx) files. 15 15 16 Supports a command line interface as well. 16 17 """ 17 18 from __future__ import division, print_function # needed? … … 43 44 import GSASIImath as G2mth 44 45 import GSASIIElem as G2elem 46 47 try: 48 import matplotlib.pyplot as plt 49 except ImportError: 50 plt = None 45 51 46 52 def LoadDictFromProjFile(ProjFile): … … 502 508 Iparm1, Iparm2 = instprm 503 509 except ValueError: 504 # TODO load_iprms505 510 Iparm1, Iparm2 = load_iprms(instprm, reader) 506 511 … … 959 964 phases = [self.phase(name) for name in phase] 960 965 966 967 # TODO: HAP parameters: 968 # Babinet 969 # Extinction 970 # HStrain 971 # Mustrain 972 # Pref. Ori 973 # Size 974 975 pwdr_set = {} 976 phase_set = {} 961 977 for key, val in refinement.get('set', {}).items(): 962 978 # Apply refinement options 963 979 if G2PwdrData.is_valid_refinement_key(key): 964 for hist in hists: 965 hist.set_refinement(key, val) 980 pwdr_set[key] = val 966 981 elif G2Phase.is_valid_refinement_key(key): 967 for phase in phases: 968 phase.set_refinement(key, val) 982 phase_set[key] = val 969 983 else: 970 984 print("Unknown refinement key:", key) 971 985 986 for hist in hists: 987 hist.set_refinements(pwdr_set) 988 for phase in phases: 989 phase.set_refinements(phase_set) 990 991 pwdr_clear = {} 992 phase_clear = {} 972 993 for key, val in refinement.get('clear', {}).items(): 973 994 # Clear refinement options 974 995 if G2PwdrData.is_valid_refinement_key(key): 975 for hist in hists: 976 hist.clear_refinement(key, val) 996 pwdr_clear[key] = val 977 997 elif G2Phase.is_valid_refinement_key(key): 978 for phase in phases: 979 phase.clear_refinement(key, val) 998 phase_clear[key] = val 980 999 else: 981 1000 print("Unknown refinement key:", key) 1001 1002 for hist in hists: 1003 hist.clear_refinements(pwdr_clear) 1004 for phase in phases: 1005 phase.clear_refinements(phase_clear) 982 1006 983 1007 def index_ids(self): … … 1206 1230 1207 1231 # TODO adjust pwddata? GSASIIpwdGUI.py:1041 1232 # TODO update background 1233 self.proj.save() 1208 1234 1209 1235 def y_calc(self): 1210 1236 return self['data'][1][3] 1211 1237 1212 def set_refinement(self, key, value): 1213 """Sets the refinement parameter 'key' to the specification 'value'""" 1214 if key == 'Limits': 1215 old_limits = self.data['Limits'][1] 1216 new_limits = value 1217 if isinstance(new_limits, dict): 1218 if 'low' in new_limits: 1219 old_limits[0] = new_limits['low'] 1220 if 'high' in new_limits: 1221 old_limits[1] = new_limits['high'] 1238 def plot(self, Yobs=True, Ycalc=True, Background=True): 1239 if plt: 1240 data = self['data'][1] 1241 if Yobs: 1242 plt.plot(data[0], data[1], label='Yobs') 1243 if Ycalc: 1244 plt.plot(data[0], data[3], label='Ycalc') 1245 if Background: 1246 plt.plot(data[0], data[4], label='Background') 1247 1248 def set_refinements(self, refs): 1249 """Sets the refinement parameter 'key' to the specification 'value' 1250 1251 :param dict refs: A dictionary of the parameters to be set. 1252 1253 :returns: None""" 1254 do_fit_fixed_points = False 1255 for key, value in refs.items(): 1256 if key == 'Limits': 1257 old_limits = self.data['Limits'][1] 1258 new_limits = value 1259 if isinstance(new_limits, dict): 1260 if 'low' in new_limits: 1261 old_limits[0] = new_limits['low'] 1262 if 'high' in new_limits: 1263 old_limits[1] = new_limits['high'] 1264 else: 1265 old_limits[0], old_limits[1] = new_limits 1266 elif key == 'Sample Parameters': 1267 sample = self.data['Sample Parameters'] 1268 for sparam in value: 1269 sample[sparam][1] = True 1270 elif key == 'Background': 1271 bkg, peaks = self.data['Background'] 1272 1273 # If True or False, just set the refine parameter 1274 if value in (True, False): 1275 bkg[1] = value 1276 return 1277 1278 if 'type' in value: 1279 bkg[0] = value['type'] 1280 if 'refine' in value: 1281 bkg[1] = value['refine'] 1282 if 'no. coeffs' in value: 1283 cur_coeffs = bkg[2] 1284 n_coeffs = value['no. coeffs'] 1285 if n_coeffs > cur_coeffs: 1286 for x in range(n_coeffs - cur_coeffs): 1287 bkg.append(0.0) 1288 else: 1289 for _ in range(cur_coeffs - n_coeffs): 1290 bkg.pop() 1291 bkg[2] = n_coeffs 1292 if 'coeffs' in value: 1293 bkg[3:] = value['coeffs'] 1294 if 'FixedPoints' in value: 1295 peaks['FixedPoints'] = [(float(a), float(b)) 1296 for a, b in value['FixedPoints']] 1297 if value.get('fit fixed points', False): 1298 do_fit_fixed_points = True 1299 1300 elif key == 'Instrument Parameters': 1301 instrument, secondary = self.data['Instrument Parameters'] 1302 for iparam in value: 1303 try: 1304 instrument[iparam][2] = True 1305 except IndexError: 1306 raise ValueError("Invalid key:", iparam) 1222 1307 else: 1223 old_limits[0], old_limits[1] = new_limits 1224 elif key == 'Sample Parameters': 1225 sample = self.data['Sample Parameters'] 1226 for sparam in value: 1227 sample[sparam][1] = True 1228 elif key == 'Background': 1229 bkg, peaks = self.data['Background'] 1230 1231 # If True or False, just set the refine parameter 1232 if value in (True, False): 1233 bkg[1] = value 1234 return 1235 1236 if 'type' in value: 1237 bkg[0] = value['type'] 1238 if 'refine' in value: 1239 bkg[1] = value['refine'] 1240 if 'no. coeffs' in value: 1241 cur_coeffs = bkg[2] 1242 n_coeffs = value['no. coeffs'] 1243 if n_coeffs > cur_coeffs: 1244 for x in range(n_coeffs - cur_coeffs): 1245 bkg.append(0.0) 1246 else: 1247 for _ in range(cur_coeffs - n_coeffs): 1248 bkg.pop() 1249 bkg[2] = n_coeffs 1250 if 'coeffs' in value: 1251 bkg[3:] = value['coeffs'] 1252 if 'FixedPoints' in value: 1253 peaks['FixedPoints'] = [(float(a), float(b)) 1254 for a, b in value['FixedPoints']] 1255 if value.get('fit fixed points', False): 1256 self.fit_fixed_points() 1257 1258 elif key == 'Instrument Parameters': 1259 instrument, secondary = self.data['Instrument Parameters'] 1260 for iparam in value: 1261 try: 1262 instrument[iparam][2] = True 1263 except IndexError: 1264 raise ValueError("Invalid key:", iparam) 1265 else: 1266 raise ValueError("Unknown key:", key) 1267 1268 def clear_refinement(self, key, value): 1308 raise ValueError("Unknown key:", key) 1309 # Fit fixed points after the fact - ensure they are after fixed points 1310 # are added 1311 if do_fit_fixed_points: 1312 self.fit_fixed_points() 1313 1314 def clear_refinements(self, refs): 1269 1315 """Clears the refinement parameter 'key' and its associated value.""" 1270 if key == 'Limits': 1271 old_limits, cur_limits = self.data['Limits'] 1272 cur_limits[0], cur_limits[1] = old_limits 1273 elif key == 'Sample Parameters': 1274 sample = self.data['Sample Parameters'] 1275 for sparam in value: 1276 sample[sparam][1] = False 1277 elif key == 'Background': 1278 bkg, peaks = self.data['Background'] 1279 1280 # If True or False, just set the refine parameter 1281 if value in (True, False): 1316 for key, value in refs.items(): 1317 if key == 'Limits': 1318 old_limits, cur_limits = self.data['Limits'] 1319 cur_limits[0], cur_limits[1] = old_limits 1320 elif key == 'Sample Parameters': 1321 sample = self.data['Sample Parameters'] 1322 for sparam in value: 1323 sample[sparam][1] = False 1324 elif key == 'Background': 1325 bkg, peaks = self.data['Background'] 1326 1327 # If True or False, just set the refine parameter 1328 if value in (True, False): 1329 bkg[1] = False 1330 return 1331 1282 1332 bkg[1] = False 1283 return 1284 1285 bkg[1] = False 1286 if 'FixedPoints' in value: 1287 if 'FixedPoints' in peaks: 1288 del peaks['FixedPoints'] 1289 elif key == 'Instrument Parameters': 1290 instrument, secondary = self.data['Instrument Parameters'] 1291 for iparam in value: 1292 instrument[iparam][2] = False 1293 else: 1294 raise ValueError("Unknown key:", key) 1333 if 'FixedPoints' in value: 1334 if 'FixedPoints' in peaks: 1335 del peaks['FixedPoints'] 1336 elif key == 'Instrument Parameters': 1337 instrument, secondary = self.data['Instrument Parameters'] 1338 for iparam in value: 1339 instrument[iparam][2] = False 1340 else: 1341 raise ValueError("Unknown key:", key) 1295 1342 1296 1343 … … 1357 1404 'vol': cell[7]} 1358 1405 1359 def set_refinement(self, key, value): 1360 if key == "Cell": 1361 self.data['General']['Cell'][0] = True 1362 elif key == "Atoms": 1363 cx, ct, cs, cia = self.data['General']['AtomPtrs'] 1364 1365 for atomlabel, atomrefinement in value.items(): 1366 if atomlabel == 'all': 1367 for atom in self.atoms(): 1406 def set_refinements(self, refs): 1407 for key, value in refs.items(): 1408 if key == "Cell": 1409 self.data['General']['Cell'][0] = True 1410 elif key == "Atoms": 1411 cx, ct, cs, cia = self.data['General']['AtomPtrs'] 1412 1413 for atomlabel, atomrefinement in value.items(): 1414 if atomlabel == 'all': 1415 for atom in self.atoms(): 1416 atom.refinement_flags = atomrefinement 1417 else: 1418 atom = self.atom(atomlabel) 1368 1419 atom.refinement_flags = atomrefinement 1369 else: 1420 elif key == "LeBail": 1421 hists = self.data['Histograms'] 1422 for hname, hoptions in hists.items(): 1423 if 'LeBail' not in hoptions: 1424 hoptions['newLeBail'] = True 1425 hoptions['LeBail'] = bool(value) 1426 else: 1427 raise ValueError("Unknown key:", key) 1428 1429 def clear_refinements(self, refs): 1430 for key, value in refs.items(): 1431 if key == "Cell": 1432 self.data['General']['Cell'][0] = False 1433 elif key == "Atoms": 1434 cx, ct, cs, cia = self.data['General']['AtomPtrs'] 1435 1436 for atomlabel in value: 1370 1437 atom = self.atom(atomlabel) 1371 atom.refinement_flags = atomrefinement 1372 elif key == "LeBail": 1373 hists = self.data['Histograms'] 1374 for hname, hoptions in hists.items(): 1375 if 'LeBail' not in hoptions: 1376 hoptions['newLeBail'] = True 1377 hoptions['LeBail'] = bool(value) 1378 else: 1379 raise ValueError("Unknown key:", key) 1380 1381 def clear_refinement(self, key, value): 1382 if key == "Cell": 1383 self.data['General']['Cell'][0] = False 1384 elif key == "Atoms": 1385 cx, ct, cs, cia = self.data['General']['AtomPtrs'] 1386 1387 for atomlabel in value: 1388 atom = self.atom(atomlabel) 1389 # Set refinement to none 1390 atom.refinement_flags = ' ' 1391 elif key == "LeBail": 1392 hists = self.data['Histograms'] 1393 for hname, hoptions in hists.items(): 1394 if 'LeBail' not in hoptions: 1395 hoptions['newLeBail'] = True 1396 hoptions['LeBail'] = False 1397 else: 1398 raise ValueError("Unknown key:", key) 1438 # Set refinement to none 1439 atom.refinement_flags = ' ' 1440 elif key == "LeBail": 1441 hists = self.data['Histograms'] 1442 for hname, hoptions in hists.items(): 1443 if 'LeBail' not in hoptions: 1444 hoptions['newLeBail'] = True 1445 hoptions['LeBail'] = False 1446 else: 1447 raise ValueError("Unknown key:", key) 1399 1448 1400 1449 … … 1481 1530 for p in phases: 1482 1531 print(fname, p.name) 1532 1533 1483 1534 def IPyBrowse(*args): 1484 1535 """Load a .gpx file and then open a IPython shell to browse it … … 1492 1543 break 1493 1544 1545 1494 1546 def refine(*args): 1495 1547 """The refine subcommand""" … … 1500 1552 def seqrefine(*args): 1501 1553 """The seqrefine subcommand""" 1502 pass1554 raise NotImplementedError("seqrefine is not yet implemented") 1503 1555 1504 1556 … … 1506 1558 """The export subcommand""" 1507 1559 # Export CIF or Structure or ... 1508 pass1560 raise NotImplementedError("export is not yet implemented") 1509 1561 1510 1562
Note: See TracChangeset
for help on using the changeset viewer.