From 7cd2299a538fd6434017d2a22c14691ab90acc97 Mon Sep 17 00:00:00 2001 From: zhuwq Date: Sat, 17 Aug 2024 18:34:29 -0700 Subject: [PATCH] update --- app.py | 2 - docs/app.py | 1 + docs/example_fastapi.ipynb | 100 +++++++++++++++++-------------------- 3 files changed, 47 insertions(+), 56 deletions(-) create mode 120000 docs/app.py diff --git a/app.py b/app.py index 019431d..1744ff7 100644 --- a/app.py +++ b/app.py @@ -19,7 +19,6 @@ def predict(picks: dict, stations: dict, config: dict): picks = pd.DataFrame(picks) picks["phase_time"] = pd.to_datetime(picks["phase_time"]) stations = pd.DataFrame(stations) - print(stations) events_, picks_ = run_gamma(picks, stations, config) picks_ = picks_.to_dict(orient="records") events_ = events_.to_dict(orient="records") @@ -132,7 +131,6 @@ def run_gamma(picks, stations, config_): events, assignments = association(picks, stations, config, 0, config["method"]) - print(events) events = pd.DataFrame(events) events[["longitude", "latitude"]] = events.apply( lambda x: pd.Series(proj(longitude=x["x(km)"], latitude=x["y(km)"], inverse=True)), axis=1 diff --git a/docs/app.py b/docs/app.py new file mode 120000 index 0000000..a709fb1 --- /dev/null +++ b/docs/app.py @@ -0,0 +1 @@ +../app.py \ No newline at end of file diff --git a/docs/example_fastapi.ipynb b/docs/example_fastapi.ipynb index b0ce951..544bc32 100644 --- a/docs/example_fastapi.ipynb +++ b/docs/example_fastapi.ipynb @@ -27,11 +27,12 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ - "ADLOC_API_URL = \"http://127.0.0.1:8000\"\n" + "# GAMMA_API_URL = \"http://127.0.0.1:8000\"\n", + "GAMMA_API_URL = \"https://ai4eps-gamma.hf.space\"\n" ] }, { @@ -1262,15 +1263,6 @@ "plt.show()" ] }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [], - "source": [ - "from app import run_gamma" - ] - }, { "cell_type": "code", "execution_count": 14, @@ -1281,29 +1273,16 @@ "output_type": "stream", "text": [ "Associating 108 picks with 1 CPUs\n", - "." + ".[{'time': '2019-07-04T17:02:55.008', 'magnitude': 4.339747018742565, 'sigma_time': 0.32614604178710815, 'sigma_amp': 0.3280145301432932, 'cov_time_amp': 0.05017926672657749, 'gamma_score': 97.9995778560105, 'num_picks': 98, 'num_p_picks': 49, 'num_s_picks': 49, 'event_index': 1, 'x(km)': 0.7269972359810098, 'y(km)': 0.5939380031466018, 'z(km)': 16.20145420615579}]\n" ] - } - ], - "source": [ - "config[\"region\"] = \"Ridgecrest\"\n", - "config[\"event_index\"] = 1\n", - "\n", - "evengts_, picks_ = run_gamma(picks, stations, config)" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ + }, { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 15, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" }, @@ -1319,18 +1298,24 @@ } ], "source": [ + "from app import run_gamma\n", + "\n", + "config[\"region\"] = \"Ridgecrest\"\n", + "config[\"event_index\"] = 1\n", + "\n", + "evengts_, picks_ = run_gamma(picks, stations, config)\n", + "\n", "plt.figure()\n", "mapping_color = lambda x: f\"C{x}\" if x!= -1 else \"black\"\n", - "plt.scatter(picks_[\"timestamp\"], picks_[\"latitude\"], c=picks_[\"event_index\"].apply(mapping_color), s=10)" + "plt.scatter(picks_[\"phase_time\"], picks_[\"latitude\"], c=picks_[\"event_index\"].apply(mapping_color), s=10)" ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ - "# call loalhost:8000/predict\n", "picks_ = picks.copy()\n", "picks_[\"phase_time\"] = picks_[\"phase_time\"].apply(lambda x: x.isoformat())\n", "stations_ = stations.copy()\n", @@ -1338,7 +1323,7 @@ "picks_ = picks_.to_dict(orient=\"records\")\n", "stations_ = stations.to_dict(orient=\"records\")\n", "\n", - "response = requests.post(f\"{ADLOC_API_URL}/predict/\", json={\"picks\": {\"data\":picks_}, \"stations\": {\"data\": stations_}, \"config\": config})\n", + "response = requests.post(f\"{GAMMA_API_URL}/predict/\", json={\"picks\": {\"data\":picks_}, \"stations\": {\"data\": stations_}, \"config\": config})\n", "\n", "if response.status_code == 200:\n", " result = response.json()\n", @@ -1350,16 +1335,16 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 29, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" }, @@ -1386,7 +1371,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 33, "metadata": {}, "outputs": [ { @@ -1429,18 +1414,18 @@ " \n", " 0\n", " 2019-07-04T17:02:55.008\n", - " 4.33975\n", + " 4.339747\n", " 0.326146\n", " 0.328015\n", " 0.050179\n", - " 97.999762\n", + " 97.999578\n", " 98\n", " 49\n", " 49\n", " 1\n", - " -117.495965\n", + " -117.495966\n", " 35.710353\n", - " 16.201628\n", + " 16.201454\n", " \n", " \n", "\n", @@ -1448,16 +1433,16 @@ ], "text/plain": [ " time magnitude sigma_time sigma_amp cov_time_amp \\\n", - "0 2019-07-04T17:02:55.008 4.33975 0.326146 0.328015 0.050179 \n", + "0 2019-07-04T17:02:55.008 4.339747 0.326146 0.328015 0.050179 \n", "\n", " gamma_score num_picks num_p_picks num_s_picks event_index longitude \\\n", - "0 97.999762 98 49 49 1 -117.495965 \n", + "0 97.999578 98 49 49 1 -117.495966 \n", "\n", " latitude depth_km \n", - "0 35.710353 16.201628 " + "0 35.710353 16.201454 " ] }, - "execution_count": 31, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -1468,7 +1453,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -1529,7 +1514,7 @@ " -117.462\n", " 694.5\n", " 1\n", - " 0.000156\n", + " 0.000016\n", " \n", " \n", " 2\n", @@ -1542,7 +1527,7 @@ " -117.598\n", " 775.0\n", " 1\n", - " 0.230912\n", + " 0.185935\n", " \n", " \n", " 3\n", @@ -1555,7 +1540,7 @@ " -117.598\n", " 775.0\n", " 1\n", - " 0.748432\n", + " 0.300784\n", " \n", " \n", " 4\n", @@ -1568,7 +1553,7 @@ " -117.598\n", " 775.0\n", " 1\n", - " 0.788955\n", + " 0.306604\n", " \n", " \n", " ...\n", @@ -1669,10 +1654,10 @@ "\n", " phase_type latitude longitude elevation_m event_index gamma_score \n", "0 S 36.025 -117.765 1143.0 -1 -1.000000 \n", - "1 P 35.587 -117.462 694.5 1 0.000156 \n", - "2 P 35.816 -117.598 775.0 1 0.230912 \n", - "3 P 35.816 -117.598 775.0 1 0.748432 \n", - "4 P 35.816 -117.598 775.0 1 0.788955 \n", + "1 P 35.587 -117.462 694.5 1 0.000016 \n", + "2 P 35.816 -117.598 775.0 1 0.185935 \n", + "3 P 35.816 -117.598 775.0 1 0.300784 \n", + "4 P 35.816 -117.598 775.0 1 0.306604 \n", ".. ... ... ... ... ... ... \n", "103 P 36.025 -117.765 1143.0 -1 -1.000000 \n", "104 P 35.982 -117.809 1469.0 -1 -1.000000 \n", @@ -1683,7 +1668,7 @@ "[108 rows x 10 columns]" ] }, - "execution_count": 30, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } @@ -1692,6 +1677,13 @@ "picks_" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null,