示例#1
0
 def read_datanode_json(self, dnodepath):
     # check path???
     print dnodepath
     self._datanode = DataNode(metadata=load_any_json(str(dnodepath)))
     print self._datanode
     if int(self._datanode.dims[1]) is None:
         # first dim is # of frames
         self._update_metadata('cols', 1)
         self._update_metadata('frames', int(self._datanode.dims[1]))
     else:
         self._update_metadata('cols', int(self._datanode.dims[0]))
         self._update_metadata('frames', int(self._datanode.dims[1]))
# '~/comp/corpusdb2/fulltest/snd' # this is where the sounds are!

###############################################################

from corpusdb2.nodegraph import BregmanNodeGraph
from corpusdb2.datacollection import DataNode, DataNodeCollection
from bregman.features import *
import glob, itertools

ROOT = "/Users/kfl/comp/corpusdb2/fulltest"

dn = DataNode()

snds = []
for filename in glob.glob(os.path.join(ROOT, "snd", "*.wav")):
    snds += [filename]

snd = os.path.basename(snds[0])

# a single node graph
myNodeGraph = BregmanNodeGraph(metadata={"rootpath": ROOT, "filename": snd, "feature": LinearFrequencySpectrum})

dn.pull_to_datanode_and_save(nodegraph=myNodeGraph)


###############################################################

from corpusdb2.nodegraph import BregmanNodeGraph
from corpusdb2.datacollection import DataNode, DataNodeCollection
from bregman.features import *
import glob, itertools
示例#3
0
class Segmentation(object):
    """
    A Segmentation consists of frame segments.
    Each segment has a start, end, and implicit duration in frames.
    """
    def __init__(self, dnodepath, metadata=None):
        if dnodepath is None:
            return "Error: a data node JSON metadata path is required."
        self._initialize(metadata)
        self._datanode = None
        self.frame_spans = []
        self.read_datanode_json(dnodepath)

    def _initialize(self, metadata):
        """
        Initialize + check metadata.
        """
        # TODO:
        # self.reset()
        self.metadata = self.default_metadata()
        self._check_metadata(metadata)
    
    @staticmethod
    def default_metadata():
        """
        	These entries are provisional and should be available for checking later whether the analysis has been done.
        """
        metadata = {
            'rootpath' : '~/comp/corpusdb2/fulltest/',
            'dir' : 'seg',
            'datanode' : '',
            'overlap' : 0,
            'frames' : -1,
            'cols' : -1,
            'duration' : -1
        }
        return metadata

    def _check_metadata(self, metadata=None):
        self.metadata = metadata if metadata is not None else self.metadata
        md = self.default_metadata()
        for k in md.keys():
            self._update_metadata(k, self.metadata.get(k, md[k]))
        return self.metadata

    def _update_metadata(self, k, val):
        print '_update'
        self.metadata[str(k)] = val
        self.__setattr__(k, self.metadata[k])
    
    def read_datanode_json(self, dnodepath):
        # check path???
        print dnodepath
        self._datanode = DataNode(metadata=load_any_json(str(dnodepath)))
        print self._datanode
        if int(self._datanode.dims[1]) is None:
            # first dim is # of frames
            self._update_metadata('cols', 1)
            self._update_metadata('frames', int(self._datanode.dims[1]))
        else:
            self._update_metadata('cols', int(self._datanode.dims[0]))
            self._update_metadata('frames', int(self._datanode.dims[1]))
            

#     def get_full_segpath(self, data_md, mflag=False, alt=None):
#         # basename, just in case?
#         dir = 'seg'
#         if alt is not None:
#             dir = str(alt)
#         filename = os.path.basename(self.filename)
#         extstring = self.available_features[self.feature.__class__.__name__] # well aren't we clever
# #         print 'dir: ', dir
#         if mflag:
#             extstring += ".json"
#         return os.path.join(
#             os.path.expanduser(self.rootpath),
#             dir,
#             (str(filename)+extstring))

    def time_spans_to_frames(self, span_list):
        pass

    def frames_to_time_spans(self, frame_list):
        pass
    
    def __getitem__(self, index):
        return self.frame_spans[index]

    def __setitem__(self, index, segment):
        self.check_segment_data(segment)
        self.frame_spans[index]=segment
    
    def __delitem__(self, index):
        if self.frame_spans[index] is not None:
            del self.frame_spans[index]
        else:
            raise IndexError("Attempting to delete an item that does not exist.")

    def __len__(self):
        return len(self.frame_spans)

    def check_segment_data(self, segment):
    	""" Only accept FrameSegments (for now). """
        if type(segment) is not FrameSegment:
            raise ValueError("Segmentation requires a Segment")
    
    # do all error- and logic-checking in caller, for now...
    def append(self, segment):
    	""" Append a FrameSegment and return the size + resulting frame spans. """
    	# check that segment data is a FrameSegment type
        self.check_segment_data(segment)
        self.frame_spans.append(segment)
        return (len(self.frame_spans), self.frame_spans)

    def assign_single_full_segment(self):
    	""" Map all frames from an audio file to a single segment. """
        self.append(FrameSegment(0,self.frames, features=self._datanode.load_data()))
        return self
    
    def assign_segments_by_hierarchical_clustering(self):
        pass
    
    def __repr__(self):
        return self.frame_spans.__repr__()