Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Damir Dobric committed Jul 2, 2023
1 parent 56463d8 commit 5ae5256
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 47 deletions.
48 changes: 25 additions & 23 deletions source/NeoCortexApi/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,27 +453,6 @@ public static List<Cell> GetApicalConnectedCells(IList<Cell> sourceCells, IList<
}



/// <summary>
/// Gets the cells from Distal Segment inside the population that are synaptically connected from the population defined by sourceCells.
/// </summary>
/// <param name="sourceCells"></param>
/// <param name="destinationCells"></param>
/// <returns></returns>
public static List<Cell> GetDistalConnectedCells(IList<Cell> sourceCells, IList<Cell> destinationCells)
{
List<Cell> cennectedCells = new List<Cell>();

foreach (var cell in sourceCells)
{
cennectedCells.AddRange(GetDistalConnectedCells(cell, destinationCells));
}

return cennectedCells;
}



/// <summary>
/// Gets the cells from Apical Segment inside the population that are synaptically connected from the cell.
/// </summary>
Expand All @@ -484,11 +463,13 @@ public static List<Cell> GetApicalConnectedCells(Cell cell, IList<Cell> populati
{
List<Cell> connectedCells = new List<Cell>();

var populationSynapses = population.SelectMany(c=>c.ApicalDendrites.SelectMany(s=>s.Synapses)).ToList();
// Get all synapses in the population.
var populationSynapses = population.SelectMany(c => c.ApicalDendrites.SelectMany(s => s.Synapses)).ToList();

// Gets intersecting synapses.
var intersected = cell.ReceptorSynapses.Intersect(populationSynapses).ToList();

intersected.ForEach((s) => connectedCells.Add(population.FirstOrDefault(c => c.ApicalDendrites.Count(d => d.SegmentIndex == s.SegmentIndex && c.Index == s.SegmentParentCellIndex)>0)));
intersected.ForEach((syn) => connectedCells.Add(population.FirstOrDefault(cell => cell.ApicalDendrites.Count(seg => seg.SegmentIndex == syn.SegmentIndex && cell.Index == syn.SegmentParentCellIndex) > 0)));


//foreach (var syn in cell.ReceptorSynapses)
Expand All @@ -511,6 +492,27 @@ public static List<Cell> GetApicalConnectedCells(Cell cell, IList<Cell> populati
return connectedCells;
}

/// <summary>
/// Gets the cells from Distal Segment inside the population that are synaptically connected from the population defined by sourceCells.
/// </summary>
/// <param name="sourceCells"></param>
/// <param name="destinationCells"></param>
/// <returns></returns>
public static List<Cell> GetDistalConnectedCells(IList<Cell> sourceCells, IList<Cell> destinationCells)
{
List<Cell> cennectedCells = new List<Cell>();

foreach (var cell in sourceCells)
{
cennectedCells.AddRange(GetDistalConnectedCells(cell, destinationCells));
}

return cennectedCells;
}





/// <summary>
/// Gets the cells from Distal Segment inside the population that are synaptically connected from the cell.
Expand Down
37 changes: 23 additions & 14 deletions source/NeoCortexApi/NAA/NeuralAssociationsAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Security;
using System.Text;
Expand Down Expand Up @@ -41,18 +42,21 @@ public class NeuralAssociationAlgorithm
/// <summary>
/// Get Active Apical Segments of currentlly active cells in the area.
/// </summary>
/// <param name="activeCells">Associating population.</param>
/// <param name="associatingActCells">Associating population. If not null, then active segments of population cells are retrieved if the population cell is connected to the set of specified associating cells.</param>
/// <returns></returns>
public List<ApicalDendrite> GetActiveApicalSegments(IList<Cell> activeCells)
public List<ApicalDendrite> GetActiveApicalSegments(IList<Cell> associatingActCells = null)
{
List<ApicalDendrite> actSegs = new List<ApicalDendrite>();

var connectedCells = Helpers.GetApicalConnectedCells(activeCells, this._area.ActiveCells);
IList<Cell> connectedCells;

if(associatingActCells!=null)
connectedCells= Helpers.GetApicalConnectedCells(associatingActCells, this._area.ActiveCells);
else
connectedCells = this._area.ActiveCells;

foreach (var cell in connectedCells)
{
// cell.ApicalDendrites.Where(s => s.Synapses.Where(ss => activeCells.Contains(ss.SourceCell)))

foreach (var seg in cell.ApicalDendrites)
{
//if(seg.ParentCell)
Expand All @@ -67,18 +71,23 @@ public List<ApicalDendrite> GetActiveApicalSegments(IList<Cell> activeCells)


/// <summary>
/// Get Matchin Apical Segments of currentlly active cells in the area.
/// Segment is the mathcing one if it has less connected synapses than _cfg.ActivationThreshold and
/// Get Matching Apical Segments of currentlly active cells in the area.
/// Segment is the mathhcing one if it has less connected synapses than _cfg.ActivationThreshold and
/// more connected synapses than _cfg.MinThreshold.
/// </summary>
/// <param name="activeCells">Associating population.</param>
/// <param name="associatingActCells">Associating population. If not null, then segments of population cells are retrievd if the cell is connected to the associating cells.</param>
/// <returns></returns>
public List<ApicalDendrite> GetMatchingApicalSegments(IList<Cell> activeCells)
public List<ApicalDendrite> GetMatchingApicalSegments(IList<Cell> associatingActCells = null)
{
List<ApicalDendrite> matchSegs = new List<ApicalDendrite>();

var connectedCells = Helpers.GetApicalConnectedCells(activeCells, this._area.ActiveCells);

IList<Cell> connectedCells;

if(associatingActCells!=null)
connectedCells= Helpers.GetApicalConnectedCells(associatingActCells, this._area.ActiveCells);
else
connectedCells = this._area.ActiveCells;

foreach (var cell in connectedCells)
{
foreach (var seg in cell.ApicalDendrites)
Expand Down Expand Up @@ -637,7 +646,7 @@ protected Synapse CreateSynapse(Segment segment, Cell presynapticCell, double pe


/// <summary>
/// Calculates the synaptic energy of the segment. SUmmirizes all permanences of apical segments.
/// Calculates the synaptic energy of the segment. Summirizes all permanences of apical segments.
/// </summary>
/// <returns></returns>
public double GetApicalSynapticEnergy()
Expand All @@ -663,8 +672,8 @@ public string TraceState()

sb.AppendLine($"Iteration {_iteration}");

sb.AppendLine($"Active Apical Segments in area {this._area.Name}: {GetActiveApicalSegments(null).Count}");
sb.AppendLine($"Matching Apical Segments: {GetMatchingApicalSegments(this._area.ActiveCells).Count}"); // todo
sb.AppendLine($"Active Apical Segments in area {this._area.Name}: {GetActiveApicalSegments().Count}");
sb.AppendLine($"Matching Apical Segments: {GetMatchingApicalSegments().Count}"); // todo
sb.AppendLine($"Inactive Apical Segments: {InactiveApicalSegments.Count}");
sb.AppendLine($"Active Cells without Apical Segments: {ActiveCellsWithoutApicalSegments.Count}.");
sb.AppendLine($"Synaptic Energy = {GetApicalSynapticEnergy()}");
Expand Down
22 changes: 12 additions & 10 deletions source/UnitTestsProject/NAATests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void GetSegmentWithHighestPotentialTest(int numSegments)

/// <summary>
/// This unit tests creates two areas X and Y and it creates one SDR in each area.
/// Both SDRs represent tha set of active cells that will be associated with each other.
/// Both SDRs represent the set of active cells that will be associated with each other.
/// </summary>
/// <param name="numCells">A total number of cells in the Y area, that might learn associationas.</param>
/// <param name="numActCellsPct">A number of active cells in percent in the area Y that will learn associations.</param>
Expand Down Expand Up @@ -150,19 +150,19 @@ public void AssociateAreasTest(int numCells, double numActCellsPct)

/// <summary>
/// This test creates N SDRs inside area X and a single SDR inside area Y.
/// Then it repeats learning over all SDRs in X and make sure that all patterns are learned correctlly.
/// This test assert that new synaptic connections are created when different populations in X
/// It repeats learning over all SDRs in X and make sure that all patterns are learned correctlly.
/// This test asserts that new synaptic connections are created when different populations in X
/// connect to a single SDR in Y.
/// </summary>
/// <param name="numCells"></param>
/// <param name="numActCellsPct"></param>
[TestMethod]
[TestCategory("NAA")]

public void RepeatingManyToOneAssociationTest()
{
int numCells = 100;
double numActCellsPct = 0.03;
// double numActCellsPct = 0.03;

var cfg = UnitTestHelpers.GetHtmConfig(numCells, 1024);

Expand All @@ -181,13 +181,13 @@ public void RepeatingManyToOneAssociationTest()

// Create specific SDRs in X area, that will be associated with the single SDR from Y area.
List<long[]> srcSdrsInX = new List<long[]>();
srcSdrsInX.Add(new long[] { 10,20,30,40,50,60});
srcSdrsInX.Add(new long[] { 10, 20, 30, 40, 50, 60 });
srcSdrsInX.Add(new long[] { 100, 110, 120, 130, 140, 150 });
srcSdrsInX.Add(new long[] { 200, 210, 220, 230, 240, 250 });
srcSdrsInX.Add(new long[] { 300, 310, 320, 330, 340, 350 });

// Create a single SDR. All SDRs from X will be associated with this single SDR in Y
long[] destSdrY = new long[] { 0, 1, 2 };
long[] destSdrY = new long[] { 0, 1, 2 };

//
// Step trough all populations.
Expand All @@ -200,16 +200,18 @@ public void RepeatingManyToOneAssociationTest()

Debug.WriteLine(naa.TraceState());

Debug.WriteLine($" --- {n} ---");

//
// We train the same association between X and Y 10 times.
// for (int i = 0; i < 10; i++)
for (int i = 0; i < 10; i++)
{
naa.Compute(areaX, true);
Debug.WriteLine(naa.TraceState());
// AssertApicalSynapsePermanences(areaY, cfg.InitialPermanence + (i) * cfg.PermanenceIncrement);
}

// AssertAssociations(srcSdrsInX[n].Length, numCells, numActCellsPct, areaY, areaX, naa);
// AssertAssociations(srcSdrsInX[n].Length, numCells, numActCellsPct, areaY, areaX, naa);
}
}

Expand Down Expand Up @@ -253,7 +255,7 @@ public void AssociatePopulationssInAreasTest(int numCells, double numActCellsPct
for (int n = 0; n < srcSdrsInX.Count; n++)
{
Debug.WriteLine($"-------------- Learning Pattern {n} in areay Y. ----------------");

areaX.ActiveCellsIndicies = srcSdrsInX[n];
areaY.ActiveCellsIndicies = destSdrsY[n];

Expand Down

0 comments on commit 5ae5256

Please sign in to comment.