diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 79a5c2db..e709b873 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -29,7 +29,8 @@ repos: # - id: black # args: [ --line-length=120 ] # - - repo: https://github.com/roy-ht/pre-commit-jupyter - rev: v1.2.1 + - repo: https://github.com/kynan/nbstripout + rev: 0.6.1 hooks: - - id: jupyter-notebook-cleanup + - id: nbstripout + diff --git a/advanced-python/10Basics.ipynb b/advanced-python/10Basics.ipynb index 93be8a95..9301d4bd 100644 --- a/advanced-python/10Basics.ipynb +++ b/advanced-python/10Basics.ipynb @@ -52,12 +52,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:55.162938280Z", - "start_time": "2023-11-09T18:21:54.591214759Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "a = ['a', 'b', 'c']\n", @@ -71,12 +66,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:55.164206324Z", - "start_time": "2023-11-09T18:21:54.591634409Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "a = {'a': '0', 'b': '1', 'c': '2'}\n", @@ -90,12 +80,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:55.165379338Z", - "start_time": "2023-11-09T18:21:54.592201956Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "a = 'foo'\n", @@ -117,12 +102,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:55.174086165Z", - "start_time": "2023-11-09T18:21:54.592801793Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "N = 10\n", @@ -143,12 +123,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:55.174961405Z", - "start_time": "2023-11-09T18:21:54.639862013Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "squares = {i: i**2 for i in range(10)}\n", @@ -158,12 +133,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:55.177083872Z", - "start_time": "2023-11-09T18:21:54.640208754Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "N = 5\n", @@ -232,12 +202,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:55.178900927Z", - "start_time": "2023-11-09T18:21:54.640684978Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "pt_cut = 1789.234567890987654\n", @@ -267,12 +232,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:55.179761160Z", - "start_time": "2023-11-09T18:21:54.683193413Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "\"Hello starterkitters\"" @@ -281,12 +241,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:55.180074403Z", - "start_time": "2023-11-09T18:21:54.683507163Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "None" @@ -302,12 +257,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:55.708097724Z", - "start_time": "2023-11-09T18:21:54.683700759Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "!ls" @@ -316,12 +266,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:57.188240198Z", - "start_time": "2023-11-09T18:21:55.712286379Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "!wget https://example.com/index.html" @@ -337,12 +282,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:57.209109687Z", - "start_time": "2023-11-09T18:21:57.194328587Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "%time sum([i**2 for i in range(10000)])" @@ -358,12 +298,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:57.268821159Z", - "start_time": "2023-11-09T18:21:57.208320012Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "%%time\n", @@ -402,12 +337,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:57.269768775Z", - "start_time": "2023-11-09T18:21:57.263045210Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def my_print(my_string):\n", @@ -417,12 +347,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.320620623Z", - "start_time": "2023-11-09T18:21:57.263292639Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "my_print?" @@ -438,12 +363,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.324026004Z", - "start_time": "2023-11-09T18:21:57.541042260Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "my_print??" @@ -452,12 +372,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.357805971Z", - "start_time": "2023-11-09T18:21:57.552034854Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "range?" @@ -473,26 +388,16 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.396066090Z", - "start_time": "2023-11-09T18:21:57.647040419Z" - } - }, + "metadata": {}, "outputs": [], "source": [ - "{'a': 'b'}.get?" + "{'a': 'b'}.get?" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.398964659Z", - "start_time": "2023-11-09T18:21:57.695017046Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "{'a': 'b'}.get" @@ -501,12 +406,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.401053224Z", - "start_time": "2023-11-09T18:21:57.695273312Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "junk = {'a': 'b'}.get\n", @@ -532,12 +432,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.404008533Z", - "start_time": "2023-11-09T18:21:57.695446136Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "max(10, 15)" @@ -549,11 +444,7 @@ "metadata": { "tags": [ "raises-exception" - ], - "ExecuteTime": { - "end_time": "2023-11-09T18:21:59.040701073Z", - "start_time": "2023-11-09T18:21:57.695622001Z" - } + ] }, "outputs": [], "source": [ @@ -572,11 +463,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.564441476Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", @@ -610,10 +497,7 @@ "metadata": { "tags": [ "raises-exception" - ], - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.606952947Z" - } + ] }, "outputs": [], "source": [ diff --git a/advanced-python/11AdvancedPython.ipynb b/advanced-python/11AdvancedPython.ipynb index ea1344cb..2a1c253b 100644 --- a/advanced-python/11AdvancedPython.ipynb +++ b/advanced-python/11AdvancedPython.ipynb @@ -17,12 +17,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.287777986Z", - "start_time": "2023-11-09T18:21:57.475130307Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import time" @@ -42,12 +37,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.288125493Z", - "start_time": "2023-11-09T18:21:57.475439183Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "a, c, *b = [3, 4, 4.5, 5, 6]" @@ -56,12 +46,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.288659478Z", - "start_time": "2023-11-09T18:21:57.475730863Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "b" @@ -77,12 +62,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.289024121Z", - "start_time": "2023-11-09T18:21:57.476020895Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "d1, d2, *d3, d4 = [1, 2, 3] # nothing left for d3" @@ -91,12 +71,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.289770343Z", - "start_time": "2023-11-09T18:21:57.476279441Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "d3" @@ -112,12 +87,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.289972173Z", - "start_time": "2023-11-09T18:21:57.476626713Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "a = [3, 4, 5]" @@ -133,12 +103,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.290148105Z", - "start_time": "2023-11-09T18:21:57.519751245Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "d, e, f, g, h, i = *a, *b" @@ -154,12 +119,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.290328424Z", - "start_time": "2023-11-09T18:21:57.519943991Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def func(*args, **kwargs):\n", @@ -170,12 +130,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.291657968Z", - "start_time": "2023-11-09T18:21:57.520119691Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "mykwargs = {'a': 5, 'b': 3}\n", @@ -186,12 +141,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.297387283Z", - "start_time": "2023-11-09T18:21:57.520366676Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "func(5, a=4)" @@ -200,12 +150,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.297741024Z", - "start_time": "2023-11-09T18:21:57.520883499Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# play around with it!" @@ -248,12 +193,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.324668018Z", - "start_time": "2023-11-09T18:21:57.563095740Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import contextlib\n", @@ -269,12 +209,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.326374361Z", - "start_time": "2023-11-09T18:21:57.563347017Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "with printer(5) as number:\n", @@ -294,12 +229,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.354444565Z", - "start_time": "2023-11-09T18:21:57.563714577Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "with open('tmp.txt', 'w') as textfile:\n", @@ -316,12 +246,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.355126940Z", - "start_time": "2023-11-09T18:21:57.563857095Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import contextlib\n", @@ -344,12 +269,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.358277200Z", - "start_time": "2023-11-09T18:21:57.569931680Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "testdict = {'value': 11, 'name': 'the answer'}" @@ -371,12 +291,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.359563889Z", - "start_time": "2023-11-09T18:21:57.611213702Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# SOLUTION\n", @@ -393,12 +308,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.359882492Z", - "start_time": "2023-11-09T18:21:57.611718752Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "@contextlib.contextmanager\n", @@ -421,12 +331,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.394698482Z", - "start_time": "2023-11-09T18:21:57.611948597Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "class MyContext:\n", @@ -447,12 +352,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.397073702Z", - "start_time": "2023-11-09T18:21:57.612144179Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "with MyContext(5) as x:\n", @@ -478,12 +378,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.397374445Z", - "start_time": "2023-11-09T18:21:57.659281779Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def make_power_func(power):\n", @@ -495,12 +390,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.397643363Z", - "start_time": "2023-11-09T18:21:57.659526538Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "pow3 = make_power_func(3)" @@ -509,12 +399,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.398133567Z", - "start_time": "2023-11-09T18:21:57.659712907Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "pow3(2)" @@ -523,12 +408,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.398357818Z", - "start_time": "2023-11-09T18:21:57.659911070Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def make_power_func(power):\n", @@ -541,12 +421,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.404364977Z", - "start_time": "2023-11-09T18:21:57.660055943Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "pow3 = make_power_func(3)" @@ -555,12 +430,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.406575801Z", - "start_time": "2023-11-09T18:21:57.707103066Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "pow3(2)" @@ -569,12 +439,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.419570582Z", - "start_time": "2023-11-09T18:21:57.707383203Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Exercise: test it here" @@ -610,12 +475,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.421942708Z", - "start_time": "2023-11-09T18:21:57.714163427Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# SOLUTION\n", @@ -633,12 +493,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.422417608Z", - "start_time": "2023-11-09T18:21:57.755158988Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def add_notime(x, y):\n", @@ -648,12 +503,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.422731709Z", - "start_time": "2023-11-09T18:21:57.755389720Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "add_timed = timed_func(add_notime)" @@ -662,12 +512,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.422948361Z", - "start_time": "2023-11-09T18:21:57.755554232Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import time" @@ -676,12 +521,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.425073110Z", - "start_time": "2023-11-09T18:21:57.755705542Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "add_timed(y=4, x=5)" @@ -690,12 +530,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.425479931Z", - "start_time": "2023-11-09T18:21:57.756004166Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# test it here" @@ -713,12 +548,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:21:58.425804868Z", - "start_time": "2023-11-09T18:21:57.756157832Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "@timed_func\n", @@ -757,11 +587,7 @@ "metadata": { "tags": [ "raises-exception" - ], - "ExecuteTime": { - "end_time": "2023-11-09T18:21:59.048528979Z", - "start_time": "2023-11-09T18:21:57.803005602Z" - } + ] }, "outputs": [], "source": [ @@ -781,10 +607,7 @@ "metadata": { "tags": [ "raises-exception" - ], - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.751370683Z" - } + ] }, "outputs": [], "source": [ @@ -809,11 +632,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.795021637Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "class MyError(Exception):\n", @@ -826,10 +645,7 @@ "metadata": { "tags": [ "raises-exception" - ], - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.795152334Z" - } + ] }, "outputs": [], "source": [ @@ -850,11 +666,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.795308728Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "class NegativeValueError(ValueError):\n", @@ -881,11 +693,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.795410621Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "try:\n", @@ -919,11 +727,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.795520523Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "try:\n", @@ -939,10 +743,7 @@ "metadata": { "tags": [ "raises-exception" - ], - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.795603420Z" - } + ] }, "outputs": [], "source": [ @@ -972,9 +773,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.795680122Z" } }, "outputs": [], @@ -1010,10 +808,7 @@ }, "tags": [ "raises-exception" - ], - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.795985110Z" - } + ] }, "outputs": [], "source": [ @@ -1048,10 +843,7 @@ }, "tags": [ "raises-exception" - ], - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.796186073Z" - } + ] }, "outputs": [], "source": [ @@ -1076,9 +868,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.796374250Z" } }, "outputs": [], @@ -1104,9 +893,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.796560001Z" } }, "outputs": [], @@ -1134,9 +920,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.796721888Z" } }, "outputs": [], @@ -1156,10 +939,7 @@ }, "tags": [ "raises-exception" - ], - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.796792565Z" - } + ] }, "outputs": [], "source": [ @@ -1172,9 +952,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "start_time": "2023-11-09T18:21:58.796936035Z" } }, "outputs": [], diff --git a/advanced-python/12AdvancedClasses.ipynb b/advanced-python/12AdvancedClasses.ipynb index cd96c254..41185a08 100644 --- a/advanced-python/12AdvancedClasses.ipynb +++ b/advanced-python/12AdvancedClasses.ipynb @@ -20,12 +20,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.090086512Z", - "start_time": "2023-11-09T18:22:01.447375824Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "class NamedValue:\n", @@ -59,12 +54,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.096491782Z", - "start_time": "2023-11-09T18:22:01.447622989Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "valleft = ValueLeft('val left')\n", @@ -74,12 +64,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.097446715Z", - "start_time": "2023-11-09T18:22:01.447833842Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "valleft + valleft2" @@ -108,12 +93,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.097894774Z", - "start_time": "2023-11-09T18:22:01.448096154Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "class Name:\n", @@ -154,12 +134,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.098105946Z", - "start_time": "2023-11-09T18:22:01.491308102Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "class Callable:\n", @@ -174,12 +149,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.098275992Z", - "start_time": "2023-11-09T18:22:01.491619526Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "call = Callable()\n", @@ -189,12 +159,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.098786414Z", - "start_time": "2023-11-09T18:22:01.491851791Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "call()" @@ -203,12 +168,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.099302008Z", - "start_time": "2023-11-09T18:22:01.492017010Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "try:\n", @@ -236,12 +196,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.099478261Z", - "start_time": "2023-11-09T18:22:01.498433654Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "class Storage:\n", @@ -261,12 +216,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.099727280Z", - "start_time": "2023-11-09T18:22:01.503944286Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "storage = Storage('one')" @@ -275,12 +225,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.100369605Z", - "start_time": "2023-11-09T18:22:01.513503176Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "storage[2]" @@ -289,12 +234,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.100965196Z", - "start_time": "2023-11-09T18:22:01.555278832Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "storage[2] = 3" @@ -318,12 +258,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.101169939Z", - "start_time": "2023-11-09T18:22:01.555519700Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "class A:\n", @@ -337,12 +272,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.101370435Z", - "start_time": "2023-11-09T18:22:01.555728122Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "a = A(4)\n", @@ -352,12 +282,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.101792662Z", - "start_time": "2023-11-09T18:22:01.599103966Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "a.add(b)" @@ -366,12 +291,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.104487019Z", - "start_time": "2023-11-09T18:22:01.599306619Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "A.add(a, b)" @@ -418,12 +338,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.104939450Z", - "start_time": "2023-11-09T18:22:01.599476172Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "a.__dict__" @@ -439,12 +354,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.106823366Z", - "start_time": "2023-11-09T18:22:01.599607114Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "a.__class__.__dict__" @@ -453,12 +363,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.107525732Z", - "start_time": "2023-11-09T18:22:01.643176331Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "A.__dict__" @@ -481,12 +386,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.107832669Z", - "start_time": "2023-11-09T18:22:01.643423445Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "class GetAndSet:\n", @@ -509,12 +409,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.108062078Z", - "start_time": "2023-11-09T18:22:01.643613141Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "get = GetAndSet()" @@ -523,12 +418,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.108503703Z", - "start_time": "2023-11-09T18:22:01.643815676Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "get.add(get)" @@ -537,12 +427,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.108799427Z", - "start_time": "2023-11-09T18:22:01.644071731Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "get.addition(get)" @@ -551,12 +436,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.109426819Z", - "start_time": "2023-11-09T18:22:01.655190658Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "get.hello" @@ -572,12 +452,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.110263772Z", - "start_time": "2023-11-09T18:22:01.662211437Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "getattr(get, 'hello')" @@ -586,12 +461,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.110508234Z", - "start_time": "2023-11-09T18:22:01.679854902Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "get.hi" @@ -614,12 +484,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:02.111369769Z", - "start_time": "2023-11-09T18:22:01.685831397Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import this" diff --git a/advanced-python/20DataAndPlotting.ipynb b/advanced-python/20DataAndPlotting.ipynb index f2d611df..df8e83ee 100644 --- a/advanced-python/20DataAndPlotting.ipynb +++ b/advanced-python/20DataAndPlotting.ipynb @@ -32,12 +32,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:11.209901401Z", - "start_time": "2023-11-09T18:22:10.715117611Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import mplhep\n", @@ -115,12 +110,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:11.213168925Z", - "start_time": "2023-11-09T18:22:10.715393509Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "my_file = uproot.open('https://cern.ch/starterkit/data/advanced-python-2018/real_data.root',\n", @@ -139,12 +129,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:11.577169Z", - "start_time": "2023-11-09T18:22:10.996475027Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "tree = my_file['DecayTree']\n", @@ -155,12 +140,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:24.452463022Z", - "start_time": "2023-11-09T18:22:11.577968658Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Load data as a pandas DataFrame\n", @@ -174,12 +154,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:24.470597938Z", - "start_time": "2023-11-09T18:22:24.447634455Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_df.columns" @@ -196,12 +171,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:24.855253849Z", - "start_time": "2023-11-09T18:22:24.453234659Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Start with a basic histogram\n", @@ -234,10 +204,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "end_time": "2023-11-09T18:22:24.855582435Z", - "start_time": "2023-11-09T18:22:24.819219840Z" } }, "outputs": [], @@ -253,10 +219,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "end_time": "2023-11-09T18:22:25.965614384Z", - "start_time": "2023-11-09T18:22:24.819392199Z" } }, "outputs": [], @@ -272,10 +234,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "end_time": "2023-11-09T18:22:26.500198095Z", - "start_time": "2023-11-09T18:22:25.966183784Z" } }, "outputs": [], @@ -291,10 +249,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "end_time": "2023-11-09T18:22:27.468758686Z", - "start_time": "2023-11-09T18:22:26.499064264Z" } }, "outputs": [], @@ -314,12 +268,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:29.632389925Z", - "start_time": "2023-11-09T18:22:27.444982487Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def plot_mass(df):\n", @@ -343,12 +292,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:29.667859406Z", - "start_time": "2023-11-09T18:22:29.626043515Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# When making the ROOT file we forgot to add some variables, no bother lets add them now!\n", @@ -366,12 +310,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:29.978476627Z", - "start_time": "2023-11-09T18:22:29.648794140Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_df.eval('mup_P = sqrt(mup_PX**2 + mup_PY**2 + mup_PZ**2)', inplace=True)\n", @@ -399,12 +338,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:30.368613646Z", - "start_time": "2023-11-09T18:22:29.678392856Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plot_mass(data_df)\n", @@ -415,12 +349,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:31.531723191Z", - "start_time": "2023-11-09T18:22:30.368802764Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plot_mass(data_df)\n", @@ -442,12 +371,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:32.129701102Z", - "start_time": "2023-11-09T18:22:31.166183480Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def plot_mass(df, **kwargs):\n", @@ -476,12 +400,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:32.156206263Z", - "start_time": "2023-11-09T18:22:32.129165982Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_df.columns" @@ -490,12 +409,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:22:33.885426832Z", - "start_time": "2023-11-09T18:22:32.134501210Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from python_lesson import check_truth\n", @@ -526,12 +440,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:23:21.141300103Z", - "start_time": "2023-11-09T18:22:33.874056558Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "with uproot.open('https://starterkit.web.cern.ch/starterkit/data/advanced-python-2018/simulated_data.root') as mc_file:\n", @@ -558,12 +467,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:23:21.631231066Z", - "start_time": "2023-11-09T18:23:21.182938710Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "bkg_df = data_df.query('~(3.0 < Jpsi_M < 3.2)')\n", @@ -589,12 +493,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:23:22.134866766Z", - "start_time": "2023-11-09T18:23:21.630331279Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "var = 'Jpsi_PT'\n", @@ -612,12 +511,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:23:22.642315719Z", - "start_time": "2023-11-09T18:23:22.130434993Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Those are hard to compare!\n", @@ -640,12 +534,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:23:22.648142526Z", - "start_time": "2023-11-09T18:23:22.640877567Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def plot_comparision(var, mc_df, bkg_df):\n", @@ -663,12 +552,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:23:22.657440397Z", - "start_time": "2023-11-09T18:23:22.644203876Z" - } - }, + "metadata": {}, "outputs": [], "source": [] }, @@ -682,12 +566,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:23:39.984761182Z", - "start_time": "2023-11-09T18:23:22.649403635Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "for var in data_df.columns:\n", @@ -717,12 +596,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:23:39.987782824Z", - "start_time": "2023-11-09T18:23:39.981983050Z" - } - }, + "metadata": {}, "outputs": [], "source": [] }, @@ -739,10 +613,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "end_time": "2023-11-09T18:23:40.835372688Z", - "start_time": "2023-11-09T18:23:39.986976807Z" } }, "outputs": [], @@ -758,10 +628,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "end_time": "2023-11-09T18:23:40.835551238Z", - "start_time": "2023-11-09T18:23:40.835014982Z" } }, "outputs": [], @@ -773,10 +639,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "end_time": "2023-11-09T18:23:40.835677817Z", - "start_time": "2023-11-09T18:23:40.835124429Z" } }, "outputs": [], diff --git a/advanced-python/30Classification.ipynb b/advanced-python/30Classification.ipynb index 7679df59..9c07d9aa 100644 --- a/advanced-python/30Classification.ipynb +++ b/advanced-python/30Classification.ipynb @@ -19,12 +19,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:09.454693438Z", - "start_time": "2023-11-09T18:25:07.925906320Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "%store -r bkg_df\n", @@ -35,12 +30,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:09.455020356Z", - "start_time": "2023-11-09T18:25:08.698996144Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import mplhep\n", @@ -71,12 +61,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:11.748577377Z", - "start_time": "2023-11-09T18:25:09.007997107Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.scatter(mc_df['mup_PT'], mc_df['mum_PT'], s=1, marker=',', label='Signal')\n", @@ -110,12 +95,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:11.762307655Z", - "start_time": "2023-11-09T18:25:11.747609552Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "training_columns = [\n", @@ -130,12 +110,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:11.882528359Z", - "start_time": "2023-11-09T18:25:11.752531431Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# We then define the classifier we want to use\n", @@ -155,12 +130,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:11.882858520Z", - "start_time": "2023-11-09T18:25:11.795008544Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "bkg_df = bkg_df.copy()\n", @@ -171,12 +141,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:12.365576579Z", - "start_time": "2023-11-09T18:25:11.795121407Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Now merge the data together\n", @@ -188,12 +153,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:13.766805931Z", - "start_time": "2023-11-09T18:25:12.375467218Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# We can now fit the BDT\n", @@ -210,12 +170,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:13.798097938Z", - "start_time": "2023-11-09T18:25:13.764223021Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "bdt.predict_proba(data_df[training_columns].head())" @@ -233,12 +188,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:13.866038246Z", - "start_time": "2023-11-09T18:25:13.787561597Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# We can now use slicing to select column 1 in the array from for all rows\n", @@ -256,12 +206,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:14.147837572Z", - "start_time": "2023-11-09T18:25:13.865725416Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "mc_df['BDT'] = bdt.predict_proba(mc_df[training_columns])[:,1]\n", @@ -280,12 +225,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:14.352377276Z", - "start_time": "2023-11-09T18:25:14.147685274Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "for df in [mc_df, bkg_df, data_df, training_data]:\n", @@ -304,12 +244,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:14.364834431Z", - "start_time": "2023-11-09T18:25:14.351502685Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def plot_comparision(var, mc_df, bkg_df):\n", @@ -335,12 +270,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:14.602892746Z", - "start_time": "2023-11-09T18:25:14.359199993Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plot_comparision('BDT', mc_df, bkg_df)" @@ -356,12 +286,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:16.410750494Z", - "start_time": "2023-11-09T18:25:14.601766515Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plot_mass(data_df, label='No cuts', density=1)\n", @@ -388,12 +313,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:17.390975490Z", - "start_time": "2023-11-09T18:25:16.409914497Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# That would be too nice to use in analysis\n", @@ -417,12 +337,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:17.576076450Z", - "start_time": "2023-11-09T18:25:17.380970864Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plot_comparision('BDT', mc_df, bkg_df)" @@ -442,12 +357,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:17.674307301Z", - "start_time": "2023-11-09T18:25:17.573465928Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "y_score = bdt.predict_proba(training_data[training_columns])[:,1]\n", @@ -469,12 +379,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:17.825716688Z", - "start_time": "2023-11-09T18:25:17.695016918Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.plot([0, 1], [0, 1], color='grey', linestyle='--', label='Randomly guess')\n", @@ -498,12 +403,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:17.883519883Z", - "start_time": "2023-11-09T18:25:17.824214208Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "area = auc(fpr, tpr)" @@ -512,12 +412,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:17.996072891Z", - "start_time": "2023-11-09T18:25:17.871252944Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.plot([0, 1], [0, 1], color='grey', linestyle='--')\n", @@ -543,12 +438,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:18.048225663Z", - "start_time": "2023-11-09T18:25:17.994427329Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "n_sig = 1200\n", @@ -565,12 +455,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:18.048735130Z", - "start_time": "2023-11-09T18:25:18.035311051Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "S = n_sig*tpr\n", @@ -588,12 +473,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:18.256753265Z", - "start_time": "2023-11-09T18:25:18.035655070Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.plot(thresholds, metric)\n", @@ -612,12 +492,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:18.257317434Z", - "start_time": "2023-11-09T18:25:18.243137106Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "optimal_index = np.argmax(metric)\n", @@ -629,12 +504,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:18.635953003Z", - "start_time": "2023-11-09T18:25:18.243349315Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plot_mass(data_df, label='No cuts', density=1)\n", @@ -648,12 +518,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:18.742133399Z", - "start_time": "2023-11-09T18:25:18.551392406Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plot_mass(data_with_cuts_df, label='Using BDT only')" @@ -671,12 +536,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:18.813621527Z", - "start_time": "2023-11-09T18:25:18.743820685Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def plot_roc(bdt, training_data, training_columns, label=None):\n", @@ -701,12 +561,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:18.813858248Z", - "start_time": "2023-11-09T18:25:18.791163354Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def plot_significance(bdt, training_data, training_columns, label=None):\n", @@ -731,12 +586,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:30:20.710903847Z", - "start_time": "2023-11-09T18:25:18.791369479Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "with uproot.open('https://cern.ch/starterkit/data/advanced-python-2018/real_data.root') as datafile:\n", @@ -783,12 +633,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:30:21.685114217Z", - "start_time": "2023-11-09T18:30:20.708314393Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "%store bkg_df\n", @@ -799,12 +644,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:30:21.727449098Z", - "start_time": "2023-11-09T18:30:21.679809042Z" - } - }, + "metadata": {}, "outputs": [], "source": [] } diff --git a/advanced-python/31ClassificationExtension.ipynb b/advanced-python/31ClassificationExtension.ipynb index 2b9421ea..3213818e 100644 --- a/advanced-python/31ClassificationExtension.ipynb +++ b/advanced-python/31ClassificationExtension.ipynb @@ -25,12 +25,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:00.013914071Z", - "start_time": "2023-11-09T18:39:58.305187065Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "%store -r training_data\n", @@ -43,12 +38,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:00.401140920Z", - "start_time": "2023-11-09T18:40:00.001945534Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import mplhep\n", @@ -62,12 +52,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:00.424165539Z", - "start_time": "2023-11-09T18:40:00.375142416Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def plot_comparision(var, mc_df, bkg_df):\n", @@ -123,12 +108,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:22.730604251Z", - "start_time": "2023-11-09T18:40:00.386255547Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Train the Gradient Booster\n", @@ -141,12 +121,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:32.407167754Z", - "start_time": "2023-11-09T18:40:22.738816458Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Train the Adaptive Booster\n", @@ -159,12 +134,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:33.896052239Z", - "start_time": "2023-11-09T18:40:32.406995877Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Train XGBoost Classifier\n", @@ -177,12 +147,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:37.959441690Z", - "start_time": "2023-11-09T18:40:33.890325623Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.figure()\n", @@ -231,12 +196,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:38.867894240Z", - "start_time": "2023-11-09T18:40:37.963705203Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "for df in [mc_df, bkg_df, data_df, training_data]:\n", @@ -268,12 +228,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.395771938Z", - "start_time": "2023-11-09T18:40:38.878807747Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# bdtclass = GradientBoostingClassifier()\n", @@ -375,12 +330,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.395898908Z", - "start_time": "2023-11-09T18:40:44.896492177Z" - } - }, + "metadata": {}, "outputs": [], "source": [] } diff --git a/advanced-python/32BoostingToUniformity.ipynb b/advanced-python/32BoostingToUniformity.ipynb index e1b81ff5..ae62b845 100644 --- a/advanced-python/32BoostingToUniformity.ipynb +++ b/advanced-python/32BoostingToUniformity.ipynb @@ -25,12 +25,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:39.380065511Z", - "start_time": "2023-11-09T18:25:38.723060607Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import uproot\n", @@ -53,12 +48,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:26:25.983039940Z", - "start_time": "2023-11-09T18:25:39.055897465Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "used_columns = [\"Y1\", \"Y2\", \"Y3\", \"M2AB\", \"M2AC\"]\n", @@ -80,12 +70,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:26:26.722476388Z", - "start_time": "2023-11-09T18:26:26.022942250Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def plot_distribution(data_frame, var_name1='M2AB', var_name2='M2AC', bins=40):\n", @@ -111,12 +96,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:26:26.738984299Z", - "start_time": "2023-11-09T18:26:26.722774930Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "trainX, testX, trainY, testY = train_test_split(data, labels, random_state=42, test_size=0.5)" @@ -132,12 +112,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:26:26.847926700Z", - "start_time": "2023-11-09T18:26:26.737604646Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "uniform_features = [\"M2AB\", \"M2AC\"]\n", @@ -156,12 +131,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:30:50.832483520Z", - "start_time": "2023-11-09T18:26:26.744692640Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "classifiers = {}\n", @@ -176,7 +146,7 @@ "classifiers['uBoost'] = uboost.uBoostClassifier(uniform_features=uniform_features, uniform_label=1,\n", " base_estimator=base_estimator,\n", " n_estimators=n_estimators, train_features=train_features,\n", - " efficiency_steps=12, n_threads=4)\n", + " efficiency_steps=12, n_jobs=4)\n", "\n", "flatnessloss = ugb.KnnFlatnessLossFunction(uniform_features, fl_coefficient=3., power=1.3, uniform_label=1)\n", "classifiers['uGB+FL'] = ugb.UGradientBoostingClassifier(loss=flatnessloss, max_depth=4,\n", @@ -202,12 +172,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:30:57.270917786Z", - "start_time": "2023-11-09T18:30:50.874988212Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from sklearn.metrics import roc_auc_score\n", @@ -230,12 +195,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:31:04.075394547Z", - "start_time": "2023-11-09T18:30:57.271060612Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from sklearn.metrics import roc_curve\n", @@ -271,12 +231,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:31:04.079427234Z", - "start_time": "2023-11-09T18:31:04.071407036Z" - } - }, + "metadata": {}, "outputs": [], "source": [] } diff --git a/advanced-python/33ModelTuning.ipynb b/advanced-python/33ModelTuning.ipynb index 0ea4d465..be6ea321 100644 --- a/advanced-python/33ModelTuning.ipynb +++ b/advanced-python/33ModelTuning.ipynb @@ -190,7 +190,7 @@ "LR = 0.3 # the coefficient of step size decay, eta, has alias 'learning_rate' with default 0.3\n", "\n", "stime = time.time()\n", - "bdt = XGBClassifier(learning_rate = LR, n_estimators=100, seed=123, n_threads=-1)\n", + "bdt = XGBClassifier(learning_rate = LR, n_estimators=100, seed=123, n_jobs=-1)\n", "bdt.fit(training_data[training_columns], training_data['catagory'])\n", "print(\"XGBoost --- %s seconds ---\" % (time.time() - stime))\n", "\n", @@ -405,7 +405,7 @@ " #colsample_bytree=0.8, scale_pos_weight=1,\n", " #objective='binary:logistic', # default for binary classification\n", " #objective='mutli:softprob', num_class=3, # for multiclassifiers\n", - " seed=123, n_threads=-1)\n", + " seed=123, n_jobs=-1)\n", "\n", "# Timing the CV using early stopping\n", "stime = time.time()\n", @@ -512,7 +512,7 @@ "source": [ "# Running with estimators maximum for shortened training\n", "bdt_st = XGBClassifier( learning_rate = LR, n_estimators=estimators,\n", - " seed=123, n_threads=-1)\n", + " seed=123, n_jobs=-1)\n", "\n", "# Running timed hyperparameter gridscan\n", "stime = time.time()\n", @@ -542,7 +542,7 @@ "#bdt_opt = XGBClassifier( learning_rate = LR, n_estimators=1000,\n", "# max_depth=gs.best_params_['max_depth'],\n", "# min_child_weight=gs.best_params_['min_child_weight'],\n", - "# seed=123, n_threads=-1 )\n", + "# seed=123, n_jobs=-1 )\n", "\n", "## Run with CV early stopping\n", "#stime = time.time()\n", diff --git a/advanced-python/40Histograms.ipynb b/advanced-python/40Histograms.ipynb index 808cd467..2827a6a9 100644 --- a/advanced-python/40Histograms.ipynb +++ b/advanced-python/40Histograms.ipynb @@ -3,7 +3,6 @@ { "cell_type": "markdown", "metadata": { - "collapsed": true, "pycharm": { "name": "#%% md\n" } @@ -34,10 +33,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.090763040Z", - "start_time": "2023-11-09T18:40:41.774097506Z" } }, "outputs": [], @@ -59,10 +54,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.092581529Z", - "start_time": "2023-11-09T18:40:42.719110990Z" } }, "outputs": [], @@ -129,24 +120,14 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.092804229Z", - "start_time": "2023-11-09T18:40:42.910621980Z" - } - }, + "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.092969634Z", - "start_time": "2023-11-09T18:40:42.916739161Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "start, stop = data_df['Jpsi_M'].min(), data_df['Jpsi_M'].max()\n", @@ -166,10 +147,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.093124685Z", - "start_time": "2023-11-09T18:40:42.927046922Z" } }, "outputs": [], @@ -180,12 +157,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.094256975Z", - "start_time": "2023-11-09T18:40:42.937126378Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_h.fill(data_df['Jpsi_M'])" @@ -194,12 +166,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.094471841Z", - "start_time": "2023-11-09T18:40:42.949363041Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "mc_h = hist.Hist(axis1).fill(mc_df['Jpsi_M']) # we can also chain the commands" @@ -219,12 +186,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.095025556Z", - "start_time": "2023-11-09T18:40:42.955171365Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "mplhep.histplot(data_h)" @@ -243,12 +205,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.095656170Z", - "start_time": "2023-11-09T18:40:43.211187900Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_h.plot1d()" @@ -257,12 +214,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.096292049Z", - "start_time": "2023-11-09T18:40:43.415174250Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_h.plot1d()\n", @@ -272,12 +224,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.096697901Z", - "start_time": "2023-11-09T18:40:43.622364563Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "mc_df.columns" @@ -295,12 +242,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.097017931Z", - "start_time": "2023-11-09T18:40:43.629433139Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "start, stop = data_df['BDT'].min(), data_df['BDT'].max()\n", @@ -310,12 +252,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.097244707Z", - "start_time": "2023-11-09T18:40:43.671235206Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "mc_h2d = hist.Hist(axis1, axis_bdt).fill(BDT=mc_df['BDT'], mass=mc_df['Jpsi_M']) # using names" @@ -324,12 +261,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.136940250Z", - "start_time": "2023-11-09T18:40:43.671469880Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_h2d = hist.Hist(axis1, axis_bdt)\n", @@ -339,12 +271,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.138342837Z", - "start_time": "2023-11-09T18:40:43.712937233Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "mplhep.hist2dplot(data_h2d)" @@ -362,12 +289,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.138747800Z", - "start_time": "2023-11-09T18:40:44.022960001Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Access by bin number\n", @@ -388,12 +310,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.140841528Z", - "start_time": "2023-11-09T18:40:44.029760988Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_h2d.density()" @@ -411,12 +328,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.142148636Z", - "start_time": "2023-11-09T18:40:44.062252938Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_h2d.project(\"mass\") # we will here retain the 1D histogram" @@ -434,12 +346,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.142629942Z", - "start_time": "2023-11-09T18:40:44.071307572Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_h2d.axes" @@ -448,12 +355,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.143016256Z", - "start_time": "2023-11-09T18:40:44.076331708Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_h2d.axes['mass']" @@ -462,12 +364,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.143633118Z", - "start_time": "2023-11-09T18:40:44.087011298Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_h2d.axes['mass'].edges" @@ -476,12 +373,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.144130758Z", - "start_time": "2023-11-09T18:40:44.098073547Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_h2d.axes['mass'].centers # bin centers" @@ -490,12 +382,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.144584441Z", - "start_time": "2023-11-09T18:40:44.102908180Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_h2d.axes['mass'].widths # bin widths" @@ -513,12 +400,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.145653144Z", - "start_time": "2023-11-09T18:40:44.111345363Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_h2d.axes.edges\n", @@ -540,12 +422,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.145851826Z", - "start_time": "2023-11-09T18:40:44.159338713Z" - } - }, + "metadata": {}, "outputs": [], "source": [] }, @@ -563,12 +440,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.172428378Z", - "start_time": "2023-11-09T18:40:44.159650878Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data_df_bdt = data_df.query(\"BDT > 0.9\")\n", @@ -580,12 +452,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.173168648Z", - "start_time": "2023-11-09T18:40:44.190637837Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "ratio = data_bdt_h2d.project(\"mass\") / data_h2d.project(\"mass\")" @@ -594,12 +461,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.200611311Z", - "start_time": "2023-11-09T18:40:44.231188307Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "ratio.plot1d()" @@ -608,12 +470,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.213022780Z", - "start_time": "2023-11-09T18:40:44.419136722Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "ratio_large = ratio * 10\n", @@ -630,12 +487,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.213326313Z", - "start_time": "2023-11-09T18:40:44.667191807Z" - } - }, + "metadata": {}, "outputs": [], "source": [] }, @@ -653,12 +505,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.213535481Z", - "start_time": "2023-11-09T18:40:44.667447153Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "weight = np.random.normal(1., 0.1, size=mc_df.shape[0])\n", @@ -669,12 +516,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.231990095Z", - "start_time": "2023-11-09T18:40:44.667709042Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "mc_h2d" @@ -683,12 +525,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.395015033Z", - "start_time": "2023-11-09T18:40:44.698968002Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "mc_h2d.variances()" @@ -704,12 +541,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:45.395215588Z", - "start_time": "2023-11-09T18:40:44.743359631Z" - } - }, + "metadata": {}, "outputs": [], "source": [] } diff --git a/advanced-python/45DemoReweighting.ipynb b/advanced-python/45DemoReweighting.ipynb index abcf3a2f..02dd9a47 100644 --- a/advanced-python/45DemoReweighting.ipynb +++ b/advanced-python/45DemoReweighting.ipynb @@ -25,12 +25,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:25:53.648661700Z", - "start_time": "2023-11-09T18:25:53.142978500Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", @@ -52,12 +47,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:27:45.758389346Z", - "start_time": "2023-11-09T18:25:53.489875451Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "columns = ['hSPD', 'pt_b', 'pt_phi', 'vchi2_b', 'mu_pt_sum']\n", @@ -90,12 +80,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:27:45.959472949Z", - "start_time": "2023-11-09T18:27:45.799133696Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", @@ -112,12 +97,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:27:45.975481005Z", - "start_time": "2023-11-09T18:27:45.965651595Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from hep_ml.metrics_utils import ks_2samp_weighted\n", @@ -148,12 +128,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:27:45.990266338Z", - "start_time": "2023-11-09T18:27:45.971355495Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# pay attention, actually we have very few data\n", @@ -163,12 +138,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:27:52.291018726Z", - "start_time": "2023-11-09T18:27:45.983111062Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "draw_distributions(original, target, original_weights)" @@ -184,12 +154,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:27:56.786090353Z", - "start_time": "2023-11-09T18:27:52.282166289Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "draw_distributions(original_train, target_train, original_weights_train)" @@ -205,12 +170,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:28:00.210019779Z", - "start_time": "2023-11-09T18:27:56.779869734Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "draw_distributions(original_test, target_test, original_weights_test)" @@ -241,12 +201,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:28:21.654914654Z", - "start_time": "2023-11-09T18:28:00.206358826Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "bins_reweighter = reweight.BinsReweighter(n_bins=20, n_neighs=1.)\n", @@ -275,12 +230,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:33:22.286971428Z", - "start_time": "2023-11-09T18:28:21.649791476Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "reweighter = reweight.GBReweighter(n_estimators=250, learning_rate=0.1, max_depth=3, min_samples_leaf=1000,\n", @@ -304,12 +254,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:33:22.445738489Z", - "start_time": "2023-11-09T18:33:22.284510361Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def check_ks_of_expression(expression):\n", @@ -327,12 +272,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:33:22.447484398Z", - "start_time": "2023-11-09T18:33:22.327051319Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "check_ks_of_expression('hSPD')" @@ -341,12 +281,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:33:22.658006592Z", - "start_time": "2023-11-09T18:33:22.415016102Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "check_ks_of_expression('hSPD * pt_phi')" @@ -355,12 +290,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:33:22.903957662Z", - "start_time": "2023-11-09T18:33:22.656149470Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "check_ks_of_expression('hSPD * pt_phi * vchi2_b')" @@ -369,12 +299,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:33:23.155559842Z", - "start_time": "2023-11-09T18:33:22.899381940Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "check_ks_of_expression('pt_b * pt_phi / hSPD ')" @@ -383,12 +308,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:33:23.345847337Z", - "start_time": "2023-11-09T18:33:23.155057276Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "check_ks_of_expression('hSPD * pt_b * vchi2_b / pt_phi')" @@ -410,12 +330,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:33:42.202790691Z", - "start_time": "2023-11-09T18:33:23.345111007Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from sklearn.ensemble import GradientBoostingClassifier\n", @@ -463,12 +378,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:33:43.726731060Z", - "start_time": "2023-11-09T18:33:42.207967134Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.hist(weights['gb_weights'], bins=50)\n", @@ -479,12 +389,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:33:43.730253817Z", - "start_time": "2023-11-09T18:33:43.723035309Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "np.max(weights['gb_weights']), np.sum(weights['gb_weights'])" @@ -521,12 +426,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:38:48.740721947Z", - "start_time": "2023-11-09T18:33:43.728815796Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# define base reweighter\n", @@ -555,12 +455,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:00.647769707Z", - "start_time": "2023-11-09T18:38:48.738767636Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "data = np.concatenate([original, target])\n", @@ -582,12 +477,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:40:01.473145845Z", - "start_time": "2023-11-09T18:40:00.647093226Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.hist(weights['2-folding'], bins=50)\n", diff --git a/advanced-python/50LikelihoodInference.ipynb b/advanced-python/50LikelihoodInference.ipynb index 1f70ce61..0ec7c601 100644 --- a/advanced-python/50LikelihoodInference.ipynb +++ b/advanced-python/50LikelihoodInference.ipynb @@ -41,12 +41,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:04.548448189Z", - "start_time": "2023-11-09T18:41:03.508890856Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "%store -r bkg_df\n", @@ -57,12 +52,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:04.555465994Z", - "start_time": "2023-11-09T18:41:04.545771470Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import hepstats\n", @@ -75,12 +65,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:04.607549796Z", - "start_time": "2023-11-09T18:41:04.552563192Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# apply cuts\n", @@ -103,12 +88,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:04.643172087Z", - "start_time": "2023-11-09T18:41:04.628270711Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "obs = zfit.Space('Jpsi_M', limits=(2.8, 3.5)) # defining the observable" @@ -117,12 +97,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:04.643396920Z", - "start_time": "2023-11-09T18:41:04.636406001Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# bkg = zfit.Data.from_pandas(bkg_df['Jpsi_M'], obs=obs)\n", @@ -134,12 +109,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:04.866062850Z", - "start_time": "2023-11-09T18:41:04.643452582Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "mc = zfit.Data.from_pandas(mc_df['Jpsi_M'], obs=obs)\n", @@ -160,12 +130,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:04.959244148Z", - "start_time": "2023-11-09T18:41:04.910984590Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "lambd = zfit.Parameter('lambda', -0.1, -2, 2)\n", @@ -179,12 +144,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:04.959518485Z", - "start_time": "2023-11-09T18:41:04.959061502Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "bkg_pdf = zfit.pdf.Exponential(lambd, obs=obs)\n", @@ -194,12 +154,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:04.959692598Z", - "start_time": "2023-11-09T18:41:04.959203182Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "sig_pdf = zfit.pdf.Gauss(obs=obs, mu=mu, sigma=sigma)\n", @@ -209,12 +164,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:04.965883067Z", - "start_time": "2023-11-09T18:41:04.959353512Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "model = zfit.pdf.SumPDF([bkg_pdf, sig_pdf])" @@ -232,12 +182,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:04.966167348Z", - "start_time": "2023-11-09T18:41:04.959441956Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def plot_fit(model, data, nbins=30, ax=None):\n", @@ -273,12 +218,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:07.019191120Z", - "start_time": "2023-11-09T18:41:04.959591758Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plot_fit(model, data) # before the fit" @@ -296,12 +236,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:07.303510860Z", - "start_time": "2023-11-09T18:41:07.018115082Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "sig_nll = zfit.loss.UnbinnedNLL(sig_pdf, mc)" @@ -317,12 +252,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:07.315026018Z", - "start_time": "2023-11-09T18:41:07.282948708Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "minimizer = zfit.minimize.Minuit()\n", @@ -333,12 +263,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:08.077766311Z", - "start_time": "2023-11-09T18:41:07.283066840Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "minimizer.minimize(sig_nll)" @@ -356,12 +281,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:08.081842858Z", - "start_time": "2023-11-09T18:41:08.067454337Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "sigma.floating = False" @@ -370,12 +290,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:08.677325245Z", - "start_time": "2023-11-09T18:41:08.071212416Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "nll = zfit.loss.ExtendedUnbinnedNLL(model, data)" @@ -384,12 +299,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:09.481473179Z", - "start_time": "2023-11-09T18:41:08.679126453Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "result = minimizer.minimize(nll)" @@ -398,12 +308,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:09.501631838Z", - "start_time": "2023-11-09T18:41:09.483882912Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "result" @@ -412,12 +317,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:10.998164125Z", - "start_time": "2023-11-09T18:41:09.499282781Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "result.hesse() # calculate hessian error\n", @@ -428,12 +328,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:11.291271535Z", - "start_time": "2023-11-09T18:41:10.991252129Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plot_fit(model, data)" @@ -449,12 +344,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:11.471911896Z", - "start_time": "2023-11-09T18:41:11.293166815Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from hepstats.hypotests.calculators import AsymptoticCalculator\n", @@ -481,12 +371,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:13.160970054Z", - "start_time": "2023-11-09T18:41:11.474037243Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from hepstats.hypotests import Discovery\n", diff --git a/advanced-python/60sPlot.ipynb b/advanced-python/60sPlot.ipynb index 2b713810..cfac4125 100644 --- a/advanced-python/60sPlot.ipynb +++ b/advanced-python/60sPlot.ipynb @@ -15,12 +15,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:33.690188079Z", - "start_time": "2023-11-09T18:41:33.174962617Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import mplhep\n", @@ -32,12 +27,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:33.690535258Z", - "start_time": "2023-11-09T18:41:33.175105562Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "size = 10000\n", @@ -60,12 +50,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:35.114704154Z", - "start_time": "2023-11-09T18:41:33.175205312Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.hist(sig_data, color='b', alpha=0.5, bins=30, label='electron')\n", @@ -92,12 +77,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:35.169658981Z", - "start_time": "2023-11-09T18:41:35.114998403Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "n_sig1, n_bck1 = 8000, 2000\n", @@ -109,12 +89,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:35.640708376Z", - "start_time": "2023-11-09T18:41:35.159083078Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=[15, 6])\n", @@ -165,12 +140,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:35.761702659Z", - "start_time": "2023-11-09T18:41:35.641363489Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def plot_with_weights(datas, weights, **kargs):\n", @@ -183,12 +153,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:35.974742303Z", - "start_time": "2023-11-09T18:41:35.682988823Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plot_with_weights([first_bin, second_bin], [n_bck2, -n_bck1], density=True, label='reconstructed electron')\n", @@ -209,12 +174,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:36.278752546Z", - "start_time": "2023-11-09T18:41:35.973225726Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plot_with_weights([first_bin, second_bin], [n_bck2, -n_bck1], density=True, label='reconstructed electons', edgecolor='none')\n", @@ -234,12 +194,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:36.370668105Z", - "start_time": "2023-11-09T18:41:36.281084677Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.bar([0, 2, 4], [3, 2, 1], width=1, color='b', alpha=0.5)\n", @@ -282,12 +237,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:36.463010940Z", - "start_time": "2023-11-09T18:41:36.367032133Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "mu = zfit.Parameter('mu', 5279, 5100, 5400)\n", @@ -299,12 +249,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:36.474956577Z", - "start_time": "2023-11-09T18:41:36.420048637Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "obs = zfit.Space('mass', (5000, 6000))\n", @@ -327,12 +272,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:39.707390072Z", - "start_time": "2023-11-09T18:41:36.467019213Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Signal distributions.\n", @@ -376,12 +316,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:41.699026635Z", - "start_time": "2023-11-09T18:41:39.708082190Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Builds the loss.\n", @@ -400,12 +335,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:41.701583440Z", - "start_time": "2023-11-09T18:41:41.690949558Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "def plot_fit_projection(model, data, nbins=30, ax=None):\n", @@ -440,12 +370,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:42.049462266Z", - "start_time": "2023-11-09T18:41:41.695055246Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Visualization of the result.\n", @@ -478,13 +403,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "scrolled": true, - "ExecuteTime": { - "end_time": "2023-11-09T18:41:42.306789925Z", - "start_time": "2023-11-09T18:41:42.047242018Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from hepstats.splot import compute_sweights\n", @@ -497,12 +416,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:42.308477707Z", - "start_time": "2023-11-09T18:41:42.255058309Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "print(\"Sum of signal sWeights: \", np.sum(weights[sig_yield]))\n", @@ -519,12 +433,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:43.917695568Z", - "start_time": "2023-11-09T18:41:42.302977717Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "fig, axs = plt.subplots(1, 2, figsize=(16, 6))\n", @@ -556,12 +465,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:43.935640680Z", - "start_time": "2023-11-09T18:41:43.914957342Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "print(f\"Correlation between m and t: {np.corrcoef(np_m_sw, np_t_sw)[0, 1]}\")" @@ -577,12 +481,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:44.217521866Z", - "start_time": "2023-11-09T18:41:43.919368369Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.hist(np_m_sw, bins=100, range=(5000, 6000), weights=weights[sig_yield]);" @@ -604,12 +503,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:44.222398796Z", - "start_time": "2023-11-09T18:41:44.214787904Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from scipy.stats import expon, norm" @@ -618,12 +512,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:44.615692777Z", - "start_time": "2023-11-09T18:41:44.221274727Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=[15, 6])\n", @@ -662,12 +551,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:44.950482870Z", - "start_time": "2023-11-09T18:41:44.614171146Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=[15, 6])\n", @@ -701,12 +585,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:45.182653107Z", - "start_time": "2023-11-09T18:41:44.945907133Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "x = np.linspace(0, 10)\n", @@ -731,12 +610,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:45.200613740Z", - "start_time": "2023-11-09T18:41:45.185498912Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import pandas\n", @@ -748,12 +622,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:45.535033034Z", - "start_time": "2023-11-09T18:41:45.195431060Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.plot(mass, probs.sig, label='sig probability')\n", @@ -773,12 +642,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:45.636452372Z", - "start_time": "2023-11-09T18:41:45.536975210Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from hep_ml import splot\n", @@ -796,12 +660,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:46.023070838Z", - "start_time": "2023-11-09T18:41:45.649725765Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.plot(mass, sWeights.sig, label='sig sWeight')\n", @@ -821,12 +680,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:46.636288872Z", - "start_time": "2023-11-09T18:41:46.023629125Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=[15, 7])\n", @@ -857,12 +711,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:46.670488310Z", - "start_time": "2023-11-09T18:41:46.636425810Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "np.corrcoef(abs(mass - 4), p) [0, 1]" @@ -878,12 +727,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:46.808545533Z", - "start_time": "2023-11-09T18:41:46.644686446Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "print(np.corrcoef(abs(sig_mass - 4), sig_p)[0, 1])\n", @@ -900,12 +744,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:47.029394075Z", - "start_time": "2023-11-09T18:41:46.669469133Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "hist_conf = dict(bins=30, alpha=0.5, range=[-1, 7])\n", diff --git a/advanced-python/70ScikitHEPUniverse.ipynb b/advanced-python/70ScikitHEPUniverse.ipynb index d311cbf3..98710b73 100644 --- a/advanced-python/70ScikitHEPUniverse.ipynb +++ b/advanced-python/70ScikitHEPUniverse.ipynb @@ -28,12 +28,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:37.509856814Z", - "start_time": "2023-11-09T18:41:36.790957906Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import formulate\n", @@ -45,12 +40,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:37.519217132Z", - "start_time": "2023-11-09T18:41:37.505747578Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "momentum.to_numexpr() # as used in Pandas eval/query" @@ -59,12 +49,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:37.569995559Z", - "start_time": "2023-11-09T18:41:37.510628475Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "momentum.to_root() # as used in ROOT" @@ -82,12 +67,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:37.733545504Z", - "start_time": "2023-11-09T18:41:37.522736067Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "# Particle\n", @@ -107,12 +87,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:37.779734080Z", - "start_time": "2023-11-09T18:41:37.779040900Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "piplus.mass" @@ -121,12 +96,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:37.780222164Z", - "start_time": "2023-11-09T18:41:37.779268397Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "piplus.charge" @@ -135,12 +105,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:37.785865315Z", - "start_time": "2023-11-09T18:41:37.779388976Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "piplus.width" @@ -149,12 +114,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:37.786318570Z", - "start_time": "2023-11-09T18:41:37.779605715Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "piplus.name" @@ -171,12 +131,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:37.903977630Z", - "start_time": "2023-11-09T18:41:37.779707095Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "Particle.findall(lambda p: p.pdgid.has_bottom and p.charge==0)" @@ -197,12 +152,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:37.905992034Z", - "start_time": "2023-11-09T18:41:37.835409659Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "from hepunits.constants import c_light\n", @@ -213,12 +163,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:37.907634350Z", - "start_time": "2023-11-09T18:41:37.841358263Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import hepunits as u # u for units" @@ -227,12 +172,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:37.910230835Z", - "start_time": "2023-11-09T18:41:37.883077241Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "150 * u.MeV + 1.1 * u.GeV # result in MeV" @@ -255,12 +195,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:38.058044969Z", - "start_time": "2023-11-09T18:41:37.883233940Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import vector" @@ -272,10 +207,6 @@ "metadata": { "pycharm": { "name": "#%%\n" - }, - "ExecuteTime": { - "end_time": "2023-11-09T18:41:38.087743681Z", - "start_time": "2023-11-09T18:41:38.000640796Z" } }, "outputs": [], @@ -316,12 +247,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:38.088074302Z", - "start_time": "2023-11-09T18:41:38.047094192Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "vector.obj(x=1, y=2, z=3).theta\n", @@ -331,12 +257,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:38.088361257Z", - "start_time": "2023-11-09T18:41:38.047282206Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "vector.obj(x=3, y=4, z=-2, t=10) # Cartesian 4D vector" @@ -345,12 +266,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2023-11-09T18:41:38.088472813Z", - "start_time": "2023-11-09T18:41:38.047400199Z" - } - }, + "metadata": {}, "outputs": [], "source": [] } diff --git a/environment.yml b/environment.yml index 0e4a96ef..a10aefbc 100644 --- a/environment.yml +++ b/environment.yml @@ -19,7 +19,7 @@ dependencies: - pip - scikit-learn - scipy - - uproot + - uproot <5.0.0 # 5.0.0 breaks the httpsource argument with open, TODO upgrade (what's the equivalent?) in the "get_truth" function - uproot3 - vector - wget