Skip to content

Commit 81be1e0

Browse files
committed
hotfix: fix utils.py import
1 parent 783b20e commit 81be1e0

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

docs/source/release-history.rst

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Release History
22
===============
33

4+
v1.4.1 (2023-08-02)
5+
-------------------
6+
7+
- hotfix: fix .utils import in basic.py
8+
49
v1.4.0 (2023-07-28)
510
-------------------
611

mscales/basic.py

+48-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from collections.abc import Iterable
44
import matplotlib.pyplot as plt
55
import pretty_midi as pm
6-
from utils import find_ngrams
6+
from .utils import find_ngrams
77
from collections import Counter
88

99
rng = np.random.default_rng()
@@ -37,7 +37,9 @@ def __sub__(self, other):
3737
if isinstance(other, PitchClassInterval):
3838
return PitchClass((self.p - other.i) % self.c)
3939
else:
40-
raise TypeError(f"Can't subtract type {type(other)} from pitch class {self.p}.")
40+
raise TypeError(
41+
f"Can't subtract type {type(other)} from pitch class {self.p}."
42+
)
4143

4244
def __eq__(self, other):
4345
return self.p == other.p
@@ -70,7 +72,9 @@ def __sub__(self, other):
7072
if isinstance(other, PitchClassInterval):
7173
return self.i - other.i % self.c
7274
else:
73-
raise TypeError(f"Can't subtract type {type(other)} from interval {self.i}.")
75+
raise TypeError(
76+
f"Can't subtract type {type(other)} from interval {self.i}."
77+
)
7478

7579
def __eq__(self, other):
7680
return self.i == other.i
@@ -89,11 +93,13 @@ def __init__(self, pcset, c: int = 12):
8993
assert all(
9094
x in [str(i) for i in range(10)] + ["T"] + ["E"] for x in list(pcset)
9195
), "Some pitch classes are not valid."
92-
self.pcs = np.array([10 if p == "T" else 11 if p == "E" else int(p) for p in list(pcset)])
96+
self.pcs = np.array(
97+
[10 if p == "T" else 11 if p == "E" else int(p) for p in list(pcset)]
98+
)
9399
elif isinstance(pcset, (Iterable, PitchClassSet)):
94100
self.pcs = np.array([int(p) for p in pcset])
95101
else:
96-
raise TypeError(f"I don't recognize the pitch-class input {type(pcset)}.")
102+
raise TypeError(f"I don't recognize the pitch-class input {type(pcset)}.")
97103

98104
def __repr__(self):
99105
return f"PitchClassSet({self.pcs})"
@@ -138,7 +144,9 @@ def normal_form(self):
138144
elif len(self.pcs) == 1:
139145
return self
140146
else:
141-
rotations = np.array([np.roll(self.pcs, i) for i in range(self.pcs.shape[0])])
147+
rotations = np.array(
148+
[np.roll(self.pcs, i) for i in range(self.pcs.shape[0])]
149+
)
142150
for length in range(self.d - 1, 0, -1):
143151
spans = [(r[-1] - r[0]) % self.c for r in rotations[:, : length + 1]]
144152
mask = spans == min(spans)
@@ -214,12 +222,15 @@ def interval_vector(self):
214222

215223
def maximally_even(self):
216224
"""
217-
Calculates all maximally even sets for chromatic cardinality c
225+
Calculates all maximally even sets for chromatic cardinality c
218226
and diatonic cardinality d.
219227
"""
220228

221-
D = [ [ np.floor((self.c * k + m) / self.d).astype(int) for k in range(self.d) ] for m in range(self.c) ]
222-
D = [ np.array(s) for s in set(tuple(i) for i in D) ]
229+
D = [
230+
[np.floor((self.c * k + m) / self.d).astype(int) for k in range(self.d)]
231+
for m in range(self.c)
232+
]
233+
D = [np.array(s) for s in set(tuple(i) for i in D)]
223234

224235
for s in D:
225236
if set(s) == set(self.pcs):
@@ -233,29 +244,36 @@ def spectrum(self, i):
233244
given chromatic cardinality c and diatonic cardinality d.
234245
"""
235246

236-
assert i in range(self.d), f"Generic interval i={i} has to be between 0 and {self.d-1}."
247+
assert i in range(
248+
self.d
249+
), f"Generic interval i={i} has to be between 0 and {self.d-1}."
237250

238-
return { (k - j) % self.c for j, k in zip(self.pcs, np.roll(self.pcs, -i)) }
251+
return {(k - j) % self.c for j, k in zip(self.pcs, np.roll(self.pcs, -i))}
239252

240253
def myhill(self):
241254
"""
242255
Returns whether pitch-class set has Myhill's property.
243256
"""
244257

245-
specs = set([ len(self.spectrum(i=i)) for i in range(1,self.d) ])
246-
258+
specs = set([len(self.spectrum(i=i)) for i in range(1, self.d)])
259+
247260
return True if specs == {2} else False
248261

249262
def cardinality_equals_variety(self):
250263
"""
251264
Tests if cardinality equals variety holds for PCSet.
252265
See: https://en.wikipedia.org/wiki/Cardinality_equals_variety
253266
"""
254-
cev = True
255-
for n in range(2,self.d + 1):
256-
s = np.append(self.pcs, self.pcs[:n-1])
267+
268+
for n in range(2, self.d + 1):
269+
s = np.append(self.pcs, self.pcs[: n - 1])
257270
ngrams = find_ngrams(s, n=n)
258-
intervals = [ "".join([str((gram[i] - gram[i-1]) % 12) for i in range(1, len(gram))]) for gram in ngrams ]
271+
intervals = [
272+
"".join(
273+
[str((gram[i] - gram[i - 1]) % 12) for i in range(1, len(gram))]
274+
)
275+
for gram in ngrams
276+
]
259277
if n != len(Counter(intervals)):
260278
return False
261279
return True
@@ -314,7 +332,9 @@ def plot(self, kind: str = "area", save: bool = False):
314332
ax.plot(thetas, radii, c="k", zorder=5)
315333
ax.fill(thetas, radii, alpha=0.75, zorder=4)
316334
else:
317-
print("I don't recognize the plot kind." "Valid values are 'polar' and 'bar'.")
335+
print(
336+
"I don't recognize the plot kind." "Valid values are 'polar' and 'bar'."
337+
)
318338
if save:
319339
plt.savefig(save)
320340

@@ -332,7 +352,9 @@ def play(
332352
starts = np.arange(n_notes) * note_duration # onsets
333353
ends = starts + note_duration # offsets
334354

335-
pitches = [x for x in rng.choice(np.nonzero(self.to_vector())[0], size=n_notes)]
355+
pitches = [
356+
x for x in rng.choice(np.nonzero(self.to_vector())[0], size=n_notes)
357+
]
336358
octaves = rng.choice(np.arange(3, 7), size=n_notes)
337359
midi_pitches = [(p + 12 * o) for p, o in list(zip(pitches, octaves))]
338360
elif mode == "chord":
@@ -389,11 +411,14 @@ def info(self):
389411
s += f"interval vector\t: {self.interval_vector()}" + "\n\n"
390412

391413
s += "Diatonic Scale Theory" + "\n"
392-
s += "=====================" + "\n"
393-
s += f"Maximally even: {str(self.maximally_even())}" + "\n"
414+
s += "=====================" + "\n"
415+
s += f"Maximally even: {str(self.maximally_even())}" + "\n"
394416
s += f"Spectrum (step): {str(self.spectrum(i=1))}" + "\n"
395417
s += f"Myhill's property: {str(self.myhill())}" + "\n"
396-
s += f"Cardinality equals variety: {str(self.cardinality_equals_variety())}" + "\n\n"
418+
s += (
419+
f"Cardinality equals variety: {str(self.cardinality_equals_variety())}"
420+
+ "\n\n"
421+
)
397422

398423
s += "Serialism" + "\n"
399424
s += "=========" + "\n"
@@ -435,7 +460,7 @@ def info(self):
435460

436461
# pcset.play(save_as="test.mid", mode="cloud")
437462

438-
## maximally even test
463+
# maximally even test
439464
# t = PitchClassSet("1234")
440465
# dia = PitchClassSet("024579E")
441466
p = PitchClassSet("1368T")

0 commit comments

Comments
 (0)