Commit b6e36c45 authored by lafabregue's avatar lafabregue
Browse files

update currentview to ArrayList

parent f944eb14
......@@ -3,6 +3,7 @@ package jcl.clustering;
import java.awt.Color;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;
......@@ -376,10 +377,10 @@ public abstract class ClusteringResult implements Serializable, MemoryFlush {
*
* @return les centres des classes
*/
public DataObject[] getClusterCenters() {
final DataObject clusterCenters[] = new DataObject[this.clusters.length];
public List<DataObject> getClusterCenters() {
final List<DataObject> clusterCenters = new ArrayList<DataObject>(this.clusters.length);
for (int i = 0; i < this.clusters.length; i++) {
clusterCenters[i] = this.clusters[i].getClusterCenter();
clusterCenters.add(this.clusters[i].getClusterCenter());
}
return clusterCenters;
}
......
......@@ -306,18 +306,14 @@ public class SimpleCluster extends Cluster {
* @return le l'objet à l'indice objIndex
*/
public DataObject getObject(final int objIndex) {
// gets the array of every objects in the cluster
DataObject[] objectsArray;
objectsArray = this.data.getViewDataObjects();
// check if objIndex is ok
if (objIndex >= objectsArray.length) {
if (objIndex >= this.data.getViewDataObjects().size()) {
System.out.println("error: class Cluster, method getObject");
return null;
}
// returns requested object
return objectsArray[objIndex];
return this.data.getViewDataObjects().get(objIndex);
}
/**
......
......@@ -19,7 +19,16 @@ public class LabelConstraint extends Constraint {
this.index = index;
this.classID = classID;
}
/**
* Return the id of the class of the label realtion
*
* @return the class id
*/
public int getClassID() {
return classID;
}
/**
* Return the object of the label relation
*
......
This diff is collapsed.
package jcl.data.ImageBased;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Vector;
import jcl.clustering.ClusteringResult;
import jcl.clustering.constraints.Constraint;
import jcl.data.Data;
import jcl.data.DataObject;
import jcl.data.SimpleData;
......@@ -19,7 +21,6 @@ import jcl.data.attribute.AttributeSequence;
import jcl.data.attribute.AttributeSymbolicCategorial;
import jcl.data.mask.Mask;
import jcl.utils.Images.StreamedImageReaderWrapper;
import jcl.weights.GlobalWeights;
import jcl.weights.Weights;
import jj2000.j2k.NotImplementedError;
......@@ -42,10 +43,10 @@ public class StreamedImageData extends Data {
private Vector<StreamedImageReaderWrapper> reader = null;
/** Index of the current view */
private int viewIndex = 0;
//private int viewIndex = 0;
/** Size of the current view */
private int viewSize = 0;
//private int viewSize = 0;
/** Total number of pixels in the image */
private int totalPixels = 0;
......@@ -163,18 +164,18 @@ public class StreamedImageData extends Data {
nbAttributes++;
}
}
this.currentView = new DataObject[data.currentView.length];
for (int i = 0; i < this.currentView.length; i++) {
this.currentView[i] = new DataObject(nbAttributes);
this.currentView = new ArrayList<DataObject>(data.currentView.size());
for (int i = 0; i < data.currentView.size(); i++) {
this.currentView.add(new DataObject(nbAttributes));
}
int current = 0;
for (int i = 0; i < weights.getNbAttributes(); i++) {
if (weights.getWeight(i) > 0) {
for (int j = 0; j < this.currentView.length; j++) {
this.currentView[j].setAttribute(current, (Attribute) data.currentView[j].getAttribute(i).clone());
this.currentView[j].getAttribute(current).mul(weights.getWeight(i));
for (int j = 0; j < this.currentView.size(); j++) {
this.currentView.get(j).setAttribute(current, (Attribute) data.currentView.get(j).getAttribute(i).clone());
this.currentView.get(j).getAttribute(current).mul(weights.getWeight(i));
}
current++;
......@@ -243,60 +244,60 @@ public class StreamedImageData extends Data {
return null;
}
final int nbAttributes = this.currentView[0].getNbAttributes();
final int nbAttributes = this.currentView.get(0).getNbAttributes();
final DataObject res = new DataObject(nbAttributes);
Attribute sample;
for (int a = 0; a < nbAttributes; a++) {
sample = this.currentView[0].getAttribute(a);
sample = this.currentView.get(0).getAttribute(a);
if (sample instanceof AttributeNumerical) {
double value = 0.0;
for (int i = 0; i < this.currentView.length; i++) {
value += this.currentView[i].getAttribute(a).getValue();
for (int i = 0; i < this.currentView.size(); i++) {
value += this.currentView.get(i).getAttribute(a).getValue();
}
value = value / this.currentView.length;
value = value / this.currentView.size();
res.setAttribute(a, new AttributeNumerical(value));
} else if (sample instanceof AttributeSequence) {
final AttributeSequence[] tab = new AttributeSequence[this.currentView.length];
for (int i = 0; i < this.currentView.length; i++) {
tab[i] = (AttributeSequence) this.currentView[i].getAttribute(a);
final AttributeSequence[] tab = new AttributeSequence[this.currentView.size()];
for (int i = 0; i < this.currentView.size(); i++) {
tab[i] = (AttributeSequence) this.currentView.get(i).getAttribute(a);
}
res.setAttribute(a, AttributeSequence.mean(tab));
} else if (sample instanceof AttributeMultiDimSequence) {
final AttributeMultiDimSequence[] tab = new AttributeMultiDimSequence[this.currentView.length];
for (int i = 0; i < this.currentView.length; i++) {
tab[i] = (AttributeMultiDimSequence) this.currentView[i].getAttribute(a);
final AttributeMultiDimSequence[] tab = new AttributeMultiDimSequence[this.currentView.size()];
for (int i = 0; i < this.currentView.size(); i++) {
tab[i] = (AttributeMultiDimSequence) this.currentView.get(i).getAttribute(a);
}
res.setAttribute(a, AttributeMultiDimSequence.mean(tab));
} else if (sample instanceof AttributeHardConstrainedMultiDimSequence) {
final AttributeHardConstrainedMultiDimSequence[] tab = new AttributeHardConstrainedMultiDimSequence[this.currentView.length];
for (int i = 0; i < this.currentView.length; i++) {
tab[i] = (AttributeHardConstrainedMultiDimSequence) this.currentView[i].getAttribute(a);
final AttributeHardConstrainedMultiDimSequence[] tab = new AttributeHardConstrainedMultiDimSequence[this.currentView.size()];
for (int i = 0; i < this.currentView.size(); i++) {
tab[i] = (AttributeHardConstrainedMultiDimSequence) this.currentView.get(i).getAttribute(a);
}
res.setAttribute(a, AttributeHardConstrainedMultiDimSequence.mean(tab));
} else if (sample instanceof AttributeDSCMDSequence) {
final AttributeDSCMDSequence[] tab = new AttributeDSCMDSequence[this.currentView.length];
for (int i = 0; i < this.currentView.length; i++) {
tab[i] = (AttributeDSCMDSequence) this.currentView[i].getAttribute(a);
final AttributeDSCMDSequence[] tab = new AttributeDSCMDSequence[this.currentView.size()];
for (int i = 0; i < this.currentView.size(); i++) {
tab[i] = (AttributeDSCMDSequence) this.currentView.get(i).getAttribute(a);
}
res.setAttribute(a, AttributeDSCMDSequence.mean(tab));
} else if (sample instanceof AttributeDCMDSequence) {
final AttributeDCMDSequence[] tab = new AttributeDCMDSequence[this.currentView.length];
for (int i = 0; i < this.currentView.length; i++) {
tab[i] = (AttributeDCMDSequence) this.currentView[i].getAttribute(a);
final AttributeDCMDSequence[] tab = new AttributeDCMDSequence[this.currentView.size()];
for (int i = 0; i < this.currentView.size(); i++) {
tab[i] = (AttributeDCMDSequence) this.currentView.get(i).getAttribute(a);
}
res.setAttribute(a, AttributeDCMDSequence.mean(tab));
} else if (sample instanceof AttributeHistogramSymbolique) {
final AttributeHistogramSymbolique[] tab = new AttributeHistogramSymbolique[this.currentView.length];
for (int i = 0; i < this.currentView.length; i++) {
tab[i] = (AttributeHistogramSymbolique) this.currentView[i].getAttribute(a);
final AttributeHistogramSymbolique[] tab = new AttributeHistogramSymbolique[this.currentView.size()];
for (int i = 0; i < this.currentView.size(); i++) {
tab[i] = (AttributeHistogramSymbolique) this.currentView.get(i).getAttribute(a);
}
res.setAttribute(a, AttributeHistogramSymbolique.mean(tab));
} else if (sample instanceof AttributeSymbolicCategorial) {
......@@ -314,7 +315,7 @@ public class StreamedImageData extends Data {
System.err.println("Method not yet supported for images");
//TODO : a test should be done to test if the currentView has already been loaded
//TODO : it's not supposed to be the currentview index but the image index
return this.currentView[i];
return this.currentView.get(i);
}
@Override
......@@ -328,14 +329,14 @@ public class StreamedImageData extends Data {
aux *= this.dimensions[i + 1];
index += coordonneDataObject[i] * aux;
}
return this.currentView[index];
return this.currentView.get(index);
}
@Override
public DataObject getDataObject(int x, int y) {
//TODO : a test should be done to test if the currentView has already been loaded
final int index = x * this.dimensions[1] + y;
return this.currentView[index];
return this.currentView.get(index);
}
@Override
......@@ -346,7 +347,7 @@ public class StreamedImageData extends Data {
@Override
public DataObject getOneDataObject() {
//TODO : a test should be done to test if the currentView has already been loaded
return this.currentView[0];
return this.currentView.get(0);
}
@Override
......@@ -362,7 +363,7 @@ public class StreamedImageData extends Data {
public ImageDataIterator(final StreamedImageData data) {
this.data = data;
this.cursor = 0;
this.end = data.currentView.length;
this.end = data.currentView.size();
}
......@@ -415,5 +416,29 @@ public class StreamedImageData extends Data {
// TODO Auto-generated method stub
return null;
}
@Override
public double getEstimatedMaxDistance() {
// TODO Auto-generated method stub
return 0;
}
@Override
public double getEstimatedMinDistance() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Vector<Constraint> updateAndSetConstraintsToSample(Vector<Constraint> constraints) {
// TODO Auto-generated method stub
return null;
}
@Override
public Vector<Constraint> getConstraints() {
// TODO Auto-generated method stub
return null;
}
}
package jcl.data;
import java.io.Serializable;
import java.util.Arrays;
import java.util.ArrayList;
import jcl.data.attribute.AttributeMultiDimSequence;
......@@ -31,7 +31,7 @@ public class ImageMultiDimSequenceData extends SimpleData implements Serializabl
/* creating the band names */
int nbAttributes = tabArrfData[0].getDataObject(0).getNbAttributes();
String[] attributesNames = Arrays.copyOf(tabArrfData[0].getAttributesNames(),tabArrfData[0].getAttributesNames().length);
//String[] attributesNames = Arrays.copyOf(tabArrfData[0].getAttributesNames(),tabArrfData[0].getAttributesNames().length);
String[] attNames = { "Sequence" };
this.setAttributesNames(attNames);
......@@ -44,7 +44,7 @@ public class ImageMultiDimSequenceData extends SimpleData implements Serializabl
*/
this.setLocked(new boolean[nbDataObjects]);
this.currentView = new DataObject[nbDataObjects];
this.currentView = new ArrayList<DataObject>(nbDataObjects);
/*
* les indices représentent, de gauche à droite : - le numero de l'image
......@@ -67,8 +67,8 @@ public class ImageMultiDimSequenceData extends SimpleData implements Serializabl
for (int indexCourant = 0; indexCourant < buffer.length; indexCourant++) {
// every data object has one attribute : a sequence
currentView[indexCourant] = new DataObject(1);
currentView[indexCourant].setAttribute(0,new AttributeMultiDimSequence(buffer[indexCourant]));
currentView.add(new DataObject(1));
currentView.get(indexCourant).setAttribute(0,new AttributeMultiDimSequence(buffer[indexCourant]));
this.setLocked(indexCourant, false);
}
......
This diff is collapsed.
......@@ -2,7 +2,9 @@ package jcl.data.attribute;
import static java.lang.Math.sqrt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import jcl.data.Data;
import jcl.data.DataObject;
......@@ -534,19 +536,17 @@ public class AttributeMDMRHSequence extends Attribute {
}
double[][] res = new double[SAMPLING][tupleTab[0][0].length];
DataObject[] objects = new DataObject[taille];
List<DataObject> objects = new ArrayList<DataObject>(taille);
int decalage = 0;
int index;
int index = 0;
for (int t = 0; t < tupleTab.length; t++) {
for (int i = 0; i < tupleTab[t].length; i++) {
index = decalage + i;
objects[index] = new DataObject(tupleTab[t][i].length);
objects.add(new DataObject(tupleTab[t][i].length));
for (int j = 0; j < tupleTab[t][i].length; j++) {
objects[index].setAttribute(j, new AttributeNumerical(tupleTab[t][i][j]));
objects.get(index).setAttribute(j, new AttributeNumerical(tupleTab[t][i][j]));
}
index++;
}
decalage += tupleTab[t].length;
}
Data data = new SimpleData(objects);
ParametersKmeans param = new ParametersKmeans(SAMPLING, 10, new GlobalWeights(data));
......
package jcl.data.random;
import java.util.ArrayList;
import java.util.List;
import jcl.clustering.ClusteringResult;
import jcl.data.DataObject;
import jcl.data.SimpleData;
......@@ -42,7 +45,10 @@ public class RandomData extends SimpleData {
this.setAttributesNames(names);
this.setDataName("Random data");
this.setViewDataObjects(new DataObject[params.nbObjects * nbClusters]);
List<DataObject> newView = new ArrayList<DataObject>(params.nbObjects * nbClusters);
for (int i = 0; i < params.nbObjects * nbClusters ; i++) {
newView.add(null);
}
final int result[] = new int[this.getNbObjects()];
......@@ -51,8 +57,7 @@ public class RandomData extends SimpleData {
final int index = k * params.nbObjects + i;
result[index] = k;
this.setDataObject(index, new DataObject(this.getDataObject(0)
.getNbAttributes()));
newView.add(index, new DataObject(nbAttributes));
int c = 0;
for (int j = 0; j < nbAttributes; j++) {
......@@ -69,7 +74,7 @@ public class RandomData extends SimpleData {
for (int l = 0; l < params.distributions[k][j]
.getCorrelations(); l++) {
if (l == 0) {
this.getDataObject(index)
newView.get(index)
.setAttribute(
c,
new AttributeNumerical(distribution
......@@ -77,7 +82,7 @@ public class RandomData extends SimpleData {
// this.getDataObject(index).setAttributeName(c,
// "Attribute_" + (index + 1));
} else {
this.getDataObject(index).setAttribute(
newView.get(index).setAttribute(
c,
new AttributeNumerical(NormalDistribution
.get(distribution)));
......@@ -89,6 +94,7 @@ public class RandomData extends SimpleData {
}
}
}
this.setViewDataObjects(newView);
this.setKnownResult(ClusteringResult.gerenerateDefaultClusteringResult(null, result,
new GlobalWeights(new Weights(this.getDataObject(0)
......
package jcl.data.sampling;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import jcl.data.DataObject;
import jcl.data.attribute.AttributeMultiDimSequence;
import jcl.data.attribute.AttributeNumerical;
import jcl.data.mask.Mask;
import jcl.utils.RandomizeTools;
import jcl.utils.Images.StreamedImageReaderWrapper;
import jcl.utils.exceptions.MethodNotImplementedException;
import loci.formats.FormatException;
......@@ -32,11 +35,12 @@ public class ImageSampler extends Sampler {
/** Width of the images */
private int imageWidth;
/** The offset between to element to pick */
/** The offset between to element to pick
* (used only for the interval sampling method) */
private double offset = 1;
/** The list of indexes used to build the sample */
private int[] selectedIndexes = null;
private List<Integer> selectedIndexes = null;
/** Object to read the image */
private Vector<StreamedImageReaderWrapper> readers = null;
......@@ -50,7 +54,6 @@ public class ImageSampler extends Sampler {
private int[] mandatoriesIndexes = null;
/**
* Constructor from a sequence of images
*
......@@ -171,7 +174,7 @@ public class ImageSampler extends Sampler {
// by default the sample is the whole image
setSizeByCount(dataSize);
updateRAMAvailable();
updateRAMAvailable(true);
this.geoTranslateVectors = sampler.geoTranslateVectors;
this.startX = sampler.startX;
......@@ -179,12 +182,12 @@ public class ImageSampler extends Sampler {
}
@Override
public DataObject[] getDataObjects() {
public List<DataObject> getDataObjects() {
if (mandatoriesIndexes != null && mandatoriesIndexes.length > sizeByCount) {
throw new IndexOutOfBoundsException("The number of mandatory pixels"
+ " is bigger than the expeted sample size");
}
updateRAMAvailable();
updateRAMAvailable(false);
if (readers.size() > 1) {
return getDataFromImagesSequence();
} else {
......@@ -193,7 +196,7 @@ public class ImageSampler extends Sampler {
}
@Override
public DataObject[] getDataObjects(final Mask mask) {
public List<DataObject> getDataObjects(final Mask mask) {
int mandatoryPixelCount = unmaskedPixelsCount(mandatoriesIndexes, mask);
if (mandatoryPixelCount > sizeByCount) {
throw new IndexOutOfBoundsException("The number of mandatory pixels"
......@@ -224,28 +227,37 @@ public class ImageSampler extends Sampler {
* Return the sampling from a single image
* @return the sample
*/
private DataObject[] getDataFromOneImage() {
DataObject[] result = new DataObject[sizeByCount];
double index = 0;
private List<DataObject> getDataFromOneImage() {
List<DataObject> result = new ArrayList<DataObject>(sizeByCount);
int index = 0;
int x = 0;
int y = 0;
int channelCount = readers.get(0).getChannelCount();
selectedIndexes = new int[sizeByCount];
switch (getSamplingMethod()) {
case Sampler.RANDOM_METHOD_TYPE :
selectedIndexes = RandomizeTools.getReservoirSamplingIndex(getDataSize(), sizeByCount);
break;
case Sampler.INTERVAL_METHOD_TYPE :
selectedIndexes = new ArrayList<Integer>();
for(int i = 0 ; i < sizeByCount ; i++) {
selectedIndexes.add(null);
}
break;
}
for(int i = 0; i < sizeByCount ; i++) {
x = ((int) index) % imageWidth;
y = ((int) index) / imageWidth;
selectedIndexes[i] = (int) index;
result[i] = new DataObject(channelCount);
index = getIndex(i);
x = (index) % imageWidth;
y = (index) / imageWidth;
result.add(new DataObject(channelCount));
try {
double [] pixel = readers.get(0).getPixel(x, y);
for(int c = 0 ; c < readers.get(0).getChannelCount() ; c++) {
result[i].setAttribute(c, new AttributeNumerical(pixel[c]));
result.get(i).setAttribute(c, new AttributeNumerical(pixel[c]));
}
} catch (FormatException | IOException e) {
e.printStackTrace();
}
index += offset;
}
return result;
......@@ -262,36 +274,54 @@ public class ImageSampler extends Sampler {
*
* @return the sample
*/
private DataObject[] getDataFromOneImage(final Mask mask, int mandatoryPixelCount) {
DataObject[] result = new DataObject[sizeByCount];
private List<DataObject> getDataFromOneImage(final Mask mask, int mandatoryPixelCount) {
List<DataObject> result = new ArrayList<DataObject>(sizeByCount);
int x = 0;
int y = 0;
int channelCount = readers.get(0).getChannelCount();
int tmpOffset = mask.getCarinality() / dataSize;
double tmpOffset = mask.getCarinality() / dataSize;
int iterPos = 0;
Iterator<Integer> iter = mask.getIncludedIndexes();
double index = iter.next();
selectedIndexes = new int[sizeByCount];
int index = 0;
Iterator<Integer> iter = null;
switch (getSamplingMethod()) {
case Sampler.RANDOM_METHOD_TYPE :
selectedIndexes = RandomizeTools.getReservoirSamplingIndex(getDataSize(), sizeByCount, mask);
break;
case Sampler.INTERVAL_METHOD_TYPE :
selectedIndexes = new ArrayList<Integer>();
for(int i = 0 ; i < sizeByCount ; i++) {
selectedIndexes.add(null);
}
iter = mask.getIncludedIndexes();
break;
}
for(int i = 0; i < sizeByCount ; i++) {
if (getSamplingMethod() == Sampler.INTERVAL_METHOD_TYPE) {
if(i > 0) {
iterPos += tmpOffset;
while(iterPos >= 1) {
index = iter.next();
iterPos --;
}
}
selectedIndexes.set(i, index);
} else {
index = getIndex(i);
}
x = ((int) index) % imageWidth;
y = ((int) index) / imageWidth;
result[i] = new DataObject(channelCount);
selectedIndexes[i] = (int) index;
result.add(new DataObject(channelCount));
try {
double [] pixel = readers.get(0).getPixel(x, y);
for(int c = 0 ; c < readers.get(0).getChannelCount() ; c++) {