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.utils;
031
032import org.openimaj.feature.FeatureVector;
033import org.openimaj.feature.SparseByteFV;
034import org.openimaj.feature.SparseDoubleFV;
035import org.openimaj.feature.SparseFloatFV;
036import org.openimaj.feature.SparseIntFV;
037import org.openimaj.feature.SparseLongFV;
038import org.openimaj.feature.SparseShortFV;
039import org.openimaj.util.array.SparseByteArray;
040import org.openimaj.util.array.SparseDoubleArray;
041import org.openimaj.util.array.SparseFloatArray;
042import org.openimaj.util.array.SparseIntArray;
043import org.openimaj.util.array.SparseLongArray;
044import org.openimaj.util.array.SparseShortArray;
045
046import de.bwaldvogel.liblinear.Feature;
047import de.bwaldvogel.liblinear.FeatureNode;
048
049/**
050 * Helper methods for interoperability of OpenIMAJ types with Liblinear.
051 *
052 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
053 *
054 */
055public class LiblinearHelper {
056        /**
057         * Convert a {@link FeatureVector} to an array of {@link Feature}s.
058         *
059         * @param feature
060         *            input {@link FeatureVector}
061         * @param bias
062         *            any bias term to add. if <=0 then no term is added; otherwise
063         *            an extra element will be added to the end of the vector set to
064         *            this value.
065         * @return output {@link Feature} array
066         */
067        public static Feature[] convert(FeatureVector feature, double bias) {
068                final Feature[] out;
069                final int extra = bias <= 0 ? 0 : 1;
070                int i = 0;
071
072                if (feature instanceof SparseDoubleFV) {
073                        out = new Feature[((SparseDoubleFV) feature).values.used() + extra];
074
075                        for (final SparseDoubleArray.Entry entry : ((SparseDoubleFV) feature).getVector().entries()) {
076                                out[i++] = new FeatureNode(entry.index + 1, entry.value);
077                        }
078                } else if (feature instanceof SparseFloatFV) {
079                        out = new Feature[((SparseFloatFV) feature).values.used() + extra];
080
081                        for (final SparseFloatArray.Entry entry : ((SparseFloatFV) feature).getVector().entries()) {
082                                out[i++] = new FeatureNode(entry.index + 1, entry.value);
083                        }
084                } else if (feature instanceof SparseByteFV) {
085                        out = new Feature[((SparseByteFV) feature).values.used() + extra];
086
087                        for (final SparseByteArray.Entry entry : ((SparseByteFV) feature).getVector().entries()) {
088                                out[i++] = new FeatureNode(entry.index + 1, entry.value);
089                        }
090                } else if (feature instanceof SparseShortFV) {
091                        out = new Feature[((SparseShortFV) feature).values.used() + extra];
092
093                        for (final SparseShortArray.Entry entry : ((SparseShortFV) feature).getVector().entries()) {
094                                out[i++] = new FeatureNode(entry.index + 1, entry.value);
095                        }
096                } else if (feature instanceof SparseIntFV) {
097                        out = new Feature[((SparseIntFV) feature).values.used() + extra];
098
099                        for (final SparseIntArray.Entry entry : ((SparseIntFV) feature).getVector().entries()) {
100                                out[i++] = new FeatureNode(entry.index + 1, entry.value);
101                        }
102                } else if (feature instanceof SparseLongFV) {
103                        out = new Feature[((SparseLongFV) feature).values.used() + extra];
104
105                        for (final SparseLongArray.Entry entry : ((SparseLongFV) feature).getVector().entries()) {
106                                out[i++] = new FeatureNode(entry.index + 1, entry.value);
107                        }
108                } else {
109                        final double[] array = feature.asDoubleVector();
110                        int numZero = 0;
111
112                        for (i = 0; i < array.length; i++) {
113                                if (array[i] == 0)
114                                        numZero++;
115                        }
116
117                        out = new Feature[array.length - numZero + extra];
118
119                        int j;
120                        for (i = 0, j = 0; i < array.length; i++) {
121                                if (array[i] != 0)
122                                        out[j++] = new FeatureNode(i + 1, array[i]);
123                        }
124                }
125
126                if (extra == 1) {
127                        out[out.length - 1] = new FeatureNode(feature.length() + 1, bias);
128                }
129
130                return out;
131        }
132
133        /**
134         * Convert a {@link FeatureVector} to an array of doubles using
135         * {@link FeatureVector#asDoubleVector()}.
136         *
137         * @param feature
138         *            the feature
139         * @param bias
140         *            any bias term to add. if <=0 then no term is added; otherwise
141         *            an extra element will be added to the end of the vector set to
142         *            this value.
143         * @return the double[] version of the feature
144         */
145        public static double[] convertDense(FeatureVector feature, double bias) {
146                final double[] arr = feature.asDoubleVector();
147
148                if (bias <= 0)
149                        return arr;
150
151                final double[] arr2 = new double[arr.length + 1];
152                System.arraycopy(arr, 0, arr2, 0, arr.length);
153                arr2[arr.length] = bias;
154                return arr2;
155        }
156}