001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.ml.annotation.basic;
031
032import gnu.trove.map.hash.TIntIntHashMap;
033
034import java.util.ArrayList;
035import java.util.Collection;
036import java.util.HashSet;
037import java.util.List;
038import java.util.Set;
039
040import org.openimaj.ml.annotation.Annotated;
041import org.openimaj.ml.annotation.BatchAnnotator;
042import org.openimaj.ml.annotation.ScoredAnnotation;
043import org.openimaj.ml.annotation.basic.util.NumAnnotationsChooser;
044
045import cern.jet.random.Uniform;
046import cern.jet.random.engine.MersenneTwister;
047
048/**
049 * An annotator that chooses annotations completely randomly from the set of all
050 * known annotations. The number of annotations produced is set by the type of
051 * {@link NumAnnotationsChooser} used.
052 * 
053 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
054 * 
055 * @param <OBJECT>
056 *            Type of object being annotated
057 * @param <ANNOTATION>
058 *            Type of annotation.
059 */
060public class UniformRandomAnnotator<OBJECT, ANNOTATION> extends BatchAnnotator<OBJECT, ANNOTATION> {
061        protected List<ANNOTATION> annotations;
062        protected NumAnnotationsChooser numAnnotations;
063        protected Uniform rnd;
064
065        /**
066         * Construct with the given {@link NumAnnotationsChooser} to determine how
067         * many annotations are produced by calls to {@link #annotate(Object)}.
068         * 
069         * @param chooser
070         *            the {@link NumAnnotationsChooser} to use.
071         */
072        public UniformRandomAnnotator(NumAnnotationsChooser chooser) {
073                this.numAnnotations = chooser;
074        }
075
076        @Override
077        public void train(List<? extends Annotated<OBJECT, ANNOTATION>> data) {
078                final HashSet<ANNOTATION> annotationsSet = new HashSet<ANNOTATION>();
079                final TIntIntHashMap nAnnotationCounts = new TIntIntHashMap();
080                int maxVal = 0;
081
082                for (final Annotated<OBJECT, ANNOTATION> sample : data) {
083                        final Collection<ANNOTATION> annos = sample.getAnnotations();
084                        annotationsSet.addAll(annos);
085                        nAnnotationCounts.adjustOrPutValue(annos.size(), 1, 1);
086
087                        if (annos.size() > maxVal)
088                                maxVal = annos.size();
089                }
090
091                annotations = new ArrayList<ANNOTATION>(annotationsSet);
092
093                rnd = new Uniform(0, annotations.size() - 1, new MersenneTwister());
094
095                numAnnotations.train(data);
096        }
097
098        @Override
099        public List<ScoredAnnotation<ANNOTATION>> annotate(OBJECT image) {
100                final int nAnnotations = numAnnotations.numAnnotations();
101
102                final List<ScoredAnnotation<ANNOTATION>> annos = new ArrayList<ScoredAnnotation<ANNOTATION>>();
103
104                for (int i = 0; i < nAnnotations; i++) {
105                        final int annotationIdx = rnd.nextInt();
106                        annos.add(new ScoredAnnotation<ANNOTATION>(annotations.get(annotationIdx), 1.0f / annotations.size()));
107                }
108
109                return annos;
110        }
111
112        @Override
113        public Set<ANNOTATION> getAnnotations() {
114                return new HashSet<ANNOTATION>(annotations);
115        }
116}