def compute_contrast_for_table(luminance, kernel): contrast = numpy.ndarray(shape=luminance.shape, dtype=luminance.dtype) contrast[:]['time'] = luminance[:]['time'] contrast[:]['frame'] = luminance[:]['frame'] contrast[:]['obj_id'] = luminance[:]['obj_id'] num = len(luminance) pbar = progress_bar('Computing contrast', num) for i in xrange(num): pbar.update(i) y = luminance[i]['value'].astype('float32') c = intrinsic_contrast(y, kernel) contrast[i]['value'][:] = c return contrast
def main(): sigma_deg = 6 kernel1 = get_contrast_kernel(sigma_deg=sigma_deg, eyes_interact=True) kernel2 = get_contrast_kernel(sigma_deg=sigma_deg, eyes_interact=False) # better kernel1 = kernel1.astype('float32') kernel2 = kernel2.astype('float32') meany = Expectation() ex1 = Expectation() ex2 = Expectation() cp = ClientProcess() cp.config_use_white_arena() cp.config_stimulus_xml(example_stim_xml) #position = [0.15, 0.5, 0.25] position = [0.35, 0.5, 0.25] linear_velocity_body = [0, 0, 0] angular_velocity_body = [0, 0, 0] #from flydra_render.contrast import intrinsic_contrast from fast_contrast import intrinsic_contrast #@UnresolvedImport N = 360 pb = progress_bar('Computing contrast', N) orientation = numpy.linspace(0, 2 * numpy.pi, N) for i, theta in enumerate(orientation): attitude = rotz(theta) pb.update(i) res = cp.render(position, attitude, linear_velocity_body, angular_velocity_body) y = numpy.array(res['luminance']).astype('float32') meany.update(y) #y = numpy.random.rand(1398) c1 = intrinsic_contrast(y, kernel1) c2 = intrinsic_contrast(y, kernel2) ex1.update(c1) ex2.update(c2) r = Report() r.data_rgb('meany', scale(values2retina(meany.get_value()))) r.data_rgb('mean1', plot_contrast(ex1.get_value())) r.data_rgb('mean2', plot_contrast(ex2.get_value())) r.data_rgb('one-y', (plot_luminance(y))) r.data_rgb('one-c1', plot_contrast(c1)) r.data_rgb('one-c2', plot_contrast(c2)) r.data_rgb('kernel', scale(values2retina(kernel2[100, :]))) f = r.figure(shape=(2, 3)) f.sub('one-y', 'One random image') f.sub('one-c1', 'Contrast of random image') f.sub('one-c2', 'Contrast of random image') f.sub('meany', 'Mean luminance') f.sub('mean1', 'Mean over %s samples' % N) f.sub('mean2', 'Mean over %s samples' % N) f.sub('kernel') filename = 'compute_contrast_demo.html' print("Writing on %s" % filename) r.to_html(filename)
def render_saccades_view(saccades, stimulus_xml, host=None, white=False): client = get_rfsee_client(host) if white: # before stimulus_xml client.config_use_white_arena() client.config_stimulus_xml(stimulus_xml) num_frames = len(saccades) dtype = [('time', 'float64'), ('obj_id', 'int'), ('frame', 'int'), ('value', ('float32', 1398))] view_start = numpy.zeros(shape=(num_frames,), dtype=dtype) view_stop = numpy.zeros(shape=(num_frames,), dtype=dtype) view_rstop = numpy.zeros(shape=(num_frames,), dtype=dtype) view_random = numpy.zeros(shape=(num_frames,), dtype=dtype) view_sstop = numpy.zeros(shape=(num_frames,), dtype=dtype) pb = progress_bar('Rendering saccades', num_frames) for i in range(num_frames): pb.update(i) saccade = saccades[i] orientation_start = numpy.radians(saccade['orientation_start']) orientation_stop = numpy.radians(saccade['orientation_stop']) # simulate how it would look like sampling from a random distribution random_index = numpy.random.randint(0, len(saccades)) random_displacement = \ numpy.radians(saccades[random_index]['amplitude']) * \ saccades[random_index]['sign'] orientation_rstop = orientation_start + random_displacement # keep the sign, use random amplitude srandom_displacement = \ numpy.radians(saccades[random_index]['amplitude']) * \ saccade['sign'] orientation_sstop = orientation_start + srandom_displacement # finally, a random orientation orientation_random = numpy.random.rand() * 2 * numpy.pi position = saccade['position'] attitude_start = rotz(orientation_start) attitude_stop = rotz(orientation_stop) attitude_rstop = rotz(orientation_rstop) attitude_random = rotz(orientation_random) attitude_sstop = rotz(orientation_sstop) linear_velocity_body = [0, 0, 0] angular_velocity_body = [0, 0, 0] res_start = client.render(position, attitude_start, linear_velocity_body, angular_velocity_body) res_stop = client.render(position, attitude_stop, linear_velocity_body, angular_velocity_body) res_rstop = client.render(position, attitude_rstop, linear_velocity_body, angular_velocity_body) res_random = client.render(position, attitude_random, linear_velocity_body, angular_velocity_body) res_sstop = client.render(position, attitude_sstop, linear_velocity_body, angular_velocity_body) view_start['value'][i] = numpy.array(res_start['luminance']) view_stop['value'][i] = numpy.array(res_stop['luminance']) view_rstop['value'][i] = numpy.array(res_rstop['luminance']) view_random['value'][i] = numpy.array(res_random['luminance']) view_sstop['value'][i] = numpy.array(res_sstop['luminance']) # copy other fields for arr in [view_start, view_stop, view_rstop, view_random]: arr['frame'][i] = saccades[i]['frame'] arr['obj_id'][i] = saccades[i]['obj_id'] arr['time'][i] = saccades[i]['time_middle'] client.close() return view_start, view_stop, view_rstop, view_random, view_sstop
def render(rows, stimulus_xml, host=None, compute_mu=False, white=False): cp = get_rfsee_client(host) if white: # before stimulus_xml cp.config_use_white_arena() cp.config_stimulus_xml(stimulus_xml) cp.config_compute_mu(compute_mu) num_frames = len(rows) dtype = [('time', 'float64'), ('obj_id', 'int'), ('frame', 'int'), ('value', ('float32', 1398))] luminance = numpy.zeros(shape=(num_frames,), dtype=dtype) # copy index fields copy_fields = ['time', 'frame', 'obj_id'] for field in copy_fields: luminance[field] = rows[:][field] if compute_mu: nearness = numpy.zeros(shape=(num_frames,), dtype=dtype) dtype = [('time', 'float64'), ('obj_id', 'int'), ('frame', 'int'), ('value', ('float32', (1398, 2)))] retinal_velocities = numpy.zeros(shape=(num_frames,), dtype=dtype) for a in [nearness, retinal_velocities]: for field in copy_fields: a[field] = rows[:][field] pb = progress_bar('Rendering', num_frames) for i in range(num_frames): pb.update(i) position = rows[i]['position'] attitude = rows[i]['attitude'] linear_velocity_body = rows[i]['linear_velocity_body'] angular_velocity_body = rows[i]['angular_velocity_body'] res = cp.render(position, attitude, linear_velocity_body, angular_velocity_body) luminance['value'][i] = numpy.array(res['luminance']) if compute_mu: nearness['value'][i] = numpy.array(res['nearness']) retinal_velocities['value'][i] = numpy.array(res['retinal_velocities']) res = {'luminance': luminance} if compute_mu: res['retinal_velocities'] = retinal_velocities res['nearness'] = nearness cp.close() return res