Skip to content

main

Batch-processing helpers built on top of ImportedPiece: exporting match results to CSV, converting EMA addresses into note-level score excerpts, and gathering ngram tables across pattern-matching results.

export_to_csv

export_to_csv(matches: list)

Exports matches data to a csv in the current working directory

Parameters:

Name Type Description Default
matches list

return value from either find_exact_matches or find_close_matches

required
Source code in crim_intervals/main.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def export_to_csv(matches: list):
    """Exports matches data to a csv in the current working directory

    Parameters
    ----------
    matches : list
        return value from either find_exact_matches or find_close_matches
    """
    proceed = input("This method will create a csv file in your current working directory. Continue? (y/n): ").lower()
    csv_name = input("Enter a name for your csv file (.csv will be appended): ")
    csv_name += '.csv'
    if proceed != 'y' and proceed != 'yes':
        print("Exiting...")
        return
    import csv
    with open(csv_name, mode='w') as matches_file:
        matches_writer = csv.writer(matches_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        if type(matches[0]) == PatternMatches:
            matches_writer.writerow(
                ['Pattern Generating Match', 'Pattern matched', 'Piece Title', 'Part', 'First Note Measure Number',
                 'Last Note Measure Number', 'Note Durations', 'EMA', 'EMA url'])
            for match_series in matches:
                for match in match_series.matches:
                    matches_writer.writerow(
                        [match_series.pattern, match.pattern, match.first_note.metadata.title, match.first_note.part,
                         match.first_note.note.measureNumber, match.last_note.note.measureNumber, match.durations,
                         match.ema, match.ema_url])
        else:
            matches_writer.writerow(
                ['Pattern Generating Match', 'Classification Type', 'EMA', 'EMA url', 'Soggetti 1 Part',
                 'Soggetti 1 Measure', 'Soggetti 2 Part', 'Soggetti 2 Measure', 'Soggetti 3 Part', 'Soggetti 3 Measure',
                 'Soggetti 4 Part', 'Soggetti 4 Measure'])
            for classified_matches in matches:
                row_array = [classified_matches.pattern, classified_matches.type, classified_matches.ema,
                             classified_matches.ema_url]
                for soggetti in classified_matches.matches:
                    row_array.append(soggetti.first_note.part)
                    row_array.append(soggetti.first_note.note.measureNumber)
                matches_writer.writerow(row_array)

    print("CSV created in your current working directory.")

ema2ex

ema2ex(emaStr, df)

Return the excerpt from the df that is designated by the emaStr. The df must have a Measure and Beat multiIndex as the index labels. You can get this by passing a regular df of an ImportedPiece to ImportedPiece.detailIndex().

Source code in crim_intervals/main.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def ema2ex(emaStr, df):
    """
    Return the excerpt from the df that is designated by the emaStr. The df 
    must have a Measure and Beat multiIndex as the index labels. You can get 
    this by passing a regular df of an ImportedPiece to 
    ImportedPiece.detailIndex().
    """
    # retrieve the measures from ema address and create start and end in place
    post = {}
    mStr, vStr, bStr = emaStr.split("/")
    voices = _emaVoiceHelper(vStr, df.columns)
    beats = _emaBeatHelper(bStr)
    for mi, measure in enumerate(_emaMeasureHelper(mStr)):
        voicesInMeasure = voices[mi]
        for vi, voice in enumerate(voicesInMeasure):
            beatsInVoice = beats[mi][vi]
            for beatRange in beatsInVoice:
                chunk = df.loc[(measure, beatRange[0]):(measure, beatRange[1]), voice]
                if voice in post:
                    post[voice].append(chunk)
                else:
                    post[voice] = [chunk]
    parts = [pd.concat(vox) for vox in post.values()]
    ret = pd.concat(parts, axis=1)
    ret.columns = post.keys()
    return ret

gatherNgrams

gatherNgrams(df, ema_col='ema', piece_col='piece.piece_id', filename='')
Source code in crim_intervals/main.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def gatherNgrams(df, ema_col='ema', piece_col='piece.piece_id', filename=''):
    url_root = 'https://crimproject.org/mei/'
    df2 = df.loc[:, [ema_col, piece_col, 'mt_cad_cantizans', 'mt_cad_tenorizans']].copy()
    df2[piece_col] = url_root + df2[piece_col] + '.mei'
    ngrams = df2.apply(_gatherNgram, axis=1, result_type='expand')
    ngrams.columns = ('Ngram', 'N', 'LowerCVF', 'UpperCVF')
    ngrams = ngrams[~ngrams.Ngram.isin((False, float('nan')))]
    if filename:
        if filename[-4:] != '.csv':
            filename += '.csv'
        path = './data/cadences/' + filename
        ngrams.to_csv(path, index=False)
    else:
        return ngrams