world-ecoregion/utils.py
2019-02-27 15:06:20 +03:30

86 lines
2.7 KiB
Python

import numpy as np
import tensorflow as tf
import pandas as pd
from constants import *
inputs = ['elevation', 'distance_to_water']
output = 'biome_num'
def normalize(v):
return (v - np.min(v)) / (np.max(v) - np.min(v))
def normalize_ndarray(ar):
tr = np.transpose(ar)
for i in range(tr.shape[0]):
tr[i] = normalize(tr[i])
return np.transpose(tr)
def normalize_df(df):
for col in df.columns:
df.loc[col] = normalize(df[col])
return df
def dataframe_to_dataset_biomes(df):
rows = df.shape[0]
# 8 for seasonal temp and precipitation
# 3 for latitude, elevation and distance_to_water
columns = 11
tf_inputs = np.empty((0, columns))
tf_output = np.empty((0))
latitude = np.array(df.index.get_level_values(1))
for year in range(MIN_YEAR, MAX_YEAR + 1):
local_inputs = list(inputs)
for season in SEASONS:
local_inputs += [
'temp_{}_{}'.format(season, year),
'precip_{}_{}'.format(season, year)
]
local_df = df[local_inputs]
local_df.loc[:, 'latitude'] = pd.Series(latitude, index=local_df.index)
tf_inputs = np.concatenate((tf_inputs, local_df.values), axis=0)
tf_output = np.concatenate((tf_output, df[output].values), axis=0)
tf_inputs = tf.cast(normalize_ndarray(tf_inputs), tf.float32)
tf_output = tf.cast(tf_output, tf.int64)
return int(tf_inputs.shape[0]), 11, 14, tf.data.Dataset.from_tensor_slices((tf_inputs, tf_output))
def dataframe_to_dataset_temp_precip(df):
rows = df.shape[0]
# elevation, distance_to_water, latitude
# season, year
columns = 5
tf_inputs = np.empty((0, columns))
tf_output = np.empty((0, 2))
latitude = np.array(df.index.get_level_values(1))
for year in range(MIN_YEAR, MAX_YEAR + 1):
local_inputs = list(inputs)
for idx, season in enumerate(SEASONS):
season_index = idx / len(season)
local_df = df[local_inputs]
local_df.loc[:, 'latitude'] = pd.Series(latitude, index=local_df.index)
local_df.loc[:, 'season'] = pd.Series(np.repeat(season_index, rows), index=local_df.index)
local_df.loc[:, 'year'] = pd.Series(np.repeat(year, rows), index=local_df.index)
output = ['temp_{}_{}'.format(season, year), 'precip_{}_{}'.format(season, year)]
tf_inputs = np.concatenate((tf_inputs, local_df.values), axis=0)
tf_output = np.concatenate((tf_output, df[output].values), axis=0)
tf_inputs = tf.cast(normalize_ndarray(tf_inputs), tf.float32)
tf_output = tf.cast(normalize_ndarray(tf_output), tf.float32)
return int(tf_inputs.shape[0]), 5, 2, tf.data.Dataset.from_tensor_slices((tf_inputs, tf_output))