<jupyter_start><jupyter_text>Creating Trajectory and Graph Objects, Store Local In this Notebook we learn how to save our data as pickle files locally. This will help to create later on a CSV files with all the attributes we will need for prediction techniques. Remember we are saving objects so their size is considerable.  Import Files & Modules<jupyter_code>from Tonnetz_Select import fromMidiToPCS as fmpc
from Tonnetz_Select import analysisFromCorpus
from TrajectoryCalculationsWithClass import NewTrajectory, TrajectoryLookBefore
from graph_creation import CreateGraph
from FirstNotePosition import PlaceFirstNote
from structural_functions import mergeDicts
import os
import pickle
from itertools import islice, groupby
from operator import itemgetter
import music21 as ms
import pretty_midi as ptm<jupyter_output><empty_output><jupyter_text>Use pickle to Save Files with ".p" extension<jupyter_code>def pickleSave(dictOfGraphs, Composer_Name) :
    completeName = "Comparison_Results/GraphObjects_new/"+ Composer_Name + ".p"
    with open(completeName, 'wb') as config_dictionary_file:
      # Step 3
      pickle.dump(dictOfGraphs, config_dictionary_file)<jupyter_output><empty_output><jupyter_text>Functions to find the Midi attributes :<jupyter_code>def findInstruments(file):
    instruments = []
    time_signatures = []
    s = ms.converter.parse(file)
    for element in s.recurse() :
        if 'TimeSignature' in element.classes :
            time_signatures.append(element.ratioString)
        if 'Instrument' in element.classes :
            x = str(element)
            if x != '' :
                if ': ' in x :
                    newstring = x.split(': ', -1)[1]
                    instruments.append(newstring)
                else :
                    instruments.append(x)
    time_signatures = list(map(itemgetter(0), groupby(time_signatures)))
    return instruments, time_signatures
def TempoAdd(directory):
    s = ptm.PrettyMIDI(directory)
    tempo = s.estimate_tempo()
    return int(tempo)<jupyter_output><empty_output><jupyter_text>The following Definitions are defines in python files but we added some details (found in AutoHarmonic Analysis)<jupyter_code>def GraphOfNewPiece(newPiece, directory):
    '''Build a graph Object from a midi file.'''
    # extract the directory and the file extension from the piece
    shortfilename = os.path.splitext(os.path.basename(newPiece))[0]
    # Choose from where to parse (directory, corpus)
    if directory == 'corpus':
        file = ms.corpus.parse(newPiece)
        chordList, Tonnetz = analysisFromCorpus(file)
    # add the instruments
        instruments, time_signatures = findInstruments(newPiece)
        tempo = []
    else:
        if newPiece.endswith(".mid") or newPiece.endswith(".MID"):
            complete_name = directory + '/' + newPiece
            chordList, Tonnetz = fmpc(complete_name)
        # add the instruments
            instruments, time_signatures = findInstruments(complete_name)
            tempo = TempoAdd(complete_name)
    firstPoint = PlaceFirstNote(chordList, Tonnetz)
    # build 2 different trajectories
    trajectoryFut = NewTrajectory(chordList, Tonnetz, firstPoint)
    trajectoryFut.addType('Trajectory With Future')
    trajectoryFut.addInstruments(instruments)
    trajectoryFut.addTime_signatures(time_signatures)
    trajectoryFut.addTempo(tempo)
    graphFut = CreateGraph(trajectoryFut)
    graphFut.addName(shortfilename)
    
    trajectoryRec = TrajectoryLookBefore(chordList, Tonnetz, firstPoint)
    trajectoryRec.addType('Recursive Trajectory')
    trajectoryRec.addInstruments(instruments)
    trajectoryRec.addTime_signatures(time_signatures)
    trajectoryRec.addTempo(tempo)
    graphRec = CreateGraph(trajectoryRec)
    graphRec.addName(shortfilename)
    
    return graphFut, graphRec
def GetWorksByComposer(composerName, composer, style):
    listOfGraphs = []
#     composer, style, harmony = inputQuestions()
    listofWorks = ms.corpus.getComposer(composerName)
    if len(listofWorks) > 60 :
        listofWorks = list(islice(listofWorks, 60))
    if len(listofWorks) > 0:
        for piece in listofWorks:
            try:
                print("Building Trajectory for ", piece)
                graphFut, graphRec = GraphOfNewPiece(piece, 'corpus')
#                 addingLabels(composer, style, harmony, graph)
                graphFut.addStyle(style)
                graphFut.addComposer(composer)
                graphRec.addStyle(style)
                graphRec.addComposer(composer)
                listOfGraphs.append([graphFut, graphRec])
            except BaseException:
                print("--> Cannot build Trajectory for ", piece)
    return listOfGraphs
def GetWorksByDirectory(directory, composer, style):
    listOfGraphs = []
#     composer, style, harmony = inputQuestions()
    for file in os.listdir(directory):
        if file.endswith(".mid") or file.endswith(
                ".MID") or file.endswith(".mxl") or file.endswith(".xml"):
            try:
                print("Building Trajectory for ", file)
                graphFut, graphRec = GraphOfNewPiece(file, directory)
#                 addingLabels(composer, style, harmony, graph)
                graphFut.addStyle(style)
                graphFut.addComposer(composer)
                graphRec.addStyle(style)
                graphRec.addComposer(composer)
                listOfGraphs.append([graphFut, graphRec])
            except BaseException:
                print("--> Cannot build Trajectory for ", file)
    return listOfGraphs<jupyter_output><empty_output><jupyter_text>Begin Process (define Directory, composer and style)<jupyter_code>mozart_quartets = GetWorksByDirectory('Midi_files/mozart_string_quartets', 'mozart', 'classical')
haydn_quartets = GetWorksByDirectory('Midi_files/haydn_string_quartets', 'haydn', 'classical')
pickleSave(mozart_quartets, 'mozart_quartets')
pickleSave(haydn_quartets, 'haydn_quartets')
bach = GetWorksByComposer('bach', 'bach', 'barok')
beethoven = GetWorksByDirectory('Midi_files/Classical/Beethoven', 'beethoven', 'classical')
beethovenCorpus = GetWorksByComposer('beethoven', 'beethoven', 'classical')
chopin = GetWorksByDirectory('Midi_files/Classical/Chopin', 'chopin', 'classical')
chopinCorpus = GetWorksByComposer('chopin', 'chopin', 'classical')
mozart = GetWorksByDirectory('Midi_files/Classical/Mozart', 'mozart', 'classical')
mozartCorpus = GetWorksByComposer('mozart', 'mozart', 'classical')
schumann = GetWorksByDirectory('Midi_files/Classical/Schumann', 'schumann', 'classical')
schumannCorpus = GetWorksByComposer('schumann', 'schumann', 'classical')
palestrina = GetWorksByComposer('palestrina', 'palestrina', 'renaissance')
monteverdi = GetWorksByComposer('monteverdi', 'monteverdi', 'renaissance')
jazz = GetWorksByDirectory('Midi_files/Standard_Jazz/Random', 'Unknown', 'jazz')<jupyter_output><empty_output><jupyter_text>Storing<jupyter_code>pickleSave(chopin, 'chopin')
pickleSave(chopinCorpus, 'chopinCorpus')

pickleSave(beethoven, 'beethoven')
pickleSave(beethovenCorpus, 'beethovenCorpus')

pickleSave(schumann, 'schumann')
pickleSave(schumannCorpus, 'schumannCorpus')

pickleSave(mozart, 'mozart')
pickleSave(mozartCorpus, 'mozartCorpus')

pickleSave(palestrina, 'palestrina')

pickleSave(monteverdi, 'monteverdi')

pickleSave(bach, 'bach')

pickleSave(jazz, 'jazz')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09649122807017543, 'fraction_numerical': 0.0017543859649122807, 'mean_word_length': 4.038069340584636, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '359367', 'license': 'MIT', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2264, 'n_words': 482, 'path': '09_Re-Formating Trajectories with Objects.ipynb', 'repo_name': 'melkisedeath/Harmonic_Analysis_and_Trajectory', 'size': '7410'}