Beispiel #1
0
def _plates_simulation(name,
                       width,
                       height,
                       seed,
                       temps=[.874, .765, .594, .439, .366, .124],
                       humids=[.941, .778, .507, .236, 0.073, .014, .002],
                       gamma_curve=1.25,
                       curve_offset=.2,
                       num_plates=10,
                       ocean_level=1.0,
                       step=Step.full(),
                       verbose=get_verbose()):
    e_as_array, p_as_array = generate_plates_simulation(seed,
                                                        width,
                                                        height,
                                                        num_plates=num_plates,
                                                        verbose=verbose)

    world = World(name, Size(width, height), seed,
                  GenerationParameters(num_plates, ocean_level, step), temps,
                  humids, gamma_curve, curve_offset)
    world.elevation = (numpy.array(e_as_array).reshape(height, width), None)
    world.plates = numpy.array(p_as_array,
                               dtype=numpy.uint16).reshape(height, width)
    return world
Beispiel #2
0
    def test_watermap_rng_stabilty(self):
        seed = 12345
        numpy.random.seed(seed)

        size = Size(16, 8)

        ocean = numpy.fromfunction(lambda y, x: y == x,
                                   (size.height, size.width))

        percipitation = numpy.ones((size.height, size.width))

        elevation = numpy.fromfunction(lambda y, x: y * x,
                                       (size.height, size.width))

        t = numpy.zeros(5)

        w = World("watermap", size, seed, GenerationParameters(0, 1.0, 0))
        w.ocean = ocean
        w.precipitation = (percipitation, t)
        w.elevation = (elevation, t)

        d = numpy.random.randint(0, 100)
        self.assertEqual(d, 98)

        data, t = WatermapSimulation._watermap(w, 200)

        self.assertAlmostEqual(data[4, 4], 0.0)
        self.assertAlmostEqual(data[3, 5], 4.20750776)

        d = numpy.random.randint(0, 100)
        self.assertEqual(d, 59)
    def test_watermap_rng_stabilty(self):
        seed=12345
        numpy.random.seed(seed)

        size = Size(16,8)

        ocean = numpy.fromfunction(lambda y, x: y==x, (size.height, size.width))

        percipitation = numpy.ones((size.height, size.width))

        elevation = numpy.fromfunction(lambda y, x: y*x, (size.height, size.width))

        t = numpy.zeros(5)

        w = World("watermap",  size, seed, GenerationParameters(0, 1.0, 0))
        w.ocean = ocean
        w.precipitation = (percipitation, t)
        w.elevation = (elevation, t)

        d = numpy.random.randint(0,100)
        self.assertEqual(d, 98)

        data, t = WatermapSimulation._watermap(w, 200)

        self.assertAlmostEqual(data[4,4], 0.0)
        self.assertAlmostEqual(data[3,5], 4.20750776)

        d = numpy.random.randint(0,100)
        self.assertEqual(d, 59)
Beispiel #4
0
def generate_plates(seed, world_name, output_dir, width, height,
                    num_plates=10):
    """
    Eventually this method should be invoked when generation is called at
    asked to stop at step "plates", it should not be a different operation
    :param seed:
    :param world_name:
    :param output_dir:
    :param width:
    :param height:
    :param num_plates:
    :return:
    """
    elevation, plates = generate_plates_simulation(seed, width, height,
                                                   num_plates=num_plates)

    world = World(world_name, Size(width, height), seed,
                  GenerationParameters(num_plates, -1.0, "plates"))
    world.elevation = (numpy.array(elevation).reshape(height, width), None)
    world.plates = numpy.array(plates, dtype=numpy.uint16).reshape(height, width)

    # Generate images
    filename = '%s/plates_%s.png' % (output_dir, world_name)
    draw_simple_elevation_on_file(world, filename, None)
    print("+ plates image generated in '%s'" % filename)
    geo.center_land(world)
    filename = '%s/centered_plates_%s.png' % (output_dir, world_name)
    draw_simple_elevation_on_file(world, filename, None)
    print("+ centered plates image generated in '%s'" % filename)
Beispiel #5
0
def _plates_simulation(name, width, height, seed, temps=
                       [.874, .765, .594, .439, .366, .124], humids=
                       [.941, .778, .507, .236, 0.073, .014, .002], gamma_curve=1.25,
                       curve_offset=.2, num_plates=10, ocean_level=1.0,
                       step=Step.full(), verbose=get_verbose()):
    e_as_array, p_as_array = generate_plates_simulation(seed, width, height,
                                                        num_plates=num_plates,
                                                        verbose=verbose)

    world = World(name, Size(width, height), seed,
                  GenerationParameters(num_plates, ocean_level, step),
                  temps, humids, gamma_curve, curve_offset)
    world.elevation = (numpy.array(e_as_array).reshape(height, width), None)
    world.plates = numpy.array(p_as_array, dtype=numpy.uint16).reshape(height, width)
    return world
Beispiel #6
0
    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)

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

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

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

        w.elevation = (elevation, t)
        w.ocean = ocean

        desired_result = numpy.asarray([0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, \
                                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.7, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7, 0.9, \
                                0.9, 0.7, 0.5, 0.3, 0.3, 0.3, 0.3, 0.3, 0.5, 0.7, 0.9, \
                                0.9, 0.7, 0.5, 0.3, 0.0, 0.0, 0.0, 0.3, 0.5, 0.7, 0.9, \
                                0.9, 0.7, 0.5, 0.3, 0.0, -1.0, 0.0, 0.3, 0.5, 0.7, 0.9, \
                                0.9, 0.7, 0.5, 0.3, 0.0, 0.0, 0.0, 0.3, 0.5, 0.7, 0.9, \
                                0.9, 0.7, 0.5, 0.3, 0.3, 0.3, 0.3, 0.3, 0.5, 0.7, 0.9, \
                                0.9, 0.7, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7, 0.9, \
                                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])

        # this part is verbatim from the function. It's not part of the test
        # Some refactoring is in order to increase test quality
        desired_result = anti_alias(desired_result, 10)

        min_depth = desired_result.min()
        max_depth = desired_result.max()
        desired_result = (desired_result - min_depth) / (max_depth - min_depth)

        # end of verbatim part

        result = sea_depth(w, ocean_level)

        for y in range(extent):
            for x in range(extent):
                self.assertAlmostEqual(desired_result[y, x], result[y, x])
    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)

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

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

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

        w.elevation = (elevation, t)
        w.ocean = ocean

        desired_result = numpy.asarray([0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, \
                                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.7, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7, 0.9, \
                                0.9, 0.7, 0.5, 0.3, 0.3, 0.3, 0.3, 0.3, 0.5, 0.7, 0.9, \
                                0.9, 0.7, 0.5, 0.3, 0.0, 0.0, 0.0, 0.3, 0.5, 0.7, 0.9, \
                                0.9, 0.7, 0.5, 0.3, 0.0, -1.0, 0.0, 0.3, 0.5, 0.7, 0.9, \
                                0.9, 0.7, 0.5, 0.3, 0.0, 0.0, 0.0, 0.3, 0.5, 0.7, 0.9, \
                                0.9, 0.7, 0.5, 0.3, 0.3, 0.3, 0.3, 0.3, 0.5, 0.7, 0.9, \
                                0.9, 0.7, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7, 0.9, \
                                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])

        # this part is verbatim from the function. It's not part of the test
        # Some refactoring is in order to increase test quality
        desired_result = anti_alias(desired_result, 10)

        min_depth = desired_result.min()
        max_depth = desired_result.max()
        desired_result = (desired_result - min_depth) / (max_depth - min_depth)

        # end of verbatim part

        result = sea_depth(w, ocean_level)

        for y in range(extent):
            for x in range(extent):
                self.assertAlmostEqual(desired_result[y,x], result[y,x])
Beispiel #8
0
def generate_plates(seed,
                    world_name,
                    output_dir,
                    width,
                    height,
                    num_plates=10):
    """
    Eventually this method should be invoked when generation is called at
    asked to stop at step "plates", it should not be a different operation
    :param seed:
    :param world_name:
    :param output_dir:
    :param width:
    :param height:
    :param num_plates:
    :return:
    """
    elevation, plates = generate_plates_simulation(seed,
                                                   width,
                                                   height,
                                                   num_plates=num_plates)

    world = World(world_name, Size(width, height), seed,
                  GenerationParameters(num_plates, -1.0, "plates"))
    world.elevation = (numpy.array(elevation).reshape(height, width), None)
    world.plates = numpy.array(plates,
                               dtype=numpy.uint16).reshape(height, width)

    # Generate images
    filename = '%s/plates_%s.png' % (output_dir, world_name)
    draw_simple_elevation_on_file(world, filename, None)
    print("+ plates image generated in '%s'" % filename)
    geo.center_land(world)
    filename = '%s/centered_plates_%s.png' % (output_dir, world_name)
    draw_simple_elevation_on_file(world, filename, None)
    print("+ centered plates image generated in '%s'" % filename)
def load_world_to_hdf5(filename):
    f = h5py.File(filename, libver='latest', mode='r')

    w = World(f['general/name'].value,
              Size(f['general/width'].value, f['general/height'].value),
              f['generation_params/seed'].value,
              GenerationParameters(f['generation_params/n_plates'].value,
                                   f['generation_params/ocean_level'].value,
                                   Step.get_by_name(f['generation_params/step'].value)))

    # Elevation
    e = numpy.array(f['elevation/data'])
    e_th = [('sea', f['elevation/thresholds/sea'].value),
            ('plain', f['elevation/thresholds/plain'].value),
            ('hill', f['elevation/thresholds/hill'].value),
            ('mountain', None)]
    w.elevation = (e, e_th)

    # Plates
    w.plates = numpy.array(f['plates'])

    # Ocean
    w.ocean = numpy.array(f['ocean'])
    w.sea_depth = numpy.array(f['sea_depth'])

    # Biome
    if 'biome' in f.keys():
        biome_data = []
        for y in range(w.height):
            row = []
            for x in range(w.width):
                value = f['biome'][y, x]
                row.append(biome_index_to_name(value))
            biome_data.append(row)
        biome = numpy.array(biome_data, dtype=object)
        w.biome = biome

    if 'humidity' in f.keys():
        data, quantiles = _from_hdf5_matrix_with_quantiles(f['humidity'])
        w.humidity = (data, quantiles)

    if 'irrigation' in f.keys():
        w.irrigation = numpy.array(f['irrigation'])

    if 'permeability' in f.keys():
        p = numpy.array(f['permeability/data'])
        p_th = [
            ('low', f['permeability/thresholds/low'].value),
            ('med', f['permeability/thresholds/med'].value),
            ('hig', None)
        ]
        w.permeability = (p, p_th)

    if 'watermap' in f.keys():
        data = numpy.array(f['watermap/data'])
        thresholds = {}
        thresholds['creek'] = f['watermap/thresholds/creek'].value
        thresholds['river'] =  f['watermap/thresholds/river'].value
        thresholds['main river'] = f['watermap/thresholds/mainriver'].value
        w.watermap = (data, thresholds)

    if 'precipitation' in f.keys():
        p = numpy.array(f['precipitation/data'])
        p_th = [
            ('low', f['precipitation/thresholds/low'].value),
            ('med', f['precipitation/thresholds/med'].value),
            ('hig', None)
        ]
        w.precipitation = (p, p_th)

    if 'temperature' in f.keys():
        t = numpy.array(f['temperature/data'])
        t_th = [
            ('polar', f['temperature/thresholds/polar'].value),
            ('alpine', f['temperature/thresholds/alpine'].value),
            ('boreal', f['temperature/thresholds/boreal'].value),
            ('cool', f['temperature/thresholds/cool'].value),
            ('warm', f['temperature/thresholds/warm'].value),
            ('subtropical', f['temperature/thresholds/subtropical'].value),
            ('tropical', None)
        ]
        w.temperature = (t, t_th)

    if 'icecap' in f.keys():
        w.icecap = numpy.array(f['icecap'])

    if 'lake_map' in f.keys():
        w.lakemap = numpy.array(f['lake_map'])

    if 'river_map' in f.keys():
        w.rivermap = numpy.array(f['river_map'])

    f.close()

    return w
Beispiel #10
0
def load_world_to_hdf5(filename):
    f = h5py.File(filename, libver='latest', mode='r')

    w = World(
        f['general/name'].value,
        Size(f['general/width'].value, f['general/height'].value),
        f['generation_params/seed'].value,
        GenerationParameters(
            f['generation_params/n_plates'].value,
            f['generation_params/ocean_level'].value,
            Step.get_by_name(f['generation_params/step'].value)))

    # Elevation
    e = numpy.array(f['elevation/data'])
    e_th = [('sea', f['elevation/thresholds/sea'].value),
            ('plain', f['elevation/thresholds/plain'].value),
            ('hill', f['elevation/thresholds/hill'].value), ('mountain', None)]
    w.elevation = (e, e_th)

    # Plates
    w.plates = numpy.array(f['plates'])

    # Ocean
    w.ocean = numpy.array(f['ocean'])
    w.sea_depth = numpy.array(f['sea_depth'])

    # Biome
    if 'biome' in f.keys():
        biome_data = []
        for y in range(w.height):
            row = []
            for x in range(w.width):
                value = f['biome'][y, x]
                row.append(biome_index_to_name(value))
            biome_data.append(row)
        biome = numpy.array(biome_data, dtype=object)
        w.biome = biome

    if 'humidity' in f.keys():
        data, quantiles = _from_hdf5_matrix_with_quantiles(f['humidity'])
        w.humidity = (data, quantiles)

    if 'irrigation' in f.keys():
        w.irrigation = numpy.array(f['irrigation'])

    if 'permeability' in f.keys():
        p = numpy.array(f['permeability/data'])
        p_th = [('low', f['permeability/thresholds/low'].value),
                ('med', f['permeability/thresholds/med'].value), ('hig', None)]
        w.permeability = (p, p_th)

    if 'watermap' in f.keys():
        data = numpy.array(f['watermap/data'])
        thresholds = {}
        thresholds['creek'] = f['watermap/thresholds/creek'].value
        thresholds['river'] = f['watermap/thresholds/river'].value
        thresholds['main river'] = f['watermap/thresholds/mainriver'].value
        w.watermap = (data, thresholds)

    if 'precipitation' in f.keys():
        p = numpy.array(f['precipitation/data'])
        p_th = [('low', f['precipitation/thresholds/low'].value),
                ('med', f['precipitation/thresholds/med'].value),
                ('hig', None)]
        w.precipitation = (p, p_th)

    if 'temperature' in f.keys():
        t = numpy.array(f['temperature/data'])
        t_th = [('polar', f['temperature/thresholds/polar'].value),
                ('alpine', f['temperature/thresholds/alpine'].value),
                ('boreal', f['temperature/thresholds/boreal'].value),
                ('cool', f['temperature/thresholds/cool'].value),
                ('warm', f['temperature/thresholds/warm'].value),
                ('subtropical', f['temperature/thresholds/subtropical'].value),
                ('tropical', None)]
        w.temperature = (t, t_th)

    if 'icecap' in f.keys():
        w.icecap = numpy.array(f['icecap'])

    if 'lake_map' in f.keys():
        w.lakemap = numpy.array(f['lake_map'])

    if 'river_map' in f.keys():
        w.rivermap = numpy.array(f['river_map'])

    f.close()

    return w