Search in sources :

Example 1 with LabelList

use of de.lmu.ifi.dbs.elki.data.LabelList in project elki by elki-project.

the class ExternalIDFilter method filter.

@Override
public MultipleObjectsBundle filter(MultipleObjectsBundle objects) {
    MultipleObjectsBundle bundle = new MultipleObjectsBundle();
    // Find a labellist column
    boolean done = false;
    boolean keeplabelcol = false;
    for (int i = 0; i < objects.metaLength(); i++) {
        SimpleTypeInformation<?> meta = objects.meta(i);
        // Skip non-labellist columns - or if we already had a labellist
        if (done || !LabelList.class.equals(meta.getRestrictionClass())) {
            bundle.appendColumn(meta, objects.getColumn(i));
            continue;
        }
        done = true;
        // We split the label column into two parts
        List<ExternalID> eidcol = new ArrayList<>(objects.dataLength());
        List<LabelList> lblcol = new ArrayList<>(objects.dataLength());
        // Split the column
        ArrayList<String> lbuf = new ArrayList<>();
        for (Object obj : objects.getColumn(i)) {
            if (obj != null) {
                LabelList ll = (LabelList) obj;
                int off = externalIdIndex >= 0 ? externalIdIndex : (ll.size() - externalIdIndex);
                eidcol.add(new ExternalID(ll.get(off)));
                lbuf.clear();
                for (int j = 0; j < ll.size(); j++) {
                    if (j == off) {
                        continue;
                    }
                    lbuf.add(ll.get(j));
                }
                lblcol.add(LabelList.make(lbuf));
                if (ll.size() > 0) {
                    keeplabelcol = true;
                }
            } else {
                eidcol.add(null);
                lblcol.add(null);
            }
        }
        bundle.appendColumn(TypeUtil.EXTERNALID, eidcol);
        // Only add the label column when it's not empty.
        if (keeplabelcol) {
            bundle.appendColumn(meta, lblcol);
        }
    }
    return bundle;
}
Also used : ExternalID(de.lmu.ifi.dbs.elki.data.ExternalID) LabelList(de.lmu.ifi.dbs.elki.data.LabelList) MultipleObjectsBundle(de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle) ArrayList(java.util.ArrayList)

Example 2 with LabelList

use of de.lmu.ifi.dbs.elki.data.LabelList in project elki by elki-project.

the class ByLabelFilterTest method parameters.

/**
 * Test with parameter s as the label to look for.
 */
@Test
public void parameters() {
    String s = "yes";
    String filename = UNITTEST + "label-selection-test-1.csv";
    ByLabelFilter filter = // 
    new ELKIBuilder<>(ByLabelFilter.class).with(ByLabelFilter.Parameterizer.LABELFILTER_PATTERN_ID, // 
    s).build();
    MultipleObjectsBundle filteredBundle = readBundle(filename, filter);
    // Load the test data again without a filter.
    MultipleObjectsBundle unfilteredBundle = readBundle(filename);
    // Ensure the first column are the vectors.
    assertEquals("Dimensionality", getFieldDimensionality(unfilteredBundle, 0, TypeUtil.NUMBER_VECTOR_FIELD), getFieldDimensionality(filteredBundle, 0, TypeUtil.NUMBER_VECTOR_FIELD));
    // Verify that the filter selected all vectors which match the pattern.
    int count_match = 0;
    for (int row = 0; row < unfilteredBundle.dataLength(); row++) {
        LabelList ll = get(unfilteredBundle, row, 1, LabelList.class);
        if (ll.get(0).equals(s)) {
            count_match++;
        }
    }
    assertTrue("Expected at least one match", count_match > 0);
    assertEquals("Unexpected number of matches", count_match, filteredBundle.dataLength());
}
Also used : ELKIBuilder(de.lmu.ifi.dbs.elki.utilities.ELKIBuilder) LabelList(de.lmu.ifi.dbs.elki.data.LabelList) MultipleObjectsBundle(de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle) AbstractDataSourceTest(de.lmu.ifi.dbs.elki.datasource.AbstractDataSourceTest) Test(org.junit.Test)

Example 3 with LabelList

use of de.lmu.ifi.dbs.elki.data.LabelList in project elki by elki-project.

the class SortByLabelFilterTest method defaultParameters.

/**
 * Test with default parameters.
 */
@Test
public void defaultParameters() {
    String filename = UNITTEST + "label-selection-test-1.csv";
    SortByLabelFilter filter = new ELKIBuilder<>(SortByLabelFilter.class).build();
    MultipleObjectsBundle bundle = readBundle(filename, filter);
    // Expect vectors to come first, labels second.
    getFieldDimensionality(bundle, 0, TypeUtil.NUMBER_VECTOR_FIELD);
    // Verify that the vectors are in alphabetical order.
    for (int row = 0; row < bundle.dataLength() - 1; row++) {
        LabelList llFirst = get(bundle, row, 1, LabelList.class);
        LabelList llSecond = get(bundle, row + 1, 1, LabelList.class);
        assertTrue("Expected alphabetical order", llFirst.get(0).compareToIgnoreCase(llSecond.get(0)) <= 0);
    }
}
Also used : LabelList(de.lmu.ifi.dbs.elki.data.LabelList) MultipleObjectsBundle(de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle) AbstractDataSourceTest(de.lmu.ifi.dbs.elki.datasource.AbstractDataSourceTest) Test(org.junit.Test)

Example 4 with LabelList

use of de.lmu.ifi.dbs.elki.data.LabelList in project elki by elki-project.

the class EvaluateRetrievalPerformance method match.

/**
 * Test whether two relation agree.
 *
 * @param ref Reference object
 * @param test Test object
 * @return {@code true} if the objects match
 */
protected static boolean match(Object ref, Object test) {
    if (ref == null) {
        return false;
    }
    // Cheap and fast, may hold for class labels!
    if (ref == test) {
        return true;
    }
    if (ref instanceof LabelList && test instanceof LabelList) {
        final LabelList lref = (LabelList) ref;
        final LabelList ltest = (LabelList) test;
        final int s1 = lref.size(), s2 = ltest.size();
        if (s1 == 0 || s2 == 0) {
            return false;
        }
        for (int i = 0; i < s1; i++) {
            String l1 = lref.get(i);
            if (l1 == null) {
                continue;
            }
            for (int j = 0; j < s2; j++) {
                if (l1.equals(ltest.get(j))) {
                    return true;
                }
            }
        }
    }
    // Fallback to equality, e.g. on class labels
    return ref.equals(test);
}
Also used : LabelList(de.lmu.ifi.dbs.elki.data.LabelList)

Example 5 with LabelList

use of de.lmu.ifi.dbs.elki.data.LabelList in project elki by elki-project.

the class AveragePrecisionAtK method match.

/**
 * Test whether two relation agree.
 *
 * @param ref Reference object
 * @param test Test object
 * @return {@code true} if the objects match
 */
protected static boolean match(Object ref, Object test) {
    if (ref == null) {
        return false;
    }
    // Cheap and fast, may hold for class labels!
    if (ref == test) {
        return true;
    }
    if (ref instanceof LabelList && test instanceof LabelList) {
        final LabelList lref = (LabelList) ref;
        final LabelList ltest = (LabelList) test;
        final int s1 = lref.size(), s2 = ltest.size();
        if (s1 == 0 || s2 == 0) {
            return false;
        }
        for (int i = 0; i < s1; i++) {
            String l1 = lref.get(i);
            if (l1 == null) {
                continue;
            }
            for (int j = 0; j < s2; j++) {
                if (l1.equals(ltest.get(j))) {
                    return true;
                }
            }
        }
    }
    // Fallback to equality, e.g. on class labels
    return ref.equals(test);
}
Also used : LabelList(de.lmu.ifi.dbs.elki.data.LabelList)

Aggregations

LabelList (de.lmu.ifi.dbs.elki.data.LabelList)9 MultipleObjectsBundle (de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle)5 ArrayList (java.util.ArrayList)4 AbstractDataSourceTest (de.lmu.ifi.dbs.elki.datasource.AbstractDataSourceTest)2 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)2 Test (org.junit.Test)2 ClassLabel (de.lmu.ifi.dbs.elki.data.ClassLabel)1 ExternalID (de.lmu.ifi.dbs.elki.data.ExternalID)1 SimpleClassLabel (de.lmu.ifi.dbs.elki.data.SimpleClassLabel)1 BundleMeta (de.lmu.ifi.dbs.elki.datasource.bundle.BundleMeta)1 ELKIBuilder (de.lmu.ifi.dbs.elki.utilities.ELKIBuilder)1 Object2IntOpenHashMap (it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1