Пример #1
0
def wrap_sample_expdata_plot(data_dir, sample, plot_func, function_args):
    """  def plot_func(sample, exp_data) """
    with safe_flydra_db_open(data_dir) as db:
        exp_data = db.get_table(sample, EXP_DATA_TABLE)
        result = plot_func(sample, exp_data, **function_args)
        db.release_table(exp_data)
        return result
Пример #2
0
def get_saccades(flydra_db_directory, db_group, interval):
    
    with safe_flydra_db_open(flydra_db_directory) as db:
        
        saccades = db.get_table_for_group(db_group, 'saccades')
        logger.info('Found %d saccades for group %r.' % (len(saccades), db_group))
        if len(saccades) == 0:
            raise Exception('No saccades found for group %r.' % db_group)
           
        # make sure we use array
        saccades = np.array(saccades)
        
        for s in saccades:
            check_saccade_is_well_formed(s)
            
        saccades = add_position_information(saccades) # XXX: using default arena size
       
       
        def test(saccade):
            return (saccade['distance_from_wall'] >= interval[0] and
                    saccade['distance_from_wall'] <= interval[1])
        include = map(test, saccades)
        select = saccades[np.array(include)]
    
        logger.info(' of which %d saccades are in interval %g <= d <= %g.' % 
                    (len(select), interval[0], interval[1]))
        
        if len(select) == 0:
            raise Exception('No saccades in interval %g <= d <= %g' %
                            interval )
      
        return select
Пример #3
0
def main():
    parser = OptionParser(usage=description)
    parser.add_option("--peters_pickle",
                      help="Peter's pickle file", default='weir.pkl')
    parser.add_option("--db", help="FlydraDB directory")
     
    (options, args) = parser.parse_args()
    
    if not options.db:
        raise Exception('Please define FlydraDB directory using `--db`.')
     
    if args:
        raise Exception('Extraneous arguments: %r.' % args)

    if not os.path.exists(options.peters_pickle):
        raise Exception('File %r does not exist. ' % options.peters_pickle)
    
    
    print("Loading file %r ..." % options.peters_pickle)
    data = pickle.load(open(options.peters_pickle, 'rb'))
    print("...done.")

    
    with safe_flydra_db_open(options.db, create=True) as flydra_db:
        for experiment, flies in data.items():
            experiment = experiment.replace('/', '')
            for fly, fly_data in flies.items(): 
                import_sample(flydra_db, experiment, fly, fly_data)
Пример #4
0
def get_saccades_for_group(flydra_db_directory, db_group, version):
    center = ParamsEstimation.arena_center
    radius = ParamsEstimation.arena_radius 
    warnings.warn('Using hardcoded arena size and position (%s, %s)' 
                  % (center, radius))
        
    with safe_flydra_db_open(flydra_db_directory) as db:

        print('Getting all saccades from group %r.' % db_group)
        saccades = db.get_table_for_group(group=db_group,
                                          table='saccades',
                                          version=version)
        if len(saccades) == 0:
            raise Exception('No saccades found for group %r.' % db_group)
        
        print('Checking saccades are well formed.')
        for s in saccades:
            check_saccade_is_well_formed(s)
        
        print('Adding position information')
        saccades = add_position_information(saccades,
                                            arena_radius=radius,
                                            arena_center=center)  
        
        return saccades
def main():
    parser = OptionParser(usage=description)
    parser.add_option("--db", help="FlydraDB directory") 
        
    (options, args) = parser.parse_args()  # @UnusedVariable

    try:    
        if options.db is None:
            raise Exception('Please provide --db option')
        
        if len(args) != 1:
            raise Exception('Please provide exactly one argument.')
   
    except Exception as e:
        logger.error('Error while parsing configuration.')
        logger.error(str(e))
        sys.exit(-1)

    # detection parameters
    directory = args[0]
    
    try:
            
        if not os.path.exists(directory):
            raise Exception('Directory %r does not exist.' % directory)
    
        pattern = '*-saccades.h5'
        
        logger.info('Looking for files with pattern %r in directory %r.' % 
                    (pattern, directory))
        
        files = sorted(list(locate(pattern=pattern, root=directory)))
        
        if not files:
            raise Exception(
                    'No files with pattern %r found in directory %r ' % 
                            (pattern, directory))
        
        logger.info('Found %d files.' % len(files))
        
        with flydra_db.safe_flydra_db_open(options.db, create=True) as db:
            for i, filename in enumerate(files):
                saccades = saccades_read_h5(filename)
                saccades['sample_num'] = i
                logger.debug('Sample %s: %d saccades.' % 
                             (filename, len(saccades)))
                store_sample_in_flydra_db(saccades, db)
    
    except Exception as e:
        logger.error('Error while processing. Exception and traceback follow.')
        logger.error(str(e))
        logger.error(traceback.format_exc())
        sys.exit(-2)
def main():
    parser = OptionParser(usage=description)
    parser.add_option("--db", help="FlydraDB directory")

    (options, args) = parser.parse_args()  # @UnusedVariable

    try:
        if options.db is None:
            raise Exception('Please provide --db option')

        if len(args) != 1:
            raise Exception('Please provide exactly one argument.')

    except Exception as e:
        logger.error('Error while parsing configuration.')
        logger.error(str(e))
        sys.exit(-1)

    # detection parameters
    directory = args[0]

    try:

        if not os.path.exists(directory):
            raise Exception('Directory %r does not exist.' % directory)

        pattern = '*-saccades.h5'

        logger.info('Looking for files with pattern %r in directory %r.' %
                    (pattern, directory))

        files = sorted(list(locate(pattern=pattern, root=directory)))

        if not files:
            raise Exception('No files with pattern %r found in directory %r ' %
                            (pattern, directory))

        logger.info('Found %d files.' % len(files))

        with flydra_db.safe_flydra_db_open(options.db, create=True) as db:
            for i, filename in enumerate(files):
                saccades = saccades_read_h5(filename)
                saccades['sample_num'] = i
                logger.debug('Sample %s: %d saccades.' %
                             (filename, len(saccades)))
                store_sample_in_flydra_db(saccades, db)

    except Exception as e:
        logger.error('Error while processing. Exception and traceback follow.')
        logger.error(str(e))
        logger.error(traceback.format_exc())
        sys.exit(-2)
Пример #7
0
def wrap_sample_saccades_plot(data_dir, sample, configuration, plot_func, function_args):
    """  def plot_func(sample, configuration, exp_data, saccades) """
    with safe_flydra_db_open(data_dir) as db:
        if db.has_table(sample, EXP_DATA_TABLE):
            exp_data = db.get_table(sample, EXP_DATA_TABLE)
        else:
            exp_data = None
        table = db.get_table(sample, table=SACCADES_TABLE, version=configuration)
        saccades = numpy.array(table, dtype=table.dtype)
        result = plot_func(sample, exp_data, configuration, saccades, **function_args)
        db.release_table(table)
        if exp_data:
            db.release_table(exp_data)
        return result
def compute_saccade_stats(flydra_db_directory, samples, image, conditions):
    '''
    Computes the stats of an image for the saccades that respect a 
    set of conditions.
     
    db: FlydraDB directory
    samples: list of IDs
    
    condition: saccade table -> true/false selector
    '''
    with safe_flydra_db_open(flydra_db_directory) as db:
        
        progress('Computing stats', (0, 2), 'First pass')
        # first compute the mean
        group_mean = Expectation()
        group_min = None
        group_max = None
        iter = saccades_iterate_image('computing mean',
                                       db, samples, image, conditions)
        for sample, sample_saccades, values in iter: #@UnusedVariable 
            sample_mean = numpy.mean(values, axis=0)
            sample_min = numpy.min(values, axis=0)
            sample_max = numpy.max(values, axis=0)
            if group_min is None:
                group_min = sample_min
                group_max = sample_max
            else:
                group_min = numpy.minimum(sample_min, group_min)
                group_max = numpy.maximum(sample_max, group_max)
                
            group_mean.update(sample_mean, len(values))
    
        num_samples = group_mean.num_samples
        group_mean = group_mean.get_value()
    
        group_var = Expectation() 
        progress('Computing stats', (1, 2), 'Second pass')
        iter = saccades_iterate_image('computing var',
                                      db, samples, image, conditions)
        for sample, sample_saccades, values in iter: #@UnusedVariable
            err = values - group_mean
            sample_var = numpy.mean(numpy.square(err), axis=0)
            group_var.update(sample_var, len(values))
        group_var = group_var.get_value()
        
        result = Stats(mean=group_mean, var=group_var,
                       min=group_min, max=group_max, nsamples=num_samples)
        
        return result 
Пример #9
0
def main():
    np.seterr(all='raise')
    
    parser = OptionParser(usage=description)
    parser.add_option("--db", help="Main data directory")
    
    parser.add_option("--interactive", action="store_true", default=False,
                      help="Starts an interactive compmake session.")
    
    parser.add_option("--outdir", help="Output directory")
    
        
    (options, args) = parser.parse_args() #@UnusedVariable
    
    try:
        if args:
            raise Exception('Spurious arguments %r.' % args)
    
        if not options.db:
            raise Exception('Please provide --db option')
    
        if not options.outdir:
            raise Exception('Please provide --outdir option')
        
    except Exception as e:
        logger.error('Error while parsing configuration.')
        logger.error(str(e))
        sys.exit(-1)
  
  
    try:
        
        compmake_dir = os.path.join(options.outdir, 'compmake')
        use_filesystem(compmake_dir)
        
        with safe_flydra_db_open(options.db) as db:
            spontaneous_analysis(db, options.outdir)
    
    
        compmake_console()
        
    except Exception as e:
        logger.error('Error while processing. Exception and traceback follow.')
        logger.error(str(e))
        logger.error(traceback.format_exc())
        sys.exit(-2)
Пример #10
0
def get_group_density_stats(flydra_db_directory, db_group, version, cells):
    center = ParamsEstimation.arena_center
    radius = ParamsEstimation.arena_radius 
    warnings.warn('Using hardcoded arena size and position (%s, %s)' 
                    % (center, radius))
    with safe_flydra_db_open(flydra_db_directory) as db:
        rows = db.get_table_for_group(db_group, table='rows', version=version)
        print('Read %d rows' % len(rows))
    
        print('Computing extra information')

        rowsp = add_position_information_to_rows(rows,
                                                 arena_radius=radius,
                                                 arena_center=center)
        
        print('Computing histogram')
        stats = compute_histogram(rowsp, cells)
        return stats
Пример #11
0
def wrap_group_plot(data_dir, group, configuration, plot_func, function_args):
    """  def plot_func(group, configuration, saccades) """
    with safe_flydra_db_open(data_dir) as db:
        saccades = get_table_for_group(db, group, SACCADES_TABLE, configuration)
        return plot_func(group, configuration, saccades, **function_args)
def flydra_db_detect(args):
    np.seterr(all='raise')

    parser = LenientOptionParser(usage=usage)

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

    parser.add_option("--version",
                      default=None,
                      type='string',
                      help="Which version of the 'rows' table to use.")

    parser.add_option("--debug_output",
                      help="Creates debug figures.",
                      default=False,
                      action="store_true")

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

    # detection parameters
    dt = 1.0 / 60  # XXX: read from file
    warnings.warn('Using fixed dt = %s.' % dt)
    parser.add_option("--deltaT_inner_sec",
                      default=4 * dt,
                      type='float',
                      help="Inner interval [= %default]")
    parser.add_option("--deltaT_outer_sec",
                      default=10 * dt,
                      type='float',
                      help="Outer interval [= %default]")
    parser.add_option("--min_amplitude_deg",
                      default=25,
                      type='float',
                      help="Minimum saccade amplitude (deg) [= %default]")
    parser.add_option(
        "--min_linear_velocity",
        default=0.1,
        type='float',
        help="Minimum linear velocity when saccading (m/s) [= %default]")
    parser.add_option(
        "--max_linear_acceleration",
        default=20,
        type='float',
        help="Maximum linear acceleration when saccading (m/s^2) [= %default]")
    parser.add_option(
        "--max_angular_velocity",
        default=8000,
        type='float',
        help="Maximum angular velocity when saccading (deg/s) [= %default]")
    parser.add_option("--max_orientation_dispersion_deg",
                      default=15,
                      type='float',
                      help="Maximum dispersion (deg) [= %default]")
    parser.add_option("--minimum_interval_sec",
                      default=10 * dt,
                      type='float',
                      help="Minimum interval between saccades. [= %default]")

    (options, args) = parser.parse_args(args)

    if not options.db:
        raise Exception('No flydra DB directory  specified.')

    if args:
        raise Exception('Spurious arguments')

    # Create processed string
    processed = get_computed_string('geometric_saccade_detector', __version__)

    rows_table_name = 'rows'
    rows_table_version = options.version
    saccades_table_name = 'saccades'
    annotations_table_name = 'annotated'
    saccades_table_version = options.version

    with safe_flydra_db_open(options.db) as db:
        samples = db.list_samples()

        for i, sample in enumerate(samples):

            already_has = db.has_table(sample, saccades_table_name,
                                       saccades_table_version)

            if options.nocache and already_has:
                msg = ('Sample %r already has table %s:%s; skipping.' %
                       (sample, saccades_table_name, saccades_table_version))
                logger.info(msg)
                continue

            if not db.has_table(sample, rows_table_name, rows_table_version):
                msg = ('Sample %r does not have table %s:%s.' %
                       (sample, rows_table_name, rows_table_version))
                raise Exception(msg)

            with db.safe_get_table(sample, rows_table_name,
                                   rows_table_version) as rows:

                params = {
                    'deltaT_inner_sec': options.deltaT_inner_sec,
                    'deltaT_outer_sec': options.deltaT_outer_sec,
                    'min_amplitude_deg': options.min_amplitude_deg,
                    'max_orientation_dispersion_deg':
                    options.max_orientation_dispersion_deg,
                    'minimum_interval_sec': options.minimum_interval_sec,
                    'max_linear_acceleration': options.max_linear_acceleration,
                    'min_linear_velocity': options.min_linear_velocity,
                    'max_angular_velocity': options.max_angular_velocity,
                }

                rows = np.array(rows[:])
                saccades, annotated = geometric_saccade_detect(rows, params)

                for saccade in saccades:
                    check_saccade_is_well_formed(saccade)

                dt = 1.0 / 60
                logger.info(
                    "%4d/%d %s: %d saccades for %d rows (%g saccades/s)" %
                    (i, len(samples), sample, len(saccades), len(rows),
                     len(rows) * dt / len(saccades)))

                db.set_table(sample=sample,
                             table=saccades_table_name,
                             data=saccades,
                             version=saccades_table_version)

                db.set_table(sample=sample,
                             table=annotations_table_name,
                             data=annotated,
                             version=saccades_table_version)

                db.set_attr(sample,
                            'saccades_%s_processed' % saccades_table_version,
                            processed)

            # Write debug figures
            if options.debug_output:

                if not os.path.exists(options.output_dir):
                    os.makedirs(options.output_dir)
                basename = sample
                debug_output_dir = os.path.join(options.output_dir, basename)
                logger.info("Writing HTML+png to %s" % debug_output_dir)
                write_debug_output(debug_output_dir, basename, annotated,
                                   saccades)
def flydra_db_detect(args):
    np.seterr(all='raise')
                 
    parser = LenientOptionParser(usage=usage)

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

    parser.add_option("--version", default=None, type='string',
                      help="Which version of the 'rows' table to use.")
    
    parser.add_option("--debug_output", help="Creates debug figures.",
                      default=False, action="store_true")

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

    # detection parameters
    dt = 1.0 / 60 # XXX: read from file
    warnings.warn('Using fixed dt = %s.' % dt)
    parser.add_option("--deltaT_inner_sec", default=4 * dt, type='float',
                      help="Inner interval [= %default]")
    parser.add_option("--deltaT_outer_sec", default=10 * dt, type='float',
                      help="Outer interval [= %default]")
    parser.add_option("--min_amplitude_deg", default=25, type='float',
                      help="Minimum saccade amplitude (deg) [= %default]")
    parser.add_option("--min_linear_velocity", default=0.1, type='float',
                      help="Minimum linear velocity when saccading (m/s) [= %default]")
    parser.add_option("--max_linear_acceleration", default=20, type='float',
                      help="Maximum linear acceleration when saccading (m/s^2) [= %default]")
    parser.add_option("--max_angular_velocity", default=8000, type='float',
                      help="Maximum angular velocity when saccading (deg/s) [= %default]")
    parser.add_option("--max_orientation_dispersion_deg", default=15, type='float',
                      help="Maximum dispersion (deg) [= %default]")
    parser.add_option("--minimum_interval_sec", default=10 * dt, type='float',
                      help="Minimum interval between saccades. [= %default]")
    
    (options, args) = parser.parse_args(args)
    
    if not options.db:
        raise Exception('No flydra DB directory  specified.')
    
    if args:
        raise Exception('Spurious arguments')
    
        
    # Create processed string
    processed = get_computed_string('geometric_saccade_detector', __version__)

        
    rows_table_name = 'rows'
    rows_table_version = options.version
    saccades_table_name = 'saccades'
    annotations_table_name = 'annotated'
    saccades_table_version = options.version

    with safe_flydra_db_open(options.db) as db:
        samples = db.list_samples()

        for i, sample in enumerate(samples):
           
            already_has = db.has_table(sample, saccades_table_name,
                                       saccades_table_version)
                
            if options.nocache and already_has:
                msg = ('Sample %r already has table %s:%s; skipping.' % 
                       (sample, saccades_table_name, saccades_table_version))
                logger.info(msg)
                continue
            
            if not db.has_table(sample, rows_table_name, rows_table_version):
                msg = ('Sample %r does not have table %s:%s.' % 
                       (sample, rows_table_name, rows_table_version))
                raise Exception(msg)
            
            with db.safe_get_table(sample, rows_table_name,
                                   rows_table_version) as rows:
                 
                params = {
                  'deltaT_inner_sec': options.deltaT_inner_sec,
                  'deltaT_outer_sec':  options. deltaT_outer_sec,
                  'min_amplitude_deg':  options.min_amplitude_deg,
                  'max_orientation_dispersion_deg': 
                    options.max_orientation_dispersion_deg,
                  'minimum_interval_sec':  options.minimum_interval_sec,
                  'max_linear_acceleration':  options.max_linear_acceleration,
                  'min_linear_velocity': options.min_linear_velocity,
                  'max_angular_velocity': options.max_angular_velocity,
                }
                
                rows = np.array(rows[:])
                saccades, annotated = geometric_saccade_detect(rows, params)
        
                for saccade in saccades:
                    check_saccade_is_well_formed(saccade)
        
                dt = 1.0 / 60
                logger.info("%4d/%d %s: %d saccades for %d rows (%g saccades/s)" % 
                    (i, len(samples), sample,
                     len(saccades), len(rows), len(rows) * dt / len(saccades))) 
       
       
                db.set_table(sample=sample,
                             table=saccades_table_name,
                             data=saccades,
                             version=saccades_table_version)
                
                db.set_table(sample=sample,
                             table=annotations_table_name,
                             data=annotated,
                             version=saccades_table_version)
            
                db.set_attr(sample,
                            'saccades_%s_processed' % saccades_table_version,
                            processed)
            
            # Write debug figures
            if options.debug_output:
                
                if not os.path.exists(options.output_dir):
                    os.makedirs(options.output_dir)
                basename = sample
                debug_output_dir = os.path.join(options.output_dir, basename)
                logger.info("Writing HTML+png to %s" % debug_output_dir)    
                write_debug_output(debug_output_dir, basename,
                                   annotated, saccades)
def compute_stats(flydra_db_directory, samples, image):
    '''
    Computes the stats of an image.
    
    *db*
      FlydraDB directory
    
    *samples*
      list of IDs
      
    image: name of a table
    '''
    
    with safe_flydra_db_open(flydra_db_directory) as db:
         
        def data_pass(name):
            for i, id in enumerate(samples):
                progress(name, (i, len(samples)), "Sample %s" % id)
            
                if not (db.has_sample(id) and db.has_table(id, image)):
                    raise ValueError('No table "%s" for id %s' % (image, id))
                
                rows = db.get_rows(id)
                data = db.get_table(id, image)
                
                select = rows[:]['linear_velocity_modulus'] > 0.1
                #select = condition(rows)
                values = data[select]['value']
                
                yield id, values
                db.release_table(data)
                db.release_table(rows)
    
        progress('Computing stats', (0, 2), 'First pass')
        # first compute the mean
        group_mean = Expectation()
        group_min = None
        group_max = None
        for sample, values in data_pass('computing mean'): #@UnusedVariable
            sample_mean = numpy.mean(values, axis=0)
            sample_min = numpy.min(values, axis=0)
            sample_max = numpy.max(values, axis=0)
            if group_min is None:
                group_min = sample_min
                group_max = sample_max
            else:
                group_min = numpy.minimum(sample_min, group_min)
                group_max = numpy.maximum(sample_max, group_max)
                
            group_mean.update(sample_mean, len(values))
    
        num_samples = group_mean.num_samples
        group_mean = group_mean.get_value()
    
        group_var = Expectation() 
        progress('Computing stats', (1, 2), 'Second pass')
        for sample, values in data_pass('computing var'): #@UnusedVariable
            err = values - group_mean
            sample_var = numpy.mean(numpy.square(err), axis=0)
            group_var.update(sample_var, len(values))
        group_var = group_var.get_value()
        
        return Stats(mean=group_mean, var=group_var,
                       min=group_min, max=group_max, nsamples=num_samples)
Пример #15
0
def detect_angvel(argv):
    np.seterr(all='raise')
                 
    parser = LenientOptionParser(usage=usage)

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

    parser.add_option("--version", default='smooth', type='string',
                help="[=%default] Which version of the 'rows' table to use.")
     
    parser.add_option("--saccades_table_version", default='angvel',
                      type='string',
                      help='[=%default] Version of output saccades tables.')

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

    parser.add_option("--out", help="Output directory for graphic representation.")

    # detection parameters
    dt = 1.0 / 60  # XXX: read from file
    warnings.warn('Using fixed dt = %s.' % dt)
    parser.add_option("--deltaT_inner_sec", default=4 * dt, type='float',
                      help="Inner interval [= %default]")
    parser.add_option("--deltaT_outer_sec", default=10 * dt, type='float',
                      help="Outer interval [= %default]")
    parser.add_option("--min_amplitude_deg", default=25, type='float',
                      help="Minimum saccade amplitude (deg) [= %default]")
    parser.add_option("--min_linear_velocity", default=0.1, type='float',
                      help="Minimum linear velocity when"
                      " saccading (m/s) [= %default]")
    parser.add_option("--max_linear_acceleration", default=20, type='float',
                      help="Maximum linear acceleration when saccading "
                      "(m/s^2) [= %default]")
    parser.add_option("--max_angular_velocity", default=8000, type='float',
                      help="Maximum angular velocity when saccading"
                      " (deg/s) [= %default]")
    parser.add_option("--max_orientation_dispersion_deg", default=15,
                      type='float',
                      help="Maximum dispersion (deg) [= %default]")
    parser.add_option("--minimum_interval_sec", default=10 * dt, type='float',
                      help="Minimum interval between saccades. [= %default]")
    
    (options, args) = parser.parse_args(argv)
    
    if not options.db:
        raise Exception('No flydra DB directory  specified.')
    
    if args:
        raise Exception('Spurious arguments')
    
    # Create processed string
    processed = get_computed_string('angvel_saccade_detector', __version__)
        
    rows_table_name = NamingConventions.ROWS_TABLE
    rows_table_version = options.version
    saccades_table_name = NamingConventions.SACCADES_TABLE
    # annotations_table_name = 'annotated'
    saccades_table_version = options.saccades_table_version

    with safe_flydra_db_open(options.db) as db:
        samples = db.list_samples()

        for i, sample in enumerate(samples):
           
            already_has = db.has_table(sample, saccades_table_name,
                                       saccades_table_version)
                
            if options.nocache and already_has:
                msg = ('Sample %r already has table %s:%s; skipping.' % 
                       (sample, saccades_table_name, saccades_table_version))
                logger.info(msg)
                continue
            
            if not db.has_table(sample, rows_table_name, rows_table_version):
                msg = ('Sample %r does not have table %s:%s.' % 
                       (sample, rows_table_name, rows_table_version))
                raise Exception(msg)
            
            with db.safe_get_table(sample, rows_table_name,
                                   rows_table_version) as rows:
                 
                rows = np.array(rows[:])
                params = {}
#                saccades, annotated 
                data = angvel_saccade_detect(rows, **params)
                saccades = data['saccades'] 
                
                for saccade in saccades:
                    check_saccade_is_well_formed(saccade)
        
                DT = rows['timestamp'][-1] - rows['timestamp'][0]  
                logger.info("%4d/%d %s: %6d saccades for %6d rows (%6g saccades/s)" % 
                    (i, len(samples), sample,
                     len(saccades), len(rows), DT / len(saccades))) 
       
                if True:
                    db.set_table(sample=sample,
                                 table=saccades_table_name,
                                 data=saccades,
                                 version=saccades_table_version)
                     
                    db.set_attr(sample,
                                'saccades_%s_processed' % saccades_table_version,
                                processed)
                
                if options.out is not None:
                    outdir = os.path.join(options.out, 'angvel_sac_detect')
                    if not os.path.exists(outdir):
                        os.makedirs(outdir)
                    resources = os.path.join(outdir, 'images')
                    filename = os.path.join(outdir, '%s.html' % sample)
                 
                    r = Report()
                    
                    chunks = enumerate_chunks(len(rows), max_chunk_size=300)
                    for i, select in enumerate(chunks):
                        rows_i = rows[select]
                    
                        ri = plot_angvel_saccade_detect_results(rows_i)
                        ri.nid = 'chunk_%s' % i
                        r.add_child(ri)
                    
                    logger.info('Writing to %r.' % filename)
                    r.to_html(filename, resources_dir=resources)
Пример #16
0
def detect_angvel(argv):
    pdb.set_trace()

    np.seterr(all='raise')

    parser = LenientOptionParser(usage=usage)

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

    parser.add_option(
        "--version",
        default='smooth',
        type='string',
        help="[=%default] Which version of the 'rows' table to use.")

    parser.add_option("--saccades_table_version",
                      default='angvel',
                      type='string',
                      help='[=%default] Version of output saccades tables.')

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

    parser.add_option("--out",
                      help="Output directory for graphic representation.")

    parser.add_option("--fps", help="Recording framerate")
    fps = parser.parse_args(args='--fps', type='float')

    # detection parameters
    dt = 1.0 / 60  # XXX: read from file
    warnings.warn('Using fixed dt = %s.' % dt)
    parser.add_option("--deltaT_inner_sec",
                      default=4 * dt,
                      type='float',
                      help="Inner interval [= %default]")
    parser.add_option("--deltaT_outer_sec",
                      default=10 * dt,
                      type='float',
                      help="Outer interval [= %default]")
    parser.add_option("--min_amplitude_deg",
                      default=25,
                      type='float',
                      help="Minimum saccade amplitude (deg) [= %default]")
    parser.add_option("--min_linear_velocity",
                      default=0.1,
                      type='float',
                      help="Minimum linear velocity when"
                      " saccading (m/s) [= %default]")
    parser.add_option("--max_linear_acceleration",
                      default=20,
                      type='float',
                      help="Maximum linear acceleration when saccading "
                      "(m/s^2) [= %default]")
    parser.add_option("--max_angular_velocity",
                      default=8000,
                      type='float',
                      help="Maximum angular velocity when saccading"
                      " (deg/s) [= %default]")
    parser.add_option("--max_orientation_dispersion_deg",
                      default=15,
                      type='float',
                      help="Maximum dispersion (deg) [= %default]")
    parser.add_option("--minimum_interval_sec",
                      default=10 * dt,
                      type='float',
                      help="Minimum interval between saccades. [= %default]")

    (options, args) = parser.parse_args(argv)

    if not options.db:
        raise Exception('No flydra DB directory  specified.')

    if args:
        raise Exception('Spurious arguments')

    # Create processed string
    processed = get_computed_string('angvel_saccade_detector', __version__)

    rows_table_name = NamingConventions.ROWS_TABLE
    rows_table_version = options.version
    saccades_table_name = NamingConventions.SACCADES_TABLE
    # annotations_table_name = 'annotated'
    saccades_table_version = options.saccades_table_version

    with safe_flydra_db_open(options.db) as db:
        samples = db.list_samples()

        for i, sample in enumerate(samples):

            already_has = db.has_table(sample, saccades_table_name,
                                       saccades_table_version)

            if options.nocache and already_has:
                msg = ('Sample %r already has table %s:%s; skipping.' %
                       (sample, saccades_table_name, saccades_table_version))
                logger.info(msg)
                continue

            if not db.has_table(sample, rows_table_name, rows_table_version):
                msg = ('Sample %r does not have table %s:%s.' %
                       (sample, rows_table_name, rows_table_version))
                raise Exception(msg)

            with db.safe_get_table(sample, rows_table_name,
                                   rows_table_version) as rows:

                rows = np.array(rows[:])
                params = {}
                #                saccades, annotated
                data = angvel_saccade_detect(rows, **params)
                saccades = data['saccades']

                for saccade in saccades:
                    check_saccade_is_well_formed(saccade)

                DT = rows['timestamp'][-1] - rows['timestamp'][0]
                logger.info(
                    "%4d/%d %s: %6d saccades for %6d rows (%6g saccades/s)" %
                    (i, len(samples), sample, len(saccades), len(rows),
                     DT / len(saccades)))

                if True:
                    db.set_table(sample=sample,
                                 table=saccades_table_name,
                                 data=saccades,
                                 version=saccades_table_version)

                    db.set_attr(
                        sample,
                        'saccades_%s_processed' % saccades_table_version,
                        processed)

                if options.out is not None:
                    outdir = os.path.join(options.out, 'angvel_sac_detect')
                    if not os.path.exists(outdir):
                        os.makedirs(outdir)
                    resources = os.path.join(outdir, 'images')
                    filename = os.path.join(outdir, '%s.html' % sample)

                    r = Report()

                    chunks = enumerate_chunks(len(rows), max_chunk_size=300)
                    for i, select in enumerate(chunks):
                        rows_i = rows[select]

                        ri = plot_angvel_saccade_detect_results(rows_i)
                        ri.nid = 'chunk_%s' % i
                        r.add_child(ri)

                    logger.info('Writing to %r.' % filename)
                    r.to_html(filename, resources_dir=resources)
def bet_on_flies(flydra_db_directory, samples, image, saccades_set):
    
    kernels = {}
    for degrees in [15, 30, 45, 60, 90, 180]:
        # kernels['mf%d' % degrees] = create_matched_filter(degrees, [-45, 0], False)
        kernels['mf%d' % degrees] = create_matched_filter(degrees, [-20, 20], False)

        #kernels['pf%d' % degrees] = create_matched_filter(degrees, True)
         
    results = {}
    with safe_flydra_db_open(flydra_db_directory) as db:
        conditions = [saccades_set.args]
        
        # looking for the optimal dividing plane
        ex = {-1: Expectation(), +1: Expectation()}
        for sample, sample_saccades, image_values in saccades_iterate_image(#@UnusedVariable
                'computing optimal kernel', db, samples, image, conditions):
            for s in [-1, +1]:
                values = image_values[sample_saccades['sign'] == s]
                
                if len(values) > 0:
                    ex[s].update(values.mean(axis=0), len(values))
        
        kernels['optimal'] = ex[+1].get_value() - ex[-1].get_value()  
    
        dir = '${SNPENV_DATA}/flydra_db/out/saccade_view_joint_analysis/lasvegas/images/'
        others = {
         'center': 'lasvegas_contrast_w_posts_center:optimal:kernel.pickle',
         'border': 'lasvegas_contrast_w_posts_border:optimal:kernel.pickle',
         'allsac': 'lasvegas_contrast_w_posts_allsac:optimal:kernel.pickle'
        }
        
        for name, f in others.items():
            filename = os.path.expandvars(os.path.join(dir, f))
            kernel = cPickle.load(open(filename, 'rb'))
            #mask_deg = create_matched_filter(75, [-60, 60],True)
            mask_deg = create_matched_filter(75, [-90, 90], True)
            kernel = kernel * numpy.abs(mask_deg)
            
            kernels['common_%s' % name] = kernel
            
    
        
        for i, kernel_name in enumerate(kernels.keys()):            
            progress('betting', (i, len(kernels)), kernel_name)
            kernel = kernels[kernel_name]
            
            signs = []
            response = []
            overlap = []
            for sample, sample_saccades, image_values in saccades_iterate_image(#@UnusedVariable
                'computing response', db, samples, image, conditions):
                    
                s = sample_saccades[:]['sign']
                    
                signs.extend(s)
                
                for i in range(len(image_values)): 
                    
                    response_i = (image_values[i, :] * kernel).sum()
                    response.append(response_i)
                    
                    overlap_i = (image_values[i, :] * 
                                    numpy.abs(kernel)).sum()
                    overlap.append(overlap_i)
        
            signs = numpy.array(signs)
            response = numpy.array(response)
            results[kernel_name] = { 'signs': signs, 'response': response,
                                     'kernel': kernel, 'overlap': overlap} 
            

    return results