示例#1
0
def vizInterSequenceClusters(traces, outputDir, cellsType, numCells, 
                             numClasses, ignoreNoise=True):
  clusters = assignClusters(traces)

  # compare inter-cluster distance over time
  numRptsPerCategory = {}
  categories = np.unique(traces['actualCategory'])
  repetition = np.array(clusters['repetition'])
  for category in categories:
    numRptsPerCategory[category] = np.max(
      repetition[np.array(traces['actualCategory']) == category])

  SDRclusters = []
  clusterAssignments = []
  numRptsMin = np.min(numRptsPerCategory.values()).astype('int32')
  for rpt in range(numRptsMin + 1):
    idx0 = np.logical_and(np.array(traces['actualCategory']) == 0,
                          repetition == rpt)
    idx1 = np.logical_and(np.array(traces['actualCategory']) == 1,
                          repetition == rpt)
    idx2 = np.logical_and(np.array(traces['actualCategory']) == 2,
                          repetition == rpt)

    c0slice = [traces['tmPredictedActiveCells'][i] for i in range(len(idx0)) if
               idx0[i]]
    c1slice = [traces['tmPredictedActiveCells'][i] for i in range(len(idx1)) if
               idx1[i]]
    c2slice = [traces['tmPredictedActiveCells'][i] for i in range(len(idx2)) if
               idx2[i]]

    if not ignoreNoise:
      SDRclusters.append(c0slice)
      clusterAssignments.append(0)
    SDRclusters.append(c1slice)
    clusterAssignments.append(1)
    SDRclusters.append(c2slice)
    clusterAssignments.append(2)

    print " Presentation #{}: ".format(rpt)
    if not ignoreNoise:
      d01 = clusterDist(c0slice, c1slice, numCells)
      print '=> d(c0, c1): %s' % d01
      d02 = clusterDist(c0slice, c2slice, numCells)
      print '=> d(c0, c2): %s' % d02

    d12 = clusterDist(c1slice, c2slice, numCells)
    print '=> d(c1, c2): %s' % d12

  npos, distanceMat = projectClusters2D(SDRclusters, numCells)
  title = 'Inter-sequence clusters in 2D (using %s)' % cellsType
  outputFile = '%s/%s' % (outputDir, title)
  viz2DProjection(title, outputFile, numClasses, clusterAssignments, npos)
  title = 'Inter-sequence clusters distances (using %s)' % cellsType
  outputFile = '%s/%s' % (outputDir, title)
  plotDistanceMat(distanceMat, title, outputFile)
def computeClusterDistanceMat(sdrClusters):
  """
  Compute distance matrix between clusters of SDRs
  :param sdrClusters: list of sdr clusters,
                      each cluster is a list of SDRs
                      each SDR is a list of active indices
  :return: distance matrix
  """
  numClusters = len(sdrClusters)
  distanceMat = np.zeros((numClusters, numClusters), dtype=np.float64)
  for i in range(numClusters):
    for j in range(i, numClusters):
      distanceMat[i, j] = clusterDist(sdrClusters[i], sdrClusters[j])
      distanceMat[j, i] = distanceMat[i, j]

  return distanceMat
示例#3
0
def computeClusterDistanceMat(sdrClusters, numCells):
    """
  Compute distance matrix between clusters of SDRs
  :param sdrClusters: list of sdr clusters,
                      each cluster is a list of SDRs
                      each SDR is a list of active indices
  :return: distance matrix
  """
    numClusters = len(sdrClusters)
    distanceMat = np.zeros((numClusters, numClusters), dtype=np.float64)
    for i in range(numClusters):
        for j in range(i, numClusters):
            distanceMat[i, j] = clusterDist(sdrClusters[i], sdrClusters[j],
                                            numCells)
            distanceMat[j, i] = distanceMat[i, j]

    return distanceMat
示例#4
0
def vizInterSequenceClusters(traces,
                             outputDir,
                             cellsType,
                             numCells,
                             numClasses,
                             ignoreNoise=True):
    clusters = assignClusters(traces)

    # compare inter-cluster distance over time
    numRptsPerCategory = {}
    categories = np.unique(traces['actualCategory'])
    repetition = np.array(clusters['repetition'])
    for category in categories:
        numRptsPerCategory[category] = np.max(
            repetition[np.array(traces['actualCategory']) == category])

    SDRclusters = []
    clusterAssignments = []
    numRptsMin = np.min(numRptsPerCategory.values()).astype('int32')
    for rpt in range(numRptsMin + 1):
        idx0 = np.logical_and(
            np.array(traces['actualCategory']) == 0, repetition == rpt)
        idx1 = np.logical_and(
            np.array(traces['actualCategory']) == 1, repetition == rpt)
        idx2 = np.logical_and(
            np.array(traces['actualCategory']) == 2, repetition == rpt)

        c0slice = [
            traces['tmPredictedActiveCells'][i] for i in range(len(idx0))
            if idx0[i]
        ]
        c1slice = [
            traces['tmPredictedActiveCells'][i] for i in range(len(idx1))
            if idx1[i]
        ]
        c2slice = [
            traces['tmPredictedActiveCells'][i] for i in range(len(idx2))
            if idx2[i]
        ]

        if not ignoreNoise:
            SDRclusters.append(c0slice)
            clusterAssignments.append(0)
        SDRclusters.append(c1slice)
        clusterAssignments.append(1)
        SDRclusters.append(c2slice)
        clusterAssignments.append(2)

        print " Presentation #{}: ".format(rpt)
        if not ignoreNoise:
            d01 = clusterDist(c0slice, c1slice, numCells)
            print '=> d(c0, c1): %s' % d01
            d02 = clusterDist(c0slice, c2slice, numCells)
            print '=> d(c0, c2): %s' % d02

        d12 = clusterDist(c1slice, c2slice, numCells)
        print '=> d(c1, c2): %s' % d12

    npos, distanceMat = projectClusters2D(SDRclusters, numCells)
    title = 'Inter-sequence clusters in 2D (using %s)' % cellsType
    outputFile = '%s/%s' % (outputDir, title)
    viz2DProjection(title, outputFile, numClasses, clusterAssignments, npos)
    title = 'Inter-sequence clusters distances (using %s)' % cellsType
    outputFile = '%s/%s' % (outputDir, title)
    plotDistanceMat(distanceMat, title, outputFile)