File indexing completed on 2026-04-09 07:48:47
0001
0002 """
0003 eprint.py
0004 ===========
0005
0006 In order for the below "eval" code to work from an invoking script
0007 it is necessary to pass globals() and locals() from the script
0008 into these functions.
0009
0010 For example::
0011
0012 from opticks.ana.eprint import eprint, epr
0013 ...
0014 eprint("np.all( t.boundary_lookup_all == t.boundary_lookup_all_src )", globals(), locals() )
0015
0016 # OR returning the evaluation with epr:
0017 bm = epr("bm = np.all( t.boundary_lookup_all == t.boundary_lookup_all_src )", globals(), locals() )
0018
0019 TODO: annotated check asserts
0020
0021 """
0022
0023 def eprint( expr, g, l, lprefix="", rprefix="", tail="" ):
0024 """
0025 Returns the evaluated expression and prints a formatted line showing:
0026
0027 1. lhs : numpy/python expression
0028 2. rhs : evaluated result of the expression
0029
0030 :param expr: expression to be evaluated
0031 :param g: globals() from calling scope
0032 :param l: locals() from calling scope
0033 :param lprefix: lhs prefix before the expression
0034 :param rprefix: rhs prefix before the result
0035 :param tail: after the result
0036 :return ret: result of the evaluation
0037 """
0038 ret = eval(expr, g, l)
0039 lhs = "%s%s" % (lprefix, expr)
0040 rhs = "%s%s" % (rprefix, ret )
0041 print("%-50s : %s%s" % ( lhs, rhs, tail ) )
0042 return ret
0043
0044 def epr(arg, g, l, **kwa):
0045 """
0046 Slightly higher level variant of expression printing that invokes eprint
0047 with lprefix and expr obtained by parsing the *arg* to find the symbol
0048 and expression on either side of equal sign.
0049
0050 :param arg:
0051 :param g: globals() from calling scope
0052 :param l: locals() from calling scope
0053 :param kwa: named args passed to eprint
0054 :return ret: evaluated result
0055 """
0056 p = arg.find("=")
0057 if p > -1:
0058 var_eq = arg[:p+1]
0059 expr = arg[p+1:]
0060 lprefix = var_eq
0061 else:
0062 lprefix, expr = "", arg
0063 pass
0064 return eprint(expr, g, l, lprefix=lprefix, **kwa)
0065
0066
0067 def edv(expr, g, l):
0068 """
0069 Example comparing deviations of record point 1 intersect z position in two histories::
0070
0071 epr("o = cuss(a.seq[:,0])", globals(), locals(), rprefix="\n" )
0072 edv("a.record[w0,1,0,2] - b.record[w0,1,0,2] # point 1 z", globals(), locals(), rprefix="\n")
0073 edv("a.record[w3,1,0,2] - b.record[w3,1,0,2] # point 1 z", globals(), locals(), rprefix="\n")
0074
0075 """
0076 dv = epr("dv = %s" % expr, g, l )
0077
0078 g["dv"] = dv
0079
0080 eprint("dv.shape", g, l )
0081 eprint("dv.min()", g, l )
0082 eprint("dv.max()", g, l )
0083 eprint("len(np.where( dv < 0 )[0])", g, l )
0084 eprint("len(np.where( dv == 0 )[0])", g, l )
0085 eprint("len(np.where( dv > 0 )[0])", g, l )
0086
0087
0088
0089
0090
0091