feat(random_map_generator): generate maps randomly

This commit is contained in:
Mahdi Dibaiee 2019-05-22 08:29:42 +04:30
parent d91c04e2f5
commit c9c7f26910
6 changed files with 71 additions and 27 deletions

View File

@ -1,12 +1,13 @@
import os 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') GEODATA = directory / 'geodata'
ECOREGIONS = os.path.join(GEODATA, 'ecoregions', 'single-parts.shp') ECOREGIONS = GEODATA / 'ecoregions' / 'single-parts.shp'
ELEVATION = os.path.join(GEODATA, 'srtm', 'topo30-180.tif') ELEVATION = GEODATA / 'srtm' / 'topo30-180.tif'
TEMP = os.path.join(GEODATA, 'air_temp') TEMP = GEODATA / 'air_temp'
PRECIP = os.path.join(GEODATA, 'precipitation') PRECIP = GEODATA / 'precipitation'
MIN_YEAR = 1900 MIN_YEAR = 1900
MAX_YEAR = 2017 MAX_YEAR = 2017

View File

@ -8,7 +8,7 @@ from constants import BIOMES
import pandas as pd import pandas as pd
import cartopy.crs as ccrs 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) logger.debug('draw(df, %s)', path)
biomes = {} biomes = {}
biome_numbers = df['biome_num'].unique() 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: if earth:
p = (row.longitude, row.latitude) p = (row.longitude, row.latitude)
else: 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: if row.biome_num in biomes:
biomes[row.biome_num].append(p) biomes[row.biome_num].append(p)

View File

@ -57,7 +57,7 @@ parameters = {
'max': 1e5, 'max': 1e5,
}, },
'water_proportion': { 'water_proportion': {
'default': 0.6, 'default': 0.8,
'type': 'float', 'type': 'float',
'min': 0, 'min': 0,
'max': 0.99, 'max': 0.99,
@ -242,7 +242,6 @@ def random_elevate_agent(ground, position, height, size=p['mountain_area_elevati
def mountain_agent(ground, position): def mountain_agent(ground, position):
print('mountain_agent')
if not away_from_sea(ground, position): if not away_from_sea(ground, position):
return return
@ -373,7 +372,7 @@ def generate_biomes(ground):
print('running prediction models') print('running prediction models')
print(p['mean_precipitation'], p['mean_temperature']) print(p['mean_precipitation'], p['mean_temperature'])
result = predict_end_to_end(df, boundary) result = predict_end_to_end(df)
# fig = plt.figure() # fig = plt.figure()
# ax = fig.add_subplot(111) # 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['distance_to_water'].min(), df['distance_to_water'].max())
print(df['latitude'].min(), df['latitude'].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] batch_size = A_params['batch_size']['grid_search'][0]
layers = A_params['layers']['grid_search'][0] layers = A_params['layers']['grid_search'][0]
optimizer = A_params['optimizer']['grid_search'][0](A_params['lr']['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 = input_df[INPUTS]
inputs.loc[:, 'mean_temp'] = p['mean_temperature'] inputs.loc[:, 'mean_temp'] = p['mean_temperature']
inputs_copy = inputs.copy() inputs_copy = df[INPUTS]
inputs_copy.loc[:, 'mean_temp'] = mean_temperature_over_years(df, size=inputs.shape[0]) inputs_copy.loc[:, 'mean_temp'] = mean_temperature_over_years(df, size=inputs_copy.shape[0])
inputs = inputs.to_numpy() inputs = inputs.to_numpy()
inputs = normalize_ndarray(inputs, inputs_copy) 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 = input_df[INPUTS]
inputs.loc[:, 'mean_precip'] = p['mean_precipitation'] inputs.loc[:, 'mean_precip'] = p['mean_precipitation']
inputs_copy = inputs.copy() inputs_copy = df[INPUTS]
inputs_copy.loc[:, 'mean_precip'] = mean_precipitation_over_years(df, size=inputs.shape[0]) inputs_copy.loc[:, 'mean_precip'] = mean_precipitation_over_years(df, size=inputs_copy.shape[0])
inputs = inputs.to_numpy() inputs = inputs.to_numpy()
inputs = normalize_ndarray(inputs, inputs_copy) 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'] columns = ['latitude', 'longitude', 'biome_num']
new_data = pd.DataFrame(columns=columns) 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: for season in SEASONS:
inputs += [ inputs += [
@ -487,14 +486,10 @@ def predict_end_to_end(input_df, boundary, checkpoint_temp='checkpoints/temp.h5'
}, columns=columns) }, columns=columns)
new_data = new_data.append(f) new_data = new_data.append(f)
#print(new_data) draw(new_data, earth=False, only_draw=True, longitude_max=p['width'], latitude_max=p['height'], alpha=0.7)
draw(new_data, earth=False, only_draw=True, width=p['width'], height=p['height'], alpha=0.7)
# TODO: reduce opacity of biome layer
if __name__ == "__main__": if __name__ == "__main__":
# p['width'] = 50 # p['width'] = 50
# p['height'] = 50 # p['height'] = 50
p['seed'] = 3
generate_map(True) 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() plt.show()

35
biomes/mars.py Normal file
View File

@ -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()

View File

@ -138,15 +138,16 @@ def predicted_precips_cmd(checkpoint='checkpoints/precip.h5', year=2000):
Precip.restore(checkpoint) Precip.restore(checkpoint)
predicted_precips(Precip, year=year) 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 columns = INPUTS
df = pd.read_pickle('data.p')
inputs = df[INPUTS] 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] all_temps = ['temp_{}_{}'.format(season, year) for season in SEASONS]
inputs.loc[:, 'mean_temp'] = np.mean(df[all_temps].values) 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']) print(inputs['mean_temp'])
inputs = inputs.to_numpy() inputs = inputs.to_numpy()
@ -208,7 +209,7 @@ def predict_end_to_end(Temp, Precip, Biomes, year=2000):
print(new_data) print(new_data)
draw(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] batch_size = A_params['batch_size']['grid_search'][0]
layers = A_params['layers']['grid_search'][0] layers = A_params['layers']['grid_search'][0]
optimizer = A_params['optimizer']['grid_search'][0](A_params['lr']['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.prepare_for_use()
Biomes.restore(checkpoint_biomes) 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__": if __name__ == "__main__":

View File

@ -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