Diar

Diar is a class describing an audio/video segmentation file. A diarization contains a list of segments. Where each row is segment composed of n values identified by a attribut names. The diarization file is the most important file in the toolkit. All programs are driven by a diarization file and most of them generate a diarization file (trainer generate gmm).

A diarization stores a list of ‘’segments’’ composed of attributes.

A diarization could draw data from several shows. It is very useful in a batch mode context (training of GMM, computing log likelihood ratio, cross-show diarization, etc.).

Example

>>> diarization[0] //get the first segment
['20041006_0700_0800_CLASSIQUE', 'Emmanuel_Cugny', 'speaker', 164, 1170]
>>> diarization[0]['show']
'20041006_0700_0800_CLASSIQUE'
>>> diarization[0]['cluster']
'Emmanuel_Cugny'
>>> diarization[0]['start']
164
>>> diarization[0]['stop']
1170
where:
  • attribut 0 named ‘’show’‘: ‘‘20041006_0700_0800_CLASSIQUE’’ = the show speaker
  • attribut 1 named ‘’cluster’’ : ‘’Emmanuel_Cugny’’ the speaker speaker
  • attribut 2 named ‘’type’’ : ‘’speaker’’ contains the cluster type (speaker or head)
  • attribut 3 named ‘’start’‘: ‘‘164’’ is the index of the first feature of the segment
  • attribut 4 named ‘’stop’‘: ‘‘1170’’ is the index of the last feature of the segment

How to

  • Read a diarization:
    from s4d.diarization import Diar
    diarization = Diar.read_seg('foo.seg') //LIUM Spk Diarization format
    diarization = Diar.read_mdtm('foo.seg') //MDTM format
    diarization = Diar.read_rttm('foo.seg') //RTTM format
    diarization = Diar.read_uem('foo.seg') //UEM format
    
  • Get a segment:
    seg = diarization[0]
    
  • Get a attribut of a segment
    seg['cluster']
    
  • Write a diarization:
    diarization = Diar.write_seg('foo.seg', diarization) //LIUM format
    
  • Add or remove an attribut:
    diarization.add_attribut(speaker='gender', default=None) // add an attribut named 'gender, the default value is None
    diarization.del_attribut('gender') // remove the attribut
    
  • Sort a diarization:
    diarization.sort()
    
  • Create a new segment:
    diarization.append(show='foo', cluster='speaker', start=0, stop=100)
    
Add all the segments of diar2 into diar1:
diar1.append_diar(diar2)

Modules

class diar.AttributeNames[source]

Class AttributeNames defines a list of column names

add(name, default='')[source]

a a column speaker :param name: a str :param default: the default value

delete(name)[source]

remove a column and the default value given it column speaker :param name:

exist(name)[source]

Test the existing value of speaker in the list of column names :param name: the speaker of the column :return: a boolean

index_of(name)[source]
Parameters:name – the speaker of the column
Returns:the position (a int) of the speaker
initialize(names, defaults)[source]

Initialaze AttributeNames object :param names: a list of column names :param defaults: the list of default values

sorted()[source]

sort the column names :return:

class diar.Diar[source]

The diarization class.

Attr _attributes:
 a AttributeNames object storing the attribut definitions
Attr cluster_types:
 a list object
Attr segments:a list of Segment object
add_attribut(new_attribut, default='')[source]

Add a attribut :param new_attribut: the speaker of the new attribut :param default: the default value of the attribut

append(**kwargs)[source]

Transforme a list of values into a segment and append the segmnt into the existing segment list. :param kwargs: the values :return:

append_diar(out_diarization)[source]

Append a diarization. :param out_diarization: a diarization object

append_list(segment_lst)[source]

Append a list of segments into the existing segment segment_lst. :param segment_lst: a list of segments

append_seg(segment)[source]

Append a Segment object into the existing segment list.

Parameters:segment – a Segment object
clear()[source]

remove all the segments :return:

collar(epsilon=0, warning=False)[source]

Apply a collar on each segment. A collar is the no-score zone around reference speaker segment boundaries. (Speaker Diarization output is not evaluated within +/- collar seconds of a reference speaker segment boundary.) :param epsilon: the int value to add

copy_structure()[source]

Copy the internal structure of the diarization, ie the attribut names and the cluster types. The data is not copy. :return: a Diar object

del_all(attribute, value)[source]

Delete all segments satisfing the boolean expression [attribute = value] :param attribute: speaker of the attribute to delete :param value: :return:

del_attribut(attribut)[source]

Delete a attribut :param attribut: the speaker of the attribut to detele

duration()[source]
Returns:the sum of the segment duration
features(show=None, maximum_length=None)[source]

Generate the index features of a show :param show: a string corresponding to the speaker of the show :param maximum_length: maximum length of the show :return: a list object of indexes

features_by_cluster(show=None, maximum_length=None)[source]

Generate the indexes of a show :param show: the speaker of the show :param maximum_length: maximum length of the show :return: a dict object (keys are the cluster_list)

filter(attribute, operator, value)[source]

build a new diarization whose segments satisfy the boolean expression [attribute operator value] :param attribute: a attribute speaker (str) :param operator: a comperator opertor (> < >= <= in == !=) :param value: the value (int, float, str, list...) :return: a Diar object

id_map(id_attribut='cluster', show_attribut='show', prefix_id_attrubut=None, suffix_show_attribut=None)[source]

Generate a IdMap object for the StatServer :param id_attribut: speaker id_attribut attribut :param show_attribut: show_attribut attribut :param prefix_id_attrubut: prefix string of id_attribut :param suffix_show_attribut: suffix string of id_attribut

Parameters:out_diarization – a diarization object
Returns:a IdMap object
insert(i, **kwargs)[source]

Insert values into the list at offset index :param i: This is the Index where the object obj need to be inserted. :param kwargs: the values

classmethod intersection(diarization1, diarization2)[source]

Compute the intersection between two diarization :param diarization1: first diarization :param diarization2: second diarization :return: a diarization object

make_index(attributes)[source]

Build a n level key dictionary (dictionary of dictionaries of dictionaries...) based on Index. Index is an implementation of perl’s autovivification feature. The values contains a list of row.

example :

d = make_index([‘show’, ‘gender’, ‘cluster’])

print(d[‘show1’][‘M’][‘speaker’])

Parameters:attributes – a list of attribut _attributes corresponding to the key indexes
Returns:a dictionary of sub diarization. Segments are not copy.
pack(epsilon=0)[source]

merge segments with a gap less than epsilon :param epsilon: a int value

pad(epsilon=0)[source]

Add epsilon frames to the start and stop of each segment :param epsilon: the int value to remove :return:

classmethod read_ctm(filename, normalize_cluster=False)[source]

Read a segmentation file :param filename: the str input filename :param normalize_cluster: normalize the cluster by removing upper case and accents :return: a diarization object

classmethod read_mdtm(filename, normalize_cluster=False)[source]

Read a MDTM file :param filename: the str input filename :param normalize_cluster: normalize the cluster by removing upper case and accents :return: a diarization object

classmethod read_rttm(filename, normalize_cluster=False)[source]

Read rttm file :param filename: str input filename :param normalize_cluster: normalize the cluster by removing upper case and accents :return: a diarization object

classmethod read_seg(filename, normalize_cluster=False)[source]

Read a segmentation file :param filename: the str input filename :param normalize_cluster: normalize the cluster speaker by removing upper case and accents :return: a diarization object

classmethod read_uem(filename)[source]

Read a UEM file :param filename: the str input filename :return: a diarization object

remove_overlap()[source]

remove overlap zone :return: a new diarization without overlap

rename(attribute, old_values, new_value)[source]

Rename all values in list old_values into the new value new_value :param attribute: speaker of the attribute :param old_values: list of old values :param new_value: new value

sort(attributes=['show', 'start'], reverse=False)[source]

Sort the segments :param attributes: a list of attribut names :param reverse: if true, make a reverse sort

classmethod to_string_seg(diar)[source]

transform a diarization into a string :param diar: a diarization :return: a string

unique(attibute)[source]
Parameters:attibute – the attibute of the attribut
Returns:a list object of unique value of the attribut
classmethod write_lbl(diarization, label_dir='', label_file_extension='.lbl')[source]

Write diarization to label file :param diarization: the diarization to write :param label_dir: the string directory of the ouput filename :param label_file_extension: the string extension of the output filename

classmethod write_seg(filename, diarization)[source]

Write diarization to a segmentation file :param filename: the str output filename :param diarization: the diarization to write

class diar.Index(*args, **kwargs)[source]

Implementation of perl’s autovivification feature. Thanks to http://stackoverflow.com/questions/651794/whats-the-best-way-to-initialize-a-dict-of-dicts-in-python

class diar.Segment(data, attributes)[source]

Class to store the segment informations.

Attr _attributes:
 is the list of attribut names
Attr data:the data associated to each attribut
classmethod diff(segment1, segment2)[source]

The difference between the two segments. Returns one or two segments and the source of the new segment: 1 means segment1, 2 means segment2.

Parameters:
  • segment1 – a Segment object
  • segment2 – a Segment object
Returns:

a list of segments

duration()[source]
Returns:the duration of the segment
classmethod gap(segment1, segment2)[source]

Returns the inter segment gap between 2 segments. :param segment1: a Segment object :param segment2: a Segment object :return: a Segment object

>>> from s4d.diarization import Diar, Segment
>>> diarization=Diar()
>>> diarization.append(show='empty', start=0, stop=100, cluster='spk1')
>>> diarization.append(show='empty', start=50, stop=150, cluster='spk2')
>>> s = Segment.intersection(diarization[0], diarization[1])
>>> s
['empty', 'spk1', 'speaker', 100, 50]
>>> s.duration()
- 50
>>> diarization.append(show='empty', start=200, stop=250, cluster='spk1')
>>> Segment.gap(diarization[0], diarization[2])
['empty', 'spk1', 'speaker', 100, 200]
classmethod intersection(segment1, segment2)[source]

Intersection between 2 segments. Return None if the intersection is empty. :param segment1: a Segment object :param segment2: a Segment object :return: a Segment object

>>> from s4d.diarization import Diar, Segment
>>> diarization=Diar()
>>> diarization.append(show='empty', start=0, stop=100, cluster='spk1')
>>> diarization.append(show='empty', start=50, stop=150, cluster='spk2')
>>> Segment.intersection(diarization[0], diarization[1])
['empty', 'spk1 / spk2', 'speaker', 50, 100]
>>> diarization.append(show='empty', start=50, stop=75, cluster='spk1')
>>> Segment.intersection(diarization[0], diarization[2])
['empty', 'spk1 / spk1', 'speaker', 50, 75]
>>> diarization.append(show='empty', start=200, stop=250, cluster='spk1')
>>> s = Segment.intersection(diarization[0], diarization[3])
>>> s is None

True

seg_features(features)[source]

Given a FeatureServer, returns a list of feature index corresponding to the segment.

Parameters:features – a FeatureServer
Returns:a list of int
classmethod union(segment1, segment2)[source]

Union between 2 segments. :param segment1: a Segment object :param segment2: a Segment object :return: a Segment object

>>> from s4d.diarization import Diar, Segment
>>> diarization=Diar()
>>> diarization.append(show='empty', start=0, stop=100, cluster='spk1')
>>> diarization.append(show='empty', start=50, stop=150, cluster='spk2')
>>> Segment.union(diarization[0], diarization[1])
['empty', 'spk1', 'speaker', 0, 150]
>>> diarization.append(show='empty', start=50, stop=75, cluster='spk1')
>>> Segment.union(diarization[0], diarization[2])
['empty', 'spk1', 'speaker', 0, 100]
>>> diarization.append(show='empty', start=200, stop=250, cluster='spk1')
>>> Segment.union(diarization[0], diarization[3])

[‘empty’, ‘spk1’, ‘speaker’, 0, 250]

True

diar.rolling_window(a, window)[source]

Make an ndarray with a rolling window of the last dimension

>>> x=numpy.arange(10).reshape((2,5))
>>> rolling_window(x, 3)
array([[[0, 1, 2], [1, 2, 3], [2, 3, 4]],
       [[5, 6, 7], [6, 7, 8], [7, 8, 9]]])

Calculate rolling mean of last dimension: >>> numpy.mean(rolling_window(x, 3), -1) array([[ 1., 2., 3.],

[ 6., 7., 8.]])
Parameters:
  • a – Array to add rolling window to
  • window – Size of rolling window
Returns:

Array that is a view of the original array with a added dimension

of size w.