Skip to content

Commit

Permalink
Action update.
Browse files Browse the repository at this point in the history
  • Loading branch information
iwatkot committed Jul 20, 2024
1 parent af84a7a commit cab1a92
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 12 deletions.
19 changes: 17 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install mypy pylint opencv-python osmnx rasterio python_dotenv tqdm types-tqdm pandas-stubs types-requests
pip install mypy pylint opencv-python osmnx rasterio python_dotenv tqdm types-tqdm pandas-stubs types-requests pytest pytest-cov
- name: Run mypy to generate cache
run: mypy maps4fs || true
Expand All @@ -32,4 +32,19 @@ jobs:
run: mypy maps4fs

- name: Run pylint
run: pylint maps4fs
run: pylint maps4fs

- name: Run pytest with coverage
run: pytest --cov=maps4fs --cov-report xml

- name: Download Code Climate test-reporter
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
- name: Upload coverage data to Code Climate
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
run: |
./cc-test-reporter after-build --exit-code 0 -t coverage.py --prefix /home/runner/work/maps4fs/maps4fs/
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ previews/
maps4fs.zip
.DS_Store
maps/
.mypy_cache/
.mypy_cache/
.pytest_cache/
htmlcov/
tests/data/
10 changes: 4 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,16 @@
}
},
{
"name": "Release Script",
"type": "python",
"name": "Current File",
"type": "debugpy",
"request": "launch",
"program": "release.py",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"env": {
"PYTHONPATH": "${workspaceFolder};${PYTHONPATH}",
"LOG_LEVEL": "DEBUG",
"PYTHONPATH": "${workspaceFolder}:${PYTHONPATH}"
}
},

{
"name": "Linux / Mac Bot",
"type": "python",
Expand Down
7 changes: 7 additions & 0 deletions maps4fs/generator/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def __init__(
"it may not work properly in Giants Editor."
)

# Blur seed should be positive and odd.
if blur_seed <= 0:
raise ValueError("Blur seed should be positive.")
if blur_seed % 2 == 0:
blur_seed += 1

self._add_components(blur_seed, max_height)

def _add_components(self, blur_seed: int, max_height: int) -> None:
Expand Down Expand Up @@ -87,6 +93,7 @@ def generate(self) -> None:
component.__class__.__name__,
e,
)
raise e
pbar.update(1)

def previews(self) -> list[str]:
Expand Down
6 changes: 3 additions & 3 deletions maps4fs/generator/texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ def _read_parameters(self) -> None:
self.minimum_y = min(south, north)
self.maximum_x = max(west, east)
self.maximum_y = max(south, north)
self.logger.degug("Map minimum coordinates (XxY): %s x %s.", self.minimum_x, self.minimum_y)
self.logger.degug("Map maximum coordinates (XxY): %s x %s.", self.maximum_x, self.maximum_y)
self.logger.debug("Map minimum coordinates (XxY): %s x %s.", self.minimum_x, self.minimum_y)
self.logger.debug("Map maximum coordinates (XxY): %s x %s.", self.maximum_x, self.maximum_y)

self.height = abs(north - south)
self.width = abs(east - west)
Expand Down Expand Up @@ -317,7 +317,7 @@ def get_relative_y(self, y: float) -> int:
raw_y = y - self.minimum_y
return self.height - int(raw_y * self.width_coef)

def _to_np(self, geometry: shapely.geometry.polygon.Polygon) -> np.ndarray:
def _to_np(self, geometry: shapely.geometry.polygon.Polygon, *args) -> np.ndarray:
"""Converts Polygon geometry to numpy array of polygon points.
Args:
Expand Down
Empty file added tests/__init__.py
Empty file.
174 changes: 174 additions & 0 deletions tests/test_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import os
import shutil
from random import choice, randint
from time import time

import cv2

from maps4fs import Map
from maps4fs.generator.texture import TEXTURES

working_directory = os.getcwd()
map_template = os.path.join(working_directory, "data/map-template.zip")
if not os.path.isfile(map_template):
raise FileNotFoundError(f"Map template not found at {map_template}")
base_directory = os.path.join(working_directory, "tests/data")
if os.path.isdir(base_directory):
shutil.rmtree(base_directory)

coordinates_cases = [
(45.369648574398184, 19.801106980618925),
(39.51147544442993, -115.96064194571787),
(-13.038004302866275, -57.09179831840436),
(-15.912277514425883, 25.9557832265989),
(-35.95760563718185, 149.1495358824173),
(58.52085065306593, 31.27771396353221),
(35.25541295723034, 139.04857855524995),
]


def random_distance() -> int:
"""Return random distance.
Returns:
int: Random distance.
"""
distances_cases = [1024, 2048, 4096, 8192]
return choice(distances_cases[:2]) # Larger maps are too slow for automated tests.


def random_blur_seed() -> int:
"""Return random blur seed.
Returns:
int: Random blur seed.
"""
return randint(1, 30)


def random_max_height() -> int:
"""Return random max height.
Returns:
int: Random max height.
"""
return randint(100, 3000)


def map_directory() -> str:
"""Creates a new map directory and returns its path.
Returns:
str: Path to the new map directory.
"""
timestamp = int(time())
directory = os.path.join(base_directory, f"map_{timestamp}")
os.makedirs(directory, exist_ok=True)
return directory


def test_map():
"""Test Map generation for different cases."""
for coordinates in coordinates_cases:
distance = random_distance()
blur_seed = random_blur_seed()
max_height = random_max_height()
directory = map_directory()

map = Map(
coordinates=coordinates,
distance=distance,
map_directory=directory,
blur_seed=blur_seed,
max_height=max_height,
map_template=map_template,
)

map.generate()

textures_directory = os.path.join(directory, "maps/map/data")
for texture_name, numer_of_layers in TEXTURES.items():
if numer_of_layers == 0:
continue
for idx in range(1, numer_of_layers + 1):
texture_path = os.path.join(
textures_directory, f"{texture_name}{str(idx).zfill(2)}_weight.png"
)
assert os.path.isfile(texture_path), f"Texture not found: {texture_path}"
img = cv2.imread(texture_path)
assert img is not None, f"Texture could not be read: {texture_path}"
assert img.shape == (
distance * 2,
distance * 2,
3,
), f"Texture shape mismatch: {img.shape} != {(distance * 2, distance * 2, 3)}"
assert img.dtype == "uint8", f"Texture dtype mismatch: {img.dtype} != uint8"

dem_file = os.path.join(textures_directory, "map_dem.png")
assert os.path.isfile(dem_file), f"DEM file not found: {dem_file}"
img = cv2.imread(dem_file, cv2.IMREAD_UNCHANGED)
assert img is not None, f"DEM could not be read: {dem_file}"
assert img.shape == (
distance + 1,
distance + 1,
), f"DEM shape mismatch: {img.shape} != {(distance + 1, distance + 1)}"
assert img.dtype == "uint16", f"DEM dtype mismatch: {img.dtype} != uint16"


def test_map_preview():
"""Test Map preview generation."""
case = choice(coordinates_cases)
distance = random_distance()
blur_seed = random_blur_seed()
max_height = random_max_height()

directory = map_directory()
map = Map(
coordinates=case,
distance=distance,
map_directory=directory,
blur_seed=blur_seed,
max_height=max_height,
map_template=map_template,
)
map.generate()
previews_paths = map.previews()
for preview_path in previews_paths:
assert os.path.isfile(preview_path), f"Preview not found: {preview_path}"
img = cv2.imread(preview_path)
assert img is not None, f"Preview could not be read: {preview_path}"
assert img.shape == (
2048,
2048,
3,
), f"Preview shape mismatch: {img.shape} != (2048, 2048, 3)"
assert img.dtype == "uint8", f"Preview dtype mismatch: {img.dtype} != uint8"


def test_map_pack():
"""Test Map packing into zip archive."""
case = choice(coordinates_cases)
distance = random_distance()
blur_seed = random_blur_seed()
max_height = random_max_height()

directory = map_directory()
map = Map(
coordinates=case,
distance=distance,
map_directory=directory,
blur_seed=blur_seed,
max_height=max_height,
map_template=map_template,
)
map.generate()
archive_name = os.path.join(base_directory, "archive")
archive_path = map.pack(archive_name)
assert os.path.isfile(archive_path), f"Archive not found: {archive_path}"
unpacked_directory = os.path.join(base_directory, "unpacked")
os.makedirs(unpacked_directory, exist_ok=True)
try:
shutil.unpack_archive(archive_path, unpacked_directory)
except Exception as e:
assert False, f"Archive could not be unpacked: {e}"
assert os.path.isdir(unpacked_directory), f"Unpacked directory not found: {unpacked_directory}"

0 comments on commit cab1a92

Please sign in to comment.