
    3j|                    H   d Z ddlmZ ddlZddlZddlZddlZddl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
lmZ ddlmZ ddlmZ ddlmZ ej0                  rddlmZ dZ G d dej6                        Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de      Z! G d de!      Z" G d de!      Z# G d  d!e"      Z$ G d" d#e      Z% G d$ d%e      Z& G d& d'ejN                        Z(e)d(k(  rddlZ ejT                  e(       yy))z
Definitions for extracting data from a Stream to place on one axis of a
:class:`~music21.graph.plot.PlotStream` or similar object.
    )annotationsN)accidentalLabelToUnicodeGraphException)bar)common)duration)dynamics)pitch)prebase)stream)elements)pitchAnalysis)notec                      e Zd ZU dZddddddZded	<   d
ZddddZdZded<   ddZ	d Z
ed        Zej                  d        Zed        Zej                  d        Zed        ZddZd Zd Zd dZy)!Axisz
    An Axis is an easier way of specifying what to plot on any given axis.

    Client should be a .plot.PlotStream or None.  Eventually a Stream may be allowed,
    but not yet.
    z=the name of the axis.  One of "x" or "y" or for 3D Plots, "z"zV
            None or number representing the axis minimum.  Default None.
            zV
            None or number representing the axis maximum.  Default None.
            a{  
            a dict of {'x': 0, 'y': 1, 'z': 2} mapping where an axis's data can
            be found in self.client.data after extract data is run:

            >>> b = corpus.parse('bwv66.6')
            >>> plot = graph.plot.ScatterPitchClassOffset(b)
            >>> pcAxis = plot.axisY
            >>> pcAxis.axisName
            'y'
            >>> pcAxisDataIndex = pcAxis.axisDataMap[pcAxis.axisName]
            >>> pcAxisDataIndex
            1
            >>> plot.extractData()
            >>> pcValues = [dataTuple[pcAxisDataIndex] for dataTuple in plot.data]
            >>> pcValues[0:2]
            [1, 11]
            a  
            a tuple of strings representing the quantities the axis can plot.
            The first element of the tuple is the authoritative name.

            >>> ax = graph.axis.DynamicsAxis()
            >>> ax.quantities
            ('dynamic', 'dynamics', 'volume')
            )axisNameminValuemaxValueaxisDataMap
quantitiesdict[str, str]	_DOC_ATTRzan axisr         )xyz)genericonenothingblanktuple[str, ...]r   Nc                    t        |t              rt        d      d | _        d | _        || _        || _        d | _        d | _        y )Nz,Client must be a PlotStream, Stream, or None)	
isinstancestrr   _client_labelclientr   r   r   )selfr)   r   s      ?/DATA/.local/lib/python3.12/site-packages/music21/graph/axis.py__init__zAxis.__init__[   sB    fc" !OPP     c                t    | j                   }||j                  j                  }nd}d| j                   d| S )a  
        The representation of the Axis shows the client and the axisName
        in addition to the class name.

        >>> s = stream.Stream()
        >>> plot = graph.plot.ScatterPitchClassQuarterLength(s)
        >>> plot.axisX
        <music21.graph.axis.QuarterLengthAxis: x axis for ScatterPitchClassQuarterLength>

        >>> plot.axisY
        <music21.graph.axis.PitchClassAxis: y axis for ScatterPitchClassQuarterLength>

        >>> axIsolated = graph.axis.DynamicsAxis(axisName='z')
        >>> axIsolated
        <music21.graph.axis.DynamicsAxis: z axis for (no client)>

        >>> s = stream.Part()
        >>> axStream = graph.axis.DynamicsAxis(s, axisName='y')
        >>> axStream
        <music21.graph.axis.DynamicsAxis: y axis for Part>

        z(no client)z: z
 axis for )r)   	__class____name__r   )r*   c
clientNames      r+   _reprInternalzAxis._reprInternalg   s=    . KK=--J&JDMM?*ZL99r-   c                J    | j                   | j                   S | j                  S )z
        Returns self.label or class.labelDefault if not set:

        >>> ax = graph.axis.Axis(axisName='y')
        >>> ax.label
        'an axis'
        >>> ax.label = 'velocity'
        >>> ax.label
        'velocity'
        )r(   labelDefaultr*   s    r+   labelz
Axis.label   s$     ;;";;$$$r-   c                    || _         y Nr(   r*   values     r+   r7   z
Axis.label   	    r-   c                @    t        j                  | j                        S )z
        The client stores a reference to the Plot that
        makes reference to this axis.

        (Like all music21 clients, It is normally stored internally as a weakref,
        so no need for garbage collecting)
        )r   unwrapWeakrefr'   r6   s    r+   r)   zAxis.client   s     ##DLL11r-   c                8    t        j                  |      | _        y r9   )r   wrapWeakrefr'   )r*   referents     r+   r)   zAxis.client   s    ))(3r-   c                p    | j                   }|yt        |t        j                        r|S |j                  S )z
        Returns a reference to the client's .streamObj  (or None if client is None)

        If the client is itself a stream, return it.

        Read-only
        N)r)   r%   r   Stream	streamObj)r*   r1   s     r+   r   zAxis.stream   s2     KK96==)H;;r-   c                     y)z(
        Override in subclasses
        r    r*   n
formatDicts      r+   extractOneElementzAxis.extractOneElement   s     r-   c                    | j                   |rt        |      | _         | j                  |rt        |      | _        yyy)a{  
        If self.minValue is not set,
        then set self.minValue to be the minimum of these values.

        Same with maxValue

        >>> ax = graph.axis.Axis()
        >>> print(ax.minValue)
        None

        >>> values = [10, 0, 3, 5]
        >>> ax.setBoundariesFromData(values)
        >>> ax.minValue
        0
        >>> ax.maxValue
        10

        If a boundary is given or .setXXXFromData is False then no changes are made

        >>> ax = graph.axis.Axis()
        >>> ax.minValue = -1
        >>> ax.setBoundariesFromData(values)
        >>> ax.minValue
        -1
        >>> ax.maxValue
        10
        N)r   minr   maxr*   valuess     r+   setBoundariesFromDatazAxis.setBoundariesFromData   s:    : == VKDM== VKDM &, r-   c                   | j                   }| j                  }|d}|d}||z
  }|dk(  rd}n!t        t        j                  ||z
              }d|z  }|dkD  r||z  dk  rt        |dz        }t        ||z        dz
  |z  }|dk  r|dk\  rd}t        ||z        dz   |z  }t        |||      }g }	|D ]  }
|	j                  |
t        |
      f         |	S )az  
        Get a set of ticks for this data.  Used by several numeric axes
        to make a reasonable number of ticks.

        >>> cax = graph.axis.Axis()
        >>> cax.minValue = 1
        >>> cax.maxValue = 9
        >>> cax.ticks()
        [(0, '0'), (1, '1'), (2, '2'), (3, '3'), (4, '4'),
         (5, '5'), (6, '6'), (7, '7'), (8, '8'), (9, '9'), (10, '10')]

        For larger data, the ticks are farther apart.

        >>> cax.minValue = 7
        >>> cax.maxValue = 80
        >>> cax.ticks()
        [(0, '0'), (10, '10'), (20, '20'), (30, '30'), (40, '40'),
         (50, '50'), (60, '60'), (70, '70'), (80, '80'), (90, '90')]

        >>> cax.minValue = 712
        >>> cax.maxValue = 2213
        >>> cax.ticks()
        [(600, '600'), (700, '700'), (800, '800'), (900, '900'), (1000, '1000'),
         ...
         (2100, '2100'), (2200, '2200'), (2300, '2300')]
        r   
   r   r   )r   r   intmathlog10rangeappendr&   )r*   minVmaxV
differencelog10distance	closest10
startValue	stopValuestepstickstickNums              r+   ra   z
Axis.ticks   s    6 }}}}<D<DD[
?M

4$; 78M-'	q=j94:IN+I$*+a/9<
>daiJ	)*Q.);	j)Y7GLL'3w<01 r-   c                     y)z
        Routine to be called after data has been extracted to
        do any cleanup, etc.  Defaults to doing nothing, but
        see CountingAxis for an example of how this works.
        NrG   )r*   dataLists     r+   postProcessDatazAxis.postProcessData  s     	r-   Nr   )rI   znote.GeneralNoterJ   zdict[str, t.Any]returnzt.Anyr9   )r0   
__module____qualname____doc__r   __annotations__r5   r   r   r,   r3   propertyr7   setterr)   r   rK   rQ   ra   re   rG   r-   r+   r   r   -   s     T"3!!I~ !F L*K"HJH
:> % %  \\  2 2 ]]4 4    (D7rr-   r   c                  h     e Zd ZU dZdddddZded<   d	Zd
Zded<   d fd	Ze	dd       Z
d Z xZS )	PitchAxisz0
    Axis subclass for dealing with Pitches
    za
            bool on whether to show both common enharmonics in labels, default True
            zZ
            bool on whether to hide labels for unused pitches, default True.
            zw
            bool on whether not to even show a tick when a pitch doesn't exist.
            default True.
            z
            bool or 'few' about whether to show octave numbers.  If 'few' then
            only the first pitch in each octave is shown.  Default 'few'
            )showEnharmonicblankLabelUnused
hideUnusedshowOctavesr   r   Pitch)pitchGenericr#   r   c                ^    t         |   ||       d| _        d| _        d| _        d| _        y )NfewT)superr,   rs   rp   rq   rr   r*   r)   r   r/   s      r+   r,   zPitchAxis.__init__>  s1    * " $r-   c                Z    g }| D ]#  \  }}t        |      }|j                  ||f       % |S )u  
        Given a list of ticks, replace all labels with alternative/unicode symbols where necessary.

        >>> ticks = [(60, 'C4'), (61, 'C#4'), (62, 'D4'), (63, 'E-4')]
        >>> t2 = graph.axis.PitchAxis.makePitchLabelsUnicode(ticks)
        >>> len(t2)
        4
        >>> [num for num, label in t2]
        [60, 61, 62, 63]
        >>> t2[0]
        (60, 'C4')
        >>> for num, label in t2:
        ...     label
        'C4'
        'C♯4'
        'D4'
        'E♭4'
        )r   rX   )ra   postr<   r7   s       r+   makePitchLabelsUnicodez PitchAxis.makePitchLabelsUnicodeE  s8    0 !LE5,U3EKK' " r-   c                   | j                   }|i }nt        j                  ||      }g }i }t               }d }d }	t	        t        | j                        t        | j                        dz         D ]  }
t        j                         }t        |||
       g }|D ]G  }||vr"t        t        j                  |      |      ||<   ||   |
k(  s2|j                  ||   |f       I | j                  r|j                  |	       n|j                  |       d}|s)| j                  r| j                   st        ||      }nOd}nL| j                  s	|d   d   }n7g }|D ]  \  }}|j                  t#        |             ! dj%                  |      }| j&                  d	u rt)        j*                  d
d|      }ne| j&                  dk(  rVt)        j,                  d
|      }|r>|j/                  d      }||v rt)        j*                  d
d|      }n|j1                  |       |j                  |
|f        | j3                  |      }|S )z
        Helper method that can apply all the showEnharmonic, etc. values consistently

        see the `ticks` methods below.

        Returns a list of two-element tuples
        Nc                L    | \  }}|j                  d      rd|dd z   }d|z  |fS )a  
            ensure that higher weighed weights come first, but
            then alphabetical by name, except that G comes before
            A.  That's the only "out of order" item we need to be
            concerned with since we are only comparing enharmonics.
            AHr   Nr   
startswithr   weight	sort_names      r+   weightedSortHelperz6PitchAxis._pitchTickHelper.<locals>.weightedSortHelperw  s;     !"FI##C()AB-/	K++r-   c                F    | \  }}|j                  d      rd|dd  z   }||fS )Nr   r   r   r   r   s      r+   unweightedSortHelperz8PitchAxis._pitchTickHelper.<locals>.unweightedSortHelper  s5     !FI##C()AB-/	I&&r-   r   )key r   /Fz\drw   )r   r   pitchAttributeCountsetrW   rT   r   r   r
   rt   setattrgetattrrX   rp   sortrr   rq   r   joinrs   resubsearchgroupaddr|   )r*   attributeCounterattributeCompares	nameCountra   
helperDictoctavesSeenr   r   ipweightsr   r7   r   unused_weightnamematchOctaveoctaveMatchs                       r+   _pitchTickHelperzPitchAxis._pitchTickHelperc  s    KK9I%99!=MNI
e
	,	' s4==)3t}}+=+ABAAA'+G j( '.ekk#.>@P&QJsOc?a'NNIcNC#89 ! ""!56!34E??,,#A'78EE((
1+2'M4JJ7=> ,35(ub%0!!U* iiu5"-"3"3A"6K"k1 "ub% 8#4LL!U$] C^ ++E2r-   rf   )ra   list[tuple[t.Any, str]]rg   r   )r0   rh   ri   rj   r   rk   r5   r   r,   staticmethodr|   r   __classcell__r/   s   @r+   ro   ro   '  sZ    !I~   L"4J4  :Vr-   ro   c                  F     e Zd ZU dZdZdZded<   d	 fd	Zd
dZd Z	 xZ
S )PitchClassAxiszr
    Axis subclass for dealing with PitchClasses

    By default, axis is not set from data, but set to 0, 11
    zPitch Class)
pitchClass
pitchclasspcr#   r   c                P    d| _         t        | 	  ||       d| _        d| _        y )NFr      )rs   rx   r,   r   r   ry   s      r+   r,   zPitchClassAxis.__init__  s(     *r-   c                H    t        |d      r|j                  j                  S y Nr
   )hasattrr
   r   rH   s      r+   rK   z PitchClassAxis.extractOneElement  s    1g77%%%r-   c                &    | j                  dd      S )u'
  
        Get ticks and labels for pitch classes.

        If `showEnharmonic` is `True` (default) then
        when choosing whether to display as sharp or flat use
        the most commonly used enharmonic.

        >>> s = corpus.parse('bach/bwv324.xml')
        >>> s.analyze('key')
        <music21.key.Key of G major>

        >>> plotS = graph.plot.PlotStream(s)
        >>> ax = graph.axis.PitchClassAxis(plotS)
        >>> ax.hideUnused = True

        Ticks returns a list of two-element tuples:

        >>> ax.ticks()
        [(0, 'C'), (2, 'D'), ..., (11, 'B')]

        >>> for position, noteName in ax.ticks():
        ...            print(str(position) + ' ' + noteName)
        0 C
        2 D
        3 D♯
        4 E
        6 F♯
        7 G
        9 A
        11 B

        >>> s = corpus.parse('bach/bwv281.xml')
        >>> plotS = graph.plot.PlotStream(s)
        >>> ax = graph.axis.PitchClassAxis(plotS)
        >>> ax.hideUnused = True
        >>> ax.showEnharmonic = True

        >>> for position, noteName in ax.ticks():
        ...            print(str(position) + ' ' + noteName)
        0 C
        2 D
        3 E♭
        4 E
        5 F
        7 G
        9 A
        10 B♭
        11 B

        >>> ax.blankLabelUnused = True
        >>> ax.hideUnused = False
        >>> for position, noteName in ax.ticks():
        ...            print(str(position) + ' ' + noteName)
        0 C
        1
        2 D
        3 E♭
        4 E
        5 F
        6
        7 G
        8
        9 A
        10 B♭
        11 B

        `.showEnharmonic` will change here:

        >>> s.append(note.Note('A#4'))
        >>> s.append(note.Note('G#4'))
        >>> s.append(note.Note('A-4'))
        >>> s.append(note.Note('A-4'))
        >>> for position, noteName in ax.ticks():
        ...            print(str(position) + ' ' + noteName)
        0 C
        1
        2 D
        3 E♭
        4 E
        5 F
        6
        7 G
        8 G♯/A♭
        9 A
        10 A♯/B♭
        11 B

        Make sure that Ab shows since there are two of them and only one G#

        >>> ax.showEnharmonic = False
        >>> for position, noteName in ax.ticks():
        ...            print(str(position) + ' ' + noteName)
        0 C
        1
        2 D
        3 E♭
        4 E
        5 F
        6
        7 G
        8 A♭
        9 A
        10 B♭
        11 B

        OMIT_FROM_DOCS

        TODO: this ultimately needs to look at key signature/key to determine
            defaults for undefined notes where blankLabelUnused is False.
        r   r   r   r6   s    r+   ra   zPitchClassAxis.ticks  s    b $$V\::r-   rf   )rg   z
int | None)r0   rh   ri   rj   r5   r   rk   r,   rK   ra   r   r   s   @r+   r   r     s)    
 !L"DJD
q;r-   r   c                  2    e Zd ZU dZdZdZded<   d Zd	dZy)
PitchSpaceAxiszB
    Axis subclass for dealing with PitchSpace (MIDI numbers)
    rt   )
pitchSpacer
   
pitchspacepsr#   r   c                H    t        |d      r|j                  j                  S y r   )r   r
   r   rH   s      r+   rK   z PitchSpaceAxis.extractOneElementK  s    1g77:: r-   c                &    | j                  dd      S )u  
        >>> ax = graph.axis.PitchSpaceAxis()
        >>> ax.hideUnused = False
        >>> ax.blankLabelUnused = False
        >>> ax.minValue = 20
        >>> ax.maxValue = 24
        >>> for ps, label in ax.ticks():
        ...     print(str(ps) + ' ' + label)
        20 G♯0
        21 A
        22 B♭
        23 B
        24 C1

        >>> ax.showOctaves = False
        >>> for ps, label in ax.ticks():
        ...     print(str(ps) + ' ' + label)
        20 G♯
        21 A
        22 B♭
        23 B
        24 C

        >>> ax.showOctaves = True
        >>> for ps, label in ax.ticks():
        ...     print(str(ps) + ' ' + label)
        20 G♯0
        21 A0
        22 B♭0
        23 B0
        24 C1

        >>> ax.minValue = 60
        >>> ax.maxValue = 72
        >>> [x for x, y in ax.ticks()]
        [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72]

        >>> bach = corpus.parse('bwv66.6')
        >>> plotS = graph.plot.PlotStream(bach.parts[-1])
        >>> ax = graph.axis.PitchSpaceAxis(plotS)
        >>> ax.hideUnused = False
        >>> ax.minValue = 36
        >>> ax.maxValue = 100
        >>> ticks = ax.ticks()
        >>> ticks[0]  # blank because no note 36 in data
        (36, '')
        >>> ticks[21]
        (57, 'A')
        nameWithOctaver   r   )r*   dataMindataMaxs      r+   ra   zPitchSpaceAxis.ticksO  s    d $$%5t<<r-   N)$   d   )	r0   rh   ri   rj   r5   r   rk   rK   ra   rG   r-   r+   r   r   D  s"     L"MJM2=r-   r   c                  >     e Zd ZU dZdZdZded<   d fd	Zd Z xZ	S )	PitchSpaceOctaveAxiszF
    An axis similar to pitch classes, but just shows the octaves
    Octave)octaveoctavesr#   r   c                4    t         |   ||       d| _        y )NC2)rx   r,   startNameWithOctavery   s      r+   r,   zPitchSpaceOctaveAxis.__init__  s    *#' r-   c                   g }t        j                  | j                        }|j                  | j                  k  rx|j                  | j
                  k\  r0|j                  t        |j                        |j                  f       |xj                  dz  c_	        |j                  | j                  k  rx| j                  |      }|S )a  
        This class does not currently take into account whether the octaves themselves
        are found in the Stream.

        >>> ax = graph.axis.PitchSpaceOctaveAxis()
        >>> ax.minValue = 36
        >>> ax.maxValue = 100
        >>> ax.ticks()
        [(36, 'C2'), (48, 'C3'), (60, 'C4'), (72, 'C5'), (84, 'C6'), (96, 'C7')]

        >>> ax.startNameWithOctave = 'A2'
        >>> ax.ticks()
        [(45, 'A2'), (57, 'A3'), (69, 'A4'), (81, 'A5'), (93, 'A6')]
        r   )r
   rt   r   r   r   r   rX   rT   r   r   r|   )r*   ra   currentPitchs      r+   ra   zPitchSpaceOctaveAxis.ticks  s     {{4#;#;<oo.$--/c,//2L4O4OPQ1$ oo. ++E2r-   rf   )
r0   rh   ri   rj   r5   r   rk   r,   ra   r   r   s   @r+   r   r     s#     L"7J7(r-   r   c                  J     e Zd ZU dZddiZded<   dZdZded	<   d fd
	Z xZ	S )PositionAxisz2
    Axis subclass for dealing with Positions
    graceNoteQLz
            length to substitute a grace note or other Zero-length element for.
            Default is the length of a 64th note (1/16 of a QL)
        r   r   Position)position	positionsr#   r   c                4    t         |   ||       d| _        y )Ng      ?)rx   r,   r   ry   s      r+   r,   zPositionAxis.__init__  s    * r-   rf   )
r0   rh   ri   rj   r   rk   r5   r   r,   r   r   s   @r+   r   r     s;     	 !I~  L";J;! !r-   r   c                       e Zd ZU dZdddddddZd	ed
<   dZdZded<   d fd	Zd Z	e
d        Zej                  d        ZddZd Zd Zd ZddZ xZS )
OffsetAxisz0
    Axis subclass for dealing with Offsets
    z
            bool or None for whether offsets (False) or measure numbers (True) should be used
            in the case of an offset access.  Default, None, meaning to check whether
            the stream has measures first.
            z
            If measures are not used then this number is used to create the number
            of steps between an axis tick.  Currently the default is 10, but it
            might become a function of the length of the stream eventually.
            z
            If True then only the first and last values will be used to
            create ticks for measures.  Default False.
            zHThe lowest starting position (as an offset).  Will be set automatically.zGThe highest ending position (as an offset).  Will be set automatically.a  
            When plotting measures, will limit the number of ticks given to at most
            this number.  Note that since all double/final/heavy bars are show, this number
            may be exceeded if there are more that this number of double bars.  Default: 20.
            )useMeasuresoffsetStepSizeminMaxMeasureOnlyr   r   mostMeasureTicksToShowr   r   Offset)offsetmeasureoffsetsmeasurestimer#   r   c                ^    t         |   ||       d | _        d| _        d| _        d| _        y )NrS      F)rx   r,   r   r   r   r   ry   s      r+   r,   zOffsetAxis.__init__  s2    * &(#!&r-   c                8    |j                  | j                        S r9   )getOffsetInHierarchyr   rH   s      r+   rK   zOffsetAxis.extractOneElement  s    %%dkk22r-   c                v    | j                   | j                   S | j                  }|| j                         }|ryy)a  
        Return an axis label for measure or offset, depending on if measures are available.

        >>> a = graph.axis.OffsetAxis()
        >>> a.label
        'Offset'
        >>> a.useMeasures = True
        >>> a.label
        'Measure Number'
        zMeasure Numberr   )r(   r   setUseMeasuresFromOffsetMap)r*   r   s     r+   r7   zOffsetAxis.label!  s@     ;;";;&&::<K#r-   c                    || _         y r9   r:   r;   s     r+   r7   zOffsetAxis.label9  r=   r-   c                    	 | j                   j                  | _        | j                   j                  | _        y # t
        $ r% d| _        |rt        |      | _        Y y d| _        Y y w xY w)Nr   rS   )r   lowestOffsetr   highestTimer   AttributeErrorrN   rO   s     r+   rQ   z OffsetAxis.setBoundariesFromData=  sT    	# KK44DM KK33DM 	#DM #F "	#s   69 "A'A'&A'c                   | j                         }| j                  |       | j                  r'| j                  | j                  | j
                  |      S g }t        t        j                  | j                              }t        t        j                  | j
                              }t        ||dz   | j                        D ]  }|j                  |t        |      f         |S )a
  
        Get offset or measure ticks

        >>> bach = corpus.parse('bach/bwv281.xml')
        >>> plotS = graph.plot.PlotStream(bach)
        >>> ax = graph.axis.OffsetAxis(plotS)
        >>> ax.setBoundariesFromData()
        >>> ax.ticks()  # on whole score, showing anacrusis spacing
        [(0.0, '0'), (1.0, '1'), (5.0, '2'), (9.0, '3'), (13.0, '4'), (17.0, '5'),
         (21.0, '6'), (25.0, '7'), (29.0, '8')]

        We can reduce the number of ticks shown:

        >>> ax.mostMeasureTicksToShow = 4
        >>> ax.ticks()
        [(0.0, '0'), (9.0, '3'), (21.0, '6'), (29.0, '8')]

        We can also plot on a part:

        >>> soprano = bach.parts.first()
        >>> plotSoprano = graph.plot.PlotStream(soprano)
        >>> ax = graph.axis.OffsetAxis(plotSoprano)
        >>> ax.setBoundariesFromData()
        >>> ax.ticks()  # on whole score, showing anacrusis spacing
        [(0.0, '0'), (1.0, '1'), (5.0, '2'), (9.0, '3'), (13.0, '4'), (17.0, '5'),
         (21.0, '6'), (25.0, '7'), (29.0, '8')]

        Now we will show just the first and last measure:

        >>> ax.minMaxMeasureOnly = True
        >>> ax.ticks()
        [(0.0, '0'), (29.0, '8')]

        Only show ticks between minValue and maxValue (in offsets):

        >>> ax.minMaxMeasureOnly = False
        >>> ax.minValue = 8
        >>> ax.maxValue = 12
        >>> ax.ticks()
        [(9.0, '3')]

        Double bars and other heavy bars always show up.
        (Let's get a new axis object to see.)

        >>> ax = graph.axis.OffsetAxis(plotSoprano)
        >>> ax.setBoundariesFromData()
        >>> ax.mostMeasureTicksToShow = 4
        >>> ax.ticks()
        [(0.0, '0'), (9.0, '3'), (21.0, '6'), (29.0, '8')]
        >>> m5 = soprano.getElementsByClass(stream.Measure)[5]
        >>> m5.number
        5
        >>> m5.rightBarline = bar.Barline('double')
        >>> ax.ticks()
        [(0.0, '0'), (13.0, '4'), (17.0, '5'), (29.0, '8')]

        Future improvements might make the spacing around the double bars
        a bit better.  It'd be nice to see measure 2 or 3 ticked rather
        than measure 4.

        On a raw collection of notes with no measures, offsets are used:

        >>> n = note.Note('a')
        >>> s = stream.Stream()
        >>> s.repeatAppend(n, 20)
        >>> plotS = graph.plot.PlotStream(s)
        >>> ax = graph.axis.OffsetAxis(plotS)
        >>> ax.setBoundariesFromData()
        >>> ax.ticks()
        [(0, '0'), (10, '10'), (20, '20')]

        The space between offsets is configured by `.offsetStepSize`.  At
        present mostMeasureTicksToShow to does affect streams without measures.

        >>> ax.offsetStepSize = 5
        >>> ax.ticks()
        [(0, '0'), (5, '5'), (10, '10'), (15, '15'), (20, '20')]
        r   )getOffsetMapr   r   _measureTicksr   r   rT   rU   floorceilrW   r   rX   r&   )r*   	offsetMapra   oMinoMaxr   s         r+   ra   zOffsetAxis.ticksH  s    ^ %%'	((3%%dmmT]]INN Etzz$--01Dtyy/0D44+>+>?aQ[) @ Lr-   c                l   g g t        j                               }|D ]"  }||cxk  r|k  sn j                  |       $ | j                  rCdD ]<  }|   }|   d   j                  }|t        |      f}	|	vs,j                  |	       > S t               fd}
 |
d        |
t              dz
         t        dt              dz
        D ]K  }|   }|   d   }|j                  |j                  j                  t        j                  v sD |
|       M t        | j                  t              z
  dz   t                    }t        t              |z  d      }|}|t              dz
  k  r |
|       ||z  }|t              dz
  k  rj!                          S )zB
        helper method for ticks() just to pull out code.
        )r   r   r   c                    | v ry |    }|   d   }|j                   }|t        |      f}j                  |       j                  |        y Nr   )numberr&   rX   r   )	index_in_mNoToUser   foundMeasuremNumber	tickTuplemNoToUser   tickIndexesUsedra   s	        r+   add_tick_tuplez0OffsetAxis._measureTicks.<locals>.add_tick_tuple  s\    $7!"34(03&--#S\2	Y'##$56r-   r   )listkeysrX   r   r   r&   r   lenrW   rightBarlinetyper   strongBarlineTypesrM   r   rN   r   )r*   r   r   r   
sortedKeysr   r   r   r   r   r   	mapOffset
mapMeasuremaxMoreTicksToAddmNoStepSizer   r   ra   s      `           @@@r+   r   zOffsetAxis._measureTicks  s     )..*+
C#(( $  !!!!#F+A.55#S\2	E)LL+ Z M "eO	7 13x=1,- 1c(ma/0$QK	&y1!4
++7&3388C<R<RR"1% 1 !$D$?$?#oBV$VYZ$Z$'M!3c(m/@@!DKAc(ma''q![  c(ma'' JJLr-   c                H   | j                   }|i S |j                         rM|j                  t         j                        j	                         j                  t         j                  g      }|S |j                         r"|j                  t         j                  g      }|S i }|S )a  
        Find the first partlike object and get the measureOffsetMap from it, or an
        empty-dict if not.

        >>> b = corpus.parse('bwv66.6')
        >>> p = graph.plot.PlotStream(b)
        >>> ax = graph.axis.OffsetAxis(p, 'x')
        >>> om = ax.getOffsetMap()
        >>> om
        OrderedDict([(0.0, [<music21.stream.Measure 0 offset=0.0>]),
                     (1.0, [<music21.stream.Measure 1 offset=1.0>]),
                     (5.0, [<music21.stream.Measure 2 offset=5.0>]),
                     ...])

        Same if called on a single part:

        >>> p = graph.plot.PlotStream(b.parts[0])
        >>> ax = graph.axis.OffsetAxis(p, 'x')
        >>> om2 = ax.getOffsetMap()
        >>> om2
        OrderedDict([(0.0, [<music21.stream.Measure 0 offset=0.0>]),
                     (1.0, [<music21.stream.Measure 1 offset=1.0>]),
                     (5.0, [<music21.stream.Measure 2 offset=5.0>]),
                     ...])

        But empty if called on a single Measure ...

        >>> p = graph.plot.PlotStream(b.parts[0].getElementsByClass(stream.Measure)[2])
        >>> ax = graph.axis.OffsetAxis(p, 'x')
        >>> om3 = ax.getOffsetMap()
        >>> om3
        {}

        )r   hasPartLikeStreamsgetElementsByClassrD   firstmeasureOffsetMapMeasurehasMeasures)r*   r   r   s      r+   r   zOffsetAxis.getOffsetMap  s    F KK9I! --fmm<BBD**FNN+;<   ]]_**FNN+;<I  Ir-   c                    | j                   | j                   S || j                         }t        |      | _         | j                   S )ac  
        Given an offsetMap and `.useMeasures=None` return
        True or False based on whether the offsetMap or self.getOffsetMap() is
        non-empty.

        >>> b = corpus.parse('bwv66.6')
        >>> p = graph.plot.PlotStream(b)
        >>> ax = graph.axis.OffsetAxis(p, 'x')
        >>> print(ax.useMeasures)
        None
        >>> ax.setUseMeasuresFromOffsetMap()
        True

        Sets `.useMeasures` as a side effect:

        >>> ax.useMeasures
        True

        same as:

        >>> ax = graph.axis.OffsetAxis(p, 'x')
        >>> om = ax.getOffsetMap()
        >>> ax.setUseMeasuresFromOffsetMap(om)
        True

        If `.useMeasures` is set explicitly, then
        we return that

        >>> ax.useMeasures = False
        >>> ax.setUseMeasuresFromOffsetMap()
        False

        Returns False if the offsetMap is empty

        >>> p = graph.plot.PlotStream(b.parts[0].getElementsByClass(stream.Measure)[2])
        >>> axMeasure = graph.axis.OffsetAxis(p, 'x')
        >>> axMeasure.setUseMeasuresFromOffsetMap()
        False
        >>> axMeasure.useMeasures
        False
        )r   r   bool)r*   r   s     r+   r   z&OffsetAxis.setUseMeasuresFromOffsetMap   sI    T '###))+I	?r-   rf   r9   )r0   rh   ri   rj   r   rk   r5   r   r,   rK   rl   r7   rm   rQ   ra   r   r   r   r   r   s   @r+   r   r     s    

 _]##!I~ 0 L"VJV'3  . \\ 	#]~CJ2h/ r-   r   c                       e Zd ZU dZdddZded<   dZdZd	ed
<   d fd	Zd Z	d Z
d Zd Ze fd       Zej                   fd       Zd Z xZS )QuarterLengthAxisz7
    Axis subclass for dealing with QuarterLengths
    z
            bool or int for whether to scale numbers logarithmically.  Adds (log2) to the
            axis label if used.  If True (default) then log2 is assumed.  If an int, then
            log the int (say, 10) is used. instead.
        z
            If used then duration names replace numbers for ticks.
            If set, probably will want to change tickFontSize in the graph object
        )useLogScaleuseDurationNamesr   r   zQuarter Length)quarterLengthqlquarterlengths	durationsr   r#   r   c                B    t         |   ||       d| _        d| _        y )NTF)rx   r,   r  r  ry   s      r+   r,   zQuarterLengthAxis.__init__j  s"    * %r-   c                L    | j                  |j                  j                        S r9   )
dataFromQLr   r  rH   s      r+   rK   z#QuarterLengthAxis.extractOneElemento  s    qzz7788r-   c                    | j                   r| j                  |      }|S |dkD  rt        |      }|S | j                  }|S r   )r  remapQuarterLengthfloatr   )r*   r  r   s      r+   r  zQuarterLengthAxis.dataFromQLr  sM    ''+A
 	 !Vb	A    Ar-   c                   | j                   }|sg S | j                  j                  r|j                         }n|j                         }|j	                  | j                  j
                        }t        j                  |d      }g }t        |      D ]g  }| j                  |      }| j                  r t        j                  |      j                  }nt        t        |d            }|j!                  ||f       i |S )aA  
        Get ticks for quarterLength.

        If `remap` is `True` (the default), the `remapQuarterLength()`
        method will be used to scale displayed quarter lengths
        by log base 2.

        Note that mix and max do nothing, but must be included
        in order to set the tick style.

        >>> s = stream.Stream()
        >>> for t in ['32nd', '16th', 'eighth', 'quarter', 'half']:
        ...     n = note.Note()
        ...     n.duration.type = t
        ...     s.append(n)

        >>> plotS = graph.plot.PlotStream(s)
        >>> ax = graph.axis.QuarterLengthAxis(plotS)
        >>> ax.ticks()
        [(-3.0, '0.12'), (-2.0, '0.25'), (-1.0, '0.5'), (0.0, '1.0'), (1.0, '2.0')]

        >>> ax.useLogScale = False
        >>> ax.ticks()
        [(0.125, '0.12'), (0.25, '0.25'), (0.5, '0.5'), (1.0, '1.0'), (2.0, '2.0')]
        >>> ax.useDurationNames = True
        >>> ax.ticks()
        [(0.125, '32nd'), (0.25, '16th'), (0.5, 'Eighth'), (1.0, 'Quarter'), (2.0, 'Half')]

        >>> nGrace = note.Note()
        >>> nGrace.getGrace(inPlace=True)
        >>> s.append(nGrace)
        >>> plotS = graph.plot.PlotStream(s)
        >>> ax = graph.axis.QuarterLengthAxis(plotS)
        >>> ax.ticks()[0]
        (-4.0, '0.0')

        >>> ax.useLogScale = False
        >>> ax.ticks()[0]
        (0.0625, '0.0')
        r  r   )r   r)   recurseiterr	  classFilterListelementAnalysisattributeCountsortedr  r  r   DurationfullNamer&   roundrX   )r*   r   sSrcmappingra   r  r   r7   s           r+   ra   zQuarterLengthAxis.ticks{  s    T KKI[[  99;D668D&&t{{'B'BC!00G/B#A$$ ))"-66E"aL)LL!U$ " r-   c                `    | j                   du ry| j                   du ryd| j                   ddS )a  
        Returns a TeX formatted tag to the axis label depending on whether
        the scale is logarithmic or not.  Checks `.useLogScale`

        >>> a = graph.axis.QuarterLengthAxis()
        >>> a.useLogScale
        True
        >>> a.labelLogTag()
        ' ($log_2$)'

        >>> a.useLogScale = False
        >>> a.labelLogTag()
        ''

        >>> a.useLogScale = 10
        >>> a.labelLogTag()
        ' ($log_10$)'
        Fr   Tz
 ($log_2$)z ($log_dz$))r  r6   s    r+   labelLogTagzQuarterLengthAxis.labelLogTag  s>    & u$%T--a033r-   c                :    t         |   | j                         z   S r9   )rx   r7   r,  )r*   r/   s    r+   r7   zQuarterLengthAxis.label  s    w}t//111r-   c                $    |t               _        y r9   )rx   r7   )r*   r<   r/   s     r+   r7   zQuarterLengthAxis.label  s    r-   c                    |dk(  r| j                   }	 t        j                  t        |            S # t        $ r t        d|       w xY w)z
        Remap a quarter length as its log2.  Essentially it's
        just math.log2(x), but x=0 is replaced with self.graceNoteQL.
        r   zcannot take log of x value: )r   rU   log2r  
ValueErrorr   )r*   r   s     r+   r  z$QuarterLengthAxis.remapQuarterLength  sS    
 6  A	E99U1X&& 	E #?s!CDD	Es	   1 A	rf   )r0   rh   ri   rj   r   rk   r5   r   r,   rK   r  ra   r,  rl   r7   rm   r  r   r   s   @r+   r  r  R  s    

!I~ 
 $L#%J %&
9>@44 2 2 \\ Er-   r  c                  R     e Zd ZU dZddiZded<   dZded<   d	d
ef fd	Zd Z	 xZ
S )OffsetEndAxiszI
    An Axis that gives beginning and ending values for each element
    noteSpacingzt
            amount in QL to leave blank between untied notes.
            (default = self.graceNoteQL)
            r   r   )	offsetEnd	timespanstimespanr#   r   Nr   c                j    t         |   ||       || _        |t        k(  r| j                  | _        y y r9   )rx   r,   r4  USE_GRACE_NOTE_SPACINGr   )r*   r)   r   r4  r/   s       r+   r,   zOffsetEndAxis.__init__  s6    *&00#//D 1r-   c                z   t        |j                  | j                              }t        |j                  j                        }|| j
                  k  r| j
                  }||fS || j
                  dz  kD  rDt        |d      r)|j                  |j                  j                  dv r	 ||fS || j
                  z  }||fS )Nr   tie)startcontinue)	r  r   r   r   r  r4  r   r;  r   )r*   rI   rJ   offuseQLs        r+   rK   zOffsetEndAxis.extractOneElement  s    A**4;;78ajj../4###$$E U| T%%))q% QUU%6155::I^;^ U| )))U|r-   )r0   rh   ri   rj   r   rk   r   r9  r,   rK   r   r   s   @r+   r3  r3    s?     	 !I~  #IJH"S>T 0r-   r3  c                  >     e Zd ZU dZdZdZded<   d fd	Zd Z xZ	S )	DynamicsAxisz1
    Axis subclass for dealing with Dynamics
    Dynamic)dynamicr	   volumer#   r   c                    |)d| _         t        t        j                        dz
  | _        y t
        |   |       t        | j                         | _         t        | j                        | _        y )Nr   r   )r   r   r	   
shortNamesr   rx   rQ   rT   )r*   rP   r/   s     r+   rQ   z"DynamicsAxis.setBoundariesFromData  sS    >DM 3 34q8DMG)&1.DM.DMr-   c                    g }| j                   | j                          t        | j                   | j                  dz         D ]*  }|j	                  |dt
        j                  |    df       , |S )ak  
        Utility method to get ticks in dynamic values:

        >>> ax = graph.axis.DynamicsAxis()
        >>> ax.ticks()
        [(0, '$pppppp$'), (1, '$ppppp$'), (2, '$pppp$'), (3, '$ppp$'), (4, '$pp$'),
         (5, '$p$'), (6, '$mp$'), (7, '$mf$'), (8, '$f$'), (9, '$fp$'), (10, '$sf$'),
         (11, '$ff$'), (12, '$fff$'), (13, '$ffff$'), (14, '$fffff$'), (15, '$ffffff$')]

        A minimum and maximum dynamic index can be specified as minValue and maxValue

        >>> ax.minValue = 3
        >>> ax.maxValue = 6
        >>> ax.ticks()
        [(3, '$ppp$'), (4, '$pp$'), (5, '$p$'), (6, '$mp$')]

        r   $)r   rQ   rW   r   rX   r	   rF  )r*   ra   r   s      r+   ra   zDynamicsAxis.ticks  sj    $ == &&(t}}dmma&78ALL!q!4!4Q!7 8:;< 9 r-   r9   )
r0   rh   ri   rj   r5   r   rk   rQ   ra   r   r   s   @r+   rA  rA    s#     L"CJC/r-   rA  c                  P     e Zd ZU dZddiZded<   dZdZded	<   d fd
	Zd Z	 xZ
S )CountingAxisa  
    Axis subclass for counting data in another Axis.

    Used for histograms, weighted scatter, etc.

    >>> bach = corpus.parse('bwv66.6')
    >>> plotS = graph.plot.PlotStream(bach)
    >>> plotS.axisX = graph.axis.PitchSpaceAxis(plotS, 'x')
    >>> plotS.axisY = graph.axis.CountingAxis(plotS)
    >>> plotS.doneAction = None
    >>> plotS.run()
    >>> plotS.data
    [(42.0, 1, {}), (45.0, 1, {}), (46.0, 1, {}), (47.0, 5, {}), (49.0, 6, {}), ...]
    	countAxeszf
            a string or tuple of strings representing an axis or axes to use in counting
            r   r   Count)countquantity	frequencycountingr#   r   c                4    t         |   ||       d| _        y rf   )rx   r,   rK  ry   s      r+   r,   zCountingAxis.__init__R  s    *r-   c                     j                   }|g S ddlm}  j                  }t	        j
                  |      s|f}t         fd|D              } j                   j                     } || }|j                  D cg c]
  } ||       }}i }	|j                  D ]>  }
 ||
      }|
d   }t        |t              s!||	v r|	|   j                  |       :||	|<   @ t        j                  |      }g }|D ]r  }dgt        |      dz   z  }t        |      dkD  r|D ]
  }||   ||<    n|||d   <   ||   ||<   |	j!                  |i       }|j#                  t        |      |fz          t t%        |      |_	        |j                  S c c}w )zS
        Replace client.data with a list that only includes each key once.
        Nr   )
itemgetterc              3  <   K   | ]  }j                   |     y wr9   )r   ).0r   r*   s     r+   	<genexpr>z/CountingAxis.postProcessData.<locals>.<genexpr>c  s     Qy8D,,X6ys   r   r   )r)   operatorrS  rK  r   
isIterabletupler   r   datar%   dictupdatecollectionsCounterr   getrX   r$  )r*   r)   rS  rK  axesIndices	thisIndexselector
innerTuplerelevantDatatupleFormatDict	dataPoint	dataIndexrJ   counternewClientData
counterKey	innerListdependentIndexs   `                 r+   re   zCountingAxis.postProcessDataV  s    >I'NN	  +"IQyQQ$$T]]3	{+?E{{K{,{K I +I"2Jj$/O+	*11*=-7	* % %%l3!J#k"2Q"67I;!#&1N0:>0JIn- '2 -7	+a.)#*:#6Ii (,,Z<J  y!1ZM!AB " ]+{{; Ls   <F)Nr   )r0   rh   ri   rj   r   rk   r5   r   r,   re   r   r   s   @r+   rJ  rJ  :  s;     	 !I~  L"PJP-r-   rJ  c                      e Zd Zd Zy)Testc           
     &   d }ddl m} ddlm} |j	                  d      } ||      }d |_        t        |d      |_        ||j                  _        |j                          | j                  |j                  ddi fddd	d
ifddi fddd	d
ifg       y )Nc                d    | j                   j                  d|d<   | j                   j                  S )Nredcolor)r
   
accidentaldiatonicNoteNum)rI   rJ   s     r+   countingAxisFormatterz:Test.testCountingAxisFormat.<locals>.countingAxisFormatter  s,    ww!!-&+
7#77***r-   r   )	Histogram)	converterz$tinynotation: 4/4 C4 D E F C D# E F#r   r   r   rr  rq        )music21.graph.plotrv  music21rw  parse
doneActionr   axisXrK   runassertEqualrZ  )r*   ru  rv  rw  r   hists         r+   testCountingAxisFormatzTest.testCountingAxisFormat  s    	+
 	1%OOBC|$_
'<

$
a*q!gu-=&>a*q!gu-=&>@	Ar-   N)r0   rh   ri   r  rG   r-   r+   rn  rn    s    Ar-   rn  __main__)+rj   
__future__r   r]  rU   r   typingtunittestmusic21.graph.utilitiesr   r   r{  r   r   r   r	   r
   r   r   music21.analysisr   r"  r   TYPE_CHECKINGr   r9  ProtoM21Objectr   ro   r   r   r   r   r   r  r3  rA  rJ  TestCasern  r0   mainTestrG   r-   r+   <module>r     s(   #   	   L        8 * ??  u7!! utR RjE;Y E;P==Y ==@!> !@!4 !&X  X v
VE VErJ D(4 (ZI4 IZA8 A, zGT r-   