class FlydraData(Generator):
    ''' This block outputs the data from a FlydraDB for 
        a particular sample. '''
    Block.alias('flydra_db_source')
        
    Block.config('db', 'Database directory')
    Block.config('sample', 'Sample ID -- such as "DATA20080611_191809".')
    
    for f in fields:
        Block.output(f)
    
    Block.output('stimulus_xml')
    
    def init(self):
        self.db = FlydraDB(self.config.db, False)
        self.rows = self.db.get_rows(self.config.sample)
        self.next_index = 0

    def update(self):
        row = self.rows[self.next_index]
        t = row['time']
        
        for field in fields:
            self.set_output(field, value=row[field].copy(), timestamp=t)
        
        if self.db.has_attr(self.config.sample, 'stimulus_xml'):
            stim_xml = self.db.get_attr(self.config.sample, 'stimulus_xml')
            self.set_output('stimulus_xml', stim_xml, timestamp=t)
        
        self.next_index += 1
        if self.next_index == len(self.rows):
            self.next_index = None
        
    def next_data_status(self):
        # TODO: put new interface
        if self.next_index is None: # EOF
            return (False, None)
        else:
            return (True, self.rows[self.next_index]['time'])
def main():
    
    parser = OptionParser(usage=description)

    parser.add_option("--db", default='flydra_db', help="FlydraDB directory")

    parser.add_option("--nocache", help="Ignores already computed results.",
                      default=False, action="store_true")
    
    parser.add_option("--white", help="Computes luminance_w, with the arena"
                      " painted white.", default=False, action="store_true")
    
    parser.add_option("--host", help="Use a remote rfsee. Otherwise," 
                      "use local process.", default=None)
    
    (options, args) = parser.parse_args()
    

    if options.db is None:
        logger.error('Please specify a directory using --db.')
        sys.exit(-1)
        
        
    db = FlydraDB(options.db)
    
    if args:
        do_samples = args
        
    else:
        # look for samples with the rows table
        all_samples = db.list_samples()
        do_samples = filter(lambda x: db.has_saccades(x) and 
                                      db.has_attr(x, 'stimulus_xml'),
                            all_samples)
        logger.info('Found %d/%d samples with saccades and stimulus info.' % 
                    (len(do_samples), len(all_samples)))
    
    image = 'luminance_w' if options.white else 'luminance'
        
    target_start = 'saccades_view_start_%s' % image
    target_stop = 'saccades_view_stop_%s' % image
    target_rstop = 'saccades_view_rstop_%s' % image
    target_sstop = 'saccades_view_sstop_%s' % image
    target_random = 'saccades_view_random_%s' % image
    
    for i, sample_id in enumerate(do_samples):
        
        logger.info('Sample %s/%s: %s' % (i + 1, len(do_samples), sample_id))
        
        if not db.has_sample(sample_id):
            raise Exception('Sample %s not found in db.' % sample_id)
        
        if not db.has_saccades(sample_id):
            raise Exception('Sample %s does not have saccades table.' % sample_id)
        
        if not db.has_attr(sample_id, 'stimulus_xml'):
            raise Exception('Sample %s does not have the stimulus'
                            ' information ("stimulus_xml")' % sample_id)
       
        # todo: check stale dependencies
        if db.has_table(sample_id, target_start) and \
            db.has_table(sample_id, target_stop) and \
            db.has_table(sample_id, target_rstop) and \
            db.has_table(sample_id, target_sstop) and \
            db.has_table(sample_id, target_random) and \
            not options.nocache:
            logger.info('Targets already computed for %s; skipping' % sample_id)
            continue
        
        # Get the stimulus description
        stimulus_xml = db.get_attr(sample_id, 'stimulus_xml')
        saccades = db.get_saccades(sample_id)
        
        view_start, view_stop, view_rstop, view_random, view_sstop = \
            render_saccades_view(
                saccades=saccades,
                stimulus_xml=stimulus_xml,
                host=options.host,
                white=options.white)
   
        db.set_table(sample_id, target_start, view_start)
        db.set_table(sample_id, target_stop, view_stop)
        db.set_table(sample_id, target_rstop, view_rstop)
        db.set_table(sample_id, target_random, view_random)
        db.set_table(sample_id, target_sstop, view_sstop)
        
        db.release_table(saccades)
        
    db.close()
Example #3
0
def main():
    
    parser = OptionParser()
    
    parser.add_option("--db", default='flydra_db', help="FlydraDB directory")

    parser.add_option("--nocache", help="Ignores already computed results.",
                      default=False, action="store_true")

    parser.add_option("--compute_mu", help="Computes mu and optic flow.",
                      default=False, action="store_true")
    
    parser.add_option("--white", help="Computes luminance_w, with the arena"
                      " painted white.", default=False, action="store_true")
    
    parser.add_option("--host", help="Use a remote rfsee. Otherwise, use local process.",
                       default=None)
    
    (options, args) = parser.parse_args()
     
        
    db = FlydraDB(options.db, False)
    
    if args:
        do_samples = args
    else:
        # look for samples with the rows table
        do_samples = db.list_samples()
        do_samples = filter(lambda x: db.has_rows(x), do_samples)
    
    if options.white:
        target = 'luminance_w'
    else:
        target = 'luminance'
    
    for i, sample_id in enumerate(do_samples):
        
        print 'Sample %s/%s: %s' % (i + 1, len(do_samples), sample_id)
        
        if not db.has_sample(sample_id):
            raise Exception('Sample %r not found in db.' % sample_id)
        
        if not db.has_rows(sample_id):
            raise Exception('Sample %r does not have rows table.' % sample_id)
       
        if not db.has_attr(sample_id, 'stimulus_xml'):
            raise Exception('Sample %r does not have the "stimulus_xml" attribute.'
                            %sample_id)
       
        if options.compute_mu:
            if db.has_table(sample_id, 'nearness') and not options.nocache:
                logger.info('Already computed nearness for %r; skipping' % sample_id)
                continue
        else:
            if db.has_table(sample_id, target) and not options.nocache:
                logger.info('Already computed luminance for %r; skipping' % sample_id)
                continue
        
        rows = db.get_rows(sample_id)
        
        stimulus_xml = db.get_attr(sample_id, 'stimulus_xml')
        
        results = render(rows, stimulus_xml, host=options.host,
                         compute_mu=options.compute_mu, white=options.white)
   
        db.set_table(sample_id, target, results['luminance'])
        
        if options.compute_mu:
            db.set_table(sample_id, 'nearness', results['nearness'])
            db.set_table(sample_id, 'retinal_velocities',
                         results['retinal_velocities'])
        
        db.release_table(rows)    
   
    db.close()