Beispiel #1
0
def test_tile_img_pil_serial_exists(capsys):
    """Test convert.tile_img skips tiling"""
    helpers.disbale_tqdm()
    helpers.setup(with_data=True)

    out_dir = helpers.TEST_PATH
    test_image = os.path.join(out_dir, "test_tiling_image.jpg")
    pbar_loc = 0
    min_zoom = 0
    image_engine = convert.IMG_ENGINE_PIL

    os.mkdir(os.path.join(out_dir, "test_tiling_image"))

    convert.tile_img(
        test_image,
        pbar_loc,
        min_zoom=min_zoom,
        image_engine=image_engine,
        out_dir=out_dir,
    )

    captured = capsys.readouterr()
    helpers.tear_down()
    helpers.enable_tqdm()

    assert "test_tiling_image.jpg already tiled. Skipping tiling." in captured.out
Beispiel #2
0
def test_tile_img_mpl_parallel():
    """Test convert.tile_img"""
    helpers.disbale_tqdm()
    helpers.setup(with_data=True)

    out_dir = helpers.TEST_PATH
    test_image = os.path.join(out_dir, "test_tiling_image.jpg")
    pbar_loc = 0
    min_zoom = 0
    image_engine = convert.IMG_ENGINE_MPL

    convert.tile_img(
        test_image,
        pbar_loc,
        min_zoom=min_zoom,
        image_engine=image_engine,
        out_dir=out_dir,
        mp_procs=2,
    )

    expected_dir = os.path.join(out_dir, "expected_test_tiling_image_mpl")
    actual_dir = os.path.join(out_dir, "test_tiling_image")

    dirs_match = helpers.compare_file_directories(expected_dir, actual_dir)

    helpers.tear_down()
    helpers.enable_tqdm()

    assert dirs_match
Beispiel #3
0
def test_line_to_json_ra_dec():
    """Test convert.line_to_json with ra/dec"""
    helpers.setup(with_data=True)

    in_wcs = WCS(
        fits.getheader(os.path.join(helpers.TEST_PATH, "test_image.fits")))

    columns = ["id", "ra", "dec", "col1", "col2"]
    catalog_assets_path = os.path.join(helpers.TEST_PATH, "catalog_assets")
    os.mkdir(catalog_assets_path)
    in_line = ["1", "53.18575", "-27.898664", "abc", "123"]

    expected_json = dict(
        geometry=dict(coordinates=[289.87867109328727, 301.2526406693396]),
        tags=dict(
            a=-1,
            b=-1,
            theta=-1,
            catalog_id="1",
            cat_path="catalog_assets",
        ),
    )

    actual_json = convert.line_to_json(
        in_wcs,
        columns,
        catalog_assets_path,
        in_line,
    )

    helpers.tear_down()

    assert expected_json == actual_json
Beispiel #4
0
def test_layer_name_to_dict_catalog():
    """test cartographer.layer_name_to_dict"""
    helpers.setup()
    out_dir = helpers.DATA_DIR
    min_zoom = 0
    max_zoom = 2
    name = "test"
    color = "#4C72B0"
    columns = "a,b,c"

    with open(os.path.join(out_dir, f"{name}.columns"), "w") as f:
        f.write(columns)

    actual_dict = c.layer_name_to_dict(out_dir, min_zoom, max_zoom, name,
                                       color)

    expected_dict = dict(
        directory=name + "/{z}/{y}/{x}.pbf",
        name=name,
        min_zoom=min_zoom,
        max_zoom=max_zoom + 5,
        max_native_zoom=max_zoom,
        color=color,
        columns=[f'"{c}"' for c in columns.split(",")],
    )

    helpers.tear_down()

    assert expected_dict == actual_dict
Beispiel #5
0
def test_line_to_json_xy():
    """Test convert.line_to_json with x/y"""

    helpers.setup()

    in_wcs = None
    columns = ["id", "x", "y", "col1", "col2"]
    catalog_assets_path = os.path.join(helpers.TEST_PATH, "catalog_assets")
    os.mkdir(catalog_assets_path)
    in_line = ["1", "10", "20", "abc", "123"]

    expected_json = dict(
        geometry=dict(coordinates=[9.5, 19.5]),
        tags=dict(
            a=-1,
            b=-1,
            theta=-1,
            catalog_id="1",
            cat_path="catalog_assets",
        ),
    )

    actual_json = convert.line_to_json(
        in_wcs,
        columns,
        catalog_assets_path,
        in_line,
    )

    helpers.tear_down()

    assert expected_json == actual_json
Beispiel #6
0
def test_chart_with_wcs():
    """test cartographer.chart"""

    helpers.setup(with_data=True)

    out_dir = helpers.TEST_PATH
    title = "test"
    map_layer_names = "test_layer"
    marker_file_names = "test_marker"
    wcs = WCS(os.path.join(out_dir, "test_image.fits"))
    columns = "a,b,c"

    with open(os.path.join(out_dir, f"{marker_file_names}.columns"), "w") as f:
        f.write(columns)

    list(
        map(
            lambda r: os.makedirs(
                os.path.join(out_dir, map_layer_names, str(r))),
            range(2),
        ))

    os.mkdir(os.path.join(out_dir, "js"))
    os.mkdir(os.path.join(out_dir, "css"))

    c.chart(
        out_dir,
        title,
        [map_layer_names],
        [marker_file_names],
        wcs,
        float("inf"),
        [100, 100],
    )

    # inject current version in to test_index.html
    version = helpers.get_version()
    raw_path = os.path.join(out_dir, "test_index_wcs.html")
    with open(raw_path, "r") as f:
        converted = list(
            map(lambda l: l.replace("VERSION", version), f.readlines()))

    with open(raw_path, "w") as f:
        f.writelines(converted)

    actual_html = os.path.join(out_dir, "index.html")
    expected_html = os.path.join(out_dir, "test_index_wcs.html")

    files_match = filecmp.cmp(expected_html, actual_html)

    helpers.tear_down()

    assert files_match
Beispiel #7
0
def test_move_support_images():
    """test cartographer.move_support_images"""

    helpers.setup()

    actual_moved_images = c.move_support_images(helpers.TEST_PATH)

    expected_moved_images = ["favicon.ico", "loading-logo.svg"]

    helpers.tear_down()

    assert actual_moved_images == expected_moved_images
Beispiel #8
0
def test_make_fname_js_safe_no_change():
    """Test the cartographer.make_fname_js_safe functions."""

    expected_shape = (738, 480)

    helpers.setup(with_data=True)

    actual_shape = u.get_fits_image_size(
        os.path.join(helpers.TEST_PATH, "test_image.fits"))
    helpers.tear_down()

    assert expected_shape == actual_shape
Beispiel #9
0
def test_get_array_png():
    """Test convert.get_array"""

    helpers.setup()

    # make test array
    expected_array = camera()
    out_path = os.path.join(helpers.TEST_PATH, "test.png")
    Image.fromarray(expected_array).save(out_path)

    # get test array
    actual_array = convert.get_array(out_path)

    helpers.tear_down()

    np.testing.assert_equal(expected_array, np.flipud(actual_array))
Beispiel #10
0
def test_get_array_fits_fails():
    """Test convert.get_array"""

    helpers.setup()

    # make test array
    tmp = np.zeros((3), dtype=np.float32)
    out_path = os.path.join(helpers.TEST_PATH, "test.fits")
    fits.PrimaryHDU(data=tmp).writeto(out_path)

    with pytest.raises(ValueError) as excinfo:
        convert.get_array(out_path)

    helpers.tear_down()

    assert "FitsMap only supports 2D" in str(excinfo.value)
Beispiel #11
0
def test_dir_to_map():
    """Integration test for making files into map"""
    helpers.disbale_tqdm()
    helpers.setup(with_data=True)

    with_path = lambda f: os.path.join(helpers.TEST_PATH, f)
    out_dir = with_path("test_web")
    in_dir = with_path("test_web_in")
    if not os.path.exists(in_dir):
        os.mkdir(in_dir)

    files = [
        "test_tiling_image.jpg",
        "test_catalog_radec.cat",
    ]

    for f in files:
        shutil.copy(with_path(f), os.path.join(in_dir, f))

    expected_dir = with_path("expected_test_web")

    # inject current version in to test_index.html
    version = helpers.get_version()
    raw_path = os.path.join(expected_dir, "index.html")
    with open(raw_path, "r") as f:
        converted = list(
            map(lambda l: l.replace("VERSION", version), f.readlines()))

    with open(raw_path, "w") as f:
        f.writelines(converted)

    convert.dir_to_map(
        in_dir,
        out_dir=out_dir,
        catalog_delim=" ",
        cat_wcs_fits_file=with_path("test_image.fits"),
    )

    actual_dir = out_dir

    dirs_match = helpers.compare_file_directories(expected_dir, actual_dir)

    helpers.tear_down()
    helpers.enable_tqdm()

    assert dirs_match
Beispiel #12
0
def test_build_conditional_css():
    """test cartographer.build_conditional_css"""

    helpers.setup()

    actual_css = c.build_conditional_css(helpers.TEST_PATH)

    expected_css = "\n".join([
        "    <link rel='preload' href='https://unpkg.com/[email protected]/dist/leaflet-search.src.css'  as='style' onload='this.rel=\"stylesheet\"'/>",
        "    <link rel='preload' href='css/MarkerCluster.Default.min.css'  as='style' onload='this.rel=\"stylesheet\"'/>",
        "    <link rel='preload' href='css/MarkerCluster.min.css'  as='style' onload='this.rel=\"stylesheet\"'/>",
        "    <link rel='preload' href='css/MarkerPopup.min.css'  as='style' onload='this.rel=\"stylesheet\"'/>",
        "    <link rel='preload' href='css/TileNearestNeighbor.min.css'  as='style' onload='this.rel=\"stylesheet\"'/>",
    ])

    helpers.tear_down()

    assert expected_css == actual_css
Beispiel #13
0
def test_dir_to_map_fails_no_files():
    """Integration test for making files into map"""
    helpers.disbale_tqdm()
    helpers.setup(with_data=True)

    with_path = lambda f: os.path.join(helpers.TEST_PATH, f)
    out_dir = with_path("test_web")
    in_dir = with_path("test_web_in")
    if not os.path.exists(in_dir):
        os.mkdir(in_dir)

    with pytest.raises(ValueError):
        convert.dir_to_map(in_dir,
                           out_dir=out_dir,
                           cat_wcs_fits_file=with_path("test_image.fits"))

    helpers.tear_down()
    helpers.enable_tqdm()
Beispiel #14
0
def test_make_tile_mpl():
    """Test convert.make_tile_mpl"""
    helpers.setup()

    out_dir = helpers.TEST_PATH
    test_arr = np.arange(100 * 100).reshape([100, 100])
    test_job = (0, 0, 0, slice(0, 100), slice(0, 100))

    os.makedirs(os.path.join(out_dir, "0/0/"))

    convert.make_tile_mpl(out_dir, test_arr, test_job)

    actual_img = np.array(Image.open(os.path.join(out_dir, "0/0/0.png")))

    expected_img = test_arr // 256

    np.array_equal(expected_img, actual_img)

    helpers.tear_down()
Beispiel #15
0
def test_get_array_fits():
    """Test convert.get_array"""

    helpers.setup()

    # make test array
    tmp = np.zeros((3, 3), dtype=np.float32)
    out_path = os.path.join(helpers.TEST_PATH, "test.fits")
    fits.PrimaryHDU(data=tmp).writeto(out_path)

    pads = [[0, 1], [0, 1]]
    expected_array = np.pad(tmp, pads, mode="constant", constant_values=np.nan)

    # get test array
    actual_array = convert.get_array(out_path)

    helpers.tear_down()

    np.testing.assert_equal(expected_array, actual_array)
Beispiel #16
0
def test_build_conditional_js():
    """test cartographer.build_conditional_js"""

    helpers.setup()

    acutal_js = c.build_conditional_js(helpers.TEST_PATH)

    expected_js = "\n".join([
        '    <script src="https://unpkg.com/[email protected]/dist/pbf.js", crossorigin=""></script>',
        '    <script src="https://cdn.jsdelivr.net/npm/leaflet-search" crossorigin=""></script>',
        "    <script src='js/customSearch.min.js'></script>",
        "    <script src='js/l.ellipse.min.js'></script>",
        "    <script src='js/tiledMarkers.min.js'></script>",
        "    <script src='js/vector-tile.min.js'></script>",
    ])

    helpers.tear_down()

    assert expected_js == acutal_js
Beispiel #17
0
def test_make_dirs():
    """Test convert.make_dirs"""

    helpers.setup()

    out_dir = helpers.TEST_PATH

    expected_dirs = set(
        list(
            map(
                lambda f: os.path.join(out_dir, f),
                ["0/0", "1/0", "1/1", "2/0", "2/1", "2/2", "2/3"],
            )))

    convert.make_dirs(out_dir, 0, 2)

    dirs_exists = all(map(os.path.exists, expected_dirs))

    helpers.tear_down()

    assert dirs_exists
Beispiel #18
0
def test_files_to_map_fails_file_not_found():
    """Integration test for making files into map"""
    helpers.disbale_tqdm()
    helpers.setup(with_data=True)

    with_path = lambda f: os.path.join(helpers.TEST_PATH, f)
    out_dir = with_path("test_web")

    files = [
        with_path("test_tiling_image.jpg"),
        with_path("test_catalog_radec.cat"),
        with_path("does_not_exist.txt"),
    ]

    with pytest.raises(FileNotFoundError):
        convert.files_to_map(files,
                             out_dir=out_dir,
                             cat_wcs_fits_file=with_path("test_image.fits"))

    helpers.tear_down()
    helpers.enable_tqdm()
Beispiel #19
0
def test_build_conditional_js():
    """test cartographer.build_conditional_js"""

    helpers.setup()

    acutal_js = c.build_conditional_js(helpers.TEST_PATH, True)

    expected_js = "\n".join([
        "    <script defer src='https://cdnjs.cloudflare.com/ajax/libs/leaflet-search/3.0.2/leaflet-search.src.min.js'></script>",
        "    <script defer src='js/customSearch.min.js'></script>",
        "    <script defer src='js/tiledMarkers.min.js'></script>",
        "    <script defer src='js/urlCoords.js'></script>",
        "    <script defer src='js/index.js'></script>",
        "    <script defer src='https://unpkg.com/[email protected]/dist/cbor.js'></script>",
        "    <script defer src='https://unpkg.com/[email protected]/dist/pbf.js'></script>",
        "    <script defer src='js/l.ellipse.min.js'></script>",
        "    <script defer src='js/vector-tile.min.js'></script>",
    ])

    helpers.tear_down()

    assert expected_js == acutal_js