-
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 #338 from alercebroker/revert-336-revert-335-cross…
…match Crossmatch
- Loading branch information
Showing
18 changed files
with
2,591 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import os | ||
|
||
import uvicorn | ||
|
||
|
||
def run(): | ||
port = os.getenv("PORT", default=8000) | ||
uvicorn.run("crossmatch_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
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,33 @@ | ||
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/crossmatch_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,19 @@ | ||
import requests | ||
|
||
def get_alerce_data(ra, dec, radius): | ||
base_url = "https://catshtm.alerce.online/crossmatch_all" | ||
|
||
# Parameters for the API request | ||
params = { | ||
"ra": ra, | ||
"dec": dec, | ||
"radius": radius | ||
} | ||
|
||
try: | ||
# Make the GET request | ||
response = requests.get(base_url, params=params) | ||
return response.json() # Return the JSON response | ||
|
||
except requests.RequestException as e: | ||
return f"Error: {str(e)}" |
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.
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 | ||
import os | ||
from typing import Annotated | ||
from fastapi import Query | ||
import json | ||
|
||
from core.services.object import get_object | ||
from fastapi import APIRouter, Request, HTTPException | ||
from fastapi.templating import Jinja2Templates | ||
from fastapi.responses import HTMLResponse | ||
from ..result_handler import handle_error, handle_success | ||
from ..get_crossmatch_data import get_alerce_data | ||
router = APIRouter() | ||
templates = Jinja2Templates( | ||
directory="src/crossmatch_api/templates", autoescape=True, auto_reload=True | ||
) | ||
templates.env.globals["API_URL"] = os.getenv( | ||
"API_URL", "http://localhost:8005" | ||
) | ||
|
||
@router.get("/crossmatch/{oid}", response_class=HTMLResponse) | ||
async def object_mag_app( | ||
request: Request, | ||
oid: str, | ||
): | ||
|
||
object = get_object(oid,session_factory = request.app.state.psql_session) | ||
|
||
cross = get_alerce_data(object.meanra, object.meandec, 50) | ||
|
||
cross_keys_raw = [] | ||
for i in range(len(cross)): | ||
cross_keys_raw.append(next(iter(cross[i].keys()))) | ||
|
||
cross_keys = [] | ||
for i in range(len(cross)): | ||
if next(iter(cross[i].values()))['distance']['value'] <= 20: | ||
cross_keys.append(next(iter(cross[i].keys()))) | ||
|
||
return templates.TemplateResponse( | ||
name='crossmatch.html.jinja', | ||
context={'request': request, | ||
'cross': cross, | ||
'crossKeys': cross_keys | ||
}, | ||
) |
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,22 @@ | ||
from fastapi import APIRouter, HTTPException, Request | ||
from fastapi.responses import HTMLResponse | ||
from fastapi.templating import Jinja2Templates | ||
|
||
from core.exceptions import ObjectNotFound | ||
from core.services.object import get_object | ||
from ..get_crossmatch_data import get_alerce_data | ||
|
||
|
||
router = APIRouter() | ||
templates = Jinja2Templates( | ||
directory="src/crossmatch_api/templates", autoescape=True, auto_reload=True | ||
) | ||
|
||
@router.get("/") | ||
def root(): | ||
return "this is the crossmatch module" | ||
|
||
|
||
@router.get("/healthcheck") | ||
def healthcheck(): | ||
return "OK" |
Oops, something went wrong.