Back to home page

EIC code displayed by LXR

 
 

    


Warning, /eic-opticks/ana/tools.rst is written in an unsupported language. File is not indexed.

0001 Tools : bash, python, numpy, ipython, matplotlib
0002 ======================================================
0003 
0004 Opticks uses bash functions extensively and 
0005 analysis and debugging relies on:
0006 
0007 * Python
0008 * IPython
0009 * NumPy http://www.numpy.org
0010 * MatPlotLib
0011 
0012 
0013 In general the best way to install common software such as
0014 these tools is with your systems package manager. For example:
0015 
0016 * Linux: yum, apt-get
0017 * macOS: https://www.macports.org or homebrew
0018 * Windows: https://chocolatey.org 
0019 
0020 However there are other ways to install packages such as
0021 using **pip** for python packages.
0022 
0023 
0024 IPython Install on a mac with pip
0025 ----------------------------------
0026 
0027 Installing ipython on a mac if you do not use macports.
0028 
0029 ::
0030 
0031     delta:tests blyth$ sudo pip install ipython==1.2.1
0032     Password:
0033     Downloading/unpacking ipython==1.2.1
0034       Downloading ipython-1.2.1.tar.gz (8.7MB): 8.7MB downloaded
0035       Running setup.py (path:/private/tmp/pip_build_root/ipython/setup.py) egg_info for package ipython
0036         running egg_info
0037         
0038     Installing collected packages: ipython
0039       Running setup.py install for ipython
0040         running install
0041         
0042         Installing iplogger script to /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
0043         Installing iptest script to /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
0044         Installing ipcluster script to /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
0045         Installing ipython script to /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
0046         Installing pycolor script to /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
0047         Installing ipcontroller script to /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
0048         Installing irunner script to /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
0049         Installing ipengine script to /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
0050     Successfully installed ipython
0051     Cleaning up...
0052     delta:tests blyth$ 
0053     delta:tests blyth$ which ipython
0054     /opt/local/bin/ipython
0055     delta:tests blyth$ ipython
0056     Python 2.7.11 (default, Dec  5 2015, 23:51:51) 
0057     Type "copyright", "credits" or "license" for more information.
0058 
0059     IPython 1.2.1 -- An enhanced Interactive Python.
0060     ?         -> Introduction and overview of IPython's features.
0061     %quickref -> Quick reference.
0062     help      -> Python's own help system.
0063     object?   -> Details about 'object', use 'object??' for extra details.
0064 
0065     In [1]: import numpy as np
0066 
0067     In [2]: np.__version__
0068     Out[2]: '1.9.2'
0069 
0070     In [3]: import matplotlib
0071 
0072     In [4]: import matplotlib.pyplot as plt 
0073 
0074     In [5]: matplotlib.__version__
0075     Out[5]: '1.3.1'
0076 
0077 
0078 
0079 
0080 
0081 Working with bash functions
0082 -----------------------------
0083 
0084 Bash functions make you into a power user of the shell, by
0085 allowing you to operate at a higher level than individual commands
0086 without having to write separate scripts for everything.
0087 
0088 .. code-block:: sh 
0089 
0090     delta:tests blyth$ alias t="typeset -f"    # enables introspection of bash functions
0091     delta:tests blyth$ t opticks-
0092     opticks- () 
0093     { 
0094         source $(opticks-source) && opticks-env $*
0095     }
0096     delta:tests blyth$ t tprism-
0097     tprism- () 
0098     { 
0099         . $(opticks-home)/tests/tprism.bash && tprism-env $*
0100     }
0101     delta:tests blyth$ t tprism--     # not defined yet 
0102     delta:tests blyth$ tprism-        # running precursor defines functions including tprism-- tprism-usage and tprism-vi
0103     delta:tests blyth$ t tprism--
0104     tprism-- () 
0105     { 
0106         local msg="=== $FUNCNAME :";
0107         local cmdline=$*;
0108         local pol=${1:-s};
0109         shift;
0110         local tag=$(tprism-tag $pol);
0111         echo $msg pol $pol tag $tag;
0112         local phifrac0=0.1667;
0113         local phifrac1=0.4167;
0114         local phifrac2=0.6667;
0115         local quadrant=$phifrac1,$phifrac2;
0116         local critical=0.4854,0.4855;
0117         local material=GlassSchottF2;
0118         local azimuth=$quadrant;
0119         local surfaceNormal=0,1,0;
0120         local torch_config=(type=invcylinder photons=500000 mode=${pol}pol,wavelengthComb polarization=$surfaceNormal frame=-1 transform=0.500,0.866,0.000,0.000,-0.866,0.500,0.000,0.000,0.000,0.000,1.000,0.000,-86.603,0.000,0.000,1.000 target=0,-500,0 source=0,0,0 radius=300 distance=25 zenithazimuth=0,1,$azimuth material=Vacuum wavelength=0);
0121         local test_config=(mode=BoxInBox analytic=1 shape=box parameters=-1,1,0,700 boundary=Rock//perfectAbsorbSurface/Vacuum shape=prism parameters=60,300,300,200 boundary=Vacuum///$material);
0122         op.sh $* --animtimemax 7 --timemax 7 --geocenter --eye 0,0,1 --test --testconfig "$(join _ ${test_config[@]})" --torch --torchconfig "$(join _ ${torch_config[@]})" --torchdbg --save --tag $tag --cat $(tprism-det)
0123     }
0124 
0125     delta:tests blyth$ tprism-  # pressing [tab] shows available functions beginning tprism-
0126     tprism-        tprism-args    tprism-det     tprism-env     tprism-py      tprism-src     tprism-test    tprism-vi      
0127     tprism--       tprism-cd      tprism-dir     tprism-pol     tprism-source  tprism-tag     tprism-usage   
0128     delta:tests blyth$ tprism-    
0129 
0130 
0131 
0132 A powerful way to rapidly develop bash functions is to use 
0133 two terminal sessions, in the first edit the functions:
0134 
0135 .. code-block:: sh
0136 
0137     tprism-vi  
0138 
0139 In the second test the **tprism-foo** you are developing:
0140 
0141 .. code-block:: sh
0142 
0143     tprism-;tprism-foo    # tprism- updates function definitions and the tprism-foo runs it 
0144 
0145 
0146 
0147 IPython : interactive python example
0148 ---------------------------------------
0149 
0150 IPython enables you to inspect live python objects, so you can 
0151 learn by discovery.  In order to see the plots created by Opticks analysis
0152 scripts you will need to use IPython. 
0153 
0154 .. code-block:: py
0155 
0156     delta:ana blyth$ ipython
0157     Python 2.7.11 (default, Dec  5 2015, 23:51:51) 
0158     Type "copyright", "credits" or "license" for more information.
0159 
0160     IPython 1.2.1 -- An enhanced Interactive Python.
0161     ?         -> Introduction and overview of IPython's features.
0162     %quickref -> Quick reference.
0163     help      -> Python's own help system.
0164     object?   -> Details about 'object', use 'object??' for extra details.
0165 
0166     IPython profile: g4opticks
0167 
0168     In [1]: run tprism.py --tag 1
0169     tprism.py --tag 1
0170     INFO:__main__:sel prism/torch/  1 : TO BT BT SA 20160716-1941 /tmp/blyth/opticks/evt/prism/fdomtorch/1.npy 
0171     INFO:__main__:prism Prism(array([  60.,  300.,  300.,    0.]),Boundary Vacuum///GlassSchottF2 ) alpha 60.0  
0172     ...
0173 
0174 
0175 IPython Tab Completion
0176 ~~~~~~~~~~~~~~~~~~~~~~~~
0177 
0178 Discover available methods of an object by interactive exploration:
0179 
0180 .. code-block:: py
0181 
0182     In [2]: evt.  # press [tab] to see the possibilities
0183     evt.RPOL                evt.desc                evt.histype             evt.material_table      evt.ph                  evt.rflgs_              evt.seqhis              evt.td
0184     evt.RPOST               evt.description         evt.idom                evt.mattype             evt.polw                evt.rpol_               evt.seqhis_or_not       evt.tdii
0185     evt.RQWN_BINSCALE       evt.det                 evt.incident_angle      evt.msize               evt.post                evt.rpol_bins           evt.seqmat              evt.unique_wavelength
0186     evt.a_deviation_angle   evt.deviation_angle     evt.init_index          evt.nrec                evt.post_center_extent  evt.rpost_              evt.seqs                evt.valid
0187     evt.a_recside           evt.dirw                evt.init_metadata       evt.ox                  evt.ps                  evt.rs                  evt.src                 evt.wl
0188     evt.a_side              evt.fdom                evt.init_photons        evt.p0                  evt.py                  evt.rsmry_              evt.stamp               evt.x
0189     evt.all_history         evt.flags               evt.init_records        evt.p_out               evt.pyc                 evt.rsr                 evt.summary             evt.y
0190     evt.all_material        evt.flags_table         evt.init_selection      evt.path                evt.rec                 evt.rx                  evt.t                   evt.z
0191     evt.brief               evt.history             evt.label               evt.paths               evt.recflags            evt.rx_raw              evt.tag                 evt.zrt_profile
0192     evt.c4                  evt.history_table       evt.material            evt.pbins               evt.recwavelength       evt.selection           evt.tbins               
0193 
0194 
0195 
0196 One question mark gives the documentation:
0197 
0198 .. code-block:: py 
0199 
0200     In [2]: evt.deviation_angle?
0201     Type:       instancemethod
0202     File:       /Users/blyth/opticks/ana/evt.py
0203     Definition: evt.deviation_angle(self, side=None, incident=None)
0204     Docstring:
0205     Deviation angle for parallel squadrons of incident photons 
0206     without assuming a bounce count
0207 
0208 Two question marks gives the implementation:
0209 
0210 .. code-block:: py 
0211 
0212     In [4]: evt.zrt_profile??
0213 
0214     Type:       instancemethod
0215     File:       /Users/blyth/opticks/ana/evt.py
0216     Definition: evt.zrt_profile(self, n, pol=True)
0217     Source:
0218         def zrt_profile(self, n, pol=True):
0219             """
0220             :param n: number of bounce steps 
0221             :return: min, max, mid triplets for z, r and t  at n bounce steps
0222 
0223             ::
0224 
0225                 In [7]: a_zrt
0226                 Out[7]: 
0227                 array([[ 300.    ,  300.    ,  300.    ,    1.1748,   97.0913,   49.133 ,    0.1001,    0.1001,    0.1001],
0228                        [  74.2698,  130.9977,  102.6337,    1.1748,   97.0913,   49.133 ,    0.9357,    1.2165,    1.0761],
0229                        [  56.0045,  127.9946,   91.9996,    1.1748,   98.1444,   49.6596,    0.9503,    1.3053,    1.1278]])
0230 
0231 
0232             """
0233             slab = "z r t"
0234             if pol:
0235                 slab += " lx ly lz"
0236 
0237             labs = slab.split()
0238             nqwn = 3
0239             zrt = np.zeros((n,len(labs)*nqwn))
0240             tfmt = "%10.3f " * nqwn
0241             fmt = " ".join(["%s: %s " % (lab, tfmt) for lab in labs])
0242 
0243             for i in range(n):
0244                 p = self.rpost_(i)
0245                 l = self.rpol_(i)
0246                 lx = l[:,0]
0247                 ly = l[:,1]
0248                 lz = l[:,2]
0249 
0250 
0251 
0252 
0253 
0254 
0255 
0256 NumPy
0257 -------
0258 
0259 Opticks although mostly implemented in C++ uses the NPY serialization format
0260 for all geometry and event buffers allowing debugging and analysis
0261 to be done from the IPython using NumPy.
0262 
0263 NPY serialized files used extension **.npy**, find some:
0264 
0265 .. code-block:: sh
0266 
0267     delta:~ blyth$ find /tmp/blyth/opticks/evt/lens -name 1.npy
0268     /tmp/blyth/opticks/evt/lens/fdomtorch/1.npy
0269     /tmp/blyth/opticks/evt/lens/idomtorch/1.npy
0270     /tmp/blyth/opticks/evt/lens/notorch/1.npy
0271     /tmp/blyth/opticks/evt/lens/oxtorch/1.npy
0272     /tmp/blyth/opticks/evt/lens/phtorch/1.npy
0273     /tmp/blyth/opticks/evt/lens/pstorch/1.npy
0274     /tmp/blyth/opticks/evt/lens/rstorch/1.npy
0275     /tmp/blyth/opticks/evt/lens/rxtorch/1.npy
0276 
0277 
0278 Determine maximum photon time **in one line**:
0279 
0280 .. code-block:: sh
0281 
0282     delta:~ blyth$ python -c "import numpy as np ; print np.load('/tmp/blyth/opticks/evt/lens/oxtorch/1.npy')[:,0,3].max() "
0283     11.2614
0284 
0285 Do that more sedately with ipython:
0286 
0287 .. code-block:: py
0288 
0289     delta:~ blyth$ ipython
0290     Python 2.7.11 (default, Dec  5 2015, 23:51:51) 
0291     Type "copyright", "credits" or "license" for more information.
0292 
0293     IPython 1.2.1 -- An enhanced Interactive Python.
0294     ?         -> Introduction and overview of IPython's features.
0295     %quickref -> Quick reference.
0296     help      -> Python's own help system.
0297     object?   -> Details about 'object', use 'object??' for extra details.
0298 
0299     IPython profile: g4opticks
0300 
0301     In [1]: ox = np.load("/tmp/blyth/opticks/evt/lens/oxtorch/1.npy")
0302 
0303     In [2]: ox
0304     Out[2]: 
0305     array([[[   1.884,    0.202,  700.   ,    4.569],
0306             [   0.098,    0.011,    0.995,    1.   ],
0307             [  -0.107,    0.994,    0.   ,  630.652],
0308             [   0.   ,    0.   ,    0.   ,    0.   ]],
0309 
0310            [[   0.745,   -1.288,  700.   ,    4.568],
0311             [   0.05 ,   -0.087,    0.995,    1.   ],
0312             [   0.866,    0.501,    0.   ,  753.122],
0313             [   0.   ,    0.   ,    0.   ,    0.   ]],
0314 
0315            [[ 290.308, -500.146, -700.   ,    6.057],
0316             [   0.21 ,   -0.433,   -0.877,    1.   ],
0317             [   0.501,    0.818,   -0.283,  425.268],
0318             [   0.   ,    0.   ,    0.   ,    0.   ]],
0319 
0320            ..., 
0321            [[   2.217,    0.106,  700.   ,    4.572],
0322             [   0.078,    0.004,    0.997,    1.   ],
0323             [  -0.048,    0.999,    0.   ,  472.495],
0324             [   0.   ,    0.   ,    0.   ,    0.   ]],
0325 
0326            [[  -1.872,   -0.614,  700.   ,    4.574],
0327             [  -0.055,   -0.018,    0.998,    1.   ],
0328             [   0.312,   -0.95 ,    0.   ,  405.361],
0329             [   0.   ,    0.   ,    0.   ,    0.   ]],
0330 
0331            [[   2.042,   -3.525,  700.   ,    4.571],
0332             [   0.067,   -0.116,    0.991,    1.   ],
0333             [   0.865,    0.501,    0.   ,  589.317],
0334             [   0.   ,    0.   ,    0.   ,    0.   ]]], dtype=float32)
0335 
0336     In [3]: ox.shape
0337     Out[3]: (500000, 4, 4)    # 0.5M photons with (4,4) floats each 
0338 
0339     In [4]: ox[0]             # 1st photon 
0340     Out[4]: 
0341     array([[   1.884,    0.202,  700.   ,    4.569],
0342            [   0.098,    0.011,    0.995,    1.   ],
0343            [  -0.107,    0.994,    0.   ,  630.652],
0344            [   0.   ,    0.   ,    0.   ,    0.   ]], dtype=float32)
0345 
0346     In [5]: ox[-1]           # last photon  
0347     Out[5]: 
0348     array([[   2.042,   -3.525,  700.   ,    4.571],
0349            [   0.067,   -0.116,    0.991,    1.   ],
0350            [   0.865,    0.501,    0.   ,  589.317],
0351            [   0.   ,    0.   ,    0.   ,    0.   ]], dtype=float32)
0352 
0353     In [6]: ox[:,0,3]        # "wildcard" first dimension with ":" and pick the other two, giving end times of the photons  
0354     Out[6]: array([ 4.569,  4.568,  6.057, ...,  4.572,  4.574,  4.571], dtype=float32)
0355 
0356     In [7]: ox[:,0,3].min()  # earliest time 
0357     Out[7]: 0.15406403
0358 
0359     In [8]: ox[:,0,3].max()  # latest time
0360     Out[8]: 11.261424
0361 
0362 
0363 See :doc:`../ana/evt` for a higher level way of loading event buffers.
0364 
0365 
0366 
0367 
0368