/
__init__.py
243 lines (194 loc) · 8.96 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
# Name: graph.py
# Purpose: Classes for graphing in matplotlib and/or other graphing tools.
#
# Authors: Christopher Ariza
# Michael Scott Asato Cuthbert
# Evan Lynch
#
# Copyright: Copyright © 2009-2022 Michael Scott Asato Cuthbert
# License: BSD, see license.txt
# ------------------------------------------------------------------------------
'''
Tools for graphing, plotting, or otherwise visualizing :class:`~music21.stream.Stream` objects.
The easiest and most common way of using plotting functions is to call
`.plot('typeOfGraph')` on a Stream. See :meth:`~music21.stream.Stream.plot`.
That method uses tools from the `music21.graph.findPlot` module to map between
names of plots and classes that can show them.
The :class:`~music21.graph.plot.PlotStream`
subclasses in the `music21.graph.plot` module give easy to use
and configurable ways of graphing data and structures in
:class:`~music21.stream.Stream` objects. These Plot objects use classes from
the `music21.graph.axis` module to automatically extract relevant data for you.
At a lower level, the :class:`~music21.graph.primitives.Graph` subclasses
in the `music21.graph.primitives` modules give abstract fundamental
graphing archetypes using the matplotlib library. They are to be used when
you already have data extracted on your own but still want to take advantage
of musically-aware axes and scaling.
From highest level to lowest level usage, ways of graphing are as follows:
1. `streamObj.plot('graphName')`
2. `graph.plot.Class(streamObj).run()`
3. `plotter = graph.primitives.Class(); plotter.data = ...; plotter.process()`
4. Use `matplotlib` directly to create any graph, musical or non-musical.
'''
from __future__ import annotations
__all__ = [
'axis', 'findPlot', 'plot', 'primitives', 'utilities',
'plotStream',
]
import typing as t
import unittest
from music21 import common
from music21 import environment
from music21.graph import axis
from music21.graph import findPlot
from music21.graph import plot
from music21.graph import primitives
from music21.graph import utilities
if t.TYPE_CHECKING:
from music21 import stream
environLocal = environment.Environment('graph')
def plotStream(
streamObj: stream.Stream,
graphFormat: t.Optional[str] = None,
xValue=None,
yValue=None,
zValue=None,
**keywords,
):
'''
Given a stream and any keyword configuration arguments, create and display a plot.
Note: plots require matplotlib to be installed.
Plot methods can be specified as additional arguments or by keyword.
Two keyword arguments can be given: `format` and `values`.
If positional arguments are given, the first is taken as `format`
and the rest are collected as `values`. If `format` is the class
name, that class is collected. Additionally, every
:class:`~music21.graph.PlotStream` subclass defines one `format`
string and a list of `values` strings. The `format` parameter
defines the type of Graph (e.g. scatter, histogram, colorGrid). The
`values` list defines what values are graphed
(e.g. quarterLength, pitch, pitchClass).
If a user provides a `format` and one or more `values` strings, a plot with
the corresponding profile, if found, will be generated. If not, the first
Plot to match any of the defined specifiers will be created.
In the case of :class:`~music21.graph.PlotWindowedAnalysis` subclasses,
the :class:`~music21.analysis.discrete.DiscreteAnalysis`
subclass :attr:`~music21.analysis.discrete.DiscreteAnalysis.identifiers` list
is added to the Plot's `values` list.
Available plots include the following:
* :class:`~music21.graph.plot.HistogramPitchSpace`
* :class:`~music21.graph.plot.HistogramPitchClass`
* :class:`~music21.graph.plot.HistogramQuarterLength`
* :class:`~music21.graph.plot.ScatterPitchSpaceQuarterLength`
* :class:`~music21.graph.plot.ScatterPitchClassQuarterLength`
* :class:`~music21.graph.plot.ScatterPitchClassOffset`
* :class:`~music21.graph.plot.ScatterPitchSpaceDynamicSymbol`
* :class:`~music21.graph.plot.HorizontalBarPitchSpaceOffset`
* :class:`~music21.graph.plot.HorizontalBarPitchClassOffset`
* :class:`~music21.graph.plot.ScatterWeightedPitchSpaceQuarterLength`
* :class:`~music21.graph.plot.ScatterWeightedPitchClassQuarterLength`
* :class:`~music21.graph.plot.ScatterWeightedPitchSpaceDynamicSymbol`
* :class:`~music21.graph.plot.Plot3DBarsPitchSpaceQuarterLength`
* :class:`~music21.graph.plot.WindowedKey`
* :class:`~music21.graph.plot.WindowedAmbitus`
* :class:`~music21.graph.plot.Dolan`
>>> s = corpus.parse('bach/bwv324.xml') #_DOCS_HIDE
>>> thePlot = s.plot('histogram', 'pitch', doneAction=None) #_DOCS_HIDE
>>> #_DOCS_SHOW s = corpus.parse('bach/bwv57.8')
>>> #_DOCS_SHOW thePlot = s.plot('histogram', 'pitch')
.. image:: images/HistogramPitchSpace.*
:width: 600
>>> s = corpus.parse('bach/bwv324.xml') #_DOCS_HIDE
>>> thePlot = s.plot('pianoroll', doneAction=None) #_DOCS_HIDE
>>> #_DOCS_SHOW s = corpus.parse('bach/bwv57.8')
>>> #_DOCS_SHOW thePlot = s.plot('pianoroll')
.. image:: images/HorizontalBarPitchSpaceOffset.*
:width: 600
'''
plotMake = findPlot.getPlotsToMake(graphFormat, xValue, yValue, zValue)
# environLocal.printDebug(['plotClassName found', plotMake])
obj = None
for plotInfo in plotMake:
if not common.isIterable(plotInfo):
plotClassName = plotInfo
plotDict = None
else:
plotClassName, plotDict = plotInfo
obj = plotClassName(streamObj, **keywords)
if plotDict:
for axisName, axisClass in plotDict.items():
attrName = 'axis' + axisName.upper()
setattr(obj, attrName, axisClass(obj, axisName))
obj.run()
if obj:
return obj # just first one...
# ------------------------------------------------------------------------------
class TestExternal(unittest.TestCase):
show = True
def testAll(self):
from music21 import corpus
from music21 import dynamics
a = corpus.parse('bach/bwv57.8')
a.parts[0].insert(0, dynamics.Dynamic('mf'))
a.parts[0].insert(10, dynamics.Dynamic('f'))
if self.show:
plotStream(a, 'all')
class Test(unittest.TestCase):
def testCopyAndDeepcopy(self):
from music21.test.commonTest import testCopyAll
testCopyAll(self, globals())
def testAll(self):
from music21 import corpus
a = corpus.parse('bach/bwv57.8')
plotStream(a.flatten(), doneAction=None)
def testPlotChordsC(self):
from music21 import dynamics
from music21 import note
from music21 import stream # pylint: disable=redefined-outer-name # only in TYPE_CHECKING
from music21 import scale
sc = scale.MajorScale('c4')
s = stream.Stream()
s.append(dynamics.Dynamic('f'))
s.append(note.Note('c4'))
s.append(sc.getChord('e3', 'a3', quarterLength=0.5))
# s.append(note.Note('c3', quarterLength=2))
s.append(dynamics.Dynamic('mf'))
s.append(sc.getChord('b3', 'e4', quarterLength=1.5))
s.append(dynamics.Dynamic('pp'))
s.append(sc.getChord('f4', 'g5', quarterLength=3))
s.append(sc.getChord('f4', 'g5', quarterLength=3))
s.append(note.Note('c5', quarterLength=3))
for plotType, xValue, yValue in [
('histogram', 'pitch', None),
('histogram', 'pitchclass', None),
('histogram', 'quarterlength', None),
('scatter', 'pitch', 'quarterlength'),
('scatter', 'pitchspace', 'offset'),
('scatter', 'pitch', 'offset'),
('scatter', 'dynamics', None),
('bar', 'pitch', None),
('bar', 'pc', None),
('weighted', 'pc', 'duration'),
('weighted', 'dynamics', None),
]:
s.plot(plotType, xValue=xValue, yValue=yValue, doneAction=None)
def testHorizontalInstrumentationB(self):
from music21 import corpus
from music21 import dynamics
from music21 import stream # pylint: disable=redefined-outer-name # only in TYPE_CHECKING
s = corpus.parse('bwv66.6')
dyn = ['p', 'mf', 'f', 'ff', 'mp', 'fff', 'ppp']
i = 0
for p in s.parts:
for m in p.getElementsByClass(stream.Measure):
m.insert(0, dynamics.Dynamic(dyn[i % len(dyn)]))
i += 1
s.plot('dolan', fillByMeasure=True, segmentByTarget=True, doneAction=None)
# -----------------------------------------------------------------------------
_DOC_ORDER = [plotStream]
# -----------------------------------------------------------------------------
if __name__ == '__main__':
import music21
music21.mainTest(Test) # , runTest='testPlot3DPitchSpaceQuarterLengthCount')