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


maxPos = 4000 # 8740

genData = []

def maxIndex(v):
  return max([n for n, x in enumerate(v) if x > 0])

pos = 0
first = True
cs = []
xs = []
ys = []
pts = [512, 1024, 2048,3000,3500,3700,3900,4000]
yss = { k : [] for k in pts }
with open("mimx.csv") as f:
  for row in csv.reader(f, delimiter=","):
    if first or len(row) < 9:
      first = False
      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

    cs.append(int(row[7]))
    w = np.load(fileName, allow_pickle=False)
    for k in pts:
      yss[k].append(sum(w[k:k + 100]))

    ys.append(sum(w[2048:]))
    xs.append(maxIndex(w))

    #for n, x in enumerate(w):
    #  if x > 1000:
    #    w[n] = 1000

    #yss[int(row[7])].append(np.log(1 + w))

    ##plt.plot(w)


plt.scatter(xs,ys,cs)
plt.savefig(f"graphics/width.pdf")
plt.close()

for k in pts:
  for n in [5,4,3]:
    plt.scatter([x for x, c in zip(ys, cs) if c == n], [x for x, c in zip(yss[k], cs) if c == n])
  plt.savefig(f"graphics/width{k}.pdf")
  plt.close()

#for n in [3,4,5]:
#  for w in yss[n]:
#    plt.plot(w)
#  plt.savefig(f"graphics/width{n}.pdf")
#  plt.close()


print()
print("done")
