|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +"""This module defines functions within IPython session to simulate |
| 4 | +the old pdffit2 interactive session. |
| 5 | +
|
| 6 | +Usage: %load_ext diffpy.pdffit2.ipy_ext |
| 7 | +""" |
| 8 | + |
| 9 | + |
1 | 10 | def load_ipython_extension(ipython): |
2 | 11 | from diffpy.pdffit2 import PdfFit |
3 | 12 | pf = PdfFit() |
| 13 | + pdf = EasyPDFPlotting(pf) |
4 | 14 | print(' Type help(pdffit) or help(topic) for information.') |
5 | 15 | print |
6 | | - ns = dict(pdffit=PdfFit) |
| 16 | + ns = dict(pdffit=PdfFit, pdf=pdf) |
7 | 17 | pf._exportAll(ns) |
8 | 18 | ipython.user_ns.update(ns) |
9 | 19 | return |
| 20 | + |
| 21 | + |
| 22 | +class EasyPDFPlotting(object): |
| 23 | + """Convenience functions for accessing and plotting PDFfit2 data. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self, pdffit_instance): |
| 27 | + self._pdffit = pdffit_instance |
| 28 | + return |
| 29 | + |
| 30 | + @property |
| 31 | + def r(self): |
| 32 | + "R-grid for PDF simulation." |
| 33 | + return self._asarray(self._pdffit.getR(), dtype=float) |
| 34 | + |
| 35 | + @property |
| 36 | + def Gobs(self): |
| 37 | + "Observed PDF data." |
| 38 | + return self._asarray(self._pdffit.getpdf_obs(), dtype=float) |
| 39 | + |
| 40 | + @property |
| 41 | + def Gcalc(self): |
| 42 | + "Calculated PDF data." |
| 43 | + return self._asarray(self._pdffit.getpdf_fit()) |
| 44 | + |
| 45 | + @property |
| 46 | + def Gdiff(self): |
| 47 | + "Difference between the observed and simulated PDF." |
| 48 | + return self.Gobs - self.Gcalc |
| 49 | + |
| 50 | + def showfit(self, offset=None): |
| 51 | + """Plot observed and simulated PDFs and the difference curve. |
| 52 | +
|
| 53 | + offset -- offset for the difference curve. |
| 54 | +
|
| 55 | + No return value. |
| 56 | + """ |
| 57 | + from matplotlib.pyplot import gca |
| 58 | + from math import floor |
| 59 | + cr = self.r |
| 60 | + cGobs = self.Gobs |
| 61 | + cGcalc = self.Gcalc |
| 62 | + cGdiff = self.Gdiff |
| 63 | + if offset is None: |
| 64 | + offset = floor(min([min(cGobs), min(cGcalc)]) - max(cGdiff)) |
| 65 | + ax = gca() |
| 66 | + ax.plot(cr, cGobs, 'r.', cr, cGcalc, 'b-', cr, cGdiff + offset, 'g-') |
| 67 | + xl = ax.xaxis.get_label().get_text() |
| 68 | + yl = ax.yaxis.get_label().get_text() |
| 69 | + if xl == "": |
| 70 | + ax.set_xlabel('r (A)') |
| 71 | + if yl == "": |
| 72 | + ax.set_ylabel('G (A**-2)') |
| 73 | + return |
| 74 | + |
| 75 | + def showRw(self): |
| 76 | + "Plot cumulative Rw." |
| 77 | + from matplotlib.pyplot import gca |
| 78 | + cRw = self._asarray(self._pdffit.getcrw()) |
| 79 | + ax = gca() |
| 80 | + ax.plot(self.r, cRw) |
| 81 | + ax.set_title('Cumulative Rw = %.4f' % cRw[-1]) |
| 82 | + ax.set_xlabel('r') |
| 83 | + ax.set_ylabel('Rw') |
| 84 | + return |
| 85 | + |
| 86 | + @staticmethod |
| 87 | + def _asarray(x, dtype=None): |
| 88 | + import numpy |
| 89 | + return numpy.asarray(x, dtype=dtype) |
| 90 | + |
| 91 | +# End of class EasyPDFPlotting |
0 commit comments