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 = []

model = network.load()
pos = 0
first = True
with open("prediction.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(["fileName"] + row + [x + "_predicted" for x in row[7:11]])
        continue

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

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

      y = np.array([float(row[7]), float(row[8]), float(row[9]), float(row[10])])

      img = pydicom.dcmread(fileName).pixel_array
      w = wavelet.refine(img)
      pos += 1
      w = 1.0 * w.reshape((512 * 512,))

      prediction = model.predict(np.array([w]), verbose=0)

      wtr.writerow([fileName] + row + list(prediction[0]))




print()
print("done")
