Mercurial > hg > medcouple
changeset 66:916349600c4b
algorithm-visualisation code
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Wed, 18 May 2016 17:03:43 -0400 |
parents | 364a0c9df823 |
children | 1aa38f4846e0 |
files | talk/code/show_mc.py talk/code/showalgo.m |
diffstat | 2 files changed, 142 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/talk/code/show_mc.py +++ b/talk/code/show_mc.py @@ -21,7 +21,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. -import random +import numpy as np from itertools import tee, izip @@ -83,6 +83,20 @@ Computing, Vol. 7, No. 2 (May 1978), pp. 147-53. """ + + # Yeah, yeah, leaky file handles; it'll be handled by the time the + # program closes. + Hfile = open("H", "w") + Amfile = open("Am", "w") + Rfile = open("R", "w") + Lfile = open("L", "w") + Pfile = open("P", "w") + Qfile = open("Q", "w") + RemFile = open("remaining", "w") + Ptotfile = open("Ptotal", "w") + Qtotfile = open("Qtotal", "w") + jackpot = open("jackpot", "w") + # FIXME: Figure out what to do about NaNs. n = len(X) @@ -140,6 +154,11 @@ return h + for i, zp in enumerate(Zplus): + for j, zm in enumerate(Zminus): + Hfile.write("%s " % h_kern(i, j)), + Hfile.write("\n") + # Init left and right borders L = [0]*n_plus R = [n_minus - 1]*n_plus @@ -148,6 +167,10 @@ Rtot = n_minus*n_plus medc_idx = Rtot//2 + Lfile.write("%s\n" % L) + Rfile.write("%s\n" % R) + RemFile.write("%s\n" % Rtot) + # kth pair algorithm (Johnson & Mizoguchi) while Rtot - Ltot > n_plus: @@ -161,6 +184,8 @@ Am_eps = eps1*(eps1 + abs(Am)) + Amfile.write("%s\n" % Am) + # Compute new left and right boundaries, based on the weighted # median P = [] @@ -185,19 +210,32 @@ sumP = sum(P) + len(P) sumQ = sum(Q) + Ptotfile.write("%s\n" % sumP) + Qtotfile.write("%s\n" % sumQ) + Pfile.write("%s\n" % P) + Qfile.write("%s\n" % Q) + if medc_idx <= sumP - 1: R = P Rtot = sumP + Lfile.write("%s\n" % L) + Rfile.write("%s\n" % R) + RemFile.write("%s\n" % (Rtot - Ltot)) else: if medc_idx > sumQ - 1: L = Q Ltot = sumQ + Lfile.write("%s\n" % L) + Rfile.write("%s\n" % R) + RemFile.write("%s\n" % (Rtot - Ltot)) else: + jackpot.write("1") return Am # Didn't find the median, but now we have a very small search # space to find it in, just between the left and right boundaries. # This space is of size Rtot - Ltot which is <= n_plus + jackpot.write("0") A = [] for (i, (l, r)) in enumerate(izip(L, R)): for j in xrange(l, r + 1): @@ -221,12 +259,11 @@ def main(): - import sys - fname = sys.argv[1] - with open(fname) as f: - data = [float(x) for x in f.readlines() if x.strip() != ""] + data = np.random.uniform(size=18) - print "%.16g" % medcouple_1d(data) + mc = medcouple_1d(data) + with open("mc", "w") as f: + f.write("%s\n" % mc) if __name__ == "__main__": main()
new file mode 100644 --- /dev/null +++ b/talk/code/showalgo.m @@ -0,0 +1,99 @@ +H = load("H"); +L = load("L") + 1; +R = load("R") + 1; +P = load("P") + 1; +Q = load("Q") + 1; +Ptot = load("Ptotal"); +Qtot = load("Qtotal"); +remaining = load("remaining"); +mc = load("mc"); +jackpot = load("jackpot"); +Am = load("Am"); +Am(end+1) = mc; + +colidx = 1:columns(H); + +origH = repmat((H+1)/2, 1, 1, 3); + +for iter = 1:length(Am) + ## Reset the image + imgH = origH; + + ## Make greater-than red + red = imgH(:,:,1); + red(colidx < L(iter, :)') = 1; + imgH(:, :, 1) = red; + + ## Make less-than blue + blue = imgH(:,:,3); + blue(colidx > R(iter, :)') = 1; + imgH(:, :, 3) = blue; + + imshow(imgH); + pause + + ## Make the guess yellow + [i,j] = find(Am(iter) == H); + imgH(i,j,1) = imgH(i,j,2) = 1; + imgH(i,j,3) = 0; + + imshow(imgH); + pause + + if iter == length(Am) + break; + endif + + ## Check the left side + imgLeft = imgH; + red = imgH(:, :, 1); + green = imgH(:, :, 2); + blue = imgH(:, :, 3); + + idx = colidx > P(iter,:)'; + + ## Brighten blue + blue(idx) = 1; + + ## Dim red and green + red(idx) .*= 0.5; + green(idx) .*= 0.5; + + imgLeft(:,:,2) = green; + imgLeft(:,:,3) = blue; + + ## Make the guess yellow again + [i,j] = find(Am(iter) == H); + imgH(i,j,1) = imgH(i,j,2) = 1; + imgH(i,j,3) = 0; + + imshow(imgLeft); + pause + + ## Check the right side + imgRight = imgH; + red = imgH(:, :, 1); + green = imgH(:, :, 2); + blue = imgH(:, :, 3); + + idx = colidx < Q(iter,:)'; + + ## Brighten red + blue(idx) = 1; + + ## Dim blue and green + blue(idx) .*= 0.5; + green(idx) .*= 0.5; + + imgRight(:,:,2) = green; + imgRight(:,:,3) = blue; + + ## Make the guess yellow again + [i,j] = find(Am(iter) == H); + imgH(i,j,1) = imgH(i,j,2) = 1; + imgH(i,j,3) = 0; + + imshow(imgRight); + pause + +endfor