
    rh                    T   S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSK	J
r
Jr  SSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  \R0                  (       a  SSKJr  Sr " S S\R6                  5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r  " S S\5      r! " S S\!5      r" " S S\!5      r# " S  S!\"5      r$ " S" S#\5      r% " S$ S%\5      r& " S& S'\RN                  5      r(\)S(:X  a  SSKr\RT                  " \(5        gg))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                      \ rS rSr% SrSSSSSS.rS	\S
'   SrSSSS.rSr	S\S'   S S jr
S r\S 5       r\R                  S 5       r\S 5       r\R                  S 5       r\S 5       rS!S jrS rS rS"S jrSrg)#Axis.   z
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                    [        U[        5      (       a  [        S5      eS U l        S U l        Xl        X l        S U l        S U l        g )Nz,Client must be a PlotStream, Stream, or None)	
isinstancestrr   _client_labelclientr   r   r   )selfr*   r   s      L/home/james-whalen/.local/lib/python3.13/site-packages/music21/graph/axis.py__init__Axis.__init__\   sA    fc"" !OPP     c                v    U R                   nUb  UR                  R                  nOSnSU R                   SU 3$ )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,   _reprInternalAxis._reprInternalh   s=    . KK=--J&JDMM?*ZL99r/   c                L    U R                   b  U R                   $ U R                  $ )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,   label
Axis.label   s$     ;;";;$$$r/   c                    Xl         g Nr)   r+   values     r,   r:   r;          r/   c                B    [         R                  " U R                  5      $ )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(   r9   s    r,   r*   Axis.client   s     ##DLL11r/   c                :    [         R                  " U5      U l        g r=   )r   wrapWeakrefr(   )r+   referents     r,   r*   rD      s    ))(3r/   c                |    U R                   nUc  g[        U[        R                  5      (       a  U$ UR                  $ )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+   r3   s     r,   r   Axis.stream   s5     KK96==))H;;r/   c                    g)z
Override in subclasses
r    r+   n
formatDicts      r,   extractOneElementAxis.extractOneElement   s     r/   c                    U R                   c  U(       a  [        U5      U l         U R                  c  U(       a  [        U5      U l        ggg)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,   setBoundariesFromDataAxis.setBoundariesFromData   s:    : == VKDM== VKDM &, r/   c                   U R                   nU R                  nUc  SnUc  SnX!-
  nUS:X  a  SnO![        [        R                  " X!-
  5      5      nSU-  nUS:  a  X5-  S::  a  [        US-  5      n[        X-  5      S-
  U-  nUS:  a  US:  a  Sn[        X%-  5      S-   U-  n[        XgU5      n/ n	U H  n
U	R                  U
[        U
5      45        M!     U	$ )a  
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,   ri   
Axis.ticks   s    6 }}}}<D<D[
?M

4; 78M-'	q=j4:IN+I$*+a/9<
>daiJ)*Q.);	jY7GLL'3w<01 r/   c                    g)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.
NrM   )r+   dataLists     r,   postProcessDataAxis.postProcessData  s     	r/   )r(   r)   r   r*   r   r   Nr   )rO   znote.GeneralNoterP   zdict[str, t.Any]returnzt.Anyr=   )r2   
__module____qualname____firstlineno____doc__r   __annotations__r8   r   r   r-   r5   propertyr:   setterr*   r   rQ   rX   ri   rn   __static_attributes__rM   r/   r,   r   r   .   s     T"3!!I~ !F L*K"HJH
:> % %  \\  2 2 ]]4 4    (D7rr/   r   c                  v   ^  \ rS rSr% SrSSSSS.rS\S	'   S
rSrS\S'   SU 4S jjr	\
SS j5       rS rSrU =r$ )	PitchAxisi(  z(
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U ]  X5        SU l        SU l        SU l        SU l        g )NfewT)superr-   r   r|   r}   r~   r+   r*   r   r1   s      r,   r-   PitchAxis.__init__?  s/    * " $r/   c                Z    / nU  H"  u  p#[        U5      nUR                  X#45        M$     U$ )un  
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   r`   )ri   postr@   r:   s       r,   makePitchLabelsUnicode PitchAxis.makePitchLabelsUnicodeF  s4    0 !LE,U3EKK' " r/   c                   U R                   nUc  0 nO[        R                  " X15      n/ n0 n[        5       nS nS n	[	        [        U R                  5      [        U R                  5      S-   5       GH  n
[        R                  " 5       n[        XU
5        / nU HI  nX;  a"  [        [        R                  " U5      U5      Xm'   Xm   U
:X  d  M4  UR                  XM   U45        MK     U R                  (       a  UR                  U	S9  OUR                  US9  SnU(       d3  U R                  (       a  M  U R                   (       d  [        X5      nOVSnOSU R                  (       d	  US   S   nO9/ nU H   u  nnUR                  [#        U5      5        M"     SR%                  U5      nU R&                  S	L a  [(        R*                  " S
SU5      nOoU R&                  S:X  a_  [(        R,                  " S
U5      nU(       aA  UR/                  S5      nUU;   a  [(        R*                  " S
SU5      nOUR1                  U5        UR                  X45        GM     U R3                  U5      nU$ )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                T    U u  pUR                  S5      (       a  SUSS -   nSU-  U4$ )z
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,   weightedSortHelper6PitchAxis._pitchTickHelper.<locals>.weightedSortHelperx  s<     !"F##C(()AB-/	K++r/   c                L    U u  pUR                  S5      (       a  SUSS  -   nX4$ )Nr   r   r   r   r   s      r,   unweightedSortHelper8PitchAxis._pitchTickHelper.<locals>.unweightedSortHelper  s4     !F##C(()AB-/	&&r/   r   )key r   /Fz\dr   )r   r   pitchAttributeCountsetr_   r\   r   r   r
   r   setattrgetattrr`   r|   sortr~   r}   r   joinr   resubsearchgroupaddr   )r+   attributeCounterattributeCompares	nameCountri   
helperDictoctavesSeenr   r   ipweightsr   r:   r   unused_weightnamematchOctaveoctaveMatchs                       r,   _pitchTickHelperPitchAxis._pitchTickHelperd  s    KK9I%99!NI
e
	,	' s4==)3t}}+=+ABAAA+G ( '.ekk#.>@P&QJO?a'NNINC#89 ! ""!56!34E??,,#A8EE((
1+2'M4JJ7=> ,35(ub%0!!U* iiu5"-"3"3A"6K"k1 "ub% 8#4LL!$] C^ ++E2r/   )r}   r~   r|   r   rp   )ri   list[tuple[t.Any, str]]rq   r   )r2   rr   rs   rt   ru   r   rv   r8   r   r-   staticmethodr   r   ry   __classcell__r1   s   @r,   r{   r{   (  s`    !I~   L"4J4  :V Vr/   r{   c                  T   ^  \ rS rSr% SrSrSrS\S'   SU 4S jjrSS jr	S	 r
S
rU =r$ )PitchClassAxisi  zf
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                N   > SU l         [        TU ]	  X5        SU l        SU l        g )NFr      )r   r   r-   r   r   r   s      r,   r-   PitchClassAxis.__init__  s&     *r/   c                R    [        US5      (       a  UR                  R                  $ g Nr
   )hasattrr
   r   rN   s      r,   rQ    PitchClassAxis.extractOneElement  s"    1g77%%%r/   c                &    U R                  SS5      $ )u9  
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   r9   s    r,   ri   PitchClassAxis.ticks  s    f $$V\::r/   )r   r   r   rp   )rq   z
int | None)r2   rr   rs   rt   ru   r8   r   rv   r-   rQ   ri   ry   r   r   s   @r,   r   r     s/    
 !L"DJD
s; s;r/   r   c                  <    \ rS rSr% SrSrSrS\S'   S rSS jr	S	r
g
)PitchSpaceAxisiG  z:
Axis subclass for dealing with PitchSpace (MIDI numbers)
r   )
pitchSpacer
   
pitchspacepsr$   r   c                R    [        US5      (       a  UR                  R                  $ g r   )r   r
   r   rN   s      r,   rQ    PitchSpaceAxis.extractOneElementN  s"    1g77:: r/   c                &    U R                  SS5      $ )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,   ri   PitchSpaceAxis.ticksR  s    d $$%5t<<r/   rM   N)$   d   )r2   rr   rs   rt   ru   r8   r   rv   rQ   ri   ry   rM   r/   r,   r   r   G  s"     L"MJM2=r/   r   c                  J   ^  \ rS rSr% SrSrSrS\S'   S
U 4S jjrS r	S	r
U =r$ )PitchSpaceOctaveAxisi  z>
An axis similar to pitch classes, but just shows the octaves
Octave)octaveoctavesr$   r   c                2   > [         TU ]  X5        SU l        g )NC2)r   r-   startNameWithOctaver   s      r,   r-   PitchSpaceOctaveAxis.__init__  s    *#' r/   c                   / n[         R                  " U R                  5      nUR                  U R                  ::  a{  UR                  U R
                  :  a0  UR                  [        UR                  5      UR                  45        U=R                  S-  sl	        UR                  U R                  ::  a  M{  U R                  U5      nU$ )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
   r   r   r   r   r   r`   r\   r   r   r   )r+   ri   currentPitchs      r,   ri   PitchSpaceOctaveAxis.ticks  s     {{4#;#;<oo.$--/c,//2L4O4OPQ1$ oo. ++E2r/   )r   rp   )r2   rr   rs   rt   ru   r8   r   rv   r-   ri   ry   r   r   s   @r,   r   r     s(     L"7J7( r/   r   c                  V   ^  \ rS rSr% SrSS0rS\S'   SrSrS	\S
'   SU 4S jjr	Sr
U =r$ )PositionAxisi  z*
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                2   > [         TU ]  X5        SU l        g )Ng      ?)r   r-   r   r   s      r,   r-   PositionAxis.__init__  s    * r/   )r   rp   )r2   rr   rs   rt   ru   r   rv   r8   r   r-   ry   r   r   s   @r,   r   r     s;     	 !I~  L";J;! !r/   r   c                     ^  \ rS rSr% SrSSSSSSS	.rS
\S'   SrSrS\S'   SU 4S jjr	S r
\S 5       r\R                  S 5       rSS jrS rS rS rSS jrSrU =r$ )
OffsetAxisi  z(
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U ]  X5        S U l        SU l        SU l        SU l        g )Nr[      F)r   r-   r   r   r   r   r   s      r,   r-   OffsetAxis.__init__  s0    * &(#!&r/   c                8    UR                  U R                  5      $ r=   )getOffsetInHierarchyr   rN   s      r,   rQ   OffsetAxis.extractOneElement!  s    %%dkk22r/   c                    U R                   b  U R                   $ U R                  nUc  U R                  5       nU(       a  gg)z
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,   r:   OffsetAxis.label$  s@     ;;";;&&::<K#r/   c                    Xl         g r=   r>   r?   s     r,   r:   r   <  rA   r/   c                     U R                   R                  U l        U R                   R                  U l        g ! [
         a*    SU l        U(       a  [        U5      U l         g SU l         g f = f)Nr   r[   )r   lowestOffsetr   highestTimer   AttributeErrorrU   rV   s     r,   rX    OffsetAxis.setBoundariesFromData@  sT    	# KK44DM KK33DM 	#DM #F "	#s   69 (A-#A-,A-c                   U R                  5       nU R                  U5        U R                  (       a'  U R                  U R                  U R
                  U5      $ / n[        [        R                  " U R                  5      5      n[        [        R                  " U R
                  5      5      n[        X4S-   U R                  5       H  nUR                  U[        U5      45        M!     U$ )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   r\   r]   floorceilr_   r   r`   r'   )r+   	offsetMapri   oMinoMaxr   s         r,   ri   OffsetAxis.ticksK  s    d %%'	((3%%dmmT]]INN Etzz$--01Dtyy/0D44+>+>?aQ[) @ Lr/   c                  ^^^^ / m/ m[        TR                  5       5      nU H&  nXs=::  a  U::  d  M  O  M  TR                  U5        M(     U R                  (       aH  S H@  nTU   nTU   S   R                  nU[        U5      4n	U	T;  d  M/  TR                  U	5        MB     T$ [        5       mUUUU4S jn
U
" S5        U
" [        T5      S-
  5        [        S[        T5      S-
  5       HQ  nTU   nTU   S   nUR                  c  M  UR                  R                  [        R                  ;   d  MI  U
" U5        MS     [        U R                  [        T5      -
  S-   [        T5      5      n[        [        T5      U-  S5      nUnU[        T5      S-
  :  a   U
" U5        Xn-  nU[        T5      S-
  :  a  M   TR!                  5         T$ )z2
helper method for ticks() just to pull out code.
)r   r   r   c                   > U T;   a  g TU    nTU   S   nUR                   nU[        U5      4nTR                  U5        TR                  U 5        g Nr   )numberr'   r`   r   )	index_in_mNoToUser   foundMeasuremNumber	tickTuplemNoToUser  tickIndexesUsedri   s	        r,   add_tick_tuple0OffsetAxis._measureTicks.<locals>.add_tick_tuple  s\    $7!"34(03&--#S\2	Y'##$56r/   r   )listkeysr`   r   r  r'   r   lenr_   rightBarlinetyper   strongBarlineTypesrT   r   rU   r   )r+   r   r   r  
sortedKeysr   r   r   r  r  r  	mapOffset
mapMeasuremaxMoreTicksToAddmNoStepSizer  r  ri   s      `           @@@r,   r  OffsetAxis._measureTicks  s     )..*+
C((( $  !!!!#F+A.55#S\2	E)LL+ Z M "eO	7 	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                ^   U R                   nUc  0 $ UR                  5       (       aM  UR                  [         R                  5      R	                  5       R                  [         R                  /5      nU$ UR                  5       (       a"  UR                  [         R                  /5      nU$ 0 nU$ )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getElementsByClassrI   firstmeasureOffsetMapMeasurehasMeasures)r+   r   r  s      r,   r  OffsetAxis.getOffsetMap  s    F KK9I!! --fmm<BBD**FNN+;<   ]]__**FNN+;<I  Ir/   c                    U R                   b  U R                   $ Uc  U R                  5       n[        U5      U l         U R                   $ )ak  
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   &OffsetAxis.setUseMeasuresFromOffsetMap&  sI    T '###))+I	?r/   )r)   r   r   r   r   r   r   rp   r=   )r2   rr   rs   rt   ru   r   rv   r8   r   r-   rQ   rw   r:   rx   rX   ri   r  r  r   ry   r   r   s   @r,   r   r     s    

 _]##!I~ 0 L"VJV'3  . \\ 	#`DCJ2h/  / r/   r   c                     ^  \ rS rSr% SrSSS.rS\S'   SrS	rS
\S'   SU 4S jjr	S r
S rS rS r\U 4S j5       r\R                   U 4S j5       rS rSrU =r$ )QuarterLengthAxisiX  z/
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                @   > [         TU ]  X5        SU l        SU l        g )NTF)r   r-   r4  r5  r   s      r,   r-   QuarterLengthAxis.__init__p  s     * %r/   c                L    U R                  UR                  R                  5      $ r=   )
dataFromQLr   r6  rN   s      r,   rQ   #QuarterLengthAxis.extractOneElementu  s    qzz7788r/   c                    U R                   (       a  U R                  U5      nU$ US:  a  [        U5      nU$ U R                  nU$ r  )r4  remapQuarterLengthfloatr   )r+   r7  r   s      r,   r=  QuarterLengthAxis.dataFromQLx  sM    ''+A
 	 !Vb	A    Ar/   c                   U R                   nU(       d  / $ U R                  R                  (       a  UR                  5       nOUR                  5       nUR	                  U R                  R
                  5      n[        R                  " US5      n/ n[        U5       Hm  nU R                  U5      nU R                  (       a!  [        R                  " U5      R                  nO[        [        US5      5      nUR!                  Xg45        Mo     U$ )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')
r6  r   )r   r*   recurseiterr)  classFilterListelementAnalysisattributeCountsortedr=  r5  r   DurationfullNamer'   roundr`   )r+   r   sSrcmappingri   r7  r   r:   s           r,   ri   QuarterLengthAxis.ticks  s    T KKI[[  99;D668D&&t{{'B'BC!00G/B#A$$ ))"-66E"aL)LL!$ " r/   c                d    U R                   SL a  gU R                   SL a  gSU R                   S S3$ )aH  
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$))r4  r9   s    r,   labelLogTagQuarterLengthAxis.labelLogTag  s>    & u$%T--a033r/   c                :   > [         TU ]  U R                  5       -   $ r=   )r   r:   rR  )r+   r1   s    r,   r:   QuarterLengthAxis.label  s    w}t//111r/   c                $   > U[        5       l        g r=   )r   r:   )r+   r@   r1   s     r,   r:   rU    s    r/   c                    US:X  a  U R                   n [        R                  " [        U5      5      $ ! [         a    [        SU 35      ef = f)zu
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   r]   log2rA  
ValueErrorr   )r+   r   s     r,   r@  $QuarterLengthAxis.remapQuarterLength  sS    
 6  A	E99U1X&& 	E #?s!CDD	Es	   3 A)r5  r4  rp   )r2   rr   rs   rt   ru   r   rv   r8   r   r-   rQ   r=  ri   rR  rw   r:   rx   r@  ry   r   r   s   @r,   r3  r3  X  s    

!I~ 
 $L#%J %&
9>@44 2 2 \\ E Er/   r3  c                  ^   ^  \ rS rSr% SrSS0rS\S'   SrS\S	'   S
S\4U 4S jjr	S r
SrU =r$ )OffsetEndAxisi  zA
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                h   > [         TU ]  X5        X0l        U[        :X  a  U R                  U l        g g r=   )r   r-   r]  USE_GRACE_NOTE_SPACINGr   )r+   r*   r   r]  r1   s       r,   r-   OffsetEndAxis.__init__  s2    *&00#//D 1r/   c                   [        UR                  U R                  5      5      n[        UR                  R                  5      nX@R
                  :  a  U R
                  nX44$ X@R
                  S-  :  aJ  [        US5      (       a+  UR                  b  UR                  R                  S;   a   X44$ X@R
                  -  nX44$ )Nr   tie)startcontinue)	rA  r   r   r   r6  r]  r   re  r  )r+   rO   rP   offuseQLs        r,   rQ   OffsetEndAxis.extractOneElement  s    A**4;;78ajj../###$$E | %%))q%  QUU%6155::I^;^ | )))|r/   )r]  )r2   rr   rs   rt   ru   r   rv   r   rb  r-   rQ   ry   r   r   s   @r,   r\  r\    sD     	 !I~  #IJH"S>T 0 r/   r\  c                  J   ^  \ rS rSr% SrSrSrS\S'   S
U 4S jjrS r	S	r
U =r$ )DynamicsAxisi  z)
Axis subclass for dealing with Dynamics
Dynamic)dynamicr	   volumer$   r   c                   > Uc)  SU l         [        [        R                  5      S-
  U l        g [
        TU ]  U5        [        U R                   5      U l         [        U R                  5      U l        g )Nr   r   )r   r  r	   
shortNamesr   r   rX   r\   )r+   rW   r1   s     r,   rX   "DynamicsAxis.setBoundariesFromData  sS    >DM 3 34q8DMG)&1.DM.DMr/   c                    / nU R                   c  U R                  5         [        U R                   U R                  S-   5       H*  nUR	                  US[
        R                  U   -  45        M,     U$ )a  
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   z$%s$)r   rX   r_   r   r`   r	   rq  )r+   ri   r   s      r,   ri   DynamicsAxis.ticks#  se    $ == &&(t}}dmma&78ALL!Wx':':1'==>? 9 r/   )r   r   r=   )r2   rr   rs   rt   ru   r8   r   rv   rX   ri   ry   r   r   s   @r,   rl  rl    s(     L"CJC/ r/   rl  c                  \   ^  \ rS rSr% SrSS0rS\S'   SrSrS	\S
'   SU 4S jjr	S r
SrU =r$ )CountingAxisi@  a  
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                2   > [         TU ]  X5        SU l        g rp   )r   r-   rw  r   s      r,   r-   CountingAxis.__init__X  s    *r/   c                D  ^  T R                   nUc  / $ SSKJn  T R                  n[        R
                  " U5      (       d  U4n[        U 4S jU 5       5      nT R                  T R                     nU" U6 nUR                   Vs/ s H
  ov" U5      PM     nn0 n	UR                   HE  n
U" U
5      nU
S   n[        U[        5      (       d  M'  X;   a  X   R                  U5        MA  XU'   MG     [        R                  " U5      n/ nU Hu  nS/[        U5      S-   -  n[        U5      S:  a  U H  nUU   UU'   M     OUUUS   '   X   UU'   U	R!                  U0 5      nUR#                  [        U5      U4-   5        Mw     [%        U5      Ul	        UR                  $ s  snf )zC
Replace client.data with a list that only includes each key once.
Nr   )
itemgetterc              3  B   >#    U  H  nTR                   U   v   M     g 7fr=   )r   ).0r   r+   s     r,   	<genexpr>/CountingAxis.postProcessData.<locals>.<genexpr>i  s     Qy8D,,X6ys   r   r   )r*   operatorr  rw  r   
isIterabletupler   r   datar&   dictupdatecollectionsCounterr  getr`   rI  )r+   r*   r  rw  axesIndices	thisIndexselector
innerTuplerelevantDatatupleFormatDict	dataPoint	dataIndexrP   counternewClientData
counterKey	innerListdependentIndexs   `                 r,   rn   CountingAxis.postProcessData\  s    >I'NN	  ++"IQyQQ$$T]]3	{+?E{{K{,{K I +I"2Jj$//+*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)rw  )Nr   )r2   rr   rs   rt   ru   r   rv   r8   r   r-   rn   ry   r   r   s   @r,   rv  rv  @  s@     	 !I~  L"PJP- -r/   rv  c                      \ rS rSrS rSrg)Testi  c           
     $   S nSSK Jn  SSKJn  UR	                  S5      nU" U5      nS Ul        [        US5      Ul        XR                  l        UR                  5         U R                  UR                  SS0 4SSS	S
04SS0 4SSS	S
04/5        g )Nc                f    U R                   R                  b  SUS'   U R                   R                  $ )Nredcolor)r
   
accidentaldiatonicNoteNum)rO   rP   s     r,   countingAxisFormatter: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   r  r        )music21.graph.plotr  music21r  parse
doneActionr   axisXrQ   runassertEqualr  )r+   r  r  r  r   hists         r,   testCountingAxisFormatTest.testCountingAxisFormat  s    	+
 	1%OOBC|$_
'<

$
a*q!gu-=&>a*q!gu-=&>@	Ar/   rM   N)r2   rr   rs   rt   r  ry   rM   r/   r,   r  r    s    Ar/   r  __main__)+ru   
__future__r   r  r]   r   typingtunittestmusic21.graph.utilitiesr   r   r  r   r   r   r	   r
   r   r   music21.analysisr   rG  r   TYPE_CHECKINGr   rb  ProtoM21Objectr   r{   r   r   r   r   r   r3  r\  rl  rv  TestCaser  r2   mainTestrM   r/   r,   <module>r     s&   #   	   L        8 * ??  u7!! utR RjG;Y G;T==Y ==@!> !@!4 !&[  [ |
VE VErJ D(4 (ZI4 IZA8 A, zT r/   