Пример #1
0
def process_file(filename, treatment, rat, session, num_epochs, print_dict=False): 
  allzones_by_epoch = zones_by_epoch(filename,num_epochs)
  distances_by_epoch = distance_by_epoch(filename,num_epochs)

  for epoch, (ezones, edist) in enumerate(zip(allzones_by_epoch, distances_by_epoch)):
    unigram_counts  = unigrams(ezones)
    unigram_probs   = probs(unigram_counts)
    unigram_entropy = h(unigram_probs.values())

    bigram_counts = bigrams(ezones)
    bigram_probs  = probs_bigrams(bigram_counts)

    between_zone_bi = remove_self_transitions(bigram_counts)
    between_zone_pp = probs_bigrams(between_zone_bi)

    total_count = 0
    total_entropy = 0
    total_between_zone_count = 0
    total_between_zone_entropy = 0

    for (p, zone) in bigram_probs.sorted():
      entropy = h(p.values())
      count   = unigram_counts[zone]

      total_count = total_count + count
      total_entropy = total_entropy + (count * entropy)

      bz_p = between_zone_pp[zone]
      bz_entropy = h(bz_p.values())
      bz_count = sum(between_zone_bi[zone].values()) 
      
      total_between_zone_count   = total_between_zone_count + bz_count 
      total_between_zone_entropy = total_between_zone_entropy + (bz_count * bz_entropy)

      if print_dict:
        details = ' '.join([ "%4s:%f" % pair for pair in p.iteritems()])
        print "\t%4s : %4d * %f -> %4s" % (zone, count, entropy, details)
      
    bigram_entropy    = total_entropy / total_count
    bigram_bz_entropy = total_between_zone_entropy / total_between_zone_count

    print ','.join(
      [str(x) for x in [treatment, rat, session, epoch, total_count, 
                        edist, unigram_entropy, bigram_entropy, 
                        total_between_zone_count, bigram_bz_entropy]])
Пример #2
0
def process_file(filename, treatment, rat, session): 
  """prints out a variety of statistics for each zone"""

  zone_g, between_zone_g = zone_graphs("zone-graph.txt")

  baseline              = uniform_zone_entropy(zone_g)
  between_zone_baseline = uniform_zone_entropy(between_zone_g)

  allzones = zones(filename)

  bigram_counts = remove_terminal_transitions(bigrams(allzones))
  bigram_probs  = probs_bigrams(bigram_counts)

  between_zone_bigram_counts = remove_self_transitions(bigram_counts)
  between_zone_bigram_probs  = probs_bigrams(between_zone_bigram_counts)

  #print filename 

  for (zone, p) in bigram_probs.iteritems():

    zone_destinations = zone_g[zone].keys()

    #impossible_transitions = [(zone, z2, pr) for (z2, pr) in p.iteritems() if z2 not in zone_destinations]
    impossible_transitions = find_impossible_transitions(zone_g, zone, p)

    #if len(impossible_transitions) > 0:
      #print "impossible transitions: " , treatment, rat, session, impossible_transitions

    entropy = h(p.values())
    count   = sum(bigram_counts[zone].values())

    bz_p       = between_zone_bigram_probs[zone]
    bz_entropy = h(bz_p.values())
    bz_count   = sum(between_zone_bigram_counts[zone].values())

    #details = ' '.join([ "%4s:%f" % pair for pair in p.iteritems()])

    #print "\t%4s : %4d * %f -> %4s" % (zone, count, entropy, details)
  
    print ','.join([str(x) for x in 
                    [treatment, rat, session, zone, 
                     entropy, baseline[zone], baseline[zone]-entropy,
                     bz_entropy, between_zone_baseline[zone], between_zone_baseline[zone]-bz_entropy, 
                     count, bz_count]])
Пример #3
0
def uniform_zone_entropy(graph):
    pp = probs_bigrams(graph)
    return dict([(z, h(p.values())) for (z,p) in pp.iteritems()])