-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #334 from alercebroker/probability
Probability API
- Loading branch information
Showing
19 changed files
with
2,763 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import os | ||
|
||
import uvicorn | ||
|
||
|
||
def run(): | ||
port = os.getenv("PORT", default=8000) | ||
uvicorn.run("probability_api.api:app", port=int(port), reload=True, reload_dirs=[".", "../libs"]) | ||
|
||
|
||
if __name__ == "__main__": | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
from fastapi import FastAPI | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from fastapi.staticfiles import StaticFiles | ||
from prometheus_fastapi_instrumentator import Instrumentator | ||
|
||
from .routes import htmx, rest | ||
from database.mongo import connect as connect_mongo | ||
from database.sql import connect as connect_sql, session_wrapper | ||
|
||
app = FastAPI(openapi_url="/v2/object/openapi.json") | ||
app.state.mongo_db = None | ||
psql_engine = connect_sql() | ||
app.state.psql_session = session_wrapper(psql_engine) | ||
instrumentator = Instrumentator().instrument(app).expose(app) | ||
|
||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
|
||
app.include_router(rest.router) | ||
app.include_router(prefix="/htmx", router=htmx.router) | ||
|
||
app.mount("/static", StaticFiles(directory="src/probability_api/static"), name="static") | ||
|
||
|
||
@app.get("/openapi.json") | ||
def custom_swagger_route(): | ||
return app.openapi() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import re | ||
|
||
|
||
def update_config_dict(config_dict): | ||
config_dict["FILTERS_MAP"] = { | ||
"filter_atlas_detections": filter_atlas_detection_non_detection, | ||
"filter_atlas_non_detections": filter_atlas_detection_non_detection, | ||
"filter_atlas_lightcurve": filter_atlas_lightcurve, | ||
"filter_atlas_forced_photometry": filter_atlas_detection_non_detection, | ||
} | ||
|
||
|
||
def get_filters_map(): | ||
return { | ||
"filter_atlas_detections": filter_atlas_detection_non_detection, | ||
"filter_atlas_non_detections": filter_atlas_detection_non_detection, | ||
"filter_atlas_lightcurve": filter_atlas_lightcurve, | ||
"filter_atlas_forced_photometry": filter_atlas_detection_non_detection, | ||
} | ||
|
||
|
||
def filter_atlas_detection_non_detection(lc_object): | ||
pattern = re.compile("atlas*", re.IGNORECASE) | ||
if pattern.match(lc_object["tid"]): | ||
return False | ||
return True | ||
|
||
|
||
def filter_atlas_lightcurve(lc_object): | ||
non_filtered_detections = [] | ||
non_filtered_non_detections = [] | ||
non_filtered_forced_photometry = [] | ||
|
||
for detection in lc_object["detections"]: | ||
if filter_atlas_detection_non_detection(detection): | ||
non_filtered_detections.append(detection) | ||
for non_detecton in lc_object["non_detections"]: | ||
if filter_atlas_detection_non_detection(non_detecton): | ||
non_filtered_non_detections.append(non_detecton) | ||
for forced_photometry in lc_object["forced_photometry"]: | ||
if filter_atlas_detection_non_detection(forced_photometry): | ||
non_filtered_forced_photometry.append(forced_photometry) | ||
|
||
lc_object["detections"] = non_filtered_detections | ||
lc_object["non_detections"] = non_filtered_non_detections | ||
lc_object["forced_photometry"] = non_filtered_forced_photometry | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import traceback | ||
import logging | ||
|
||
from fastapi import HTTPException | ||
|
||
from core.exceptions import ( | ||
AtlasNonDetectionError, | ||
DatabaseError, | ||
ObjectNotFound, | ||
SurveyIdError, | ||
) | ||
|
||
|
||
def handle_success(result): | ||
return result | ||
|
||
|
||
def _handle_client_error(err: BaseException, code=400): | ||
raise HTTPException(status_code=code, detail=str(err)) | ||
|
||
|
||
def _handle_server_error(err: BaseException): | ||
if err.__traceback__: | ||
traceback.print_exception(err) | ||
logging.error(err) | ||
raise HTTPException(status_code=500, detail=str(err)) | ||
|
||
|
||
def handle_error(err: BaseException): | ||
if isinstance(err, DatabaseError): | ||
_handle_server_error(err) | ||
if isinstance(err, SurveyIdError): | ||
_handle_client_error(err) | ||
if isinstance(err, AtlasNonDetectionError): | ||
_handle_client_error(err) | ||
if isinstance(err, ObjectNotFound): | ||
_handle_client_error(err, code=404) |
Empty file.
Oops, something went wrong.