praatio.data_classes.data_point API documentation

# praatio.data_classes.data_point

An PointObject is used for storing non-annotation data in praat

There are two variants: PointObject1D and PointObject2D

PointObject1D only stores temporal data (it can track pulses/occurances in time). PointObject2D stores temporal data and some other data (eg pitch). It's not so different from a PointTier, except that PointTiers specifically hold annotation data.

View Source

      1"""An PointObject is used for storing non-annotation data in praat
      2
      3There are two variants: PointObject1D and PointObject2D
      4
      5PointObject1D only stores temporal data (it can track pulses/occurances in time).
      6PointObject2D stores temporal data and some other data (eg pitch).  It's not
      7so different from a PointTier, except that PointTiers specifically hold annotation
      8data.
      9"""
     10
     11import io
     12
     13from typing_extensions import Literal
     14from typing import List, Optional, Tuple, cast
     15
     16from praatio.utilities import constants
     17from praatio.utilities import errors
     18from praatio.utilities import my_math
     19
     20
     21class PointObject:
     22    def __init__(
     23        self,
     24        pointList: List[Tuple[float, ...]],  # Either (float) or (float, float)
     25        objectClass: str,
     26        minTime: float = 0,
     27        maxTime: float = None,
     28    ):
     29        self.pointList = [tuple(float(val) for val in row) for row in pointList]
     30        self.objectClass = objectClass
     31        self.minTime = minTime if minTime > 0 else 0
     32        self.maxTime = maxTime
     33
     34    def __eq__(self, other):
     35        if not isinstance(other, PointObject):
     36            return False
     37
     38        isEqual = True
     39        isEqual &= self.objectClass == other.objectClass
     40        isEqual &= self.minTime == other.minTime
     41        isEqual &= self.maxTime == other.maxTime
     42        isEqual &= len(self.pointList) == len(other.pointList)
     43
     44        if isEqual:
     45            for selfEntry, otherEntry in zip(self.pointList, other.pointList):
     46                isEqual &= selfEntry == otherEntry
     47
     48        return isEqual
     49
     50    def save(self, fn: str) -> None:
     51        header = 'File type = "ooTextFile"\n' 'Object class = "%s"\n' "\n%s\n%s\n%d"
     52        header %= (
     53            self.objectClass,
     54            my_math.numToStr(self.minTime),
     55            my_math.numToStr(self.maxTime),
     56            len(self.pointList),
     57        )
     58
     59        tmp = [my_math.numToStr(val) for entry in self.pointList for val in entry]
     60        strPoints = "\n".join(tmp)
     61
     62        outputStr = "%s\n%s\n" % (header, strPoints)
     63
     64        with io.open(fn, "w", encoding="utf-8") as fd:
     65            fd.write(outputStr)
     66
     67    def getPointsInInterval(
     68        self, start: float, end: float, startIndex: int = 0
     69    ) -> List[float]:
     70
     71        returnPointList = []
     72        for entry in self.pointList[startIndex:]:
     73            time = entry[0]
     74            if time >= start:
     75                if time <= end:
     76                    returnPointList.append(time)
     77                else:
     78                    break
     79
     80        return returnPointList
     81
     82
     83class PointObject1D(PointObject):
     84    """Points that only carry temporal information"""
     85
     86    def __init__(
     87        self,
     88        pointList: List[Tuple[float]],
     89        objectClass: Literal["point"],
     90        minTime: float = 0,
     91        maxTime: Optional[float] = None,
     92    ):
     93
     94        suitable1dPointTypes = [constants.DataPointTypes.POINT]
     95        if objectClass not in suitable1dPointTypes:
     96            raise errors.WrongOption("objectClass", objectClass, suitable1dPointTypes)
     97
     98        if maxTime is None:
     99            maxTime = max([row[0] for row in pointList])
    100
    101        castPointList = cast(List[Tuple[float, ...]], pointList)
    102        super(PointObject1D, self).__init__(
    103            castPointList, objectClass, minTime, maxTime
    104        )
    105
    106
    107class PointObject2D(PointObject):
    108    """Points that carry a temporal value and some other value"""
    109
    110    def __init__(
    111        self,
    112        pointList: List[Tuple[float, float]],
    113        objectClass: Literal["pitch", "duration"],
    114        minTime: float = 0,
    115        maxTime: float = None,
    116    ):
    117        suitable2dPointTypes = [
    118            constants.DataPointTypes.PITCH,
    119            constants.DataPointTypes.DURATION,
    120        ]
    121        if objectClass not in suitable2dPointTypes:
    122            raise errors.WrongOption(
    123                "objectClass",
    124                objectClass,
    125                suitable2dPointTypes,
    126            )
    127
    128        if maxTime is None:
    129            maxTime = max([timeV for timeV, _ in pointList])
    130
    131        castPointList = cast(List[Tuple[float, ...]], pointList)
    132        super(PointObject2D, self).__init__(
    133            castPointList, objectClass, minTime, maxTime
    134        )

class PointObject: View Source

    22class PointObject:
    23    def __init__(
    24        self,
    25        pointList: List[Tuple[float, ...]],  # Either (float) or (float, float)
    26        objectClass: str,
    27        minTime: float = 0,
    28        maxTime: float = None,
    29    ):
    30        self.pointList = [tuple(float(val) for val in row) for row in pointList]
    31        self.objectClass = objectClass
    32        self.minTime = minTime if minTime > 0 else 0
    33        self.maxTime = maxTime
    34
    35    def __eq__(self, other):
    36        if not isinstance(other, PointObject):
    37            return False
    38
    39        isEqual = True
    40        isEqual &= self.objectClass == other.objectClass
    41        isEqual &= self.minTime == other.minTime
    42        isEqual &= self.maxTime == other.maxTime
    43        isEqual &= len(self.pointList) == len(other.pointList)
    44
    45        if isEqual:
    46            for selfEntry, otherEntry in zip(self.pointList, other.pointList):
    47                isEqual &= selfEntry == otherEntry
    48
    49        return isEqual
    50
    51    def save(self, fn: str) -> None:
    52        header = 'File type = "ooTextFile"\n' 'Object class = "%s"\n' "\n%s\n%s\n%d"
    53        header %= (
    54            self.objectClass,
    55            my_math.numToStr(self.minTime),
    56            my_math.numToStr(self.maxTime),
    57            len(self.pointList),
    58        )
    59
    60        tmp = [my_math.numToStr(val) for entry in self.pointList for val in entry]
    61        strPoints = "\n".join(tmp)
    62
    63        outputStr = "%s\n%s\n" % (header, strPoints)
    64
    65        with io.open(fn, "w", encoding="utf-8") as fd:
    66            fd.write(outputStr)
    67
    68    def getPointsInInterval(
    69        self, start: float, end: float, startIndex: int = 0
    70    ) -> List[float]:
    71
    72        returnPointList = []
    73        for entry in self.pointList[startIndex:]:
    74            time = entry[0]
    75            if time >= start:
    76                if time <= end:
    77                    returnPointList.append(time)
    78                else:
    79                    break
    80
    81        return returnPointList

PointObject( pointList: List[Tuple[float, ...]], objectClass: str, minTime: float = 0, maxTime: float = None) View Source

    23    def __init__(
    24        self,
    25        pointList: List[Tuple[float, ...]],  # Either (float) or (float, float)
    26        objectClass: str,
    27        minTime: float = 0,
    28        maxTime: float = None,
    29    ):
    30        self.pointList = [tuple(float(val) for val in row) for row in pointList]
    31        self.objectClass = objectClass
    32        self.minTime = minTime if minTime > 0 else 0
    33        self.maxTime = maxTime

pointList
objectClass
minTime
maxTime
def save(self, fn: str) -> None: View Source

    51    def save(self, fn: str) -> None:
    52        header = 'File type = "ooTextFile"\n' 'Object class = "%s"\n' "\n%s\n%s\n%d"
    53        header %= (
    54            self.objectClass,
    55            my_math.numToStr(self.minTime),
    56            my_math.numToStr(self.maxTime),
    57            len(self.pointList),
    58        )
    59
    60        tmp = [my_math.numToStr(val) for entry in self.pointList for val in entry]
    61        strPoints = "\n".join(tmp)
    62
    63        outputStr = "%s\n%s\n" % (header, strPoints)
    64
    65        with io.open(fn, "w", encoding="utf-8") as fd:
    66            fd.write(outputStr)

def getPointsInInterval(self, start: float, end: float, startIndex: int = 0) -> List[float]: View Source

    68    def getPointsInInterval(
    69        self, start: float, end: float, startIndex: int = 0
    70    ) -> List[float]:
    71
    72        returnPointList = []
    73        for entry in self.pointList[startIndex:]:
    74            time = entry[0]
    75            if time >= start:
    76                if time <= end:
    77                    returnPointList.append(time)
    78                else:
    79                    break
    80
    81        return returnPointList

class PointObject1D(PointObject): View Source

     84class PointObject1D(PointObject):
     85    """Points that only carry temporal information"""
     86
     87    def __init__(
     88        self,
     89        pointList: List[Tuple[float]],
     90        objectClass: Literal["point"],
     91        minTime: float = 0,
     92        maxTime: Optional[float] = None,
     93    ):
     94
     95        suitable1dPointTypes = [constants.DataPointTypes.POINT]
     96        if objectClass not in suitable1dPointTypes:
     97            raise errors.WrongOption("objectClass", objectClass, suitable1dPointTypes)
     98
     99        if maxTime is None:
    100            maxTime = max([row[0] for row in pointList])
    101
    102        castPointList = cast(List[Tuple[float, ...]], pointList)
    103        super(PointObject1D, self).__init__(
    104            castPointList, objectClass, minTime, maxTime
    105        )

Points that only carry temporal information

PointObject1D( pointList: List[Tuple[float]], objectClass: Literal['point'], minTime: float = 0, maxTime: Optional[float] = None) View Source

     87    def __init__(
     88        self,
     89        pointList: List[Tuple[float]],
     90        objectClass: Literal["point"],
     91        minTime: float = 0,
     92        maxTime: Optional[float] = None,
     93    ):
     94
     95        suitable1dPointTypes = [constants.DataPointTypes.POINT]
     96        if objectClass not in suitable1dPointTypes:
     97            raise errors.WrongOption("objectClass", objectClass, suitable1dPointTypes)
     98
     99        if maxTime is None:
    100            maxTime = max([row[0] for row in pointList])
    101
    102        castPointList = cast(List[Tuple[float, ...]], pointList)
    103        super(PointObject1D, self).__init__(
    104            castPointList, objectClass, minTime, maxTime
    105        )

##### Inherited Members

PointObject
  pointList
  objectClass
  minTime
  maxTime
  save
  getPointsInInterval

class PointObject2D(PointObject): View Source

    108class PointObject2D(PointObject):
    109    """Points that carry a temporal value and some other value"""
    110
    111    def __init__(
    112        self,
    113        pointList: List[Tuple[float, float]],
    114        objectClass: Literal["pitch", "duration"],
    115        minTime: float = 0,
    116        maxTime: float = None,
    117    ):
    118        suitable2dPointTypes = [
    119            constants.DataPointTypes.PITCH,
    120            constants.DataPointTypes.DURATION,
    121        ]
    122        if objectClass not in suitable2dPointTypes:
    123            raise errors.WrongOption(
    124                "objectClass",
    125                objectClass,
    126                suitable2dPointTypes,
    127            )
    128
    129        if maxTime is None:
    130            maxTime = max([timeV for timeV, _ in pointList])
    131
    132        castPointList = cast(List[Tuple[float, ...]], pointList)
    133        super(PointObject2D, self).__init__(
    134            castPointList, objectClass, minTime, maxTime
    135        )

Points that carry a temporal value and some other value

PointObject2D( pointList: List[Tuple[float, float]], objectClass: Literal['pitch', 'duration'], minTime: float = 0, maxTime: float = None) View Source

    111    def __init__(
    112        self,
    113        pointList: List[Tuple[float, float]],
    114        objectClass: Literal["pitch", "duration"],
    115        minTime: float = 0,
    116        maxTime: float = None,
    117    ):
    118        suitable2dPointTypes = [
    119            constants.DataPointTypes.PITCH,
    120            constants.DataPointTypes.DURATION,
    121        ]
    122        if objectClass not in suitable2dPointTypes:
    123            raise errors.WrongOption(
    124                "objectClass",
    125                objectClass,
    126                suitable2dPointTypes,
    127            )
    128
    129        if maxTime is None:
    130            maxTime = max([timeV for timeV, _ in pointList])
    131
    132        castPointList = cast(List[Tuple[float, ...]], pointList)
    133        super(PointObject2D, self).__init__(
    134            castPointList, objectClass, minTime, maxTime
    135        )

##### Inherited Members

PointObject
  pointList
  objectClass
  minTime
  maxTime
  save
  getPointsInInterval