<jupyter_start><jupyter_text>Select Piecepiece = 'https://raw.githubusercontent.com/RichardFreedman/CRIM_additional_works/main/MEI/CRIM_Cadences.mei'<jupyter_code>piece = 'https://crimproject.org/mei/CRIM_Model_0009.mei'
# piece = 'https://raw.githubusercontent.com/RichardFreedman/CRIM_additional_works/main/MEI/CRIM_Model_0018_n2.mei_msg.mei'
corpus = CorpusBase([piece])
model = corpus.scores[0]
model.score.metadata.title
add_offset(model.score)
model.score.measures(1, 30).show()<jupyter_output><empty_output><jupyter_text>Music as a table<jupyter_code>model.getNoteRest().head(10).fillna("-").T.style.applymap(map_colours)
model.getNoteRest()<jupyter_output><empty_output><jupyter_text>Melodic patterns<jupyter_code>model_melodic = model.getMelodic(kind="c", unit=2)
model_melodic
ngrams = model.getNgrams(df=model.getMelodic(kind="d", compound=True), n=5)
ngrams
ngrams.stack().to_frame()<jupyter_output><empty_output><jupyter_text>Searching with various parameters* Note that if 'units' are no specified, then the results are simply the melodic vectors, regardless of duration.* If we specify "units" then the results are incremental by that unit* The results will be different in each case* Sample search: 4, 1, 2, 2 or chromatic 5, 0, 2, 2, -4<jupyter_code># Ngrams without time_units
@interact(search_pattern="", kind=["d", "c"])
def get_ngrams(search_pattern, kind):
    ngrams = model.getNgrams(df=model.getMelodic(kind=kind), n=5)
    mask = ngrams.apply(lambda x: x.astype(str).str.contains(search_pattern).any(), axis=1)
    filtered_ngrams = ngrams[mask].copy()
    return filtered_ngrams.fillna("-").style.applymap(lambda x: "background: #ccebc5" if search_pattern in x else "")
# NGrams with time_units

@interact(search_pattern="", kind=["d", "c", "z"], time_unit=[1, 2, 4, 8], length=[3, 4, 5, 6, 7])
def get_ngrams(search_pattern, kind, time_unit, length):
    ngrams = model.getNgrams(df=model.getMelodic(kind=kind), n=length, unit=time_unit)
    mask = ngrams.apply(lambda x: x.astype(str).str.contains(search_pattern).any(), axis=1)
    filtered_ngrams = ngrams[mask].copy()
    return filtered_ngrams.fillna("-").style.applymap(lambda x: "background: #ccebc5" if search_pattern in x else "")<jupyter_output><empty_output><jupyter_text>Count of melodic intervals<jupyter_code>model.getMelodic(directed=True).apply(pd.Series.value_counts).fillna(0).astype(int).style.bar()<jupyter_output><empty_output><jupyter_text>Harmonic intervals Example search pattern: 12, 10, 8, 8|5, 3, 1, 1$Authentic cadence:  7, 6, 8<jupyter_code>@interact(search_pattern="", kind=["d", "c"], length=[5, 3, 4, 6, 7])
def get_ngrams(search_pattern, kind, length):
    ngrams = model.getNgrams(df=model.getMelodic(kind=kind), n=length)
    mask = ngrams.apply(lambda x: x.astype(str).str.contains(search_pattern).any(), axis=1)
    filtered_ngrams = ngrams[mask].copy()
    return filtered_ngrams.fillna("-").style.applymap(lambda x: "background: #ccebc5" if search_pattern in x else "")
har = model.getHarmonic(kind="d")
regHar = model.regularize(df=har, unit=1)
regHar
mel = model.getNgrams(df=model.getMelodic(kind="d"), n=4)
# regMel = ave_maria.regularize(df=mel, unit=4)
# regMel.stack().to_frame().reset_index(level=1)
b = mel.stack().to_frame().reset_index(level=1)
b.groupby("level_1")<jupyter_output><empty_output><jupyter_text>Return offsets as list For use with filtered DFs from classifier, etc.<jupyter_code>df = get_harmonic_ngrams("7, 6, 8", True, "d", 1, 3)
a = df.index.tolist()
a

# measures
for i in a:
    b = i/8 - 1
    print(b)
a
cadence_filter = get_harmonic_ngrams("7, 6, 8", True, "d", 1, 3)
cadence_list = cadence_filter.index.tolist()
cadence_tones = model.getNoteRest()
cadence_tones.loc[cadence_list,:].fillna("-")<jupyter_output><empty_output><jupyter_text>Modules:* Authentic/Phrygian cadence with suspension in diatonic:  7_Held, 6_-2, 8* Authentic/Phrygian cadence with suspension in chromatic: 11_Held, 9_-2, 12* Authentic/Phrygian cadence with suspension in zero-based:  6_Held, 5_-2, 7* Authentic<jupyter_code>@interact(search_pattern="", compound=[True, False], time_unit=[0, 4, 2, 1, 8], length=[3, 4, 5, 6])
def get_modules(search_pattern, compound, time_unit, length):
    
    model_har = model.getHarmonic(kind="d", compound=compound)
#     ngrams = model.getNgrams(df=model_har, n=length, how='modules', exclude=['Rest'], cell_type="str", unit=time_unit)
    ngrams = model.getNgrams(df=model_har, n=length, how='modules', exclude=['Rest'], cell_type="str")

    filtered_ngrams = ngrams[ngrams.apply(lambda x: x.astype(str).str.contains(search_pattern).any(), axis=1)].copy()
    
    return filtered_ngrams.fillna("-").style.applymap(lambda x: "background: #ccebc5" if re.match(search_pattern, x) else "")
    
@interact(search_pattern="", compound=[True, False], time_unit=[0, 4, 2, 1, 8], length=[3, 4, 5, 6])
def get_modules(search_pattern, compound, time_unit, length):
    
    model_har = model.getHarmonic(kind="d", compound=compound)
#     ngrams = model.getNgrams(df=model_har, n=length, how='modules', exclude=['Rest'], cell_type="str", unit=time_unit)
    ngrams = model.getNgrams(df=model_har, n=length, how='modules', exclude=['Rest'], cell_type="str")

    filtered_ngrams = ngrams[ngrams.apply(lambda x: x.astype(str).str.contains(search_pattern).any(), axis=1)].copy()
    
    a = filtered_ngrams.fillna("-").style.applymap(lambda x: "background: #ccebc5" if re.match(search_pattern, x) else "")
    c = a.index.to_list()
    return c
# here we take the offsets of the predicted cadences and find the pitches associated with them
# note that these are the _start_ offsets of the module.  we will need to find the end points for a more accurate tonal map


c = [87.0, 88.0, 111.0, 131.5, 132.0, 167.5, 168.0]
notes = model.getNoteRest()
notes.loc[a]
model.score.measures(1, 30).show()
a
model_har = model.getHarmonic(kind="d", compound=True)
ngrams = model.getNgrams(df=model_har, n=3, how='modules', exclude=['Rest'], cell_type="str")
# filtered_ngrams = ngrams[ngrams.apply(lambda x: x.astype(str).str.contains('7').any(), axis=1)].copy()
# filtered_ngrams.stack().value_counts().to_frame()
ngrams.stack().value_counts().to_frame()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15871485943775102, 'fraction_numerical': 0.023614457831325302, 'mean_word_length': 6.447368421052632, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '123992', 'license': 'Apache-2.0', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2268, 'n_words': 458, 'path': 'crim_intervals_drb.ipynb', 'repo_name': 'RichardFreedman/CRIM_Intervals_Notebooks', 'size': '6225'}