view 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
line wrap: on
line source
/*
 *	Copyright (c) 2001, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY;
 *	without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.units;

import samer.core.*;
import samer.core.types.*;
import samer.core.util.*;
import samer.maths.*;
import samer.tools.*;
import java.util.*;

/**
	Builds up a 2D joint histogram of 2 elements of a vector.
*/

public class JointHistogram extends Viewable implements Agent, Task, Observer {
	VInteger		I1, I2;
	VMap			vmap;
	Matrix			bins;
	IMap			map;
	int				i1, i2;
	double[]		xArray;
	double[][]	binArray;
	double[]		lbw;
	VDouble		L;
	int				count;

	public JointHistogram(Vec x, int bins, int i1, int i2) {
		this(x,bins);
		I1.value=i1; I1.changed();
		I2.value=i2; I2.changed();
	}

	public JointHistogram(Vec x, int M)
	{
		super("joint.histogram");

		xArray=x.array();
		if (xArray==null) throw new Error("vec array not accessible");

		Shell.push(node);

		// int M=Shell.getInt("bins",32);
		int N=x.size();

		Shell.setAutoRegister(false);
		I1=new VInteger("i1",1);
		I2=new VInteger("i2",2);
		I1.setRange(0,N-1);
		I2.setRange(0,N-1);

		lbw = new double[M];
		vmap=new VMap(new LinearMap(0,1,M)); // ??
		bins = new Matrix("bins",M,M);
		L = new VDouble("likelihood");
		Shell.setAutoRegister(true);
		Shell.pop();

		binArray=bins.getArray();
		I1.addObserver(this);
		I2.addObserver(this);
		vmap.addObserver(this);
		update(vmap,VMap.NEW_MAP);
		setAgent(this);
		Shell.registerViewable(this);
	}

	public void clear() { count=0; bins.zero(); bins.changed(); }
	public Matrix getBinMatrix() { return bins; }
	public VDouble getLikelihoodSignal() { return L; }
	public VMap getVMap() { return vmap; }

	public void starting() {}
	public void stopping() {}
	public void run()	{
		int j=map.clipInt(xArray[i1]);
		int k=map.clipInt(xArray[i2]);
		L.value=-Math.log((++binArray[j][k])/(double)(++count)) + lbw[k] + lbw[j];
		L.changed();
		bins.changed();
	}

	public void dispose() {
		I1.dispose();
		I2.dispose();
		bins.dispose();
		L.dispose();
	}

	public void normalise() {
		int n=binArray.length, c=0;
		for (int i=0; i<n; i++) c+=(int)Mathx.sum(binArray[i]);
		count=c;
		Shell.print("JointHistogram: count="+count);
 	}

	public void update(Observable o, Object a)
	{
		if (o==vmap) {
			if (a==VMap.NEW_MAP) map=vmap.getMap();
			for (int i=0; i<binArray.length; i++) {
				lbw[i]=Math.log(map.inverseFromInt(i+1) - map.inverseFromInt(i));
			}
		} else {
			i1=I1.value;
			i2=I2.value;
		}
	}

	public void getCommands(Registry r) {
		r.add("clear").add("normalise");
		r.group(); vmap.getCommands(r);
	}

	public void execute(String cmd, Environment env) throws Exception {
		if (cmd.equals("clear")) clear();
		else if (cmd.equals("normalise")) normalise();
		vmap.execute(cmd,env);
	}

	public Viewer getViewer()
	{
		DefaultViewer vwr=new DefaultViewer(this);

		vwr.add(I1);
		vwr.add(I2);
		vwr.add(bins.viewable());
		vwr.add(L);

		return vwr;
	}
}