Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:46

0001 #!/usr/bin/env python
0002 #
0003 # Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004 #
0005 # This file is part of Opticks
0006 # (see https://bitbucket.org/simoncblyth/opticks).
0007 #
0008 # Licensed under the Apache License, Version 2.0 (the "License"); 
0009 # you may not use this file except in compliance with the License.  
0010 # You may obtain a copy of the License at
0011 #
0012 #   http://www.apache.org/licenses/LICENSE-2.0
0013 #
0014 # Unless required by applicable law or agreed to in writing, software 
0015 # distributed under the License is distributed on an "AS IS" BASIS, 
0016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017 # See the License for the specific language governing permissions and 
0018 # limitations under the License.
0019 #
0020 
0021 """
0022 cfplot.py : Comparison Plotter with Chi2 Underplot 
0023 ======================================================
0024 
0025 To control this warning, see the rcParam `figure.max_num_figures
0026 
0027 
0028 """
0029 import os, logging, numpy as np
0030 from collections import OrderedDict as odict
0031 from opticks.ana.main import opticks_main
0032 from opticks.ana.cfh import CFH 
0033 log = logging.getLogger(__name__)
0034 
0035 try:
0036     import matplotlib.pyplot as plt
0037     import matplotlib.gridspec as gridspec
0038     plt.rcParams["figure.max_open_warning"] = 200    # default is 20
0039 except ImportError:
0040     print("matplotlib missing : you need this to make plots")
0041     plt = None
0042 
0043 
0044 def cfplot(fig, gss, h, xline=[]): 
0045     """
0046     :param fig:
0047     :param gss: 2 element gridspec list 
0048     :param h:  .ahis .bhis .la .lb .ylim .bins .chi2 .c2label .c2_ymax .log 
0049     """
0050 
0051     ax = fig.add_subplot(gss[0])
0052     ax0 = ax
0053 
0054     ax.plot( h.bins[:-1], h.ahis , drawstyle="steps", label=h.la  )
0055     ax.plot( h.bins[:-1], h.bhis , drawstyle="steps", label=h.lb  )
0056 
0057     if h.log:
0058         ax.set_yscale('log')
0059     pass
0060 
0061     ylim0 = h.ylim
0062     if len(xline) > 0:
0063         for x in xline:
0064             ax.plot( [x,x], ylim0, linestyle="dashed" )
0065         pass
0066     pass
0067 
0068     ax.set_ylim(ylim0)
0069     ax.legend()
0070 
0071     xlim = ax.get_xlim()
0072 
0073     ax = fig.add_subplot(gss[1])
0074     ax1 = ax
0075 
0076     ax.plot( h.bins[:-1], h.chi2, drawstyle='steps', label=h.c2label )
0077 
0078     ax.set_xlim(xlim) 
0079     ax.legend()
0080 
0081     ylim1 = [0,h.c2_ymax*1.05]
0082     if len(xline) > 0:
0083         for x in xline:
0084             ax.plot( [x,x], ylim1, linestyle="dashed" )
0085         pass
0086     pass
0087     ax.set_ylim(ylim1) 
0088 
0089     return [ax0,ax1]
0090 
0091 
0092 
0093 def qwns_plot( ok, hh, suptitle ):
0094     nhh = len(hh)
0095     nxm = 4 
0096     if nhh > nxm:
0097         # pagination 
0098         for p in range(nhh/nxm):
0099             phh = hh[p*nxm:(p+1)*nxm]
0100             qwns_plot( ok, phh, suptitle + " (%d)" % p )   
0101         pass
0102     else:
0103         fig = plt.figure(figsize=ok.figsize)
0104         fig.suptitle(suptitle)
0105         ny = 2 
0106         nx = len(hh)
0107         gs = gridspec.GridSpec(ny, nx, height_ratios=[3,1])
0108         for ix in range(nx):
0109             gss = [gs[ix], gs[nx+ix]]
0110             h = hh[ix]
0111             cfplot(fig, gss, hh[ix] )
0112         pass
0113 
0114 def one_cfplot(ok, h, xline=[]):
0115     """
0116     :param ok: used for figsize
0117     :param h: CFH instance
0118     """
0119     fig = plt.figure(figsize=ok.figsize)
0120     fig.suptitle(h.suptitle)
0121     ny = 2
0122     nx = 1
0123     gs = gridspec.GridSpec(ny, nx, height_ratios=[3,1])
0124 
0125     ix = 0 
0126     gss = [gs[ix], gs[nx+ix]]
0127     axs = cfplot(fig, gss, h, xline=xline )
0128     return fig, axs 
0129 
0130 
0131 def multiplot(ok, ab, start=0, stop=5, log_=False):
0132     """
0133     """
0134     pages = ok.qwn.split(",")    ## default ok.qwn 'XYZT,ABCW'
0135 
0136     for i,isel in enumerate(range(start, stop)):
0137 
0138         ab.sel = slice(isel, isel+1)
0139         nrec = ab.nrec      ## number of slots with the selected history 
0140 
0141         for irec in range(nrec):     
0142 
0143             ab.irec = irec           
0144             suptitle = ab.suptitle   ## irec "slot" gets highlighted in the title eg "... SI [BT] BT AB "
0145 
0146             log.info("multiplot irec %d nrec %d suptitle %s " % (irec, nrec, suptitle))
0147 
0148             for page in pages:
0149                 hh = ab.rhist(page, irec, log_ )
0150                 qwns_plot( ok, hh, suptitle )
0151             pass
0152         pass
0153     pass
0154 
0155 
0156 def sc_selection_plot(ok, ab, log_=False):
0157     """
0158     :param ok:
0159     :param ab:
0160     :param log_:
0161 
0162     Plotting quantities at first scatter, needs a single selection.
0163     """
0164     pages = ok.qwn.split(",")
0165     irec = ab.iflg("SC")
0166 
0167     ab.irec = irec
0168     suptitle = ab.suptitle
0169 
0170     for page in pages:
0171         hh = ab.rhist(page, irec, log_)
0172         qwns_plot( ok, hh, suptitle )
0173     pass
0174 
0175 
0176 
0177 if __name__ == '__main__':
0178     ok = opticks_main()
0179     print(ok)
0180     plt.ion()
0181     plt.close()
0182 
0183     from opticks.ana.ab import AB
0184     h = AB.rrandhist()
0185     assert type(h).__name__  == "CFH"
0186     one_cfplot(ok, h) 
0187