Exemplo n.º 1
0
def animalNum(stats):

    chart = XYLineChart(400, 300)

    max_wx = 0
    for creature in ['rabbit', 'wolf']:
        WX = []
        WY = []
        count = 0
        for t in sorted(stats[creature].keys()):
            WX.append(t)
            for event in stats[creature][t]['event']:
                if event == LiveLog.BIRTH:
                    count += 1
                else:
                    count -= 1
          #  print count, creature, str(stats[creature][t]['event'])
            WY.append(count)

        chart.add_data(WX)
        chart.add_data(WY)
        max_wx = max(max_wx, max(WX))

    chart.set_title("Population size")
    chart.set_colours(['ff0000', '0000ff'])
    chart.set_legend(['rabbits', 'wolves'])
    chart.set_grid(0, 10, 3, 3)

    chart.set_axis_labels(Axis.BOTTOM, range(0, max_wx + 1, 100))
    chart.download('number.png')
class Graph():
    
    colours = []
    legend = []
    max_wx = 0
    max_wy = 0
    min_wy = 4000000000
    
    label_suffix = " MB"
    scale = 1024*1024

    def __init__(self):
        self.chart = XYLineChart(750, 400)
        
    
    def set_config(self, config):
        self.label_suffix = config.get("general", "label_suffix")
        self.scale = int(config.get("general", "scale"))

    def add_serie(self, WX, WY, colour, descr):
        self.chart.add_data(WX)
        self.chart.add_data(WY)
        self.colours.append(colour)
        self.legend.append(descr)
        
        self.max_wx = max(self.max_wx, max(WX))
        self.max_wy = max(self.max_wy, max(WY))
        self.min_wy = min(self.min_wy, min(WY))
        
    def set_title(self, title):
        self.chart.set_title(title)
        
    def make_chart(self, name):
        densX = self.max_wx / 15
        
        wy_max_lab = int(self.max_wy / self.scale + 1)
        wy_min_lab = int(max(self.min_wy / self.scale - 1, 0))
        densY = max((wy_max_lab - wy_min_lab) / 10, 1) 
        self.chart.set_axis_labels(Axis.BOTTOM, range(0, self.max_wx + 1, densX))
        self.chart.set_axis_labels(Axis.LEFT, self.__make_Ylabels(wy_min_lab, wy_max_lab, int(densY)))
        self.chart.y_range = (wy_min_lab * self.scale, wy_max_lab * self.scale)
        
        self.chart.set_legend(self.legend)
        self.chart.set_colours(self.colours)
        self.chart.set_grid(15, 10, 1, 5)
        
        self.chart.download("%s.png" % (name))
        
    def __make_Ylabels(self, wy_min_lab, wy_max_lab, densY):
        result = []
        for i in range(wy_min_lab, wy_max_lab, densY):
            result.append("%d%s" % (i, self.label_suffix))
        return result
Exemplo n.º 3
0
def presence_chart(days):
  """Returns a graph of user presence, in his language of choice."""

  start = datetime.date.today() - datetime.timedelta(days=days)
  start = datetime.date(start.year, start.month, 1)

  genreh = Rehearsal.objects.filter(type='G').filter(date__gte=start).order_by('date')
  begreh = Rehearsal.objects.filter(type='B').filter(date__gte=start).order_by('date')
  concert = Concert.objects.filter(start__gte=start).order_by('start')

  # Calculates the vertical range
  max_y = 0
  genreh_presence = [] 
  active = []
  begreh_presence = []
  concert_presence = []
  for k in genreh: 
    active.append(((k.date-start).days, k.members.count()))
    if k.members.count() > max_y: max_y = k.members.count()
    genreh_presence.append(((k.date-start).days, k.presence().count()))
  for k in begreh: 
    begreh_presence.append(((k.date-start).days, k.presence().count()))
  for k in concert: 
    concert_presence.append(((k.start.date()-start).days, k.participated().count()))
    if k.participated().count() > max_y: max_y = k.participated().count()
  max_y = 5+5*(max_y/5)

  # Chart size of 200x125 pixels and specifying the range for the Y axis
  width = 500
  height = 200
  chart = XYLineChart(width, height, y_range=[0, max_y])
  chart.add_data([k[0] for k in genreh_presence])
  chart.add_data([k[1] for k in genreh_presence])
  chart.add_data([k[0] for k in begreh_presence])
  chart.add_data([k[1] for k in begreh_presence])
  chart.add_data([k[0] for k in concert_presence])
  chart.add_data([k[1] for k in concert_presence])
  chart.add_data([k[0] for k in active])
  chart.add_data([k[1] for k in active])

  # Set the line colours
  chart.set_colours(['0000FF', 'FF0000', '00FF00','000000'])
  chart.set_legend([_(u'General rehearsals'),_(u'Beginner rehearsals'),_(u'Concerts'),_(u'Active members')])
  chart.set_title(_(u'Member presence'))

  # Contents of each axis
  x_labels = range(0, max_y + 1, 5)
  x_labels[0] = ''
  chart.set_axis_labels(Axis.LEFT, x_labels)

  # Calculates the months
  months = [start.month + k for k in range(int(math.ceil(days/30)))]
  while max(months) > 12:
    for k in range(len(months)): 
      if months[k] > 12: months[k] -= 12 
  labels = [k.strftime('%b') for k in [datetime.date(1970, j, 1) for j in months]] + [''] 
  chart.set_axis_labels(Axis.BOTTOM, labels)

  # Set the horizontal dotted lines
  chart.set_grid(0, 20, 1, 5)

  chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 1.0/(len(labels)-1), 'FFFFFF', 1.0/(len(labels)-1))

  return {'url': chart.get_url(), 'width': width, 'height': height} 
Exemplo n.º 4
0
def main():

    parseLive()

    stats = {}
    time_stats = {}

    fp = open("genetics.log", "r")
    lines = fp.readlines()
    header = [h.strip() for h in lines[0].split(",")]
    for line in lines[1:]:
        data = line.split(",")
        type = data[0].strip()
        generation = int(data[1])
        time = int(float(data[2]) / 10) * 10

        if type not in stats:
            stats[type] = {}
            time_stats[type] = {}
        if generation not in stats[type]:
            stats[type][generation] = {}
        if time not in time_stats[type]:
            time_stats[type][time] = {}

        d = stats[type][generation]
        t = time_stats[type][time]

        for phene, value in zip(header[3:], data[3:]):
            if phene not in d:
                d[phene] = []
            d[phene].append(float(value))
            if phene not in t:
                t[phene] = []
            t[phene].append(float(value))

    for s, name in [(stats, "generation"), (time_stats, "time")]:

        for feature in Phenotype.VALID:

            chart = XYLineChart(400, 300)

            max_wx = 0
            for creature in ['rabbit', 'wolf']:
                WX = []
                WYavg = []
                WYmin = []
                WYmax = []
                for g, phenes in sorted(s[creature].items()):
                    WX.append(g)
                    WYavg.append(sum(phenes[feature]) / len(phenes[feature]))
                    WYmin.append(min(phenes[feature]))
                    WYmax.append(max(phenes[feature]))

                if len(WX) < 2:
                    WX.append(2)

                chart.add_data(WX)
                chart.add_data(WYmin)
                chart.add_data(WX)
                chart.add_data(WYmax)
                chart.add_data(WX)
                chart.add_data(WYavg)



                max_wx = max(max_wx, max(WX))

            chart.set_title("Phenotype: %s / %s" % (feature, name))
            chart.set_colours(['ffe9bf', 'daffbf', 'ff0000', 'ebbfff', 'bffff5', '0000ff'])
            chart.set_legend(['rabbits-min', 'rabbits-max', 'rabbits-avg', 'wolves-min', 'wolves-max', 'wolves-avg'])
            chart.set_grid(0, 10, 3, 3)

            if name == "generation":
                chart.set_axis_labels(Axis.BOTTOM, range(0, max_wx + 1, 5))
            else:
                chart.set_axis_labels(Axis.BOTTOM, range(0, max_wx + 1, 100))
            chart.download("%s-%s.png" % (name, feature))