Example #1
0
def std_test_frame(op, frame1, frame2, result, inplace, handle_mask):
    """Standart comparison tests with frames."""
    if handle_mask:
        # Use two different masks to check if they are compined
        mask1 = np.zeros(SHAPE, dtype=bool)
        mask1[2, 2] = 1
        frame1.mask = mask1

        exp_mask = np.zeros(SHAPE, dtype=bool)
        exp_mask[2, 2] = 1

        if isinstance(frame2, FrameData):
            # If frame2 is qfloat, quantity or a number, it don't have mask
            mask2 = np.zeros(SHAPE, dtype=bool)
            mask2[3, 3] = 1
            exp_mask[3, 3] = 1
            frame2.mask = mask2

        result.mask = exp_mask

    res = imarith(frame1, frame2, op, inplace=inplace, join_masks=handle_mask)

    assert_equal(res.data, result.data)
    assert_almost_equal(res.get_uncertainty(False),
                        result.get_uncertainty(False))
    assert_equal(res.unit, result.unit)
    if handle_mask:
        assert_equal(res.mask, result.mask)

    if inplace:
        assert_is(res, frame1)
    else:
        assert_is_not(res, frame1)
Example #2
0
 def test_register_image_equal(self):
     im = gen_image((50, 50), [25], [25], [10000], 10, 0, sigma=3)
     ar = CrossCorrelationRegister()
     im_reg, mask_reg, tform = ar.register_image(im, im)
     assert_is(im_reg, im)
     assert_equal(im_reg, im)
     assert_equal(mask_reg, np.zeros_like(im))
     assert_equal(tform.translation, [0, 0])
Example #3
0
 def test_run_command_stderr(self, com):
     stdout = []
     stderr = []
     _, out, err = run_command(com, stdout=stdout, stderr=stderr,
                               stdout_loglevel='WARN')
     assert_is(out, stdout)
     assert_is(err, stderr)
     assert_equal(stdout, [])
     assert_equal(stderr, ['this is an error'])
Example #4
0
 def test_run_command(self, com):
     stdout = []
     stderr = []
     _, out, err = run_command(com, stdout=stdout, stderr=stderr,
                               stdout_loglevel='WARN')
     assert_is(out, stdout)
     assert_is(err, stderr)
     assert_equal(stdout, [str(i) for i in range(1, 11)])
     assert_equal(stderr, [])
Example #5
0
 def test_register_frame_equal(self, inplace):
     im = gen_image((50, 50), [25], [25], [10000], 10, 0, sigma=3)
     im = FrameData(im)
     ar = CrossCorrelationRegister()
     im_reg = ar.register_framedata(im, im, inplace=inplace)
     if inplace:
         assert_is(im_reg, im)
     else:
         assert_is_not(im_reg, im)
     assert_equal(im_reg.data, im.data)
     assert_equal(im_reg.mask, np.zeros_like(im))
     assert_equal(im_reg.meta['astropop registration_shift'], [0, 0])
Example #6
0
    def test_register_framedata(self, inplace, cval, fill):
        im1 = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
               [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
               [1, 1, 2, 2, 2, 1, 1, 1, 1, 1], [1, 2, 4, 6, 4, 2, 1, 1, 1, 1],
               [1, 2, 6, 8, 6, 2, 1, 1, 1, 1], [1, 2, 4, 6, 4, 2, 1, 1, 1, 1],
               [1, 1, 2, 2, 2, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

        im2 = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
               [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 2, 2, 2, 1, 1, 1],
               [1, 1, 1, 2, 4, 6, 4, 2, 1, 1], [1, 1, 1, 2, 6, 8, 6, 2, 1, 1],
               [1, 1, 1, 2, 4, 6, 4, 2, 1, 1], [1, 1, 1, 1, 2, 2, 2, 1, 1, 1],
               [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

        expect = np.array(im1, dtype='f8')
        expect[0, :] = fill
        expect[:, -2:] = fill

        mask = np.zeros_like(im2, dtype=bool)
        mask[0, :] = 1
        mask[:, -2:] = 1

        expect_unct = np.ones_like(im2, dtype='f8')
        expect_unct[0, :] = np.nan
        expect_unct[:, -2:] = np.nan

        frame1 = FrameData(im1, dtype='f8')
        frame1.meta['moving'] = False
        frame1.uncertainty = np.ones_like(im1)
        frame2 = FrameData(im2, dtype='f8')
        frame2.meta['moving'] = True
        frame2.uncertainty = np.ones_like(im2)

        ar = CrossCorrelationRegister()
        frame_reg = ar.register_framedata(frame1,
                                          frame2,
                                          cval=cval,
                                          inplace=inplace)

        assert_equal(frame_reg.data, expect)
        assert_equal(frame_reg.mask, mask)
        assert_equal(frame_reg.uncertainty, expect_unct)
        assert_equal(frame_reg.meta['astropop registration'],
                     'cross-correlation')
        assert_equal(frame_reg.meta['astropop registration_shift'], [2, -1])
        assert_equal(frame_reg.meta['astropop registration_rot'], 0)
        assert_equal(frame_reg.meta['moving'], True)
        if inplace:
            assert_is(frame_reg, frame2)
        else:
            assert_is_not(frame_reg, frame2)
Example #7
0
 def test_logging_err(self, com):
     logl = []
     # stdout messages must not appear due to loglevel
     expect_log = ['this is an error']
     lh = log_to_list(logger, logl)
     stdout = []
     stderr = []
     _, out, err = run_command(com, stdout=stdout, stderr=stderr,
                               stdout_loglevel='DEBUG',
                               stderr_loglevel='ERROR')
     assert_is(out, stdout)
     assert_is(err, stderr)
     assert_equal(stdout, [])
     assert_equal(stderr, ['this is an error'])
     assert_equal(logl, expect_log)
     logger.removeHandler(lh)
Example #8
0
 def test_register_framedata_asterism(self, inplace):
     frame_list = self.gen_frame_list((1024, 1024))
     reg_list = register_framedata_list(frame_list,
                                        algorithm='asterism-matching',
                                        inplace=inplace,
                                        max_control_points=30,
                                        detection_threshold=5)
     assert_equal(len(frame_list), len(reg_list))
     for org, reg in zip(frame_list, reg_list):
         if inplace:
             assert_is(org, reg)
         else:
             assert_is_not(org, reg)
         assert_almost_equal(reg.meta['astropop registration_shift'],
                             org.meta['test expect_shift'],
                             decimal=0)
Example #9
0
 def test_register_framedata_crosscorr(self, inplace):
     frame_list = self.gen_frame_list((1024, 1024))
     reg_list = register_framedata_list(frame_list,
                                        algorithm='cross-correlation',
                                        inplace=inplace,
                                        upsample_factor=10,
                                        space='real')
     assert_equal(len(frame_list), len(reg_list))
     for org, reg in zip(frame_list, reg_list):
         if inplace:
             assert_is(org, reg)
         else:
             assert_is_not(org, reg)
         assert_almost_equal(reg.meta['astropop registration_shift'],
                             org.meta['test expect_shift'],
                             decimal=0)
Example #10
0
    def test_logging(self, com):
        logl = []
        logcmd = com if isinstance(com, list) else shlex.split(com)
        logcmd = " ".join(logcmd)
        expect_log = [f"Runing: {logcmd}"]
        expect_log += list(range(1, 11))
        expect_log += [f"Done with process: {logcmd}"]

        lh = log_to_list(logger, logl)
        stdout = []
        stderr = []
        _, out, err = run_command(com, stdout=stdout, stderr=stderr,
                                  stdout_loglevel='WARN')
        assert_is(out, stdout)
        assert_is(err, stderr)
        assert_equal(stdout, [str(i) for i in range(1, 11)])
        assert_equal(stderr, [])
        assert_equal(logl, expect_log)
        logger.removeHandler(lh)
Example #11
0
    def test_simple_bias(self, inplace):
        expected = np.ones((20, 20)) * 2
        expected[0:5, 0:5] = 2.5

        frame4bias = FrameData(np.ones((20, 20)) * 3, unit='adu')

        master_bias = FrameData(np.ones((20, 20)), unit='adu')
        master_bias.data[0:5, 0:5] = 0.5

        res4 = subtract_bias(frame4bias, master_bias, inplace=inplace)

        assert_is_instance(res4, FrameData)
        assert_equal(res4.data, expected)
        assert_equal(res4.header['hierarch astropop bias_corrected'], True)

        if inplace:
            assert_is(res4.data, frame4bias.data)
        else:
            assert_is_not(res4.data, frame4bias.data)
Example #12
0
    def test_simple_flat(self, inplace):
        expect = np.ones((20, 20)) * 3
        expect[0:5, 0:5] = 3 / 0.5

        # Checking flat division:
        frame1 = FrameData(np.ones((20, 20)) * 3, unit=u.adu)

        master_flat_dimless = FrameData(np.ones((20, 20)), unit=None)
        master_flat_dimless.data[0:5, 0:5] = 0.5

        res1 = flat_correct(frame1, master_flat_dimless, inplace=inplace)

        assert_is_instance(res1, FrameData)
        assert_equal(res1.data, expect)
        assert_equal(res1.header['hierarch astropop flat_corrected'], True)

        if inplace:
            assert_is(res1.data, frame1.data)
        else:
            assert_is_not(res1.data, frame1.data)
Example #13
0
def test_imarith_ops_frames(op, vs, inplace, handle_mask):
    def gen_frame(v):
        # Gen frames with {'v', 'u'} dict
        shape = (10, 10)
        if v['u'] is None:
            frame = FrameData(np.ones(shape, dtype='f8'), unit='adu')
        else:
            frame = FrameData(np.ones(shape, dtype='f8'), unit='adu',
                              uncertainty=v['u'])
        frame.data[:] = v['v']
        return frame

    frame1 = gen_frame(vs['f1'])
    frame2 = gen_frame(vs['f2'])
    exp_res = gen_frame(vs['r'])
    if handle_mask:
        mask1 = np.zeros((10, 10))
        mask2 = np.zeros((10, 10))
        mask1[5, 5] = 1
        mask2[3, 3] = 1
        exp_mask = np.zeros((10, 10))
        exp_mask[5, 5] = 1
        exp_mask[3, 3] = 1
        frame1.mask = mask1
        frame2.mask = mask2
        exp_res.mask = exp_mask

    res = imarith(frame1, frame2, op, inplace=inplace,
                  join_masks=handle_mask)

    assert_almost_equal(res.data, exp_res.data)
    assert_almost_equal(res.uncertainty, exp_res.uncertainty)
    if handle_mask:
        assert_equal(res.mask, exp_res.mask)

    if inplace:
        assert_is(res, frame1)
    else:
        assert_is_not(res, frame1)
Example #14
0
 def test_assert_is(self):
     a = [1]
     b = a
     assert_is(a, b)
     with pytest.raises(AssertionError):
         assert_is(a, [])