示例#1
0
def test_extract_array_return_pos():
    '''Check that the return position is calculated correctly.

    The result will differ by mode. All test here are done in 1d because it's
    easier to construct correct test cases.
    '''
    large_test_array = np.arange(5)
    for i in np.arange(-1, 6):
        extracted, new_pos = extract_array(large_test_array,
                                           3,
                                           i,
                                           mode='partial',
                                           return_position=True)
        assert new_pos == (1, )
    # Now check an array with an even number
    for i, expected in zip([1.49, 1.51, 3], [0.49, 0.51, 1]):
        extracted, new_pos = extract_array(large_test_array, (2, ), (i, ),
                                           mode='strict',
                                           return_position=True)
        assert new_pos == (expected, )
    # For mode='trim' the answer actually depends
    for i, expected in zip(np.arange(-1, 6), (-1, 0, 1, 1, 1, 1, 1)):
        extracted, new_pos = extract_array(large_test_array, (3, ), (i, ),
                                           mode='trim',
                                           return_position=True)
        assert new_pos == (expected, )
示例#2
0
def test_extract_array_1d_odd():
    '''Extract 1 d arrays.

    All dimensions are treated the same, so we can test in 1 dim.
    The first few lines test the most error-prone part: Extraction of an
    array on the boundaries.
    Additional tests (e.g. dtype of return array) are done for the last
    case only.
    '''
    assert np.all(
        extract_array(np.arange(4), (3, ), (
            -1, ), fill_value=-99) == np.array([-99, -99, 0]))
    assert np.all(
        extract_array(np.arange(4), (3, ), (
            0, ), fill_value=-99) == np.array([-99, 0, 1]))
    for i in [1, 2]:
        assert np.all(
            extract_array(np.arange(4), (3, ), (
                i, )) == np.array([i - 1, i, i + 1]))
    assert np.all(
        extract_array(np.arange(4), (3, ), (
            3, ), fill_value=-99) == np.array([2, 3, -99]))
    arrayin = np.arange(4.)
    extracted = extract_array(arrayin, (3, ), (4, ))
    assert extracted[0] == 3
    assert np.isnan(extracted[1])  # since I cannot use `==` to test for nan
    assert extracted.dtype == arrayin.dtype
示例#3
0
def test_extract_array_odd_shape_rounding():
    """
    Test overlap_slices (via extract_array) for rounding with an
    even-shaped extraction.
    """

    data = np.arange(10)
    shape = (3, )
    positions_expected = [(1.49, (0, 1, 2)), (1.5, (0, 1, 2)),
                          (1.501, (1, 2, 3)), (1.99, (1, 2, 3)),
                          (2.0, (1, 2, 3)), (2.01, (1, 2, 3)),
                          (2.49, (1, 2, 3)), (2.5, (1, 2, 3)),
                          (2.501, (2, 3, 4)), (2.99, (2, 3, 4)),
                          (3.0, (2, 3, 4)), (3.01, (2, 3, 4))]

    for pos, exp in positions_expected:
        out = extract_array(data, shape, (pos, ), mode='partial')
        assert_array_equal(out, exp)

    # test negative positions
    positions = (-0.99, -0.51, -0.5, -0.49, -0.01, 0)
    exp1 = (-99, -99, 0)
    exp2 = (-99, 0, 1)
    expected = [
        exp1,
    ] * 3 + [
        exp2,
    ] * 4

    for pos, exp in zip(positions, expected):
        out = extract_array(data,
                            shape, (pos, ),
                            mode='partial',
                            fill_value=-99)
        assert_array_equal(out, exp)
示例#4
0
def test_extract_array_1d():
    """In 1d, shape can be int instead of tuple"""
    assert np.all(
        extract_array(np.arange(4), 3, (
            -1, ), fill_value=-99) == np.array([-99, -99, 0]))
    assert np.all(
        extract_array(np.arange(4), 3, -1, fill_value=-99) == np.array(
            [-99, -99, 0]))
示例#5
0
def test_extract_array_1d_even():
    '''Extract 1 d arrays.

    All dimensions are treated the same, so we can test in 1 dim.
    '''
    assert np.all(
        extract_array(np.arange(4), (2, ), (
            0, ), fill_value=-99) == np.array([-99, 0]))
    for i in [1, 2, 3]:
        assert np.all(
            extract_array(np.arange(4), (2, ), (i, )) == np.array([i - 1, i]))
    assert np.all(
        extract_array(np.arange(4.), (2, ), (
            4, ), fill_value=np.inf) == np.array([3, np.inf]))
示例#6
0
def test_extract_array_1d_trim():
    '''Extract 1 d arrays.

    All dimensions are treated the same, so we can test in 1 dim.
    '''
    assert np.all(
        extract_array(np.arange(4), (2, ), (
            0, ), mode='trim') == np.array([0]))
    for i in [1, 2, 3]:
        assert np.all(
            extract_array(np.arange(4), (2, ), (
                i, ), mode='trim') == np.array([i - 1, i]))
    assert np.all(
        extract_array(np.arange(4.), (2, ), (
            4, ), mode='trim') == np.array([3]))
示例#7
0
    def extract(self,x0,x1,y0,y1,**kwargs):
        ''' extract a sub array.  equivalent to hextract '''

        setLTV=lambda k,v: hdr[k]-v if k in hdr else -v
        
        # use astropy to cut
        shape=(y1-y0+1,x1-x0+1)
        position=((y0+y1+1)/2.,(x0+x1+1)/2.)
        img=nddata.extract_array(self.image,shape,position,**kwargs)

        # update the new header keywords
        hdr=self.header.copy()
        hdr['NAXIS1']=img.shape[1]
        hdr['NAXIS2']=img.shape[0]
        hdr['CRPIX1']-=x0
        hdr['CRPIX2']-=y0
        hdr['LTV1']=setLTV('LTV1',x0)
        hdr['LTV2']=setLTV('LTV2',y0)
        
        # add new keywords
        hdr['XMIN']=(x0,'lower x-bound')
        hdr['XMAX']=(x1,'upper x-bound')
        hdr['YMIN']=(y0,'lower y-bound')
        hdr['YMAX']=(y1,'upper y-bound')
        
        # update the history
        history='Extracted from region (x,y)=[{}:{},{}:{}]'.format(x0,x1,y0,y1)
        hdr.add_history(history)

        # create the output
        output=type(self)(img,hdr)

        return output
示例#8
0
 def make_cutouts(self, data):
     cutouts = []
     for xpos, ypos in self.xypos:
         cutouts.append(
             extract_array(data,
                           self.cutout_shape, (ypos, xpos),
                           fill_value=0.0))
     return np.array(cutouts)
示例#9
0
def test_extract_array_easy(mode):
    """
    Test extract_array utility function.

    Test by extracting an array of ones out of an array of zeros.
    """
    large_test_array = np.zeros((11, 11))
    small_test_array = np.ones((5, 5))
    large_test_array[3:8, 3:8] = small_test_array
    extracted_array = extract_array(large_test_array, (5, 5), (5, 5),
                                    mode=mode)
    assert np.all(extracted_array == small_test_array)
示例#10
0
def test_extract_Array_float():
    """integer is at bin center"""
    for a in np.arange(2.51, 3.49, 0.1):
        assert np.all(extract_array(np.arange(5), 3, a) == np.array([2, 3, 4]))
示例#11
0
def test_extract_array_wrong_mode():
    '''Call extract_array with non-existing mode.'''
    with pytest.raises(ValueError) as e:
        extract_array(np.arange(4), (2, ), (0, ), mode='full')
    assert "Valid modes are 'partial', 'trim', and 'strict'." == str(e.value)
示例#12
0
def test_extract_array_nan_fillvalue():
    if Version(np.__version__) >= Version('1.20'):
        msg = 'fill_value cannot be set to np.nan if the input array has'
        with pytest.raises(ValueError, match=msg):
            extract_array(np.ones((10, 10), dtype=int), (5, 5), (1, 1),
                          fill_value=np.nan)