Praat

class pympi.Praat.TextGrid(file_path=None, xmin=0, xmax=None, codec='ascii', stream=False)

Read write and edit Praat’s TextGrid files.

Note

All times are in seconds and can have decimals

Variables:
  • xmin (float) – Minimum x value.
  • xmax (float) – Maximum x value.
  • tier_num (int) – Number of tiers.
  • tiers (list) – Internal (unsorted) list of tiers.
  • codec (str) – Codec of the input file.
__init__(file_path=None, xmin=0, xmax=None, codec='ascii', stream=False)

Construct either a new TextGrid object or read one from a file/stream. When you create an empty TextGrid you must at least specify the xmax. Binary, short and normal TextGrids are supported.

Parameters:
  • file_path (str) – Path to read from, - for stdin. If None an empty TextGrid will be created.
  • xmin (int) – Xmin value, only needed when not loading from file.
  • xmax (int) – Xmax value, needed when not loading from file.
  • codec (str) – Text encoding for the input.
  • stream (bool) – Flag for loading from a stream(not used, only for debugging purposes)
Raises Exception:
 

If filepath is specified but no xmax

add_tier(name, tier_type='IntervalTier', number=None)

Add an IntervalTier or a TextTier on the specified location.

Parameters:
  • name (str) – Name of the tier, duplicate names is allowed.
  • tier_type (str) – Type of the tier. (‘IntervalTier’, ‘TextTier’)
  • number (int) – Place to insert the tier, when None the number is generated and the tier will be placed on the bottom.
Returns:

The created tier.

Raises ValueError:
 

If the number is out of bounds.

change_tier_name(name_num, name2)

Changes the name of the tier, when multiple tiers exist with that name only the first is renamed.

Parameters:
  • name_num (int or str) – Name or number of the tier to rename.
  • name2 (str) – New name of the tier.
Raises TierNotFoundException:
 

If the tier doesn’t exist.

from_stream(ifile, codec='ascii')

Read textgrid from stream.

Parameters:
  • ifile (file) – Stream to read from.
  • codec (str) – Text encoding.
get_tier(name_num)

Gives a tier, when multiple tiers exist with that name only the first is returned.

Parameters:name_num (int or str) – Name or number of the tier to return.
Returns:The tier.
Raises TierNotFoundException:
 If the tier doesn’t exist.
get_tier_name_num()

Give all tiers with their numbers.

Returns:List consisting of the form [(num1, tier1), (num2, tier2) ... (numn, tiern)]
get_tiers()

Give all tiers.

Yields:Available tiers
remove_tier(name_num)

Remove a tier, when multiple tiers exist with that name only the first is removed.

Parameters:name_num (int or str) – Name or number of the tier to remove.
Raises TierNotFoundException:
 If the tier doesn’t exist.
sort_tiers(key=None)

Sort the tiers given the key. Example key functions:

Sort according to the tiername in a list:

lambda x: ['name1', 'name2' ... 'namen'].index(x.name).

Sort according to the number of annotations:

lambda x: len(x.get_intervals())

Sort by name in reverse:

lambda x: list(reversed(['name1', 'name2' ... 'namen'])).index(x.name).

Parameters:key (func) – A key function. Default sorts on name.
to_eaf(pointlength=0.1)

Convert the object to an pympi.Elan.Eaf object

Parameters:

pointlength (int) – Length of respective interval from points in seconds

Returns:

pympi.Elan.Eaf object

Raises:
  • ImportError – If the Eaf module can’t be loaded.
  • ValueError – If the pointlength is not strictly positive.
to_file(filepath, codec='utf-8', short=False)

Write the object to a file.

Parameters:
  • filepath (str) – Path of the file, ‘-‘ for stdout.
  • codec (str) – Text encoding.
to_stream(f, codec='utf-8', short=False)

Write the object to a stream.

Parameters:
  • f (file) – Open stream to write to.
  • codec (str) – Text encoding.
  • short (bool) – Flag to use the short notation(saves space)
update()

Update the xmin, xmax and number of tiers value

class pympi.Praat.Tier(name=None, tier_type=None, ifile=None, codec='ascii', binary=False)

Class representing a TextGrid tier, either an Interval or TextTier

Variables:
  • name (str) – Name of the tier.
  • intervals (list) – List of intervals where each interval is (start, [end,] value).
  • tier_type (str) – Type of the tier(‘IntervalTier’ or ‘TextTier’).
  • xmin (int) – Minimum x value.
  • xmax (int) – Maximum x value.
__init__(name=None, tier_type=None, ifile=None, codec='ascii', binary=False)

Creates a tier, if lines is None a new tier is created and codec is ignored.

Parameters:
  • name (str) – Name of the tier.
  • tier_type (str) – Type of the tier(‘IntervalTier’ or ‘TextTier’).
  • lines (iter) – Iterator of the input lines.
  • codec (str) – Text encoding of the input.
  • binary (bool) – Flag to read the files in binary.
Raises TierTypeException:
 

If the tier type is unknown.

add_interval(begin, end, value, check=True)

Add an interval to the IntervalTier.

Parameters:
  • begin (float) – Start time of the interval.
  • end (float) – End time of the interval.
  • value (str) – Text of the interval.
  • check (bool) – Flag to check for overlap.
Raises:
  • TierTypeException – If the tier is not a IntervalTier.
  • Exception – If there is already an interval in that time.
add_point(point, value, check=True)

Add a point to the TextTier

Parameters:
  • point (int) – Time of the point.
  • value (str) – Text of the point.
  • check (bool) – Flag to check for overlap.
Raises:
  • TierTypeException – If the tier is not a TextTier.
  • Exception – If there is already a point at that time.
clear_intervals()

Removes all the intervals in the tier

get_intervals(sort=False)

Give all the intervals or points.

Parameters:sort (bool) – Flag for yielding the intervals or points sorted.
Yields:All the intervals
remove_interval(time)

Remove an interval, if no interval is found nothing happens.

Parameters:time (int) – Time of the interval.
Raises TierTypeException:
 If the tier is not a IntervalTier.
remove_point(time)

Remove a point, if no point is found nothing happens.

Parameters:time (int) – Time of the point.
Raises TierTypeException:
 If the tier is not a TextTier.
update()

Update the internal values

exception pympi.Praat.TierNotFoundException(tier)

Exception that is raised when a tier is adressed that doesn’t exist

__init__(tier)

Construct a TierNotFoundException.

Parameters:tier (int or str) – Name or number of the tier.
__weakref__

list of weak references to the object (if defined)

exception pympi.Praat.TierTypeException

Exception that is raised when a tiertype is unknown or wrong for the function in which it is raised

__init__()

Construct a TierTypeException.

__weakref__

list of weak references to the object (if defined)

pympi.Praat.VERSION = '1.30'

Helper objects/functions to extract data:

lamfloat - Extract binary float 8bytes lamint - Extract binary integer 4bytes lamshort - Extract binary integer 2bytes regfloat - Extract text float regint - Extract text integer regstr - Extract text string

pympi.Praat.lamstr(ifile)

Helper function for extracting string from a binary TextGrid

Parameters:ifile (file) – Stream with information