diff --git a/src/simweights/_fluxes.py b/src/simweights/_fluxes.py index 7354a15..9571781 100644 --- a/src/simweights/_fluxes.py +++ b/src/simweights/_fluxes.py @@ -23,7 +23,7 @@ from numpy import asarray, bool_, broadcast_arrays, exp, float64, genfromtxt, int32, piecewise, sqrt from numpy import sum as nsum -from scipy.interpolate import CubicSpline # pylint: disable=import-error +from scipy.interpolate import PchipInterpolator # pylint: disable=import-error from ._pdgcode import PDGCode @@ -345,7 +345,7 @@ def __init__(self: GlobalSplineFitBase) -> None: self._funcs = [] for z_low, z_high in self.groups: self._funcs.append( - CubicSpline(energy, nsum(elements[z_low - 1 : z_high], axis=0), extrapolate=False, axis=0), + PchipInterpolator(energy, nsum(elements[z_low - 1 : z_high], axis=0), extrapolate=False, axis=0), ) diff --git a/tests/flux_values.json b/tests/flux_values.json index 4ad0752..28b0e90 100644 --- a/tests/flux_values.json +++ b/tests/flux_values.json @@ -388,195 +388,195 @@ 1.363932529828272e-18, 8.020822568423635e-22, 6.771998772413694e-25 ] }, - "GlobalSplineFit": { - "2212": [ - 0.04585449431423036, 7.789783588789647e-5, 1.9061451942412818e-7, - 3.913540332159069e-10, 9.171060628426788e-13, 7.933042844441958e-16, - 6.150944574738882e-19, 1.1644454409750712e-21, 3.79633922979983e-25 - ], - "1000020040": [ - 0.03192055738163634, 6.467091543445148e-5, 1.7585497031971908e-7, - 4.1712912655120846e-10, 8.035956231065464e-13, 9.732708568816786e-16, - 5.416659594465701e-19, 5.335274557933634e-22, 1.3445890398933065e-24 - ], - "1000030070": [ - 0.0002609510766336242, 2.274343425025973e-7, 3.724499675892747e-10, - 1.2226323182468344e-12, 2.326122127893717e-15, 2.6089553804676702e-18, - 1.9856870864908692e-21, 6.247815833484682e-25, 2.2885149941116613e-27 - ], - "1000040090": [ - 0.00023969518248799946, 2.248623529169532e-7, 1.4261186841218902e-10, - 1.9644070292100862e-13, 4.0782082610440713e-16, 4.550395466907163e-19, - 4.1839264250228577e-22, 1.5311767319988435e-25, 2.280822430442393e-28 - ], - "1000050110": [ - 0.000968714217672618, 9.467142665814123e-7, 1.3405402027976345e-9, - 4.326897059729931e-12, 9.619922235497481e-15, 1.1026107336077989e-17, - 1.1548329314711419e-20, 4.581043097262913e-24, 3.3622755953575e-27 - ], - "1000060120": [ - 0.005547076555520563, 1.2799697268073433e-5, 2.775292712861645e-8, - 7.040811588027278e-11, 1.5499880427915251e-13, 1.8438188890902068e-16, - 2.1044142454275582e-19, 8.793113074128186e-23, 3.75450835257545e-26 - ], - "1000070140": [ - 0.0016175250377955634, 1.5408108919107632e-6, 5.2594426531501526e-9, - 1.9972292339110316e-11, 4.920346462768073e-14, 6.085634314314811e-17, - 7.344323232767663e-20, 3.186888775002853e-23, 9.268415283335083e-27 - ], - "1000080160": [ - 0.008037479012892586, 2.2377178154165915e-5, 4.499012505079767e-8, - 1.356709485146817e-10, 3.47928631771247e-13, 4.468202911122647e-16, - 5.588404861111031e-19, 2.500061770046132e-22, 5.70217429380718e-26 - ], - "1000090190": [ - 0.00017633665164879103, 1.295902740285884e-7, 2.5939273090464554e-10, - 7.640766083410931e-13, 2.0271036433836476e-15, 2.6950371412987985e-18, - 3.4442614593183124e-21, 1.58270873137e-24, 3.1513267340344177e-28 - ], - "1000100200": [ - 0.0016484509141859532, 4.350652380484184e-6, 7.587173094675204e-9, - 2.9082222528530805e-11, 3.574255602067578e-14, 5.404263204830641e-17, - 7.867371742513358e-20, 5.329142271780384e-24, 3.892172091541455e-27 - ], - "1000110230": [ - 0.0003723283605645931, 4.324524241218175e-7, 9.075298448549944e-10, - 2.0755785024128175e-12, 2.5738187808059842e-15, 3.818558949676035e-18, - 5.8209594459030345e-21, 4.906870823836711e-25, 2.179404327539832e-28 - ], - "1000120240": [ - 0.002629970422798157, 7.619075013149221e-6, 1.4023449103133063e-8, - 7.01742332526235e-11, 9.512379390719279e-14, 1.3875892504072645e-16, - 2.193633132655073e-19, 2.2297147905861442e-23, 6.264740927180148e-27 - ], - "1000130270": [ - 0.0005058248502310309, 1.1962164774198692e-6, 2.448289506906274e-9, - 5.8350451726336046e-12, 7.39287379194231e-15, 1.0617114709408009e-17, - 1.72709154477702e-20, 2.0676579882146996e-24, 3.7815235495612897e-28 - ], - "1000140280": [ - 0.002462214826779496, 8.936414025602394e-6, 1.4276570289164372e-8, - 5.73924482693435e-11, 8.40364214346626e-14, 1.1906679753105584e-16, - 1.9800654386749318e-19, 2.737246748711825e-23, 3.3272381977077325e-27 - ], - "1000150310": [ - 0.00011311369346187186, 2.555247000665526e-7, 4.564798801106201e-10, - 1.1212464033565257e-12, 1.454086239320666e-15, 2.0340019245956552e-18, - 3.446435527330383e-21, 5.413193737710945e-25, 4.436255825161067e-29 - ], - "1000160320": [ - 0.0005573597560203442, 1.785184626054518e-6, 3.3075440810664735e-9, - 8.222857377644762e-12, 1.0788619233611736e-14, 1.4912899340136912e-17, - 2.5643700686327068e-20, 4.5199869327196665e-24, 2.5191144954193517e-28 - ], - "1000170350": [ - 0.00013185513628894763, 3.0151430266955243e-7, 5.043068209288322e-10, - 1.2675329868106502e-12, 1.682705262769923e-15, 2.3017773216178627e-18, - 4.0044770390387e-21, 7.83697143877456e-25, 2.982583223870045e-29 - ], - "1000180400": [ - 0.0002736704827819255, 8.091255666340449e-7, 1.6693603222732777e-9, - 4.238310322810843e-12, 5.6911759762526175e-15, 7.710804481576707e-18, - 1.3542562490412449e-20, 2.91537043507859e-24, 7.577548753965019e-29 - ], - "1000190390": [ - 0.00019280313245155858, 4.831576958054902e-7, 6.653287088767548e-10, - 1.7013436437992835e-12, 2.311457775301533e-15, 3.103299916508759e-18, - 5.489065107802631e-21, 1.2913500036495106e-24, 2.2809516139886024e-29 - ], - "1000200400": [ - 0.00047407066427825056, 1.6153921904462592e-6, 2.8775021605895078e-9, - 7.409850987632564e-12, 1.0178819813921696e-14, 1.3556218312694959e-17, - 2.411961092261588e-20, 6.154137178979787e-24, 7.330277950033355e-29 - ], - "1000210450": [ - 0.00010754041112486113, 3.1127906797829883e-7, 5.733939578179578e-10, - 1.4858777235097754e-12, 2.0647380119410574e-15, 2.7290847658820963e-18, - 4.877645994946823e-21, 1.3422908293942618e-24, 1.0638036744202279e-29 - ], - "1000220480": [ - 0.0003625442405172111, 1.1413042456128601e-6, 1.7194739470364572e-9, - 4.480478285948322e-12, 6.295357855848949e-15, 8.258843993802465e-18, - 1.480811919067405e-20, 4.3767497374019796e-24, 2.264741019463204e-29 - ], - "1000230510": [ - 0.00018377102628343363, 6.72215406996513e-7, 9.541844709310794e-10, - 2.4986419036958105e-12, 3.5493311906934634e-15, 4.62551938776331e-18, - 8.3160189078844e-21, 2.625948047181121e-24, 8.64851015643081e-30 - ], - "1000240520": [ - 0.00039501688833914843, 1.5590459236978165e-6, 2.668965173099529e-9, - 7.0144937205175394e-12, 1.0070655920620955e-14, 1.3043211009599968e-17, - 2.348697100466661e-20, 7.895004543577372e-24, 1.5999081758208345e-29 - ], - "1000250550": [ - 0.00025708515296602184, 1.3996679760796709e-6, 2.21982102666149e-9, - 5.85490230179275e-12, 8.494162738735532e-15, 1.0941186469015683e-17, - 1.972608752509139e-20, 7.031165783304692e-24, 8.363658587293129e-30 - ], - "1000260560": [ - 0.0028428595256566394, 1.954816204878481e-5, 4.24613569531508e-8, - 1.122891296131649e-10, 1.645916773145853e-13, 2.108279047243917e-16, - 3.8027230541641873e-19, 1.4331817296462265e-22, 9.390749178637021e-29 - ], - "1000270590": [ - 1.5357963305288045e-5, 8.960619931228483e-8, 1.4097176121377432e-10, - 3.7390711787987645e-13, 5.535560256007192e-16, 7.056420486555915e-19, - 1.2724151377412506e-21, 5.0570888886638785e-25, 1.6641433783029013e-31 - ], - "1000280590": [ - 0.00014283301919948755, 1.1987507320697202e-6, 1.739591195855993e-9, - 4.619667427337427e-12, 6.908847283690165e-15, 8.766393677915383e-18, - 1.5799727859114336e-20, 6.6030571725042165e-24, 9.498125766747713e-31 - ] - }, - "GlobalSplineFit5Comp": { - "2212": [ - 0.04585449431423036, 7.789783588789647e-5, 1.9061451942412818e-7, - 3.913540332159069e-10, 9.171060628426788e-13, 7.933042844441958e-16, - 6.150944574738882e-19, 1.1644454409750712e-21, 3.79633922979983e-25 - ], - "1000020040": [ - 0.03338991785843059, 6.606992639645244e-5, 1.7771057235851816e-7, - 4.2287509663210625e-10, 8.159494882960421e-13, 9.87360959144915e-16, - 5.556183684902746e-19, 5.388863981471748e-22, 1.3504679127258198e-24 - ], - "1000070140": [ - 0.01739919653260805, 4.16303813927847e-5, 8.675659050299912e-8, - 2.5797323437334945e-10, 5.924743791229458e-13, 7.526147513037147e-16, - 9.306640813118904e-19, 3.772087335814577e-22, 1.0826048694486026e-25 - ], - "1000130270": [ - 0.005711123793270556, 1.800723021623804e-5, 3.1204788779314336e-8, - 1.3452297309795713e-10, 1.8800717537311834e-13, 2.7047683920578597e-16, - 4.38087208108101e-19, 5.2278592754965484e-23, 1.001449403809562e-26 - ], - "1000260560": [ - 0.0057939343800136305, 2.9715655250072123e-5, 5.976220938364594e-8, - 1.5683732598520708e-10, 2.262722571198835e-13, 2.9271639177164597e-16, - 5.2555897942362e-19, 1.8275958248865348e-22, 6.139956685254725e-28 - ] - }, - "GlobalSplineFit_IT": { - "2212": [ - 0.04585449431423036, 7.789783588789647e-5, 1.9061451942412818e-7, - 3.913540332159069e-10, 9.171060628426788e-13, 7.933042844441958e-16, - 6.150944574738882e-19, 1.1644454409750712e-21, 3.79633922979983e-25 - ], - "1000020040": [ - 0.03192055738163634, 6.467091543445148e-5, 1.7585497031971908e-7, - 4.1712912655120846e-10, 8.035956231065464e-13, 9.732708568816786e-16, - 5.416659594465701e-19, 5.335274557933634e-22, 1.3445890398933065e-24 - ], - "1000080160": [ - 0.018496228648837695, 4.259693993066384e-5, 8.770466269694322e-8, - 2.6164362595183446e-10, 6.022544255316352e-13, 7.62886294617275e-16, - 9.38795530909692e-19, 3.8207698885288526e-22, 1.1392141934461967e-25 - ], - "1000260560": [ - 0.012020219553048266, 4.9354088622501694e-5, 9.361411920367124e-8, - 2.9805554501291447e-10, 4.2376209855749804e-13, 5.757781836050234e-16, - 9.852668748367386e-19, 2.4213191949850687e-22, 1.0847379951951749e-26 - ] - } +"GlobalSplineFit": { +"2212": [ + 0.04585496359243566, 7.789996992661386e-05, 1.906109553848489e-07, + 3.9137200583417463e-10, 9.171690470565732e-13, 7.9337267934623845e-16, + 6.150893025573896e-19, 1.1644398925774302e-21, 3.796441872307821e-25 +], +"1000020040": [ + 0.031920527947683505, 6.467243917299695e-05, 1.7585162032645172e-07, + 4.171457677567575e-10, 8.036436027741618e-13, 9.733794517983287e-16, + 5.416652540933012e-19, 5.335279433164195e-22, 1.3446258153589729e-24 +], +"1000030070": [ + 0.0002609478749035958, 2.2744181376335508e-07, 3.7244275084429075e-10, + 1.2226627885835615e-12, 2.326285556967646e-15, 2.609209514465516e-18, + 1.985705334354709e-21, 6.247806937782244e-25, 2.288552189314752e-27 +], +"1000040090": [ + 0.00023969486917087447, 2.2486667643323023e-07, 1.4260659713783722e-10, + 1.9644825553223522e-13, 4.0784363453035937e-16, 4.550764213625319e-19, + 4.183969993401365e-22, 1.5311712855266297e-25, 2.2808745870829323e-28 +], +"1000050110": [ + 0.0009687285931260943, 9.467722931340138e-07, 1.3405062651893623e-09, + 4.3270996494274495e-12, 9.619873788420451e-15, 1.1027014966285629e-17, + 1.1548484229827861e-20, 4.581046532727734e-24, 3.362297395084582e-27 +], +"1000060120": [ + 0.005547030533546444, 1.2800045254962681e-05, 2.7752483423338075e-08, + 7.041101503478009e-11, 1.5501286997907851e-13, 1.8440371643582626e-16, + 2.1044063971298339e-19, 8.793115347092041e-23, 3.7545649803445715e-26 +], +"1000070140": [ + 0.001617531646600424, 1.540872714695563e-06, 5.2593817404007e-09, + 1.9972669459508026e-11, 4.9206686957054364e-14, 6.086137708182563e-17, + 7.344353696283388e-20, 3.1868896293810046e-23, 9.268489920902913e-27 +], +"1000080160": [ + 0.008037482717773091, 2.2378014654644375e-05, 4.498941571903058e-08, + 1.3567703493806215e-10, 3.479464195576382e-13, 4.46860352363075e-16, + 5.588382134679474e-19, 2.500054166418075e-22, 5.702264361959419e-26 +], +"1000090190": [ + 0.00017633315313676093, 1.2959248627457465e-07, 2.593891426561183e-10, + 7.641084621878149e-13, 2.0272346485269955e-15, 2.695271111613686e-18, + 3.444232074534516e-21, 1.5827049719002872e-24, 3.1514093102634206e-28 +], +"1000100200": [ + 0.0016484067012100024, 4.350806040971699e-06, 7.587052558444333e-09, + 2.9081185843832247e-11, 3.57446620048406e-14, 5.404722086199983e-17, + 7.867347690873547e-20, 5.3291619213316326e-24, 3.892159783291285e-27 +], +"1000110230": [ + 0.000372332740212037, 4.3248318269372535e-07, 9.075152308184956e-10, + 2.0756536218462697e-12, 2.574058898372109e-15, 3.818889032873567e-18, + 5.820941891077295e-21, 4.9068880069591675e-25, 2.1793401611655557e-28 +], +"1000120240": [ + 0.0026299636534375228, 7.61931472998443e-06, 1.4023267057724496e-08, + 7.017725615973831e-11, 9.513143753399047e-14, 1.387751950661416e-16, + 2.1935777753985015e-19, 2.229727620233974e-23, 6.26504815102405e-27 +], +"1000130270": [ + 0.0005058226448408322, 1.196251867825368e-06, 2.4482575184379857e-09, + 5.835298374512406e-12, 7.393352199737378e-15, 1.0618361402213613e-17, + 1.7270791029468116e-20, 2.0676591747013924e-24, 3.7815586253615267e-28 +], +"1000140280": [ + 0.0024622078224600585, 8.936714513329986e-06, 1.4276436287265315e-08, + 5.739459272911544e-11, 8.404288303396298e-14, 1.19075360781316e-16, + 1.9801003585018473e-19, 2.7372472969293057e-23, 3.327200863059742e-27 +], +"1000150310": [ + 0.00011311581260060748, 2.555368310076864e-07, 4.564724898931961e-10, + 1.121332485999586e-12, 1.4542485371798075e-15, 2.0341891337280484e-18, + 3.446418734036004e-21, 5.413198018159857e-25, 4.4362421494764494e-29 +], +"1000160320": [ + 0.0005573594252918286, 1.785221088423684e-06, 3.307495302124531e-09, + 8.223185402300863e-12, 1.0789464379335231e-14, 1.4914506367160837e-17, + 2.5643612234959898e-20, 4.519986205155354e-24, 2.5191028674728866e-28 +], +"1000170350": [ + 0.00013185040720988298, 3.015251925948764e-07, 5.04299182378528e-10, + 1.2676207336452756e-12, 1.6828093151592873e-15, 2.301951034710983e-18, + 4.004476069091409e-21, 7.836975002015426e-25, 2.9825213833533743e-29 +], +"1000180400": [ + 0.00027366940780253543, 8.091485131768592e-07, 1.6693186357289724e-09, + 4.238542730443356e-12, 5.691542886236795e-15, 7.711388121335978e-18, + 1.3542489316777137e-20, 2.915369087780524e-24, 7.577431737160115e-29 +], +"1000190390": [ + 0.0001928007021330019, 4.831755151993833e-07, 6.653189887565927e-10, + 1.701409700111199e-12, 2.3115706538131395e-15, 3.103502594674569e-18, + 5.4890152798641356e-21, 1.2913467461192819e-24, 2.2809097982761635e-29 +], +"1000200400": [ + 0.00047406636900559765, 1.6154292591189026e-06, 2.877447404703096e-09, + 7.410171936535992e-12, 1.0179315467339414e-14, 1.3557679777126891e-17, + 2.4119344771701342e-20, 6.154138634398928e-24, 7.330049241279684e-29 +], +"1000210450": [ + 0.00010753985791325448, 3.1128829018461445e-07, 5.73385371047877e-10, + 1.4859076273550833e-12, 2.0649090825514866e-15, 2.7292330136890018e-18, + 4.877611840407419e-21, 1.3422900334813612e-24, 1.0637653147448717e-29 +], +"1000220480": [ + 0.00036254357572723146, 1.141341131540081e-06, 1.7194551591301397e-09, + 4.480702494993713e-12, 6.295801064994948e-15, 8.259548903533721e-18, + 1.4808388704973347e-20, 4.376761484717892e-24, 2.2646342798972115e-29 +], +"1000230510": [ + 0.00018377093301307374, 6.722396707511286e-07, 9.541713302163614e-10, + 2.498789173121882e-12, 3.5495410654769326e-15, 4.62588058435377e-18, + 8.315966546969282e-21, 2.6259474184857977e-24, 8.648150221633039e-30 +], +"1000240520": [ + 0.0003950101691020963, 1.559087945121471e-06, 2.668931085667026e-09, + 7.014768631369204e-12, 1.0071002349655513e-14, 1.3044197068887225e-17, + 2.3486689799408015e-20, 7.89501314248373e-24, 1.5998092556074022e-29 +], +"1000250550": [ + 0.0002570781572123518, 1.3997175199058435e-06, 2.219783132473186e-09, + 5.8551262251404896e-12, 8.494591239843473e-15, 1.094213754180689e-17, + 1.9726200825888293e-20, 7.031160012042902e-24, 8.36323366948029e-30 +], +"1000260560": [ + 0.002842823933534924, 1.954886657761042e-05, 4.246070492436761e-08, + 1.1229009640432841e-10, 1.645976445302043e-13, 2.108465186023526e-16, + 3.802679592642396e-19, 1.4331763686674318e-22, 9.390628847435543e-29 +], +"1000270590": [ + 1.5357914071095045e-05, 8.960862724420335e-08, 1.4096845994185887e-10, + 3.7391808221461915e-13, 5.535929281285702e-16, 7.057003063347519e-19, + 1.2724120854928575e-21, 5.05708574082789e-25, 1.6643806366478897e-31 +], +"1000280590": [ + 0.0001428298874291966, 1.1988016090859055e-06, 1.7395551185299618e-09, + 4.6198835134403076e-12, 6.909328369367064e-15, 8.767007771296035e-18, + 1.5799576829840197e-20, 6.6030534056644986e-24, 9.503510501308364e-31 +] +}, +"GlobalSplineFit5Comp": { +"2212": [ + 0.04585496359243566, 7.789996992661386e-05, 1.906109553848489e-07, + 3.9137200583417463e-10, 9.171690470565732e-13, 7.9337267934623845e-16, + 6.150893025573896e-19, 1.1644398925774302e-21, 3.796441872307821e-25 +], +"1000020040": [ + 0.0333898992585283, 6.607151909623054e-05, 1.7770717609826288e-07, + 4.228919791397361e-10, 8.159976041165978e-13, 9.874707359687682e-16, + 5.556178344735248e-19, 5.388868875506889e-22, 1.350504754907221e-24 +], +"1000070140": [ + 0.01739911667063377, 4.163181251293069e-05, 8.675523841454199e-08, + 2.57982428406128e-10, 5.925119236117241e-13, 7.526868238527486e-16, + 9.306610497155397e-19, 3.772080113612603e-22, 1.0826210057094147e-25 +], +"1000130270": [ + 0.005711110093190119, 1.8007817917772172e-05, 3.120443466764892e-08, + 1.345284814835353e-10, 1.8802192044868815e-13, 2.7050307380029037e-16, + 4.380849990088892e-19, 5.2278726641183234e-23, 1.0014576655898525e-26 +], +"1000260560": [ + 0.005793870699845916, 2.9716648857588622e-05, 5.976127925781268e-08, + 1.5684024226968786e-10, 2.262817833853498e-13, 2.927422475864482e-16, + 5.255541662618777e-19, 1.8275905525903871e-22, 6.139747237555774e-28 +] +}, +"GlobalSplineFit_IT": { +"2212": [ + 0.04585496359243566, 7.789996992661386e-05, 1.906109553848489e-07, + 3.9137200583417463e-10, 9.171690470565732e-13, 7.9337267934623845e-16, + 6.150893025573896e-19, 1.1644398925774302e-21, 3.796441872307821e-25 +], +"1000020040": [ + 0.031920527947683505, 6.467243917299695e-05, 1.7585162032645172e-07, + 4.171457677567575e-10, 8.036436027741618e-13, 9.733794517983287e-16, + 5.416652540933012e-19, 5.335279433164195e-22, 1.3446258153589729e-24 +], +"1000080160": [ + 0.01849615457424927, 4.2598409943263974e-05, 8.770327902148133e-08, + 2.6165298358572266e-10, 6.02291866733151e-13, 7.629592350166858e-16, + 9.387926922480087e-19, 3.8207626748390343e-22, 1.1392311676855142e-25 +], +"1000260560": [ + 0.012020143466808282, 4.935575122181731e-05, 9.361278555527713e-08, + 2.9806430213565154e-10, 4.2378708454389846e-13, 5.758312177797872e-16, + 9.852596811847251e-19, 2.4213151651170136e-22, 1.0847343219212744e-26 +] +} } diff --git a/tests/test_fluxes.py b/tests/test_fluxes.py index 2c161e3..aab813a 100755 --- a/tests/test_fluxes.py +++ b/tests/test_fluxes.py @@ -74,5 +74,17 @@ def test_GlobalSplineFit_similar(gsf): assert f == pytest.approx(f_gsf, rel=0.4) +@pytest.mark.parametrize("gsf", gsfmodels) +def test_GlobalSplineFIt_negative(gsf): + """ + Since GSF was using a cubic spline, it could oscillate a bit between the interpolated data point. + This was noticed due to a very minor undershoot below zero for protons at high energies. + So here we explicitly test that we only return non-negative values. + """ + test_e = np.geomspace(9.95e10, 10e10, 9) + flux = gsf(*np.meshgrid(test_e, gsf.pdgids)) + assert np.all(flux >= 0) + + if __name__ == "__main__": sys.exit(pytest.main(["-v", __file__, *sys.argv[1:]]))