def test_early_winner_bias():
  
  particle_size = (5,5)
  max_num_particles = 50
  
  feature_size = (5,5)
  image_shape = (1,20,20)
  image_iter = GridImageGenerator(
                              num_features = 5,
                              feature_size = feature_size,
                              image_shape = image_shape,
                              num_images = 100,
                              sparsity = 0.6,
                              random_seed = int(time.time() % 100000)
                            )
  f1 = [[[0,0,1,0,0],
         [0,0,1,0,0],
         [1,1,1,1,1],
         [0,0,1,0,0],
         [0,0,1,0,0]]]
  
  f2 = [[[1,0,0,0,1],
         [0,1,0,1,0],
         [0,0,1,0,0],
         [0,1,0,1,0],
         [1,0,0,0,1]]]
  
  f3 = [[[0,0,0,0,0],
         [0,1,1,1,0],
         [0,1,1,1,0],
         [0,1,1,1,0],
         [0,0,0,0,0]]]
  
  f4 = [[[0,0,1,1,0],
         [0,0,1,0,0],
         [1,0,1,0,1],
         [0,0,1,0,0],
         [0,1,1,0,0]]]
  eq_area_feats = [ numpy.array(f1), numpy.array(f2), 
                         numpy.array(f3), 
                         numpy.array(f4)
                       ]
  
  f5 = numpy.array([[[0,1,0,0,0],
                     [1,0,0,0,1],
                     [1,0,0,0,1],
                     [1,0,0,0,1],
                     [0,0,0,1,0]]])
  
  image_iter.features = eq_area_feats
  image_iter.show_features()

  particles = ParticleList(particle_size, image_shape,
                           max_num_particles=max_num_particles)
  
  t1 = time.time()
  
  while image_iter.has_next():
    image = image_iter.next()
    particles.update(image)
  particles.show_winners(50)
  
  eq_area_feats.append(f5)
  image_iter.features = eq_area_feats
  image_iter.show_features()
  image_iter.reset()
  
  while image_iter.has_next():
    image = image_iter.next()
    particles.update(image)
  particles.show_winners(50)
  
  t2 = time.time()
  learning_time = t2 - t1
  
  # Validate results
  features = particles.get_features()
  target_features = image_iter.features[:]
  target_num_features = len(target_features)
  num_targets_learned = 0
  
  for learned_feat in features:
    for i in range(len(target_features)):
      target = target_features[i]
      if (learned_feat == target).all():
        del target_features[i]
        num_targets_learned += 1
        break
  
  print "Learned %s of the %s target features." % (num_targets_learned, target_num_features)
  print "Learned %s features in total." % len(features)
  print "Learning time: %.06f seconds" % learning_time
  print
def basic_test_particle_learning():
  
  particle_size = (5,5)
  num_particles = 40
  
  feature_size = (5,5)
  image_shape = (1,20,20)
  image_iter = GridImageGenerator(
                              num_features = 5,
                              feature_size = feature_size,
                              image_shape = image_shape,
                              num_images = 50,
                              sparsity = 0.6,
                              random_seed = int(time.time() % 100000)
                            )
  f1 = [[[0,0,1,0,0],
         [0,0,1,0,0],
         [1,1,1,1,1],
         [0,0,1,0,0],
         [0,0,1,0,0]]]
  
  f2 = numpy.array([[[1,0,0,0,1],
                     [0,1,0,1,0],
                     [0,0,1,0,0],
                     [0,1,0,1,0],
                     [1,0,0,0,1]]])
  
  f3 = numpy.array([[[0,0,0,0,0],
                     [0,1,1,1,0],
                     [0,1,1,1,0],
                     [0,1,1,1,0],
                     [0,0,0,0,0]]])
  
  f4 = [[[0,0,1,1,0],
         [0,0,1,0,0],
         [1,0,1,0,1],
         [0,0,1,0,0],
         [0,1,1,0,0]]]
  eq_area_feats = [ numpy.array(f1), f2, 
                         f3, 
                         numpy.array(f4)
                       ]
  
  subsume_feats = [ f2, f3 ]
  
  f1 = [[[0,0,1,0,0],
         [0,0,1,0,0],
         [0,0,1,0,0],
         [0,0,1,0,0],
         [0,0,1,0,0]]]
  
  f3 = numpy.array([[[0,0,0,0,0],
                     [0,0,0,1,0],
                     [0,0,1,0,0],
                     [0,1,0,0,0],
                     [0,0,0,0,0]]])
  
  f2 = numpy.array([[[0,0,0,0,0],
                     [0,1,0,0,0],
                     [0,0,1,0,0],
                     [0,0,0,1,0],
                     [0,0,0,0,0]]])
  
  f4 = [[[0,1,0,0,0],
         [1,0,0,0,1],
         [1,0,0,0,1],
         [1,0,0,0,1],
         [0,0,0,1,0]]]
  
  subsume_feats.extend([f2, f3])
  
  line_feats = [ numpy.array(f1), f2, 
                         f3, 
                         numpy.array(f4)
                       ]
  
  image_iter.features = subsume_feats #eq_area_feats + line_feats
  image_iter.show_features()

  particles = ParticleList(particle_size, image_shape,
                           max_num_particles=40)
  
  t1 = time.time()
  
  while image_iter.has_next():
    image = image_iter.next()
    particles.update(image)
    #toimage(image[0]).show()
  particles.show_winners(500)
    #from IPython.Debugger import Pdb; Pdb(color_scheme='Linux').set_trace()
  
  t2 = time.time()
  learning_time = t2 - t1
  
  # Validate results
  features = particles.get_features()
  target_features = image_iter.features[:]
  target_num_features = len(target_features)
  num_targets_learned = 0
  
  for learned_feat in features:
    for i in range(len(target_features)):
      target = target_features[i]
      if (learned_feat == target).all():
        del target_features[i]
        num_targets_learned += 1
        break
  
  print "Learned %s of the %s target features." % (num_targets_learned, target_num_features)
  print "Learned %s features in total." % len(features)
  print "Learning time: %.06f seconds" % learning_time
  print