use of de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException in project elki by elki-project.
the class PersistentPageFile method byteArrayToPage.
/**
* Reconstruct a serialized object from the specified byte array.
*
* @param array the byte array from which the object should be reconstructed
* @return a serialized object from the specified byte array
*/
private P byteArrayToPage(byte[] array) {
try {
ByteArrayInputStream bais = new ByteArrayInputStream(array);
ObjectInputStream ois = new ObjectInputStream(bais);
int type = ois.readInt();
if (type == EMPTY_PAGE) {
return null;
} else if (type == FILLED_PAGE) {
P page;
try {
page = pageclass.newInstance();
page.readExternal(ois);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new AbortException("Error instanciating an index page", e);
}
return page;
} else {
throw new IllegalArgumentException("Unknown type: " + type);
}
} catch (IOException e) {
throw new AbortException("IO Error in page file", e);
}
}
use of de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException in project elki by elki-project.
the class KMedoidsPAM method run.
/**
* Run k-medoids
*
* @param database Database
* @param relation relation to use
* @return result
*/
public Clustering<MedoidModel> run(Database database, Relation<V> relation) {
if (relation.size() <= 0) {
return new Clustering<>("PAM Clustering", "pam-clustering");
}
if (k > 0x7FFF) {
throw new NotImplementedException("PAM supports at most " + 0x7FFF + " clusters.");
}
DistanceQuery<V> distQ = DatabaseUtil.precomputedDistanceQuery(database, relation, getDistanceFunction(), LOG);
DBIDs ids = relation.getDBIDs();
// Choose initial medoids
if (LOG.isStatistics()) {
LOG.statistics(new StringStatistic(KEY + ".initialization", initializer.toString()));
}
ArrayModifiableDBIDs medoids = DBIDUtil.newArray(initializer.chooseInitialMedoids(k, ids, distQ));
if (medoids.size() != k) {
throw new AbortException("Initializer " + initializer.toString() + " did not return " + k + " means, but " + medoids.size());
}
// Setup cluster assignment store
WritableIntegerDataStore assignment = DataStoreUtil.makeIntegerStorage(ids, DataStoreFactory.HINT_HOT | DataStoreFactory.HINT_TEMP, -1);
run(distQ, ids, medoids, assignment);
ArrayModifiableDBIDs[] clusters = ClusteringAlgorithmUtil.partitionsFromIntegerLabels(ids, assignment, k);
// Wrap result
Clustering<MedoidModel> result = new Clustering<>("PAM Clustering", "pam-clustering");
for (DBIDArrayIter it = medoids.iter(); it.valid(); it.advance()) {
result.addToplevelCluster(new Cluster<>(clusters[it.getOffset()], new MedoidModel(DBIDUtil.deref(it))));
}
return result;
}
use of de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException in project elki by elki-project.
the class ClusteringVectorParser method nextEvent.
@Override
public Event nextEvent() {
if (nextevent != null) {
Event ret = nextevent;
nextevent = null;
return ret;
}
try {
while (reader.nextLineExceptComments()) {
buf1.clear();
lbl.clear();
Int2IntOpenHashMap csize = new Int2IntOpenHashMap();
String name = null;
for (; /* initialized by nextLineExceptComments() */
tokenizer.valid(); tokenizer.advance()) {
try {
int cnum = tokenizer.getIntBase10();
buf1.add(cnum);
// Update cluster sizes:
csize.addTo(cnum, 1);
} catch (NumberFormatException e) {
final String label = tokenizer.getSubstring();
lbl.add(label);
if (name == null) {
name = label;
}
}
}
if (name == null) {
name = "Cluster";
}
// Update meta on first record:
boolean metaupdate = (range == null);
if (range == null) {
range = DBIDUtil.generateStaticDBIDRange(buf1.size());
}
if (buf1.size() != range.size()) {
throw new AbortException("Clusterings do not contain the same number of elements!");
}
// Build clustering to store in the relation.
Int2ObjectOpenHashMap<ModifiableDBIDs> clusters = new Int2ObjectOpenHashMap<>(csize.size());
curclu = new Clustering<>(name, name);
for (ObjectIterator<Int2IntMap.Entry> iter = csize.int2IntEntrySet().fastIterator(); iter.hasNext(); ) {
Int2IntMap.Entry entry = iter.next();
if (entry.getIntValue() > 0) {
clusters.put(entry.getIntKey(), DBIDUtil.newArray(entry.getIntValue()));
}
}
DBIDArrayIter iter = range.iter();
for (int i = 0; i < buf1.size(); i++) {
clusters.get(buf1.getInt(i)).add(iter.seek(i));
}
for (ModifiableDBIDs cids : clusters.values()) {
curclu.addToplevelCluster(new Cluster<Model>(cids, ClusterModel.CLUSTER));
}
// Label handling.
if (!haslbl && !lbl.isEmpty()) {
haslbl = true;
metaupdate = true;
}
curlbl = LabelList.make(lbl);
if (metaupdate) {
// Force a meta update.
nextevent = Event.NEXT_OBJECT;
return Event.META_CHANGED;
}
return Event.NEXT_OBJECT;
}
return Event.END_OF_STREAM;
} catch (IOException e) {
throw new IllegalArgumentException("Error while parsing line " + reader.getLineNumber() + ".");
}
}
use of de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException in project elki by elki-project.
the class AlgorithmTabPanel method executeStep.
@Override
protected void executeStep() {
if (input.canRun() && !input.isComplete()) {
input.execute();
}
if (!input.isComplete()) {
throw new AbortException("Input data not available.");
}
// Get the database and run the algorithms
Database database = input.getInputStep().getDatabase();
algorithms.runAlgorithms(database);
basedOnDatabase = new WeakReference<Object>(database);
}
use of de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException in project elki by elki-project.
the class OutputTabPanel method executeStep.
@Override
protected void executeStep() {
if (input.canRun() && !input.isComplete()) {
input.execute();
}
if (evals.canRun() && !evals.isComplete()) {
evals.execute();
}
if (!input.isComplete()) {
throw new AbortException("Input data not available.");
}
if (!evals.isComplete()) {
throw new AbortException("Evaluation failed.");
}
// Get the database and run the algorithms
Database database = input.getInputStep().getDatabase();
outs.runResultHandlers(database.getHierarchy(), database);
Result eres = evals.getEvaluationStep().getResult();
basedOnResult = new WeakReference<Object>(eres);
}
Aggregations