o
    xi8                     @   s   d Z ddlmZmZmZmZmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ d
d Zddee
 fddZG dd dejZdS )ze
A PointTier is a tier containing an array of points -- data that exists at a specific point in time
    )ListTupleOptionalAnySequence)Literal)Point
POINT_TIER)	constants)errors)utils)my_math)textgrid_tierc                 C   s   dd | D }|   |S )z
    Enforces consistency in points

    - converts all entries to points
    - removes whitespace in labels
    - sorts values by time
    c                 S   s"   g | ]\}}t t|| qS  )r   floatstrip.0timelabelr   r   b/home/ubuntu/maya3_transcribe/venv/lib/python3.10/site-packages/praatio/data_classes/point_tier.py
<listcomp>   s   " z&_homogenizeEntries.<locals>.<listcomp>)sort)entriesprocessedEntriesr   r   r   _homogenizeEntries   s   r   Nr   c                 C   sl   dd | D }|d ur| t| |d ur| t| zt|}t|}W ||fS  ty5   t w )Nc                 S      g | ]\}}|qS r   r   r   r   r   r   r   #       z+_calculateMinAndMaxTime.<locals>.<listcomp>)appendr   minmax
ValueErrorr   TimelessTextgridTierException)r   minTmaxTtimeListcalculatedMinTcalculatedMaxTr   r   r   _calculateMinAndMaxTime"   s   
r(   c                       s  e Zd ZeZeZ		d<dedee de	e
 de	e
 f fddZedee
 fd	d
Z		d=de
de
ded dedd f
ddZdeddfddZ	d>dejde
dd fddZ	d?de
ded dd fd d!Z	"d@d#eee
d$f  d%edeeed$f  fd&d'Z	(	dAd)e
d*e
d+ed, d-edd f
d.d/Z	(	dBded+ed0 d1ed2 ddfd3d4Z	(dCd)e
d5e
d6ed7 dd fd8d9Z	d?ded defd:d;Z  ZS )D	PointTierNnamer   r#   r$   c                    s2   t |}t|||\}}tt| |||| dS )a{  A point tier is for annotating instaneous events

        The entries is of the form:
        [(timeVal1, label1), (timeVal2, label2), ]

        The data stored in the labels can be anything but will
        be interpreted as text by praatio (the label could be descriptive
        text e.g. ('peak point here') or numerical data e.g. (pitch values
        like '132'))
        N)r   r(   superr)   __init__)selfr*   r   r#   r$   r&   r'   	__class__r   r   r,   6   s   zPointTier.__init__returnc                 C   s(   dd | j D }tt|}|  |S )z'All unique timestamps used in this tierc                 S   r   r   r   )r   r   _r   r   r   r   O   r   z(PointTier.timestamps.<locals>.<listcomp>)r   listsetr   )r-   tmpTimestampsuniqueTimestampsr   r   r   
timestampsL   s   zPointTier.timestampslaxT	cropStartcropEndmode)strictr7   	truncatedrebaseToZeroc           
         s    |krt d  d| dg }| jD ]}|j}| kr'||kr'|| q|du r< fdd|D }d}|  }	n }|}	t| j|||	S )a  Creates a new tier containing all entries inside the new interval

        Args:
            cropStart:
            cropEnd:
            mode: Mode is ignored.  This parameter is kept for
                compatibility with IntervalTier.crop()
            rebaseToZero: if True, all entries will have their
                timestamps subtracted by *cropStart*.

        Returns:
            the modified version of the current tier
        zCrop error: start time (z) must occur before end time ()Tc                    s   g | ]\}}t |  |qS r   )r   )r   timeVr   r8   r   r   r   x   s    z"PointTier.crop.<locals>.<listcomp>g        )r   ArgumentErrorr   r   r   r)   r*   )
r-   r8   r9   r:   r=   
newEntriesentry	timestampr#   r$   r   r@   r   cropV   s&   



zPointTier.croprC   c                 C   s   | j | j | dS )z!Removes an entry from the entriesN)_entriespopindex)r-   rC   r   r   r   deleteEntry   s   zPointTier.deleteEntryMbP?referenceTiermaxDifferencec                    s`   |j }g }| jD ]!\ }t| fddd}tt | |r"| | |f q| j|dS )aC  
        Set timestamps in this tier to be the same as values in the reference tier

        Timestamps will only be moved if they are less than maxDifference away from the
        reference time.

        This can be used to correct minor alignment errors between tiers, as made when
        annotating files manually, etc.

        Args:
            referenceTier: the IntervalTier or PointTier to use as a reference
            maxDifference: the maximum amount to allow timestamps to be moved by

        Returns:
            the modified version of the current tier
        c                    s   t |   S )N)abs)xr   r   r   <lambda>   s    z$PointTier.dejitter.<locals>.<lambda>)key)r   )r6   r   r   r   lessThanOrEqualrM   r   new)r-   rK   rL   referenceTimestampsrB   r   timeComparer   rO   r   dejitter   s   zPointTier.dejitterwarningoffsetreportingMode)silencerW   errorc                 C   s   t d|tj t |}g }| jD ]%\}}|| }t || j| t || j	| |dk r/q|
t|| qdd |D }t|}	t|}
|	| jkrO| j}	|
| j	k rW| j	}
t| j||	|
S )at  Modifies all timestamps by a constant amount

        Args:
            offset:
            reportingMode: one of "silence", "warning", or "error". This flag
                determines the behavior if an entries moves outside of minTimestamp
                or maxTimestamp after being edited

        Returns:
            the modified version of the current tier
        rY   r   c                 S   s   g | ]}t |jqS r   )r   r   )r   pointr   r   r   r      s    z,PointTier.editTimestamps.<locals>.<listcomp>)r   validateOptionr
   ErrorReportingModegetErrorReporterr   checkIsUndershootminTimestampcheckIsOvershootmaxTimestampr   r   r   r    r)   r*   )r-   rX   rY   errorReporterrB   rD   r   newTimestampr%   newMinnewMaxr   r   r   editTimestamps   s(   


zPointTier.editTimestampsFdataTupleList.fuzzyMatchingc           
      C   sH   d}g }t |}| jD ]\}}tj||||d}|\}	}||	 q|S )a  Get the values that occur at points in the point tier

        The procedure assumes that all data is ordered in time.
        dataTupleList should be in the form
        [(t1, v1a, v1b, ..), (t2, v2a, v2b, ..), ..]

        It returns the data in the form of
        [(t1, v1a, v1b, ..), (t2, v2a, v2b), ..]

        The procedure makes one pass through dataTupleList and one
        pass through self.entries.  If the data is not sequentially
        ordered, the incorrect response will be returned.

        Args:
            dataTupleList:
            fuzzyMatching: if True, if there is not a feature value
                at a point, the nearest feature value will be taken.

        Returns:
            A list of values that exist at the given timepoints
        r   )rj   startI)sortedr   r   getValueAtTimer   )
r-   ri   rj   currentIndexretListsortedDataTupleListrD   r   retTupleretRowr   r   r   getValuesAtPoints   s   zPointTier.getValuesAtPointsr[   startendcollisionMode)truncatecategoricalr[   doShrinkc                 C   s   |   }|||tjjd}|j}t|dkr&|ddd D ]}|| q|du r^g }	|| }
|jD ]}|j|k r@|		| q3|j|krQ|		t
|j|
 |j q3|j|
 }|j |	|d}|S )a  Makes a region in a tier blank (removes all contained entries)

        Args:
            start: the start of the deletion interval
            end: the end of the deletion interval
            collisionMode: Ignored for the moment (added for compatibility with
                eraseRegion() for Interval Tiers)
            doShrink: if True, moves leftward by (/end/ - /start/) all points
                to the right of /end/

        Returns:
            The modified version of the current tier
        Fr   NTr   rc   )rS   rE   r
   CropCollision	TRUNCATEDr   lenrI   r   r   r   r   rc   )r-   rt   ru   rv   ry   newTiercroppedTier	matchListr\   rB   diffrg   r   r   r   eraseRegion   s$   



zPointTier.eraseRegion)replacemerger[   collisionReportingMode)rZ   rW   c              	   C   s|  t d|tj t d|tj t |}t|ts$t|d |d }n|}g }d}t| j	D ]\}}|j
|j
kr@||  nq/t|dkrN| j| nQ|tjjkrc| | j	|  | j| n<|tjjkr| j	| }	t|j
d|	j|jg}
| | j|  | j|
 ntd| d| j d	d
d |D  d|   t|dkr|tjd| d| d| j d dS dS )a  Inserts an interval into the tier

        Args:
            entry: the entry to insert
            collisionMode: determines the behavior if intervals exist in
                the insertion area.
                - 'replace', existing items will be removed
                - 'merge', inserting item will be fused with existing items
                - 'error', will throw TextgridCollisionException
            collisionReportingMode:

        Returns:
            None
        rv   r   r      N-zAttempted to insert interval z into tier z% of textgrid but overlapping entries c                 S   s   g | ]}t |qS r   )tuple)r   intervalr   r   r   r   j  r   z)PointTier.insertEntry.<locals>.<listcomp>z already existzCollision warning for (z) with items (z) of tier '')r   r]   r
   IntervalCollisionr^   r_   
isinstancer   	enumerater   r   r   r~   rF   REPLACErI   MERGEjoinr   r   CollisionErrorr*   r   )r-   rC   rv   r   collisionReporternewPointr   ir\   oldPointmergedPointr   r   r   insertEntry,  sV   



zPointTier.insertEntryduration_collisionMode)stretchsplit	no_changer[   c                 C   s`   g }| j D ]}|j|kr|| q|j|kr#|t|j| |j q| j|| j| d}|S )a{  Inserts a region into the tier

        Args:
            start: the start time to insert a space at
            duration: the duration of the space to insert
            collisionMode: Ignored for the moment (added for compatibility
                with insertSpace() for Interval Tiers)

        Returns:
            PointTier: the modified version of the current tier
        r{   )r   r   r   r   r   rS   rc   )r-   rt   r   r   rB   r\   r   r   r   r   insertSpacev  s   



zPointTier.insertSpacec                 C   s   t d|tj t |}d}d}| jD ]1}|r-|j|jkr-d}|tjd| d| d t 	|j| j
|r8d}t |j| j|rCd}|}q|S )a  Validate this tier

        Returns whether the tier is valid or not. If reportingMode is "warning"
        or "error" this will also print on error or stop execution, respectively.

        Args:
            reportingMode: Determines the behavior if there is a size difference
                between the maxTimestamp in the tier and the current textgrid.

        Returns:
            True if this tier is valid; False otherwise
        rY   TNFz!Points are not sorted in time: [(z), (z)])r   r]   r
   r^   r_   r   r   r   TextgridStateErrorr`   ra   rb   rc   )r-   rY   rd   isValidpreviousPointr\   r   r   r   validate  s.   

zPointTier.validateNN)r7   T)rJ   )rW   )F)r[   T)r[   rW   )r[   )__name__
__module____qualname__r	   tierTyper   	entryTypestrr   r   r   r,   propertyr6   r   boolrE   rI   r   TextgridTierrV   rh   r   r   rs   r   r   r   r   __classcell__r   r   r.   r   r)   2   s    
-
"
0
/
1
N
 r)   r   )__doc__typingr   r   r   r   r   typing_extensionsr   praatio.utilities.constantsr   r	   praatio.utilitiesr
   r   r   r   praatio.data_classesr   r   r(   r   r)   r   r   r   r   <module>   s    