forked from mexca/mexca-sd-experiment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rttm.py
193 lines (145 loc) · 5.59 KB
/
rttm.py
1
2
3
4
5
6
7
8
9
10
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""Storing and writing data in RTTM format"""
from click import FileError
from copy import copy, deepcopy
from dataclasses import dataclass, field
import warnings
import sys
import numpy as np
@dataclass
class RttmObj:
type: str
file: str
chnl: int
tbeg: float
tdur: float
ortho: str = None
stype: str = None
name: str = None
conf: float = None
def check_attrs(self) -> None:
if isinstance(self.tbeg, float) and self.tbeg < 0.0:
raise ValueError("Attribute 'tbeg' must be greater than zero")
if isinstance(self.tdur, float) and self.tdur < 0.0:
raise ValueError("Attribute 'tdur' must be greater than zero")
if isinstance(self.conf, float) and (self.conf < 0.0 or self.conf > 1.0):
raise ValueError(
"Attribute 'conf' must be greater than zero and smaller than 1.0")
def __post_init__(self):
self.check_attrs()
def __copy__(self):
return type(self)(
self.type,
self.file,
self.chnl,
self.tbeg,
self.tdur,
self.ortho,
self.stype,
self.name,
self.conf
)
def get_default_header():
return ["type", "file", "chnl", "tbeg", "tdur", "ortho", "stype", "name", "conf"]
def check_rttm_filename(filename):
filename_split = filename.split(".")
if filename_split[-1] != "rttm":
raise FileError(filename, "Cannot open files without '.rttm' extension")
@dataclass
class RttmSeq:
sequence: list[RttmObj]
header: list[str] = field(default_factory=get_default_header)
def __copy__(self):
return type(self)(self.sequence, self.header)
def __deepcopy__(self, memo):
id_self = id(self)
_copy = memo.get(id_self)
if _copy is None:
_copy = type(self)(
deepcopy(self.sequence, memo),
deepcopy(self.header, memo))
memo[id_self] = _copy
return _copy
def __str__(self, end="\t", file=sys.stdout, header=True):
if header:
for h in self.header:
print(h, end=end, file=file)
print("", file=file)
for obj in self.sequence:
for _, value in obj.__dict__.items():
if isinstance(value, type(None)):
print("<NA>", end=end, file=file)
elif isinstance(value, float):
print(round(value, 2), end=end, file=file)
else:
print(str(value), end=end, file=file)
print("", file=file)
return ""
def get_duration(self):
last_segment = self.sort().sequence[-1]
return float(last_segment.tbeg + last_segment.tdur)
def sort(self):
indices = np.argsort([seg.tbeg for seg in self.sequence])
sorted_segments = [self.sequence[i] for i in indices]
self.sequence = sorted_segments
return self
def split(self, time_sep):
duration = self.get_duration()
split_sequences = []
for i, end in enumerate(time_sep + [duration]):
subsequence = []
if i == 0:
start = 0.0
else:
start = time_sep[i-1]
for seg in self.sort().sequence:
tend = seg.tbeg + seg.tdur
if tend <= end and seg.tbeg >= start:
seg_copy = copy(seg)
seg_copy.tbeg = seg.tbeg - start
subsequence.append(seg_copy)
split_sequences.append(subsequence)
if len(self.sequence) != sum([len(seq) for seq in split_sequences]):
warnings.warn("The summed lengths of the split sequences differ from the length of the origina sequence. This can lead to missing/additional segments in the split sequences.")
return [RttmSeq(seq) for seq in split_sequences]
def append(self, new_sequence):
new_sequence_copy = deepcopy(new_sequence)
last_segment = self.sort().sequence[-1]
start = last_segment.tbeg + last_segment.tdur
for seg in new_sequence_copy.sequence:
seg.tbeg += start
self.sequence.append(seg)
return self
def write(self, filename):
check_rttm_filename(filename)
with open(filename, "w") as file:
self.__str__(end=" ", file=file, header=False)
def get_audio_segments(self, audio, sample_rate, max_length):
speech_segments = []
for seg in self.sequence:
start = int(seg.tbeg*sample_rate)
end = int(start + seg.tdur*sample_rate)
length = end - start
n = length // max_length + 1
subsegments = np.array_split(audio[start:end], n)
for subsegment in subsegments:
speech_segments.append(subsegment)
return speech_segments
def read_rttm(filename):
check_rttm_filename(filename)
with open(filename, "r") as file:
sequence = []
for row in file:
row_split = [None if cell == "<NA>" else cell for cell in row.split(" ")]
segment = RttmObj(
type=row_split[0],
file=row_split[1],
chnl=int(row_split[2]),
tbeg=float(row_split[3]),
tdur=float(row_split[4]),
ortho=row_split[5],
stype=row_split[6],
name=row_split[7],
conf=float(row_split[8]) if not isinstance(row_split[8], type(None)) else None
)
sequence.append(segment)
return RttmSeq(sequence, get_default_header())