def test_update_fit_parameters(input_stream, output_stream, starburst_params,
                               ransac_params, pupil_bounding_box,
                               cr_bounding_box, kwargs):
    tracker = et.EyeTracker(input_stream)
    tracker.update_fit_parameters(starburst_params, ransac_params,
                                  pupil_bounding_box, cr_bounding_box,
                                  **kwargs)
    if input_stream is None:
        assert(tracker.im_shape is None)
    else:
        assert(tracker.im_shape == input_stream.frame_shape)
    if pupil_bounding_box is None:
        test_pupil_bbox = et.default_bounding_box(tracker.im_shape)
    else:
        test_pupil_bbox = pupil_bounding_box
    if cr_bounding_box is None:
        test_cr_bbox = et.default_bounding_box(tracker.im_shape)
    else:
        test_cr_bbox = cr_bounding_box
    assert(np.all(tracker.pupil_bounding_box == test_pupil_bbox))
    assert(np.all(tracker.cr_bounding_box == test_cr_bbox))
    assert(input_stream == tracker.input_stream)
    if output_stream is None:
        assert(tracker.annotator.output_stream is None)
    else:
        assert(tracker.annotator.output_stream is not None)
def test_mean_frame(shape, input_stream, output_stream, starburst_params,
                    ransac_params, pupil_bounding_box,
                    cr_bounding_box, generate_QC_output, kwargs):
    tracker = et.EyeTracker(shape, input_stream, output_stream,
                            starburst_params, ransac_params,
                            pupil_bounding_box, cr_bounding_box,
                            generate_QC_output, **kwargs)
    assert(tracker.mean_frame.shape == shape)
def test_process_image(image, input_stream, output_stream,
                       starburst_params, ransac_params, pupil_bounding_box,
                       cr_bounding_box, kwargs):
    tracker = et.EyeTracker(input_stream, output_stream,
                            starburst_params, ransac_params,
                            pupil_bounding_box, cr_bounding_box, **kwargs)
    with patch.object(tracker, "update_last_pupil_color") as mock_update:
        cr, pupil, cr_err, pupil_err = tracker.process_image(image)
        if not kwargs.get("adaptive_pupil", True):
            assert mock_update.call_count == 0
def test_process_stream(shape, input_stream, output_stream, starburst_params,
                        ransac_params, pupil_bounding_box,
                        cr_bounding_box, generate_QC_output, kwargs):
    tracker = et.EyeTracker(shape, input_stream, output_stream,
                            starburst_params, ransac_params,
                            pupil_bounding_box, cr_bounding_box,
                            generate_QC_output, **kwargs)
    pupil, cr = tracker.process_stream(start=3)
    assert(pupil.shape == (input_stream.num_frames - 3, 5))
    pupil, cr = tracker.process_stream(update_mean_frame=False)
    assert(pupil.shape == (input_stream.num_frames, 5))
    tracker.input_stream = InputStream(0)
    with patch.object(tracker, "process_image") as mock_process:
        pupil, cr = tracker.process_stream()
        assert mock_process.call_count == 0
def test_update_last_pupil_color(mock_ellipse_points, pupil_params,
                                 recolor_cr):
    tracker = et.EyeTracker((10, 10), None, recolor_cr=recolor_cr)
    with patch.object(tracker, "blurred_image",
                      MagicMock(return_value=np.zeros((10, 10)))) as blur:
        with patch.object(tracker, "cr_filled_image",
                          MagicMock(return_value=np.ones((10, 10)))) as cr:
            tracker.update_last_pupil_color(pupil_params)
            if np.any(np.isnan(pupil_params)):
                assert cr.__getitem__.call_count == 0
                assert blur.__getitem__.call_count == 0
                assert mock_ellipse_points.call_count == 0
            elif recolor_cr:
                cr.__getitem__.assert_called_once()
                mock_ellipse_points.assert_called_once_with(pupil_params,
                                                            (10, 10))
                assert blur.__getitem__.call_count == 0
            else:
                blur.__getitem__.assert_called_once()
                mock_ellipse_points.assert_called_once_with(pupil_params,
                                                            (10, 10))
                assert cr.__getitem__.call_count == 0
def test_eye_tracker_init(im_shape, input_stream, output_stream,
                          starburst_params, ransac_params, pupil_bounding_box,
                          cr_bounding_box, kwargs):
    tracker = et.EyeTracker(im_shape, input_stream, output_stream,
                            starburst_params, ransac_params,
                            pupil_bounding_box, cr_bounding_box, **kwargs)
    assert(tracker.im_shape == im_shape)
    if pupil_bounding_box is None:
        test_pupil_bbox = et.default_bounding_box(im_shape)
    else:
        test_pupil_bbox = pupil_bounding_box
    if cr_bounding_box is None:
        test_cr_bbox = et.default_bounding_box(im_shape)
    else:
        test_cr_bbox = cr_bounding_box
    assert(np.all(tracker.pupil_bounding_box == test_pupil_bbox))
    assert(np.all(tracker.cr_bounding_box == test_cr_bbox))
    assert(input_stream == tracker.input_stream)
    if output_stream is None:
        assert(tracker.annotator.output_stream is None)
    else:
        assert(tracker.annotator.output_stream is not None)
def test_update_last_pupil_color(mock_ellipse_points, pupil_params,
                                 recolor_cr):
    input_stream = InputStream()
    tracker = et.EyeTracker(input_stream, recolor_cr=recolor_cr)
    shape = input_stream.frame_shape
    with patch.object(tracker, "blurred_image",
                      MagicMock(return_value=np.zeros(shape))) as blur:
        with patch.object(tracker, "cr_filled_image",
                          MagicMock(return_value=np.ones(shape))) as cr:
            tracker.update_last_pupil_color(pupil_params)
            if np.any(np.isnan(pupil_params)):
                assert cr.__getitem__.call_count == 0
                assert blur.__getitem__.call_count == 0
                assert mock_ellipse_points.call_count == 0
            elif recolor_cr:
                cr.__getitem__.assert_called_once()
                mock_ellipse_points.assert_called_once_with(pupil_params,
                                                            ANY)
                assert blur.__getitem__.call_count == 0
            else:
                blur.__getitem__.assert_called_once()
                mock_ellipse_points.assert_called_once_with(pupil_params,
                                                            ANY)
                assert cr.__getitem__.call_count == 0