import numpy as np
import keras


def createModel(loss="mse", optimizer="adam"):
  return createModel1(loss, optimizer)


def createModel1(loss="mse", optimizer="adam"):
  inputs = keras.Input(shape=(512*512,))
  x = keras.layers.Dense(128, activation="softsign")(inputs)
  x = keras.layers.Dense(32, activation="softsign")(x)
  outputs = keras.layers.Dense(4, activation="relu")(x)
  model = keras.Model(inputs=inputs, outputs=outputs)
  model.compile(optimizer=optimizer, loss=loss)
  model.summary()
  return model

def createModel2(loss="mse", optimizer="adam"):
  inputs = keras.Input(shape=(512*512,))
  x = keras.layers.Dense(1024, activation="softsign")(inputs)
  x = keras.layers.Dense(128, activation="softsign")(x)
  x = keras.layers.Dense(32, activation="softsign")(x)
  outputs = keras.layers.Dense(4, activation="relu")(x)
  model = keras.Model(inputs=inputs, outputs=outputs)
  model.compile(optimizer=optimizer, loss=loss)
  model.summary()
  return model

def createModelHistogram(loss="mse", optimizer="adam", nInner=32, lastActivation="relu"):
  inputs = keras.Input(shape=(4096,))
  x = keras.layers.Dense(nInner, activation="softsign")(inputs)
  outputs = keras.layers.Dense(5, activation=lastActivation)(x)
  model = keras.Model(inputs=inputs, outputs=outputs)
  model.compile(optimizer=optimizer, loss=loss)
  model.summary()
  return model

def createModelHistogram2(loss="mse", optimizer="adam", nInner=32, lastActivation="sigmoid"):
  inputs = keras.Input(shape=(4096,))
  x = keras.layers.Reshape((4096,1))(inputs)
  x = keras.layers.AveragePooling1D(8)(x)
  x = keras.layers.Reshape((512,))(x)
  x = keras.layers.Dense(nInner, activation="softsign")(x)
  outputs = keras.layers.Dense(5, activation=lastActivation)(x)
  model = keras.Model(inputs=inputs, outputs=outputs)
  model.compile(optimizer=optimizer, loss=loss)
  model.summary()
  return model

def save(model, fileName="model.keras"):
  model.save(fileName)


def load(fileName="model.keras"):
  return keras.saving.load_model(fileName)
  

def toOneHot(arrItems, value):
  r = []
  for v in arrItems:
    if v == value:
      r.append(1.0)
    else:
      r.append(0.0)
  return r


def smooth(w):
  v = []
  for p in range(len(w)):
    n = 0
    x = 0
    for k in [-2,-1,0,1,2]:
      if p + k >= 0 and p + k < len(w):
        x += w[p+k]
        n += 1
    v.append(x / n)
  return np.array(v)

