Skip to content

Commit 84ccc30

Browse files
committed
add test and lint
1 parent 58fb7b2 commit 84ccc30

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

musicaiz/plotters/pianorolls.py

+16-18
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
get_subdivisions,
1212
TimingConsts
1313
)
14-
from musicaiz.structure import Note
14+
from musicaiz.structure import Note, Instrument
1515

1616

1717
COLOR_EDGES = [
@@ -131,13 +131,12 @@ def _add_new_bar_subdiv(subdivisions):
131131
"ticks": subdivisions[-1]["ticks"] + range_ticks,
132132
})
133133

134-
def _notes_loop(self, notes: List[Note], show_pitch_labels: bool = True):
134+
def _notes_loop(self, notes: List[Note]):
135135
plt.ylabel("Pitch")
136136
#highest_pitch = get_highest_pitch(track.instrument)
137137
#lowest_pitch = get_lowest_pitch(track.instrument)
138138
# plt.ylim((0, (highest_pitch - lowest_pitch) - 1))
139139

140-
pitches = []
141140
for note in notes:
142141
plt.vlines(x=note.start_ticks,
143142
ymin=note.pitch,
@@ -226,14 +225,14 @@ def _notes_loop(self, notes: List[Note]):
226225
x0=note.start_ticks,
227226
y0=note.pitch,
228227
x1=note.end_ticks,
229-
y1=note.pitch+1,
228+
y1=note.pitch + 1,
230229
line=dict(
231230
color=COLOR_EDGES[0],
232231
width=2,
233232
),
234233
fillcolor=COLOR[0],
235234
)
236-
235+
237236
# this is to add a hover information on each note
238237
self.fig.add_trace(
239238
go.Scatter(
@@ -246,23 +245,22 @@ def _notes_loop(self, notes: List[Note]):
246245
],
247246
y=[
248247
note.pitch,
249-
note.pitch+1,
250-
note.pitch+1,
248+
note.pitch + 1,
249+
note.pitch + 1,
251250
note.pitch,
252251
note.pitch
253-
],
252+
],
254253
fill="toself",
255254
mode="lines",
256-
name=f"pitch={note.pitch}<br>\n"\
257-
f"velocity={note.velocity}<br>\n"\
258-
f"start_ticks={note.start_ticks}<br>\n"\
255+
name=f"pitch={note.pitch}<br>\n"
256+
f"velocity={note.velocity}<br>\n"
257+
f"start_ticks={note.start_ticks}<br>\n"
259258
f"end_ticks={note.end_ticks}<br>",
260259
opacity=0,
261260
showlegend=False,
262261
)
263262
)
264263

265-
266264
def plot_grid(self, subdivisions):
267265
# all the pitch values to be written in the axis
268266
#self.fig.update_xaxes(range[0, len(subdivisions) - 1])
@@ -296,10 +294,9 @@ def plot_grid(self, subdivisions):
296294
self.fig.add_vline(x=s["ticks"], line_width=0.2, line_color="blue")
297295
prev_bar, prev_beat = s["bar"], s["bar_beat"]
298296

299-
300297
def plot_instrument(
301298
self,
302-
track,
299+
track: Instrument,
303300
bar_start: int,
304301
bar_end: int,
305302
subdivision: str,
@@ -309,7 +306,6 @@ def plot_instrument(
309306
time_sig: str = TimingConsts.DEFAULT_TIME_SIGNATURE.value,
310307
bpm: int = TimingConsts.DEFAULT_BPM.value,
311308
resolution: int = TimingConsts.RESOLUTION.value,
312-
print_measure_data: bool = True,
313309
show: bool = True
314310
):
315311

@@ -333,11 +329,11 @@ def plot_instrument(
333329
total_bars = bar_end - bar_start
334330
subdivisions = get_subdivisions(total_bars, subdivision, time_sig, bpm, resolution)
335331
self.plot_grid(subdivisions)
336-
332+
337333
# this is to add the yaxis labels# horizontal line for pitch grid
338334
labels = [i for i in range(min(pitches) - 1, max(pitches) + 2)]
339335
for pitch in labels:
340-
self.fig.add_hline(y=pitch+1, line_width=0.1, line_color="white")
336+
self.fig.add_hline(y=pitch + 1, line_width=0.1, line_color="white")
341337
# if we do have too many pitches, we won't label all of them in the yaxis
342338
if max(pitches) - min(pitches) > 24:
343339
cleaned_labels = [label for label in labels if label % 2 == 0]
@@ -357,7 +353,9 @@ def plot_instrument(
357353
),
358354
)
359355

360-
self.fig.update_layout(legend={"xanchor":"center", "yanchor":"top"})
356+
self.fig.update_layout(legend={
357+
"xanchor": "center", "yanchor": "top"
358+
})
361359

362360
if save_plot:
363361
self.fig.write_html(Path(path, filename + ".html"))

tests/unit/musicaiz/plotters/test_pianorolls.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
2+
import matplotlib.pyplot as plt
23

3-
from musicaiz.plotters import Pianoroll
4+
from musicaiz.plotters import Pianoroll, PianorollHTML
45
from musicaiz.loaders import Musa
56

67

@@ -20,3 +21,17 @@ def test_Pianoroll_plot_instrument(midi_sample):
2021
print_measure_data=False,
2122
show_bar_labels=False
2223
)
24+
plt.close('all')
25+
26+
27+
def test_PianorollHTML_plot_instrument(midi_sample):
28+
plot = PianorollHTML()
29+
musa_obj = Musa(midi_sample, structure="bars")
30+
plot.plot_instrument(
31+
track=musa_obj.instruments[0],
32+
bar_start=1,
33+
bar_end=2,
34+
subdivision="quarter",
35+
time_sig=musa_obj.time_sig.time_sig,
36+
show=False
37+
)

0 commit comments

Comments
 (0)