# Licensed under an MIT open source license - see LICENSE
from __future__ import print_function, absolute_import, division


import numpy.testing as npt
import astropy.units as u

from ..statistics import DeltaVariance, DeltaVariance_Distance
from ._testing_data import \
    dataset1, dataset2, computed_data, computed_distances


def test_DelVar_method():
    tester = \
        DeltaVariance(dataset1["moment0"],
                      weights=dataset1["moment0_error"][0])
    tester.run()
    npt.assert_allclose(tester.delta_var, computed_data['delvar_val'])
    npt.assert_almost_equal(tester.slope, computed_data['delvar_slope'])


def test_DelVar_method_fitlimits():

    distance = 250 * u.pc

    xlow = 4 * u.pix
    xhigh = 30 * u.pix

    tester = DeltaVariance(dataset1["moment0"])
    tester.run(xlow=xlow, xhigh=xhigh)

    xlow = xlow.value * (dataset1['moment0'][1]['CDELT2'] * u.deg)
    xhigh = xhigh.value * (dataset1['moment0'][1]['CDELT2'] * u.deg)

    tester2 = DeltaVariance(dataset1["moment0"])
    tester2.run(xlow=xlow, xhigh=xhigh)

    xlow = xlow.to(u.rad).value * distance
    xhigh = xhigh.to(u.rad).value * distance

    tester3 = DeltaVariance(dataset1["moment0"], distance=distance)
    tester3.run(xlow=xlow, xhigh=xhigh)

    npt.assert_allclose(tester.slope, tester2.slope)
    npt.assert_allclose(tester.slope, tester3.slope)


def test_DelVar_distance():
    tester_dist = \
        DeltaVariance_Distance(dataset1["moment0"],
                               dataset2["moment0"],
                               weights1=dataset1["moment0_error"][0],
                               weights2=dataset2["moment0_error"][0])
    tester_dist.distance_metric()
    npt.assert_almost_equal(tester_dist.curve_distance,
                            computed_distances['delvar_curve_distance'],
                            decimal=3)
    npt.assert_almost_equal(tester_dist.slope_distance,
                            computed_distances['delvar_slope_distance'],
                            decimal=3)

@@ -347,24 +347,188 @@
 r_method():%0A
+    # There is a difference in the convolution of astropy 1.x and 2.x on the%0A    # large-scales. Restrict the fitting region to where the convolution%0A    # agrees.%0A
     tester =
@@ -648,17 +648,134 @@
 ter.run(
-)
+xhigh=11. * u.pix)%0A    # The slice is again to restrict where the convolution functions both give%0A    # the same value
 %0A    npt
@@ -807,17 +807,46 @@
 elta_var
-,
+%5B:-7%5D,%0A                       
  compute
@@ -865,16 +865,21 @@
 ar_val'%5D
+%5B:-7%5D
 )%0A    np
@@ -943,24 +943,678 @@
 _slope'%5D)%0A%0A%0A
+def test_DelVar_method_fill():%0A    # There is a difference in the convolution of astropy 1.x and 2.x on the%0A    # large-scales. Restrict the fitting region to where the convolution%0A    # agrees.%0A    tester = %5C%0A        DeltaVariance(dataset1%5B%22moment0%22%5D,%0A                      weights=dataset1%5B%22moment0_error%22%5D%5B0%5D)%0A    tester.run(boundary='fill', xhigh=11. * u.pix)%0A    # The slice is again to restrict where the convolution functions both give%0A    # the same value%0A    npt.assert_allclose(tester.delta_var%5B:-7%5D,%0A                        computed_data%5B'delvar_fill_val'%5D%5B:-7%5D)%0A    npt.assert_almost_equal(tester.slope, computed_data%5B'delvar_fill_slope'%5D)%0A%0A%0A
 def test_Del
@@ -2613,32 +2613,81 @@
 ment0_error%22%5D%5B0%5D
+,%0A                               xhigh=11 * u.pix
 )%0A    tester_dis

Update del-var tests and add boundary='fill' test