Skip to content

Commit

Permalink
Linting (#255)
Browse files Browse the repository at this point in the history
* draw channels individually

* use common.anti_alias instead of local implementation

* More whitespace

* still more whitespace

* changelog reflects speedup in ancient map

* massive refactoring of draw_ancientmap

* make cool temperate forests use new _draw_biome

* whitespace

* linting

* linting

---------

Co-authored-by: Bret Curtis <[email protected]>
  • Loading branch information
MM1nd and psi29a committed Aug 31, 2023
1 parent 9baa797 commit 000a2ac
Show file tree
Hide file tree
Showing 34 changed files with 298 additions and 255 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ generator.spec
/.vs/worldengine/v14/.suo
/worldengine.sln
*.pyperf
*.pylintrc
2 changes: 2 additions & 0 deletions manual/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# pylint: skip-file

# -*- coding: utf-8 -*-
#
# WorldEngine documentation build configuration file, created by
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from setuptools import setup, Extension, Command
from setuptools import setup
# from pip.req import parse_requirements
# import pip.download

Expand Down
14 changes: 9 additions & 5 deletions tests/astar_test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
"""Tests for the A* Implementation."""
import unittest
import numpy
from worldengine import astar
from worldengine.common import _equal


class TestCommon(unittest.TestCase):
"""Common container for tests for A*."""

def test_traversal(self):
"""Test that A* actually returns a shortest path for a very simple maze"""
test_map = numpy.zeros((20, 20))
line = numpy.array(20)
line.fill(1.0)
test_map[10, :] = line
test_map[10, 18] = 0.0
path_data = numpy.array([[0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8], [0, 9],
[1, 9], [2, 9], [3, 9], [4, 9], [5, 9], [6, 9], [7, 9], [8, 9], [9, 9],
[10, 9], [11, 9], [12, 9], [13, 9], [14, 9], [15, 9], [16, 9], [17, 9],
[18, 9], [18, 10], [18, 11], [18, 12], [18, 13], [18, 14], [18, 15],
[18, 16], [18, 17], [18, 18], [18, 19], [19, 19]])
path_data = numpy.array(
[[0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8], [0, 9],
[1, 9], [2, 9], [3, 9], [4, 9], [5, 9], [6, 9], [7, 9], [8, 9], [9, 9],
[10, 9], [11, 9], [12, 9], [13, 9], [14, 9], [15, 9], [16, 9], [17, 9],
[18, 9], [18, 10], [18, 11], [18, 12], [18, 13], [18, 14], [18, 15],
[18, 16], [18, 17], [18, 18], [18, 19], [19, 19]])
shortest_path = astar.PathFinder().find(test_map, [0, 0], [19, 19])
self.assertTrue(_equal(path_data, numpy.array(shortest_path)))

Expand Down
1 change: 0 additions & 1 deletion tests/basic_map_operations_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import numpy
import unittest
from worldengine.basic_map_operations import distance, index_of_nearest

Expand Down
2 changes: 1 addition & 1 deletion tests/biome_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def setUp(self):
tests_dir = os.path.dirname(os.path.realpath(__file__))
self.tests_data_dir = os.path.abspath(os.path.join(
tests_dir, "../../worldengine-data/tests/data"))
pass

def test_biome_by_name(self):
self.assertRaises(Exception, Biome.by_name, "unexisting biome")
Expand Down Expand Up @@ -123,6 +122,7 @@ def test_biome_index_to_name(self):

def test_locate_biomes(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
#TODO: do something to the result
cm, biome_cm = BiomeSimulation().execute(w, 28070)

@staticmethod
Expand Down
14 changes: 9 additions & 5 deletions tests/blessed_images/generated_blessed_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ def main(blessed_images_dir, tests_data_dir):
draw_biome_on_file(w, "%s/biome_28070.png" % blessed_images_dir)
draw_scatter_plot_on_file(w, "%s/scatter_28070.png" % blessed_images_dir)
draw_satellite_on_file(w, "%s/satellite_28070.png" % blessed_images_dir)
draw_ancientmap_on_file(w, "%s/ancientmap_28070_factor3.png" % blessed_images_dir, resize_factor=3)
draw_ancientmap_on_file(
w, "%s/ancientmap_28070_factor3.png" % blessed_images_dir, resize_factor=3)

img = PNGWriter.rgba_from_dimensions(w.width * 2, w.height * 2, "%s/rivers_28070_factor2.png" % blessed_images_dir)
img = PNGWriter.rgba_from_dimensions(
w.width * 2, w.height * 2, "%s/rivers_28070_factor2.png" % blessed_images_dir)
draw_rivers_on_image(w, img, factor=2)
img.complete()

if __name__ == '__main__':
blessed_images_dir = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../../worldengine-data/tests/images"))
tests_data_dir = os.path.abspath(os.path.join(blessed_images_dir, '../data'))
main(blessed_images_dir, tests_data_dir)
BLESSED_IMAGES_DIR = os.path.abspath(
os.path.join(
os.path.dirname(os.path.realpath(__file__)), "../../../worldengine-data/tests/images"))
TESTS_DATA_DIR = os.path.abspath(os.path.join(BLESSED_IMAGES_DIR, '../data'))
main(BLESSED_IMAGES_DIR, TESTS_DATA_DIR)
32 changes: 16 additions & 16 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys
from tests.draw_test import TestBase
import unittest
from tests.draw_test import TestBase
from worldengine import __main__
from worldengine.cli.main import main

Expand Down Expand Up @@ -74,23 +74,23 @@ def test_smoke_full(self):
raise e

def test_smoke_ancient(self):
backup_argv = sys.argv
sys.argv = ["python", "ancient_map", "-w", self.world]
try:
main()
except Exception as e:
raise e
sys.argv = backup_argv
backup_argv = sys.argv
sys.argv = ["python", "ancient_map", "-w", self.world]
try:
main()
except Exception as e:
raise e
sys.argv = backup_argv

def test_smoke_plates(self):
backup_argv = sys.argv
sys.argv = ["python", "plates", "--width", "16",
"--height", "16", "--number-of-plates", "2"]
try:
main()
except Exception as e:
raise e
sys.argv = backup_argv
backup_argv = sys.argv
sys.argv = ["python", "plates", "--width", "16",
"--height", "16", "--number-of-plates", "2"]
try:
main()
except Exception as e:
raise e
sys.argv = backup_argv


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions tests/common_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def test_dictionary_equality(self):

a['foo'] = 'bar'

self.assertFalse(_equal(a,b))
self.assertFalse(_equal(a, b))

b['foo'] = 'bar'

self.assertTrue(_equal(a,b))
self.assertTrue(_equal(a, b))

if __name__ == '__main__':
unittest.main()
8 changes: 4 additions & 4 deletions tests/data/data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import os
import numpy
from worldengine.plates import world_gen, _plates_simulation
from worldengine.plates import world_gen


def main(tests_data_dir):
Expand All @@ -19,6 +19,6 @@ def main(tests_data_dir):


if __name__ == '__main__':
blessed_images_dir = os.path.dirname(os.path.realpath(__file__))
tests_data_dir = os.path.abspath(os.path.join(blessed_images_dir, '../data'))
main(tests_data_dir)
BLESSED_IMAGES_DIR = os.path.dirname(os.path.realpath(__file__))
TESTS_DATA_DIR = os.path.abspath(os.path.join(BLESSED_IMAGES_DIR, '../data'))
main(TESTS_DATA_DIR)
29 changes: 16 additions & 13 deletions tests/draw_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,45 @@ def _assert_img_equal(self, blessed_image_name, drawn_image):
"and/or amount of channels. Blessed %s, drawn %s"
% (str(blessed_img.array.shape), str(drawn_image.array.shape)))

# compare images; cmp_array will be an array of booleans in case of equal shapes (and a pure boolean otherwise)
# compare images;
# cmp_array will be an array of booleans in case of equal shapes
# and a pure boolean otherwise
cmp_array = blessed_img.array != drawn_image.array

# avoid calling assertTrue if shapes differed; results would be weird (and meaningless)
if numpy.any(cmp_array):
diff = numpy.transpose(numpy.nonzero(cmp_array)) # list of tuples of differing indices
self.assertTrue(False,
"Pixels at %i, %i are different. Blessed %s, drawn %s"
% (diff[0][0], diff[0][1],
blessed_img.array[diff[0][0], diff[0][1]],
drawn_image.array[diff[0][0], diff[0][1]]))
self.assertTrue(
False,
"Pixels at %i, %i are different. Blessed %s, drawn %s" % (
diff[0][0], diff[0][1],
blessed_img.array[diff[0][0], diff[0][1]],
drawn_image.array[diff[0][0], diff[0][1]]))


class TestDraw(TestBase):

def setUp(self):
super(TestDraw, self).setUp()

def test_biome_colors(self):
self.assertEqual(Biome.all_names(), sorted(_biome_colors.keys()))

def test_elevation_color(self):
for i in range(0, 20):
v = i / 2.0
c = ra, ga, ba = elevation_color(v)
c = elevation_color(v)
delta = 0.0000001
c_low = rb, gb, bb = elevation_color(v - delta)
c_high = rc, gc, bc = elevation_color(v + delta)
c_low = elevation_color(v - delta)
c_high = elevation_color(v + delta)

# we want values to be in range
self._assert_is_valid_color(c, "color for %f" % v)
self._assert_is_valid_color(c_low, "color for %f (low)" % (v - delta))
self._assert_is_valid_color(c_high, "color for %f (high)" % (v + delta))

# we look for discontinuities
# TODO verify this
# TODO: verify this; probably move it to a seperate test
# ra, ga, ba = c
# rb, gb, bb = c_low
# rc, gc, bc = c_high
#self.assertAlmostEqual(ra, rb, 5, "value %f, red, low, from %f to %f" % (v, ra, rb))
#self.assertAlmostEqual(ra, rc, 5, "value %f, red, high, from %f to %f" % (v, ra, rc))
#self.assertAlmostEqual(ga, gb, 5, "value %f, green, low, from %f to %f" % (v, ga, gb))
Expand Down
30 changes: 11 additions & 19 deletions tests/generation_test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import unittest

import numpy

from worldengine.plates import Step, center_land, world_gen
from worldengine.model.world import World, Size, GenerationParameters
from tests.draw_test import TestBase

from worldengine.plates import Step, center_land, world_gen
from worldengine.model.world import World, Size, GenerationParameters
from worldengine.generation import sea_depth
from worldengine.common import anti_alias


class TestGeneration(TestBase):

def setUp(self):
super(TestGeneration, self).setUp()

def test_world_gen_does_not_explode_badly(self):
# FIXME remove me when proper tests are in place
# FIXME: remove me when proper tests are in place
# Very stupid test that just verify nothing explode badly
world_gen("Dummy", 32, 16, 1, step=Step.get_by_name("full"))

Expand Down Expand Up @@ -45,13 +41,14 @@ def test_center_land(self):
def test_sea_depth(self):
ocean_level = 1.0
extent = 11
w = World("sea_depth", Size(extent,extent), 0, GenerationParameters(0, ocean_level, 0), None)
w = World(
"sea_depth", Size(extent, extent), 0, GenerationParameters(0, ocean_level, 0), None)

ocean = numpy.full([extent,extent], True)
ocean[5,5]=False
ocean = numpy.full([extent, extent], True)
ocean[5, 5] = False

elevation = numpy.zeros([extent,extent], float)
elevation[5,5] = 2.0
elevation = numpy.zeros([extent, extent], float)
elevation[5, 5] = 2.0

t = numpy.zeros([extent, extent])

Expand All @@ -70,7 +67,7 @@ def test_sea_depth(self):
0.9, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.9, \
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9])

desired_result = desired_result.reshape([extent,extent])
desired_result = desired_result.reshape([extent, extent])

# this part is verbatim from the function. It's not part of the test
# Some refactoring is in order to increase test quality
Expand All @@ -86,12 +83,7 @@ def test_sea_depth(self):

for y in range(extent):
for x in range(extent):
self.assertAlmostEqual(desired_result[y,x], result[y,x])





self.assertAlmostEqual(desired_result[y, x], result[y, x])


if __name__ == '__main__':
Expand Down
6 changes: 4 additions & 2 deletions tests/serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ def test_hdf5_serialize_unserialize(self):
f = tempfile.NamedTemporaryFile(delete=False)
f.close()
filename = f.name
serialized = save_world_to_hdf5(w, filename)
save_world_to_hdf5(w, filename)
unserialized = load_world_to_hdf5(filename)
self.assertEqual(set(w.layers.keys()), set(unserialized.layers.keys()))
self.assertEqual(w.layers['humidity'].quantiles, unserialized.layers['humidity'].quantiles)
self.assertEqual(
w.layers['humidity'].quantiles,
unserialized.layers['humidity'].quantiles)
for l in w.layers.keys():
self.assertEqual(w.layers[l], unserialized.layers[l], "Comparing %s" % l)
self.assertTrue(_equal(w.ocean_level, unserialized.ocean_level))
Expand Down
Loading

0 comments on commit 000a2ac

Please sign in to comment.