• 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/PassThroughEncoder.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
package org.numenta.nupic.encoders;
23

24
import gnu.trove.list.array.TDoubleArrayList;
25

26
import java.util.*;
27

28
import org.numenta.nupic.util.*;
29

30
/**
31
 * Pass an encoded SDR straight to the model.
32
 * Each encoding is an SDR in which w out of n bits are turned on.
33
 * @author wilsondy (from Python original)
34
 *
35
 */
36
public class PassThroughEncoder extends Encoder<int[]> {
37
        
38
        /**
39
         * This is used to check that there are exactly outputBitsOn in the outgoing bits
40
         * The Python claims to do more, but I don't think it actually does anything other than throw an error
41
         * as we do here also. (This is w in the Python code)
42
         */
43
        private Integer outputBitsOnCount;
44
        
45
        protected PassThroughEncoder() {}
×
46

47
        public PassThroughEncoder(int outputWidth, Integer outputBitsOnCount) {
×
48
                super.setW(outputWidth);
×
49
                super.setN(outputWidth);
×
50
                super.setForced(false);
×
51
                this.outputBitsOnCount = outputBitsOnCount;
×
52
        }
×
53
        
54
        /**
55
         * Returns a builder for building PassThroughEncoders. 
56
         * This builder may be reused to produce multiple builders
57
         * 
58
         * @return a {@code PassThroughEncoder.Builder}
59
         */
60
        public static Encoder.Builder<PassThroughEncoder.Builder, PassThroughEncoder> builder() {
61
                return new PassThroughEncoder.Builder();
×
62
        }
63
        
64
        public void init() {
65
                setForced(false);
×
66
                this.outputBitsOnCount = getW() > 0 ? getW() : null;
×
67
        }
×
68
        
69
        @Override
70
        /**
71
         * Does a bitwise compare of the two bitmaps and returns a fractional 
72
         * value between 0 and 1 of how similar they are.
73
         * 1 => identical
74
         * 0 => no overlapping bits
75
         * IGNORES difference in length (only compares bits of shorter list)  e..g 11 and 1100101010 are "identical"
76
         * @see org.numenta.nupic.encoders.Encoder#closenessScores(gnu.trove.list.TDoubleList, gnu.trove.list.TDoubleList, boolean)
77
         */
78
        public gnu.trove.list.TDoubleList closenessScores(gnu.trove.list.TDoubleList expValues, gnu.trove.list.TDoubleList actValues, boolean fractional) {
79
                TDoubleArrayList result = new TDoubleArrayList();
×
80

81
                double ratio = 1.0d;
×
82
                double expectedSum = expValues.sum();
×
83
                double actualSum = actValues.sum();        
×
84

85
                if (actualSum > expectedSum) {
×
86
                        double diff = actualSum - expectedSum;
×
87
                        if (diff < expectedSum)
×
88
                                ratio = 1 - diff / expectedSum;
×
89
                        else
90
                                ratio = 1 / diff;
×
91
                }
92

93
                int[] expectedInts = ArrayUtils.toIntArray(expValues.toArray());
×
94
                int[] actualInts = ArrayUtils.toIntArray(actValues.toArray());
×
95

96
                int[] overlap = ArrayUtils.and(expectedInts, actualInts);
×
97

98
                int overlapSum = ArrayUtils.sum(overlap);
×
99
                double r = 0.0;
×
100
                if (expectedSum == 0)
×
101
                        r = 0.0;
×
102
                else
103
                        r = overlapSum / expectedSum;
×
104
                r = r * ratio;
×
105

106
                result.add(r);
×
107
                return result;
×
108
        }
109
        
110
        @Override
111
        public int getWidth() {
112
                return w;
×
113
        }
114

115
        @Override
116
        public boolean isDelta() {
117
                return false;
×
118
        }
119

120
        
121

122
        /**
123
         * Check for length the same and copy input into output
124
         * If outputBitsOnCount (w) set, throw error if not true
125
         * @param input 
126
         * @param output
127
         */
128
        public void encodeIntoArray(int[] input, int[] output){
129
                if( input.length != output.length)
130
                        throw new IllegalArgumentException(String.format("Different input (%i) and output (%i) sizes", input.length, output.length));
×
131
                if(this.outputBitsOnCount != null && ArrayUtils.sum(input) != outputBitsOnCount)
×
132
                        throw new IllegalArgumentException(String.format("Input has %i bits but w was set to %i.",  ArrayUtils.sum(input), outputBitsOnCount));
×
133
                
×
134
                System.arraycopy(input, 0, output, 0, input.length);
135

×
136
        }
137
        
×
138
        /**
139
         * Not much real work to do here as this concept doesn't really apply.
140
         */
141
        public Tuple decode(int[] encoded, String parentFieldName) {
142
            //TODO: these methods should be properly implemented (this comment in Python)                  
143
                String fieldName = this.name;
144
            if (parentFieldName != null && parentFieldName.length() >0)
145
                    String.format("%s.%s", parentFieldName, this.name);
×
146

×
147
                List<MinMax> ranges = new ArrayList<MinMax>();
×
148
                ranges.add(new MinMax(0,0));
149
            RangeList inner = new RangeList(ranges, "input");
×
150
                Map<String, RangeList> fieldsDict = new HashMap<String, RangeList>();
×
151
                fieldsDict.put(fieldName, inner);
×
152
                
×
153
            //return ({fieldName: ([[0, 0]], "input")}, [fieldName])
×
154
                return new DecodeResult(fieldsDict, Arrays.asList(new String[] { fieldName }));
155

156

×
157
        }
158

159
        @Override
160
        public void setLearning(boolean learningEnabled) {
161
                //NOOP                
162
        }
×
163

164
        @Override
165
        public <T> List<T> getBucketValues(Class<T> returnType) { 
166
                return null;
×
167
        }
168

169
        /**
170
         * Returns a {@link EncoderBuilder} for constructing {@link PassThroughEncoder}s
171
         * 
172
         * The base class architecture is put together in such a way where boilerplate
173
         * initialization can be kept to a minimum for implementing subclasses, while avoiding
174
         * the mistake-proneness of extremely long argument lists.
175
         * 
176
         */
177
        public static class Builder extends Encoder.Builder<PassThroughEncoder.Builder, PassThroughEncoder> {
178
                private Builder() {}
×
179

180
                @Override
181
                public PassThroughEncoder build() {
182
                        //Must be instantiated so that super class can initialize 
183
                        //boilerplate variables.
184
                        encoder = new PassThroughEncoder();
×
185
                        
186
                        //Call super class here
187
                        super.build();
×
188
                        
189
                        ////////////////////////////////////////////////////////
190
                        //  Implementing classes would do setting of specific //
191
                        //  vars here together with any sanity checking       //
192
                        ////////////////////////////////////////////////////////
193
                        
194
                        ((PassThroughEncoder)encoder).init();
×
195
                        
196
                        return (PassThroughEncoder)encoder;
×
197
                }
198
        }
199
}
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