Skip to content

Commit

Permalink
Remove Pandera as dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ntellis committed Jan 24, 2024
1 parent adb43e1 commit 4f8ee16
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 89 deletions.
11 changes: 4 additions & 7 deletions cutouts/filter.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import logging

import numpy as np
import pandera as pa
from pandera.typing import DataFrame
import pandas as pd

from .io.types import CutoutRequest, CutoutResult, CutoutsResultSchema
from .io.types import CutoutRequest, CutoutResult

logger = logging.getLogger(__file__)


@pa.check_types
def select_cutout(
results_df: DataFrame[CutoutsResultSchema], cutout_request: CutoutRequest
results_df: pd.DataFrame, cutout_request: CutoutRequest
) -> CutoutResult:
"""
Select the cutout closest to the requested exposure start time +- delta_time.
Expand Down Expand Up @@ -91,9 +89,8 @@ def select_cutout(
return result


@pa.check_types
def select_comparison_cutout(
results_df: DataFrame[CutoutsResultSchema],
results_df: pd.DataFrame,
cutout_result: CutoutResult,
cutout_request: CutoutRequest,
min_time_separation: float = 1 / 24,
Expand Down
9 changes: 3 additions & 6 deletions cutouts/io/nsc.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import logging

import pandera as pa
from pandera.typing import DataFrame
import pandas as pd
from pyvo.dal.sia import SIAResults

from .sia import SIAHandler
from .types import CutoutRequest, CutoutsResultSchema
from .util import exposure_id_from_url

logger = logging.getLogger(__name__)
Expand All @@ -16,10 +14,9 @@ def _get_generic_image_url_from_cutout_url(cutout_url: str):
return cutout_url.split("&POS=")[0]


@pa.check_types
def find_cutouts_nsc_dr2(
cutout_request: CutoutRequest,
) -> DataFrame[CutoutsResultSchema]:
cutout_request: pd.DataFrame,
) -> pd.DataFrame:
"""
Search the NOIRLab Archive for cutouts and images at a given RA, Dec.
Expand Down
7 changes: 2 additions & 5 deletions cutouts/io/skymapper.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import logging

import pandas as pd
import pandera as pa
from pandera.typing import DataFrame
from pyvo.dal.sia import SIAResults

from .sia import SIAHandler
from .types import CutoutRequest, CutoutsResultSchema
from .types import CutoutRequest

logger = logging.getLogger(__name__)

Expand All @@ -22,10 +20,9 @@ def _get_generic_image_url_from_cutout_url(cutout_url: str):
return url_string


@pa.check_types
def find_cutouts_skymapper_dr2(
cutout_request: CutoutRequest,
) -> DataFrame[CutoutsResultSchema]:
) -> pd.DataFrame:
"""
Search the Skymapper SIA service for cutouts and images at a given RA, Dec.
Expand Down
24 changes: 24 additions & 0 deletions cutouts/io/tests/test_nsc_dr2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import os
from contextlib import contextmanager
from unittest.mock import patch
import pathlib
import pickle
import pandas as pd

from astropy.io.votable import parse
from pyvo.dal.sia import SIAResults

from ..nsc import NSC_DR2_SIA, find_cutouts_nsc_dr2
from ...main import get_cutouts
from ..types import CutoutRequest

# 2014 HE199 (2014-04-28T08:07:52.435)
Expand Down Expand Up @@ -67,3 +71,23 @@ def test_sia_nsc_dr2_query():
assert col in results.columns

assert "VR" in results["filter"].values


def test_sia_nsc_dr2_query_():

with mock_sia_nsc_dr2_query(
"nsc_dr2_227.5251615214173_-27.026013823449265_56775.33880132809.xml"
) as mock:
results, comparison_results = get_cutouts(
pd.DataFrame(cutout_request1),
out_dir=pathlib.Path(os.path.dirname(os.path.abspath(__file__)))
)
print(results)
print(comparison_results)

with open('cutout_results.pickle', 'wb') as f:
pickle.dump(results, f)
with open('cutout_comparison_results.pickle', 'wb') as f:
pickle.dump(comparison_results, f)

assert False
42 changes: 0 additions & 42 deletions cutouts/io/types.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,8 @@
from typing import Optional

import pandera as pa
from pandera.typing import Series
from pydantic import BaseModel


class CutoutRequestSchema(pa.SchemaModel):
"""
Dataframe validation for multiple cutout requests
"""

request_id: Optional[Series[str]] = pa.Field(nullable=True)
observatory_code: Series[str] = pa.Field(coerce=True)
exposure_start_mjd: Series[float] = pa.Field(nullable=False, coerce=True)
ra_deg: Series[float] = pa.Field(ge=0, le=360, coerce=True)
dec_deg: Series[float] = pa.Field(ge=-90, le=90, coerce=True)
filter: Optional[
Series[str]
] = pa.Field() # TODO: validate against real list of filters?
exposure_id: Optional[Series[str]] = pa.Field(nullable=True)
exposure_duration: Optional[Series[float]] = pa.Field(
ge=0, le=2000, coerce=True, nullable=True
)
height_arcsec: Series[float] = pa.Field(ge=0, le=200, coerce=True, nullable=True)
width_arcsec: Series[float] = pa.Field(ge=0, le=200, coerce=True, nullable=True)
delta_time: Series[float] = pa.Field(ge=0, le=100, coerce=True, nullable=True)


class CutoutsResultSchema(pa.SchemaModel):
# TODO: ra, dec here are the ra, dec returned by the query and
# these should be equal to the queried ra and dec.
# However, this may not always be true and we may want to consider
# adding additional fields to allow backends to return things such as the
# the center of the image/cutout.
ra_deg: Series[float] = pa.Field(ge=0, le=360, coerce=True)
dec_deg: Series[float] = pa.Field(ge=-90, le=90, coerce=True)
filter: Series[str] = pa.Field()
exposure_id: Series[str] = pa.Field()
exposure_start_mjd: Series[float] = pa.Field(nullable=False, coerce=True)
exposure_duration: Series[float] = pa.Field(ge=0, le=2000, coerce=True)
cutout_url: Series[str] = pa.Field(coerce=True)
image_url: Series[str] = pa.Field(coerce=True)
height_arcsec: Series[float] = pa.Field(ge=0, le=200, coerce=True)
width_arcsec: Series[float] = pa.Field(ge=0, le=200, coerce=True)


class CutoutRequest(BaseModel):
"""
A single cutout request
Expand Down
7 changes: 2 additions & 5 deletions cutouts/io/ztf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import backoff
import numpy as np
import pandas as pd
import pandera as pa
import requests
from pandera.typing import DataFrame

from .types import CutoutRequest, CutoutsResultSchema
from .types import CutoutRequest

logger = logging.getLogger(__file__)

Expand Down Expand Up @@ -83,8 +81,7 @@ def perform_request(search_url):
return response.text


@pa.check_types()
def find_cutouts_ztf(cutout_request: CutoutRequest) -> DataFrame[CutoutsResultSchema]:
def find_cutouts_ztf(cutout_request: CutoutRequest) -> pd.DataFrame:
"""
Search the ZTF service for cutouts and images at a given RA, Dec.
Expand Down
12 changes: 4 additions & 8 deletions cutouts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
import logging
import pathlib
import sys
from typing import Any, Dict, Iterable, Optional, Tuple, cast
from typing import Any, Dict, Iterable, Optional, Tuple

import numpy as np
import pandas as pd
import pandera as pa
from astropy.time import Time
from pandera.typing import DataFrame

from .filter import select_comparison_cutout, select_cutout
from .io import download_cutout, find_cutouts
from .io.types import CutoutRequest, CutoutRequestSchema
from .io.types import CutoutRequest
from .plot import generate_gif, plot_comparison_cutouts, plot_cutouts

logger = logging.getLogger("cutouts")
Expand All @@ -27,9 +25,8 @@
}


@pa.check_types
def get_cutouts(
cutout_requests: DataFrame[CutoutRequestSchema],
cutout_requests: pd.DataFrame,
out_dir: pathlib.Path,
timeout: Optional[int] = 180,
full_image_timeout: Optional[int] = 600,
Expand Down Expand Up @@ -226,7 +223,6 @@ def run_cutouts_from_precovery(
compare: bool = False,
compare_kwargs: Optional[dict] = None,
):

# This seems unecessary but linting fails without it
out_dir_path = pathlib.Path(str(out_dir))
out_file_path = pathlib.Path(str(out_file))
Expand Down Expand Up @@ -268,7 +264,7 @@ def run_cutouts_from_precovery(
cutout_requests["delta_time"].fillna(1e-8, inplace=True)

cutout_results, comparison_results = get_cutouts(
cast(DataFrame[CutoutRequestSchema], cutout_requests),
cutout_requests,
out_dir=out_dir_path,
download_full_image=download_full_image,
timeout=timeout,
Expand Down
134 changes: 119 additions & 15 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,119 @@
astropy
backoff
numpy
pandas
pandera
pyvo
matplotlib
multimethod>=1.9.1
imageio
pytest
pytest-cov
pre-commit
setuptools>=45
wheel
setuptools_scm>=6.0
#
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --all-extras
#
annotated-types==0.6.0
# via pydantic
astropy==6.0.0
# via
# cutouts (pyproject.toml)
# pyvo
astropy-iers-data==0.2024.1.22.0.30.30
# via astropy
backoff==2.2.1
# via cutouts (pyproject.toml)
certifi==2023.11.17
# via requests
cfgv==3.4.0
# via pre-commit
charset-normalizer==3.3.2
# via requests
contourpy==1.2.0
# via matplotlib
coverage[toml]==7.4.0
# via
# coverage
# pytest-cov
cycler==0.12.1
# via matplotlib
distlib==0.3.8
# via virtualenv
filelock==3.13.1
# via virtualenv
fonttools==4.47.2
# via matplotlib
identify==2.5.33
# via pre-commit
idna==3.6
# via requests
imageio==2.33.1
# via cutouts (pyproject.toml)
iniconfig==2.0.0
# via pytest
kiwisolver==1.4.5
# via matplotlib
matplotlib==3.8.2
# via cutouts (pyproject.toml)
nodeenv==1.8.0
# via pre-commit
numpy==1.26.3
# via
# astropy
# contourpy
# cutouts (pyproject.toml)
# imageio
# matplotlib
# pandas
# pyerfa
packaging==23.2
# via
# astropy
# matplotlib
# pytest
pandas==2.2.0
# via cutouts (pyproject.toml)
pillow==10.2.0
# via
# imageio
# matplotlib
platformdirs==4.1.0
# via virtualenv
pluggy==1.4.0
# via pytest
pre-commit==3.6.0
# via cutouts (pyproject.toml)
pydantic==2.5.3
# via cutouts (pyproject.toml)
pydantic-core==2.14.6
# via pydantic
pyerfa==2.0.1.1
# via astropy
pyparsing==3.1.1
# via matplotlib
pytest==7.4.4
# via
# cutouts (pyproject.toml)
# pytest-cov
pytest-cov==4.1.0
# via cutouts (pyproject.toml)
python-dateutil==2.8.2
# via
# matplotlib
# pandas
pytz==2023.3.post1
# via pandas
pyvo==1.5
# via cutouts (pyproject.toml)
pyyaml==6.0.1
# via
# astropy
# pre-commit
requests==2.31.0
# via pyvo
six==1.16.0
# via python-dateutil
typing-extensions==4.9.0
# via
# pydantic
# pydantic-core
tzdata==2023.4
# via pandas
urllib3==2.1.0
# via requests
virtualenv==20.25.0
# via pre-commit

# The following packages are considered to be unsafe in a requirements file:
# setuptools
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ install_requires =
pyvo
matplotlib
imageio
pandera
pydantic

[options.extras_require]
Expand Down

0 comments on commit 4f8ee16

Please sign in to comment.