Example #1
0
def write_csv(dataMatrix, outputFileNameBase):

    timeStampStr = dww.time_stamp()

    #    strList = [outputFileNameBase, "_", timeStampStr, ".csv"]
    #    outputFileName = "".join(strList)

    outputFileName = "%s_%s.csv" % (outputFileNameBase, timeStampStr)

    print "Writing data to csv file %s ...\n" % outputFileName

    with open(outputFileName, "wb") as filePointer:
        csvMatrix = csv.writer(filePointer)
        csvMatrix.writerows(dataMatrix)
    filePointer.close()

    return ()
Example #2
0
def write_csv(dataMatrix, outputFileNameBase):

    import csv
    
    from diek_functions import time_stamp

    print '\nWriting data to csv file ...\n'
    
    timeStampStr = time_stamp()
    
    strList = ['python_output/', outputFileNameBase, '_', timeStampStr, '.csv']
    
    outputFileName = ''.join(strList)
    
    with open(outputFileName, "wb") as filePointer:

	    csvMatrix = csv.writer(filePointer)
	
	    csvMatrix.writerows(dataMatrix)

    filePointer.close()
	
    return()
def plot_mean_quantity(nCells, nLayers, overlapBinaryDF, quantitySelectionsDF, meansDF, subregionDF):

    import numpy as np

    import matplotlib.pyplot as plt
    import matplotlib.patches as patches

    from diek_functions import pause
    from diek_functions import time_stamp

    from plot_functions import HCcolors as c
    from plot_functions import make_color_map

    nNeurons = len(overlapBinaryDF)
    nRows, nColumns = overlapBinaryDF.shape
    nParcels = nColumns - 4 # account for header columns (UniqueIDs - EorI)
    
    overlapBinaryLayers = overlapBinaryParcels = list(overlapBinaryDF)[4:]
    for j in range(nParcels):
        idx = [pos for pos, char in enumerate(overlapBinaryParcels[j]) if char == ':']
        overlapBinaryLayers[j] = overlapBinaryParcels[j][idx[0]+1:]
    
    regionColors = [c.BROWN_DG,
                    c.BROWN_CA3,
                    c.YELLOW_CA2,
                    c.ORANGE_CA1,
                    c.YELLOW_Sub,
                    c.GREEN_EC]
    DG = 0
    CA3 = 1
    CA2 = 2
    CA1 = 3
    Sub = 4
    EC = 5
    
    START = subregionDF.loc[0, 'start']
    END = subregionDF.loc[0, 'end']
    
    nNeuronsDisplayed = END - START
    
    overlapBinaryColors = [c.WHITE,        # 0
                           c.RED_LIGHT,    # 1
                           c.BLUE_LIGHT,   # 2
                           c.PURPLE_LIGHT] # 3
              
    nOverlapBinaryColors = len(overlapBinaryColors)
    
    print "Making overlap binary color map ..."
    overlapBinaryColorMap = make_color_map(overlapBinaryColors)
    
    print "Plotting overlap binary background data ..."
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.set_aspect("equal")
    
    ax.axis('off')
    
    overlapBinaryDataDF = overlapBinaryDF.iloc[:,4:]
    CijOverlap = overlapBinaryDataDF.iloc[START:(END+1), :]
    plt.pcolormesh(CijOverlap, cmap=overlapBinaryColorMap, edgecolors=c.BLACK, linewidth=0.1)
    
    vStart = -3
    
    plt.xlim(-12, nParcels)
    plt.ylim(vStart, nNeuronsDisplayed)
    
    plt.gca().invert_yaxis()
    
    # Add hatch lines to the colored squares
#    shadingLineWidths = 0.2
#    for i in range(nNeuronsDisplayed):
#        for j in range(nParcels):
#            if ((CijOverlap.iloc[i,j] == 1) or (CijOverlap.iloc[i,j] == 3)): # axons
#                plt.plot([j+0.5, j+0.5], [i+0.1, i+0.9], color=c.WHITE, linewidth=shadingLineWidths)
#            if ((CijOverlap.iloc[i,j] == 2) or (CijOverlap.iloc[i,j] == 3)): # dendrites
#                plt.plot([j+0.1, j+0.9], [i+0.5, i+0.5], color=c.WHITE, linewidth=shadingLineWidths)
    
    # color-coded tags for the subregions
    ax.add_patch(patches.Rectangle((sum(nLayers[:DG]), vStart), nLayers[DG], 3, edgecolor="none",
                                   facecolor=c.BROWN_DG))
    ax.add_patch(patches.Rectangle((sum(nLayers[:CA3]), vStart), nLayers[CA3], 3, edgecolor="none",
                                   facecolor=c.BROWN_CA3))
    ax.add_patch(patches.Rectangle((sum(nLayers[:CA2]), vStart), nLayers[CA2], 3, edgecolor="none",
                                   facecolor=c.YELLOW_CA2))
    ax.add_patch(patches.Rectangle((sum(nLayers[:CA1]), vStart), nLayers[CA1], 3, edgecolor="none",
                                   facecolor=c.ORANGE_CA1))
    ax.add_patch(patches.Rectangle((sum(nLayers[:Sub]), vStart), nLayers[Sub], 3, edgecolor="none",
                                   facecolor=c.YELLOW_Sub))
    ax.add_patch(patches.Rectangle((sum(nLayers[:EC]), vStart), nLayers[EC], 3, edgecolor="none",
                                   facecolor=c.GREEN_EC))
    
    hTab = -9

    # add title label to plot
    if (subregionDF.loc[0, 'label'] == 'All'):
        displayFontSize = 1
    else:
        displayFontSize = 5
    ax.text(hTab, -1.5, quantitySelectionsDF.loc[0, 'string'], rotation=0, horizontalalignment="left",
            fontsize=displayFontSize, color=c.BLACK)

    # parcellation subregion headers
    tab = [sum(nLayers[:DG]) + (nLayers[DG]+1)/2,
           sum(nLayers[:CA3]) + (nLayers[CA3]+0.5)/2,
           sum(nLayers[:CA2]) + (nLayers[CA2]+1)/2,
           sum(nLayers[:CA1]) + (nLayers[CA1]+1)/2,
           sum(nLayers[:Sub]) + (nLayers[Sub]+0.5)/2,
           sum(nLayers[:EC]) + (nLayers[EC]+1)/2
          ]
    ax.text(tab[DG], -2, "DG", rotation=0, horizontalalignment="center", fontsize=displayFontSize, color=c.WHITE)
    ax.text(tab[CA3], -2, "CA3", rotation=0, horizontalalignment="center", fontsize=displayFontSize, color=c.WHITE)
    ax.text(tab[CA2], -2, "CA2", rotation=0, horizontalalignment="center", fontsize=displayFontSize, color=c.BLACK)
    ax.text(tab[CA1], -2, "CA1", rotation=0, horizontalalignment="center", fontsize=displayFontSize, color=c.WHITE)
    ax.text(tab[Sub], -2, "Sub", rotation=0, horizontalalignment="center", fontsize=displayFontSize, color=c.BLACK)
    ax.text(tab[EC], -2, "EC", rotation=0, horizontalalignment="center", fontsize=displayFontSize, color=c.WHITE)

    for j in range(sum(nLayers)):
        ax.text(j+0.5, -0.1, overlapBinaryLayers[j], rotation=90, horizontalalignment="center", 
                verticalalignment="bottom", fontsize=displayFontSize, color=c.WHITE)
    for j in range(sum(nLayers[:CA2]), sum(nLayers[:CA1])):
        ax.text(j+0.5, -0.1, overlapBinaryLayers[j], rotation=90, horizontalalignment="center", 
                verticalalignment="bottom", fontsize=displayFontSize, color=c.BLACK)
    for j in range(sum(nLayers[:Sub]), sum(nLayers[:EC])):
        ax.text(j+0.5, -0.1, overlapBinaryLayers[j], rotation=90, horizontalalignment="center", 
                verticalalignment="bottom", fontsize=displayFontSize, color=c.BLACK)

    # cell type subregion headers
    strng = 'DG'
    vCorrection = 0.0
    if ((subregionDF.loc[0, 'label'] == 'DG') or (subregionDF.loc[0, 'label'] == 'All')):
        DGstart = nCells[DG]/2
        ax.text(hTab, DGstart+vCorrection, strng, horizontalalignment="center", rotation=90,
                fontsize=displayFontSize, color=c.BLACK)
    strng = 'CA3'
    vCorrection = 0.0
    if (subregionDF.loc[0, 'label'] == 'CA3'):
        CA3start = nCells[CA3]/2
        ax.text(hTab, CA3start+vCorrection, strng, horizontalalignment="center", rotation=90,
                fontsize=displayFontSize, color=c.BLACK)
    elif (subregionDF.loc[0, 'label'] == 'All'):
        CA3start = sum(nCells[:CA3]) + nCells[CA3]/2
        ax.text(hTab, CA3start+vCorrection, strng, horizontalalignment="center", rotation=90,
                fontsize=displayFontSize, color=c.BLACK)
    strng = 'CA2'
    vCorrection = 0.0
    if (subregionDF.loc[0, 'label'] == 'CA2'):
        CA2start = nCells[CA2]/2
        ax.text(hTab, CA2start+vCorrection, strng, horizontalalignment="center", rotation=90,
                fontsize=displayFontSize, color=c.BLACK)
    elif (subregionDF.loc[0, 'label'] == 'All'):
        CA2start = sum(nCells[:CA2]) + nCells[CA2]/2
        ax.text(hTab, CA2start+vCorrection, strng, horizontalalignment="center", rotation=90,
                fontsize=displayFontSize, color=c.BLACK)
    strng = 'CA1'
    vCorrection = 0.0
    if (subregionDF.loc[0, 'label'] == 'CA1'):
        CA1start = nCells[CA1]/2
        ax.text(hTab, CA1start+vCorrection, strng, horizontalalignment="center", rotation=90,
                fontsize=displayFontSize, color=c.BLACK)
    elif (subregionDF.loc[0, 'label'] == 'All'):
        CA1start = sum(nCells[:CA1]) + nCells[CA1]/2
        ax.text(hTab, CA1start+vCorrection, strng, horizontalalignment="center", rotation=90,
                fontsize=displayFontSize, color=c.BLACK)
    strng = 'Sub'
    vCorrection = 0.0
    if (subregionDF.loc[0, 'label'] == 'Sub'):
        Substart = nCells[Sub]/2
        ax.text(hTab, Substart+vCorrection, strng, horizontalalignment="center", rotation=90,
                fontsize=displayFontSize, color=c.BLACK)
    elif (subregionDF.loc[0, 'label'] == 'All'):
        Substart = sum(nCells[:Sub]) + nCells[Sub]/2
        ax.text(hTab, Substart+vCorrection, strng, horizontalalignment="center", rotation=90,
                fontsize=displayFontSize, color=c.BLACK)
    strng = 'EC'
    vCorrection = 0.0
    if (subregionDF.loc[0, 'label'] == 'EC'):
        ECstart = nCells[EC]/2
        ax.text(hTab, ECstart+vCorrection, strng, horizontalalignment="center", rotation=90,
                fontsize=displayFontSize, color=c.BLACK)
    elif (subregionDF.loc[0, 'label'] == 'All'):
        ECstart = sum(nCells[:EC]) + nCells[EC]/2
        ax.text(hTab, ECstart+vCorrection, strng, horizontalalignment="center", rotation=90,
                fontsize=displayFontSize, color=c.BLACK)

    # thick horizontal lines on cell type axis
    if (subregionDF.loc[0, 'label'] == 'All'):
        lineWidth = 0.2
        CA3line = sum(nCells[:CA3])
        CA2line = sum(nCells[:CA2])
        CA1line = sum(nCells[:CA1])
        Subline = sum(nCells[:Sub])
        ECline = sum(nCells[:EC])
        
        ax.plot([hTab, nParcels], [CA3line, CA3line], linewidth=lineWidth, color=c.BLACK)
        ax.plot([hTab, nParcels], [CA2line, CA2line], linewidth=lineWidth, color=c.BLACK)
        ax.plot([hTab, nParcels], [CA1line, CA1line], linewidth=lineWidth, color=c.BLACK)
        ax.plot([hTab, nParcels], [Subline, Subline], linewidth=lineWidth, color=c.BLACK)
        ax.plot([hTab, nParcels], [ECline, ECline], linewidth=lineWidth, color=c.BLACK)

    # plot labels
    for i in range(START, END):
        hTab = -0.5
        labelCode = "{0:s} ({1:d})".format(overlapBinaryDF.loc[i, 'Names'], int(meansDF.loc[i, 'Factors']))
        labelColor = c.BLACK
        if overlapBinaryDF.loc[i, 'EorI'] == 'I':
            labelColor = c.GRAY
        ax.text(hTab, i-START+0.5, labelCode, rotation=0, horizontalalignment="right", verticalalignment="center",
                fontsize=displayFontSize, color=labelColor)
        
    nMeanValues = len(meansDF)
    
    print "Plotting mean quantity values ..."

    for i in range(nMeanValues):
        vTab = int(overlapBinaryDF['UniqueIDs'].index[overlapBinaryDF['UniqueIDs'] ==
                                                      meansDF.loc[i, 'UniqueIDs']].tolist() - START)
        neuriteStr = meansDF.loc[i, 'Neurites']
        idx = [pos for pos, char in enumerate(neuriteStr) if char == ':']
        parcel = neuriteStr[:idx[1]]
        hTab = parcel_lookup(parcel)
        neurite = neuriteStr[idx[1]+1:]
        
        if (np.isnan(meansDF.loc[i, 'Means'])):
            strng = '?'
        else:
            value = meansDF.loc[i, 'Means']
            if (quantitySelectionsDF.loc[0, 'selection'] == 1): # total neurite length
                strng = "{0:.0f}".format(value)
            elif (quantitySelectionsDF.loc[0, 'selection'] == 2): # percent of neurite tree
                strng = "{0:.2f}".format(value)
            elif (quantitySelectionsDF.loc[0, 'selection'] == 3): # density
                strng = "{0:.4f}".format(value)
            elif (quantitySelectionsDF.loc[0, 'selection'] == 4): # average maximum path length
                strng = "{0:.0f}".format(value)
            else: # (quantitySelectionsDF.loc[0, 'selection'] == 2): # average mean path length
                strng = "{0:.0f}".format(value)

        if (((meansDF.loc[i, 'UniqueIDs'] // 1000) == subregionDF.loc[0, 'code']) or
            (subregionDF.loc[0, 'code'] == 0)):
            if (neurite == 'A'):
                if (overlapBinaryDataDF.iloc[vTab+START, hTab] == 1): # axons only
                    ax.add_patch(patches.Rectangle((hTab+0.02, vTab+0.02), 0.96, 0.48, edgecolor="none",
                                   facecolor=c.RED))
                    ax.add_patch(patches.Rectangle((hTab+0.02, vTab+0.5), 0.96, 0.48, edgecolor="none",
                                   facecolor=c.WHITE))
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] >= 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.25, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.WHITE)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] < 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.25, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.YELLOW)
                    else:
                        ax.text(hTab+0.5, vTab+0.25, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.WHITE)
                elif (overlapBinaryDataDF.iloc[vTab+START, hTab] == 3): # axons from axons and dendrites
                    ax.add_patch(patches.Rectangle((hTab+0.02, vTab+0.02), 0.96, 0.48, edgecolor="none",
                                   facecolor=c.RED))
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] >= 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.25, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.WHITE)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] < 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.25, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.YELLOW)
                    else:
                        ax.text(hTab+0.5, vTab+0.25, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.WHITE)
                elif (overlapBinaryDataDF.iloc[vTab+START, hTab] == 2): # dendrites only
                    ax.add_patch(patches.Rectangle((hTab+0.02, vTab+0.02), 0.96, 0.48, edgecolor="none",
                                   facecolor=c.WHITE))
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] >= 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.25, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.BLACK)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] < 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.25, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.GREEN)
                    else:
                        ax.text(hTab+0.5, vTab+0.25, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.BLACK)
                else: # (overlapBinaryDataDF.iloc[vTab+START, hTab] == 0): # no axons or dendrites
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] >= 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.25, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.RED)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] < 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.25, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.GREEN)
                    else:
                        ax.text(hTab+0.5, vTab+0.25, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.BLACK)
                    ax.plot([hTab, hTab+1], [vTab+0.5, vTab+0.5], linewidth=0.1, color=c.BLACK) # diving line
            if (neurite == 'D'):
                if (overlapBinaryDataDF.iloc[vTab+START, hTab] == 2): # dendrites only
                    ax.add_patch(patches.Rectangle((hTab+0.02, vTab+0.5), 0.96, 0.48, edgecolor="none",
                                   facecolor=c.BLUE))
                    ax.add_patch(patches.Rectangle((hTab+0.02, vTab+0.02), 0.96, 0.48, edgecolor="none",
                                   facecolor=c.WHITE))
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] >= 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.75, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.WHITE)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] < 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.75, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.YELLOW)
                    else:
                        ax.text(hTab+0.5, vTab+0.75, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.WHITE)
                elif (overlapBinaryDataDF.iloc[vTab+START, hTab] == 3): # dendrites from axons and dendrites
                    ax.add_patch(patches.Rectangle((hTab+0.02, vTab+0.5), 0.96, 0.48, edgecolor="none",
                                   facecolor=c.BLUE))
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] >= 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.75, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.WHITE)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] < 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.75, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.YELLOW)
                    else:
                        ax.text(hTab+0.5, vTab+0.75, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.WHITE)
                elif (overlapBinaryDataDF.iloc[vTab+START, hTab] == 1): # axons only
                    ax.add_patch(patches.Rectangle((hTab+0.02, vTab+0.5), 0.96, 0.48, edgecolor="none",
                                   facecolor=c.WHITE))
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] >= 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.75, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.BLACK)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] < 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.75, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.GREEN)
                    else:
                        ax.text(hTab+0.5, vTab+0.75, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.BLACK)
                else: # (overlapBinaryDataDF.iloc[vTab+START, hTab] == 0): # no axons or dendrites
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] >= 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.75, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.BLUE)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and meansDF.loc[i, 'Means'] < 15): # % of neurite tree
                        ax.text(hTab+0.5, vTab+0.75, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.GREEN)
                    else:
                        ax.text(hTab+0.5, vTab+0.75, strng, horizontalalignment="center", rotation=0,
                                verticalalignment="center", fontsize=displayFontSize/2.0, color=c.BLACK)
                    ax.plot([hTab, hTab+1], [vTab+0.5, vTab+0.5], linewidth=0.1, color=c.BLACK) # diving line
    plt.show()
    
    # save plot
    outputFileName = "output/{0:s}_matrix_{1:s}.pdf".format(quantitySelectionsDF.loc[0, 'plotString'], time_stamp())
    print "Saving data to pdf file %s ...\n" % outputFileName    
    fig.savefig(outputFileName, dpi=600)
   
    return
Example #4
0
        if parametersUniqueId == namesCellId:

            for jPatternsColumnNo in range(nPatterns):

                patternsFiringPattern = patternsMatrix[patternsHeaderRowNo][
                    jPatternsColumnNo]

                if parametersFiringPattern == patternsFiringPattern:

                    namesByPatternsMatrix[iNamesRowNo - 1][
                        jPatternsColumnNo] += 1  # account for names header row

outputFileNameBase = "output/FP_csv_output/FP_matrix"

timeStampStr = time_stamp()

outputFileName = "{0}_{1}.csv".format(outputFileNameBase, timeStampStr)

with open(outputFileName, "w") as filePointer:

    csvMatrix = csv.writer(filePointer)

    outputRow = []

    for jOutputNamesHeaderColumnNo in range(nNamesHeaderColumns):
        outputRow.append(namesMatrix[namesHeaderRowNo]
                         [jOutputNamesHeaderColumnNo].encode("utf-8"))

    for jOutputPatternsHeaderColumnNo in range(nPatterns -
                                               1):  # omit *No_data column
def plot_mean_quantity(nCells, nLayers, overlapBinaryDF, quantitySelectionsDF,
                       meansDF, subregionDF):

    import numpy as np

    import matplotlib.pyplot as plt
    import matplotlib.patches as patches

    from diek_functions import pause
    from diek_functions import time_stamp

    from plot_functions import HCcolors as c
    from plot_functions import make_color_map

    nNeurons = len(overlapBinaryDF)
    nRows, nColumns = overlapBinaryDF.shape
    nParcels = nColumns - 4  # account for header columns (UniqueIDs - EorI)

    overlapBinaryLayers = overlapBinaryParcels = list(overlapBinaryDF)[4:]
    for j in range(nParcels):
        idx = [
            pos for pos, char in enumerate(overlapBinaryParcels[j])
            if char == ':'
        ]
        overlapBinaryLayers[j] = overlapBinaryParcels[j][idx[0] + 1:]

    regionColors = [
        c.BROWN_DG, c.BROWN_CA3, c.YELLOW_CA2, c.ORANGE_CA1, c.YELLOW_Sub,
        c.GREEN_EC
    ]
    DG = 0
    CA3 = 1
    CA2 = 2
    CA1 = 3
    Sub = 4
    EC = 5

    START = subregionDF.loc[0, 'start']
    END = subregionDF.loc[0, 'end']

    nNeuronsDisplayed = END - START

    overlapBinaryColors = [
        c.WHITE,  # 0
        c.RED_LIGHT,  # 1
        c.BLUE_LIGHT,  # 2
        c.PURPLE_LIGHT
    ]  # 3

    nOverlapBinaryColors = len(overlapBinaryColors)

    print "Making overlap binary color map ..."
    overlapBinaryColorMap = make_color_map(overlapBinaryColors)

    print "Plotting overlap binary background data ..."
    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax.set_aspect("equal")

    ax.axis('off')

    overlapBinaryDataDF = overlapBinaryDF.iloc[:, 4:]
    CijOverlap = overlapBinaryDataDF.iloc[START:(END + 1), :]
    plt.pcolormesh(CijOverlap,
                   cmap=overlapBinaryColorMap,
                   edgecolors=c.BLACK,
                   linewidth=0.1)

    vStart = -3

    plt.xlim(-12, nParcels)
    plt.ylim(vStart, nNeuronsDisplayed)

    plt.gca().invert_yaxis()

    # Add hatch lines to the colored squares
    #    shadingLineWidths = 0.2
    #    for i in range(nNeuronsDisplayed):
    #        for j in range(nParcels):
    #            if ((CijOverlap.iloc[i,j] == 1) or (CijOverlap.iloc[i,j] == 3)): # axons
    #                plt.plot([j+0.5, j+0.5], [i+0.1, i+0.9], color=c.WHITE, linewidth=shadingLineWidths)
    #            if ((CijOverlap.iloc[i,j] == 2) or (CijOverlap.iloc[i,j] == 3)): # dendrites
    #                plt.plot([j+0.1, j+0.9], [i+0.5, i+0.5], color=c.WHITE, linewidth=shadingLineWidths)

    # color-coded tags for the subregions
    ax.add_patch(
        patches.Rectangle((sum(nLayers[:DG]), vStart),
                          nLayers[DG],
                          3,
                          edgecolor="none",
                          facecolor=c.BROWN_DG))
    ax.add_patch(
        patches.Rectangle((sum(nLayers[:CA3]), vStart),
                          nLayers[CA3],
                          3,
                          edgecolor="none",
                          facecolor=c.BROWN_CA3))
    ax.add_patch(
        patches.Rectangle((sum(nLayers[:CA2]), vStart),
                          nLayers[CA2],
                          3,
                          edgecolor="none",
                          facecolor=c.YELLOW_CA2))
    ax.add_patch(
        patches.Rectangle((sum(nLayers[:CA1]), vStart),
                          nLayers[CA1],
                          3,
                          edgecolor="none",
                          facecolor=c.ORANGE_CA1))
    ax.add_patch(
        patches.Rectangle((sum(nLayers[:Sub]), vStart),
                          nLayers[Sub],
                          3,
                          edgecolor="none",
                          facecolor=c.YELLOW_Sub))
    ax.add_patch(
        patches.Rectangle((sum(nLayers[:EC]), vStart),
                          nLayers[EC],
                          3,
                          edgecolor="none",
                          facecolor=c.GREEN_EC))

    hTab = -9

    # add title label to plot
    if (subregionDF.loc[0, 'label'] == 'All'):
        displayFontSize = 1
    else:
        displayFontSize = 5
    ax.text(hTab,
            -1.5,
            quantitySelectionsDF.loc[0, 'string'],
            rotation=0,
            horizontalalignment="left",
            fontsize=displayFontSize,
            color=c.BLACK)

    # parcellation subregion headers
    tab = [
        sum(nLayers[:DG]) + (nLayers[DG] + 1) / 2,
        sum(nLayers[:CA3]) + (nLayers[CA3] + 0.5) / 2,
        sum(nLayers[:CA2]) + (nLayers[CA2] + 1) / 2,
        sum(nLayers[:CA1]) + (nLayers[CA1] + 1) / 2,
        sum(nLayers[:Sub]) + (nLayers[Sub] + 0.5) / 2,
        sum(nLayers[:EC]) + (nLayers[EC] + 1) / 2
    ]
    ax.text(tab[DG],
            -2,
            "DG",
            rotation=0,
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.WHITE)
    ax.text(tab[CA3],
            -2,
            "CA3",
            rotation=0,
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.WHITE)
    ax.text(tab[CA2],
            -2,
            "CA2",
            rotation=0,
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.BLACK)
    ax.text(tab[CA1],
            -2,
            "CA1",
            rotation=0,
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.WHITE)
    ax.text(tab[Sub],
            -2,
            "Sub",
            rotation=0,
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.BLACK)
    ax.text(tab[EC],
            -2,
            "EC",
            rotation=0,
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.WHITE)

    for j in range(sum(nLayers)):
        ax.text(j + 0.5,
                -0.1,
                overlapBinaryLayers[j],
                rotation=90,
                horizontalalignment="center",
                verticalalignment="bottom",
                fontsize=displayFontSize,
                color=c.WHITE)
    for j in range(sum(nLayers[:CA2]), sum(nLayers[:CA1])):
        ax.text(j + 0.5,
                -0.1,
                overlapBinaryLayers[j],
                rotation=90,
                horizontalalignment="center",
                verticalalignment="bottom",
                fontsize=displayFontSize,
                color=c.BLACK)
    for j in range(sum(nLayers[:Sub]), sum(nLayers[:EC])):
        ax.text(j + 0.5,
                -0.1,
                overlapBinaryLayers[j],
                rotation=90,
                horizontalalignment="center",
                verticalalignment="bottom",
                fontsize=displayFontSize,
                color=c.BLACK)

    # cell type subregion headers
    strng = 'DG'
    vCorrection = 0.0
    if ((subregionDF.loc[0, 'label'] == 'DG')
            or (subregionDF.loc[0, 'label'] == 'All')):
        DGstart = nCells[DG] / 2
        ax.text(hTab,
                DGstart + vCorrection,
                strng,
                horizontalalignment="center",
                rotation=90,
                fontsize=displayFontSize,
                color=c.BLACK)
    strng = 'CA3'
    vCorrection = 0.0
    if (subregionDF.loc[0, 'label'] == 'CA3'):
        CA3start = nCells[CA3] / 2
        ax.text(hTab,
                CA3start + vCorrection,
                strng,
                horizontalalignment="center",
                rotation=90,
                fontsize=displayFontSize,
                color=c.BLACK)
    elif (subregionDF.loc[0, 'label'] == 'All'):
        CA3start = sum(nCells[:CA3]) + nCells[CA3] / 2
        ax.text(hTab,
                CA3start + vCorrection,
                strng,
                horizontalalignment="center",
                rotation=90,
                fontsize=displayFontSize,
                color=c.BLACK)
    strng = 'CA2'
    vCorrection = 0.0
    if (subregionDF.loc[0, 'label'] == 'CA2'):
        CA2start = nCells[CA2] / 2
        ax.text(hTab,
                CA2start + vCorrection,
                strng,
                horizontalalignment="center",
                rotation=90,
                fontsize=displayFontSize,
                color=c.BLACK)
    elif (subregionDF.loc[0, 'label'] == 'All'):
        CA2start = sum(nCells[:CA2]) + nCells[CA2] / 2
        ax.text(hTab,
                CA2start + vCorrection,
                strng,
                horizontalalignment="center",
                rotation=90,
                fontsize=displayFontSize,
                color=c.BLACK)
    strng = 'CA1'
    vCorrection = 0.0
    if (subregionDF.loc[0, 'label'] == 'CA1'):
        CA1start = nCells[CA1] / 2
        ax.text(hTab,
                CA1start + vCorrection,
                strng,
                horizontalalignment="center",
                rotation=90,
                fontsize=displayFontSize,
                color=c.BLACK)
    elif (subregionDF.loc[0, 'label'] == 'All'):
        CA1start = sum(nCells[:CA1]) + nCells[CA1] / 2
        ax.text(hTab,
                CA1start + vCorrection,
                strng,
                horizontalalignment="center",
                rotation=90,
                fontsize=displayFontSize,
                color=c.BLACK)
    strng = 'Sub'
    vCorrection = 0.0
    if (subregionDF.loc[0, 'label'] == 'Sub'):
        Substart = nCells[Sub] / 2
        ax.text(hTab,
                Substart + vCorrection,
                strng,
                horizontalalignment="center",
                rotation=90,
                fontsize=displayFontSize,
                color=c.BLACK)
    elif (subregionDF.loc[0, 'label'] == 'All'):
        Substart = sum(nCells[:Sub]) + nCells[Sub] / 2
        ax.text(hTab,
                Substart + vCorrection,
                strng,
                horizontalalignment="center",
                rotation=90,
                fontsize=displayFontSize,
                color=c.BLACK)
    strng = 'EC'
    vCorrection = 0.0
    if (subregionDF.loc[0, 'label'] == 'EC'):
        ECstart = nCells[EC] / 2
        ax.text(hTab,
                ECstart + vCorrection,
                strng,
                horizontalalignment="center",
                rotation=90,
                fontsize=displayFontSize,
                color=c.BLACK)
    elif (subregionDF.loc[0, 'label'] == 'All'):
        ECstart = sum(nCells[:EC]) + nCells[EC] / 2
        ax.text(hTab,
                ECstart + vCorrection,
                strng,
                horizontalalignment="center",
                rotation=90,
                fontsize=displayFontSize,
                color=c.BLACK)

    # thick horizontal lines on cell type axis
    if (subregionDF.loc[0, 'label'] == 'All'):
        lineWidth = 0.2
        CA3line = sum(nCells[:CA3])
        CA2line = sum(nCells[:CA2])
        CA1line = sum(nCells[:CA1])
        Subline = sum(nCells[:Sub])
        ECline = sum(nCells[:EC])

        ax.plot([hTab, nParcels], [CA3line, CA3line],
                linewidth=lineWidth,
                color=c.BLACK)
        ax.plot([hTab, nParcels], [CA2line, CA2line],
                linewidth=lineWidth,
                color=c.BLACK)
        ax.plot([hTab, nParcels], [CA1line, CA1line],
                linewidth=lineWidth,
                color=c.BLACK)
        ax.plot([hTab, nParcels], [Subline, Subline],
                linewidth=lineWidth,
                color=c.BLACK)
        ax.plot([hTab, nParcels], [ECline, ECline],
                linewidth=lineWidth,
                color=c.BLACK)

    # plot labels
    for i in range(START, END):
        hTab = -0.5
        labelCode = "{0:s} ({1:d})".format(overlapBinaryDF.loc[i, 'Names'],
                                           int(meansDF.loc[i, 'Factors']))
        labelColor = c.BLACK
        if overlapBinaryDF.loc[i, 'EorI'] == 'I':
            labelColor = c.GRAY
        ax.text(hTab,
                i - START + 0.5,
                labelCode,
                rotation=0,
                horizontalalignment="right",
                verticalalignment="center",
                fontsize=displayFontSize,
                color=labelColor)

    nMeanValues = len(meansDF)

    print "Plotting mean quantity values ..."

    for i in range(nMeanValues):
        vTab = int(overlapBinaryDF['UniqueIDs'].index[
            overlapBinaryDF['UniqueIDs'] == meansDF.loc[
                i, 'UniqueIDs']].tolist() - START)
        neuriteStr = meansDF.loc[i, 'Neurites']
        idx = [pos for pos, char in enumerate(neuriteStr) if char == ':']
        parcel = neuriteStr[:idx[1]]
        hTab = parcel_lookup(parcel)
        neurite = neuriteStr[idx[1] + 1:]

        if (np.isnan(meansDF.loc[i, 'Means'])):
            strng = '?'
        else:
            value = meansDF.loc[i, 'Means']
            if (quantitySelectionsDF.loc[0, 'selection'] == 1
                ):  # total neurite length
                strng = "{0:.0f}".format(value)
            elif (quantitySelectionsDF.loc[0, 'selection'] == 2
                  ):  # percent of neurite tree
                strng = "{0:.2f}".format(value)
            elif (quantitySelectionsDF.loc[0, 'selection'] == 3):  # density
                strng = "{0:.4f}".format(value)
            elif (quantitySelectionsDF.loc[0, 'selection'] == 4
                  ):  # average maximum path length
                strng = "{0:.0f}".format(value)
            else:  # (quantitySelectionsDF.loc[0, 'selection'] == 2): # average mean path length
                strng = "{0:.0f}".format(value)

        if (((meansDF.loc[i, 'UniqueIDs'] // 1000) == subregionDF.loc[0,
                                                                      'code'])
                or (subregionDF.loc[0, 'code'] == 0)):
            if (neurite == 'A'):
                if (overlapBinaryDataDF.iloc[vTab + START,
                                             hTab] == 1):  # axons only
                    ax.add_patch(
                        patches.Rectangle((hTab + 0.02, vTab + 0.02),
                                          0.96,
                                          0.48,
                                          edgecolor="none",
                                          facecolor=c.RED))
                    ax.add_patch(
                        patches.Rectangle((hTab + 0.02, vTab + 0.5),
                                          0.96,
                                          0.48,
                                          edgecolor="none",
                                          facecolor=c.WHITE))
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2)
                            and meansDF.loc[i, 'Means'] >=
                            15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.25,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.WHITE)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and
                          meansDF.loc[i, 'Means'] < 15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.25,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.YELLOW)
                    else:
                        ax.text(hTab + 0.5,
                                vTab + 0.25,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.WHITE)
                elif (overlapBinaryDataDF.iloc[vTab + START, hTab] == 3
                      ):  # axons from axons and dendrites
                    ax.add_patch(
                        patches.Rectangle((hTab + 0.02, vTab + 0.02),
                                          0.96,
                                          0.48,
                                          edgecolor="none",
                                          facecolor=c.RED))
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2)
                            and meansDF.loc[i, 'Means'] >=
                            15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.25,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.WHITE)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and
                          meansDF.loc[i, 'Means'] < 15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.25,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.YELLOW)
                    else:
                        ax.text(hTab + 0.5,
                                vTab + 0.25,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.WHITE)
                elif (overlapBinaryDataDF.iloc[vTab + START,
                                               hTab] == 2):  # dendrites only
                    ax.add_patch(
                        patches.Rectangle((hTab + 0.02, vTab + 0.02),
                                          0.96,
                                          0.48,
                                          edgecolor="none",
                                          facecolor=c.WHITE))
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2)
                            and meansDF.loc[i, 'Means'] >=
                            15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.25,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.BLACK)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and
                          meansDF.loc[i, 'Means'] < 15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.25,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.GREEN)
                    else:
                        ax.text(hTab + 0.5,
                                vTab + 0.25,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.BLACK)
                else:  # (overlapBinaryDataDF.iloc[vTab+START, hTab] == 0): # no axons or dendrites
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2)
                            and meansDF.loc[i, 'Means'] >=
                            15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.25,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.RED)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and
                          meansDF.loc[i, 'Means'] < 15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.25,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.GREEN)
                    else:
                        ax.text(hTab + 0.5,
                                vTab + 0.25,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.BLACK)
                    ax.plot([hTab, hTab + 1], [vTab + 0.5, vTab + 0.5],
                            linewidth=0.1,
                            color=c.BLACK)  # diving line
            if (neurite == 'D'):
                if (overlapBinaryDataDF.iloc[vTab + START,
                                             hTab] == 2):  # dendrites only
                    ax.add_patch(
                        patches.Rectangle((hTab + 0.02, vTab + 0.5),
                                          0.96,
                                          0.48,
                                          edgecolor="none",
                                          facecolor=c.BLUE))
                    ax.add_patch(
                        patches.Rectangle((hTab + 0.02, vTab + 0.02),
                                          0.96,
                                          0.48,
                                          edgecolor="none",
                                          facecolor=c.WHITE))
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2)
                            and meansDF.loc[i, 'Means'] >=
                            15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.75,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.WHITE)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and
                          meansDF.loc[i, 'Means'] < 15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.75,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.YELLOW)
                    else:
                        ax.text(hTab + 0.5,
                                vTab + 0.75,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.WHITE)
                elif (overlapBinaryDataDF.iloc[vTab + START, hTab] == 3
                      ):  # dendrites from axons and dendrites
                    ax.add_patch(
                        patches.Rectangle((hTab + 0.02, vTab + 0.5),
                                          0.96,
                                          0.48,
                                          edgecolor="none",
                                          facecolor=c.BLUE))
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2)
                            and meansDF.loc[i, 'Means'] >=
                            15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.75,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.WHITE)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and
                          meansDF.loc[i, 'Means'] < 15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.75,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.YELLOW)
                    else:
                        ax.text(hTab + 0.5,
                                vTab + 0.75,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.WHITE)
                elif (overlapBinaryDataDF.iloc[vTab + START,
                                               hTab] == 1):  # axons only
                    ax.add_patch(
                        patches.Rectangle((hTab + 0.02, vTab + 0.5),
                                          0.96,
                                          0.48,
                                          edgecolor="none",
                                          facecolor=c.WHITE))
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2)
                            and meansDF.loc[i, 'Means'] >=
                            15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.75,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.BLACK)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and
                          meansDF.loc[i, 'Means'] < 15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.75,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.GREEN)
                    else:
                        ax.text(hTab + 0.5,
                                vTab + 0.75,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.BLACK)
                else:  # (overlapBinaryDataDF.iloc[vTab+START, hTab] == 0): # no axons or dendrites
                    if ((quantitySelectionsDF.loc[0, 'selection'] == 2)
                            and meansDF.loc[i, 'Means'] >=
                            15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.75,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.BLUE)
                    elif ((quantitySelectionsDF.loc[0, 'selection'] == 2) and
                          meansDF.loc[i, 'Means'] < 15):  # % of neurite tree
                        ax.text(hTab + 0.5,
                                vTab + 0.75,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.GREEN)
                    else:
                        ax.text(hTab + 0.5,
                                vTab + 0.75,
                                strng,
                                horizontalalignment="center",
                                rotation=0,
                                verticalalignment="center",
                                fontsize=displayFontSize / 2.0,
                                color=c.BLACK)
                    ax.plot([hTab, hTab + 1], [vTab + 0.5, vTab + 0.5],
                            linewidth=0.1,
                            color=c.BLACK)  # diving line
    plt.show()

    # save plot
    outputFileName = "output/{0:s}_matrix_{1:s}.pdf".format(
        quantitySelectionsDF.loc[0, 'plotString'], time_stamp())
    print "Saving data to pdf file %s ...\n" % outputFileName
    fig.savefig(outputFileName, dpi=600)

    return
def plot_probabilities(nCells, overlapBinaryDF, overlapBinaryProbabilityDF,
                       gradedProbabilitiesDF):

    import matplotlib.pyplot as plt
    import matplotlib.patches as patches

    from diek_functions import pause
    from diek_functions import time_stamp

    from plot_functions import HCcolors as c
    from plot_functions import make_color_map

    nNeurons = len(overlapBinaryDF)

    regionColors = [
        c.BROWN_DG, c.BROWN_CA3, c.YELLOW_CA2, c.ORANGE_CA1, c.YELLOW_Sub,
        c.GREEN_EC
    ]
    DG = 0
    CA3 = 1
    CA2 = 2
    CA1 = 3
    Sub = 4
    EC = 5

    probabilityColors = [c.GRAY_LIGHT, c.WHITE, c.GRAY_DARK]

    nProbabilityColors = len(probabilityColors)

    print "Making probability color map ..."
    probabilityColorMap = make_color_map(probabilityColors)

    print "Plotting probability background data ..."
    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax.set_aspect("equal")

    ax.axis('off')

    plt.pcolormesh(overlapBinaryProbabilityDF.iloc[:, :],
                   cmap=probabilityColorMap)

    plt.xlim(-10, nNeurons)
    plt.ylim(-10, nNeurons)

    plt.gca().invert_yaxis()

    displayFontSize = 0.5

    # color-coded tags for the subregions
    ax.add_patch(
        patches.Rectangle((sum(nCells[:DG]), -1),
                          nCells[DG],
                          1,
                          edgecolor="none",
                          facecolor=c.BROWN_DG))
    ax.add_patch(
        patches.Rectangle((sum(nCells[:CA3]), -1),
                          nCells[CA3],
                          1,
                          edgecolor="none",
                          facecolor=c.BROWN_CA3))
    ax.add_patch(
        patches.Rectangle((sum(nCells[:CA2]), -1),
                          nCells[CA2],
                          1,
                          edgecolor="none",
                          facecolor=c.YELLOW_CA2))
    ax.add_patch(
        patches.Rectangle((sum(nCells[:CA1]), -1),
                          nCells[CA1],
                          1,
                          edgecolor="none",
                          facecolor=c.ORANGE_CA1))
    ax.add_patch(
        patches.Rectangle((sum(nCells[:Sub]), -1),
                          nCells[Sub],
                          1,
                          edgecolor="none",
                          facecolor=c.YELLOW_Sub))
    ax.add_patch(
        patches.Rectangle((sum(nCells[:EC]), -1),
                          nCells[EC],
                          1,
                          edgecolor="none",
                          facecolor=c.GREEN_EC))

    ax.add_patch(
        patches.Rectangle((-1, sum(nCells[:DG])),
                          1,
                          nCells[DG],
                          edgecolor="none",
                          facecolor=c.BROWN_DG))
    ax.add_patch(
        patches.Rectangle((-1, sum(nCells[:CA3])),
                          1,
                          nCells[CA3],
                          edgecolor="none",
                          facecolor=c.BROWN_CA3))
    ax.add_patch(
        patches.Rectangle((-1, sum(nCells[:CA2])),
                          1,
                          nCells[CA2],
                          edgecolor="none",
                          facecolor=c.YELLOW_CA2))
    ax.add_patch(
        patches.Rectangle((-1, sum(nCells[:CA1])),
                          1,
                          nCells[CA1],
                          edgecolor="none",
                          facecolor=c.ORANGE_CA1))
    ax.add_patch(
        patches.Rectangle((-1, sum(nCells[:Sub])),
                          1,
                          nCells[Sub],
                          edgecolor="none",
                          facecolor=c.YELLOW_Sub))
    ax.add_patch(
        patches.Rectangle((-1, sum(nCells[:EC])),
                          1,
                          nCells[EC],
                          edgecolor="none",
                          facecolor=c.GREEN_EC))

    # parcellation subregion headers
    tab = [
        sum(nCells[:DG]) + (nCells[DG] + 1) / 2,
        sum(nCells[:CA3]) + (nCells[CA3] + 1) / 2,
        sum(nCells[:CA2]) + (nCells[CA2] + 1) / 2,
        sum(nCells[:CA1]) + (nCells[CA1] + 1) / 2,
        sum(nCells[:Sub]) + (nCells[Sub] + 1) / 2,
        sum(nCells[:EC]) + (nCells[EC] + 1) / 2
    ]
    ax.text(tab[DG],
            -0.25,
            "DG",
            rotation=0,
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.WHITE)
    ax.text(tab[CA3],
            -0.25,
            "CA3",
            rotation=0,
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.WHITE)
    ax.text(tab[CA2],
            -0.25,
            "CA2",
            rotation=0,
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.BLACK)
    ax.text(tab[CA1],
            -0.25,
            "CA1",
            rotation=0,
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.WHITE)
    ax.text(tab[Sub],
            -0.25,
            "Sub",
            rotation=0,
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.BLACK)
    ax.text(tab[EC],
            -0.25,
            "EC",
            rotation=0,
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.WHITE)

    ax.text(-0.5,
            tab[DG],
            "DG",
            rotation=90,
            verticalalignment="bottom",
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.WHITE)
    ax.text(-0.5,
            tab[CA3],
            "CA3",
            rotation=90,
            verticalalignment="bottom",
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.WHITE)
    ax.text(-0.5,
            tab[CA2],
            "CA2",
            rotation=90,
            verticalalignment="bottom",
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.BLACK)
    ax.text(-0.5,
            tab[CA1],
            "CA1",
            rotation=90,
            verticalalignment="bottom",
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.WHITE)
    ax.text(-0.5,
            tab[Sub],
            "Sub",
            rotation=90,
            verticalalignment="bottom",
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.BLACK)
    ax.text(-0.5,
            tab[EC],
            "EC",
            rotation=90,
            verticalalignment="bottom",
            horizontalalignment="center",
            fontsize=displayFontSize,
            color=c.WHITE)

    for i in range(nNeurons):
        textColor = c.BLACK
        if overlapBinaryDF.loc[i, 'EorI'] == 'I':
            textColor = c.GRAY
        ax.text(i + 0.5,
                -1.25,
                overlapBinaryDF.loc[i, 'Abbreviations'],
                rotation=90,
                horizontalalignment="center",
                verticalalignment="bottom",
                fontsize=displayFontSize,
                color=textColor)
        ax.text(-1.25,
                i + 0.5,
                overlapBinaryDF.loc[i, 'Abbreviations'],
                rotation=0,
                horizontalalignment="right",
                verticalalignment="center",
                fontsize=displayFontSize,
                color=textColor)

    for i in range(nNeurons):
        for j in range(nNeurons):
            if gradedProbabilityDF.iloc[i, j] > 0:
                strng = '{:.2e}'.format(gradedProbabilityDF.iloc[i, j])
                if overlapBinaryProbabilityDF.iloc[i, j] == 0:
                    textColor = c.BLACK
                    if overlapBinaryDF.loc[i, 'EorI'] == 'I':
                        textColor = c.GRAY
                    ax.text(j + 1.5,
                            i + 0.5,
                            strng,
                            rotation=0,
                            horizontalalignment="center",
                            fontsize=displayFontSize / 2,
                            color=textColor)
                else:
                    faceColor = c.BLACK
                    if overlapBinaryDF.loc[i, 'EorI'] == 'I':
                        faceColor = c.GRAY
                    ax.add_patch(
                        patches.Rectangle(
                            (j, i),  # (x,y)
                            1,  # width
                            1,  # height
                            edgecolor="none",
                            facecolor=faceColor))
                    ax.text(j + 1.5,
                            i + 0.5,
                            strng,
                            rotation=0,
                            horizontalalignment="center",
                            fontsize=displayFontSize / 2,
                            color=c.WHITE)

    plt.show()

    # save plot
    outputFileName = "output/probability_matrix_%s.pdf" % time_stamp()
    print "Saving data to pdf file %s ...\n" % outputFileName
    fig.savefig(outputFileName, dpi=600)

    return ()
def plot_probabilities(nCells, overlapBinaryDF, overlapBinaryProbabilityDF, gradedProbabilitiesDF):

    import matplotlib.pyplot as plt
    import matplotlib.patches as patches

    from diek_functions import pause
    from diek_functions import time_stamp

    from plot_functions import HCcolors as c
    from plot_functions import make_color_map

    nNeurons = len(overlapBinaryDF)
    
    regionColors = [c.BROWN_DG,
                    c.BROWN_CA3,
                    c.YELLOW_CA2,
                    c.ORANGE_CA1,
                    c.YELLOW_Sub,
                    c.GREEN_EC]
    DG = 0
    CA3 = 1
    CA2 = 2
    CA1 = 3
    Sub = 4
    EC = 5
    
    probabilityColors = [c.GRAY_LIGHT,
                         c.WHITE,
                         c.GRAY_DARK]
              
    nProbabilityColors = len(probabilityColors)
    
    print "Making probability color map ..."
    probabilityColorMap = make_color_map(probabilityColors)
    
    print "Plotting probability background data ..."
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.set_aspect("equal")
    
    ax.axis('off')
    
    plt.pcolormesh(overlapBinaryProbabilityDF.iloc[:,:], cmap=probabilityColorMap)
    
    plt.xlim(-10, nNeurons)
    plt.ylim(-10, nNeurons)
    
    plt.gca().invert_yaxis()
    
    displayFontSize = 0.5
    
    # color-coded tags for the subregions
    ax.add_patch(patches.Rectangle((sum(nCells[:DG]), -1), nCells[DG], 1, edgecolor="none", facecolor=c.BROWN_DG))
    ax.add_patch(patches.Rectangle((sum(nCells[:CA3]), -1), nCells[CA3], 1, edgecolor="none", facecolor=c.BROWN_CA3))
    ax.add_patch(patches.Rectangle((sum(nCells[:CA2]), -1), nCells[CA2], 1, edgecolor="none", facecolor=c.YELLOW_CA2))
    ax.add_patch(patches.Rectangle((sum(nCells[:CA1]), -1), nCells[CA1], 1, edgecolor="none", facecolor=c.ORANGE_CA1))
    ax.add_patch(patches.Rectangle((sum(nCells[:Sub]), -1), nCells[Sub], 1, edgecolor="none", facecolor=c.YELLOW_Sub))
    ax.add_patch(patches.Rectangle((sum(nCells[:EC]), -1), nCells[EC], 1, edgecolor="none", facecolor=c.GREEN_EC))
    
    ax.add_patch(patches.Rectangle((-1, sum(nCells[:DG])), 1, nCells[DG], edgecolor="none", facecolor=c.BROWN_DG))
    ax.add_patch(patches.Rectangle((-1, sum(nCells[:CA3])), 1, nCells[CA3], edgecolor="none", facecolor=c.BROWN_CA3))
    ax.add_patch(patches.Rectangle((-1, sum(nCells[:CA2])), 1, nCells[CA2], edgecolor="none", facecolor=c.YELLOW_CA2))
    ax.add_patch(patches.Rectangle((-1, sum(nCells[:CA1])), 1, nCells[CA1], edgecolor="none", facecolor=c.ORANGE_CA1))
    ax.add_patch(patches.Rectangle((-1, sum(nCells[:Sub])), 1, nCells[Sub], edgecolor="none", facecolor=c.YELLOW_Sub))
    ax.add_patch(patches.Rectangle((-1, sum(nCells[:EC])), 1, nCells[EC], edgecolor="none", facecolor=c.GREEN_EC))

    # parcellation subregion headers
    tab = [sum(nCells[:DG]) + (nCells[DG]+1)/2,
           sum(nCells[:CA3]) + (nCells[CA3]+1)/2,
           sum(nCells[:CA2]) + (nCells[CA2]+1)/2,
           sum(nCells[:CA1]) + (nCells[CA1]+1)/2,
           sum(nCells[:Sub]) + (nCells[Sub]+1)/2,
           sum(nCells[:EC]) + (nCells[EC]+1)/2
          ]
    ax.text(tab[DG], -0.25, "DG", rotation=0, horizontalalignment="center", fontsize=displayFontSize, color=c.WHITE)
    ax.text(tab[CA3], -0.25, "CA3", rotation=0, horizontalalignment="center", fontsize=displayFontSize, color=c.WHITE)
    ax.text(tab[CA2], -0.25, "CA2", rotation=0, horizontalalignment="center", fontsize=displayFontSize, color=c.BLACK)
    ax.text(tab[CA1], -0.25, "CA1", rotation=0, horizontalalignment="center", fontsize=displayFontSize, color=c.WHITE)
    ax.text(tab[Sub], -0.25, "Sub", rotation=0, horizontalalignment="center", fontsize=displayFontSize, color=c.BLACK)
    ax.text(tab[EC], -0.25, "EC", rotation=0, horizontalalignment="center", fontsize=displayFontSize, color=c.WHITE)

    ax.text(-0.5, tab[DG], "DG", rotation=90, verticalalignment="bottom", horizontalalignment="center",
            fontsize=displayFontSize, color=c.WHITE)
    ax.text(-0.5, tab[CA3], "CA3", rotation=90, verticalalignment="bottom", horizontalalignment="center",
            fontsize=displayFontSize, color=c.WHITE)
    ax.text(-0.5, tab[CA2], "CA2", rotation=90, verticalalignment="bottom", horizontalalignment="center",
            fontsize=displayFontSize, color=c.BLACK)
    ax.text(-0.5, tab[CA1], "CA1", rotation=90, verticalalignment="bottom", horizontalalignment="center",
            fontsize=displayFontSize, color=c.WHITE)
    ax.text(-0.5, tab[Sub], "Sub", rotation=90, verticalalignment="bottom", horizontalalignment="center",
            fontsize=displayFontSize, color=c.BLACK)
    ax.text(-0.5, tab[EC], "EC", rotation=90, verticalalignment="bottom", horizontalalignment="center",
            fontsize=displayFontSize, color=c.WHITE)

    for i in range(nNeurons):
        textColor = c.BLACK
        if overlapBinaryDF.loc[i, 'EorI'] == 'I':
            textColor = c.GRAY
        ax.text(i+0.5, -1.25, overlapBinaryDF.loc[i, 'Abbreviations'], rotation=90, horizontalalignment="center",
                verticalalignment="bottom", fontsize=displayFontSize, color=textColor)
        ax.text(-1.25, i+0.5, overlapBinaryDF.loc[i, 'Abbreviations'], rotation=0, horizontalalignment="right",
                verticalalignment="center", fontsize=displayFontSize, color=textColor)

    for i in range(nNeurons):
        for j in range(nNeurons):
            if gradedProbabilityDF.iloc[i, j] > 0:
                strng = '{:.2e}'.format(gradedProbabilityDF.iloc[i, j])
                if overlapBinaryProbabilityDF.iloc[i, j] == 0:
                    textColor = c.BLACK
                    if overlapBinaryDF.loc[i, 'EorI'] == 'I':
                        textColor = c.GRAY
                    ax.text(j+1.5, i+0.5, strng, rotation=0, horizontalalignment="center", fontsize=displayFontSize/2,
                            color=textColor)
                else:
                    faceColor = c.BLACK
                    if overlapBinaryDF.loc[i, 'EorI'] == 'I':
                        faceColor = c.GRAY
                    ax.add_patch(patches.Rectangle((j, i), # (x,y)
                                                   1,             # width
                                                   1,             # height
                                                   edgecolor="none",
                                                   facecolor=faceColor
                                                  )
                                )
                    ax.text(j+1.5, i+0.5, strng, rotation=0, horizontalalignment="center", fontsize=displayFontSize/2,
                            color=c.WHITE)

    plt.show()
    
    # save plot
    outputFileName = "output/probability_matrix_%s.pdf" % time_stamp()
    print "Saving data to pdf file %s ...\n" % outputFileName    
    fig.savefig(outputFileName, dpi=600)
    
    return()
    
def plot_fuzzy_morphology_data(morphologyDataFrame,dataFrame,isPlotSupertypesOnly):
    from lib.constants import *
    
    BLACK             = (   0,   0,   0 )
    BLUE              = (   0,   0, 255 )
    BLUE_DARK         = (   0,   0, 128 )
    BLUE_MEDIUM       = (   0,   0, 192 )
    BLUE_LIGHT        = ( 143, 172, 255 )
    BLUE_SKY          = (   0, 204, 255 )
    BLUE_UBERLIGHT    = ( 227, 233, 255 )
    BLUE_ULTRALIGHT   = ( 199, 214, 255 )
    BROWN             = ( 153, 102,  51 )
    BROWN_DG          = (  91,  45,  10 )
    BROWN_CA3         = ( 165, 131, 107 )
    GRAY              = ( 128, 128, 128 )
    GRAY_DARK         = (  64,  64,  64 )
    GRAY_MEDIUM       = (  96,  96,  96 )
    GRAY_LIGHT        = ( 192, 192, 192 )
    GRAY_ULTRALIGHT   = ( 230, 230, 230 )
    GREEN             = (   0, 255,   0 )
    GREEN_MEDIUM      = (   0, 192,   0 )
    GREEN_BRIGHT      = (   0, 255,   0 )
    GREEN_EC          = ( 106, 149,  49 )
    GREEN_LEC         = (  90, 111,  47 )
    GREEN_MEC         = ( 122, 187,  51 )
    ORANGE            = ( 228, 108,  10 )
    ORANGE_LIGHT      = ( 247, 156,  21 )
    ORANGE_CA1        = ( 217, 104,  13 )
    PURPLE            = ( 128,   0, 128 )
    PURPLE_LIGHT      = ( 178, 128, 178 )
    PURPLE_UBERLIGHT  = ( 236, 224, 236 )
    PURPLE_ULTRALIGHT = ( 217, 192, 217 )
    RED               = ( 255,   0,   0 )
    RED_LIGHT         = ( 255, 178, 178 )
    RED_UBERLIGHT     = ( 255, 236, 236 )
    RED_ULTRALIGHT    = ( 255, 216, 216 )
    TEAL              = (   0, 255, 192 )
    WHITE             = ( 255, 255, 255 )
    YELLOW            = ( 255, 255,   0 )
    YELLOW_CA2        = ( 255, 255,   0 )
    YELLOW_Sub        = ( 255, 192,   0 )

    morphologyDataFrameUnique = morphologyDataFrame.drop_duplicates()
    dataFrameSubset = dataFrame.iloc[0,[SUBREGION_COL,S_DG_COL:SF_D_EC_LVI_COL]]
    dataframeSubsetUnique = dataFrameSubset.drop_duplicates()
    
    nTypes, nParcels = morphologyDataFrameUnique.shape

    nParcellations = (4,5,4,4,3,6);
    
    layerNames = ('SMo','SMi','SG','H',
                  'SLM','SR','SL','SP','SO',
                  'SLM','SR','SP','SO',
                  'SLM','SR','SP','SO',
                  'SM','SP','PL',
                  'I','II','III','IV','V','VI')
                  
    nCells = dataframeSubsetUnique['Subregion'].value_counts()

    displayFontSize = 3
    if (nTypes < 100):
        displayFontSize = 8
    if (nTypes < 50):
        displayFontSize = 10
        
    shadingLineWidths = 1.0

    colors = [WHITE,
              BLUE_ULTRALIGHT,
              BLUE_LIGHT,
              BLUE,
              RED_ULTRALIGHT,
              PURPLE_ULTRALIGHT,
              (RED_ULTRALIGHT+BLUE_LIGHT)/2,
              (RED_ULTRALIGHT+BLUE)/2,
              RED_LIGHT,
              (RED_LIGHT+BLUE_ULTRALIGHT)/2,
              PURPLE_LIGHT,
              RED,
              (RED+BLUE_YULTRALIGHT)/2,
              (RED+BLUE_LIGHT)/2,
              PURPLE]
              
    nColors = len(colors)
    
    regionColors = [BROWN_DG,
                    BROWN_CA3,
                    YELLOW_CA2,
                    ORANGE_CA1,
                    YELLOW_SUB,
                    GREEN_EC]
    
    print "Making color map ...\n"
    morphologyColorMap = make_color_map(colors)
    
    print "Plotting data ...\n"
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.set_aspect("equal")
    
    plt.pcolormesh(morphologyDataFrameUnique, cmap=morphologyColorMap)
    
    plt.xlim(0, nPatterns)
    plt.ylim(0, nTypes)
    
    plt.gca().invert_yaxis()
        
    nDGcells  = 18
    nCA3cells = 25
    nCA2cells =  5
    nCA1cells = 40
    nSubcells =  3
    nECcells  = 31
    
    line_width = 1.25
    
    DGline = nDGcells
    CA3line = DGline + nCA3cells
    CA2line = CA3line + nCA2cells
    CA1line = CA2line + nCA1cells
    Subline = CA1line + nSubcells
    
    plt.plot([0, nPatterns], [DGline, DGline], "b", linewidth=line_width)
    plt.plot([0, nPatterns], [CA3line, CA3line], "b", linewidth=line_width)
    plt.plot([0, nPatterns], [CA2line, CA2line], "b", linewidth=line_width)
    plt.plot([0, nPatterns], [CA1line, CA1line], "b", linewidth=line_width)
    plt.plot([0, nPatterns], [Subline, Subline], "b", linewidth=line_width)
    
    plt.tick_params(
        axis="x",          # changes apply to the x-axis
        which="both",      # both major and minor ticks are affected
        bottom="off",      # ticks along the bottom edge are off
        top="off",         # ticks along the top edge are off
        labelbottom="off", # labels along the bottom edge are off
        labeltop="off")    # labels along the top edge are off
        
    plt.tick_params(
        axis="y",          # changes apply to the x-axis
        which="both",      # both major and minor ticks are affected
        left="off",        # ticks along the left edge are off
        right="off",       # ticks along the right edge are off
        labelleft="off",   # labels along the left edge are off
        labelright="off")  # labels along the right edge are off
        
    if nCells > 50:
        fontSize = 2.5
    elif nCells > 25:
        fontSize = 5
    else:
        fontSize = 10
        
    print "Labeling axes ...\n"
    for i in range(nCells):
        
        ax.text(-0.5, float(i+1), cellLabels[i], rotation=0, horizontalalignment="right", fontname="Helvetica", fontsize=fontSize)

    for j in range(nPatterns):
        
        ax.text(float(j), -0.5, patternLabels[j], rotation=90, verticalalignment="bottom", fontname="Helvetica", fontsize=fontSize)
    
    ax.text(nPatterns+0.5, 1, "1 occurrence of the pattern", rotation=0, horizontalalignment="left", fontname="Helvetica", fontsize=fontSize,
            color=clrs.ORANGE)
    ax.text(nPatterns+0.5, 2, "2 occurrences of the pattern", rotation=0, horizontalalignment="left", fontname="Helvetica", fontsize=fontSize,
            color=clrs.TEAL)
    ax.text(nPatterns+0.5, 3, "3 occurrences of the pattern", rotation=0, horizontalalignment="left", fontname="Helvetica", fontsize=fontSize,
            color=clrs.BROWN)
    ax.text(nPatterns+0.5, 4, "4 occurrences of the pattern", rotation=0, horizontalalignment="left", fontname="Helvetica", fontsize=fontSize,
            color=clrs.GRAY)
    
    DGlabelPosition = nDGcells/2
    CA3labelPosition = nDGcells + nCA3cells/2
    CA2labelPosition = nDGcells + nCA3cells + nCA2cells/2
    CA1labelPosition = nDGcells + nCA3cells + nCA2cells + nCA1cells/2
    SublabelPosition = nDGcells + nCA3cells + nCA2cells + nCA1cells + nSubcells/2
    EClabelPosition = nDGcells + nCA3cells + nCA2cells + nCA1cells + nSubcells + nECcells/2
        
    ax.text(-12, DGlabelPosition, "DG", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, CA3labelPosition, "CA3", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, CA2labelPosition, "CA2", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, CA1labelPosition, "CA1", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, SublabelPosition, "Sub", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, EClabelPosition, "EC", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
        
    outputFileName = "FP_plot_output/FP_matrix_%s.jpg" % time_stamp()
    print "Saving data to jpg file %s ...\n" % outputFileName    
    fig.savefig(outputFileName, dpi=600)
    
#    plt.show()

    return
    
Example #9
0
    ax.text(nPatterns+0.5, 1, "1 occurrence of the pattern", rotation=0, horizontalalignment="left", fontname="Helvetica", fontsize=fontSize,
            color=clrs.ORANGE)
    ax.text(nPatterns+0.5, 2, "2 occurrences of the pattern", rotation=0, horizontalalignment="left", fontname="Helvetica", fontsize=fontSize,
            color=clrs.TEAL)
    ax.text(nPatterns+0.5, 3, "3 occurrences of the pattern", rotation=0, horizontalalignment="left", fontname="Helvetica", fontsize=fontSize,
            color=clrs.BROWN)
    ax.text(nPatterns+0.5, 4, "4 occurrences of the pattern", rotation=0, horizontalalignment="left", fontname="Helvetica", fontsize=fontSize,
            color=clrs.GRAY)
    
    DGlabelPosition = nDGcells/2
    CA3labelPosition = nDGcells + nCA3cells/2
    CA2labelPosition = nDGcells + nCA3cells + nCA2cells/2
    CA1labelPosition = nDGcells + nCA3cells + nCA2cells + nCA1cells/2
    SublabelPosition = nDGcells + nCA3cells + nCA2cells + nCA1cells + nSubcells/2
    EClabelPosition = nDGcells + nCA3cells + nCA2cells + nCA1cells + nSubcells + nECcells/2
        
    ax.text(-12, DGlabelPosition, "DG", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, CA3labelPosition, "CA3", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, CA2labelPosition, "CA2", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, CA1labelPosition, "CA1", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, SublabelPosition, "Sub", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, EClabelPosition, "EC", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
        
    outputFileName = "FP_plot_output/FP_matrix_%s.jpg" % time_stamp()
    print "Saving data to jpg file %s ...\n" % outputFileName    
    fig.savefig(outputFileName, dpi=600)
    
#    plt.show()

    return
    
Example #10
0
def plot_patterns(cellLabels, patternLabels, nPatternOccurrences):

    nCells = len(cellLabels)
    nPatterns = len(patternLabels)

    colors = [clrs.WHITE, clrs.ORANGE, clrs.TEAL, clrs.BROWN, clrs.GRAY]

    nColors = len(colors)

    print "Making color map ...\n"
    firingPatternColorMap = make_color_map(colors)

    print "Plotting data ...\n"
    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax.set_aspect("equal")

    plt.pcolormesh(nPatternOccurrences, cmap=firingPatternColorMap)

    plt.xlim(0, nPatterns)
    plt.ylim(0, nCells)

    plt.gca().invert_yaxis()

    print "Plotting hatch lines ...\n"
    plot_hatch_lines(nPatternOccurrences)

    nDGcells = 18
    nCA3cells = 25
    nCA2cells = 5
    nCA1cells = 40
    nSubcells = 3
    nECcells = 31

    line_width = 1.25

    DGline = nDGcells
    CA3line = DGline + nCA3cells
    CA2line = CA3line + nCA2cells
    CA1line = CA2line + nCA1cells
    Subline = CA1line + nSubcells

    plt.plot([0, nPatterns], [DGline, DGline], "b", linewidth=line_width)
    plt.plot([0, nPatterns], [CA3line, CA3line], "b", linewidth=line_width)
    plt.plot([0, nPatterns], [CA2line, CA2line], "b", linewidth=line_width)
    plt.plot([0, nPatterns], [CA1line, CA1line], "b", linewidth=line_width)
    plt.plot([0, nPatterns], [Subline, Subline], "b", linewidth=line_width)

    plt.tick_params(
        axis="x",  # changes apply to the x-axis
        which="both",  # both major and minor ticks are affected
        bottom="off",  # ticks along the bottom edge are off
        top="off",  # ticks along the top edge are off
        labelbottom="off",  # labels along the bottom edge are off
        labeltop="off")  # labels along the top edge are off

    plt.tick_params(
        axis="y",  # changes apply to the x-axis
        which="both",  # both major and minor ticks are affected
        left="off",  # ticks along the left edge are off
        right="off",  # ticks along the right edge are off
        labelleft="off",  # labels along the left edge are off
        labelright="off")  # labels along the right edge are off

    if nCells > 50:
        fontSize = 2.5
    elif nCells > 25:
        fontSize = 5
    else:
        fontSize = 10

    print "Labeling axes ...\n"
    for i in range(nCells):

        ax.text(-0.5,
                float(i + 1),
                cellLabels[i],
                rotation=0,
                horizontalalignment="right",
                fontname="Helvetica",
                fontsize=fontSize)

    for j in range(nPatterns):

        ax.text(float(j),
                -0.5,
                patternLabels[j],
                rotation=90,
                verticalalignment="bottom",
                fontname="Helvetica",
                fontsize=fontSize)

    ax.text(nPatterns + 0.5,
            1,
            "1 occurrence of the pattern",
            rotation=0,
            horizontalalignment="left",
            fontname="Helvetica",
            fontsize=fontSize,
            color=clrs.ORANGE)
    ax.text(nPatterns + 0.5,
            2,
            "2 occurrences of the pattern",
            rotation=0,
            horizontalalignment="left",
            fontname="Helvetica",
            fontsize=fontSize,
            color=clrs.TEAL)
    ax.text(nPatterns + 0.5,
            3,
            "3 occurrences of the pattern",
            rotation=0,
            horizontalalignment="left",
            fontname="Helvetica",
            fontsize=fontSize,
            color=clrs.BROWN)
    ax.text(nPatterns + 0.5,
            4,
            "4 occurrences of the pattern",
            rotation=0,
            horizontalalignment="left",
            fontname="Helvetica",
            fontsize=fontSize,
            color=clrs.GRAY)

    DGlabelPosition = nDGcells / 2
    CA3labelPosition = nDGcells + nCA3cells / 2
    CA2labelPosition = nDGcells + nCA3cells + nCA2cells / 2
    CA1labelPosition = nDGcells + nCA3cells + nCA2cells + nCA1cells / 2
    SublabelPosition = nDGcells + nCA3cells + nCA2cells + nCA1cells + nSubcells / 2
    EClabelPosition = nDGcells + nCA3cells + nCA2cells + nCA1cells + nSubcells + nECcells / 2

    ax.text(-12,
            DGlabelPosition,
            "DG",
            rotation=90,
            verticalalignment="center",
            fontname="Helvetica",
            fontsize=fontSize)
    ax.text(-12,
            CA3labelPosition,
            "CA3",
            rotation=90,
            verticalalignment="center",
            fontname="Helvetica",
            fontsize=fontSize)
    ax.text(-12,
            CA2labelPosition,
            "CA2",
            rotation=90,
            verticalalignment="center",
            fontname="Helvetica",
            fontsize=fontSize)
    ax.text(-12,
            CA1labelPosition,
            "CA1",
            rotation=90,
            verticalalignment="center",
            fontname="Helvetica",
            fontsize=fontSize)
    ax.text(-12,
            SublabelPosition,
            "Sub",
            rotation=90,
            verticalalignment="center",
            fontname="Helvetica",
            fontsize=fontSize)
    ax.text(-12,
            EClabelPosition,
            "EC",
            rotation=90,
            verticalalignment="center",
            fontname="Helvetica",
            fontsize=fontSize)

    outputFileName = "FP_plot_output/FP_matrix_%s.jpg" % time_stamp()
    print "Saving data to jpg file %s ...\n" % outputFileName
    fig.savefig(outputFileName, dpi=600)

    #    plt.show()

    return
Example #11
0
def plot_patterns(cellLabels, patternLabels, nPatternOccurrences):

    nCells = len(cellLabels)
    nPatterns = len(patternLabels)

    colors = [clrs.WHITE,
              clrs.ORANGE,
              clrs.TEAL,
              clrs.BROWN,
              clrs.GRAY]
              
    nColors = len(colors)
    
    print "Making color map ...\n"
    firingPatternColorMap = make_color_map(colors)
    
    print "Plotting data ...\n"
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.set_aspect("equal")
    
    plt.pcolormesh(nPatternOccurrences, cmap=firingPatternColorMap)
    
    plt.xlim(0, nPatterns)
    plt.ylim(0, nCells)
    
    plt.gca().invert_yaxis()
    
    print "Plotting hatch lines ...\n"
    plot_hatch_lines(nPatternOccurrences)
    
    nDGcells  = 18
    nCA3cells = 25
    nCA2cells =  5
    nCA1cells = 40
    nSubcells =  3
    nECcells  = 31
    
    line_width = 1.25
    
    DGline = nDGcells
    CA3line = DGline + nCA3cells
    CA2line = CA3line + nCA2cells
    CA1line = CA2line + nCA1cells
    Subline = CA1line + nSubcells
    
    plt.plot([0, nPatterns], [DGline, DGline], "b", linewidth=line_width)
    plt.plot([0, nPatterns], [CA3line, CA3line], "b", linewidth=line_width)
    plt.plot([0, nPatterns], [CA2line, CA2line], "b", linewidth=line_width)
    plt.plot([0, nPatterns], [CA1line, CA1line], "b", linewidth=line_width)
    plt.plot([0, nPatterns], [Subline, Subline], "b", linewidth=line_width)
    
    plt.tick_params(
        axis="x",          # changes apply to the x-axis
        which="both",      # both major and minor ticks are affected
        bottom="off",      # ticks along the bottom edge are off
        top="off",         # ticks along the top edge are off
        labelbottom="off", # labels along the bottom edge are off
        labeltop="off")    # labels along the top edge are off
        
    plt.tick_params(
        axis="y",          # changes apply to the x-axis
        which="both",      # both major and minor ticks are affected
        left="off",        # ticks along the left edge are off
        right="off",       # ticks along the right edge are off
        labelleft="off",   # labels along the left edge are off
        labelright="off")  # labels along the right edge are off
        
    if nCells > 50:
        fontSize = 2.5
    elif nCells > 25:
        fontSize = 5
    else:
        fontSize = 10
        
    print "Labeling axes ...\n"
    for i in range(nCells):
        
        ax.text(-0.5, float(i+1), cellLabels[i], rotation=0, horizontalalignment="right", fontname="Helvetica", fontsize=fontSize)

    for j in range(nPatterns):
        
        ax.text(float(j), -0.5, patternLabels[j], rotation=90, verticalalignment="bottom", fontname="Helvetica", fontsize=fontSize)
    
    ax.text(nPatterns+0.5, 1, "1 occurrence of the pattern", rotation=0, horizontalalignment="left", fontname="Helvetica", fontsize=fontSize,
            color=clrs.ORANGE)
    ax.text(nPatterns+0.5, 2, "2 occurrences of the pattern", rotation=0, horizontalalignment="left", fontname="Helvetica", fontsize=fontSize,
            color=clrs.TEAL)
    ax.text(nPatterns+0.5, 3, "3 occurrences of the pattern", rotation=0, horizontalalignment="left", fontname="Helvetica", fontsize=fontSize,
            color=clrs.BROWN)
    ax.text(nPatterns+0.5, 4, "4 occurrences of the pattern", rotation=0, horizontalalignment="left", fontname="Helvetica", fontsize=fontSize,
            color=clrs.GRAY)
    
    DGlabelPosition = nDGcells/2
    CA3labelPosition = nDGcells + nCA3cells/2
    CA2labelPosition = nDGcells + nCA3cells + nCA2cells/2
    CA1labelPosition = nDGcells + nCA3cells + nCA2cells + nCA1cells/2
    SublabelPosition = nDGcells + nCA3cells + nCA2cells + nCA1cells + nSubcells/2
    EClabelPosition = nDGcells + nCA3cells + nCA2cells + nCA1cells + nSubcells + nECcells/2
        
    ax.text(-12, DGlabelPosition, "DG", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, CA3labelPosition, "CA3", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, CA2labelPosition, "CA2", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, CA1labelPosition, "CA1", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, SublabelPosition, "Sub", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
    ax.text(-12, EClabelPosition, "EC", rotation=90, verticalalignment="center", fontname="Helvetica", fontsize=fontSize)
        
    outputFileName = "FP_plot_output/FP_matrix_%s.jpg" % time_stamp()
    print "Saving data to jpg file %s ...\n" % outputFileName    
    fig.savefig(outputFileName, dpi=600)
    
#    plt.show()

    return