• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

numenta / htm.java / #1206

26 Jan 2015 12:57PM UTC coverage: 14.404% (-0.005%) from 14.409%
#1206

push

David Ray
Merge pull request #168 from cogmission/network_api_work

testing the RNG is not part of the scope

714 of 4957 relevant lines covered (14.4%)

0.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/numenta/nupic/encoders/CoordinateEncoder.java
1
/* ---------------------------------------------------------------------
2
 * Numenta Platform for Intelligent Computing (NuPIC)
3
 * Copyright (C) 2014, Numenta, Inc.  Unless you have an agreement
4
 * with Numenta, Inc., for a separate license for this software code, the
5
 * following terms and conditions apply:
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License version 3 as
9
 * published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 * See the GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see http://www.gnu.org/licenses.
18
 *
19
 * http://numenta.org/licenses/
20
 * ---------------------------------------------------------------------
21
 */
22

23
package org.numenta.nupic.encoders;
24

25
import org.numenta.nupic.util.ArrayUtils;
26
import org.numenta.nupic.util.MersenneTwister;
27
import org.numenta.nupic.util.SortablePair;
28
import org.numenta.nupic.util.Tuple;
29

30
import java.util.ArrayList;
31
import java.util.Arrays;
32
import java.util.List;
33

34
public class CoordinateEncoder extends Encoder<Tuple> implements CoordinateOrder {
35
        private static MersenneTwister random = new MersenneTwister();
×
36

37
        /**
38
         * Package private to encourage construction using the Builder Pattern
39
         * but still allow inheritance.
40
         */
41
        CoordinateEncoder() {
×
42
        /*
43
        *description has a {@link List} of {@link Tuple}s containing
44
        */
45
        Tuple desc = new Tuple(2, "coordinate", 0);
×
46
        Tuple desc2 = new Tuple(2, "radius", 1);
×
47
        description.add(desc);
×
48
        description.add(desc2);
×
49
    }
×
50

51
        /**
52
         * @see Encoder for more information
53
         */
54
        @Override
55
        public int getWidth() {
56
                return n;
×
57
        }
58

59
        /**
60
         * @see Encoder for more information
61
         */
62
        @Override
63
        public boolean isDelta() {
64
                return false;
×
65
        }
66

67

68
        /**
69
         * Returns a builder for building ScalarEncoders.
70
         * This builder may be reused to produce multiple builders
71
         *
72
         * @return a {@code CoordinateEncoder.Builder}
73
         */
74
        public static Encoder.Builder<CoordinateEncoder.Builder, CoordinateEncoder> builder() {
75
                return new CoordinateEncoder.Builder();
×
76
        }
77

78
        /**
79
         * Returns coordinates around given coordinate, within given radius.
80
     * Includes given coordinate.
81
     *
82
         * @param coordinate        Coordinate whose neighbors to find
83
         * @param radius                Radius around `coordinate`
84
         * @return
85
         */
86
        public List<int[]> neighbors(int[] coordinate, double radius) {
87
                int[][] ranges = new int[coordinate.length][];
×
88
                for(int i = 0;i < coordinate.length;i++) {
×
89
                        ranges[i] = ArrayUtils.range(coordinate[i] - (int)radius, coordinate[i] + (int)radius + 1);
×
90
                }
91

92
                List<int[]> retVal = new ArrayList<int[]>();
×
93
                int len = ranges.length == 1 ? 1 : ranges[0].length;
×
94
                for(int k = 0;k < ranges[0].length;k++) {
×
95
                        for(int j = 0;j < len;j++) {
×
96
                                int[] entry = new int[ranges.length];
×
97
                                entry[0] = ranges[0][k];
×
98
                                for(int i = 1;i < ranges.length;i++) {
×
99
                                        entry[i] = ranges[i][j];
×
100
                                }
101
                                retVal.add(entry);
×
102
                        }
103
                }
104
                return retVal;
×
105
        }
106

107
        /**
108
         * Returns the top W coordinates by order.
109
         *
110
         * @param co                        Implementation of {@link CoordinateOrder}
111
         * @param coordinates        A 2D array, where each element
112
                            is a coordinate
113
         * @param w                                (int) Number of top coordinates to return
114
         * @return
115
         */
116
        @SuppressWarnings("unchecked")
117
        public int[][] topWCoordinates(CoordinateOrder co, int[][] coordinates, int w) {
118
                SortablePair<Double, Integer>[] pairs = new SortablePair[coordinates.length];
×
119
                for(int i = 0; i < coordinates.length;i++) {
×
120
                    pairs[i] = new SortablePair<Double, Integer>(co.orderForCoordinate(coordinates[i]), i);
×
121
                }
122

123
                Arrays.sort(pairs);
×
124

125
                int[][] topCoordinates = new int[w][];
×
126
                for(int i = 0, wIdx = pairs.length - w; i < w; i++, wIdx++) {
×
127
                    int index = pairs[wIdx].second();
×
128
                    topCoordinates[i] = coordinates[index];
×
129
                }
130
                return topCoordinates;
×
131
        }
132

133
        /**
134
         * Returns the order for a coordinate.
135
         *
136
         * @param coordinate        coordinate array
137
         *
138
         * @return        A value in the interval [0, 1), representing the
139
     *          order of the coordinate
140
         */
141
        public double orderForCoordinate(int[] coordinate) {
142
                random.setSeed(coordinate);
143
                return random.nextDouble();
×
144
        }
×
145

146
        /**
147
         * Returns the order for a coordinate.
148
         *
149
         * @param coordinate        coordinate array
150
         * @param n                                the number of available bits in the SDR
151
         *
152
         * @return        The index to a bit in the SDR
153
         */
154
        public static int bitForCoordinate(int[] coordinate, int n) {
155
                random.setSeed(coordinate);
156
                return random.nextInt(n);
×
157
        }
×
158

159
        /**
160
         * {@inheritDoc}
161
         */
162
        @Override
163
        public void encodeIntoArray(Tuple inputData, int[] output) {
164
                List<int[]> neighs = neighbors((int[])inputData.get(0), (double)inputData.get(1));
165
                int[][] neighbors = new int[neighs.size()][];
×
166
                for(int i = 0;i < neighs.size();i++) neighbors[i] = neighs.get(i);
×
167

×
168
                int[][] winners = topWCoordinates(this, neighbors, w);
169

×
170
                for(int i = 0;i < winners.length;i++) {
171
                        int bit = bitForCoordinate(winners[i], n);
×
172
                        output[bit] = 1;
×
173
                }
×
174
        }
175

×
176
        @Override
177
        public <T> List<T> getBucketValues(Class<T> returnType) {
178
                return null;
179
        }
×
180

181
        /**
182
         * Returns a {@code Builder} for constructing {@link CoordinateEncoder}s
183
         *
184
         * The base class architecture is put together in such a way where boilerplate
185
         * initialization can be kept to a minimum for implementing subclasses, while avoiding
186
         * the mistake-proneness of extremely long argument lists.
187
         *
188
         * @see ScalarEncoder.Builder#setStuff(int)
189
         */
190
        public static class Builder extends Encoder.Builder<CoordinateEncoder.Builder, CoordinateEncoder> {
191
                private Builder() {}
192

×
193
                @Override
194
                public CoordinateEncoder build() {
195
                        //Must be instantiated so that super class can initialize
196
                        //boilerplate variables.
197
                        encoder = new CoordinateEncoder();
198

×
199
                        //Call super class here
200
                        super.build();
201

×
202
                        ////////////////////////////////////////////////////////
203
                        //  Implementing classes would do setting of specific //
204
                        //  vars here together with any sanity checking       //
205
                        ////////////////////////////////////////////////////////
206

207
                        if(w <= 0 || w % 2 == 0) {
208
                                throw new IllegalArgumentException("w must be odd, and must be a positive integer");
×
209
                        }
×
210

211
                        if(n <= 6 * w) {
212
                                throw new IllegalArgumentException(
×
213
                                        "n must be an int strictly greater than 6*w. For " +
×
214
                       "good results we recommend n be strictly greater than 11*w");
215
                        }
216

217
                        if(name == null || name.equals("None")) {
218
                                name = new StringBuilder("[").append(n).append(":").append(w).append("]").toString();
×
219
                        }
×
220

221
                        return (CoordinateEncoder)encoder;
222
                }
×
223
        }
224
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc