Skip to content

Commit

Permalink
Merge pull request #422 from CPJKU/bug/issue_421
Browse files Browse the repository at this point in the history
Fixes issue #421
  • Loading branch information
manoskary authored Jan 28, 2025
2 parents 9e7c651 + 49023bc commit 9606a32
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 2 deletions.
2 changes: 1 addition & 1 deletion partitura/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def __str__(self):
return f"PerformedNote: {self['id']}"

def __eq__(self, other):
if not isinstance(PerformedNote):
if not isinstance(other, PerformedNote):
return False
if not self.keys() == other.keys():
return False
Expand Down
194 changes: 193 additions & 1 deletion tests/test_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import unittest
import numpy as np

from partitura.performance import PerformedPart, Performance
from partitura.performance import PerformedPart, Performance, PerformedNote

RNG = np.random.RandomState(1984)

Expand Down Expand Up @@ -138,3 +138,195 @@ def generate_random_note_array(n_notes=100, beat_period=0.5, n_tracks=3):
idx = track_idxs[i * track_length :]
note_array["track"][idx] = i
return note_array


class TestPerformedNote(unittest.TestCase):
def test_initialization(self):
"""
Test that the notes are initialized correctly
"""
pnote = PerformedNote(
{
"pitch": 60,
"note_on": 10,
"note_off": 20,
}
)
self.assertEqual(pnote["pitch"], 60)
self.assertEqual(pnote["note_on"], 10)
self.assertEqual(pnote["note_off"], 20)

# Test initialization to default values
self.assertEqual(pnote["track"], 0)
self.assertEqual(pnote["channel"], 1)
self.assertEqual(pnote["velocity"], 60)
self.assertEqual(pnote["sound_off"], 20)

def test_validate_values(self):
"""
Test for _validate_values
"""

# Invalid pitch
with self.assertRaises(ValueError):
PerformedNote(
{
"pitch": 128,
"note_on": 10,
"note_off": 20,
}
)

# invalid note on
with self.assertRaises(ValueError):
PerformedNote(
{
"pitch": 60,
"note_on": -5,
"note_off": 20,
}
)

# invalid note off (note off < note on)
with self.assertRaises(ValueError):
PerformedNote(
{
"pitch": 60,
"note_on": 10,
"note_off": 5,
}
)

# invalid velocity (>127)
with self.assertRaises(ValueError):
PerformedNote(
{
"pitch": 60,
"note_on": 10,
"note_off": 20,
"velocity": 150,
}
)

# validate sound off (<note_off)
with self.assertRaises(ValueError):
PerformedNote(
{
"pitch": 60,
"note_on": 10,
"note_off": 20,
"sound_off": 15,
}
)

# Invalid data type for validation
# (passing a list instead of a tuple or a dict)
pnote = PerformedNote(
{
"pitch": 60,
"note_on": 10,
"note_off": 20,
}
)
with self.assertRaises(ValueError):
pnote._validate_values(["invalid_data_type"])

def test_equality(self):
"""
Test __eq__ method
"""
# Notes are equal
pnote1 = PerformedNote(
{
"pitch": 60,
"note_on": 10,
"note_off": 20,
}
)
pnote2 = PerformedNote(
{
"pitch": 60,
"note_on": 10,
"note_off": 20,
}
)
self.assertEqual(pnote1, pnote2)

# Notes are different
pnote1 = PerformedNote(
{
"pitch": 60,
"note_on": 10,
"note_off": 20,
}
)
pnote2 = PerformedNote(
{
"pitch": 62,
"note_on": 10,
"note_off": 20,
}
)
self.assertNotEqual(pnote1, pnote2)

def test_ordering(self):
"""
Test __le__
"""
pnote1 = PerformedNote(
{
"pitch": 60,
"note_on": 10,
"note_off": 20,
}
)
pnote2 = PerformedNote(
{
"pitch": 60,
"note_on": 15,
"note_off": 25,
}
)
self.assertTrue(pnote1 < pnote2)
self.assertTrue(pnote1 <= pnote2)
self.assertTrue(pnote2 > pnote1)
self.assertTrue(pnote2 >= pnote1)

def test_get_and_set_item(self):
pnote = PerformedNote(
{
"pitch": 60,
"note_on": 10,
"note_off": 20,
}
)
pnote["velocity"] = 80
self.assertEqual(pnote["velocity"], 80)

with self.assertRaises(KeyError):
pnote["invalid_key"] = 100

def test_copy(self):
pnote = PerformedNote(
{
"pitch": 60,
"note_on": 10,
"note_off": 20,
}
)
pnote_copy = pnote.copy()
self.assertEqual(pnote, pnote_copy)
self.assertIsNot(pnote, pnote_copy)

def test_cannot_delete_items(self):
pnote = PerformedNote(
{
"pitch": 60,
"note_on": 10,
"note_off": 20,
}
)
with self.assertRaises(KeyError):
del pnote["pitch"]


0 comments on commit 9606a32

Please sign in to comment.