Skip to content

Commit 09087ac

Browse files
committed
add helper function to return region predictions
add a helper function to return the set of column predictions in the region (ANSI C HTM)
1 parent 5f71784 commit 09087ac

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

src/HTM/c_ansi/Region.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,42 @@ void deleteRegion(Region* region) {
414414
region->inputCells = NULL;
415415
}
416416

417+
/**
418+
* Populate the outData array with the current prediction values for each
419+
* column in the Region. The array must be have a size equal to the number of
420+
* columns in the Region (region->numCols). The value returned for a column
421+
* represents the fewest number of time steps the column believes an activation
422+
* will occur. For example a 1 value means the column is predicting it will
423+
* become active in the very next time step (t+1). A value of 2 means it expects
424+
* activation in 2 time steps (t+2) etc. A value of 0 means the column is not
425+
* currently making any prediction.
426+
* @param outData this array will be populated with the prediction values for
427+
* each column in the region based on the most recently processed time step.
428+
* This array must be have length equal to the number of columns in the
429+
* region.
430+
*/
431+
void getColumnPredictions(Region* region, char* outData) {
432+
int i,j;
433+
for(i=0; i<region->numCols; ++i) {
434+
Column* col = &(region->columns[i]);
435+
outData[i] = 0;
436+
437+
/*if a column has multiple predicting cells, find the one that is making
438+
the prediction that will happen the earliest and store that value*/
439+
bool colOK = false;
440+
char p = MAX_TIME_STEPS;
441+
for(j=0; j<col->numCells; ++j) {
442+
Cell* cell = &(col->cells[j]);
443+
if(cell->isPredicting && cell->predictionSteps < p) {
444+
p = cell->predictionSteps;
445+
colOK = true;
446+
}
447+
}
448+
if(colOK)
449+
outData[(col->cy*region->width)+col->cx] = p;
450+
}
451+
}
452+
417453
/**
418454
* Calculate both the activation accuracy and the prediction accuracy for all
419455
* the column cells in this region within the last processed time step.

src/HTM/c_ansi/Region.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Region* newRegion(int inputSizeX, int inputSizeY, int colGridSizeX, int colGridS
5252
float pctLocalActivity, int cellsPerCol, int segActiveThreshold,
5353
int newSynapseCount, char* inputData);
5454
void deleteRegion(Region* region);
55+
void getColumnPredictions(Region* region, char* outData);
5556
void getLastAccuracy(Region* region, float* result);
5657
int numRegionSegments(Region* region, int predictionSteps);
5758
int numRegionActiveColumns(Region* region);

src/HTM/c_ansi/UnitTests.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ void testRegion1() {
284284

285285
deleteRegion(region);
286286
free(region);
287+
free(data);
287288

288289
printf("OK\n");
289290
}
@@ -332,6 +333,7 @@ void testRegion2() {
332333

333334
deleteRegion(region);
334335
free(region);
336+
free(data);
335337

336338
printf("OK\n");
337339
}
@@ -365,6 +367,8 @@ void testRegion3() {
365367
int segActiveThreshold = 10;
366368
int newSynapseCount = 10;
367369

370+
char* outData = malloc(colGridSizeX * colGridSizeY);
371+
368372
Region* region = newRegion(inputSizeX, inputSizeY, colGridSizeX, colGridSizeY,
369373
pctInputPerCol, pctMinOverlap, localityRadius, pctLocalActivity, cellsPerCol,
370374
segActiveThreshold, newSynapseCount, data);
@@ -388,6 +392,21 @@ void testRegion3() {
388392
int nc = numRegionActiveColumns(region);
389393
if(DEBUG)
390394
printf(" nc:%d", nc);
395+
396+
/*Get the current column predictions. outData is size 32x32 to match the
397+
* column grid. each value represents whether the column is predicted to
398+
* happen soon. a value of 1 indicates the column is predicted to be active
399+
* in t+1, value of 2 for t+2, etc. value of 0 indicates column is not
400+
* being predicted any time soon. */
401+
getColumnPredictions(region, outData);
402+
int p,n1=0, n2=0, n3=0;
403+
for(p=0; p<region->numCols; ++p) {
404+
n1 += outData[p]==1 ? 1 : 0;
405+
n2 += outData[p]==2 ? 1 : 0;
406+
n3 += outData[p]==3 ? 1 : 0;
407+
}
408+
if(DEBUG)
409+
printf(" np:%i %i %i", n1, n2, n3);
391410
}
392411
if(DEBUG)
393412
printf("\n");
@@ -397,6 +416,8 @@ void testRegion3() {
397416

398417
deleteRegion(region);
399418
free(region);
419+
free(data);
420+
free(outData);
400421

401422
printf("OK\n");
402423
}
@@ -539,6 +560,7 @@ void testRegionPerformance(unsigned int nunique) {
539560

540561
deleteRegion(region);
541562
free(region);
563+
free(data);
542564

543565
printf("OK\n");
544566
}
@@ -677,6 +699,7 @@ void testRegionPerformanceDickens() {
677699

678700
deleteRegion(region);
679701
free(region);
702+
free(data);
680703

681704
printf("OK\n");
682705
}

0 commit comments

Comments
 (0)