Commit 8d1da656 authored by lafabregue's avatar lafabregue

tmp commit

parent 2a77257b
......@@ -105,18 +105,22 @@ abstract public class Cluster implements Serializable {
*
* @param o
* index de l'objet de la classe
* @param fromSample
* true if the index is related to the sample, false for the whole data
* @return la distance entre le centre le o-ieme objet de la classe
*/
abstract public double distance(final int o);
abstract public double distance(final int o, boolean fromSample);
/**
* <p>
* Cardinal de la classe.
* </p>
*
* @param fromSample
* true if the index is related to the sample, false for the whole data
* @return le cardinal de la classe
*/
abstract public int getCard();
abstract public int getCard(boolean fromSample);
/**
* <p>
......@@ -172,9 +176,11 @@ abstract public class Cluster implements Serializable {
*
* @param objIndex
* l'index de l'objet dans la classe
* @param fromSample
* true if the index is related to the sample, false for the whole data
* @return le l'objet à l'indice objIndex
*/
abstract public DataObject getObject(final int objIndex);
abstract public DataObject getObject(final int objIndex, boolean fromSample);
/**
* <p>
......@@ -183,9 +189,11 @@ abstract public class Cluster implements Serializable {
*
* @param o
* l'index de l'objet
* @param fromSample
* true if the index is related to the sample, false for the whole data
* @return le degre d'appartenance du o-ieme objet e la classe
*/
abstract public double getMembership(final int o);
abstract public double getMembership(final int o, boolean fromSample);
/**
* <p>
* Probabilite de la classe.
......
......@@ -267,8 +267,8 @@ public abstract class ClusteringResult implements Serializable, MemoryFlush {
h = cor2.length;
mat = new Matrix(w, h);
for (int p = 0; p < this.getNbObjects(); p++) {
final int i = this.indice(cor1, this.getClass(p), w);
final int j = this.indice(cor2, r2.getClass(p), h);
final int i = this.indice(cor1, this.getClass(p, false), w);
final int j = this.indice(cor2, r2.getClass(p, false), h);
mat.matrix[i + j * w]++;
}
for (int i = 0; i < w; i++) {
......@@ -325,14 +325,29 @@ public abstract class ClusteringResult implements Serializable, MemoryFlush {
/**
* <p>
* Classe d'un objet.
* Return the class of an object
* </p>
*
* @param o
* l'index de l'objet
* @return l'index de la classe du o-ieme objet
* object index
* @param onWholeData
* specify if we have to use the whole data index or the sampling index
* @return the o-th object class index
*/
abstract public int getClass(final int o);
abstract public int getClass(final int o, boolean onWholeData);
/**
* <p>
* Return the class of an object in the sample
* </p>
*
* @param o
* object index
* @return the o-th object class index
*/
public int getClass(final int o) {
return getClass(o, false);
}
/**
* <p>
......@@ -412,30 +427,21 @@ public abstract class ClusteringResult implements Serializable, MemoryFlush {
}
/**
* Le resultat du clustering sous forme de tableau, a chaque objet est
* associe le numero de sa classe
* Return the clustering result as an array with the affected class
* for each element in the Data classified
*
* @param fromSample
* tells if we the clustermap based on object from the sample or the whole data
* this parameter might not be supported, it returns the whole data in this case
*/
public int[] getClusterMap(boolean fromSample) {
if (fromSample && learningResult != null) {
// standart Kmeans case
if (learningResult instanceof LearningResultKmeans) {
return ((LearningResultKmeans) learningResult).getLearningClustermap();
}
}
return getClusterMap();
}
abstract public int[] getClusterMap(boolean fromSample);
abstract public int[] getClusterMap();
/**
* Remplace le resultat du clustering via un tableau, a chaque objet est
* associe le numero de sa classe
*/
abstract public void setClusterMap(int[] clusterMap);
abstract public void setClusterMap(int[] clusterMap, boolean fromSample);
/**
* <p>
......@@ -481,7 +487,7 @@ public abstract class ClusteringResult implements Serializable, MemoryFlush {
hist[i] = 0;
}
for (int i = 0; i < this.getNbObjects(); i++) {
hist[this.getClass(i)] += 1;
hist[this.getClass(i, false)] += 1;
}
return hist;
}
......@@ -709,6 +715,32 @@ public abstract class ClusteringResult implements Serializable, MemoryFlush {
}
return -1;
}
public int[][] getComparedConfusionMatrix(ClusteringResult comparedResult, boolean onWholeData) {
if (onWholeData) {
if (data.getWholeDataNbObjects() != comparedResult.getData().getWholeDataNbObjects()) {
return null;
}
} else {
if (data.getNbObjects() != comparedResult.getData().getNbObjects()) {
return null;
}
}
int[][] result = new int[getNbClusters()][comparedResult.getNbClusters()];
int dataCount = 0;
if (onWholeData) {
dataCount = data.getWholeDataNbObjects();
} else {
dataCount = data.getNbObjects();
}
for (int i = 0 ; i < dataCount ; i++) {
result[getClass(i,onWholeData)][comparedResult.getClass(i, onWholeData)]++;
}
return result;
}
private void initClusterTree() {
// TODO: corriger la façon d'accéder aux données de cluster (nouveau
......@@ -1062,5 +1094,5 @@ public abstract class ClusteringResult implements Serializable, MemoryFlush {
ClusteringResult result = new SimpleClusteringResult(clusters);
return result;
}
}
......@@ -45,8 +45,8 @@ public class ClustersColorizer {
final Cluster theFirstCluster = result.getCluster(0);
if (theFirstCluster != null) {
theFirstCluster.setColor(Color.getHSBColor(0, 0, 0));
int nbColorsLeft = result.getNbClusters() - 1;
int cluster = 1;
int nbColorsLeft = result.getNbClusters();
int cluster = 0;
final double saturation = 1.;
for (int i = 0; i < nbLevels; i++) {
......
......@@ -134,7 +134,7 @@ public class ImagebasedClusteringResult extends ClusteringResult {
}
@Override
public int getClass(int o) {
public int getClass(int o, boolean onWholeData) {
return clusterMap[o/clusterMap.length][o % clusterMap.length] & 0xFF;
}
......
......@@ -20,7 +20,10 @@ public class LightCluster extends Cluster {
private int id = -1;
/** the cluster cardinality */
private int cardinal = -1;
private int sampleCardinal = -1;
/** the cluster cardinality */
private int wholeDataCardinal = -1;
/** indexes used to read object in the cluster */
private int clusterIndex = 0;
......@@ -28,6 +31,8 @@ public class LightCluster extends Cluster {
/** iterator used to read object in the cluster */
private IntMaskIterator indexIterator = null;
private boolean iteratorIsFromSample;
/**
* <p>
* Constructor
......@@ -41,14 +46,18 @@ public class LightCluster extends Cluster {
* the id of the cluster in the ClusteringResult
* @param center
* the cluster centroid
* @param card
* @param sampleCard
* the cluster cardinality for the sample
* @param wholeDataCard
* the cluster cardinality
*/
public LightCluster(final ClusteringResult result, int id, DataObject center, int card) {
public LightCluster(final ClusteringResult result, int id, DataObject center,
int sampleCard, int wholeDataCard) {
this.result = result;
this.id = id;
this.clusterCenter = center;
this.cardinal = card;
this.sampleCardinal = sampleCard;
this.wholeDataCardinal = wholeDataCard;
this.weights = new Weights(center.getNbAttributes());
}
......@@ -70,7 +79,8 @@ public class LightCluster extends Cluster {
this.result = result;
this.id = id;
this.clusterCenter = center;
this.cardinal = IntArrayMask.objectInMask(result.getClusterMap(), id);
this.sampleCardinal = IntArrayMask.objectInMask(result.getClusterMap(true), id);
this.wholeDataCardinal = IntArrayMask.objectInMask(result.getClusterMap(false), id);
this.weights = new Weights(center.getNbAttributes());
}
......@@ -89,7 +99,8 @@ public class LightCluster extends Cluster {
public LightCluster(final ClusteringResult result, int id) {
this.result = result;
this.id = id;
this.cardinal = IntArrayMask.objectInMask(result.getClusterMap(), id);
this.sampleCardinal = IntArrayMask.objectInMask(result.getClusterMap(true), id);
this.wholeDataCardinal = IntArrayMask.objectInMask(result.getClusterMap(false), id);
this.weights = new Weights(result.getData().getOneDataObject().getNbAttributes());
}
......@@ -107,7 +118,8 @@ public class LightCluster extends Cluster {
this.result = cluster.getResult();
this.id = cluster.getId();
this.clusterCenter = cluster.getCenter();
this.cardinal = cluster.cardinal;
this.sampleCardinal = cluster.sampleCardinal;
this.wholeDataCardinal = cluster.wholeDataCardinal;
}
......@@ -117,14 +129,22 @@ public class LightCluster extends Cluster {
}
@Override
public double distance(int o) {
return clusterCenter.distance(result.getData().getWholeDataDataObject(o),
weights);
public double distance(int o, boolean fromSample) {
if (fromSample) {
return clusterCenter.distance(result.getData().getWholeDataDataObject(o),
weights);
} else {
return clusterCenter.distance(result.getData().getDataObject(o), weights);
}
}
@Override
public int getCard() {
return cardinal;
public int getCard(boolean fromSample) {
if (fromSample) {
return sampleCardinal;
} else {
return wholeDataCardinal;
}
}
@Override
......@@ -134,27 +154,38 @@ public class LightCluster extends Cluster {
}
@Override
public DataObject getObject(int objIndex) {
public DataObject getObject(int objIndex, boolean fromSample) {
if(objIndex > clusterIndex) {
while(clusterIndex != objIndex && getIndexIterator(false).hasNext()) {
while(clusterIndex != objIndex && getIndexIterator(fromSample).hasNext()) {
clusterIndex++;
getIndexIterator(false).next();
}
return result.getData().getWholeDataDataObject(getIndexIterator(false).current());
if (fromSample) {
return result.getData().getWholeDataDataObject(getIndexIterator(fromSample).current());
} else {
return result.getData().getDataObject(getIndexIterator(fromSample).current());
}
} else if (objIndex < clusterIndex){
while(clusterIndex != objIndex && clusterIndex > 0) {
clusterIndex--;
getIndexIterator(false).previous();
}
return result.getData().getWholeDataDataObject(getIndexIterator(false).current());
if (fromSample) {
return result.getData().getWholeDataDataObject(getIndexIterator(fromSample).current());
} else {
return result.getData().getDataObject(getIndexIterator(fromSample).current());
}
}
if (fromSample) {
return result.getData().getWholeDataDataObject(getIndexIterator(fromSample).current());
} else {
return result.getData().getDataObject(getIndexIterator(fromSample).current());
}
return result.getData().getWholeDataDataObject(getIndexIterator(false).current());
}
@Override
public double getMembership(int o) {
public double getMembership(int o, boolean fromSample) {
throw new NotImplementedError("method not Implemented for LighCluster class");
}
......@@ -176,7 +207,9 @@ public class LightCluster extends Cluster {
}
private IntMaskIterator getIndexIterator(boolean fromSample) {
if (indexIterator == null) {
if (indexIterator == null || iteratorIsFromSample != fromSample) {
clusterIndex = 0;
iteratorIsFromSample = fromSample;
indexIterator = new IntMaskIterator(result.getClusterMap(fromSample), id, false);
}
return indexIterator;
......
......@@ -25,6 +25,8 @@ public class SimpleCluster extends Cluster {
/** Le degre d'appartenance de chaque objet a ce cluster */
private double membershipDegree[] = null;
/** Le degre d'appartenance de chaque objet a ce cluster */
private double wholeDatamembershipDegree[] = null;
/** Probabilite de la classe (pour EM) */
private double membershipProbabilityEM = 0;
......@@ -305,15 +307,25 @@ public class SimpleCluster extends Cluster {
* l'index de l'objet dans la classe
* @return le l'objet à l'indice objIndex
*/
public DataObject getObject(final int objIndex) {
public DataObject getObject(final int objIndex, boolean fromSample) {
// check if objIndex is ok
if (objIndex >= this.data.getViewDataObjects().size()) {
System.out.println("error: class Cluster, method getObject");
return null;
if (fromSample) {
if (objIndex >= this.data.getNbObjects()) {
System.out.println("error: class Cluster, method getObject");
return null;
}
// returns requested object
return this.data.getDataObject(objIndex);
} else {
if (objIndex >= this.data.getWholeDataNbObjects()) {
System.out.println("error: class Cluster, method getObject");
return null;
}
// returns requested object
return this.data.getWholeDataDataObject(objIndex);
}
// returns requested object
return this.data.getViewDataObjects().get(objIndex);
}
/**
......@@ -325,10 +337,16 @@ public class SimpleCluster extends Cluster {
* l'index de l'objet
* @return le degre d'appartenance du o-ieme objet e la classe
*/
public double getMembership(final int o) {
public double getMembership(final int o, boolean fromSample) {
double degree = 0;
if ((o >= 0) && (o < this.membershipDegree.length)) {
degree = this.membershipDegree[o];
if (fromSample) {
if ((o >= 0) && (o < this.membershipDegree.length)) {
degree = this.membershipDegree[o];
}
} else {
if ((o >= 0) && (o < this.wholeDatamembershipDegree.length)) {
degree = this.wholeDatamembershipDegree[o];
}
}
return degree;
}
......
......@@ -252,30 +252,20 @@ public class SimpleClusteringResult extends ClusteringResult {
this.clusters = newList.toArray(new Cluster[0]);
}
/**
* <p>
* Classe d'un objet.
* </p>
*
* @param o
* l'index de l'objet
* @return l'index de la classe du o-ieme objet
*/
public int getClass(final int o) {
int cluster = -27;
if ((o >= 0) && (o < this.clusterMap.length)) {
@Override
public int getClass(final int o, boolean onWholeData) {
int cluster = -1;
if (!onWholeData && learningResult != null) {
// standart Kmeans case
if (learningResult instanceof LearningResultKmeans) {
cluster = ((LearningResultKmeans) learningResult).getLearningClustermap()[o];
}
} else if ((o >= 0) && (o < this.clusterMap.length)) {
cluster = this.clusterMap[o];
}
return cluster;
}
/**
* <p>
* Carte de classification.
* </p>
*
* @return la carte de classification
*/
@Override
public int[] getClusterMap() {
return this.clusterMap;
......
......@@ -1044,7 +1044,7 @@ public abstract class Data implements Cloneable, Serializable, Iterable<DataObje
final int[] groundTruth = new int[selectedObjects.length];
for (int i = 0; i < selectedObjects.length; i++) {
objects.add(this.getDataObject(selectedObjects[i]));
groundTruth[i] = this.getKnownResult().getClass(selectedObjects[i]);
groundTruth[i] = this.getKnownResult().getClass(selectedObjects[i], false);
}
subset = this.newData(objects);
......@@ -1220,7 +1220,7 @@ public abstract class Data implements Cloneable, Serializable, Iterable<DataObje
for (int i = 0, current = 0; i < this.getNbObjects() -1 ; i++) {
if (i != index) {
if (this.knownResult != null) {
newGroundTruth[current] = this.knownResult.getClass(i);
newGroundTruth[current] = this.knownResult.getClass(i, false);
}
current++;
}
......@@ -1498,9 +1498,9 @@ public abstract class Data implements Cloneable, Serializable, Iterable<DataObje
final Matrix mat = new Matrix(w, h);
for (int p = 0; p < r1.getNbObjects(); p++) {
// cluster de p dans r1
final int i = r1.indice(cor1, r1.getClass(p), w);
final int i = r1.indice(cor1, r1.getClass(p, false), w);
// cluster de p dans r2
final int j = r1.indice(cor2, r2.getClass(p), h);
final int j = r1.indice(cor2, r2.getClass(p, false), h);
mat.matrix[i + j * w]++;
}
......
......@@ -204,7 +204,7 @@ public class SimpleData extends Data {
int o = 0;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].getNbObjects(); j++) {
clusterMap[o] = data[i].getKnownResult().getClass(j);
clusterMap[o] = data[i].getKnownResult().getClass(j,false);
o++;
}
}
......@@ -530,7 +530,7 @@ public class SimpleData extends Data {
data[j].currentView.add(this.getDataObject(selected));
done[selected] = true;
if (this.getKnownResult() != null) {
clusterMap[j][i] = this.getKnownResult().getClass(selected);
clusterMap[j][i] = this.getKnownResult().getClass(selected,false);
}
remaining--;
}
......@@ -549,10 +549,10 @@ public class SimpleData extends Data {
final int[] groundTruth = new int[getNbObjects() - 1];
for (int i = 0; i < index; i++) {
groundTruth[i] = this.getKnownResult().getClass(i);
groundTruth[i] = this.getKnownResult().getClass(i,false);
}
for (int i = index; i < groundTruth.length; i++) {
groundTruth[i] = this.getKnownResult().getClass(i + 1);
groundTruth[i] = this.getKnownResult().getClass(i + 1,false);
}
List<DataObject> objects = this.currentView.subList(0, currentView.size()-1);
objects.remove(index);
......@@ -629,7 +629,7 @@ public class SimpleData extends Data {
data[0].currentView.add(this.getDataObject(selected));
done[selected] = true;
if (this.getKnownResult() != null) {
clusterMap[0][i] = this.getKnownResult().getClass(selected);
clusterMap[0][i] = this.getKnownResult().getClass(selected,false);
}
remaining--;
}
......@@ -644,7 +644,7 @@ public class SimpleData extends Data {
if (!done[i]) {
data[1].currentView.add(this.getDataObject(i));
if (this.getKnownResult() != null) {
clusterMap[1][j] = this.getKnownResult().getClass(i);
clusterMap[1][j] = this.getKnownResult().getClass(i,false);
}
j++;
}
......@@ -678,7 +678,7 @@ public class SimpleData extends Data {
for (int j = 0; j < sets[i].length; j++) {
data[i].currentView.add(this.getDataObject(sets[i][j]));
if (this.getKnownResult() != null) {
clusterMap[j] = this.getKnownResult().getClass(sets[i][j]);
clusterMap[j] = this.getKnownResult().getClass(sets[i][j],false);
}
}
if (this.getKnownResult() != null) {
......
......@@ -34,8 +34,6 @@ public class MultiIDIntArrayMask implements Mask {
*
* @param mask
* the int array representing the mask
* @param card
* the number of element unmasked
* @param id
* the list of id of the element to consider
* @param ignore
......
......@@ -809,8 +809,8 @@ public class ClusteringEvaluation {
distance[k] = iter.next().distance(
subIter.next(), weights);
avDistance += distance[k];
if (clusteringResult.getClass(i) == clusteringResult
.getClass(j)) {
if (clusteringResult.getClass(i,false) == clusteringResult
.getClass(j,false)) {
same[k] = 0;
} else {
same[k] = 1;
......@@ -925,9 +925,9 @@ public class ClusteringEvaluation {
for (int i = 0; i < nbcluster; i++) {
for (int c = 0; c < nbsample; c++) {
knowledge[i * nbsample + c] = data.getKnownResult()
.getClass(i * nbperclu + c);
.getClass(i * nbperclu + c, false);
finded[i * nbsample + c] = clusteringResult.getClass(i
* nbperclu + c);
* nbperclu + c, false);
}
}
final ResultsComparison compare = new ResultsComparison(knowledge,
......
......@@ -164,7 +164,7 @@ public class ARFFWriter extends DataFileWriter {
}
if (this.classData.getKnownResult() != null) {
final int objectCluster = this.classData.getKnownResult()
.getClass(i);
.getClass(i, false);
this.ecrivain.print(","
+ this.classData.getKnownResult().getCluster(
objectCluster).getName());
......
......@@ -364,17 +364,7 @@ public class ClassifierKmeans extends LearningMethod {
public ClusteringResult merge(LearningResult learningResult, ClusteringResult _result, Data data, Vector cr) {
if ((LearningResultKmeans) learningResult != null) {
((LearningResultKmeans) learningResult).mergeSeeds(cr);
// this is mandatory because of the LightCluster implementation
((LearningResultKmeans) learningResult).clusterAffectation(data, true);
ClusteringResult result = ((LearningResultKmeans) learningResult).classify(data);
for (KmeansSeed seed : ((LearningResultKmeans) learningResult).getSeeds()) {
if (seed instanceof LightHardSeed) {
((LightHardSeed) seed).setData(data);
((LightHardSeed) seed).setClusterMap(result.getClusterMap());
}
}
return result;
return LearningResultKmeans.classifyOnSample((LearningResultKmeans) learningResult, data);
}
return null;
}
......@@ -384,8 +374,7 @@ public class ClassifierKmeans extends LearningMethod {
if (((LearningResultKmeans) learningResult) != null) {
((LearningResultKmeans) learningResult).removeSeed(c);
// this is mandatory because of the LightCluster implementation
((LearningResultKmeans) learningResult).clusterAffectation(data, true);
return ((LearningResultKmeans) learningResult).classify(data);
return LearningResultKmeans.classifyOnSample((LearningResultKmeans) learningResult, data);
}
return null;
}
......@@ -402,15 +391,7 @@ public class ClassifierKmeans extends LearningMethod {
// remplace le noyau initialial du cluster c par les nouveaux noyaux
((LearningResultKmeans) learningResult).replaceSeeds(c, splitLearnResult.getSeeds());
// reclasse les donnees
// TODO ne pas reclasser les donnees
ClusteringResult result = ((LearningResultKmeans) learningResult).classify(data);
for (KmeansSeed seed : ((LearningResultKmeans) learningResult).getSeeds()) {
if (seed instanceof LightHardSeed) {
((LightHardSeed) seed).setData(data);
((LightHardSeed) seed).setClusterMap(result.getClusterMap());
}
}
ClusteringResult result = LearningResultKmeans.classifyOnSample((LearningResultKmeans) learningResult, data);
result.setMethode(this.getType());
return result;
}
......
......@@ -147,12 +147,49 @@ public class LearningResultKmeans extends LearningResult {
membershipDegree[i] = ((FuzzySeed) this.seeds.get(i)).membership;
result = ClusteringResult.gerenerateDefaultClusteringResult(this, clusterMap, membershipDegree, this.weights,
this.seeds.size(), null, data, this.qualityIndices);
} else
} else {
result = ClusteringResult.gerenerateDefaultClusteringResult(this, clusterMap, null, this.weights,
this.seeds.size(), null, data, this.qualityIndices);
}
return result;
}
/**
* Classify the result but only on the sample
*
* @param learningResult
* the classifier
* @param data
* the data to classify
* @return the clustering result
*/
public static ClusteringResult classifyOnSample(LearningResultKmeans learningResult, Data data) {
// this is mandatory because of the LightCluster implementation
int[] clusterMap = ((LearningResultKmeans) learningResult).clusterAffectation(data, true);
ClusteringResult result = null;
if (((ParametersKmeans) learningResult.params).fuzzy) {
double membershipDegree[][] = new double[learningResult.seeds.size()][];
for (int i = 0; i < learningResult.seeds.size(); i++)
membershipDegree[i] = ((FuzzySeed) learningResult.seeds.get(i)).membership;
result = ClusteringResult.gerenerateDefaultClusteringResult(learningResult, clusterMap,
membershipDegree, learningResult.weights, learningResult.seeds.size(),
null, data, learningResult.qualityIndices);
} else
result = ClusteringResult.gerenerateDefaultClusteringResult(learningResult, clusterMap,
null, learningResult.weights, learningResult.seeds.size(),
null, data, learningResult.qualityIndices);
for (KmeansSeed seed : ((LearningResultKmeans) learningResult).getSeeds()) {
if (seed instanceof LightHardSeed) {
((LightHardSeed) seed).setData(data);
((LightHardSeed) seed).setClusterMap(result.getClusterMap());
}
}
return result;
}
@Override
public void clean() {
......
......@@ -121,9 +121,6 @@ public class HybridClassification extends Classification implements Runnable {
private Data data = null;
/** liste des résulats initiaux avant raffinement */
public ArrayList resultatInitiaux = new ArrayList();
/**
* <p>
* Constructeur vide.
......@@ -700,12 +697,6 @@ public class HybridClassification extends Classification implements Runnable {
// calcul des classification initiales
this.classifInit();
// TODO enlever
for (int i = 0; i < this.agents.size(); i++) {
this.resultatInitiaux.add(this.agents.get(i).getResult().getClusterMap(false));
this.resultatInitiaux.add(this.agents.get(i).getResult().getNbClusters());
}
<