Beispiel #1
0
def varyResolution_varyNumModules(inFilenames,
                                  outFilename,
                                  resolutions=(2, 3, 4),
                                  moduleCounts=(
                                      6,
                                      12,
                                      18,
                                  ),
                                  xlim=None):
    if not os.path.exists(CHART_DIR):
        os.makedirs(CHART_DIR)

    allResults = defaultdict(lambda: defaultdict(list))

    for inFilename in inFilenames:
        with open(inFilename, "r") as f:
            experiments = json.load(f)
        for exp in experiments:
            numModules = exp[0]["numModules"]
            numObjects = exp[0]["numObjects"]
            resolution = exp[0]["inverseReadoutResolution"]

            results = []
            for numSensationsStr, numOccurrences in exp[1].items():
                if numSensationsStr == "null":
                    results += [np.inf] * numOccurrences
                else:
                    results += [int(numSensationsStr)] * numOccurrences

            allResults[(numModules, resolution)][numObjects] += results

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax2 = ax1.twiny()

    colors = ("C0", "C1", "C2")
    markers = ("o", "o", "o")
    markerSizes = (2, 4, 6)
    for resolution, color in zip(resolutions, colors):
        for numModules, marker, markerSize in zip(moduleCounts, markers,
                                                  markerSizes):
            resultsByNumObjects = allResults[(numModules, resolution)]

            expResults = sorted(
                (numObjects, np.median(results))
                for numObjects, results in resultsByNumObjects.iteritems())

            # Results up to the final non-infinite median.
            lineResults = [(numObjects, median)
                           for numObjects, median in expResults
                           if median != np.inf]

            # Results excluding the final non-infinite median.
            numCircleMarkers = len(lineResults)
            if len(lineResults) < len(expResults):
                numCircleMarkers -= 1

            # Results including only the final non-infinite median.
            lineEndResults = ([lineResults[-1]]
                              if len(lineResults) < len(expResults) else [])

            ax1.plot([numObjects for numObjects, median in lineResults],
                     [median for numObjects, median in lineResults],
                     "{}-".format(marker),
                     markevery=xrange(numCircleMarkers),
                     color=color,
                     linewidth=1,
                     markersize=markerSize)
            if len(lineResults) < len(expResults):
                endNumObjects, endMedian = lineEndResults[-1]
                ax1.plot([endNumObjects], [endMedian],
                         "x",
                         color=color,
                         markeredgewidth=markerSize / 2,
                         markersize=markerSize * 1.5)

    if xlim is not None:
        ax1.set_xlim(xlim[0], xlim[1])
    ax1.set_ylim(0, ax1.get_ylim()[1])
    ax1.set_xlabel("# learned objects")
    ax1.set_ylabel("Median # sensations before recognition")
    ax2.set_xlabel("Sensory ambiguity index", labelpad=8)

    leg = ax1.legend(loc="upper right",
                     title=" Readout bins per axis:",
                     frameon=False,
                     handles=[
                         matplotlib.lines.Line2D([], [], color=color)
                         for color in colors
                     ],
                     labels=resolutions)
    ax1.add_artist(leg)

    leg = ax1.legend(loc="center right",
                     title="Number of modules:",
                     bbox_to_anchor=(0.99, 0.6),
                     frameon=False,
                     handles=[
                         matplotlib.lines.Line2D([], [],
                                                 marker=marker,
                                                 markersize=markerSize,
                                                 color="black")
                         for marker, markerSize in zip(markers, markerSizes)
                     ],
                     labels=moduleCounts)

    locs, labels = ambiguity_index.getTotalExpectedOccurrencesTicks_2_5(
        ambiguity_index.
        numOtherOccurrencesOfMostUniqueFeature_lowerBound80_100features_10locationsPerObject
    )
    ax2.set_xticks(locs)
    ax2.set_xticklabels(labels)
    ax2.set_xlim(ax1.get_xlim())
    ax2_color = 'gray'
    ax2.xaxis.label.set_color(ax2_color)
    ax2.tick_params(axis='x', colors=ax2_color)

    plt.tight_layout()

    filePath = os.path.join(CHART_DIR, outFilename)
    print "Saving", filePath
    plt.savefig(filePath)
def varyResolution_varyNumModules(inFilenames,
                                  outFilename,
                                  resolutions=(2, 3, 4),
                                  moduleCounts=(
                                      6,
                                      12,
                                      18,
                                  ),
                                  maxNumObjectsByResolution={}):
    if not os.path.exists(CHART_DIR):
        os.makedirs(CHART_DIR)

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax2 = ax1.twiny()

    colors = ("C0", "C1", "C2")
    markers = ("o", "o", "o")
    markerSizes = (2, 4, 6)

    allResults = defaultdict(lambda: defaultdict(list))

    for inFilename in inFilenames:
        with open(inFilename, "r") as f:
            experiments = json.load(f)
        for exp in experiments:
            numModules = exp[0]["numModules"]
            numObjects = exp[0]["numObjects"]
            resolution = exp[0]["inverseReadoutResolution"]

            failed = exp[1].get("null", 0)
            allResults[(numModules,
                        resolution)][numObjects].append(1.0 -
                                                        (float(failed) /
                                                         float(numObjects)))

    for resolution, color in zip(resolutions, colors):
        for numModules, marker, markerSize in zip(moduleCounts, markers,
                                                  markerSizes):
            resultsByNumObjects = allResults[(numModules, resolution)]
            expResults = [
                (numObjects, sum(results) / len(results))
                for numObjects, results in resultsByNumObjects.iteritems()
                if resolution not in maxNumObjectsByResolution
                or numObjects <= maxNumObjectsByResolution[resolution]
            ]

            x = []
            y = []
            for i, j in sorted(expResults):
                x.append(i)
                y.append(j)

            ax1.plot(x,
                     y,
                     "{}-".format(marker),
                     color=color,
                     linewidth=1,
                     markersize=markerSize)

    leg = ax1.legend(loc="upper right",
                     title=" Readout bins per axis:",
                     frameon=False,
                     handles=[
                         matplotlib.lines.Line2D([], [], color=color)
                         for color in colors
                     ],
                     labels=resolutions)
    ax1.add_artist(leg)

    leg = ax1.legend(loc="center right",
                     title="Number of modules:",
                     bbox_to_anchor=(0.99, 0.6),
                     frameon=False,
                     handles=[
                         matplotlib.lines.Line2D([], [],
                                                 marker=marker,
                                                 markersize=markerSize,
                                                 color="black")
                         for marker, markerSize in zip(markers, markerSizes)
                     ],
                     labels=moduleCounts)

    ax1.set_xlabel("# learned objects")
    ax1.set_ylabel("Recognition accuracy after many sensations")
    ax2.set_xlabel("Sensory ambiguity index", labelpad=8)

    locs, labels = ambiguity_index.getTotalExpectedOccurrencesTicks_2_5(
        ambiguity_index.
        numOtherOccurrencesOfMostUniqueFeature_lowerBound80_100features_10locationsPerObject
    )
    ax2.set_xticks(locs)
    ax2.set_xticklabels(labels)
    ax2.set_xlim(ax1.get_xlim())
    ax2_color = 'gray'
    ax2.xaxis.label.set_color(ax2_color)
    ax2.tick_params(axis='x', colors=ax2_color)

    plt.tight_layout()

    filePath = os.path.join(CHART_DIR, outFilename)
    print "Saving", filePath
    plt.savefig(filePath)
Beispiel #3
0
def createCharts(inFilenames,
                 outFilename,
                 moduleWidths=(6, 9, 12),
                 moduleCounts=(6, 12, 18),
                 maxNumObjectsByParams={},
                 numObjectsAllowList=None):
    if not os.path.exists(CHART_DIR):
        os.makedirs(CHART_DIR)

    recognitionTimeResults = defaultdict(lambda: defaultdict(list))
    capacityResults = defaultdict(lambda: defaultdict(list))

    for inFilename in inFilenames:
        with open(inFilename, "r") as f:
            experiments = json.load(f)
        for exp in experiments:
            moduleWidth = exp[0]["locationModuleWidth"]
            numObjects = exp[0]["numObjects"]

            if numObjectsAllowList is not None and numObjects not in numObjectsAllowList:
                continue

            numModules = exp[0]["numModules"]

            recognitionTimes = []
            for numSensationsStr, numOccurrences in exp[1].items():
                if numSensationsStr == "null":
                    recognitionTimes += [np.inf] * numOccurrences
                else:
                    recognitionTimes += [int(numSensationsStr)
                                         ] * numOccurrences
            recognitionTimeResults[(
                moduleWidth, numModules)][numObjects] += recognitionTimes

            failed = exp[1].get("null", 0)
            accuracy = 1.0 - (float(failed) / float(numObjects))
            capacityResults[(moduleWidth,
                             numModules)][numObjects].append(accuracy)

    locs, labels = ambiguity_index.getTotalExpectedOccurrencesTicks_2_5(
        ambiguity_index.
        numOtherOccurrencesOfMostUniqueFeature_lowerBound50_100features_10locationsPerObject
    )

    ax2_color = 'gray'
    fig, (axRecognitionTime, axCapacity) = plt.subplots(figsize=(6, 8),
                                                        nrows=2,
                                                        sharex=True)

    #
    # CAPACITY
    #

    ax2Capacity = axCapacity.twiny()
    # Optional: swap axes
    # axCapacity.xaxis.tick_top()
    # axCapacity.xaxis.set_label_position('top')
    # ax2Capacity.xaxis.tick_bottom()
    # ax2Capacity.xaxis.set_label_position('bottom')

    colors = ("C0", "C1", "C2")
    markers = ("o", "o", "o")
    markerSizes = (2, 4, 6)
    for moduleWidth, color in reversed(zip(moduleWidths, colors)):
        for numModules, marker, markerSize in zip(moduleCounts, markers,
                                                  markerSizes):
            resultsByNumObjects = capacityResults[(moduleWidth, numModules)]
            expResults = [
                (numObjects, sum(results) / len(results))
                for numObjects, results in resultsByNumObjects.iteritems()
                if (moduleWidth, numModules) not in maxNumObjectsByParams or
                numObjects <= maxNumObjectsByParams[(moduleWidth, numModules)]
            ]

            x = []
            y = []
            for i, j in sorted(expResults):
                x.append(i)
                y.append(j)

            axCapacity.plot(x,
                            y,
                            "{}-".format(marker),
                            color=color,
                            linewidth=1,
                            markersize=markerSize)

    axCapacity.set_xlabel("# learned objects")
    axCapacity.set_ylabel("Recognition accuracy after many sensations")
    # ax2Capacity.set_xlabel("Sensory ambiguity index")

    ax2Capacity.set_xticks(locs)
    ax2Capacity.set_xticklabels(labels)
    ax2Capacity.set_xlim(axCapacity.get_xlim())
    ax2Capacity.xaxis.label.set_color(ax2_color)
    ax2Capacity.tick_params(axis='x', colors=ax2_color)

    #
    # RECOGNITION TIME
    #

    ax2RecognitionTime = axRecognitionTime.twiny()
    # Optional: swap axes
    # axRecognitionTime.xaxis.tick_top()
    # axRecognitionTime.xaxis.set_label_position('top')
    # ax2RecognitionTime.xaxis.tick_bottom()
    # ax2RecognitionTime.xaxis.set_label_position('bottom')

    colors = ("C0", "C1", "C2")
    markers = ("o", "o", "o")
    markerSizes = (2, 4, 6)
    for moduleWidth, color in reversed(zip(moduleWidths, colors)):
        for numModules, marker, markerSize in zip(moduleCounts, markers,
                                                  markerSizes):
            resultsByNumObjects = recognitionTimeResults[(moduleWidth,
                                                          numModules)]

            expResults = sorted(
                (numObjects, np.median(results))
                for numObjects, results in resultsByNumObjects.iteritems())

            # Results up to the final non-infinite median.
            lineResults = [(numObjects, median)
                           for numObjects, median in expResults
                           if median != np.inf]

            # Results excluding the final non-infinite median.
            numCircleMarkers = len(lineResults)
            if len(lineResults) < len(expResults):
                numCircleMarkers -= 1

            axRecognitionTime.plot(
                [numObjects for numObjects, median in lineResults],
                [median for numObjects, median in lineResults],
                "{}-".format(marker),
                markevery=xrange(numCircleMarkers),
                color=color,
                linewidth=1,
                markersize=markerSize)

            if (len(lineResults) < len(expResults) and len(lineResults) > 0):
                endNumObjects, endMedian = lineResults[-1]
                axRecognitionTime.plot([endNumObjects], [endMedian],
                                       "x",
                                       color=color,
                                       markeredgewidth=markerSize / 2,
                                       markersize=markerSize * 1.5)

    axRecognitionTime.set_ylim(0, axRecognitionTime.get_ylim()[1])
    # axRecognitionTime.set_xlabel("# learned objects")
    axRecognitionTime.set_ylabel("Median # sensations before recognition")
    ax2RecognitionTime.set_xlabel(
        "Median # locations recalled by an object's most unique feature",
        labelpad=8)

    # Carefully use whitespace in title to shift the entries in the legend to
    # align with the previous legend.
    leg = axRecognitionTime.legend(loc="upper right",
                                   title="Cells per module:       ",
                                   bbox_to_anchor=(1.035, 1.0),
                                   frameon=False,
                                   handles=[
                                       matplotlib.lines.Line2D([], [],
                                                               color=color)
                                       for color in colors
                                   ],
                                   labels=[
                                       "{}x{}".format(moduleWidth, moduleWidth)
                                       for moduleWidth in moduleWidths
                                   ])
    axRecognitionTime.add_artist(leg)

    leg = axRecognitionTime.legend(
        loc="center right",
        title="Number of modules:",
        bbox_to_anchor=(1.0, 0.5),
        frameon=False,
        handles=[
            matplotlib.lines.Line2D([], [],
                                    marker=marker,
                                    markersize=markerSize,
                                    color="black")
            for marker, markerSize in zip(markers, markerSizes)
        ],
        labels=moduleCounts)

    ax2RecognitionTime.set_xticks(locs)
    ax2RecognitionTime.set_xticklabels(labels)
    ax2RecognitionTime.set_xlim(axRecognitionTime.get_xlim())
    ax2RecognitionTime.xaxis.label.set_color(ax2_color)
    ax2RecognitionTime.tick_params(axis='x', colors=ax2_color)

    plt.tight_layout()

    filePath = os.path.join(CHART_DIR, outFilename)
    print "Saving", filePath
    plt.savefig(filePath)
def varyModuleSize_varyNumModules(inFilenames,
                                  outFilename,
                                  scalingFactors=[1, 2, 3],
                                  moduleCounts=(6, 12, 18),
                                  maxNumObjectsByParams={}):
    if not os.path.exists(CHART_DIR):
        os.makedirs(CHART_DIR)

    allResults = defaultdict(lambda: defaultdict(list))

    for inFilename in inFilenames:
        with open(inFilename, "r") as f:
            experiments = json.load(f)
        for exp in experiments:
            enlargeModuleFactor = exp[0]["enlargeModuleFactor"]
            numObjects = exp[0]["numObjects"]
            numModules = exp[0]["numModules"]

            failed = exp[1].get("null", 0)
            allResults[(enlargeModuleFactor,
                        numModules)][numObjects].append(1.0 -
                                                        (float(failed) /
                                                         float(numObjects)))

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax2 = ax1.twiny()
    # Optional: swap axes
    # ax1.xaxis.tick_top()
    # ax1.xaxis.set_label_position('top')
    # ax2.xaxis.tick_bottom()
    # ax2.xaxis.set_label_position('bottom')

    colors = ("C0", "C1", "C2")
    markers = ("o", "o", "o")
    markerSizes = (2, 4, 6)
    for scalingFactor, color in zip(scalingFactors, colors):
        for numModules, marker, markerSize in zip(moduleCounts, markers,
                                                  markerSizes):
            resultsByNumObjects = allResults[(scalingFactor, numModules)]
            expResults = [
                (numObjects, sum(results) / len(results))
                for numObjects, results in resultsByNumObjects.iteritems()
                if (scalingFactor, numModules) not in maxNumObjectsByParams
                or numObjects <= maxNumObjectsByParams[(scalingFactor,
                                                        numModules)]
            ]

            x = []
            y = []
            for i, j in sorted(expResults):
                x.append(i)
                y.append(j)

            ax1.plot(x,
                     y,
                     "{}-".format(marker),
                     color=color,
                     linewidth=1,
                     markersize=markerSize)

    # Carefully use whitespace in title to shift the entries in the legend to
    # align with the next legend.
    leg = ax1.legend(loc="upper right",
                     title="Module size:       ",
                     frameon=False,
                     handles=[
                         matplotlib.lines.Line2D([], [], color=color)
                         for color in colors
                     ],
                     labels=["rat", "rat * 2", "rat * 3"])
    ax1.add_artist(leg)

    leg = ax1.legend(loc="center right",
                     title="Number of modules:",
                     bbox_to_anchor=(1.0, 0.6),
                     frameon=False,
                     handles=[
                         matplotlib.lines.Line2D([], [],
                                                 marker=marker,
                                                 markersize=markerSize,
                                                 color="black")
                         for marker, markerSize in zip(markers, markerSizes)
                     ],
                     labels=moduleCounts)

    ax1.set_xlabel("# learned objects")
    ax1.set_ylabel("Recognition accuracy after many sensations")
    ax2.set_xlabel("Sensory ambiguity index", labelpad=8)

    locs, labels = ambiguity_index.getTotalExpectedOccurrencesTicks_2_5(
        ambiguity_index.
        numOtherOccurrencesOfMostUniqueFeature_lowerBound80_100features_10locationsPerObject
    )

    ax2.set_xticks(locs)
    ax2.set_xticklabels(labels)
    ax2.set_xlim(ax1.get_xlim())
    ax2_color = 'gray'
    ax2.xaxis.label.set_color(ax2_color)
    ax2.tick_params(axis='x', colors=ax2_color)

    plt.tight_layout()

    filePath = os.path.join(CHART_DIR, outFilename)
    print "Saving", filePath
    plt.savefig(filePath)