Skip to content

Commit

Permalink
update fastapi
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuwq0 committed Aug 18, 2024
1 parent c232215 commit d255d32
Show file tree
Hide file tree
Showing 3 changed files with 1,257 additions and 516 deletions.
31 changes: 13 additions & 18 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import multiprocessing as mp
import os
from dataclasses import dataclass, asdict
from dataclasses import asdict, dataclass

import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -11,7 +11,6 @@
from pyproj import Proj

import adloc

from adloc.eikonal2d import init_eikonal2d
from adloc.sacloc2d import ADLoc
from adloc.utils import invert_location, invert_location_iter
Expand All @@ -21,7 +20,7 @@

@app.get("/")
def greet_json():
return {"Hello": "World!"}
return {"message": "Hello ADLoc!"}


@app.post("/predict/")
Expand All @@ -31,16 +30,17 @@ 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)
picks_, events_ = run_adloc(picks, stations, config)
picks_ = picks_.to_dict(orient="records")
events_, picks_ = run_adloc(picks, stations, config)
if events_ is None:
return {"events": None, "picks": None}
events_ = events_.to_dict(orient="records")
picks_ = picks_.to_dict(orient="records")

return {"picks": picks_, "events": events_}
return {"events": events_, "picks": picks_}


def set_config(region="ridgecrest"):


config = {
"min_picks": 8,
"min_picks_ratio": 0.2,
Expand All @@ -52,7 +52,6 @@ def set_config(region="ridgecrest"):
"use_amplitude": False,
}


# ## Domain
if region.lower() == "ridgecrest":
config.update(
Expand All @@ -67,7 +66,6 @@ def set_config(region="ridgecrest"):
}
)


lon0 = (config["minlongitude"] + config["maxlongitude"]) / 2
lat0 = (config["minlatitude"] + config["maxlatitude"]) / 2
proj = Proj(f"+proj=sterea +lon_0={lon0} +lat_0={lat0} +units=km")
Expand Down Expand Up @@ -115,16 +113,16 @@ def set_config(region="ridgecrest"):

return config


config = set_config()


# %%
def run_adloc(picks, stations, config_):


# %%
config.update(config_)

proj = config["proj"]

# %%
Expand All @@ -133,7 +131,6 @@ def run_adloc(picks, stations, config_):
)
stations["z_km"] = stations["elevation_m"].apply(lambda x: -x / 1e3)


# %%
mapping_phase_type_int = {"P": 0, "S": 1}
picks["phase_type"] = picks["phase_type"].map(mapping_phase_type_int)
Expand All @@ -154,22 +151,20 @@ def run_adloc(picks, stations, config_):

if (picks is None) or (events is None):
return None, None

# %%
if "event_index" not in events.columns:
events["event_index"] = events.merge(picks[["idx_eve", "event_index"]], on="idx_eve")["event_index"]
events[["longitude", "latitude"]] = events.apply(
lambda x: pd.Series(proj(x["x_km"], x["y_km"], inverse=True)), axis=1
)
events["depth_km"] = events["z_km"]
events.drop(["idx_eve", "x_km", "y_km", "z_km"], axis=1, inplace=True, errors="ignore")
events = events.drop(["idx_eve", "x_km", "y_km", "z_km"], axis=1, errors="ignore")
events.sort_values(["time"], inplace=True)

picks.rename({"mask": "adloc_mask", "residual_s": "adloc_residual_s"}, axis=1, inplace=True)
picks["phase_type"] = picks["phase_type"].map({0: "P", 1: "S"})
picks.drop(["idx_eve", "idx_sta"], axis=1, inplace=True, errors="ignore")
picks = picks.drop(["idx_eve", "idx_sta"], axis=1, errors="ignore")
picks.sort_values(["phase_time"], inplace=True)

return picks, events


return events, picks
1 change: 1 addition & 0 deletions docs/app.py
Loading

0 comments on commit d255d32

Please sign in to comment.