Skip to content

Commit

Permalink
Merge pull request #109 from astronomy-commons/issue/43/black
Browse files Browse the repository at this point in the history
Addressing pylint findings in tests directory.
  • Loading branch information
delucchi-cmu authored Jul 11, 2023
2 parents 6c11c4b + c4c84ca commit 293a76d
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 101 deletions.
5 changes: 3 additions & 2 deletions tests/.pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ max-bool-expr=5
max-branches=20

# Maximum number of locals for function / method body.
max-locals=15
# We set this higher in tests to allow a more narrative style.
max-locals=30

# Maximum number of parents for a class (see R0901).
max-parents=7
Expand Down Expand Up @@ -503,7 +504,7 @@ ignore-imports=yes
ignore-signatures=yes

# Minimum lines number of a similarity.
min-similarity-lines=4
min-similarity-lines=6


[SPELLING]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ def test_empty_directory(tmp_path, association_catalog_info_data, association_ca

## catalog_info file exists - getting closer
file_name = os.path.join(catalog_path, "catalog_info.json")
with open(
file_name,
"w",
encoding="utf-8",
) as metadata_file:
with open(file_name, "w", encoding="utf-8") as metadata_file:
metadata_file.write(json.dumps(association_catalog_info_data))

with pytest.raises(FileNotFoundError):
Expand Down
2 changes: 1 addition & 1 deletion tests/hipscat/catalog/dataset/test_base_catalog_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ def test_str(base_catalog_info_data):
def test_read_from_file(base_catalog_info_file, assert_catalog_info_matches_dict):
base_cat_info_fp = file_io.get_file_pointer_from_path(base_catalog_info_file)
catalog_info = BaseCatalogInfo.read_from_metadata_file(base_cat_info_fp)
with open(base_catalog_info_file, "r") as cat_info_file:
with open(base_catalog_info_file, "r", encoding="utf-8") as cat_info_file:
catalog_info_json = json.load(cat_info_file)
assert_catalog_info_matches_dict(catalog_info, catalog_info_json)
2 changes: 1 addition & 1 deletion tests/hipscat/catalog/dataset/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_read_from_hipscat(dataset_path, base_catalog_info_file, assert_catalog_
assert dataset.on_disk
assert dataset.catalog_path == dataset_path
assert str(dataset.catalog_base_dir) == dataset_path
with open(base_catalog_info_file, "r") as cat_info_file:
with open(base_catalog_info_file, "r", encoding="utf-8") as cat_info_file:
catalog_info_json = json.load(cat_info_file)
assert_catalog_info_matches_dict(dataset.catalog_info, catalog_info_json)

Expand Down
12 changes: 2 additions & 10 deletions tests/hipscat/catalog/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,15 @@ def test_empty_directory(tmp_path):

## catalog_info file exists - getting closer
file_name = os.path.join(catalog_path, "catalog_info.json")
with open(
file_name,
"w",
encoding="utf-8",
) as metadata_file:
with open(file_name, "w", encoding="utf-8") as metadata_file:
metadata_file.write('{"catalog_name":"empty", "catalog_type":"source"}')

with pytest.raises(FileNotFoundError):
Catalog.read_from_hipscat(catalog_path)

## partition_info file exists - enough to create a catalog
file_name = os.path.join(catalog_path, "partition_info.csv")
with open(
file_name,
"w",
encoding="utf-8",
) as metadata_file:
with open(file_name, "w", encoding="utf-8") as metadata_file:
metadata_file.write("foo")

catalog = Catalog.read_from_hipscat(catalog_path)
Expand Down
2 changes: 1 addition & 1 deletion tests/hipscat/catalog/test_catalog_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ def test_read_from_file(catalog_info_file, assert_catalog_info_matches_dict):
]:
assert column in dataclasses.asdict(catalog_info)

with open(catalog_info_file, "r") as cat_info_file:
with open(catalog_info_file, "r", encoding="utf-8") as cat_info_file:
catalog_info_json = json.load(cat_info_file)
assert_catalog_info_matches_dict(catalog_info, catalog_info_json)
1 change: 0 additions & 1 deletion tests/hipscat/inspection/test_almanac.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def test_linked_catalogs_object(default_almanac):
source_almanac = default_almanac.get_almanac_info(object_almanac.sources[0].catalog_name)
assert source_almanac.catalog_name == "small_sky_source_catalog"

## TODO - this could use some more direct API.
source_catalog = default_almanac.get_catalog(object_almanac.sources[0].catalog_name)
assert source_catalog.catalog_name == "small_sky_source_catalog"

Expand Down
6 changes: 1 addition & 5 deletions tests/hipscat/io/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ def assert_text_file_matches(expected_lines, file_name):
file_name (str): fully-specified path of the file to read
"""
assert os.path.exists(file_name), f"file not found [{file_name}]"
with open(
file_name,
"r",
encoding="utf-8",
) as metadata_file:
with open(file_name, "r", encoding="utf-8") as metadata_file:
contents = metadata_file.readlines()

assert len(expected_lines) == len(
Expand Down
6 changes: 3 additions & 3 deletions tests/hipscat/io/file_io/test_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def test_load_csv_to_pandas(small_sky_dir):


def test_write_df_to_csv(tmp_path):
df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)), columns=list("ABCD"))
random_df = pd.DataFrame(np.random.randint(0, 100, size=(100, 4)), columns=list("ABCD"))
test_file_path = os.path.join(tmp_path, "test.csv")
test_file_pointer = get_file_pointer_from_path(test_file_path)
write_dataframe_to_csv(df, test_file_pointer, index=False)
write_dataframe_to_csv(random_df, test_file_pointer, index=False)
loaded_df = pd.read_csv(test_file_path)
pd.testing.assert_frame_equal(loaded_df, df)
pd.testing.assert_frame_equal(loaded_df, random_df)
3 changes: 1 addition & 2 deletions tests/hipscat/io/test_write_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
import pytest

import hipscat.io.file_io as file_io
from hipscat.io import file_io
import hipscat.io.write_metadata as io
import hipscat.pixel_math as hist
from hipscat.pixel_math.healpix_pixel import HealpixPixel
Expand Down
106 changes: 36 additions & 70 deletions tests/hipscat/pixel_math/test_margin_bounding.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,25 @@ def test_get_margin_bounds_and_wcs():

assert polygon.area == pytest.approx(756822775.0000424, 0.001)

x, y = wcs_margin.world_to_pixel(test_sc)
coord_x, coord_y = wcs_margin.world_to_pixel(test_sc)
expected_pixel_coord = PixCoord(coord_x, coord_y)
expected_contains_checks = np.array([False, True, True])

pc = PixCoord(x, y)
checks = polygon.contains(pc)
contains_checks = polygon.contains(expected_pixel_coord)

expected = np.array([False, True, True])
npt.assert_array_equal(contains_checks, expected_contains_checks)

npt.assert_array_equal(checks, expected)

def get_checks_for_bounds(bounds, test_sky_coords):
"""Helper method for getting the check flags for
some boundaries and sky coordinates to check."""
vals = []
for polygon, margin in bounds:
coord_x, coord_y = margin.world_to_pixel(test_sky_coords)

vals.append(polygon.contains(PixCoord(coord_x, coord_y)))

return np.array(vals).any(axis=0)


def test_get_margin_bounds_and_wcs_low_order():
Expand All @@ -108,18 +119,9 @@ def test_get_margin_bounds_and_wcs_low_order():

assert len(bounds) == 16

vals = []
for p, w in bounds:
x, y = w.world_to_pixel(test_sc)

pc = PixCoord(x, y)
vals.append(p.contains(pc))

checks = np.array(vals).any(axis=0)

expected = np.array([True, False])

npt.assert_array_equal(checks, expected)
npt.assert_array_equal(get_checks_for_bounds(bounds, test_sc), expected)


def test_get_margin_bounds_and_wcs_north_pole():
Expand All @@ -134,18 +136,9 @@ def test_get_margin_bounds_and_wcs_north_pole():

test_sc = SkyCoord(test_ra, test_dec, unit="deg")

vals = []
for p, w in bounds:
x, y = w.world_to_pixel(test_sc)

pc = PixCoord(x, y)
vals.append(p.contains(pc))

checks = np.array(vals).any(axis=0)

expected = np.array([False, True, True, True, False])

npt.assert_array_equal(checks, expected)
npt.assert_array_equal(get_checks_for_bounds(bounds, test_sc), expected)


def test_get_margin_bounds_and_wcs_south_pole():
Expand All @@ -160,18 +153,9 @@ def test_get_margin_bounds_and_wcs_south_pole():

test_sc = SkyCoord(test_ra, test_dec, unit="deg")

vals = []
for p, w in bounds:
x, y = w.world_to_pixel(test_sc)

pc = PixCoord(x, y)
vals.append(p.contains(pc))

checks = np.array(vals).any(axis=0)

expected = np.array([False, True, True, True, False, True])

npt.assert_array_equal(checks, expected)
npt.assert_array_equal(get_checks_for_bounds(bounds, test_sc), expected)


def test_get_margin_bounds_and_wcs_ra_rollover():
Expand All @@ -186,18 +170,9 @@ def test_get_margin_bounds_and_wcs_ra_rollover():

test_sc = SkyCoord(test_ra, test_dec, unit="deg")

vals = []
for p, w in bounds:
x, y = w.world_to_pixel(test_sc)

pc = PixCoord(x, y)
vals.append(p.contains(pc))

checks = np.array(vals).any(axis=0)

expected = np.array([True, False, True, False, True])

npt.assert_array_equal(checks, expected)
npt.assert_array_equal(get_checks_for_bounds(bounds, test_sc), expected)


def test_get_margin_bounds_and_wcs_origin():
Expand All @@ -212,29 +187,20 @@ def test_get_margin_bounds_and_wcs_origin():

test_sc = SkyCoord(test_ra, test_dec, unit="deg")

vals = []
for p, w in bounds:
x, y = w.world_to_pixel(test_sc)

pc = PixCoord(x, y)
vals.append(p.contains(pc))

checks = np.array(vals).any(axis=0)

expected = np.array([False, True])

npt.assert_array_equal(checks, expected)
npt.assert_array_equal(get_checks_for_bounds(bounds, test_sc), expected)


def test_check_margin_bounds():
"""Make sure check_margin_bounds works properly"""
scale = pm.get_margin_scale(3, 0.1)
bounds = pm.get_margin_bounds_and_wcs(3, 4, scale)

ra = np.array([42.4704538, 56.25, 50.61225197])
dec = np.array([1.4593925, 14.49584495, 14.4767556])
test_ra = np.array([42.4704538, 56.25, 50.61225197])
test_dec = np.array([1.4593925, 14.49584495, 14.4767556])

checks = pm.check_margin_bounds(ra, dec, bounds)
checks = pm.check_margin_bounds(test_ra, test_dec, bounds)

expected = np.array([False, True, True])

Expand All @@ -246,10 +212,10 @@ def test_check_margin_bounds_multi_poly():
scale = pm.get_margin_scale(1, 0.1)
bounds = pm.get_margin_bounds_and_wcs(1, 4, scale)

ra = np.array([42.4704538, 120.0, 135.0])
dec = np.array([1.4593925, 25.0, 19.92530172])
test_ra = np.array([42.4704538, 120.0, 135.0])
test_dec = np.array([1.4593925, 25.0, 19.92530172])

checks = pm.check_margin_bounds(ra, dec, bounds)
checks = pm.check_margin_bounds(test_ra, test_dec, bounds)

expected = np.array([False, True, True])

Expand All @@ -262,10 +228,10 @@ def test_check_polar_margin_bounds_north():
pix = 1
margin_order = 2

ra = np.array([89, -179, -45])
dec = np.array([89.9, 89.9, 85.0])
test_ra = np.array([89, -179, -45])
test_dec = np.array([89.9, 89.9, 85.0])

vals = pm.check_polar_margin_bounds(ra, dec, order, pix, margin_order, 0.1)
vals = pm.check_polar_margin_bounds(test_ra, test_dec, order, pix, margin_order, 0.1)

expected = np.array([True, True, False])

Expand All @@ -278,10 +244,10 @@ def test_check_polar_margin_bounds_south():
pix = 9
margin_order = 2

ra = np.array([89, -179, -45])
dec = np.array([-89.9, -89.9, -85.0])
test_ra = np.array([89, -179, -45])
test_dec = np.array([-89.9, -89.9, -85.0])

vals = pm.check_polar_margin_bounds(ra, dec, order, pix, margin_order, 0.1)
vals = pm.check_polar_margin_bounds(test_ra, test_dec, order, pix, margin_order, 0.1)

expected = np.array([True, True, False])

Expand All @@ -294,10 +260,10 @@ def test_check_polar_margin_bounds_non_pole():
pix = 7
margin_order = 2

ra = np.array([89, -179, -45])
dec = np.array([-89.9, -89.9, -85.0])
test_ra = np.array([89, -179, -45])
test_dec = np.array([-89.9, -89.9, -85.0])

with pytest.raises(ValueError) as value_error:
vals = pm.check_polar_margin_bounds(ra, dec, order, pix, margin_order, 0.1)
pm.check_polar_margin_bounds(test_ra, test_dec, order, pix, margin_order, 0.1)

assert str(value_error.value) == "provided healpixel must be polar"

0 comments on commit 293a76d

Please sign in to comment.