Commit 6f180691 authored by lafabregue's avatar lafabregue

Integrate sampling and new file reading tools based on bio-format library

parent a3020548
......@@ -259,7 +259,7 @@ public abstract class Classification extends Observable implements
texte += "<h3>Information</h3>";
texte += "<hr>";
texte += "<p>Nb attributes : "
+ this.getData().getDataObject(0).getNbAttributes() + "</p>";
+ this.getData().getOneDataObject().getNbAttributes() + "</p>";
texte += "<p>Nb objects" + " : " + this.getData().getNbObjects()
+ "</p>";
......
This diff is collapsed.
This diff is collapsed.
package jcl.clustering.ImageBased;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Vector;
import jcl.data.DataObject;
import jcl.data.attribute.AttributeNumerical;
public class CentroidFileManager {
/**
* <p>
* Methode qui permet de creer un fichier contenant les valeurs des centroides
* <p>
*
* @param filePath
* le chemin d'acces du fichier
* @param seeds
* l'ensemble des centroides a copier
*/
public static void writeCentroidFile(String filePath, Vector<DataObject> seeds) {
try{
PrintWriter writer = new PrintWriter(filePath, "UTF-8");
for (int i = 0; i < seeds.size() ; i++) {
String line = "";
for(int j = 0 ; j < seeds.get(0).getNbAttributes() ; j++) {
if (line != "") {
line += " ";
}
line += seeds.get(i).getAttribute(j).getValue();
}
writer.println(line);
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* <p>
* Methode qui permet de lire un fichier contenant les valeurs des centroides
* <p>
*
* @param filePath
* le chemin d'acces du fichier
* @return la liste des centroides
*/
public static Vector<DataObject> readCentroidFile(String filePath) {
Vector<DataObject> seeds = new Vector<DataObject>();
BufferedReader reader = null;
try{
reader = new BufferedReader(new FileReader(filePath));
String line = null;
while ((line = reader.readLine()) != null) {
Vector<AttributeNumerical> attributes = new Vector<AttributeNumerical>();
Scanner sc = new Scanner(line);
while(sc.hasNextDouble()) {
attributes.add(new AttributeNumerical(sc.nextDouble()));
}
DataObject obj = new DataObject(attributes.size());
for(int i = 0 ; i < attributes.size() ; i++) {
obj.setAttribute(i, attributes.get(i));
}
seeds.add(obj);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
}
}
return seeds;
}
}
package jcl.clustering.ImageBased;
import java.io.IOException;
import java.util.Vector;
import jcl.clustering.Cluster;
import jcl.clustering.ClusteringResult;
import jcl.clustering.LightCluster;
import jcl.data.DataObject;
import jcl.utils.Images.ImageReaderWrapper;
import jcl.utils.exceptions.MethodNotImplementedException;
import jcl.weights.ClassificationWeights;
import loci.formats.FormatException;
/**
* <p>
* Classe qui lit/écrit le resultat d'une classification d'une image via un fichier
* <p>
*
* @author baptistelafabregue
*
*/
public class ImagebasedClusteringResult extends ClusteringResult {
/** Chemin d'acces du fichier contenant le resultat de la classification */
private String filePath = "";
/** tableau qui contient pour chaque element d'un Data de type image son cluster de rattachement */
private byte[][] clusterMap = null;
/** non utilisé pour l'instant, mais doit pemerttre d'accéder au contenu de maniere streamee */
private int maxMemoryUsage = 0;
/** le format des pixel de l'image, correspond au formats de la classe FormatTool */
private int pixelType = 0;
/**
* <p>
* Constructeur
* <p>
*
* @param path
* le chemin d'acces du fichier contenant le resultat de la classification
* @param weights
* les ponderations utilisees
*/
public ImagebasedClusteringResult(String path, ClassificationWeights weights) {
this.filePath = path;
loadFromFile();
this.weights = weights;
this.randomColor();
}
/**
* <p>
* Constructeur
* <p>
*
* @param path
* le chemin d'acces du fichier contenant le resultat de la classification
* @param centroidsPath
* le chemin d'acces du fichier contenant la liste des centroids
* @param weights
* les ponderations utilisees
*/
public ImagebasedClusteringResult(String classifPath, String centroidsPath,
ClassificationWeights weights) {
this.filePath = classifPath;
loadFromFile();
Vector<DataObject> seeds = CentroidFileManager.readCentroidFile(centroidsPath);
this.clusters = new Cluster[seeds.size()];
for (int i = 0 ; i < seeds.size() ; i++) {
clusters[i] = new LightCluster(this, i, seeds.get(i));
}
this.weights = weights;
this.randomColor();
}
/**
* <p>
* Constructeur
* <p>
*
* @param path
* le chemin d'acces du fichier contenant le resultat de la classification
* @param seeds
* l'ensemble des centroides
* @param weights
* les ponderations utilisees
*/
public ImagebasedClusteringResult(String classifPath, Vector<DataObject> seeds,
ClassificationWeights weights) {
this.filePath = classifPath;
loadFromFile();
this.clusters = new Cluster[seeds.size()];
for (int i = 0 ; i < seeds.size() ; i++) {
clusters[i] = new LightCluster(this, i, seeds.get(i));
}
this.weights = weights;
this.randomColor();
}
/**
* <p>
* Constructeur de copie
* <p>
*
* @param result
* le FilebasedClusteringResult a copier
*/
public ImagebasedClusteringResult(ImagebasedClusteringResult result) {
this.filePath = result.getFilePath();
loadFromFile();
this.clusters = result.getClusters();
this.weights = (ClassificationWeights) result.weights.clone();
this.randomColor();
}
/** */
private static final long serialVersionUID = 1L;
@Override
public Object clone() {
return new ImagebasedClusteringResult(this);
}
@Override
public void deleteEmptyClusters() {
}
@Override
public int getClass(int o) {
return clusterMap[o/clusterMap.length][o % clusterMap.length] & 0xFF;
}
@Override
public int[] getClusterMap() {
int[] result = new int[clusterMap.length*clusterMap[0].length];
int index = 0;
for (int j = 0 ; j <clusterMap[0].length ; j++) {
for (int i = 0 ; i < clusterMap.length ; i++) {
result[index] = clusterMap[i][j] & 0xFF;
index++;
}
}
return result;
}
@Override
public void setClusterMap(int[] clusterMap) {
if (clusterMap.length != this.clusterMap.length*this.clusterMap[0].length) {
new FormatException("Les nouvelles valeurs de cluster ne respectent pas les "
+ "dimensions de l'image en paramètre").printStackTrace();
}
int index = 0;
for (int j = 0 ; j < this.clusterMap[0].length ; j++) {
for (int i = 0 ; i < this.clusterMap.length ; i++) {
this.clusterMap[i][j] = (byte) clusterMap[index];
index++;
}
}
}
@Override
public int getNbObjects() {
if (clusterMap == null) {
return 0;
}
return clusterMap.length*clusterMap[0].length;
}
@Override
public void flushMemory() throws MethodNotImplementedException {
this.clusterMap = null;
}
/**
* <p>
* Renvoi le fichier qui contient le resultat de la classification
* <p>
*
* @return le chemin d'acces du fichier
*/
public String getFilePath() {
return filePath;
}
/**
* <p>
* Indique le fichier contenatn le resulat de la classification
* <p>
*
* @param filePath
* Le chemin d'acces du fichier
*/
public void setFilePath(String filePath) {
if (this.filePath != filePath) {
this.filePath = filePath;
loadFromFile();
}
}
private void loadFromFile() {
if (clusterMap == null) {
if (filePath != "") {
ImageReaderWrapper reader = new ImageReaderWrapper();
try {
reader.setImage(filePath);
pixelType = reader.getPixelType();
clusterMap = reader.get1BytePixels(0, 0, reader.getImageWidth(), reader.getImageHeight());
} catch (FormatException | IOException e) {
e.printStackTrace();
}
}
}
}
}
package jcl.clustering;
import jcl.data.Data;
import jcl.data.DataObject;
import jcl.data.mask.IntArrayMask;
import jcl.data.mask.IntArrayMask.IntMaskIterator;
import jcl.data.mask.Mask;
import jj2000.j2k.NotImplementedError;
public class LightCluster extends Cluster {
/** */
private static final long serialVersionUID = 1L;
/** Le resultat de la classification qui contient ce cluster */
private final ClusteringResult result;
/** L'index qui correspond a la place du cluster dans result */
private int id = -1;
/** the cluster cardinality */
private int cardinal = -1;
/** indexes used to read object in the cluster */
private int clusterIndex = 0;
/** iterator used to read object in the cluster */
private IntMaskIterator indexIterator = null;
/**
* <p>
* Constructor
* Warning ! The id should always be the index of this cluster
* in the ClusteringResult to allow compatibility with other implementations
* <p>
*
* @param result
* the ClusteringCluster that contains this cluster
* @param id
* the id of the cluster in the ClusteringResult
* @param center
* the cluster centroid
* @param card
* the cluster cardinality
*/
public LightCluster(final ClusteringResult result, int id, DataObject center, int card) {
this.result = result;
this.id = id;
this.clusterCenter = center;
this.cardinal = card;
}
/**
* <p>
* Constructor
* Warning ! The id should always be the index of this cluster
* in the ClusteringResult to allow compatibility with other implementations
* <p>
*
* @param result
* the ClusteringCluster that contains this cluster
* @param id
* the id of the cluster in the CLusteringResult
* @param center
* the cluster centroid
*/
public LightCluster(final ClusteringResult result, int id, DataObject center) {
this.result = result;
this.id = id;
this.clusterCenter = center;
}
/**
* <p>
* Constructor
* Warning ! The id should always be the index of this cluster
* in the ClusteringResult to allow compatibility with other implementations
* <p>
*
* @param result
* the ClusteringCluster that contains this cluster
* @param id
* the id of the cluster in the CLusteringResult
*/
public LightCluster(final ClusteringResult result, int id) {
this.result = result;
this.id = id;
}
/**
* <p>
* Copy Constructor
* <p>
*
* @param cluster
* the cluster to copy
*/
public LightCluster(LightCluster cluster) {
super(cluster);
this.result = cluster.getResult();
this.id = cluster.getId();
this.clusterCenter = cluster.getCenter();
}
@Override
public Object clone() {
return new LightCluster(this);
}
@Override
public double distance(int o) {
return clusterCenter.distance(result.getData().getWholeDataDataObject(o),
weights);
}
@Override
public int getCard() {
return cardinal;
}
@Override
public Data getData(boolean fromSample) {
Mask mask = new IntArrayMask(result.getClusterMap(fromSample),id, false);
return result.getData().getMaskedData(mask, fromSample);
}
@Override
public DataObject getObject(int objIndex) {
if(objIndex > clusterIndex) {
while(clusterIndex != objIndex && getIndexIterator(false).hasNext()) {
clusterIndex++;
getIndexIterator(false).next();
}
return result.getData().getWholeDataDataObject(getIndexIterator(false).current());
} else if (objIndex < clusterIndex){
while(clusterIndex != objIndex && clusterIndex > 0) {
clusterIndex--;
getIndexIterator(false).previous();
}
return result.getData().getWholeDataDataObject(getIndexIterator(false).current());
}
return result.getData().getWholeDataDataObject(getIndexIterator(false).current());
}
@Override
public double getMembership(int o) {
throw new NotImplementedError("method not Implemented for LighCluster class");
}
@Override
public double getMembershipProbabilityEM() {
throw new NotImplementedError("method not Implemented for LighCluster class");
}
@Override
public void lock() {
// TODO Auto-generated method stub
}
@Override
public void unlock() {
// TODO Auto-generated method stub
}
private IntMaskIterator getIndexIterator(boolean fromSample) {
if (indexIterator == null) {
indexIterator = new IntMaskIterator(result.getClusterMap(fromSample), id, false);
}
return indexIterator;
}
/**
* <p>
* Return the id of the cluster in the ClusteringResult
* <p>
*
* @return the cluster id
*/
public int getId() {
return id;
}
/**
* <p>
* Set the id of the cluster in the ClusteringResult
* <p>
*
* @param index
* the cluster id
*/
public void setId(int id) {
this.id = id;
}
/**
* <p>
* The ClusteringResult that contains this cluster
* <p>
*
* @return the ClusteringResult
*/
public ClusteringResult getResult() {
return result;
}
@Override
public DataObject getCenter() {
return clusterCenter;
}
}
package jcl.clustering;
import java.util.Iterator;
import jcl.data.Data;
import jcl.data.DataObject;
import jcl.data.SimpleData;
import jcl.weights.Weights;
/**
* <p>
* Implementation simple de Cluster qui reprend les developpements avant abstraction
* </p>
*
* @author Baptiste Lafabregue
*/
public class SimpleCluster extends Cluster {
/** */
private static final long serialVersionUID = 1L;
/** Les donnees appartenant aéé& ce cluster */
private Data data = null;
/** Le degre d'appartenance de chaque objet a ce cluster */
private double membershipDegree[] = null;
/** Probabilite de la classe (pour EM) */
private double membershipProbabilityEM = 0;
/**
* <p>
* Constructeur de copie
* </p>
*
* @param cluster
* une classe
*/
public SimpleCluster(final SimpleCluster cluster) {
super(cluster);
this.data = (Data) cluster.data.clone();
if (cluster.clusterCenter == null) {
this.clusterCenter = this.computeCenter();
}
this.membershipDegree = new double[cluster.membershipDegree.length];
for (int i = 0; i < this.membershipDegree.length; i++) {
this.membershipDegree[i] = cluster.getMembership(i);
}
this.membershipProbabilityEM = cluster.membershipProbabilityEM;
}
/**
* <p>
* Constructeur. (principal)
* </p>
*
* @param data
* l'ensemble de donnees d'o� est extraite la classe
* @param clusterMap
* la carte de classification
* @param k
* l'index de la classe
* @param weights
* le vecteur de poid associe a la classe
* @param membershipDegree
* le degre d'appartenance de tous les objets a la classe
* @param proba
* probabilite qu'un objet appartienne a cette classe
*/
public SimpleCluster(final Data data, final int clusterMap[], final int k,
final Weights weights, final double membershipDegree[],
final double proba) {
int c = 0;
this.weights = weights;
this.membershipDegree = new double[clusterMap.length];
if (membershipDegree != null) {
for (int i = 0; i < clusterMap.length; i++) {
if (clusterMap[i] == k) {
c++;
}
this.membershipDegree[i] = membershipDegree[i];
}
} else {
for (int i = 0; i < clusterMap.length; i++) {
if (clusterMap[i] == k) {
c++;
this.membershipDegree[i] = 1.;
} else {
this.membershipDegree[i] = 0.;
}
}
}
// TODO this.data = new Data(c, data.getStructure());
this.data = new SimpleData(c);
this.data.setComments(data.getComments());
this.data.setDataName(data.getDataName());
this.data.setAttributesNames(data.getAttributesNames());
c = 0;
Iterator<DataObject> iter = data.iterator(0, clusterMap.length);
int i = 0;
while(iter.hasNext()) {
DataObject current = iter.next();
if (clusterMap[i] == k) {
this.data.setDataObject(c, current);
c++;
}
i++;
}
if (this.data.getNbObjects() != 0) {
this.clusterCenter = this.computeCenter();