diff --git a/examples/plot_journal_filters.py b/examples/plot_journal_filters.py index 90e6fbb..c732d3f 100644 --- a/examples/plot_journal_filters.py +++ b/examples/plot_journal_filters.py @@ -16,6 +16,7 @@ import numpy as np import matplotlib.pyplot as plt +from matplotlib import transforms from matplotlib.patches import Rectangle from matplotlib.patches import Ellipse @@ -27,6 +28,59 @@ plt.rcParams['lines.linewidth'] = 2 +def confidence_ellipse(x, y, ax, n_std=3.0, facecolor='none', **kwargs): + """ + Create a plot of the covariance confidence ellipse of *x* and *y*. + + Parameters + ---------- + x, y : array-like, shape (n, ) + Input data. + + ax : matplotlib.axes.Axes + The axes object to draw the ellipse into. + + n_std : float + The number of standard deviations to determine the ellipse's radiuses. + + **kwargs + Forwarded to `~matplotlib.patches.Ellipse` + + Returns + ------- + matplotlib.patches.Ellipse + """ + if x.size != y.size: + raise ValueError("x and y must be the same size") + + cov = np.cov(x, y) + pearson = cov[0, 1]/np.sqrt(cov[0, 0] * cov[1, 1]) + ## Using a special case to obtain the eigenvalues of this + ## two-dimensional dataset. + ell_radius_x = np.sqrt(1 + pearson) + ell_radius_y = np.sqrt(1 - pearson) + ellipse = Ellipse((0, 0), width=ell_radius_x * 2, height=ell_radius_y * 2, + facecolor=facecolor, **kwargs) + + ## Calculating the standard deviation of x from + ## the squareroot of the variance and multiplying + ## with the given number of standard deviations. + scale_x = np.sqrt(cov[0, 0]) * n_std + mean_x = np.mean(x) + + ## calculating the standard deviation of y ... + scale_y = np.sqrt(cov[1, 1]) * n_std + mean_y = np.mean(y) + + transf = transforms.Affine2D() \ + .rotate_deg(45) \ + .scale(scale_x, scale_y) \ + .translate(mean_x, mean_y) + + ellipse.set_transform(transf + ax.transData) + return ax.add_patch(ellipse) + + def main(): # Color Blind adjusted colors and markers colormap=['#377eb8', '#ff7f00', '#4daf4a', '#f781bf', @@ -34,17 +88,20 @@ def main(): markers=['o', '*', '.', 'x', '+', 's', 'd', 'h', 'v'] lines=['-', ':', '--', '-.'] - x1 = [0, 0.5, 3, 4, 4.25, 5] - y1 = [5, 1, 4, 4.25, 2, 0.5] + points = np.genfromtxt(f'traces/web0_reduced.csv', delimiter=',') + + worst_knee = 20 + x1 = points[worst_knee-5:worst_knee+3,0] + y1 = points[worst_knee-5:worst_knee+3,1] # Plot 1 fg, ax = plt.subplots() ax.plot(x1, y1, color=colormap[0], marker=markers[0], markersize=3) - ax.axhline(y = 1, color=colormap[1], linestyle=lines[2]) - ax.axhline(y = 2, color=colormap[2], linestyle=lines[2]) - ax.annotate('K0', (0.70, 1.1), color=colormap[1]) - ax.annotate('K1', (4.30, 2.1), color=colormap[2]) + ax.axhline(y = 0.5909, color=colormap[1], linestyle=lines[2]) + ax.axhline(y = 0.6127, color=colormap[2], linestyle=lines[2]) + ax.annotate('K0', (2.474E4, 0.5922), color=colormap[1]) + ax.annotate('K1', (2.893E4, 0.6140), color=colormap[2]) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) @@ -54,21 +111,30 @@ def main(): plt.savefig('out/knee_post_processing_00.pdf', bbox_inches='tight', transparent=True) plt.show() - x2 = [0,2,2.1,2.2,3,5] - y2 = [5,4.5,4,1.5,.25,0] + corner_knee = 45 + x2 = points[corner_knee-3:corner_knee+3,0] + y2 = points[corner_knee-3:corner_knee+3,1] + cop = (8.922E4, 0.4048) + prp = (7.32E4, 0.4057) + nep = (9.016E4, 0.3955) + arp = (prp[0], nep[1]) + w1 = cop[0] - arp[0] + h1 = cop[1] - arp[1] + w2 = nep[0] - arp[0] + h2 = prp[1] - arp[1] # Plot 2 fg, ax = plt.subplots() ax.plot(x2, y2, color=colormap[0], marker=markers[0], markersize=3) - ax.add_patch(Rectangle((0, 4), 2, .5, fill=False, hatch='/', color=colormap[1], linewidth=2)) - ax.add_patch(Rectangle((0, 4), 2.1, 1, fill=False, hatch='\\', color=colormap[2], linewidth=2)) - - ax.plot(0, 5, 'o', ms=7, color=colormap[2]) - ax.annotate('P0', (0.1, 5.1), color=colormap[2]) - ax.plot(2, 4.5, 'X', ms=7, color=colormap[1]) - ax.annotate('C', (2.2, 4.6), color=colormap[1]) - ax.plot(2.1, 4, 'o', ms=7, color=colormap[2]) - ax.annotate('P1', (2.2, 4.1), color=colormap[2]) + ax.add_patch(Rectangle(arp, w1, h1, fill=False, hatch='/', color=colormap[1], linewidth=2)) + ax.add_patch(Rectangle(arp, w2, h2, fill=False, hatch='\\', color=colormap[2], linewidth=2)) + + ax.plot(prp[0], prp[1], 'o', ms=7, color=colormap[2]) + ax.annotate('P0', (prp[0], prp[1]+0.0007), color=colormap[2]) + ax.plot(cop[0], cop[1], 'X', ms=7, color=colormap[1]) + ax.annotate('C', (cop[0]+2000, cop[1]), color=colormap[1]) + ax.plot(nep[0], nep[1], 'o', ms=7, color=colormap[2]) + ax.annotate('P1', (cop[0]+2000, nep[1]), color=colormap[2]) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) @@ -79,18 +145,22 @@ def main(): plt.show() # Plot 3 - fg, ax = plt.subplots() - ax.plot(x2, y2, color=colormap[0], marker=markers[0], markersize=3) + cluster_knee = 37 + x3 = points[cluster_knee:cluster_knee+6,0] + y3 = points[cluster_knee:cluster_knee+6,1] - ax.add_patch(Ellipse((2.2, 1.5), 4, 1.5, color=colormap[6], angle=-75, alpha=0.3)) + fg, ax = plt.subplots() + ax.plot(x3, y3, color=colormap[0], marker=markers[0], markersize=3) - ax.plot(2.1, 3, 'x', mew=5, ms=10, color=colormap[1]) - ax.plot(2.15, 2.25, 'x', mew=5, ms=10, color=colormap[1]) + ecx = np.array([5.374e+04, 5.384e+04, 5.55e+04]) + ecy = np.array([4.270992757584038957e-01, 4.263545367586771828e-01, 4.220090871822902434e-01]) - ax.plot(2.2, 1.5, 'x', mew=5, ms=10, color=colormap[2]) + confidence_ellipse(ecx, ecy, ax, n_std=2.5, + **{'color':colormap[6], 'angle':0, 'alpha':0.3}) - ax.plot(2.5, 1.0315, 'x', mew=5, ms=10, color=colormap[1]) - ax.plot(2.8, 0.5626, 'x', mew=5, ms=10, color=colormap[1]) + ax.plot(5.374e+04,4.270992757584038957e-01, 'x', mew=5, ms=10, color=colormap[1]) + ax.plot(5.384e+04,4.263545367586771828e-01, 'x', mew=5, ms=10, color=colormap[2]) + ax.plot(5.55e+04,4.220090871822902434e-01, 'x', mew=5, ms=10, color=colormap[1]) ax.spines['left'].set_visible(True) ax.spines['top'].set_visible(False) diff --git a/examples/plot_journal_kmeans.py b/examples/plot_journal_kmeans.py index cee89f3..df8eb53 100644 --- a/examples/plot_journal_kmeans.py +++ b/examples/plot_journal_kmeans.py @@ -34,6 +34,7 @@ logging.getLogger('PIL').setLevel(logging.WARNING) logger = logging.getLogger(__name__) + def main(): iris = datasets.load_iris() X = iris.data diff --git a/examples/plot_journal_knees.py b/examples/plot_journal_knees.py index 2e272e1..51043f5 100644 --- a/examples/plot_journal_knees.py +++ b/examples/plot_journal_knees.py @@ -55,15 +55,20 @@ def main(args): space_saving = round((1.0-(len(reduced)/len(points)))*100.0, 2) logger.info('Number of data points after RDP: %s(%s %%)', len(reduced), space_saving) + # save points to CSV + # np.savetxt(f'traces/web0_reduced.csv', points_reduced, delimiter=",") + # Plot original trace and reduced version x = points[:,0] y = points[:,1] plt.plot(x, y, color= colormap[0]) - points_reduced = points[reduced] - - # save points to CSV - np.savetxt(f'traces/web0_reduced.csv', points_reduced, delimiter=",") + plt.savefig('out/knees_trace_original.png', bbox_inches='tight', transparent=True) + plt.savefig('out/knees_trace_original.pdf', bbox_inches='tight', transparent=True) + plt.savefig('out/knees_trace_original.svg', bbox_inches='tight', transparent=True) + plt.show() + plt.plot(x, y, color= colormap[0]) + points_reduced = points[reduced] plt.plot(points_reduced[:, 0], points_reduced[:, 1], linestyle=lines[2], marker='o', markersize=3, color=colormap[1]) plt.savefig('out/knees_trace_reduced.png', bbox_inches='tight', transparent=True) plt.savefig('out/knees_trace_reduced.pdf', bbox_inches='tight', transparent=True) @@ -133,6 +138,7 @@ def main(args): plt.plot(x[knees], y[knees], 'o', markersize=7, color=colormap[7]) plt.savefig('out/knees_final_plot.png', bbox_inches='tight', transparent=True) plt.savefig('out/knees_final_plot.pdf', bbox_inches='tight', transparent=True) + plt.savefig('out/knees_final_plot.svg', bbox_inches='tight', transparent=True) plt.show() diff --git a/examples/plot_journal_mrc.py b/examples/plot_journal_mrc.py index 4efcf2f..e5ec993 100644 --- a/examples/plot_journal_mrc.py +++ b/examples/plot_journal_mrc.py @@ -11,13 +11,12 @@ Copyright (c) 2021-2023 The Research Foundation of SUNY ''' + import tqdm import logging import numpy as np -import matplotlib.pyplot as plt +import matplotlib.pyplot as plt -from sklearn import datasets -from sklearn.cluster import KMeans import kneeliverse.rdp as rdp import kneeliverse.kneedle as kneedle @@ -71,27 +70,33 @@ def main(): markers=['o', '*', '.', 'x', '+', 's', 'd', 'h', 'v'] lines=['-', ':', '--', '-.'] - points = np.genfromtxt('traces/rsrch1-minisim1.csv', delimiter=',') + points = np.genfromtxt('traces/web0.csv', delimiter=',') # Plot original trace and reduced version x = points[:,0]/1000 y = points[:,1] plt.plot(x, y, color= colormap[0]) - plt.legend(['MRC (LRU)']) + plt.legend(['MRC (ARC)']) plt.xlabel('Cache Size (GB)') plt.ylabel('Miss Ratio') + # Points + pa = (36.4, 0.5417) + pb = (53.7, 0.4508) + pc = (90.2, 0.3956) + pd = (162.9, 0.3469) + # Arrows - plt.annotate('A', xy=(1.29E4/1000, 0.796), weight='bold', color=colormap[7], - xytext=(2.29E4/1000,.82), arrowprops=dict(arrowstyle='->', lw=1.5, color=colormap[6])) - plt.annotate('B', xy=(3.10E4/1000, 0.786), weight='bold', color=colormap[7], - xytext=(4.10E4/1000,.82), arrowprops=dict(arrowstyle='->', lw=1.5, color=colormap[6])) - plt.annotate('C', xy=(4.25E4/1000, 0.753), weight='bold', color=colormap[7], - xytext=(5.25E4/1000,.80), arrowprops=dict(arrowstyle='->', lw=1.5, color=colormap[6])) - plt.annotate('D', xy=(9.82E4/1000, 0.706), weight='bold', color=colormap[7], - xytext=(1.08E5/1000,.75), arrowprops=dict(arrowstyle='->', lw=1.5, color=colormap[6])) + plt.annotate('A', xy=pa, weight='bold', color=colormap[7], + xytext=(pa[0]+10, pa[1]+0.05), arrowprops=dict(arrowstyle='->', lw=1.5, color=colormap[6])) + plt.annotate('B', xy=pb, weight='bold', color=colormap[7], + xytext=(pb[0]+10, pb[1]+0.05), arrowprops=dict(arrowstyle='->', lw=1.5, color=colormap[6])) + plt.annotate('C', xy=pc, weight='bold', color=colormap[7], + xytext=(pc[0]+10, pc[1]+0.05), arrowprops=dict(arrowstyle='->', lw=1.5, color=colormap[6])) + plt.annotate('D', xy=pd, weight='bold', color=colormap[7], + xytext=(pd[0]+10, pd[1]+0.05), arrowprops=dict(arrowstyle='->', lw=1.5, color=colormap[6])) # Bracket #ax = plt.gca()