Package jazzparser :: Package data :: Module tonalspace
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.data.tonalspace

  1  """Storage of tonal space analyses in a (relatively) quickly-accessible corpus. 
  2   
  3  Use the script bin/data/parsegs.py to create and store these sets from the  
  4  chord corpus. 
  5   
  6  """ 
  7  """ 
  8  ============================== License ======================================== 
  9   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
 10    
 11   This file is part of The Jazz Parser. 
 12    
 13   The Jazz Parser is free software: you can redistribute it and/or modify 
 14   it under the terms of the GNU General Public License as published by 
 15   the Free Software Foundation, either version 3 of the License, or 
 16   (at your option) any later version. 
 17    
 18   The Jazz Parser is distributed in the hope that it will be useful, 
 19   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 21   GNU General Public License for more details. 
 22    
 23   You should have received a copy of the GNU General Public License 
 24   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
 25   
 26  ============================ End license ====================================== 
 27   
 28  """ 
 29  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
 30   
 31  import os 
 32  import cPickle as pickle 
 33  from jazzparser import settings 
 34   
 35  FILE_EXTENSION = "anal" 
36 37 -class TonalSpaceAnalysisSet(object):
38 """ 39 Data structure to hold and store a set of tonal space analyses. 40 41 """
42 - def __init__(self, analyses, name="unnamed"):
43 """ 44 @type analyses: list of (string,analysis) pairs 45 @param analyses: pairings of song names and analyses 46 47 """ 48 self.analyses = [(str(song).lower(),anal) for (song,anal) in analyses] 49 self.name = name
50
51 - def get_analyses(self, song_name):
52 return [anal for (song,anal) in self.analyses if song==song_name.lower()]
53
54 - def __get_songs(self):
55 return zip(*self.analyses)[0]
56 songs = property(__get_songs) 57
58 - def __len__(self):
59 return len(self.analyses)
60
61 - def __getitem__(self, index):
62 return self.analyses[index]
63 64 ######### Storage machinery ############ 65 @staticmethod
66 - def _get_filename(name):
67 return os.path.join(settings.ANALYSIS_DATA_DIR, "%s.%s" % (name, FILE_EXTENSION))
68 - def __get_my_filename(self):
69 return type(self)._get_filename(self.name)
70 _filename = property(__get_my_filename) 71 72 @classmethod
73 - def list(cls):
74 """ Returns a list of the names of available stored sets. """ 75 datadir = settings.ANALYSIS_DATA_DIR 76 if not os.path.exists(datadir): 77 return [] 78 # Get a listing of the data directory 79 names = [name.rpartition(".") for name in os.listdir(datadir)] 80 # Return the names of all the files with the correct extension 81 return [name for name,__,ext in names if ext == FILE_EXTENSION]
82
83 - def save(self):
84 """ Saves the set data to a file. """ 85 # Get a picklable form of the set 86 data = { 87 'analyses' : self.analyses, 88 } 89 data = pickle.dumps(data, 2) 90 filename = self._filename 91 # Check the directory exists 92 filedir = os.path.dirname(filename) 93 if not os.path.exists(filedir): 94 os.mkdir(filedir) 95 # Write the data into the file 96 f = open(filename, 'w') 97 f.write(data) 98 f.close()
99
100 - def delete(self):
101 """ Removes all the data for the set. """ 102 fn = self._filename 103 if os.path.exists(fn): 104 os.remove(fn)
105 106 @classmethod
107 - def load(cls, name):
108 filename = cls._get_filename(name) 109 # Load the data from a file 110 if os.path.exists(filename): 111 f = open(filename, 'r') 112 data = f.read() 113 data = pickle.loads(data) 114 f.close() 115 else: 116 raise TonalSpaceAnalysisSetLoadError, "the tonal space analysis "\ 117 "set '%s' does not exist" % name 118 # Create the object from the loaded data 119 obj = TonalSpaceAnalysisSet(data['analyses'], name=name) 120 return obj
121
122 -class TonalSpaceAnalysisSetLoadError(Exception):
123 pass
124