Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #261. PCC permutations. #262

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
301 changes: 301 additions & 0 deletions decitala/extra/PCC_permutation_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
{
"(0,)": [
"A",
"P"
],
"(0, 0)": [
"B",
"P"
],
"(0, 1)": [
"D",
"P"
],
"(1, 0)": [
"D",
"R"
],
"(0, 1, 0)": [
"G",
"P"
],
"(1, 0, 1)": [
"G",
"I"
],
"(1, 0, 0)": [
"G",
"P",
0
],
"(0, 0, 1)": [
"G",
"P",
1
],
"(0, 1, 1)": [
"G",
"I",
0
],
"(1, 1, 0)": [
"G",
"I",
1
],
"(0, 2, 1)": [
"L",
"P"
],
"(1, 2, 0)": [
"L",
"R"
],
"(2, 0, 1)": [
"L",
"I"
],
"(1, 0, 2)": [
"L",
"RI"
],
"(0, 1, 2)": [
"L",
"P",
1
],
"(2, 1, 0)": [
"L",
"R",
0
],
"(1, 0, 2, 1)": [
"P",
"P"
],
"(1, 2, 0, 1)": [
"P",
"R"
],
"(0, 1, 2, 1)": [
"P",
"P",
0
],
"(1, 0, 1, 2)": [
"P",
"P",
2
],
"(2, 1, 0, 1)": [
"P",
"R",
0
],
"(1, 2, 1, 0)": [
"P",
"R",
2
],
"(1, 0, 3, 2)": [
"X",
"P"
],
"(2, 3, 0, 1)": [
"X",
"R"
],
"(0, 1, 3, 2)": [
"X",
"P",
0
],
"(1, 3, 0, 2)": [
"Y",
"P"
],
"(1, 0, 2, 3)": [
"X",
"P",
2
],
"(3, 2, 0, 1)": [
"X",
"R",
0
],
"(2, 0, 3, 1)": [
"X",
"R",
1
],
"(2, 3, 1, 0)": [
"X",
"R",
2
],
"(3, 1, 0, 2)": [
"Y",
"P",
0
],
"(1, 3, 2, 0)": [
"Y",
"P",
2
],
"(0, 2, 3, 1)": [
"Y",
"R",
0
],
"(2, 0, 1, 3)": [
"Y",
"R",
2
],
"(1, 0, 2, 0, 1)": [
"12a",
"P"
],
"(1, 2, 0, 2, 1)": [
"12a",
"I"
],
"(0, 1, 2, 0, 1)": [
"12a",
"P",
0
],
"(1, 2, 0, 0, 1)": [
"12a",
"P",
1
],
"(1, 0, 0, 2, 1)": [
"12a",
"P",
2
],
"(1, 0, 2, 1, 0)": [
"12a",
"P",
3
],
"(2, 1, 0, 2, 1)": [
"12a",
"I",
0
],
"(1, 0, 2, 2, 1)": [
"12a",
"I",
1
],
"(1, 2, 2, 0, 1)": [
"12a",
"I",
2
],
"(1, 2, 0, 1, 2)": [
"12a",
"I",
3
],
"(1, 0, 3, 0, 2)": [
"12b",
"P"
],
"(2, 0, 3, 0, 1)": [
"12b",
"R"
],
"(2, 3, 0, 3, 1)": [
"12b",
"I"
],
"(1, 3, 0, 3, 2)": [
"12b",
"RI"
],
"(0, 1, 3, 0, 2)": [
"12b",
"P",
0
],
"(1, 3, 0, 0, 2)": [
"12b",
"P",
1
],
"(1, 0, 0, 3, 2)": [
"12b",
"P",
2
],
"(1, 0, 3, 2, 0)": [
"12b",
"P",
3
],
"(0, 2, 3, 0, 1)": [
"12b",
"R",
0
],
"(2, 3, 0, 0, 1)": [
"12b",
"R",
1
],
"(2, 0, 0, 3, 1)": [
"12b",
"R",
2
],
"(2, 0, 3, 1, 0)": [
"12b",
"R",
3
],
"(3, 2, 0, 3, 1)": [
"12b",
"I",
0
],
"(2, 0, 3, 3, 1)": [
"12b",
"I",
1
],
"(2, 3, 3, 0, 1)": [
"12b",
"I",
2
],
"(2, 3, 0, 1, 3)": [
"12b",
"I",
3
],
"(3, 1, 0, 3, 2)": [
"12b",
"RI",
0
],
"(1, 0, 3, 3, 2)": [
"12b",
"RI",
1
],
"(1, 3, 3, 0, 2)": [
"12b",
"RI",
2
],
"(1, 3, 0, 2, 3)": [
"12b",
"RI",
3
]
}
47 changes: 47 additions & 0 deletions decitala/extra/pcc_permutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
File for generating a JSON file that holds permutations of the monophonic
prime pitch contour classes with their associated fuzzy set-theoretic
transformations.
"""
import copy
import json

from decitala.hm import contour

def add_swaps(tuple_in, name, transformation, dict_in):
i = 0
while i < len(tuple_in) - 1:
pcc_cpy = copy.deepcopy(list(tuple_in))
pcc_cpy[i], pcc_cpy[i + 1] = pcc_cpy[i + 1], pcc_cpy[i]
if tuple(pcc_cpy) not in dict_in:
dict_in[tuple(pcc_cpy)] = (name, transformation, i)
i += 1

def make_pcc_json(save_path):
pcc_data = dict()
for cc, name in contour.PRIME_CONTOUR_CLASSES.items():
ret = cc[::-1]
inv = tuple(contour.invert_contour(cc))
ret_inv = tuple(contour.invert_contour(cc)[::-1])

pcc_data[cc] = (name, "P") # Prime
if ret not in pcc_data:
pcc_data[cc[::-1]] = (name, "R") # Retrograde

if inv not in pcc_data:
pcc_data[inv] = (name, "I") # Inversion

if ret_inv not in pcc_data:
pcc_data[ret_inv] = (name, "RI") # Retrograde-Inversion

add_swaps(cc, name, "P", pcc_data)
add_swaps(ret, name, "R", pcc_data)
add_swaps(inv, name, "I", pcc_data)
add_swaps(ret_inv, name, "RI", pcc_data)

pcc_data = {str(x): y for x, y in pcc_data.items()}
with open(save_path, "w") as fp:
json.dump(obj=pcc_data, fp=fp, indent=4)
return

# make_pcc_json(save_path="/Users/lukepoeppel/decitala/decitala/extra/PCC_permutation_data.json")
33 changes: 33 additions & 0 deletions decitala/hm/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#
# Location: Kent, 2021
####################################################################################################
import os
import json

from .schultz import spc
from .contour_utils import (
_track_extrema,
Expand Down Expand Up @@ -182,6 +185,36 @@ def contour_class(
except KeyError:
ContourException(f"The contour {contour} is not prime.")

def fuzzy_contour_class(contour):
"""
Function for retrieving the exact transformation of a fuzzy input
contour.
"""
base = os.path.dirname(os.path.dirname(__file__))
fuzzy_contour_data_path = os.path.join(base, "extra/PCC_permutation_data.json")
with open(fuzzy_contour_data_path, "r") as fp:
fuzzy_data = json.load(fp)

try:
return fuzzy_data[str(contour)]
except KeyError:
ContourException(f"Contour {contour} not found.")

def invert_contour(contour):
"""
Returns the inversion of a contour. From Morris (1993, p. 207):
I: "inverts the contour; each pitch x in the contour becomes pitch n-x in the inverted
contour; n is the highest pitch in the contour."
>>> c = (0, 1, 3, 2)
>>> invert_contour(c)
[3, 2, 0, 1]
>>> c2 = (0, 2, 1, 3)
>>> invert_contour(c2)
[3, 1, 2, 0]
"""
max_contour_val = max(contour)
return [max_contour_val - c for c in contour]

####################################################################################################
# Contour reduction tools.
# Implementation of Morris contour reduction algorithm (1993).
Expand Down