Changeset 3209 for Tutorials/PythonScript/Scripting.htm
- Timestamp:
- Dec 28, 2017 12:50:40 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Tutorials/PythonScript/Scripting.htm
r3207 r3209 8 8 To demonstrate the use of the 9 9 <A href="http://gsas-ii.readthedocs.io/en/latest/GSASIIscripts.html" target="_blank"> 10 GSASIIscriptable module</A>, we create a Python script to duplicate the 11 refinement in the 12 <A 13 href="https://subversion.xray.aps.anl.gov/pyGSAS/Tutorials/CWCombined/Combined%20refinement.htm" 14 target="_blank">GSAS-II CW Combined Refinement</A> 15 tutorial. This uses a Python script 16 to perform the same refinements steps as in that tutorial, but without use of the GSAS-II 10 GSASIIscriptable module</A>. This uses a Python script 11 to perform a refinement or computation, but without use of the GSAS-II 17 12 graphical user interface. Note that the 18 13 <A href="http://gsas-ii.readthedocs.io/en/latest/GSASIIscripts.html" target="_blank"> … … 22 17 by Jackson O'Donnell, as a summer undergraduate visitor under 23 18 supervisor Dr. Maria Chan. Other programming contributions are welcome. 19 <P> 20 This tutorial has three sections: 21 <OL type="A"> 22 <LI><a href="#multistep"> Create a Multi-step Script</A></LI> 23 <LI><a href="#SingleStep">Single-Step Refinement</a></LI> 24 <LI><a href="#Simulate">Powder Pattern Simulation</a></LI> 25 </OL> 26 The first section duplicates the refinement in the 27 <A 28 href="https://subversion.xray.aps.anl.gov/pyGSAS/Tutorials/CWCombined/Combined%20refinement.htm" 29 target="_blank">GSAS-II CW Combined Refinement</A> 30 tutorial as a multi-step process. The second section repeats the previous 31 refinement, but demonstrates how a complex process can be entered into 32 a single Python dict and the refinement executed with a single 33 function call. The third section shows how a pattern simulation, 34 rather than refinement can be executed from a script. 35 24 36 25 37 <h2>Prerequisites</h2> … … 45 57 <P> 46 58 The exercise can be performed by placing all of the Python commands 47 into a script, (which is supplied for 48 <A href="https://subversion.xray.aps.anl.gov/trac/pyGSAS/browser/Tutorials/PythonScript/data/example.py?format=txt"> 49 download here</A>), 59 into a script, 50 60 but a more pedagogical approach will be to enter the 51 61 commands into a Python interpreter. Use of IPython or Jupyter to run 52 62 Python will make this a more pleasant experience. 53 63 54 <h2>Multi-step Script Approach</h2> 64 <a name="multistep"> 65 <h2>A. Create a Multi-step Script</h2></a> 66 67 Note that the script in this section is supplied for 68 <A href="https://subversion.xray.aps.anl.gov/trac/pyGSAS/browser/Tutorials/PythonScript/data/example.py?format=txt"> 69 download here</A>, but some editing will be needed to match where files 70 are found on your computer. 55 71 56 72 <h4>0: Load the GSASIIscriptable module</H4> … … 450 466 451 467 </blockquote> 452 <a name="SingleStep"><h2>Single Step Approach</h2></a> 468 <a name="SingleStep"> 469 <h2>B. Single-Step Refinement</h2></a> 453 470 <blockquote> 454 471 … … 457 474 GSASIIscriptable module documentation</A>, 458 475 the <I>project</i><tt>.do_refinements()</tt> method can be used to 459 perform multiple refinement steps. To duplicate the above steps into a 476 perform multiple refinement steps. 477 Note that this version of the exercise can be 478 <A href="https://subversion.xray.aps.anl.gov/trac/pyGSAS/browser/Tutorials/PythonScript/data/SingleStep.py?format=txt"> 479 downloaded here</A>. 480 To duplicate the above steps into a 460 481 single call, a more complex set of dicts must be created, as shown 461 482 below: 462 483 463 <blockquote><textarea rows="3 9" cols="75" readonly>484 <blockquote><textarea rows="38" cols="75" readonly> 464 485 # tutorial step 4: turn on background refinement (Hist) 465 486 refdict0 = {"set": {"Background": { "no. coeffs": 3, "refine": True }}, … … 515 536 dictList = [refdict0,refdict1,refdict2,refdict3, 516 537 refdict4a,refdict4b, 517 refdict5a,refdict5b,refdict5c] 518 </textarea></blockquote> 538 refdict5a,refdict5b,refdict5c]</textarea></blockquote> 539 519 540 Steps 4 through 10, above then can be performed with these few commands: 520 541 <blockquote><textarea rows="4" cols="75" readonly> … … 522 543 gpx.data['Controls']['data']['max cyc'] = 8 # not in API 523 544 hist2.data['Sample Parameters']['Gonio. radius'] = 650. # not in API 524 gpx.do_refinements(dictList) 525 </textarea></blockquote> 526 Note that this version of the exercise can be 527 <A href="https://subversion.xray.aps.anl.gov/trac/pyGSAS/browser/Tutorials/PythonScript/data/SingleStep.py?format=txt"> 528 downloaded here</A>). 545 gpx.do_refinements(dictList)</textarea></blockquote> 546 547 </blockquote> 548 <a name="Simulate"> 549 <h2>C. Powder Pattern Simulation</h2></a> 550 <blockquote> 551 Use of the 552 <A href="http://gsas-ii.readthedocs.io/en/latest/GSASIIscripts.html" target="_blank"> 553 GSASIIscriptable module</A> makes it very simple to simulate a powder 554 diffraction pattern using GSAS-II. This is demonstrated in this 555 section. 556 557 Note that the Python commands can be 558 <A 559 href="https://subversion.xray.aps.anl.gov/trac/pyGSAS/browser/Tutorials/PythonScript/data/sim.py?format=txt">downloaded here</A>. 560 <P> 561 As before, the location of the GSASIIscripts Python module must be 562 defined and the module must be loaded: 563 564 <blockquote><textarea rows="3" cols="60" readonly> 565 import os,sys 566 sys.path.insert(0,os.path.expanduser("~/g2conda/GSASII/")) 567 import GSASIIscriptable as G2sc</textarea></blockquote> 568 569 To simplify this example, as before we will define the location where files will 570 be read from and written (as <tt>datadir</tt> and 571 <tt>workdir</tt>). Note that files "inst_d1a.prm" and 572 "PbSO4-Wyckoff.cif" from <A 573 href="https://subversion.xray.aps.anl.gov/pyGSAS/Tutorials/PythonScript/data/">here</A> 574 are needed. 575 576 <blockquote><textarea rows="2" cols="70" readonly> 577 workdir = "/Users/toby/Scratch/PythonScript" 578 datadir = "/Users/toby/software/G2/Tutorials/PythonScript/data"</textarea></blockquote> 579 580 We then need to create a project and for this example we choose to 581 define the phase first. (It would work equally well to create the 582 histogram first and then define the phase.) 583 584 <blockquote><textarea rows="4" cols="70" readonly> 585 gpx = G2sc.G2Project(filename='PbSO4sim.gpx') # create a project 586 # setup step 1: add a phase to the project 587 phase0 = gpx.add_phase(os.path.join(datadir,"PbSO4-Wyckoff.cif"), 588 phasename="PbSO4",fmthint='CIF') </textarea></blockquote> 589 590 We then add a "dummy" histogram to the project. Note that an 591 instrument parameter file is specified, but not a data file. The range 592 of data to be used and the step size must be specified. The phases 593 parameter is specified as ``gpx.phases()`` which creates a list of all 594 the previously read phases, which in this case is equivalent to 595 ``[phase0]``. 596 597 <blockquote><textarea rows="6" cols="70" readonly> 598 # setup step 2: add a simulated histogram and link it to the previous phase(s) 599 hist1 = gpx.add_simulated_powder_histogram("PbSO4 simulation", 600 os.path.join(datadir,"inst_d1a.prm"), 601 5.,120.,0.01, 602 phases=gpx.phases())</textarea></blockquote> 603 604 Finally, to perform the simulation computation, a refinement is 605 needed: 606 607 <blockquote><textarea rows="3" cols="70" readonly> 608 gpx.data['Controls']['data']['max cyc'] = 0 # refinement not needed 609 gpx.do_refinements([{}]) 610 gpx.save()</textarea></blockquote> 611 612 However, there is no need to actually optimize any variables, 613 so the number of refinement cycles is set to zero. Refinement is 614 initiated then with <I>proj</i>.<tt>do_refinements</tt>. Finally, the 615 project is saved. 529 616 530 617 </blockquote> … … 532 619 <hr> 533 620 <address></address> 534 <!-- hhmts start -->Last modified: T ue Dec 26 17:03:22CST 2017 <!-- hhmts end -->621 <!-- hhmts start -->Last modified: Thu Dec 28 12:18:27 CST 2017 <!-- hhmts end --> 535 622 </body> </html>
Note: See TracChangeset
for help on using the changeset viewer.