Esempio n. 1
0
def test_generate_thumbnail(
        data_dir,
        input_file,
        params,
        expected_thumb,
        expected_original_size,
        expected_thumb_size,
        num_pages,
        status
):
    # Resolve the input file path
    input_file = data_dir / input_file
    # Mock the request
    url = f"https://example.com/{input_file}"
    responses.add(
        responses.GET,
        url=url,
        body=input_file.read_bytes(),
        status=200
    )
    # Create the lambda request event
    event = _make_event({"url": url, **params})
    # Get the response
    response = t4_lambda_thumbnail.lambda_handler(event, None)
    # Assert the request was handled with no errors
    assert response["statusCode"] == 200, f"response: {response}"
    # only check the body and expected image if it's a successful call
    # Parse the body / the returned thumbnail
    body = json.loads(read_body(response))
    # Assert basic metadata was filled properly
    assert body["info"]["thumbnail_size"] == expected_thumb_size
    if expected_original_size:  # PDFs don't have an expected size
        assert body["info"]["original_size"] == expected_original_size
    if "countPages" in params:
        assert body["info"]["page_count"] == num_pages
    # Assert the produced image is the same as the expected
    if params.get('input') in ('pdf', "pptx"):
        actual = Image.open(BytesIO(base64.b64decode(body['thumbnail'])))
        expected = Image.open(data_dir / expected_thumb)
        actual_array = np.array(actual)
        expected_array = np.array(expected)
        assert actual_array.shape == expected_array.shape
        assert np.allclose(expected_array, actual_array, atol=15, rtol=0.1)
    else:
        actual = AICSImage(base64.b64decode(body['thumbnail']))
        expected = AICSImage(data_dir / expected_thumb)
        assert actual.size() == expected.size()
        assert np.array_equal(actual.reader.data, expected.reader.data)
Esempio n. 2
0
def test_generate_thumbnail(data_dir, input_file, params, expected_thumb,
                            expected_original_size, expected_thumb_size,
                            num_pages, status):
    # Resolve the input file path
    input_file = data_dir / input_file
    # Mock the request
    url = f"https://example.com/{input_file}"
    responses.add(responses.GET,
                  url=url,
                  body=input_file.read_bytes(),
                  status=200)
    # Create the lambda request event
    event = _make_event({"url": url, **params})
    # Get the response
    if expected_thumb == "I16-mode-128-fallback.png":
        # Note that if this set of params fails, it may be that better resamplers
        # have been added for this mode, and either the image or test will need
        # to be updated.
        with _mock(t4_lambda_thumbnail, '_convert_I16_to_L', Image.fromarray):
            response = t4_lambda_thumbnail.lambda_handler(event, None)
    else:
        response = t4_lambda_thumbnail.lambda_handler(event, None)

    # Assert the request was handled with no errors
    assert response["statusCode"] == 200, f"response: {response}"
    # only check the body and expected image if it's a successful call
    # Parse the body / the returned thumbnail
    body = read_body(response)
    # Assert basic metadata was filled properly
    info = json.loads(response["headers"][QUILT_INFO_HEADER])
    assert info["thumbnail_size"] == expected_thumb_size
    if expected_original_size:  # PDFs don't have an expected size
        assert info["original_size"] == expected_original_size
    if "countPages" in params:
        assert info["page_count"] == num_pages
    # Assert the produced image is the same as the expected
    if params.get('input') in ('pdf', "pptx"):
        actual = Image.open(BytesIO(body))
        expected = Image.open(data_dir / expected_thumb)
        actual_array = np.array(actual)
        expected_array = np.array(expected)
        assert actual_array.shape == expected_array.shape
        assert np.allclose(expected_array, actual_array, atol=15, rtol=0.1)
    else:
        actual = AICSImage(body)
        expected = AICSImage(data_dir / expected_thumb)
        assert actual.size() == expected.size()
        assert np.array_equal(actual.reader.data, expected.reader.data)
Esempio n. 3
0
def test_known_dims(data, dims, expected_shape):
    img = AICSImage(data, known_dims=dims)
    assert img.data.shape == expected_shape
    assert img.size_x == expected_shape[5]
    assert img.size_y == expected_shape[4]
    assert img.size_z == expected_shape[3]
    assert img.size_c == expected_shape[2]
    assert img.size_t == expected_shape[1]
    assert img.size_s == expected_shape[0]
    assert img.size(dims) == data.shape
Esempio n. 4
0
def test_force_dims(data_shape, dims, expected):
    img = AICSImage(data=da.zeros(data_shape))
    img._reader._dims = dims
    assert img.data.shape == expected
    assert data_shape == img.get_image_data(out_orientation=dims).shape
    assert img.size_x == expected[5]
    assert img.size_y == expected[4]
    assert img.size_z == expected[3]
    assert img.size_c == expected[2]
    assert img.size_t == expected[1]
    assert img.size_s == expected[0]
    assert img.size(dims) == data_shape
Esempio n. 5
0
def test_known_dims(data, dims, expected_shape):
    # Check basics
    with Profiler() as prof:
        img = AICSImage(data, known_dims=dims)
        assert img.data.shape == expected_shape
        assert img.size_x == expected_shape[5]
        assert img.size_y == expected_shape[4]
        assert img.size_z == expected_shape[3]
        assert img.size_c == expected_shape[2]
        assert img.size_t == expected_shape[1]
        assert img.size_s == expected_shape[0]
        assert img.size(dims) == data.shape

        # Due to reshape and transpose there will be 2 tasks in the graph
        assert len(prof.results) == 2
Esempio n. 6
0
def test_force_dims(data_shape, dims, expected):
    # Check basics
    with Profiler() as prof:
        img = AICSImage(data=da.zeros(data_shape))
        img._reader._dims = dims
        assert img.data.shape == expected
        assert data_shape == img.get_image_data(out_orientation=dims).shape
        assert img.size_x == expected[5]
        assert img.size_y == expected[4]
        assert img.size_z == expected[3]
        assert img.size_c == expected[2]
        assert img.size_t == expected[1]
        assert img.size_s == expected[0]
        assert img.size(dims) == data_shape

        # Two operations are happening
        # First, img.data is called and so two tasks of reshape and transpose are ran
        # Then get_image_data is ran and two more reshape and transpose are ran
        assert len(prof.results) == 4