import pydicom
import numpy as np
import matplotlib.pyplot as plt
import os
import csv
import math

import wavelet
import network
import keras
import tensorflow as tf


maxPos = 4000 # 8740

genData = []

hSize = 4096
model = network.load("modelHistogram.keras")
pos = 0
first = True
with open("predictionHistogram.csv", "wt") as fout:
  wtr = csv.writer(fout)
  with open("mimx.csv") as f:
    for row in csv.reader(f, delimiter=","):
      if first or len(row) < 9:
        first = False
        wtr.writerow(["image", row[7]] + [1,2,3,4,5,] + [x + "_predicted" for x in ["v", "1", "2", "3", "4", "5"]])
        continue

      n = f"{row[2]}"
      while len(n) < 4:
        n = f"0{n}"

      fileNameImg = f"../Proband {row[0]}/SE00000{row[1]}/{row[0]}_{n}.dcm"
      fileName = f"../Proband {row[0]}/SE00000{row[1]}/{row[0]}_{n}.dcm_histogram.npy"
      print(f"load '{fileName}' -> {pos}", end="\r")
      pos += 1

      y = network.toOneHot([1.0, 2.0, 3.0, 4.0, 5.0], float(row[7]))
      w = np.log(1 + np.load(fileName, allow_pickle=False)[:hSize])
      prediction = model.predict(np.array([w]), verbose=0)
      p = prediction[0]
      s = sum(prediction[0])
      if s > 0.0:
        p = (1.0 / s) * p
      v = sum(np.array([1.0,2.0,3.0,4.0,5.0]) * p)

      wtr.writerow([fileNameImg, row[7]] + y + [v] + list(prediction[0]))




print()
print("done")
