Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Get GCP distributed fitting example working #4

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions binder/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ funcx-endpoint==0.2.2
funcx==0.2.2
# gcp
aiohttp==3.7.4.post0
# interactive jupyterlab
ipympl==0.7.0
131 changes: 124 additions & 7 deletions google-cloud-platform/fittting-faas-gcp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,91 @@
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"# Interactive matplotlib\n",
"# %matplotlib widget\n",
"%matplotlib notebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5d2ccc8-048d-42ff-a623-c8cb908b814a",
"metadata": {},
"outputs": [],
"source": [
"from descartes import PolygonPatch\n",
"\n",
"# ! pip install pandas\n",
"from shapely.geometry.polygon import Polygon"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1957ccfa-9637-4236-a9a6-374189fdfd25",
"metadata": {},
"outputs": [],
"source": [
"sys.path.insert(0, Path.cwd())\n",
"\n",
"from collections import namedtuple\n",
"\n",
"from interpolate import main as interpolate_main\n",
"from multiplex import main as multiplex_main\n",
"\n",
"# import ROOT\n",
"\n",
"\n",
"def make_results():\n",
" dataList = harvest_results(regions=[\"A\", \"C\"])\n",
" if not (dataList[0][1] or dataList[1][1]):\n",
" return None, None\n",
"\n",
" d = {\n",
" \"figureOfMerit\": \"CLsexp\",\n",
" \"modelDef\": \"msb,mn2,mn1\",\n",
" \"ignoreTheory\": True,\n",
" \"ignoreUL\": True,\n",
" \"debug\": False,\n",
" }\n",
" args = namedtuple(\"Args\", d.keys())(**d)\n",
" mux_data = multiplex_main(\n",
" args,\n",
" inputDataList=dataList,\n",
" ).to_dict(orient=\"records\")\n",
"\n",
" d = {\n",
" \"nominalLabel\": \"Nominal\",\n",
" \"xMin\": None,\n",
" \"xMax\": None,\n",
" \"yMin\": None,\n",
" \"yMax\": None,\n",
" \"smoothing\": \"0.1\",\n",
" \"areaThreshold\": 0,\n",
" \"xResolution\": 100,\n",
" \"yResolution\": 100,\n",
" \"xVariable\": \"msb\",\n",
" \"yVariable\": \"mn2\",\n",
" \"closedBands\": False,\n",
" \"forbiddenFunction\": \"x\",\n",
" \"debug\": False,\n",
" \"logX\": False,\n",
" \"logY\": False,\n",
" \"noSig\": False,\n",
" \"interpolation\": \"multiquadric\",\n",
" \"interpolationEpsilon\": 0,\n",
" \"level\": 1.64485362695,\n",
" \"useROOT\": False,\n",
" \"sigmax\": 5,\n",
" \"useUpperLimit\": False,\n",
" \"ignoreUncertainty\": False,\n",
" \"fixedParamsFile\": \"\",\n",
" }\n",
" args = namedtuple(\"Args\", d.keys())(**d)\n",
" r = interpolate_main(args, mux_data)\n",
" return r, dataList"
]
},
{
"cell_type": "markdown",
"id": "4f3d0ea7-d495-4899-8b94-c7ccbbeb8189",
Expand All @@ -49,7 +131,10 @@
"metadata": {},
"outputs": [],
"source": [
"def make_harvest_from_result(result, masses):\n",
"# def make_harvest_from_result(result, masses):\n",
"def make_harvest_from_result(masses, result):\n",
" # print(f\"{result=}\")\n",
" # print(f\"{masses=}\")\n",
" return {\n",
" \"CLs\": result[\"CLs_obs\"],\n",
" \"CLsexp\": result[\"CLs_exp\"][2],\n",
Expand Down Expand Up @@ -109,6 +194,8 @@
" # only use 60 GeV\n",
" if masses[2] != 60:\n",
" continue\n",
" # print(result) # DEBUG\n",
" # print(masses)\n",
" harvest.append(make_harvest_from_result(result, masses))\n",
" dataList.append((f\"region{region}\", harvest))\n",
" return dataList"
Expand Down Expand Up @@ -208,6 +295,9 @@
" r\"$m(\\tilde{\\chi}_2^0)$ [GeV]\", fontdict=dict(ha=\"right\", va=\"center\", size=20)\n",
" )\n",
"\n",
" # _handles, _labels = ax.get_legend_handles_labels()\n",
" # if _handles:\n",
" # ax.legend(loc=(0.05, 0.6))\n",
" ax.legend(loc=(0.05, 0.6))\n",
" ax.xaxis.set_label_coords(1.0, -0.1)\n",
" ax.yaxis.set_label_coords(-0.15, 1.0)\n",
Expand Down Expand Up @@ -307,8 +397,9 @@
" )\n",
" print(f\"Number of Patches: {len(patches)}\")\n",
" results = await make_requests(f\"{fitting_service_url}/region{region}\", patches[:])\n",
" all_ok = np.all([x[1][0] == 200 for x in results])\n",
" print(f\"->Number of Results: {len(results)} {time.time()-start} | {all_ok}\")\n",
" http_status_code_OK = 200\n",
" all_ok = np.all([x[1][0] == http_status_code_OK for x in results])\n",
" print(f\"-> Number of Results: {len(results)} {time.time()-start} | {all_ok}\")\n",
" done_live[region] = True\n",
" return results"
]
Expand All @@ -320,9 +411,13 @@
"metadata": {},
"outputs": [],
"source": [
"def plot(ax, data, color=\"steelblue\", label=\"(in progress)\", showInterPolated=True):\n",
"def plot_results(\n",
" ax, data, color=\"steelblue\", label=\"(in progress)\", showInterPolated=True\n",
"):\n",
" ax.cla()\n",
"\n",
" # print(data_live[\"A\"].items()) # DEBUG\n",
" # print((k, r) for k, r in data_live[\"A\"].items()) # DEBUG\n",
" hdata = [\n",
" (\n",
" \"regionA\",\n",
Expand Down Expand Up @@ -365,9 +460,9 @@
" global data_live\n",
" global done_live\n",
" while not (done_live[\"A\"] and done_live[\"C\"]):\n",
" plot(ax, data_live, showInterPolated=True)\n",
" plot_results(ax, data_live, showInterPolated=True)\n",
" await asyncio.sleep(1.0)\n",
" plot(ax, data_live, color=\"gold\", label=\"\")\n",
" plot_results(ax, data_live, color=\"gold\", label=\"\")\n",
" print(\"done plotting\")"
]
},
Expand Down Expand Up @@ -408,6 +503,7 @@
"outputs": [],
"source": [
"# cloud resources to re-perform stat. analysis\n",
"# fitting_service_url is a SECRET that should be of the form: https://country-region-project_name-123456.cloudfunctions.net\n",
"with open(\"fitting_service_url.txt\") as gcp_url_file:\n",
" fitting_service_url = str(gcp_url_file.read().rstrip())"
]
Expand Down Expand Up @@ -436,12 +532,33 @@
"outputs": [],
"source": [
"reset_data_live()\n",
"plot(ax, data_live)\n",
"plot_results(ax, data_live)\n",
"start = time.time()\n",
"results = await asyncio.gather(run_region(\"A\"), run_region(\"C\"), plotting_loop())\n",
"time.time() - start"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2df0f8fc",
"metadata": {},
"outputs": [],
"source": [
"plot_results(ax, data_live)\n",
"ax.figure.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e0afd40-a4a1-4658-a198-d8254687d984",
"metadata": {},
"outputs": [],
"source": [
"# results"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down