Search in sources :

Example 46 with AbortException

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)

Example 47 with AbortException

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;
}
Also used : WritableIntegerDataStore(de.lmu.ifi.dbs.elki.database.datastore.WritableIntegerDataStore) NotImplementedException(de.lmu.ifi.dbs.elki.utilities.exceptions.NotImplementedException) Clustering(de.lmu.ifi.dbs.elki.data.Clustering) MedoidModel(de.lmu.ifi.dbs.elki.data.model.MedoidModel) StringStatistic(de.lmu.ifi.dbs.elki.logging.statistics.StringStatistic) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)

Example 48 with AbortException

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() + ".");
    }
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) IOException(java.io.IOException) Int2IntOpenHashMap(it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap) ClusterModel(de.lmu.ifi.dbs.elki.data.model.ClusterModel) Model(de.lmu.ifi.dbs.elki.data.model.Model) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)

Example 49 with AbortException

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);
}
Also used : Database(de.lmu.ifi.dbs.elki.database.Database) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)

Example 50 with AbortException

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);
}
Also used : Database(de.lmu.ifi.dbs.elki.database.Database) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException) Result(de.lmu.ifi.dbs.elki.result.Result)

Aggregations

AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)99 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)25 IOException (java.io.IOException)24 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)22 ArrayList (java.util.ArrayList)16 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)13 MultipleObjectsBundle (de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle)13 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)10 DBIDArrayIter (de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter)9 DoubleRelation (de.lmu.ifi.dbs.elki.database.relation.DoubleRelation)9 Clustering (de.lmu.ifi.dbs.elki.data.Clustering)8 Model (de.lmu.ifi.dbs.elki.data.model.Model)8 VectorFieldTypeInformation (de.lmu.ifi.dbs.elki.data.type.VectorFieldTypeInformation)8 Database (de.lmu.ifi.dbs.elki.database.Database)8 WritableDoubleDataStore (de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore)8 DBIDRange (de.lmu.ifi.dbs.elki.database.ids.DBIDRange)8 OutlierResult (de.lmu.ifi.dbs.elki.result.outlier.OutlierResult)8 MaterializedDoubleRelation (de.lmu.ifi.dbs.elki.database.relation.MaterializedDoubleRelation)6 ClassLabel (de.lmu.ifi.dbs.elki.data.ClassLabel)5 DoubleVector (de.lmu.ifi.dbs.elki.data.DoubleVector)5