From c9c7f26910718f7af0015239c39ca4d73d60bd1e Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Wed, 22 May 2019 08:29:42 +0430 Subject: [PATCH] feat(random_map_generator): generate maps randomly --- biomes/constants.py | 13 +++++++------ biomes/draw.py | 4 ++-- biomes/map_generator.py | 23 +++++++++------------- biomes/mars.py | 35 ++++++++++++++++++++++++++++++++++ biomes/predict.py | 11 ++++++----- biomes/random_map_generator.py | 12 ++++++++++++ 6 files changed, 71 insertions(+), 27 deletions(-) create mode 100644 biomes/mars.py create mode 100644 biomes/random_map_generator.py diff --git a/biomes/constants.py b/biomes/constants.py index 24f6e8e..ef5aa36 100644 --- a/biomes/constants.py +++ b/biomes/constants.py @@ -1,12 +1,13 @@ import os +from pathlib import Path -directory = os.path.dirname(os.path.abspath(__file__)) +directory = Path(os.path.dirname(os.path.abspath(__file__))) -GEODATA = os.path.join(directory, 'geodata') -ECOREGIONS = os.path.join(GEODATA, 'ecoregions', 'single-parts.shp') -ELEVATION = os.path.join(GEODATA, 'srtm', 'topo30-180.tif') -TEMP = os.path.join(GEODATA, 'air_temp') -PRECIP = os.path.join(GEODATA, 'precipitation') +GEODATA = directory / 'geodata' +ECOREGIONS = GEODATA / 'ecoregions' / 'single-parts.shp' +ELEVATION = GEODATA / 'srtm' / 'topo30-180.tif' +TEMP = GEODATA / 'air_temp' +PRECIP = GEODATA / 'precipitation' MIN_YEAR = 1900 MAX_YEAR = 2017 diff --git a/biomes/draw.py b/biomes/draw.py index 9581a61..ab6e95d 100644 --- a/biomes/draw.py +++ b/biomes/draw.py @@ -8,7 +8,7 @@ from constants import BIOMES import pandas as pd import cartopy.crs as ccrs -def draw(df, earth=True, width=23.22, height=13, only_draw=False, path=None, alpha=1): +def draw(df, earth=True, width=23.22, height=13, longitude_max=None, latitude_max=None, only_draw=False, path=None, alpha=1): logger.debug('draw(df, %s)', path) biomes = {} biome_numbers = df['biome_num'].unique() @@ -17,7 +17,7 @@ def draw(df, earth=True, width=23.22, height=13, only_draw=False, path=None, alp if earth: p = (row.longitude, row.latitude) else: - p = (to_range(-180, 180, 0, width)(row.longitude), to_range(-90, 90, 0, height)(row.latitude)) + p = (to_range(-180, 180, 0, longitude_max)(row.longitude), to_range(-90, 90, 0, latitude_max)(row.latitude)) if row.biome_num in biomes: biomes[row.biome_num].append(p) diff --git a/biomes/map_generator.py b/biomes/map_generator.py index dcd5a77..95d1407 100644 --- a/biomes/map_generator.py +++ b/biomes/map_generator.py @@ -57,7 +57,7 @@ parameters = { 'max': 1e5, }, 'water_proportion': { - 'default': 0.6, + 'default': 0.8, 'type': 'float', 'min': 0, 'max': 0.99, @@ -242,7 +242,6 @@ def random_elevate_agent(ground, position, height, size=p['mountain_area_elevati def mountain_agent(ground, position): - print('mountain_agent') if not away_from_sea(ground, position): return @@ -373,7 +372,7 @@ def generate_biomes(ground): print('running prediction models') print(p['mean_precipitation'], p['mean_temperature']) - result = predict_end_to_end(df, boundary) + result = predict_end_to_end(df) # fig = plt.figure() # ax = fig.add_subplot(111) @@ -392,7 +391,7 @@ print(df['elevation'].min(), df['elevation'].max()) print(df['distance_to_water'].min(), df['distance_to_water'].max()) print(df['latitude'].min(), df['latitude'].max()) -def predict_end_to_end(input_df, boundary, checkpoint_temp='checkpoints/temp.h5', checkpoint_precip='checkpoints/precip.h5', checkpoint_biomes='checkpoints/b.h5', year=2000): +def predict_end_to_end(input_df, checkpoint_temp='checkpoints/temp.h5', checkpoint_precip='checkpoints/precip.h5', checkpoint_biomes='checkpoints/b.h5', year=2000): batch_size = A_params['batch_size']['grid_search'][0] layers = A_params['layers']['grid_search'][0] optimizer = A_params['optimizer']['grid_search'][0](A_params['lr']['grid_search'][0]) @@ -428,8 +427,8 @@ def predict_end_to_end(input_df, boundary, checkpoint_temp='checkpoints/temp.h5' inputs = input_df[INPUTS] inputs.loc[:, 'mean_temp'] = p['mean_temperature'] - inputs_copy = inputs.copy() - inputs_copy.loc[:, 'mean_temp'] = mean_temperature_over_years(df, size=inputs.shape[0]) + inputs_copy = df[INPUTS] + inputs_copy.loc[:, 'mean_temp'] = mean_temperature_over_years(df, size=inputs_copy.shape[0]) inputs = inputs.to_numpy() inputs = normalize_ndarray(inputs, inputs_copy) @@ -440,8 +439,8 @@ def predict_end_to_end(input_df, boundary, checkpoint_temp='checkpoints/temp.h5' inputs = input_df[INPUTS] inputs.loc[:, 'mean_precip'] = p['mean_precipitation'] - inputs_copy = inputs.copy() - inputs_copy.loc[:, 'mean_precip'] = mean_precipitation_over_years(df, size=inputs.shape[0]) + inputs_copy = df[INPUTS] + inputs_copy.loc[:, 'mean_precip'] = mean_precipitation_over_years(df, size=inputs_copy.shape[0]) inputs = inputs.to_numpy() inputs = normalize_ndarray(inputs, inputs_copy) @@ -466,7 +465,7 @@ def predict_end_to_end(input_df, boundary, checkpoint_temp='checkpoints/temp.h5' columns = ['latitude', 'longitude', 'biome_num'] new_data = pd.DataFrame(columns=columns) - nframe = pd.DataFrame(columns=frame.columns, data=normalize_ndarray(frame.to_numpy())) + nframe = pd.DataFrame(columns=frame.columns, data=normalize_ndarray(frame.to_numpy(), df[frame.columns].to_numpy())) for season in SEASONS: inputs += [ @@ -487,14 +486,10 @@ def predict_end_to_end(input_df, boundary, checkpoint_temp='checkpoints/temp.h5' }, columns=columns) new_data = new_data.append(f) - #print(new_data) - draw(new_data, earth=False, only_draw=True, width=p['width'], height=p['height'], alpha=0.7) + draw(new_data, earth=False, only_draw=True, longitude_max=p['width'], latitude_max=p['height'], alpha=0.7) -# TODO: reduce opacity of biome layer if __name__ == "__main__": # p['width'] = 50 # p['height'] = 50 - p['seed'] = 3 generate_map(True) - # print(normalize_ndarray(np.array([[ 5.59359803,0.99879546,-90., 45.24], [ 5.54976747, 0.99879546,-86.4, 45.24 ]]))) plt.show() diff --git a/biomes/mars.py b/biomes/mars.py new file mode 100644 index 0000000..fc1f42a --- /dev/null +++ b/biomes/mars.py @@ -0,0 +1,35 @@ +import rasterio +from constants import * +import matplotlib.pyplot as plt +from map_generator import predict_end_to_end +import pandas as pd + +MARS=GEODATA / 'planets' / 'Mars_MGS_MOLA_DEM_mosaic_global_463m.tif' +elevation = rasterio.open(ELEVATION) +elevation_data = elevation.read(1) + + +data = { + 'longitude': [], + 'latitude': [], + 'elevation': [], + 'distance_to_water': [] +} + +for longitude in range(-179, 179): + print('-', end='') + for latitude in range(-89, 89): + data['longitude'].append(longitude) + data['latitude'].append(latitude) + + elev = elevation_data[elevation.index(longitude, latitude)] + data['elevation'].append(elev) + data['distance_to_water'] = 999 + print('+', end='') + + print('') + +df = pd.DataFrame(data) + +predict_end_to_end(df) +plt.show() diff --git a/biomes/predict.py b/biomes/predict.py index ca3f330..3446439 100644 --- a/biomes/predict.py +++ b/biomes/predict.py @@ -138,15 +138,16 @@ def predicted_precips_cmd(checkpoint='checkpoints/precip.h5', year=2000): Precip.restore(checkpoint) predicted_precips(Precip, year=year) -def predict_end_to_end(Temp, Precip, Biomes, year=2000): +def predict_end_to_end(Temp, Precip, Biomes, df=pd.read_pickle('data.p'), year=2000): columns = INPUTS - df = pd.read_pickle('data.p') - inputs = df[INPUTS] + earth_df = pd.read_pickle('data.p') + earth_df_inputs = earth_df[INPUTS] all_temps = ['temp_{}_{}'.format(season, year) for season in SEASONS] inputs.loc[:, 'mean_temp'] = np.mean(df[all_temps].values) + earth_df_inputs.loc[:, 'mean_temp'] = np.mean(earth_df[all_temps].values) print(inputs['mean_temp']) inputs = inputs.to_numpy() @@ -208,7 +209,7 @@ def predict_end_to_end(Temp, Precip, Biomes, year=2000): print(new_data) draw(new_data) -def predict_end_to_end_cmd(checkpoint_temp='checkpoints/temp.h5', checkpoint_precip='checkpoints/precip.h5', checkpoint_biomes='checkpoints/b.h5', year=2000): +def predict_end_to_end_cmd(checkpoint_temp='checkpoints/temp.h5', checkpoint_precip='checkpoints/precip.h5', checkpoint_biomes='checkpoints/b.h5', year=2000, **kwargs): batch_size = A_params['batch_size']['grid_search'][0] layers = A_params['layers']['grid_search'][0] optimizer = A_params['optimizer']['grid_search'][0](A_params['lr']['grid_search'][0]) @@ -241,7 +242,7 @@ def predict_end_to_end_cmd(checkpoint_temp='checkpoints/temp.h5', checkpoint_pre Biomes.prepare_for_use() Biomes.restore(checkpoint_biomes) - predict_end_to_end(Temp=Temp, Precip=Precip, Biomes=Biomes, year=year) + predict_end_to_end(Temp=Temp, Precip=Precip, Biomes=Biomes, year=year, **kwargs) if __name__ == "__main__": diff --git a/biomes/random_map_generator.py b/biomes/random_map_generator.py new file mode 100644 index 0000000..859b928 --- /dev/null +++ b/biomes/random_map_generator.py @@ -0,0 +1,12 @@ +from map_generator import generate_map +from pathlib import Path +import matplotlib.pyplot as plt + +seed = 0 +while True: + generate_map(biomes=True, seed=seed) + root = Path('maps') / 'generated' + plt.savefig(root / f'{seed}.png') + plt.clf() + + seed += 1