From b33ed34bae6aabfef50d4f5498ec9147b7b9c2cd Mon Sep 17 00:00:00 2001 From: bparazin Date: Thu, 9 Dec 2021 16:37:32 -0600 Subject: [PATCH 01/18] First draft of to_moc method; haven't done rigorous testing on it yet, but it's simple enough code that I feel it's good enough to commit --- healpix_alchemy/types.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/healpix_alchemy/types.py b/healpix_alchemy/types.py index da80aed..a8b8959 100644 --- a/healpix_alchemy/types.py +++ b/healpix_alchemy/types.py @@ -3,7 +3,7 @@ from numbers import Integral from astropy.coordinates import SkyCoord -from astropy_healpix import uniq_to_level_ipix +from astropy_healpix import uniq_to_level_ipix, HEALPix from mocpy import MOC import numpy as np import sqlalchemy as sa @@ -28,6 +28,16 @@ def process_bind_param(self, value, dialect): value = int(value) return value + def to_moc(self, rangeSet, nside, index): + #make ranges into healpix lists + healpixSet = np.unique(np.concatenate([np.arange(start, stop, 1) for (start, stop) in rangeSet])) + if index.lower() == "ring": + hp = HEALPix(nside = nside, order = 'ring') + healpixSet = hp.ring_to_nested(healpixSet) + depth = np.ones(np.shape(healpixSet)) * np.log2(nside) + return MOC.from_healpix_cells(healpixSet, depth) + + class Tile(sa.TypeDecorator): From 28e56f2bb7969d74cd21c9f6c5b54e5927174a13 Mon Sep 17 00:00:00 2001 From: bparazin Date: Thu, 9 Dec 2021 16:38:43 -0600 Subject: [PATCH 02/18] formatting and commenting things --- healpix_alchemy/types.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/healpix_alchemy/types.py b/healpix_alchemy/types.py index a8b8959..03c528f 100644 --- a/healpix_alchemy/types.py +++ b/healpix_alchemy/types.py @@ -30,11 +30,18 @@ def process_bind_param(self, value, dialect): def to_moc(self, rangeSet, nside, index): #make ranges into healpix lists - healpixSet = np.unique(np.concatenate([np.arange(start, stop, 1) for (start, stop) in rangeSet])) + healpixSet = np.unique(np.concatenate([np.arange(start, stop, 1) for + (start, stop) in rangeSet])) + + #Convert to nested if need be if index.lower() == "ring": hp = HEALPix(nside = nside, order = 'ring') healpixSet = hp.ring_to_nested(healpixSet) + + #Create depth array depth = np.ones(np.shape(healpixSet)) * np.log2(nside) + + #Convert to an MOC return MOC.from_healpix_cells(healpixSet, depth) From e6ba7f529cce2100b58eecd1b301e9b1e217e191 Mon Sep 17 00:00:00 2001 From: bparazin Date: Thu, 9 Dec 2021 16:47:25 -0600 Subject: [PATCH 03/18] pep8 issues? --- healpix_alchemy/types.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/healpix_alchemy/types.py b/healpix_alchemy/types.py index 03c528f..8c353dd 100644 --- a/healpix_alchemy/types.py +++ b/healpix_alchemy/types.py @@ -29,19 +29,12 @@ def process_bind_param(self, value, dialect): return value def to_moc(self, rangeSet, nside, index): - #make ranges into healpix lists healpixSet = np.unique(np.concatenate([np.arange(start, stop, 1) for (start, stop) in rangeSet])) - - #Convert to nested if need be if index.lower() == "ring": - hp = HEALPix(nside = nside, order = 'ring') + hp = HEALPix(nside=nside, order='ring') healpixSet = hp.ring_to_nested(healpixSet) - - #Create depth array depth = np.ones(np.shape(healpixSet)) * np.log2(nside) - - #Convert to an MOC return MOC.from_healpix_cells(healpixSet, depth) From f1b22d793860fdeeb4588934871c3010324fd45e Mon Sep 17 00:00:00 2001 From: bparazin Date: Thu, 9 Dec 2021 16:48:25 -0600 Subject: [PATCH 04/18] pep8 2: fixes boogaloo --- healpix_alchemy/types.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/healpix_alchemy/types.py b/healpix_alchemy/types.py index 8c353dd..fd3e0e5 100644 --- a/healpix_alchemy/types.py +++ b/healpix_alchemy/types.py @@ -37,8 +37,6 @@ def to_moc(self, rangeSet, nside, index): depth = np.ones(np.shape(healpixSet)) * np.log2(nside) return MOC.from_healpix_cells(healpixSet, depth) - - class Tile(sa.TypeDecorator): cache_ok = True From 7fd9d4dc2a77433077adaedd45579185884336c9 Mon Sep 17 00:00:00 2001 From: bparazin Date: Thu, 9 Dec 2021 16:49:23 -0600 Subject: [PATCH 05/18] ok this time I have the right number of blank lines for real --- healpix_alchemy/types.py | 1 + 1 file changed, 1 insertion(+) diff --git a/healpix_alchemy/types.py b/healpix_alchemy/types.py index fd3e0e5..ea16b65 100644 --- a/healpix_alchemy/types.py +++ b/healpix_alchemy/types.py @@ -37,6 +37,7 @@ def to_moc(self, rangeSet, nside, index): depth = np.ones(np.shape(healpixSet)) * np.log2(nside) return MOC.from_healpix_cells(healpixSet, depth) + class Tile(sa.TypeDecorator): cache_ok = True From eb595575519b7e42fa972d4792f866ba66332b18 Mon Sep 17 00:00:00 2001 From: bparazin Date: Mon, 13 Dec 2021 15:47:01 -0600 Subject: [PATCH 06/18] Made a test file for to_moc. Probably first commit of several as I fix pep8 issues --- healpix_alchemy/tests/test_types.py | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 healpix_alchemy/tests/test_types.py diff --git a/healpix_alchemy/tests/test_types.py b/healpix_alchemy/tests/test_types.py new file mode 100644 index 0000000..0631c8c --- /dev/null +++ b/healpix_alchemy/tests/test_types.py @@ -0,0 +1,58 @@ +import numpy as np +from mocpy import MOC +import itertools +from astropy_healpix import HEALPix +import pytest + +from .. import types + + +def random_MOC(max_l): + json = {str(l): list( + np.unique( + np.random.randint(0, 12 * (4 ** l), + np.random.randint(12 * (4 ** l))))) + for l in range(max_l)} + return MOC.from_json(json), json + +#return integer intervals as [start, stop) +def to_ranges(iterable): + iterable = sorted(set(iterable)) + for key, group in itertools.groupby(enumerate(iterable), + lambda t: t[1] - t[0]): + group = list(group) + yield group[0][1], group[-1][1]+1 + + +def test_to_moc_ring(): + l_lim = 12 + moc, json = random_MOC(l_lim) + for level in range(l_lim-1): + for pixel in json[str(level)]: + for i in range(4): + json[str(level + 1)].append(pixel * 4 + i) + json[str(level + 1)] = np.unique(json[str(level + 1)]) + nested_hpx_list = json[str(l_lim-1)] + hp = HEALPix(nside=2**(l_lim-1), order='ring') + ring_hpx_list = hp.nested_to_ring(nested_hpx_list) + ring_hpx_ranges = to_ranges(ring_hpx_list) + point = types.Point() + assert point.to_moc(rangeSet = ring_hpx_ranges, + nside = 2**(l_lim-1), + index = 'ring') == moc + + +def test_to_moc_nested(): + l_lim = 12 + moc, json = random_MOC(l_lim) + for level in range(l_lim-1): + for pixel in json[str(level)]: + for i in range(4): + json[str(level + 1)].append(pixel * 4 + i) + json[str(level + 1)] = np.unique(json[str(level + 1)]) + nested_hpx_list = json[str(l_lim-1)] + nested_hpx_ranges = to_ranges(nested_hpx_list) + point = types.Point() + assert point.to_moc(rangeSet = nested_hpx_ranges, + nside = 2**(l_lim-1), + index = 'nested') == moc From 0b569ba352e3f64796e567d0d2145ae3740d418f Mon Sep 17 00:00:00 2001 From: bparazin Date: Mon, 13 Dec 2021 15:53:40 -0600 Subject: [PATCH 07/18] Did I get all the pep8 stuff? --- healpix_alchemy/tests/test_types.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/healpix_alchemy/tests/test_types.py b/healpix_alchemy/tests/test_types.py index 0631c8c..425f901 100644 --- a/healpix_alchemy/tests/test_types.py +++ b/healpix_alchemy/tests/test_types.py @@ -2,20 +2,19 @@ from mocpy import MOC import itertools from astropy_healpix import HEALPix -import pytest from .. import types def random_MOC(max_l): - json = {str(l): list( + json = {str(level): list( np.unique( - np.random.randint(0, 12 * (4 ** l), - np.random.randint(12 * (4 ** l))))) - for l in range(max_l)} + np.random.randint(0, 12 * (4 ** level), + np.random.randint(12 * (4 ** level))))) + for level in range(max_l)} return MOC.from_json(json), json -#return integer intervals as [start, stop) + def to_ranges(iterable): iterable = sorted(set(iterable)) for key, group in itertools.groupby(enumerate(iterable), @@ -37,9 +36,9 @@ def test_to_moc_ring(): ring_hpx_list = hp.nested_to_ring(nested_hpx_list) ring_hpx_ranges = to_ranges(ring_hpx_list) point = types.Point() - assert point.to_moc(rangeSet = ring_hpx_ranges, - nside = 2**(l_lim-1), - index = 'ring') == moc + assert point.to_moc(rangeSet=ring_hpx_ranges, + nside=2**(l_lim-1), + index='ring') == moc def test_to_moc_nested(): @@ -53,6 +52,6 @@ def test_to_moc_nested(): nested_hpx_list = json[str(l_lim-1)] nested_hpx_ranges = to_ranges(nested_hpx_list) point = types.Point() - assert point.to_moc(rangeSet = nested_hpx_ranges, - nside = 2**(l_lim-1), - index = 'nested') == moc + assert point.to_moc(rangeSet=nested_hpx_ranges, + nside=2**(l_lim-1), + index='nested') == moc From e106f065e42d8f9a31c06a99a80c41c84d0ed89d Mon Sep 17 00:00:00 2001 From: bparazin Date: Mon, 13 Dec 2021 15:55:56 -0600 Subject: [PATCH 08/18] ok pep8 round 2 --- healpix_alchemy/tests/test_types.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/healpix_alchemy/tests/test_types.py b/healpix_alchemy/tests/test_types.py index 425f901..0f9ba2c 100644 --- a/healpix_alchemy/tests/test_types.py +++ b/healpix_alchemy/tests/test_types.py @@ -11,7 +11,7 @@ def random_MOC(max_l): np.unique( np.random.randint(0, 12 * (4 ** level), np.random.randint(12 * (4 ** level))))) - for level in range(max_l)} + for level in range(max_l)} return MOC.from_json(json), json @@ -37,8 +37,8 @@ def test_to_moc_ring(): ring_hpx_ranges = to_ranges(ring_hpx_list) point = types.Point() assert point.to_moc(rangeSet=ring_hpx_ranges, - nside=2**(l_lim-1), - index='ring') == moc + nside=2**(l_lim-1), + index='ring') == moc def test_to_moc_nested(): @@ -53,5 +53,5 @@ def test_to_moc_nested(): nested_hpx_ranges = to_ranges(nested_hpx_list) point = types.Point() assert point.to_moc(rangeSet=nested_hpx_ranges, - nside=2**(l_lim-1), - index='nested') == moc + nside=2**(l_lim-1), + index='nested') == moc From 943279b8cebb5ebe291cb4406b4987b89e1c3286 Mon Sep 17 00:00:00 2001 From: B Parazin <56836732+bparazin@users.noreply.github.com> Date: Mon, 20 Dec 2021 10:57:41 -0600 Subject: [PATCH 09/18] Update healpix_alchemy/types.py Co-authored-by: Leo Singer --- healpix_alchemy/types.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/healpix_alchemy/types.py b/healpix_alchemy/types.py index ea16b65..5e3e7be 100644 --- a/healpix_alchemy/types.py +++ b/healpix_alchemy/types.py @@ -28,7 +28,8 @@ def process_bind_param(self, value, dialect): value = int(value) return value - def to_moc(self, rangeSet, nside, index): + @classmethod + def to_moc(cls, rangeSet, nside, index): healpixSet = np.unique(np.concatenate([np.arange(start, stop, 1) for (start, stop) in rangeSet])) if index.lower() == "ring": From 26071d1de7080be0d1a38ab99caf9cc989c79630 Mon Sep 17 00:00:00 2001 From: bparazin Date: Mon, 20 Dec 2021 15:02:17 -0600 Subject: [PATCH 10/18] Work on improving to_moc memory and test --- healpix_alchemy/tests/test_types.py | 54 ++++++++--------------------- healpix_alchemy/types.py | 15 ++++---- 2 files changed, 20 insertions(+), 49 deletions(-) diff --git a/healpix_alchemy/tests/test_types.py b/healpix_alchemy/tests/test_types.py index 0f9ba2c..c18cf82 100644 --- a/healpix_alchemy/tests/test_types.py +++ b/healpix_alchemy/tests/test_types.py @@ -2,19 +2,12 @@ from mocpy import MOC import itertools from astropy_healpix import HEALPix +from astropy.coordinates import Angle, Longitude, Latitude +import astropy.units as u from .. import types -def random_MOC(max_l): - json = {str(level): list( - np.unique( - np.random.randint(0, 12 * (4 ** level), - np.random.randint(12 * (4 ** level))))) - for level in range(max_l)} - return MOC.from_json(json), json - - def to_ranges(iterable): iterable = sorted(set(iterable)) for key, group in itertools.groupby(enumerate(iterable), @@ -23,35 +16,16 @@ def to_ranges(iterable): yield group[0][1], group[-1][1]+1 -def test_to_moc_ring(): - l_lim = 12 - moc, json = random_MOC(l_lim) - for level in range(l_lim-1): - for pixel in json[str(level)]: - for i in range(4): - json[str(level + 1)].append(pixel * 4 + i) - json[str(level + 1)] = np.unique(json[str(level + 1)]) - nested_hpx_list = json[str(l_lim-1)] - hp = HEALPix(nside=2**(l_lim-1), order='ring') - ring_hpx_list = hp.nested_to_ring(nested_hpx_list) - ring_hpx_ranges = to_ranges(ring_hpx_list) - point = types.Point() - assert point.to_moc(rangeSet=ring_hpx_ranges, - nside=2**(l_lim-1), - index='ring') == moc - +def test_to_moc(): + depth = 5 + rng = np.random.default_rng() + lon = Longitude(360 * rng.random() * u.deg) + lat = Latitude((180 * rng.random() - 90) * u.deg) + radius = Angle(rng.normal(loc=10, scale=2.5) * u.deg) + moc = MOC.from_cone(lon, lat, radius, depth) + hp = HEALPix(nside=2**depth, order='nested') + healpix_list = hp.cone_search_lonlat(lon, lat, radius) + nested_hpx_ranges = [item for item in to_ranges(healpix_list)] -def test_to_moc_nested(): - l_lim = 12 - moc, json = random_MOC(l_lim) - for level in range(l_lim-1): - for pixel in json[str(level)]: - for i in range(4): - json[str(level + 1)].append(pixel * 4 + i) - json[str(level + 1)] = np.unique(json[str(level + 1)]) - nested_hpx_list = json[str(l_lim-1)] - nested_hpx_ranges = to_ranges(nested_hpx_list) - point = types.Point() - assert point.to_moc(rangeSet=nested_hpx_ranges, - nside=2**(l_lim-1), - index='nested') == moc + assert types.Point.to_moc(rangeSet=nested_hpx_ranges, + nside=2**depth) == moc diff --git a/healpix_alchemy/types.py b/healpix_alchemy/types.py index 5e3e7be..d546ad3 100644 --- a/healpix_alchemy/types.py +++ b/healpix_alchemy/types.py @@ -3,7 +3,7 @@ from numbers import Integral from astropy.coordinates import SkyCoord -from astropy_healpix import uniq_to_level_ipix, HEALPix +from astropy_healpix import uniq_to_level_ipix from mocpy import MOC import numpy as np import sqlalchemy as sa @@ -29,14 +29,11 @@ def process_bind_param(self, value, dialect): return value @classmethod - def to_moc(cls, rangeSet, nside, index): - healpixSet = np.unique(np.concatenate([np.arange(start, stop, 1) for - (start, stop) in rangeSet])) - if index.lower() == "ring": - hp = HEALPix(nside=nside, order='ring') - healpixSet = hp.ring_to_nested(healpixSet) - depth = np.ones(np.shape(healpixSet)) * np.log2(nside) - return MOC.from_healpix_cells(healpixSet, depth) + def to_moc(cls, rangeSet, nside): + depth = int(np.log2(nside)) + MOCstr = f'{depth}/' + ' '.join(map(lambda x: f'{x[0]}-{x[1]-1}', + rangeSet)) + return MOC.from_str(MOCstr) class Tile(sa.TypeDecorator): From 2e87489a19d7c7d608b713f0f029cc54f4735bc6 Mon Sep 17 00:00:00 2001 From: bparazin Date: Mon, 20 Dec 2021 15:26:56 -0600 Subject: [PATCH 11/18] Made test use a cone search instead of randomly generating MOC --- healpix_alchemy/tests/test_types.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/healpix_alchemy/tests/test_types.py b/healpix_alchemy/tests/test_types.py index c18cf82..d71b899 100644 --- a/healpix_alchemy/tests/test_types.py +++ b/healpix_alchemy/tests/test_types.py @@ -17,15 +17,16 @@ def to_ranges(iterable): def test_to_moc(): - depth = 5 + depth = 10 rng = np.random.default_rng() lon = Longitude(360 * rng.random() * u.deg) lat = Latitude((180 * rng.random() - 90) * u.deg) radius = Angle(rng.normal(loc=10, scale=2.5) * u.deg) - moc = MOC.from_cone(lon, lat, radius, depth) hp = HEALPix(nside=2**depth, order='nested') healpix_list = hp.cone_search_lonlat(lon, lat, radius) + depth_list = depth * np.ones(np.shape(healpix_list)) + moc = MOC.from_healpix_cells(healpix_list, depth_list) nested_hpx_ranges = [item for item in to_ranges(healpix_list)] assert types.Point.to_moc(rangeSet=nested_hpx_ranges, - nside=2**depth) == moc + nside=2**depth) == moc From d42a00a797d83ea448b35b02879155c6f90d742c Mon Sep 17 00:00:00 2001 From: bparazin Date: Mon, 20 Dec 2021 15:28:53 -0600 Subject: [PATCH 12/18] pep8 --- healpix_alchemy/tests/test_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/healpix_alchemy/tests/test_types.py b/healpix_alchemy/tests/test_types.py index d71b899..21cfd92 100644 --- a/healpix_alchemy/tests/test_types.py +++ b/healpix_alchemy/tests/test_types.py @@ -29,4 +29,4 @@ def test_to_moc(): nested_hpx_ranges = [item for item in to_ranges(healpix_list)] assert types.Point.to_moc(rangeSet=nested_hpx_ranges, - nside=2**depth) == moc + nside=2**depth) == moc From 66be1162f1cd90f71f2d2490a37da4c28ae32e22 Mon Sep 17 00:00:00 2001 From: bparazin Date: Mon, 20 Dec 2021 15:39:06 -0600 Subject: [PATCH 13/18] Update deprecated method --- healpix_alchemy/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/healpix_alchemy/types.py b/healpix_alchemy/types.py index d546ad3..c3cd12e 100644 --- a/healpix_alchemy/types.py +++ b/healpix_alchemy/types.py @@ -33,7 +33,7 @@ def to_moc(cls, rangeSet, nside): depth = int(np.log2(nside)) MOCstr = f'{depth}/' + ' '.join(map(lambda x: f'{x[0]}-{x[1]-1}', rangeSet)) - return MOC.from_str(MOCstr) + return MOC.from_string(MOCstr) class Tile(sa.TypeDecorator): From 28ec519a047e61c09cfc6b85a465a2a4cc88555e Mon Sep 17 00:00:00 2001 From: bparazin Date: Mon, 27 Dec 2021 16:32:55 -0500 Subject: [PATCH 14/18] Lotta work to fix up the to_moc test, not ready yet, but soon (this is just saving work) --- healpix_alchemy/tests/benchmarks/conftest.py | 4 +++ healpix_alchemy/tests/benchmarks/data.py | 24 +++++++++++--- healpix_alchemy/tests/benchmarks/models.py | 4 +++ .../tests/benchmarks/test_benchmarks.py | 17 +++++++++- healpix_alchemy/tests/test_types.py | 32 ------------------- healpix_alchemy/types.py | 15 +++++---- 6 files changed, 52 insertions(+), 44 deletions(-) delete mode 100644 healpix_alchemy/tests/test_types.py diff --git a/healpix_alchemy/tests/benchmarks/conftest.py b/healpix_alchemy/tests/benchmarks/conftest.py index f8ceca3..f0bd4c9 100644 --- a/healpix_alchemy/tests/benchmarks/conftest.py +++ b/healpix_alchemy/tests/benchmarks/conftest.py @@ -40,3 +40,7 @@ def random_fields(cursor, tables, request): @pytest.fixture def random_sky_map(cursor, tables): return data.get_random_sky_map(20_000, cursor) + +@pytest.fixture +def random_moc_from_cone(cursor, tables): + return data.get_random_moc_from_cone(15, cursor) diff --git a/healpix_alchemy/tests/benchmarks/data.py b/healpix_alchemy/tests/benchmarks/data.py index 9f2d879..706ab71 100644 --- a/healpix_alchemy/tests/benchmarks/data.py +++ b/healpix_alchemy/tests/benchmarks/data.py @@ -7,7 +7,8 @@ """ import io -from astropy.coordinates import SkyCoord, uniform_spherical_random_surface +from astropy.coordinates import (SkyCoord, uniform_spherical_random_surface, + Angle, Longitude, Latitude) from astropy import units as u from mocpy import MOC import numpy as np @@ -15,13 +16,14 @@ from ...constants import HPX, LEVEL, PIXEL_AREA from ...types import Tile -from .models import Galaxy, Field, FieldTile, Skymap, SkymapTile +from .models import Galaxy, Field, FieldTile, Skymap, SkymapTile, TileList ( RANDOM_GALAXIES_SEED, RANDOM_FIELDS_SEED, - RANDOM_SKY_MAP_SEED -) = np.random.SeedSequence(12345).spawn(3) + RANDOM_SKY_MAP_SEED, + RANDOM_MOC_SEED +) = np.random.SeedSequence(12345).spawn(4) def get_ztf_footprint_corners(): @@ -122,3 +124,17 @@ def get_random_sky_map(n, cursor): cursor.copy_from(f, SkymapTile.__tablename__) return tiles, probdensity + + +def get_random_moc_from_cone(depth, cursor): + rng = np.random.default_rng(RANDOM_MOC_SEED) + # Randomly choose lon, lat & radius for cone search + lon = Longitude(360 * rng.random() * u.deg) + lat = Latitude((180 * rng.random() - 90) * u.deg) + radius = Angle(rng.normal(loc=10, scale=2.5) * u.deg) + moc = MOC.from_cone(lon, lat, radius, depth) + + f = io.StringIO('\n'.join(Tile.tiles_from_moc(moc))) + cursor.copy_from(f, TileList.__tablename__) + + return moc, depth diff --git a/healpix_alchemy/tests/benchmarks/models.py b/healpix_alchemy/tests/benchmarks/models.py index 65ad174..13d5c10 100644 --- a/healpix_alchemy/tests/benchmarks/models.py +++ b/healpix_alchemy/tests/benchmarks/models.py @@ -35,3 +35,7 @@ class SkymapTile(Base): id = sa.Column(sa.ForeignKey(Skymap.id), primary_key=True) hpx = sa.Column(Tile, primary_key=True, index=True) probdensity = sa.Column(sa.Float, nullable=False) + + +class TileList(Base): + hpx = sa.Column(Tile, primary_key=True, index=True) diff --git a/healpix_alchemy/tests/benchmarks/test_benchmarks.py b/healpix_alchemy/tests/benchmarks/test_benchmarks.py index cf18551..8e57ebc 100644 --- a/healpix_alchemy/tests/benchmarks/test_benchmarks.py +++ b/healpix_alchemy/tests/benchmarks/test_benchmarks.py @@ -5,7 +5,8 @@ import pytest from ... import func -from .models import Galaxy, FieldTile, SkymapTile +from .models import Galaxy, FieldTile, SkymapTile, TileList +from ...types import Tile @pytest.fixture @@ -104,3 +105,17 @@ def test_fields_in_90pct_credible_region(bench, random_fields, random_sky_map): # Run benchmark bench(query) + + +def test_to_moc(bench, random_moc_from_cone): + # Assemble Query + query = sa.select(TileList.hpx) + rangeset = bench(query) + + # Expected result + expected, depth = random_moc_from_cone + + assert Tile.moc_from_tiles(rangeset, depth) == expected + + #assert types.Point.to_moc(rangeSet=nested_hpx_ranges, + # nside=2**depth) == moc \ No newline at end of file diff --git a/healpix_alchemy/tests/test_types.py b/healpix_alchemy/tests/test_types.py deleted file mode 100644 index 21cfd92..0000000 --- a/healpix_alchemy/tests/test_types.py +++ /dev/null @@ -1,32 +0,0 @@ -import numpy as np -from mocpy import MOC -import itertools -from astropy_healpix import HEALPix -from astropy.coordinates import Angle, Longitude, Latitude -import astropy.units as u - -from .. import types - - -def to_ranges(iterable): - iterable = sorted(set(iterable)) - for key, group in itertools.groupby(enumerate(iterable), - lambda t: t[1] - t[0]): - group = list(group) - yield group[0][1], group[-1][1]+1 - - -def test_to_moc(): - depth = 10 - rng = np.random.default_rng() - lon = Longitude(360 * rng.random() * u.deg) - lat = Latitude((180 * rng.random() - 90) * u.deg) - radius = Angle(rng.normal(loc=10, scale=2.5) * u.deg) - hp = HEALPix(nside=2**depth, order='nested') - healpix_list = hp.cone_search_lonlat(lon, lat, radius) - depth_list = depth * np.ones(np.shape(healpix_list)) - moc = MOC.from_healpix_cells(healpix_list, depth_list) - nested_hpx_ranges = [item for item in to_ranges(healpix_list)] - - assert types.Point.to_moc(rangeSet=nested_hpx_ranges, - nside=2**depth) == moc diff --git a/healpix_alchemy/types.py b/healpix_alchemy/types.py index c3cd12e..99d170c 100644 --- a/healpix_alchemy/types.py +++ b/healpix_alchemy/types.py @@ -28,13 +28,6 @@ def process_bind_param(self, value, dialect): value = int(value) return value - @classmethod - def to_moc(cls, rangeSet, nside): - depth = int(np.log2(nside)) - MOCstr = f'{depth}/' + ' '.join(map(lambda x: f'{x[0]}-{x[1]-1}', - rangeSet)) - return MOC.from_string(MOCstr) - class Tile(sa.TypeDecorator): @@ -87,6 +80,14 @@ def tiles_from_polygon_skycoord(cls, polygon): def tiles_from_moc(cls, moc): return (f'[{lo},{hi})' for lo, hi in moc._interval_set.nested) + @classmethod + def moc_from_tiles(cls, rangeSet, nside): + depth = int(np.log2(nside)) + print(rangeSet[0]) + MOCstr = f'{depth}/' + ' '.join(map(lambda x: f'{x[0]}-{x[1]-1}', + rangeSet)) + return MOC.from_string(MOCstr) + @sa.event.listens_for(sa.Index, 'after_parent_attach') def _create_indices(index, parent): From ad8b7283e477bc02472ab3edd0dbfe3dc51e6c98 Mon Sep 17 00:00:00 2001 From: bparazin Date: Mon, 27 Dec 2021 17:11:04 -0500 Subject: [PATCH 15/18] Updated test to put MOC into database then pull it out, update it and recounstruct it --- healpix_alchemy/tests/benchmarks/data.py | 2 +- healpix_alchemy/tests/benchmarks/test_benchmarks.py | 10 +++++++--- healpix_alchemy/types.py | 1 - 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/healpix_alchemy/tests/benchmarks/data.py b/healpix_alchemy/tests/benchmarks/data.py index 706ab71..19c7c61 100644 --- a/healpix_alchemy/tests/benchmarks/data.py +++ b/healpix_alchemy/tests/benchmarks/data.py @@ -137,4 +137,4 @@ def get_random_moc_from_cone(depth, cursor): f = io.StringIO('\n'.join(Tile.tiles_from_moc(moc))) cursor.copy_from(f, TileList.__tablename__) - return moc, depth + return moc diff --git a/healpix_alchemy/tests/benchmarks/test_benchmarks.py b/healpix_alchemy/tests/benchmarks/test_benchmarks.py index 8e57ebc..768d898 100644 --- a/healpix_alchemy/tests/benchmarks/test_benchmarks.py +++ b/healpix_alchemy/tests/benchmarks/test_benchmarks.py @@ -3,6 +3,7 @@ import numpy as np import sqlalchemy as sa import pytest +import re from ... import func from .models import Galaxy, FieldTile, SkymapTile, TileList @@ -110,12 +111,15 @@ def test_fields_in_90pct_credible_region(bench, random_fields, random_sky_map): def test_to_moc(bench, random_moc_from_cone): # Assemble Query query = sa.select(TileList.hpx) - rangeset = bench(query) + rangeset = map(lambda x: + (int(re.split("[,]|[(]", str(x))[2]), + int(re.split("[,]|[(]", str(x))[3])), + bench(query)) # Expected result - expected, depth = random_moc_from_cone + expected = random_moc_from_cone - assert Tile.moc_from_tiles(rangeset, depth) == expected + assert Tile.moc_from_tiles(rangeset, 2**29) == expected #assert types.Point.to_moc(rangeSet=nested_hpx_ranges, # nside=2**depth) == moc \ No newline at end of file diff --git a/healpix_alchemy/types.py b/healpix_alchemy/types.py index 99d170c..238f19f 100644 --- a/healpix_alchemy/types.py +++ b/healpix_alchemy/types.py @@ -83,7 +83,6 @@ def tiles_from_moc(cls, moc): @classmethod def moc_from_tiles(cls, rangeSet, nside): depth = int(np.log2(nside)) - print(rangeSet[0]) MOCstr = f'{depth}/' + ' '.join(map(lambda x: f'{x[0]}-{x[1]-1}', rangeSet)) return MOC.from_string(MOCstr) From d401702285a735529910835f2e455842ced3f4e0 Mon Sep 17 00:00:00 2001 From: bparazin Date: Mon, 27 Dec 2021 17:12:26 -0500 Subject: [PATCH 16/18] pep8 --- healpix_alchemy/tests/benchmarks/test_benchmarks.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/healpix_alchemy/tests/benchmarks/test_benchmarks.py b/healpix_alchemy/tests/benchmarks/test_benchmarks.py index 768d898..f614a85 100644 --- a/healpix_alchemy/tests/benchmarks/test_benchmarks.py +++ b/healpix_alchemy/tests/benchmarks/test_benchmarks.py @@ -120,6 +120,3 @@ def test_to_moc(bench, random_moc_from_cone): expected = random_moc_from_cone assert Tile.moc_from_tiles(rangeset, 2**29) == expected - - #assert types.Point.to_moc(rangeSet=nested_hpx_ranges, - # nside=2**depth) == moc \ No newline at end of file From 8b34dfdecce80afb295253ad35900fd6c3e6947d Mon Sep 17 00:00:00 2001 From: bparazin Date: Mon, 27 Dec 2021 17:13:03 -0500 Subject: [PATCH 17/18] pep8 2 --- healpix_alchemy/tests/benchmarks/conftest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/healpix_alchemy/tests/benchmarks/conftest.py b/healpix_alchemy/tests/benchmarks/conftest.py index f0bd4c9..d9a17d2 100644 --- a/healpix_alchemy/tests/benchmarks/conftest.py +++ b/healpix_alchemy/tests/benchmarks/conftest.py @@ -41,6 +41,7 @@ def random_fields(cursor, tables, request): def random_sky_map(cursor, tables): return data.get_random_sky_map(20_000, cursor) + @pytest.fixture def random_moc_from_cone(cursor, tables): return data.get_random_moc_from_cone(15, cursor) From cb61e1b2fe8b7d68bd55b0e0d1dd51a6ba91deea Mon Sep 17 00:00:00 2001 From: bparazin Date: Mon, 27 Dec 2021 17:21:17 -0500 Subject: [PATCH 18/18] switch order of test to make codecov happy? --- healpix_alchemy/tests/benchmarks/test_benchmarks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/healpix_alchemy/tests/benchmarks/test_benchmarks.py b/healpix_alchemy/tests/benchmarks/test_benchmarks.py index f614a85..f8fa8c5 100644 --- a/healpix_alchemy/tests/benchmarks/test_benchmarks.py +++ b/healpix_alchemy/tests/benchmarks/test_benchmarks.py @@ -119,4 +119,4 @@ def test_to_moc(bench, random_moc_from_cone): # Expected result expected = random_moc_from_cone - assert Tile.moc_from_tiles(rangeset, 2**29) == expected + assert expected == Tile.moc_from_tiles(rangeset, 2**29)