annotate src/samer/units/JointHistogram.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
rev   line source
samer@0 1 /*
samer@0 2 * Copyright (c) 2001, Samer Abdallah, King's College London.
samer@0 3 * All rights reserved.
samer@0 4 *
samer@0 5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 6 * without even the implied warranty of MERCHANTABILITY or
samer@0 7 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 8 */
samer@0 9
samer@0 10 package samer.units;
samer@0 11
samer@0 12 import samer.core.*;
samer@0 13 import samer.core.types.*;
samer@0 14 import samer.core.util.*;
samer@0 15 import samer.maths.*;
samer@0 16 import samer.tools.*;
samer@0 17 import java.util.*;
samer@0 18
samer@0 19 /**
samer@0 20 Builds up a 2D joint histogram of 2 elements of a vector.
samer@0 21 */
samer@0 22
samer@0 23 public class JointHistogram extends Viewable implements Agent, Task, Observer {
samer@0 24 VInteger I1, I2;
samer@0 25 VMap vmap;
samer@0 26 Matrix bins;
samer@0 27 IMap map;
samer@0 28 int i1, i2;
samer@0 29 double[] xArray;
samer@0 30 double[][] binArray;
samer@0 31 double[] lbw;
samer@0 32 VDouble L;
samer@0 33 int count;
samer@0 34
samer@0 35 public JointHistogram(Vec x, int bins, int i1, int i2) {
samer@0 36 this(x,bins);
samer@0 37 I1.value=i1; I1.changed();
samer@0 38 I2.value=i2; I2.changed();
samer@0 39 }
samer@0 40
samer@0 41 public JointHistogram(Vec x, int M)
samer@0 42 {
samer@0 43 super("joint.histogram");
samer@0 44
samer@0 45 xArray=x.array();
samer@0 46 if (xArray==null) throw new Error("vec array not accessible");
samer@0 47
samer@0 48 Shell.push(node);
samer@0 49
samer@0 50 // int M=Shell.getInt("bins",32);
samer@0 51 int N=x.size();
samer@0 52
samer@0 53 Shell.setAutoRegister(false);
samer@0 54 I1=new VInteger("i1",1);
samer@0 55 I2=new VInteger("i2",2);
samer@0 56 I1.setRange(0,N-1);
samer@0 57 I2.setRange(0,N-1);
samer@0 58
samer@0 59 lbw = new double[M];
samer@0 60 vmap=new VMap(new LinearMap(0,1,M)); // ??
samer@0 61 bins = new Matrix("bins",M,M);
samer@0 62 L = new VDouble("likelihood");
samer@0 63 Shell.setAutoRegister(true);
samer@0 64 Shell.pop();
samer@0 65
samer@0 66 binArray=bins.getArray();
samer@0 67 I1.addObserver(this);
samer@0 68 I2.addObserver(this);
samer@0 69 vmap.addObserver(this);
samer@0 70 update(vmap,VMap.NEW_MAP);
samer@0 71 setAgent(this);
samer@0 72 Shell.registerViewable(this);
samer@0 73 }
samer@0 74
samer@0 75 public void clear() { count=0; bins.zero(); bins.changed(); }
samer@0 76 public Matrix getBinMatrix() { return bins; }
samer@0 77 public VDouble getLikelihoodSignal() { return L; }
samer@0 78 public VMap getVMap() { return vmap; }
samer@0 79
samer@0 80 public void starting() {}
samer@0 81 public void stopping() {}
samer@0 82 public void run() {
samer@0 83 int j=map.clipInt(xArray[i1]);
samer@0 84 int k=map.clipInt(xArray[i2]);
samer@0 85 L.value=-Math.log((++binArray[j][k])/(double)(++count)) + lbw[k] + lbw[j];
samer@0 86 L.changed();
samer@0 87 bins.changed();
samer@0 88 }
samer@0 89
samer@0 90 public void dispose() {
samer@0 91 I1.dispose();
samer@0 92 I2.dispose();
samer@0 93 bins.dispose();
samer@0 94 L.dispose();
samer@0 95 }
samer@0 96
samer@0 97 public void normalise() {
samer@0 98 int n=binArray.length, c=0;
samer@0 99 for (int i=0; i<n; i++) c+=(int)Mathx.sum(binArray[i]);
samer@0 100 count=c;
samer@0 101 Shell.print("JointHistogram: count="+count);
samer@0 102 }
samer@0 103
samer@0 104 public void update(Observable o, Object a)
samer@0 105 {
samer@0 106 if (o==vmap) {
samer@0 107 if (a==VMap.NEW_MAP) map=vmap.getMap();
samer@0 108 for (int i=0; i<binArray.length; i++) {
samer@0 109 lbw[i]=Math.log(map.inverseFromInt(i+1) - map.inverseFromInt(i));
samer@0 110 }
samer@0 111 } else {
samer@0 112 i1=I1.value;
samer@0 113 i2=I2.value;
samer@0 114 }
samer@0 115 }
samer@0 116
samer@0 117 public void getCommands(Registry r) {
samer@0 118 r.add("clear").add("normalise");
samer@0 119 r.group(); vmap.getCommands(r);
samer@0 120 }
samer@0 121
samer@0 122 public void execute(String cmd, Environment env) throws Exception {
samer@0 123 if (cmd.equals("clear")) clear();
samer@0 124 else if (cmd.equals("normalise")) normalise();
samer@0 125 vmap.execute(cmd,env);
samer@0 126 }
samer@0 127
samer@0 128 public Viewer getViewer()
samer@0 129 {
samer@0 130 DefaultViewer vwr=new DefaultViewer(this);
samer@0 131
samer@0 132 vwr.add(I1);
samer@0 133 vwr.add(I2);
samer@0 134 vwr.add(bins.viewable());
samer@0 135 vwr.add(L);
samer@0 136
samer@0 137 return vwr;
samer@0 138 }
samer@0 139 }
samer@0 140
samer@0 141