Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ per-file-ignores =
# https://stackoverflow.com/questions/59167405/)
__init__.py: F401
*/__init__.py: F401
# Whole-module test skips (via setUp method) for these cases means exit
# before reach undefined vars where need given module for defining those.
# This is the cleanest way but sets off F821 code en-masse so ignore that.
cf/test/test_HEALPix_utils.py: F821
4 changes: 4 additions & 0 deletions cf/test/test_Domain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import re
import unittest
from importlib.util import find_spec

import numpy as np

Expand Down Expand Up @@ -494,6 +495,9 @@ def test_Domain_cyclic_iscyclic(self):
d2.cyclic("X", iscyclic=False)
self.assertTrue(d2.iscyclic("X"))

# Note: here only need healpix for cf under-the-hood code, not in test
# directly, so no need to actually import healpix, just test it is there.
@unittest.skipUnless(find_spec("healpix"), "Requires 'healpix' package.")
def test_Domain_create_healpix(self):
"""Test Domain.create_healpix."""
d = cf.Domain.create_healpix(0)
Expand Down
12 changes: 12 additions & 0 deletions cf/test/test_Field.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

import cf

healpix_available = False
# Note: here only need healpix for cf under-the-hood code, not in test
# directly, so no need to actually import healpix, just test it is there.
if find_spec("healpix"):
healpix_available = True

n_tmpfiles = 1
tmpfiles = [
tempfile.mkstemp("_test_Field.nc", dir=os.getcwd())[1]
Expand Down Expand Up @@ -3137,6 +3143,7 @@ def test_Field_to_units(self):
with self.assertRaises(ValueError):
g.to_units("degC")

@unittest.skipUnless(healpix_available, "Requires 'healpix' package.")
def test_Field_healpix_change_indexing_scheme(self):
"""Test Field.healpix_change_indexing_scheme."""
# HEALPix field
Expand Down Expand Up @@ -3236,6 +3243,7 @@ def test_Field_healpix_change_indexing_scheme(self):
with self.assertRaises(ValueError):
self.f0.healpix_change_indexing_scheme("ring")

@unittest.skipUnless(healpix_available, "Requires 'healpix' package.")
def test_Field_healpix_to_ugrid(self):
"""Test Field.healpix_to_ugrid."""
# HEALPix field
Expand Down Expand Up @@ -3276,6 +3284,7 @@ def test_Field_healpix_to_ugrid(self):
with self.assertRaises(ValueError):
self.f0.healpix_to_ugrid()

@unittest.skipUnless(healpix_available, "Requires 'healpix' package.")
def test_Field_create_latlon_coordinates(self):
"""Test Field.create_latlon_coordinates."""
# ------------------------------------------------------------
Expand Down Expand Up @@ -3334,6 +3343,7 @@ def test_Field_create_latlon_coordinates(self):
self.assertTrue(mc[:16].equals(l2.auxiliary_coordinate(c)[:16]))
self.assertTrue(mc[16:].equals(l1.auxiliary_coordinate(c)[4:]))

@unittest.skipUnless(healpix_available, "Requires 'healpix' package.")
def test_Field_healpix_subspace(self):
"""Test Field.subspace for HEALPix grids"""
f = self.f12
Expand Down Expand Up @@ -3369,6 +3379,7 @@ def test_Field_healpix_subspace(self):
np.array_equal(g.coordinate("healpix_index"), [13, 12])
)

@unittest.skipUnless(healpix_available, "Requires 'healpix' package.")
def test_Field_healpix_decrease_refinement_level(self):
"""Test Field.healpix_decrease_refinement_level."""
f = self.f12
Expand Down Expand Up @@ -3469,6 +3480,7 @@ def my_mean(a, axis=None):
with self.assertRaises(ValueError):
self.f0.healpix_decrease_refinement_level(0, "mean")

@unittest.skipUnless(healpix_available, "Requires 'healpix' package.")
def test_Field_healpix_increase_refinement_level(self):
"""Test Field.healpix_increase_refinement_level."""
f = self.f12
Expand Down
72 changes: 44 additions & 28 deletions cf/test/test_HEALPix_utils.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,78 @@
import datetime
import unittest

import healpix
import numpy as np

import cf

# Create matching lists of selected nested, ring, nuniq and zuniq
# indices for every refinement level.
indices = [
(r, i, healpix.nest2ring(healpix.order2nside(r), i))
for r in range(30)
for i in (0, 7, (12 * 4**r) - 1)
]
refinement_levels, nested_indices, ring_indices = map(list, zip(*indices))

nuniq_indices = [
i + 4 ** (1 + r) for r, i in zip(refinement_levels, nested_indices)
]

zuniq_indices = [
(2 * i + 1) * 4 ** (29 - r)
for r, i in zip(refinement_levels, nested_indices)
]
healpix_imported = True
try:
import healpix # noqa: F401
except ImportError:
healpix_imported = False


class DataTest(unittest.TestCase):
"""Unit tests for HEALPix utilities."""

def setUp(self):
"""Preparations called immediately before each test method."""
# Skip all if healpix module not available!
if not healpix_imported:
self.skipTest(
"Test module requires 'healpix' package. Install it to run all."
)

# Create matching lists of selected nested, ring, nuniq and zuniq
# indices for every refinement level.
indices = [
(r, i, healpix.nest2ring(healpix.order2nside(r), i))
for r in range(30)
for i in (0, 7, (12 * 4**r) - 1)
]
self.refinement_levels, self.nested_indices, self.ring_indices = map(
list, zip(*indices)
)

self.nuniq_indices = [ # noqa: F841
i + 4 ** (1 + r)
for r, i in zip(self.refinement_levels, self.nested_indices)
]

self.zuniq_indices = [ # noqa: F841
(2 * i + 1) * 4 ** (29 - r)
for r, i in zip(self.refinement_levels, self.nested_indices)
]

def test_HEALPix_uniq2zuniq(self):
"""Test _uniq2zuniq"""
from cf.data.dask_utils_healpix import _uniq2zuniq

self.assertTrue(
np.array_equal(_uniq2zuniq(nuniq_indices), zuniq_indices)
np.array_equal(_uniq2zuniq(self.nuniq_indices), self.zuniq_indices)
)

def test_HEALPix_zuniq2uniq(self):
"""Test _zuniq2uniq"""
from cf.data.dask_utils_healpix import _zuniq2uniq

self.assertTrue(
np.array_equal(_zuniq2uniq(zuniq_indices), nuniq_indices)
np.array_equal(_zuniq2uniq(self.zuniq_indices), self.nuniq_indices)
)

def test_HEALPix_zuniq2pix(self):
"""Test _zuniq2pix"""
from cf.data.dask_utils_healpix import _zuniq2pix

# nested
order, i = _zuniq2pix(zuniq_indices, nest=True)
order, i = _zuniq2pix(self.zuniq_indices, nest=True)

self.assertTrue(np.array_equal(order, refinement_levels))
self.assertTrue(np.array_equal(i, nested_indices))
self.assertTrue(np.array_equal(order, self.refinement_levels))
self.assertTrue(np.array_equal(i, self.nested_indices))

# ring
with self.assertRaises(NotImplementedError):
_zuniq2pix(zuniq_indices, nest=False)
_zuniq2pix(self.zuniq_indices, nest=False)

def test_HEALPix_pix2zuniq(self):
"""Test _pix2zuniq"""
Expand All @@ -65,18 +81,18 @@ def test_HEALPix_pix2zuniq(self):
# nested
z = [
_pix2zuniq(r, i, nest=True)
for r, i in zip(refinement_levels, nested_indices)
for r, i in zip(self.refinement_levels, self.nested_indices)
]

self.assertTrue(np.array_equal(z, zuniq_indices))
self.assertTrue(np.array_equal(z, self.zuniq_indices))

# ring
z = [
_pix2zuniq(r, i, nest=False)
for r, i in zip(refinement_levels, ring_indices)
for r, i in zip(self.refinement_levels, self.ring_indices)
]

self.assertTrue(np.array_equal(z, zuniq_indices))
self.assertTrue(np.array_equal(z, self.zuniq_indices))


if __name__ == "__main__":
Expand Down
14 changes: 9 additions & 5 deletions cf/test/test_RegridOperator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
class RegridOperatorTest(unittest.TestCase):

def setUp(self):
src = cf.example_field(0)
dst = cf.example_field(1)
self.r = src.regrids(dst, "linear", return_operator=True)
# Skip all if espmy module not available!
if not esmpy_imported:
self.skipTest(
"Test module requires 'esmpy' package. Install it to run all."
)
else:
src = cf.example_field(0)
dst = cf.example_field(1)
self.r = src.regrids(dst, "linear", return_operator=True)

@unittest.skipUnless(esmpy_imported, "Requires esmpy/ESMF package.")
def test_RegridOperator_attributes(self):
self.assertEqual(self.r.coord_sys, "spherical")
self.assertEqual(self.r.method, "linear")
Expand Down Expand Up @@ -51,7 +56,6 @@ def test_RegridOperator_attributes(self):
self.assertIsNone(self.r.dst_z)
self.assertFalse(self.r.ln_z)

@unittest.skipUnless(esmpy_imported, "Requires esmpy/ESMF package.")
def test_RegridOperator_copy(self):
self.assertIsInstance(self.r.copy(), self.r.__class__)

Expand Down
4 changes: 4 additions & 0 deletions cf/test/test_collapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import tempfile
import unittest
from importlib.util import find_spec

import numpy as np

Expand Down Expand Up @@ -825,6 +826,9 @@ def test_Field_collapse_ugrid(self):
# Check the collpsed fields writes
cf.write(f, tmpfile)

# Note: here only need healpix for cf under-the-hood code, not in test
# directly, so no need to actually import healpix, just test it is there.
@unittest.skipUnless(find_spec("healpix"), "Requires 'healpix' package.")
def test_Field_collapse_HEALPix(self):
"""Test HEALPix collapses."""
f0 = cf.example_field(12)
Expand Down
4 changes: 4 additions & 0 deletions cf/test/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import platform
import sys
import unittest
from importlib.util import find_spec

import dask.array as da
import numpy as np
Expand Down Expand Up @@ -487,6 +488,9 @@ def test_normalize_slice(self):
with self.assertRaises(IndexError):
cf.normalize_slice(index, 8, cyclic=True)

# Note: here only need healpix for cf under-the-hood code, not in test
# directly, so no need to actually import healpix, just test it is there.
@unittest.skipUnless(find_spec("healpix"), "Requires 'healpix' package.")
def test_locate(self):
"""Test cf.locate"""
# HEALPix
Expand Down
Loading