def test_math_module(): "Operations with the math module" x = uncertainties.ufloat((-1.5, 0.1)) # The exponent must not be differentiated, when calculating the # following (the partial derivative with respect to the exponent # is not defined): assert (x**2).nominal_value == 2.25 # Regular operations are chosen to be unchanged: assert isinstance(umath.sin(3), float) # Python >=2.6 functions: if sys.version_info[:2] >= (2, 6): # factorial() must not be "damaged" by the umath module, so as # to help make it a drop-in replacement for math (even though # factorial() does not work on numbers with uncertainties # because it is restricted to integers, as for # math.factorial()): assert umath.factorial(4) == 24 # Boolean functions: assert not umath.isinf(x) # Comparison, possibly between an AffineScalarFunc object and a # boolean, which makes things more difficult for this code: assert umath.isinf(x) == False # fsum is special because it does not take a fixed number of # variables: assert umath.fsum([x, x]).nominal_value == -3
def test_math_module(): "Operations with the math module" x = ufloat(-1.5, 0.1) # The exponent must not be differentiated, when calculating the # following (the partial derivative with respect to the exponent # is not defined): assert (x**2).nominal_value == 2.25 # Regular operations are chosen to be unchanged: assert isinstance(umath.sin(3), float) # Python >=2.6 functions: if sys.version_info >= (2, 6): # factorial() must not be "damaged" by the umath module, so as # to help make it a drop-in replacement for math (even though # factorial() does not work on numbers with uncertainties # because it is restricted to integers, as for # math.factorial()): assert umath.factorial(4) == 24 # Boolean functions: assert not umath.isinf(x) # Comparison, possibly between an AffineScalarFunc object and a # boolean, which makes things more difficult for this code: assert umath.isinf(x) == False # fsum is special because it does not take a fixed number of # variables: assert umath.fsum([x, x]).nominal_value == -3 # The same exceptions should be generated when numbers with uncertainties # are used: ## !! The Nose testing framework seems to catch an exception when ## it is aliased: "exc = OverflowError; ... except exc:..." ## surprisingly catches OverflowError. So, tests are written in a ## version-specific manner (until the Nose issue is resolved). if sys.version_info < (2, 6): try: math.log(0) except OverflowError, err_math: # "as", for Python 2.6+ pass else: raise Exception('OverflowError exception expected') try: umath.log(0) except OverflowError, err_ufloat: # "as", for Python 2.6+ assert err_math.args == err_ufloat.args
def getCocktailSum(e0, e1, eCocktail, uCocktail): """get the cocktail sum for a given data bin range""" # get mask and according indices mask = (eCocktail >= e0) & (eCocktail <= e1) # data bin range wider than single cocktail bin if np.any(mask): idx = getMaskIndices(mask) # determine coinciding flags eCl, eCu = eCocktail[idx[0]], eCocktail[idx[1]] not_coinc_low, not_coinc_upp = (eCl != e0), (eCu != e1) # get cocktail sum in data bin (always w/o last bin) uCocktailSum = fsum(uCocktail[mask[:-1]][:-1]) logging.debug(' sum: {}'.format(uCocktailSum)) # get correction for non-coinciding edges if not_coinc_low: eCl_bw = eCl - eCocktail[idx[0]-1] corr_low = (eCl - e0) / eCl_bw abs_corr_low = float(corr_low) * uCocktail[idx[0]-1] uCocktailSum += abs_corr_low logging.debug((' low: %g == %g -> %g (%g) -> %g -> {} -> {}' % ( e0, eCl, eCl - e0, eCl_bw, corr_low )).format(abs_corr_low, uCocktailSum)) if not_coinc_upp: if idx[1]+1 < len(eCocktail): eCu_bw = eCocktail[idx[1]+1] - eCu corr_upp = (e1 - eCu) / eCu_bw abs_corr_upp = float(corr_upp) * uCocktail[idx[1]] else:# catch last index (quick fix!) abs_corr_upp = eCu_bw = corr_upp = 0 uCocktailSum += abs_corr_upp logging.debug((' upp: %g == %g -> %g (%g) -> %g -> {} -> {}' % ( e1, eCu, e1 - eCu, eCu_bw, corr_upp )).format(abs_corr_upp, uCocktailSum)) else: mask = (eCocktail >= e0) idx = getMaskIndices(mask) # only use first index # catch if already at last index if idx[0] == idx[1] and idx[0] == len(eCocktail)-1: corr = (e1 - e0) / (eCocktail[idx[0]] - eCocktail[idx[0]-1]) uCocktailSum = float(corr) * uCocktail[idx[0]-1] else: # default case corr = (e1 - e0) / (eCocktail[idx[0]+1] - eCocktail[idx[0]]) uCocktailSum = float(corr) * uCocktail[idx[0]] logging.debug(' sum: {}'.format(uCocktailSum)) return uCocktailSum
def test_math_module(): "Operations with the math module" x = ufloat(-1.5, 0.1) # The exponent must not be differentiated, when calculating the # following (the partial derivative with respect to the exponent # is not defined): assert (x**2).nominal_value == 2.25 # Regular operations are chosen to be unchanged: assert isinstance(umath.sin(3), float) # Python >=2.6 functions: if sys.version_info >= (2, 6): # factorial() must not be "damaged" by the umath module, so as # to help make it a drop-in replacement for math (even though # factorial() does not work on numbers with uncertainties # because it is restricted to integers, as for # math.factorial()): assert umath.factorial(4) == 24 # fsum is special because it does not take a fixed number of # variables: assert umath.fsum([x, x]).nominal_value == -3 # Functions that give locally constant results are tested: they # should give the same result as their float equivalent: for name in umath.locally_cst_funcs: try: func = getattr(umath, name) except AttributeError: continue # Not in the math module, so not in umath either assert func(x) == func(x.nominal_value) # The type should be left untouched. For example, isnan() # should always give a boolean: assert type(func(x)) == type(func(x.nominal_value)) # The same exceptions should be generated when numbers with uncertainties # are used: ## !! The Nose testing framework seems to catch an exception when ## it is aliased: "exc = OverflowError; ... except exc:..." ## surprisingly catches OverflowError. So, tests are written in a ## version-specific manner (until the Nose issue is resolved). if sys.version_info < (2, 6): try: math.log(0) except OverflowError, err_math: # "as", for Python 2.6+ pass else: raise Exception('OverflowError exception expected') try: umath.log(0) except OverflowError, err_ufloat: # "as", for Python 2.6+ assert err_math.args == err_ufloat.args
def test_math_module(): "Operations with the math module" x = ufloat(-1.5, 0.1) # The exponent must not be differentiated, when calculating the # following (the partial derivative with respect to the exponent # is not defined): assert (x**2).nominal_value == 2.25 # Regular operations are chosen to be unchanged: assert isinstance(umath.sin(3), float) # factorial() must not be "damaged" by the umath module, so as # to help make it a drop-in replacement for math (even though # factorial() does not work on numbers with uncertainties # because it is restricted to integers, as for # math.factorial()): assert umath.factorial(4) == 24 # fsum is special because it does not take a fixed number of # variables: assert umath.fsum([x, x]).nominal_value == -3 # Functions that give locally constant results are tested: they # should give the same result as their float equivalent: for name in umath.locally_cst_funcs: try: func = getattr(umath, name) except AttributeError: continue # Not in the math module, so not in umath either assert func(x) == func(x.nominal_value) # The type should be left untouched. For example, isnan() # should always give a boolean: assert type(func(x)) == type(func(x.nominal_value)) # The same exceptions should be generated when numbers with uncertainties # are used: ## !! The Nose testing framework seems to catch an exception when ## it is aliased: "exc = OverflowError; ... except exc:..." ## surprisingly catches OverflowError. So, tests are written in a ## version-specific manner (until the Nose issue is resolved). try: math.log(0) except ValueError as err_math: # Python 3 does not make exceptions local variables: they are # restricted to their except block: err_math_args = err_math.args else: raise Exception('ValueError exception expected') try: umath.log(0) except ValueError as err_ufloat: assert err_math_args == err_ufloat.args else: raise Exception('ValueError exception expected') try: umath.log(ufloat(0, 0)) except ValueError as err_ufloat: assert err_math_args == err_ufloat.args else: raise Exception('ValueError exception expected') try: umath.log(ufloat(0, 1)) except ValueError as err_ufloat: assert err_math_args == err_ufloat.args else: raise Exception('ValueError exception expected')
def gp_ptspec(): """example for a 2D-panel plot etc.""" fenergies = ['19', '27', '39', '62', ]# '200'] nen = len(fenergies) mee_keys = ['pi0', 'LMR', 'omega', 'phi', 'IMR', 'jpsi'] #mee_keys = ['LMR', ] mee_dict = OrderedDict((k,'') for k in mee_keys) yscale = { '200': '300', '62': '5000', '39': '50', '27': '0.3', '19': '0.001' } inDir, outDir = getWorkDirs() data, data_avpt, dpt_dict = {}, {}, {} yvals, yvalsPt = [], [] scale = { '19': 1.3410566491548412, '200': 1.0, '39': 1.2719203877292842, '27': 1.350873678084769, '62': 1.2664666321635087 } lmr_label = None for filename in os.listdir(inDir): # import data file_url = os.path.join(inDir, filename) filebase = os.path.splitext(filename)[0] # unique energy, mee_name, mee_range, data_type = splitFileName(filebase) if mee_name == 'LMR': mee_range_split = map(float, mee_range.split('-')) lmr_label = 'LMR: %g < M_{ee} < %g GeV/c^{2}' % ( mee_range_split[0], mee_range_split[1] ) if energy == '200': continue if mee_name not in mee_keys: continue mee_dict[mee_name] = mee_range data[filebase] = np.loadtxt(open(file_url, 'rb')) if data_type == 'data': #print data[filebase] data[filebase] = data[filebase][:-1] # skip mT<0.4 point if energy == '200': data[filebase][:,(1,3,4)] /= 0.5 # calculate average pT first mask = (data[filebase][:,0] > 0.4) & (data[filebase][:,0] < 2.2) avpt_data = data[filebase][mask] pTs = avpt_data[:,0] wghts = avpt_data[:,1] probs = unp.uarray(avpt_data[:,1], avpt_data[:,3]) # dN/pT probs /= umath.fsum(probs) # probabilities avpt = umath.fsum(pTs*probs) logging.info(('%s: {} %g' % ( filebase, np.average(pTs, weights = wghts) )).format(avpt)) # TODO: syst. uncertainties # save datapoint for average pT and append to yvalsPt for yaxis range dp = [ float(getEnergy4Key(energy)), avpt.nominal_value, 0., avpt.std_dev, 0. ] avpt_key = mee_name if data_type == 'cocktail': avpt_key += '_c' if data_type == 'medium': avpt_key += '_m' if data_type == 'mediumMedOnly': avpt_key += '_mMed' if data_type == 'mediumQgpOnly': avpt_key += '_mQgp' if avpt_key in data_avpt: data_avpt[avpt_key].append(dp) else: data_avpt[avpt_key] = [ dp ] yvalsPt.append(avpt.nominal_value) # now adjust data for panel plot and append to yvals if data_type != 'data': data[filebase][:,(1,3,4)] /= scale[energy] data[filebase][:,(1,3,4)] *= float(yscale[energy]) if data_type == 'cocktail' or fnmatch(data_type, '*medium*'): data[filebase][:,2:] = 0. yvals += [v for v in data[filebase][:,1] if v > 0] # prepare dict for panel plot dpt_dict_key = getSubplotTitle(mee_name, mee_range) if dpt_dict_key not in dpt_dict: ndsets = nen*2 # TODO: currently only 19/39/62 medium avail. w/ med/qgp/tot for each # July14: all energies available; TODO: fix dsidx if mee_name == 'LMR': ndsets += 4*3 dpt_dict[dpt_dict_key] = [ [None]*ndsets, [None]*ndsets, [None]*ndsets ] enidx = fenergies.index(energy) dsidx = enidx if fnmatch(data_type, '*medium*'): # 19: 0-2, 27: 3-5, 39: 6-8, 62: 9-11 dsidx = (energy=='19')*0 + (energy=='27')*3 + (energy=='39')*6 + (energy=='62')*9 dsidx += (data_type=='mediumQgpOnly')*0 + (data_type=='mediumMedOnly')*1 dsidx += (data_type=='medium')*2 else: dsidx += int(mee_name == 'LMR') * 4 * 3 # number of medium calc avail. dsidx += int(data_type == 'data') * len(fenergies) dpt_dict[dpt_dict_key][0][dsidx] = data[filebase] # data if data_type == 'data': # properties dpt_dict[dpt_dict_key][1][dsidx] = 'lt 1 lw 4 ps 1.5 lc %s pt 18' % default_colors[enidx] elif data_type == 'medium': dpt_dict[dpt_dict_key][1][dsidx] = 'with lines lt 1 lw 5 lc %s' % default_colors[enidx] else: dpt_dict[dpt_dict_key][1][dsidx] = 'with lines lt %d lw 5 lc %s' % ( 2+(data_type=='mediumMedOnly')+(data_type=='mediumQgpOnly')*2, default_colors[enidx] ) dpt_dict[dpt_dict_key][2][dsidx] = ' '.join([ # legend titles getEnergy4Key(energy), 'GeV', '{/Symbol \264} %g' % ( Decimal(yscale[energy])#.as_tuple().exponent ) ]) if data_type == 'data' else '' # use mass range in dict key to sort dpt_dict with increasing mass plot_key_order = dpt_dict.keys() plot_key_order.sort(key=lambda x: float(x.split(':')[1].split('-')[0])) # sort data_avpt by energy and apply x-shift for better visibility for k in data_avpt: data_avpt[k].sort(key=lambda x: x[0]) energies = [ dp[0] for dp in data_avpt[mee_keys[0]] ] energies.append(215.) # TODO: think of better upper limit linsp = {} for start,stop in zip(energies[:-1],energies[1:]): linsp[start] = np.linspace(start, stop, num = 4*len(mee_keys)) for k in data_avpt: key = k.split('_')[0] for i in xrange(len(data_avpt[k])): data_avpt[k][i][0] = linsp[energies[i]][mee_keys.index(key)] # make panel plot yMin, yMax = 0.5*min(yvals), 3*max(yvals) make_panel( dpt_dict = OrderedDict((k,dpt_dict[k]) for k in plot_key_order), name = os.path.join(outDir, 'ptspec'), ylabel = '1/N@_{mb}^{evt} d^{2}N@_{ee}^{acc.}/dp_{T}dM_{ee} (c^3/GeV^2)', xlabel = 'dielectron transverse momentum, p_{T} (GeV/c)', ylog = True, xr = [0, 2.2], yr = [1e-9, 1e4], #lmargin = 0.12, bmargin = 0.10, tmargin = 1., rmargin = 1., key = ['bottom left', 'samplen 0.5', 'width -2', 'opaque'], arrow_bar = 0.002, layout = '3x2', size = '8in,8in' ) #make plot for LMR spectra only #lmr_key = getSubplotTitle('LMR', '0.4-0.76') #if energy == '200': # lmr_key = getSubplotTitle('LMR', '0.3-0.76') #pseudo_point = np.array([[-1,0,0,0,0]]) #model_titles = ['Cocktail + Model', 'Cocktail', 'in-Medium', 'QGP'] #model_props = [ # 'with lines lc %s lw 5 lt %d' % (default_colors[-2], i+1) # for i in xrange(len(model_titles)) #] #make_plot( # data = dpt_dict[lmr_key][0] + [ pseudo_point ] * len(model_titles), # properties = dpt_dict[lmr_key][1] + model_props, # titles = dpt_dict[lmr_key][2] + model_titles, # name = os.path.join(outDir, 'ptspecLMR'), # ylabel = '1/N@_{mb}^{evt} d^{2}N@_{ee}^{acc.}/dp_{T}dM_{ee} (c^3/GeV^2)', # xlabel = 'dielectron transverse momentum, p_{T} (GeV/c)', # ylog = True, xr = [0, 2.0], yr = [1e-8, 100], # lmargin = 0.15, bmargin = 0.08, rmargin = 0.98, tmargin = 0.84, # key = ['maxrows 4', 'samplen 0.7', 'width -2', 'at graph 1.,1.2'], # arrow_bar = 0.005, size = '10in,13in', # labels = { # 'stat. errors only': [0.7,0.95,False], lmr_label: [0.05,0.03,False], # 'STAR Preliminary': [0.05,0.07,False], # } #) # make mean pt plot #yMinPt, yMaxPt = 0.95*min(yvalsPt), 1.05*max(yvalsPt) #make_plot( # data = [ # cocktail # np.array(data_avpt[k+'_c']) for k in mee_keys # ] + [ # medium # np.array(data_avpt['LMR_m']) # ] + [ # data # np.array(data_avpt[k]) for k in mee_keys # ], # properties = [ # 'with lines lt 1 lw 4 lc %s' % default_colors[i if i < 5 else i+1] # for i in xrange(len(mee_keys)) # ] + [ # 'with lines lt 2 lw 4 lc %s' % default_colors[mee_keys.index('LMR')] # ] + [ # 'lt 1 lw 4 ps 1.5 lc %s pt 18' % default_colors[i if i < 5 else i+1] # for i in xrange(len(mee_keys)) # ], # titles = [ getMeeLabel(k) for k in mee_keys ] + ['']*(len(mee_keys)+1), # name = os.path.join(outDir, 'meanPt'), # xlabel = '{/Symbol \326}s_{NN} (GeV)', # ylabel = '{/Symbol \341}p_{T}{/Symbol \361} in STAR Acceptance (GeV/c)', # xlog = True, xr = [17,220], yr = [yMinPt, yMaxPt], size = '11in,9in', # key = [ 'maxrows 1', 'at graph 1, 1.1' ], # lmargin = 0.11, bmargin = 0.11, tmargin = 1., rmargin = 1., # gpcalls = [ # 'format x "%g"', # 'xtics (20,"" 30, 40,"" 50, 60,"" 70,"" 80,"" 90, 100, 200)', # ] #) ## make mean pt plot for LMR only #make_plot( # data = [ # np.array(data_avpt['LMR_c']), # np.array(data_avpt['LMR_m']), # np.array(data_avpt['LMR']) # ], # properties = [ # 'with lines lt 2 lw 4 lc %s' % default_colors[0], # 'with lines lt 1 lw 4 lc %s' % default_colors[0], # 'lt 1 lw 4 ps 1.5 lc %s pt 18' % default_colors[0] # ], # titles = [ # 'cocktail', 'HMBT', getMeeLabel('data') # ], # name = os.path.join(outDir, 'meanPtLMR'), # xlabel = '{/Symbol \326}s_{NN} (GeV)', # ylabel = 'LMR {/Symbol \341}p_{T}{/Symbol \361} in STAR Acceptance (GeV/c)', # lmargin = 0.17, bmargin = 0.15, tmargin = 0.95, xlog = True, xr = [17,80], # yr = [0.65,1.05], #yr = [yMinPt, yMaxPt], # key = [ 'bottom right' ], # gpcalls = [ # 'format x "%g"', # 'xtics (20, 30, 40,"" 50, 60,"" 70,"" 80,"" 90, 100, 200)', # ], # labels = { # 'stat. errors only': [0.7,0.95,False], lmr_label: [0.05,0.07,False], # '0.4 < p_{T} < 2.2 GeV/c': [0.05,0.14,False] # } #) return 'done'
def test_math_module(): "Operations with the math module" x = uncertainties.ufloat((-1.5, 0.1)) # The exponent must not be differentiated, when calculating the # following (the partial derivative with respect to the exponent # is not defined): assert (x**2).nominal_value == 2.25 # Regular operations are chosen to be unchanged: assert isinstance(umath.sin(3), float) # Python >=2.6 functions: if sys.version_info >= (2, 6): # factorial() must not be "damaged" by the umath module, so as # to help make it a drop-in replacement for math (even though # factorial() does not work on numbers with uncertainties # because it is restricted to integers, as for # math.factorial()): assert umath.factorial(4) == 24 # Boolean functions: assert not umath.isinf(x) # Comparison, possibly between an AffineScalarFunc object and a # boolean, which makes things more difficult for this code: assert umath.isinf(x) == False # fsum is special because it does not take a fixed number of # variables: assert umath.fsum([x, x]).nominal_value == -3 # The same exceptions should be generated when numbers with uncertainties # are used: ## !! The Nose testing framework seems to catch an exception when ## it is aliased: "exc = OverflowError; ... except exc:..." ## surprisingly catches OverflowError. So, tests are written in a ## version-specific manner (until the Nose issue is resolved). if sys.version_info < (2, 6): try: math.log(0) except OverflowError(err_math): # "as", for Python 2.6+ pass else: raise Exception('OverflowError exception expected') try: umath.log(0) except OverflowError(err_ufloat): # "as", for Python 2.6+ assert err_math.args == err_ufloat.args else: raise Exception('OverflowError exception expected') try: umath.log(uncertainties.ufloat((0, 0))) except OverflowError( err_ufloat): # "as", for Python 2.6+ assert err_math.args == err_ufloat.args else: raise Exception('OverflowError exception expected') try: umath.log(uncertainties.ufloat((0, 1))) except OverflowError( err_ufloat): # "as", for Python 2.6+ assert err_math.args == err_ufloat.args else: raise Exception('OverflowError exception expected') elif sys.version_info < (3,): try: math.log(0) except ValueError( err_math): pass else: raise Exception('ValueError exception expected') try: umath.log(0) except ValueError( err_ufloat): assert err_math.args == err_ufloat.args else: raise Exception('ValueError exception expected') try: umath.log(uncertainties.ufloat((0, 0))) except ValueError( err_ufloat): assert err_math.args == err_ufloat.args else: raise Exception('ValueError exception expected') try: umath.log(uncertainties.ufloat((0, 1))) except ValueError( err_ufloat): assert err_math.args == err_ufloat.args else: raise Exception('ValueError exception expected') else: # Python 3+ # !!! The tests should be made to work with Python 3 too! pass