Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increasing Test coverage #59

Merged
merged 24 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions changelog/59.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed minimum version of `skimage` to 0.18.0, preventing faulty code in :meth:`sunkit-image.utils.points_in_poly`.
jeffreypaul15 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions changelog/59.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added multiple unit tests to increase code coverage.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ include_package_data = True
setup_requires =
setuptools_scm
install_requires =
scikit-image>=0.16.2
scikit-image>=0.18.0
sunpy>=2.0.0

[options.extras_require]
Expand Down
67 changes: 67 additions & 0 deletions sunkit_image/tests/test_radial.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,70 @@ def test_set_attenuation_coefficients():
_ = rad.set_attenuation_coefficients(order, cutoff=5)

assert str(record.value) == "Cutoff cannot be greater than order + 1."


def test_fit_polynomial_to_log_radial_intensity():

radii = (0.001, 0.002) * u.R_sun
intensity = np.asarray([1, 2])
degree = 1
expected = np.polyfit(radii.to(u.R_sun).value, np.log(intensity), degree)

assert np.allclose(rad.fit_polynomial_to_log_radial_intensity(radii, intensity, degree), expected)


def test_calculate_fit_radial_intensity():

polynomial = np.asarray([1, 2, 3])
radii = (0.001, 0.002) * u.R_sun
expected = np.exp(np.poly1d(polynomial)(radii.to(u.R_sun).value))

assert np.allclose(rad.calculate_fit_radial_intensity(radii, polynomial), expected)


def test_normalize_fit_radial_intensity():
polynomial = np.asarray([1, 2, 3])
radii = (0.001, 0.002) * u.R_sun
normalization_radii = (0.003, 0.004) * u.R_sun
expected = rad.calculate_fit_radial_intensity(radii, polynomial) / rad.calculate_fit_radial_intensity(
normalization_radii, polynomial
)

assert np.allclose(rad.normalize_fit_radial_intensity(radii, polynomial, normalization_radii), expected)


def test_intensity_enhance(map_test1):
degree = 1
fit_range = [1, 1.5] * u.R_sun
normalization_radius = 1 * u.R_sun
summarize_bin_edges = "center"
scale = 1 * map_test1.rsun_obs
radial_bin_edges = u.Quantity(utils.equally_spaced_bins()) * u.R_sun

radial_intensity = utils.get_radial_intensity_summary(map_test1, radial_bin_edges, scale=scale)

map_r = utils.find_pixel_radii(map_test1).to(u.R_sun)

radial_bin_summary = utils.bin_edge_summary(radial_bin_edges, summarize_bin_edges).to(u.R_sun)

fit_here = np.logical_and(
fit_range[0].to(u.R_sun).value <= radial_bin_summary.to(u.R_sun).value,
radial_bin_summary.to(u.R_sun).value <= fit_range[1].to(u.R_sun).value,
)

polynomial = rad.fit_polynomial_to_log_radial_intensity(
radial_bin_summary[fit_here], radial_intensity[fit_here], degree
)

enhancement = 1 / rad.normalize_fit_radial_intensity(map_r, polynomial, normalization_radius)
enhancement[map_r < normalization_radius] = 1

with pytest.raises(ValueError, match="The fit range must be strictly increasing."):
rad.intensity_enhance(
smap=map_test1, radial_bin_edges=radial_bin_edges, scale=scale, fit_range=fit_range[::-1]
)

assert np.allclose(
enhancement * map_test1.data,
rad.intensity_enhance(smap=map_test1, radial_bin_edges=radial_bin_edges, scale=scale).data,
)
137 changes: 117 additions & 20 deletions sunkit_image/utils/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import warnings

import numpy as np
import pytest

import astropy.units as u
from astropy.tests.helper import assert_quantity_allclose

from sunkit_image.utils import bin_edge_summary, equally_spaced_bins, find_pixel_radii
import sunkit_image.utils as utils
from sunkit_image import asda
from sunkit_image.data.test import get_test_filepath


@pytest.fixture
Expand All @@ -18,31 +22,31 @@ def smap():

def test_equally_spaced_bins():
# test the default
esb = equally_spaced_bins()
esb = utils.equally_spaced_bins()
assert esb.shape == (2, 100)
assert esb[0, 0] == 1.0
assert esb[1, 0] == 1.01
assert esb[0, 99] == 1.99
assert esb[1, 99] == 2.00

# Bins are 0.015 wide
esb2 = equally_spaced_bins(inner_value=0.5)
esb2 = utils.equally_spaced_bins(inner_value=0.5)
assert esb2.shape == (2, 100)
assert esb2[0, 0] == 0.5
assert esb2[1, 0] == 0.515
assert esb2[0, 99] == 1.985
assert esb2[1, 99] == 2.00

# Bins are 0.2 wide
esb2 = equally_spaced_bins(outer_value=3.0)
esb2 = utils.equally_spaced_bins(outer_value=3.0)
assert esb2.shape == (2, 100)
assert esb2[0, 0] == 1.0
assert esb2[1, 0] == 1.02
assert esb2[0, 99] == 2.98
assert esb2[1, 99] == 3.00

# Bins are 0.01 wide
esb2 = equally_spaced_bins(nbins=1000)
esb2 = utils.equally_spaced_bins(nbins=1000)
assert esb2.shape == (2, 1000)
assert esb2[0, 0] == 1.0
assert esb2[1, 0] == 1.001
Expand All @@ -51,42 +55,42 @@ def test_equally_spaced_bins():

# The radii have the correct relative sizes
with pytest.raises(ValueError):
equally_spaced_bins(inner_value=1.0, outer_value=1.0)
utils.equally_spaced_bins(inner_value=1.0, outer_value=1.0)
with pytest.raises(ValueError):
equally_spaced_bins(inner_value=1.5, outer_value=1.0)
utils.equally_spaced_bins(inner_value=1.5, outer_value=1.0)

# The number of bins is strictly greater than 0
with pytest.raises(ValueError):
equally_spaced_bins(nbins=0)
utils.equally_spaced_bins(nbins=0)


def test_bin_edge_summary():
esb = equally_spaced_bins()
esb = utils.equally_spaced_bins()

center = bin_edge_summary(esb, "center")
center = utils.bin_edge_summary(esb, "center")
assert center.shape == (100,)
assert center[0] == 1.005
assert center[99] == 1.995

left = bin_edge_summary(esb, "left")
left = utils.bin_edge_summary(esb, "left")
assert left.shape == (100,)
assert left[0] == 1.0
assert left[99] == 1.99

right = bin_edge_summary(esb, "right")
right = utils.bin_edge_summary(esb, "right")
assert right.shape == (100,)
assert right[0] == 1.01
assert right[99] == 2.0

# Correct selection of summary type
with pytest.raises(ValueError):
bin_edge_summary(esb, "should raise the error")
utils.bin_edge_summary(esb, "should raise the error")

# The correct shape of bin edges are passed in
with pytest.raises(ValueError):
bin_edge_summary(np.arange(0, 10), "center")
utils.bin_edge_summary(np.arange(0, 10), "center")
with pytest.raises(ValueError):
bin_edge_summary(np.zeros((3, 4)), "center")
utils.bin_edge_summary(np.zeros((3, 4)), "center")


@pytest.mark.remote_data
Expand All @@ -95,7 +99,7 @@ def test_find_pixel_radii(smap):
known_maximum_pixel_radius = 1.84183121

# Calculate the pixel radii
pixel_radii = find_pixel_radii(smap)
pixel_radii = utils.find_pixel_radii(smap)

# The shape of the pixel radii is the same as the input map
assert pixel_radii.shape[0] == int(smap.dimensions[0].value)
Expand All @@ -108,10 +112,103 @@ def test_find_pixel_radii(smap):
assert_quantity_allclose((np.max(pixel_radii)).value, known_maximum_pixel_radius)

# Test that the new scale is used
pixel_radii = find_pixel_radii(smap, scale=2 * smap.rsun_obs)
pixel_radii = utils.find_pixel_radii(smap, scale=2 * smap.rsun_obs)
assert_quantity_allclose(np.max(pixel_radii).value, known_maximum_pixel_radius / 2)


def test_get_radial_intensity_summary():
# TODO: Write some tests.
pass
@pytest.mark.remote_data
def test_get_radial_intensity_summary(smap):

radial_bin_edges = u.Quantity(utils.equally_spaced_bins()) * u.R_sun
summary = np.mean

map_r = utils.find_pixel_radii(smap, scale=smap.rsun_obs).to(u.R_sun)

nbins = radial_bin_edges.shape[1]

lower_edge = [map_r > radial_bin_edges[0, i].to(u.R_sun) for i in range(0, nbins)]
upper_edge = [map_r < radial_bin_edges[1, i].to(u.R_sun) for i in range(0, nbins)]

with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
nabobalis marked this conversation as resolved.
Show resolved Hide resolved
expected = np.asarray([summary(smap.data[lower_edge[i] * upper_edge[i]]) for i in range(0, nbins)])

assert np.allclose(utils.get_radial_intensity_summary(smap, radial_bin_edges=radial_bin_edges), expected)


def test_calculate_gamma():
vel_file = get_test_filepath("asda_vxvy.npz")
get_test_filepath("asda_correct.npz")
vxvy = np.load(vel_file, allow_pickle=True)
vx = vxvy["vx"]
vy = vxvy["vy"]
vxvy["data"]

factor = 1
lo = asda.Asda(vx, vy, factor=factor)

shape = vx.shape
r = 3

index = np.array([[i, j] for i in np.arange(r, shape[0] - r) for j in np.arange(r, shape[1] - r)])

vel = lo.gen_vel(index[1], index[0])

pm = np.array(
[[i, j] for i in np.arange(-lo.r, lo.r + 1) for j in np.arange(-lo.r, lo.r + 1)],
dtype=float,
)

N = (2 * lo.r + 1) ** 2

pnorm = np.linalg.norm(pm, axis=1)

cross = np.cross(pm, vel[..., 0])
vel_norm = np.linalg.norm(vel[..., 0], axis=2)
sint = cross / (pnorm * vel_norm + 1e-10)

expected = np.nansum(sint, axis=1) / N

assert np.allclose(expected, utils.calc_gamma(pm, vel[..., 0], pnorm, N))


def test_remove_duplicate():

test_data = np.random.rand(5, 2)
data_ = np.append(test_data, [test_data[0]], axis=0)
expected = np.delete(data_, -1, 0)
assert (utils.remove_duplicate(data_) == expected).all()


def test_points_in_poly():

test_data = np.asarray([[0, 0], [0, 1], [0, 2], [1, 2], [2, 2], [2, 0]])

with pytest.raises(ValueError, match="Polygon must be defined as a n x 2 array!"):
utils.points_in_poly(test_data.T)

expected = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
assert expected == utils.points_in_poly(test_data)


def test_reform_2d():

test_data = np.asarray([[0, 0], [1, 2], [3, 4]])

with pytest.raises(ValueError, match="Parameter 'factor' must be an integer!"):
utils.reform2d(test_data, 2.2)
with pytest.raises(ValueError, match="Input array must be 2d!"):
utils.reform2d(test_data[0], 2)

expected = np.asarray(
[
[0.0, 0.0, 0.0, 0.0],
[0.5, 0.75, 1.0, 1.0],
[1.0, 1.5, 2.0, 2.0],
[2.0, 2.5, 3.0, 3.0],
[3.0, 3.5, 4.0, 4.0],
[3.0, 3.5, 4.0, 4.0],
]
)

assert np.allclose(utils.reform2d(test_data, 2), expected)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ deps =

# Oldest deps we pin against.
oldestdeps: sunpy<2.1
oldestdeps: scikit-image<0.17.0
oldestdeps: scikit-image<=0.18.0
jeffreypaul15 marked this conversation as resolved.
Show resolved Hide resolved

# These are specific online extras we use to run the online tests.
online: pytest-rerunfailures
Expand Down