Example #1
0
def la_draw(new: tuple, points: tuple, triplets: tuple, anaDiff: tuple, cl: str, plt=plt):
    FOLDER = os.path.join(settings.BASE_DIR, 'static/img/lazy/')
    NB_TRIPLET_DISPLAYED = 4
    plt.xlim(0, 1)
    plt.ylim(0, 1)
    fig = plt.gcf()

    removeFiles(FOLDER)

    # IMAGE 1 #
    # Training set
    plt.scatter(
        [point[0] for point in points],
        [point[1] for point in points],
        c=[point[2] for point in points],
        s=POINTS_SIZE,
        linewidths=0
    )
    plt.scatter(new[0], new[1], c="#000000", s=POINTS_SIZE, linewidths=0)
    pylab.savefig(FOLDER + '0/0')
Example #2
0
def kmeans_draw(new, points, neighbors, nneighbors, cl):
    FOLDER = os.path.join(settings.BASE_DIR, 'static/img/kmeans/')

    removeFiles(FOLDER)

    # Clear the figure
    plt.clf()
    plt.xlim(0, 1)
    plt.ylim(0, 1)

    plt.scatter(
        [point[0] for point in points],
        [point[1] for point in points],
        c=[point[2] for point in points],
        s=POINTS_SIZE,
        linewidths=0,
    )
    pylab.savefig(FOLDER +"0/" + '1', bbox_inches='tight')

    plt.scatter(new[0], new[1], c="#000000", s=POINTS_SIZE, linewidths=0)
    pylab.savefig(FOLDER +"1/"+ '2', bbox_inches='tight')

    plt.scatter(new[0], new[1], c=cl, s=POINTS_SIZE, linewidths=0)
    pylab.savefig(FOLDER +"4/" '5', bbox_inches='tight')

    plt.scatter(new[0], new[1], c="#000000", s=POINTS_SIZE, linewidths=0)
    i=0
    for d in nneighbors:
        plt.plot([new[0], points[d[0]][0]], [new[1], points[d[0]][1]], c="#878787", alpha=.3)
        pylab.savefig(FOLDER+'3/' + str(i), bbox_inches='tight')
        i+=1

    plt.scatter(new[0], new[1], c="#000000")
    i=0
    for d in neighbors:
        plt.plot([new[0], points[d[0]][0]], [new[1], points[d[0]][1]], c="#878787", alpha=.3)
        pylab.savefig(FOLDER +"2/" +str(i), bbox_inches='tight')
        i+=1
Example #3
0
def f_draw(new: tuple, points: tuple, triplets: tuple, anaDiff: tuple, cl: str, plt=plt):
    FOLDER = os.path.join(settings.BASE_DIR, 'static/img/fadana/')
    NB_TRIPLET_DISPLAYED = 4
    plt.xlim(0, 1)
    plt.ylim(0, 1)
    fig = plt.gcf()

    removeFiles(FOLDER)

    # IMAGE 1 #
    # Training set
    plt.scatter(
        [point[0] for point in points],
        [point[1] for point in points],
        c=[point[2] for point in points],
        s=POINTS_SIZE,
        linewidths=0
    )
    plt.scatter(new[0], new[1], c="#000000", s=POINTS_SIZE, linewidths=0)
    pylab.savefig(FOLDER + '0/0')

    # IMAGE 2 #
    # Triplets
    tripletsDisplayed = [choice(triplets) for i in range(NB_TRIPLET_DISPLAYED)]
    # Highlights triplets
    label = ['A', 'B', 'C']
    for i, t in enumerate(tripletsDisplayed):
        patchs = [fig.gca().add_artist(plt.Circle((points[t[i+1]][0], points[t[i+1]][1]), radius=0.02, color='#FF0000')) for i in range(3)]
        texts = [pylab.text(points[t[i+1]][0], points[t[i+1]][1], label[i]) for i in range(3)]
        pylab.savefig(FOLDER + '1/' + str(i))
        # Clear circles and texts
        for i in range(3):
            patchs[i].remove()
            texts[i].remove()

    # IMAGE 3 #
    # computes analogical difference
    print("On trouve les meilleurs triplets en résolvant le calcul de différence analogique")
    print(tripletsDisplayed[0])
    # txt = pylab.text(.5, 1.05, 'Find best triplets solving analogical difference')
    a = points[tripletsDisplayed[0][1]]
    b = points[tripletsDisplayed[0][2]]
    c = points[tripletsDisplayed[0][3]]
    patchs = [fig.gca().add_artist(plt.Circle((points[tripletsDisplayed[0][i+1]][0],
                                       points[tripletsDisplayed[0][i+1]][1]),
                                       radius=0.02,
                                       color='#FF0000')) for i in range(3)]
    pylab.savefig(FOLDER + '2/0')
    # txt.remove()
    # remove patches
    for p in patchs:
        p.remove()

    # IMAGE 4 #
    annot = []
    for i, p in enumerate([a, b, c]):
        # Fancy annotation describing the x and y values and the place in the equation of the point
        annot.append(plt.annotate(label[i] + '\nx = '+ str(round(p[0], 3)) + '\ny = ' + str(round(p[1], 3)),
            xy=(p[0], p[1]), xytext=(-20,20),
            textcoords='offset points', ha='center', va='bottom',
            bbox=dict(boxstyle='round,pad=0.2', fc='yellow', alpha=0.3),
            arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',
                            color='red')))
    # print("On calcul la difference analogique de chaque composante (valeurs normalisées)")
    # txt = pylab.text(.6, 1.05, 'A - B, on each attribut')
    adx1 = a[0] - b[0]                # A - B
    ady1 = a[1] - b[1]
    txt1 = pylab.text(.095, 1.08, "Ax - Bx = " + str(adx1))
    txt2 = pylab.text(.095, 1.02, "Ay - By = " + str(ady1))
    pylab.savefig(FOLDER + '3/0')
    # Analogical difference C and D
    # txt.remove()
    txt1.remove()
    txt2.remove()

    # txt = pylab.text(.6, 1.05, 'C - D, on each attribut')
    adx2 = c[0] - new[0]                         # C - D
    ady2 = c[1] - new[1]
    txt1 = pylab.text(.095, 1.08, "Cx - Dx = " + str(adx2))
    txt2 = pylab.text(.095, 1.02, "Cy - Dy = " + str(ady2))
    pylab.savefig(FOLDER + '4/0')
    # txt.remove()
    txt1.remove()
    txt2.remove()

    # remove annoations
    for a in annot:
        a.remove()

    # Real analogical difference
    # pylab.text('AD = 1 - | (a - B) - (C - D) |, on each attribute, then sum everything to get the final analogical difference')
    adx = 1 - abs(adx1 - adx2)                              # AD = 1 - | (A - B) - (C - D) |
    # print("AD(Ax, Bx) = 1 - | (Ax - Bx) - (Cx - Dx) | = " + str(adx))
    ady = 1 - abs(ady1 - ady2)
    # print("AD(Ay, By) = 1 - | (Ay - By) - (Cy - Dy) | = " + str(ady))
    ad = adx + ady
    # print("AD(A, B, C, D) Finale = " + str(ad))
    txt = pylab.text(.5, 1.05, "AD(A, B, C, D) Finale = " + str(ad))
    pylab.savefig(FOLDER + '5/0')
    txt.remove()

    # IMAGE 5 #
    for i, closest in enumerate(anaDiff):
        tplt = triplets[closest[0]]
        # Circles to highlight the current points of the equation
        w, x, y = points[tplt[1]], points[tplt[2]], points[tplt[3]]
        annot1 = plt.annotate(s='A', xy=(w[0], w[1]))
        annot2 = plt.annotate(s='B', xy=(x[0], x[1]))
        annot3 = plt.annotate(s='C', xy=(y[0], y[1]))
        textEquation = pylab.text(.5, 1.05, str(w[-1]) + " : " + str(x[-1]) + " :: " + str(y[-1]) + " : x")
        pylab.savefig(FOLDER + '6/0')
        # Clear the circles & txt
        for annot in [annot1, annot2, annot3]:
            annot.remove()
        textEquation.remove()

    # IMAGE 6 #
    if(cl != None):
        plt.scatter(new[0], new[1], c=cl, s=POINTS_SIZE, linewidths=0)
    else:
        pylab.text(.095, 1.05, "Aucune equation analogique n'a pu être résolue")
    pylab.savefig(FOLDER + '7/0')
Example #4
0
def pb_draw(new, points, c, couples, classe, plt=plt):
	FOLDER = os.path.join(settings.BASE_DIR, 'static/img/pairBased/')
	NB_COUPLES_DISPLAYED = 4

	removeFiles(FOLDER) # remove previous files

	# IMAGE 1 #
	_reset(plt, points, c)
	pylab.savefig(FOLDER + '0/' + '0')

	# IMAGE 2 #
	plt.scatter(new[0], new[1], c="#000000", s=POINTS_SIZE, linewidths=0)
	pylab.savefig(FOLDER  + '1/' + '0')

	# IMAGE 3 #
	plt.plot([new[0], c[0][0]], [new[1], c[0][1]], c="#878787", alpha=.3)
	pylab.text(0.5, 1.05, 'Nearest neighbor, dist='+str(round(c[1], 4)), fontsize=12)
	pylab.savefig(FOLDER + '2/' + '0')

	# IMAGE 4 #
	# Clear and redraw the points, axes, ...
	plt.clf()
	_reset(plt, points, c)
	plt.scatter(new[0], new[1], c="#000000", s=POINTS_SIZE, linewidths=0)

	# Select random couples to display
	displayedCouples = [choice(couples) for i in range(NB_COUPLES_DISPLAYED)]
	for c in [choice(couples) for i in range(NB_COUPLES_DISPLAYED)]:
		plt.plot([c[0][0], c[1][0]], [c[0][1], c[1][1]], c="#878787", alpha=.3)	# Draw the line between the two points
		midx, midy = (c[0][0] + c[1][0]) / 2, (c[0][1] + c[1][1]) / 2	# Where the text is placed
		annot = plt.annotate(str(round(c[2], 3)), xy=(midx, midy), xytext=(-15,15),
            		textcoords='offset points', ha='center', va='bottom',
            		arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5'))
		# pylab.text(midx, midy, str(round(c[2], 3)))	 # Display the distance between the two points
	pylab.savefig(FOLDER + '3/' + '0')
	annot.remove()

	# IMAGE 5 #
	if(classe is None):
		print("Can't classify")
	print("Solving analogical equation, first solved => class given to the new point")
	# Clear and redraw the points, axes, ...
	plt.clf()
	_reset(plt, points, c)
	plt.scatter(new[0], new[1], c="#000000", s=POINTS_SIZE, linewidths=0)
	# pylab.text(0.5, 1.05, 'Couples creation + distance computation', fontsize=12)

	for i, closest in enumerate(couples):
		# Circles to highlight the current points of the equation
		w, x, y = closest[0], closest[1], c[0]
		annot1 = plt.annotate(s='A', xy=(w[0], w[1]))
		annot2 = plt.annotate(s='B', xy=(x[0], x[1]))
		annot3 = plt.annotate(s='C', xy=(y[0], y[1]))
		textEquation = pylab.text(.5, 1.05, str(w[-1]) + " : " + str(x[-1]) + " :: " + str(y[-1]) + " : x")
		pylab.savefig(FOLDER + '4/' + str(i))
		# Clear the circles & txt
		for annot in [annot1, annot2, annot3]:
			annot.remove()
		textEquation.remove()

	# IMAGE 6 #
	_reset(plt, points, c)
	plt.scatter(new[0], new[1], c=classe, s=POINTS_SIZE, linewidths=0)
	pylab.savefig(FOLDER + '5/0')