Skip to content

epochTable

Classes¤

epochTable ¤

Load epoch/stimulation from abf file using 'sweepEpochs'.

Values in epoch table are per sweep!

sweepNumber index type startPoint stopPoint startSec stopSec durSec level pulseWidth pulsePeriod digitalState 0 13 0 Step 0 25 0.0000 0.0025 0.0025 -0.1 0 0 0 1 13 1 Step 25 275 0.0025 0.0275 0.0250 -0.1 0 0 0 2 13 2 Step 275 1275 0.0275 0.1275 0.1000 0.7 0 0 0 3 13 3 Step 1275 1525 0.1275 0.1525 0.0250 -0.1 0 0 0 4 13 4 Step 1525 1600 0.1525 0.1600 0.0075 -0.1 0 0 0

Source code in sanpy/fileloaders/epochTable.py
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
class epochTable():
    """Load epoch/stimulation from abf file using 'sweepEpochs'.

    Values in epoch table are per sweep!

    sweepNumber  index  type  startPoint  stopPoint  startSec  stopSec  durSec  level  pulseWidth  pulsePeriod  digitalState
    0           13      0  Step           0         25    0.0000   0.0025  0.0025   -0.1           0            0             0
    1           13      1  Step          25        275    0.0025   0.0275  0.0250   -0.1           0            0             0
    2           13      2  Step         275       1275    0.0275   0.1275  0.1000    0.7           0            0             0
    3           13      3  Step        1275       1525    0.1275   0.1525  0.0250   -0.1           0            0             0
    4           13      4  Step        1525       1600    0.1525   0.1600  0.0075   -0.1           0            0             0

    """

    def __init__(self, abf: pyabf.ABF = None):
        self._epochList = []

        if abf is not None:
            self._builFromAbf(abf)

    def __str__(self):
        return self.getEpochList(asDataFrame=True).to_string()

    def addEpoch(self, sweepNumber, startSec, stopSec, dataPointsPerMs : int, level, type : str = 'Step'):
        newEpochIdx = self.numEpochs()

        p1 = (startSec * 1000) * dataPointsPerMs
        p1 = int(p1)

        p2 = (stopSec * 1000) * dataPointsPerMs
        p2 = int(p2)

        epochDict = {
            "sweepNumber": sweepNumber,
            "index": newEpochIdx,
            "type": type,
            "startPoint": p1,  # point the epoch starts
            "stopPoint": p2,  # point the epoch ends
            "startSec": startSec,
            "stopSec": stopSec,
            "durSec": stopSec - startSec,
            "level": level,
            # "pulseWidth": pulseWidth,
            # "pulsePeriod": pulsePeriod,
            # "digitalState": digitalState,  # list of 0/1 for 8x digital states
        }
        self._epochList.append(epochDict)

    def getSweepEpoch(self, sweep):
        return self._epochList[sweep]

    def _builFromAbf(self, abf: pyabf.ABF):
        dataPointsPerMs = abf.dataPointsPerMs
        """To convert point to seconds"""

        try:
            _tmp = abf.sweepEpochs.p1s
        except AttributeError as e:
            logger.error(e)
            return

        # sweepEpochs is type "pyabf.waveform.EpochSweepWaveform"
        for epochIdx, p1 in enumerate(abf.sweepEpochs.p1s):
            p2 = abf.sweepEpochs.p2s[epochIdx]  # stop point of each pulse
            epochLevel = abf.sweepEpochs.levels[epochIdx]
            epochType = abf.sweepEpochs.types[epochIdx]
            pulseWidth = abf.sweepEpochs.pulseWidths[epochIdx]
            pulsePeriod = abf.sweepEpochs.pulsePeriods[epochIdx]
            digitalState = abf.sweepEpochs.pulsePeriods[epochIdx]
            ##print(f"epoch index {epochIdx}: at point {p1} there is a {epochType} to level {epochLevel}")

            p1_sec = p1 / abf.dataPointsPerMs / 1000
            p2_sec = p2 / abf.dataPointsPerMs / 1000

            epochDict = {
                "sweepNumber": abf.sweepNumber,
                "index": epochIdx,
                "type": epochType,
                "startPoint": p1,  # point the epoch starts
                "stopPoint": p2,  # point the epoch ends
                "startSec": p1_sec,
                "stopSec": p2_sec,
                "durSec": p2_sec - p1_sec,
                "level": epochLevel,
                "pulseWidth": pulseWidth,
                "pulsePeriod": pulsePeriod,
                "digitalState": digitalState,  # list of 0/1 for 8x digital states
            }
            self._epochList.append(epochDict)

    def getEpochList(self, asDataFrame: bool = False):
        if asDataFrame:
            return pd.DataFrame(self._epochList)
        else:
            return self._epochList

    def findEpoch(self, pnt: int) -> Optional[int]:
        """Return epoch index for a point in recording.

        Stop points are always the same as next epoch start point.
        Be sure to use '<' like pnt<stopPnt to get epoch index correct.

        If not found, return None

        Parameters
        ----------
        pnt : int
            Point index into recording (within a sweep)
        """
        for epochIdx, epoch in enumerate(self._epochList):
            startPoint = epoch["startPoint"]
            stopPoint = epoch["stopPoint"]
            if pnt >= startPoint and pnt < stopPoint:
                return epochIdx
        #
        # return None

    def getLevel(self, epoch):
        """Given an epoch number return the 'level'"""
        return self._epochList[epoch]["level"]

    def getStartSec(self, epoch):
        """Given an epoch number return the 'startSec'"""
        return self._epochList[epoch]["startSec"]

    def getStartSecs(self):
        """Return all epoch start times."""
        startSecs = [epoch["startSec"] for epoch in self._epochList]
        return startSecs

    def getEpochLines(self, yMin=0, yMax=1):
        x = [float("nan")] * (self.numEpochs() * 3)
        y = [float("nan")] * (self.numEpochs() * 3)

        for epoch in range(self.numEpochs()):
            idx = epoch * 3
            x[idx] = self._epochList[epoch]["startSec"]
            x[idx + 1] = self._epochList[epoch]["startSec"]
            x[idx + 2] = float("nan")

            y[idx] = yMin
            y[idx + 1] = yMax
            y[idx + 2] = float("nan")

        return x, y

    def numEpochs(self):
        # print('qqq:', self._epochList)
        return len(self._epochList)

Functions¤

findEpoch(pnt) ¤

Return epoch index for a point in recording.

Stop points are always the same as next epoch start point. Be sure to use '<' like pnt<stopPnt to get epoch index correct.

If not found, return None

Parameters:

Name Type Description Default
pnt int

Point index into recording (within a sweep)

required
Source code in sanpy/fileloaders/epochTable.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def findEpoch(self, pnt: int) -> Optional[int]:
    """Return epoch index for a point in recording.

    Stop points are always the same as next epoch start point.
    Be sure to use '<' like pnt<stopPnt to get epoch index correct.

    If not found, return None

    Parameters
    ----------
    pnt : int
        Point index into recording (within a sweep)
    """
    for epochIdx, epoch in enumerate(self._epochList):
        startPoint = epoch["startPoint"]
        stopPoint = epoch["stopPoint"]
        if pnt >= startPoint and pnt < stopPoint:
            return epochIdx
getLevel(epoch) ¤

Given an epoch number return the 'level'

Source code in sanpy/fileloaders/epochTable.py
128
129
130
def getLevel(self, epoch):
    """Given an epoch number return the 'level'"""
    return self._epochList[epoch]["level"]
getStartSec(epoch) ¤

Given an epoch number return the 'startSec'

Source code in sanpy/fileloaders/epochTable.py
132
133
134
def getStartSec(self, epoch):
    """Given an epoch number return the 'startSec'"""
    return self._epochList[epoch]["startSec"]
getStartSecs() ¤

Return all epoch start times.

Source code in sanpy/fileloaders/epochTable.py
136
137
138
139
def getStartSecs(self):
    """Return all epoch start times."""
    startSecs = [epoch["startSec"] for epoch in self._epochList]
    return startSecs

Functions¤

get_logger(name, level=logging.DEBUG) ¤

Source code in sanpy/sanpyLogger.py
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def get_logger(name, level=logging.DEBUG):
    """ """

    # if getattr(sys, 'frozen', False):
    #     # running in a bundle (frozen)
    #     myPath = sys._MEIPASS
    # else:
    #     # running in a normal Python environment
    #     myPath = pathlib.Path(__file__).parent.absolute()

    # fileName = 'sanpy.log'
    # logPath = os.path.join(myPath, fileName)

    logPath = getLoggerFile()

    # print('logPath:', logPath)

    # Create a custom logger
    logger = logging.getLogger(name)
    logger.setLevel(level)  # abb 20220609

    # abb removed 20220609
    logger.propagate = False  # don't propogate to root (o.w. prints twice)
    # print('   ', logger, 'level:', level)
    if not logger.handlers:
        # print('=== sanpyLogger.get_logger() creating handlers')
        # print('    ', logger.handlers)

        # Create handlers
        c_handler = logging.StreamHandler()
        f_handler = RotatingFileHandler(logPath, maxBytes=500, backupCount=0)
        # f_handler = logging.FileHandler(logPath)

        c_handler.setLevel(level)
        f_handler.setLevel(level)

        # Create formatters and add it to handlers
        consoleFormat = "%(levelname)5s %(name)8s  %(filename)s %(funcName)s() line:%(lineno)d -- %(message)s"
        c_format = logging.Formatter(consoleFormat)

        fileFormat = "%(asctime)s  %(levelname)5s %(name)8s  %(filename)s %(funcName)s() line:%(lineno)d -- %(message)s"
        f_format = logging.Formatter(fileFormat)

        c_handler.setFormatter(c_format)
        f_handler.setFormatter(f_format)

        # Add handlers to the logger
        logger.addHandler(c_handler)
        logger.addHandler(f_handler)
    #
    #
    return logger
All material is Copyright 2011-2023 Robert H. Cudmore