Ejemplo n.º 1
0
    def analyseNetwork(self, simName, simType=None, nPlotMax=10):

        figDir = simName + "/figures/"
        if (not os.path.exists(figDir)):
            os.makedirs(figDir)

        if (simType is None):
            simType = self.simType

        if (simType == "Straub2016LTS"):
            preType = "LTS"
            self.setupExpDataDict()
        elif (simType == "Straub2016FS"):
            preType = "FSN"
            self.setupExpDataDict()
        elif (simType == "Chuhma2011"):
            preType = "SPN"
            self.setupExpDataDict()
        elif (simType == "Szydlowski2013"):
            preType = "FSN"
        else:
            print("Unknown simType : " + simType)
            exit(-1)

        print("Analysing data in " + simName)
        voltFile = simName + "/" + simType + "-network-stimulation-current.txt"

        # Read data from file
        data = np.genfromtxt(voltFile, delimiter=",")

        assert (data[0, 0] == -1)  # First column should be time
        time = data[0, 1:] * 1e-3

        current = dict()

        for rows in data[1:, :]:
            cID = int(rows[0])
            current[cID] = rows[1:] * 1e-9

        # Data in time, current now

        # Read the network info
        networkFile = simName + "/network-pruned-synapses.hdf5"
        self.snuddaLoad = SnuddaLoad(networkFile)
        self.data = self.snuddaLoad.data

        recordedNeurons = [x for x in current]

        # Group the neurons by type

        if (simType == "Chuhma2011"):
            neuronTypeList = ["dSPN", "iSPN", "FSN", "ChIN"]
        elif (simType == "Straub2016FS" or simType == "Straub2016LTS"):
            neuronTypeList = ["dSPN", "iSPN", "ChIN"]
        elif (simType == "Szydlowski2013"):
            neuronTypeList = ["LTS"]
        else:
            print("simulate: Unknown simType: " + simType)
            exit(-1)

        neuronPlotList = []

        minTimeIdx = np.where(time > self.tInj)[0][0]
        maxTimeIdx = np.where(time > self.tInj + self.tWindow)[0][0]

        for nt in neuronTypeList:
            IDList = [
                x for x in current if self.data["neurons"][x]["type"] == nt
            ]
            maxIdx = [np.argmax(np.abs(current[x][minTimeIdx:maxTimeIdx] \
                                       -current[x][minTimeIdx])) + minTimeIdx \
                      for x in IDList]

            neuronPlotList.append((IDList, maxIdx))

        matplotlib.rcParams.update({'font.size': 22})

        for plotID, maxIdx in neuronPlotList:

            if (len(plotID) == 0):
                continue

            plotType = self.data["neurons"][plotID[0]]["type"]
            figName = figDir + "/" + simType + "-" + plotType + "-current-traces.pdf"
            figNameHist = figDir + "/" + simType + "-" + plotType + "-current-histogram.pdf"

            goodMax = []

            plt.figure()

            peakAmp = []
            peakTime = []
            voltCurve = []

            for pID, mIdx in zip(plotID, maxIdx):
                tIdx = np.where(
                    np.logical_and(time > self.tInj,
                                   time < self.tInj + self.tWindow))[0]

                curAmp = current[pID][tIdx] - current[pID][tIdx[0] - 1]
                maxAmp = current[pID][mIdx] - current[pID][tIdx[0] - 1]

                if (mIdx < minTimeIdx or mIdx > maxTimeIdx
                        or abs(maxAmp) < 1e-12):
                    # No peaks
                    continue

                goodMax.append(maxAmp * 1e9)
                peakAmp.append(maxAmp * 1e9)
                peakTime.append((time[mIdx] - time[tIdx[0]]) * 1e3)
                voltCurve.append(
                    ((time[tIdx] - time[tIdx[0]]) * 1e3, curAmp * 1e9))

            # Pick which curves to plot
            sortIdx = np.argsort(peakAmp)
            if (len(sortIdx) < nPlotMax):
                keepIdx = sortIdx
            else:
                keepIdx = [sortIdx[int(np.round(x))] for x in \
                           np.linspace(0,len(sortIdx)-1,nPlotMax)]

            for x in keepIdx:
                plt.plot(voltCurve[x][0], voltCurve[x][1], 'k-')

            plt.scatter(peakTime, peakAmp, marker=".", c="blue", s=100)

            nType = self.data["neurons"][plotID[0]]["type"]
            if ((simType, nType) in self.expDataDict):
                expData = self.expDataDict[(simType, nType)]
                t = self.tWindow * 1e3 * (
                    1 + 0.03 * np.random.rand(expData.shape[0]))
                plt.scatter(t, -expData, marker=".", c="red", s=100)

            if (self.plotExpTrace and (simType, nType) in self.expTraceDict):
                data = self.expTraceDict[(simType, nType)]
                tExp = data[:, 0]
                vExp = data[:, 1:]
                tIdx = np.where(tExp < self.tWindow * 1e3)[0]
                plt.plot(tExp[tIdx], vExp[tIdx, :], c="red")

            plt.title(
                self.neuronName(preType) + " to " + self.neuronName(plotType))
            plt.xlabel("Time (ms)")
            plt.ylabel("Current (nA)")

            # Remove part of the frame
            plt.gca().spines["right"].set_visible(False)
            plt.gca().spines["top"].set_visible(False)

            plt.tight_layout()
            plt.ion()
            plt.show()
            plt.savefig(figName, dpi=300)

            # Also plot histogram
            plt.figure()
            plt.hist(goodMax)
            plt.xlabel("Current (nA)")
            plt.title(
                self.neuronName(preType) + " to " + self.neuronName(plotType))

            # Remove part of the frame
            plt.gca().spines["right"].set_visible(False)
            plt.gca().spines["top"].set_visible(False)

            plt.tight_layout()
            plt.ion()
            plt.show()
            plt.savefig(figNameHist, dpi=300)

        import pdb
        pdb.set_trace()