11import datetime
22import unittest
33
4- import healpix
54import numpy as np
65
76import cf
87
9- # Create matching lists of selected nested, ring, nuniq and zuniq
10- # indices for every refinement level.
11- indices = [
12- (r , i , healpix .nest2ring (healpix .order2nside (r ), i ))
13- for r in range (30 )
14- for i in (0 , 7 , (12 * 4 ** r ) - 1 )
15- ]
16- refinement_levels , nested_indices , ring_indices = map (list , zip (* indices ))
17-
18- nuniq_indices = [
19- i + 4 ** (1 + r ) for r , i in zip (refinement_levels , nested_indices )
20- ]
21-
22- zuniq_indices = [
23- (2 * i + 1 ) * 4 ** (29 - r )
24- for r , i in zip (refinement_levels , nested_indices )
25- ]
8+ healpix_imported = True
9+ try :
10+ import healpix # noqa: F401
11+ except ImportError :
12+ healpix_imported = False
2613
2714
2815class DataTest (unittest .TestCase ):
2916 """Unit tests for HEALPix utilities."""
3017
18+ def setUp (self ):
19+ """Preparations called immediately before each test method."""
20+ # Skip all if healpix module not available!
21+ if not healpix_imported :
22+ self .skipTest (
23+ "Test module requires 'healpix' package. Install it to run all."
24+ )
25+
26+ # Create matching lists of selected nested, ring, nuniq and zuniq
27+ # indices for every refinement level.
28+ indices = [
29+ (r , i , healpix .nest2ring (healpix .order2nside (r ), i ))
30+ for r in range (30 )
31+ for i in (0 , 7 , (12 * 4 ** r ) - 1 )
32+ ]
33+ self .refinement_levels , self .nested_indices , self .ring_indices = map (
34+ list , zip (* indices )
35+ )
36+
37+ self .nuniq_indices = [ # noqa: F841
38+ i + 4 ** (1 + r )
39+ for r , i in zip (self .refinement_levels , self .nested_indices )
40+ ]
41+
42+ self .zuniq_indices = [ # noqa: F841
43+ (2 * i + 1 ) * 4 ** (29 - r )
44+ for r , i in zip (self .refinement_levels , self .nested_indices )
45+ ]
46+
3147 def test_HEALPix_uniq2zuniq (self ):
3248 """Test _uniq2zuniq"""
3349 from cf .data .dask_utils_healpix import _uniq2zuniq
3450
3551 self .assertTrue (
36- np .array_equal (_uniq2zuniq (nuniq_indices ), zuniq_indices )
52+ np .array_equal (_uniq2zuniq (self . nuniq_indices ), self . zuniq_indices )
3753 )
3854
3955 def test_HEALPix_zuniq2uniq (self ):
4056 """Test _zuniq2uniq"""
4157 from cf .data .dask_utils_healpix import _zuniq2uniq
4258
4359 self .assertTrue (
44- np .array_equal (_zuniq2uniq (zuniq_indices ), nuniq_indices )
60+ np .array_equal (_zuniq2uniq (self . zuniq_indices ), self . nuniq_indices )
4561 )
4662
4763 def test_HEALPix_zuniq2pix (self ):
4864 """Test _zuniq2pix"""
4965 from cf .data .dask_utils_healpix import _zuniq2pix
5066
5167 # nested
52- order , i = _zuniq2pix (zuniq_indices , nest = True )
68+ order , i = _zuniq2pix (self . zuniq_indices , nest = True )
5369
54- self .assertTrue (np .array_equal (order , refinement_levels ))
55- self .assertTrue (np .array_equal (i , nested_indices ))
70+ self .assertTrue (np .array_equal (order , self . refinement_levels ))
71+ self .assertTrue (np .array_equal (i , self . nested_indices ))
5672
5773 # ring
5874 with self .assertRaises (NotImplementedError ):
59- _zuniq2pix (zuniq_indices , nest = False )
75+ _zuniq2pix (self . zuniq_indices , nest = False )
6076
6177 def test_HEALPix_pix2zuniq (self ):
6278 """Test _pix2zuniq"""
@@ -65,18 +81,18 @@ def test_HEALPix_pix2zuniq(self):
6581 # nested
6682 z = [
6783 _pix2zuniq (r , i , nest = True )
68- for r , i in zip (refinement_levels , nested_indices )
84+ for r , i in zip (self . refinement_levels , self . nested_indices )
6985 ]
7086
71- self .assertTrue (np .array_equal (z , zuniq_indices ))
87+ self .assertTrue (np .array_equal (z , self . zuniq_indices ))
7288
7389 # ring
7490 z = [
7591 _pix2zuniq (r , i , nest = False )
76- for r , i in zip (refinement_levels , ring_indices )
92+ for r , i in zip (self . refinement_levels , self . ring_indices )
7793 ]
7894
79- self .assertTrue (np .array_equal (z , zuniq_indices ))
95+ self .assertTrue (np .array_equal (z , self . zuniq_indices ))
8096
8197
8298if __name__ == "__main__" :
0 commit comments