viewer_utils.py 4.62 KB
Newer Older
Anthony Larcher's avatar
Anthony Larcher committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# -*- coding: utf-8 -*-
#
# This file is part of s4d.
#
# s4d is a python package for speaker diarization.
# Home page: http://www-lium.univ-lemans.fr/s4d/
#
# s4d is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# s4d is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with s4d.  If not, see <http://www.gnu.org/licenses/>.


"""
Anthony Larcher's avatar
Anthony Larcher committed
23
Copyright 2014-2020 Sylvain Meignier
Anthony Larcher's avatar
Anthony Larcher committed
24
25
"""

Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
26
import copy
Anthony Larcher's avatar
Anthony Larcher committed
27
28
29
30
import logging

from ..diar import Diar, Segment
from ..scoring import DER
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
31

Sylvain Meignier's avatar
Sylvain Meignier committed
32

Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
33
def add_match(ref, hyp, assign, not_assigned):
Anthony Larcher's avatar
Anthony Larcher committed
34
35
36
37
38
39
40
41
    """

    :param ref:
    :param hyp:
    :param assign:
    :param not_assigned:
    :return:
    """
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
42
43
44
    inv_map = {v: k for k, v in assign.items()}
    i = 0

Sylvain Meignier's avatar
Sylvain Meignier committed
45
    l = 0
46
47
48
49
50
51
    for cluster in ref.unique('cluster'):
        if len(cluster) > l:
            l = len(cluster)
    for cluster in hyp.unique('cluster'):
        if len(cluster) > l:
            l = len(cluster)
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
52
53
54
55
56
57
58
    f = 'Match {:03d} {:>'+str(l)+'s}'
    for key in sorted(inv_map, reverse=True):
        v = inv_map[key]
        r = v[0]
        h = v[1]
        nr = f.format(i, r)
        nh = f.format(i, h)
59
60
        ref.rename('cluster', [r], nr)
        hyp.rename('cluster', [h], nh)
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
61
62
63
64
        i += 1
    f = 'NotMatch {:>'+str(l)+'s}'
    for h in not_assigned:
        nh = f.format(h)
65
        hyp.rename('cluster', [h], nh)
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
66
67
68


def add_intersection(ref, hyp, assign):
Anthony Larcher's avatar
Anthony Larcher committed
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
    """

    :param ref:
    :param hyp:
    :param assign:
    :return:
    """
    diar = Diar()
    if not diar._attributes.exist('color'):
        diar.add_attribut('color', (0.0, 0.0, 0.0, 1.0))
    map_ref = ref.make_index(['cluster'])
    map_hyp = hyp.make_index(['cluster'])
    for (lr, lh) in assign:
        for seg1 in map_ref[lr]:
            f = False
            stop = seg1['stop']
            for Seg2 in map_hyp[lh]:
                if stop < Seg2['start']:
                    continue
                inter1 = Segment.intersection(seg1, Seg2)
                if inter1 is not None:
                    f = True
                    inter1['color'] = (0.6, 0.0, 1.0, 1.0)
                    inter1['cluster'] = seg1['cluster']
                    diar.append_seg(inter1)
                    inter2 = copy.deepcopy(inter1)
                    inter2['color'] = (0.0, 0.6, 1.0, 1.0)
                    inter2['cluster'] = Seg2['cluster']
                    diar.append_seg(inter2)
                elif f:
                    break
    return diar
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
101

Sylvain Meignier's avatar
Sylvain Meignier committed
102

Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
103
def add_prefix(diar, prefix):
Anthony Larcher's avatar
Anthony Larcher committed
104
105
106
107
108
109
    """

    :param diar:
    :param prefix:
    :return:
    """
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
110
    for seg in diar:
111
        seg['cluster'] = prefix+seg['cluster']
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
112

Sylvain Meignier's avatar
Sylvain Meignier committed
113

Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
114
def add_collar(diar, collar):
Anthony Larcher's avatar
Anthony Larcher committed
115
116
117
118
119
120
    """

    :param diar:
    :param collar:
    :return:
    """
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
121
122
    local_diar = Diar()
    local_diar.add_attribut('color', (0.2, 0.2, 0.2, 1.0))
Sylvain Meignier's avatar
new    
Sylvain Meignier committed
123
    if local_diar._attributes.exist('type'):
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
124
125
126
127
128
129
130
        local_diar.add_attribut('type', 'collar')
    l = len(diar)
    for seg in diar:
        if l % 100 == 0:
            print(l)
        l -= 1
        show = seg['show']
131
        name = seg['cluster']
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
132
133
        start = seg['start']
        stop = seg['stop']
134
135
        local_diar.append(show=show, cluster=name, start=start - collar, stop=start+collar)
        local_diar.append(show=show, cluster=name, start=stop - collar, stop=stop+collar)
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
136
137
    diar.append_diar(local_diar)

Sylvain Meignier's avatar
Sylvain Meignier committed
138

Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
139
def diar_diff(hyp, ref, match=True, inter=False, collar=25):
Anthony Larcher's avatar
Anthony Larcher committed
140
141
142
143
144
145
146
147
148
    """

    :param hyp:
    :param ref:
    :param match:
    :param inter:
    :param collar:
    :return:
    """
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
149
    hyp = copy.deepcopy(hyp)
150
    if not hyp._attributes.exist('color'):
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
151
152
153
154
        hyp.add_attribut('color', (0.0, 0.6, 0.0, 1.0))
    hyp.pack()

    ref = copy.deepcopy(ref)
155
    if not ref._attributes.exist('color'):
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
156
157
158
159
160
161
162
163
164
165
166
        ref.add_attribut('color', (0.6, 0.0, 0.0, 1.0))
    ref.pack()

    der = DER(hyp, ref)
    der.confusion()
    assign, not_assign = der.assignment()
    if match:
        add_match(ref, hyp, assign, not_assign)
    else:
        add_prefix(ref, 'REF: ')
        add_prefix(hyp, 'HYP: ')
167
168
169
170
        
    diar = copy.deepcopy(ref)
    diar.append_diar(hyp)
    
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
171
    if inter:
172
        logging.info('append intersection')
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
173
174
175
        diar.append_diar(add_intersection(ref, hyp, assign))

    if collar > 0:
Florent Desnous 's avatar
Florent Desnous committed
176
        logging.info('append collar')
Sylvain Meignier's avatar
Origin  
Sylvain Meignier committed
177
        add_collar(diar, collar)
Anthony Larcher's avatar
Anthony Larcher committed
178
179
    return diar