Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuwq0 committed Aug 18, 2024
1 parent 19e3273 commit 7cd2299
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 56 deletions.
2 changes: 0 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/app.py
100 changes: 46 additions & 54 deletions docs/example_fastapi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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,
Expand All @@ -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": [
"<matplotlib.collections.PathCollection at 0x31dd37040>"
"<matplotlib.collections.PathCollection at 0x336b22740>"
]
},
"execution_count": 15,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
Expand All @@ -1319,26 +1298,32 @@
}
],
"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",
"\n",
"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",
Expand All @@ -1350,16 +1335,16 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x32d0aba00>"
"<matplotlib.collections.PathCollection at 0x336e17fd0>"
]
},
"execution_count": 29,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
},
Expand All @@ -1386,7 +1371,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 33,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -1429,35 +1414,35 @@
" <tr>\n",
" <th>0</th>\n",
" <td>2019-07-04T17:02:55.008</td>\n",
" <td>4.33975</td>\n",
" <td>4.339747</td>\n",
" <td>0.326146</td>\n",
" <td>0.328015</td>\n",
" <td>0.050179</td>\n",
" <td>97.999762</td>\n",
" <td>97.999578</td>\n",
" <td>98</td>\n",
" <td>49</td>\n",
" <td>49</td>\n",
" <td>1</td>\n",
" <td>-117.495965</td>\n",
" <td>-117.495966</td>\n",
" <td>35.710353</td>\n",
" <td>16.201628</td>\n",
" <td>16.201454</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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"
}
Expand All @@ -1468,7 +1453,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 34,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -1529,7 +1514,7 @@
" <td>-117.462</td>\n",
" <td>694.5</td>\n",
" <td>1</td>\n",
" <td>0.000156</td>\n",
" <td>0.000016</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
Expand All @@ -1542,7 +1527,7 @@
" <td>-117.598</td>\n",
" <td>775.0</td>\n",
" <td>1</td>\n",
" <td>0.230912</td>\n",
" <td>0.185935</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
Expand All @@ -1555,7 +1540,7 @@
" <td>-117.598</td>\n",
" <td>775.0</td>\n",
" <td>1</td>\n",
" <td>0.748432</td>\n",
" <td>0.300784</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
Expand All @@ -1568,7 +1553,7 @@
" <td>-117.598</td>\n",
" <td>775.0</td>\n",
" <td>1</td>\n",
" <td>0.788955</td>\n",
" <td>0.306604</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
Expand Down Expand Up @@ -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",
Expand All @@ -1683,7 +1668,7 @@
"[108 rows x 10 columns]"
]
},
"execution_count": 30,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1692,6 +1677,13 @@
"picks_"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 7cd2299

Please sign in to comment.