'Fizyka statystyczna i termodynamika': 'Statistical Physics and Thermodynamics',
    'Opracowanie danych doświadczalnych': 'Analysis of Experimental Data',
    'Analiza matematyczna 2': 'Mathematical Analysis 2',
    'Probabilistyka': 'Probability',
    'Algebra z geometrią': 'Algebra and Geometry',
    'Wstęp do fizyki jądrowej': 'Introduction to Nuclear Physics',
    'Analiza matematyczna 3': 'Mathematical Analysis 3',
    'Laboratorium fizyki 2': 'Physics Laboratory 2',
    'Analiza matematyczna 1': 'Mathematical Analysis 1',
    'Mechanika': 'Mechanics'
}

cl = 'L'

co_corr = np.corrcoef(win.getData(class_=cl), rowvar=0)
labels = [pl_en[x] for x in win.getCoursesNames()]

mds = MDS(n_components=2, dissimilarity='precomputed')
dists = np.empty((len(co_corr), len(co_corr)))
for ii in range(len(labels)):
    for jj in range(len(labels)):
        dists[ii][jj] = math.sqrt(2 * (1 - co_corr[ii][jj]))
pos = mds.fit(dists).embedding_

G = nx.Graph()
G.add_nodes_from(range(len(labels)))
textstr = ""
for ii, l in enumerate(labels):
    textstr += str(ii) + " - " + l + "\n"
    for jj in range(ii + 1, len(labels)):
        d = dists[ii][jj]

'''spec: OE, MN, FK, FM'''
# students = win.getStudents(shuffle=False)

# cl = students[0].class_
# sp = students[0].spec
# anno = []
# anno2 = []
# for ii, st in enumerate(students):
#     if st.class_ != cl:
#         anno.append((ii - 1, students[ii - 1].class_))
#         cl = st.class_
#     if st.spec != sp:
#         anno2.append((ii - 1, students[ii - 1].spec))
#         sp = st.spec
# anno.append(((len(students) - 1), students[-1].class_))
# anno2.append(((len(students) - 1), students[-1].spec))
# anno = anno + anno2

students = win.getData(shuffle=False, spec='FK')

# win.shuffleIt(students, 2)
# win.shuffleIt(students, 1)

# st_corr = pairwise_distances(students, students, 'jaccard')
co_corr = pairwise_distances(students.T, students.T, 'jaccard')

heatmap.plotheat(co_corr, xlabels=win.getCoursesNames(), ylabels=win.getCoursesNames(), mode='special')
# heatmap.plotheat(st_corr, changeTicks=False, annotation=anno, mode='special')
matplotlib.rc("font", **font)
matplotlib.rcParams["ps.useafm"] = True
matplotlib.rcParams["pdf.use14corefonts"] = True
matplotlib.rcParams["text.usetex"] = True
# win.shuffleIt(students, 2)
# win.shuffleIt(students, 1)

# st_corr = np.corrcoef(students)

# shuffle C
students = win.getData()
win.shuffleIt(students, 2)
co_corr = np.corrcoef(students, rowvar=0)
heatmap.plotheat(
    co_corr,
    xlabels=[pl_en[x][1] for x in win.getCoursesNames()],
    ylabels=["" for x in win.getCoursesNames()],
    rotation=90,
    num_levels_=6,
    mode="special",
    vmin=vmin,
    vmax=vmax,
    showplot=False,
    removebar=True,
)
savePic(name="c_heat_shufC.eps", path="/home/luke/Documents/APPA/img/fifth/")
# real
students = win.getData()
co_corr = np.corrcoef(students, rowvar=0)
heatmap.plotheat(
    co_corr,
anno2 = []
for ii, st in enumerate(students):
    if st.class_ != cl:
        anno.append((ii - 1, students[ii - 1].class_))
        cl = st.class_
    if st.spec != sp:
        anno2.append((ii - 1, students[ii - 1].spec))
        sp = st.spec
anno.append(((len(students) - 1), students[-1].class_))
anno2.append(((len(students) - 1), students[-1].spec))
anno = anno + anno2

courses = win.getCourses()
'''correlation course-course'''
co_corr = np.empty([len(courses), len(courses)])
for ii, c1 in enumerate(courses):
    for jj, c2 in enumerate(courses):
        v1 = []
        v2 = []
        for st in students:
            v1.append(st.grades[ii])
            v2.append(st.grades[jj])
        random.shuffle(v1)
        random.shuffle(v2)
        co_corr[ii, jj] = mutual_info_score(v1, v2)[0]

# print(len(students))
heatmap.plotheat(co_corr, xlabels=win.getCoursesNames(), ylabels=win.getCoursesNames())
heatmap.plotheat(st_corr, changeTicks=False, annotation=anno)
# heatmap.plotheat(st_corr, changeTicks=False)
    'Podstawy elektroniki': 'Fundamentals of Electronics',
    'Grafika inżynierska': 'Engineering Graphics',
    'Metody matematyczne fizyki': 'Mathematical Methods of Physics',
    'Elektronika w eksperymencie fizycznym': 'Electronics in Physical Experiment',
    'Podstawy projektowania przyrządów wirtualnych': 'Fundamentals of Virtual Devices Design',
    'Programowanie obiektowe': 'Object-Oriented Programming',
    'Podstawy optyki': 'Fundamentals of Optics',
    'Fizyka kwantowa': 'Quantum Physics',
    'Fizyka statystyczna i termodynamika': 'Statistical Physics and Thermodynamics',
    'Opracowanie danych doświadczalnych': 'Analysis of Experimental Data',
    'Analiza matematyczna 2': 'Mathematical Analysis 2',
    'Probabilistyka': 'Probability',
    'Algebra z geometrią': 'Algebra and Geometry',
    'Wstęp do fizyki jądrowej': 'Introduction to Nuclear Physics',
    'Analiza matematyczna 3': 'Mathematical Analysis 3',
    'Laboratorium fizyki 2': 'Physics Laboratory 2',
    'Analiza matematyczna 1': 'Mathematical Analysis 1',
    'Mechanika': 'Mechanics'
}

courses = win.getData().T
courses_names = win.getCoursesNames()
courses_names = [pl_en[x] for x in courses_names]
co_corr = np.corrcoef(win.getData(), rowvar=0)

for ii, c in enumerate(courses):
    av = round(np.mean(c), 2)
    var = round(np.var(c), 2)
    cij = round(sum(co_corr[ii]) - 1.0, 2)
    print(courses_names[ii], av, var, cij, sep=' & ')
    'Fizyka kwantowa': ('Quantum Physics', 'QP'),
    'Fizyka statystyczna i termodynamika': ('Statistical Physics and Thermodynamics', 'SPT'),
    'Opracowanie danych doświadczalnych': ('Analysis of Experimental Data', 'AED'),
    'Analiza matematyczna 2': ('Mathematical Analysis 2', 'MA2'),
    'Probabilistyka': ('Probability', 'P'),
    'Algebra z geometrią': ('Algebra and Geometry', 'AG'),
    'Wstęp do fizyki jądrowej': ('Introduction to Nuclear Physics', 'INP'),
    'Analiza matematyczna 3': ('Mathematical Analysis 3', 'MA3'),
    'Laboratorium fizyki 2': ('Physics Laboratory 2', 'PL2'),
    'Analiza matematyczna 1': ('Mathematical Analysis 1', 'MA1'),
    'Mechanika': ('Mechanics', 'M')
}


co_corr = np.corrcoef(win.getData(class_='K'), rowvar=0)
labels = win.getCoursesNames()

G = nx.Graph()
G.add_nodes_from(range(len(labels)))
textstr = ""
for ii, l in enumerate(labels):
    textstr += str(ii) + " - " + l + "\n"
    for jj in range(ii + 1, len(labels)):
        d = math.sqrt(2 * (1 - co_corr[ii][jj]))
        G.add_edge(ii, jj, weight=d)

si = []
for n, nbrs in G.adjacency_iter():
    w = 0
    for nbr, eattr in nbrs.items():
        w += 1 / eattr['weight']
'''course variance in function of its average'''
c_vars = []
c_avs = []
for c in data.T:
    c_vars.append(np.var(c))
    c_avs.append(np.mean(c))

lr.fit([[x] for x in c_avs], [[x] for x in c_vars])
pr = lr.predict([[x] for x in c_avs])
#
plt.plot(c_avs, c_vars, 'ro', markersize=10)
plt.plot(c_avs, pr, 'g-', label='fit', linewidth=3.0)
plt.xlim(3.2, 4.8)
plt.xlabel('mean value of course\'s grades')
plt.ylabel('variance of course\'s grades')
for ind, x, y in zip(range(len(win.getCoursesNames())), c_avs,  c_vars):
    plt.annotate(pl_en[win.getCoursesNames()[ind]][1], xy=(x, y), xytext=(-6, -15),
                 textcoords='offset points', fontsize=18)

# legend = "Legend:\n"
# legend += "\n".join([str(ii) + ': ' + pl_en[name]
#                      for ii, name in enumerate(win.getCoursesNames())])
# props = dict(boxstyle='round', facecolor='white', alpha=0.1)
# ax.text(0.65, 0.99, legend,
#         transform=ax.transAxes,
#         fontsize=8, verticalalignment='top', bbox=props)
plt.savefig('/home/luke/Documents/APPA/img/sixth/avs_var.svg', format='svg',
            transparent=False, frameon=False, dpi=300, bbox_inches='tight')
#
sys.exit()
'''node force/strength with variances and avs (calculacated above) and pca'''
colourss = ['k*', 'go', 'm^', 'cv']
fc = []
sc = []
for ii, cl in enumerate(classes):
    data = win.getData(class_=cl)
    if cl is None:
        cl = 'all'
    avss = []
    avsc = []
    for s in data:
        avss.append(np.mean(s))
    for jj, c in enumerate(data.T):
        m = np.mean(c)
        avsc.append(m)
        if m < 3.65 and m > 3.35 and cl == 'all':
            fc.append(win.getCoursesNames()[jj])
        if m < 4.3 and m > 4.01 and cl == 'all':
            sc.append(win.getCoursesNames()[jj])

    hist, bins = np.histogram(avss, bins=6, range=(3.0, 5.0))
    bins = (bins[:-1] + bins[1:]) / 2
    if cl == 'all':
        plt.plot(bins, hist, colours[ii] + '-',
                 label='' + cl, linewidth=2.0, markersize=8)
    else:
        plt.plot(bins, hist, colours[ii] + '-', label='' + cl, markersize=8)


plt.xlabel('average grade')
plt.ylabel('number of students')