praatio.data_points

Code for reading, writing, and representing less complicated praat data files

see examples/get_vowel_points.py

  1"""Code for reading, writing, and representing less complicated praat data files
  2
  3see **examples/get_vowel_points.py**
  4"""
  5
  6import io
  7from typing import Tuple
  8
  9from praatio.data_classes.data_point import PointObject1D, PointObject2D
 10
 11
 12def open1DPointObject(fn: str) -> PointObject1D:
 13    with io.open(fn, "r", encoding="utf-8") as fd:
 14        data = fd.read()
 15    if "xmin" in data[:100]:  # Kindof lazy
 16        data, objectType, minT, maxT = _parseNormalHeader(fn)
 17
 18        start = 0
 19        dataList = []
 20        while True:
 21            try:
 22                start = data.index("=", start)
 23            except ValueError:
 24                break
 25
 26            pointVal, start = _getNextValue(data, start)
 27            dataList.append((float(pointVal),))
 28
 29        po = PointObject1D(dataList, objectType, minT, maxT)
 30
 31    else:
 32        data, objectType, minT, maxT = _parseShortHeader(fn)
 33        dataList = [(float(val),) for val in data.split("\n") if val.strip() != ""]
 34        po = PointObject1D(dataList, objectType, minT, maxT)
 35
 36    return po
 37
 38
 39def open2DPointObject(fn: str) -> PointObject2D:
 40    with io.open(fn, "r", encoding="utf-8") as fd:
 41        data = fd.read()
 42    if "xmin" in data[:100]:  # Kindof lazy
 43        data, objectType, minT, maxT = _parseNormalHeader(fn)
 44
 45        start = 0
 46        dataList = []
 47        while True:
 48            try:
 49                start = data.index("=", start)
 50            except ValueError:
 51                break
 52
 53            timeVal, start = _getNextValue(data, start)
 54
 55            try:
 56                start = data.index("=", start)
 57            except ValueError:
 58                break
 59
 60            pointVal, start = _getNextValue(data, start)
 61            dataList.append(
 62                (
 63                    float(timeVal),
 64                    float(pointVal),
 65                )
 66            )
 67
 68        po = PointObject2D(dataList, objectType, minT, maxT)
 69
 70    else:
 71        data, objectType, minT, maxT = _parseShortHeader(fn)
 72        dataStrList = data.split("\n")
 73        dataList = [
 74            (float(dataStrList[i]), float(dataStrList[i + 1]))
 75            for i in range(0, len(dataStrList), 2)
 76            if dataStrList[i].strip() != ""
 77        ]
 78        po = PointObject2D(dataList, objectType, minT, maxT)
 79
 80    return po
 81
 82
 83def _parseNormalHeader(fn: str) -> Tuple[str, str, float, float]:
 84    with io.open(fn, "r", encoding="utf-8") as fd:
 85        data = fd.read()
 86
 87    chunkedData = data.split("\n", 7)
 88
 89    objectType = chunkedData[1].split("=")[-1]
 90    objectType = objectType.replace('"', "").strip()
 91
 92    data = chunkedData[-1]
 93    maxT = float(chunkedData[-4].split("=")[-1].strip())
 94    minT = float(chunkedData[-5].split("=")[-1].strip())
 95
 96    return data, objectType, minT, maxT
 97
 98
 99def _getNextValue(data: str, start: int) -> Tuple[str, int]:
100    end = data.index("\n", start)
101    value = data[start + 1 : end]
102    return value, end
103
104
105def _parseShortHeader(fn: str) -> Tuple[str, str, float, float]:
106    with io.open(fn, "r", encoding="utf-8") as fd:
107        data = fd.read()
108
109    chunkedData = data.split("\n", 6)
110
111    objectType = chunkedData[1].split("=")[-1]
112    objectType = objectType.replace('"', "").strip()
113
114    data = chunkedData[-1]
115    maxT = float(chunkedData[-3])
116    minT = float(chunkedData[-4])
117
118    return data, objectType, minT, maxT
def open1DPointObject(fn: str) -> praatio.data_classes.data_point.PointObject1D:
13def open1DPointObject(fn: str) -> PointObject1D:
14    with io.open(fn, "r", encoding="utf-8") as fd:
15        data = fd.read()
16    if "xmin" in data[:100]:  # Kindof lazy
17        data, objectType, minT, maxT = _parseNormalHeader(fn)
18
19        start = 0
20        dataList = []
21        while True:
22            try:
23                start = data.index("=", start)
24            except ValueError:
25                break
26
27            pointVal, start = _getNextValue(data, start)
28            dataList.append((float(pointVal),))
29
30        po = PointObject1D(dataList, objectType, minT, maxT)
31
32    else:
33        data, objectType, minT, maxT = _parseShortHeader(fn)
34        dataList = [(float(val),) for val in data.split("\n") if val.strip() != ""]
35        po = PointObject1D(dataList, objectType, minT, maxT)
36
37    return po
def open2DPointObject(fn: str) -> praatio.data_classes.data_point.PointObject2D:
40def open2DPointObject(fn: str) -> PointObject2D:
41    with io.open(fn, "r", encoding="utf-8") as fd:
42        data = fd.read()
43    if "xmin" in data[:100]:  # Kindof lazy
44        data, objectType, minT, maxT = _parseNormalHeader(fn)
45
46        start = 0
47        dataList = []
48        while True:
49            try:
50                start = data.index("=", start)
51            except ValueError:
52                break
53
54            timeVal, start = _getNextValue(data, start)
55
56            try:
57                start = data.index("=", start)
58            except ValueError:
59                break
60
61            pointVal, start = _getNextValue(data, start)
62            dataList.append(
63                (
64                    float(timeVal),
65                    float(pointVal),
66                )
67            )
68
69        po = PointObject2D(dataList, objectType, minT, maxT)
70
71    else:
72        data, objectType, minT, maxT = _parseShortHeader(fn)
73        dataStrList = data.split("\n")
74        dataList = [
75            (float(dataStrList[i]), float(dataStrList[i + 1]))
76            for i in range(0, len(dataStrList), 2)
77            if dataStrList[i].strip() != ""
78        ]
79        po = PointObject2D(dataList, objectType, minT, maxT)
80
81    return po