[Avida-cvs] [Avida2-svn] r92 -
branches/developers/kaben/source/python/AvidaGui2
kaben at myxo.css.msu.edu
kaben at myxo.css.msu.edu
Wed Mar 30 11:44:30 PST 2005
Author: kaben
Date: 2005-03-30 14:44:30 -0500 (Wed, 30 Mar 2005)
New Revision: 92
Modified:
branches/developers/kaben/source/python/AvidaGui2/pyPetriDishCtrl.py
Log:
* Experiment to move some gui work into the processing thread.
Modified: branches/developers/kaben/source/python/AvidaGui2/pyPetriDishCtrl.py
===================================================================
--- branches/developers/kaben/source/python/AvidaGui2/pyPetriDishCtrl.py 2005-03-30 19:41:25 UTC (rev 91)
+++ branches/developers/kaben/source/python/AvidaGui2/pyPetriDishCtrl.py 2005-03-30 19:44:30 UTC (rev 92)
@@ -1,3 +1,17 @@
+# Notes:
+# - w*h population cell_info_items;
+# - cell_info_item[0] refers to population cell(0).
+# - x ord of item[n]:
+# - stores normalized_index
+#
+# x: n % w
+# y: n / w
+# xm: (x + w/2) % w
+# ym: (y + h/2) % h
+#
+# xm: (n%w + w/2) % w
+# ym: (n/w + h/2) % h
+
# -*- coding: utf-8 -*-
from AvidaCore import cConfig
@@ -7,6 +21,30 @@
from qtcanvas import *
#from pyPetriDishView import pyPetriDishView
+class pyPopulationCellItem:
+ def __init__(self, population_cell, x, y, w, h, canvas):
+ self.m_population_cell = population_cell
+ self.m_canvas_rectangle = QCanvasRectangle(x, y, w, h, canvas)
+ self.m_canvas_rectangle.show()
+ self.m_index = 0
+
+ def checkNormalizedIndexUsingFunctor(self, functor, min, range):
+ index = 1.0
+ if 0.0 < range: index = (functor(self.m_population_cell) - min) / range
+ if 1.0 < index: index = 1.0
+ elif index < 0.0: index = 0.0
+
+ if self.m_index == index:
+ return False
+ else:
+ self.m_index = index
+ return True
+
+ def updateColorUsingFunctor(self, functor):
+ color = functor(self.m_index)
+ self.m_canvas_rectangle.setBrush(QBrush(color))
+ self.m_canvas_rectangle.setPen(QPen(color))
+
#class pyPetriDishCtrl(pyPetriDishView):
class pyPetriDishCtrl(QWidget):
@@ -44,21 +82,35 @@
self.m_canvas_view.setCanvas(self.m_canvas)
if self.m_cell_info: del self.m_cell_info
- self.m_cell_info = [[QCanvasRectangle(
- x * self.m_map_cell_w,
- y * self.m_map_cell_h,
+ self.m_cell_info = [pyPopulationCellItem(
+ self.m_avida.m_population.GetCell(n),
+ ((n%world_w + world_w/2) % world_w) * self.m_map_cell_w,
+ ((n/world_w + world_h/2) % world_h) * self.m_map_cell_h,
self.m_map_cell_w,
self.m_map_cell_h,
- self.m_canvas) for y in range(world_h)] for x in range(world_w)]
+ self.m_canvas) for n in range(world_w * world_h)]
- for x in range(world_w):
- for y in range(world_h):
- self.m_cell_info[x][y].setBrush(QBrush(QColor(x*255/world_w, y*255/world_h, x*y*255/(world_w*world_h))))
- self.m_cell_info[x][y].setPen(QPen(QColor(x*255/world_w, y*255/world_h, x*y*255/(world_w*world_h))))
- self.m_cell_info[x][y].show()
+ #self.m_cell_info = [[QCanvasRectangle(
+ # x * self.m_map_cell_w,
+ # y * self.m_map_cell_h,
+ # self.m_map_cell_w,
+ # self.m_map_cell_h,
+ # self.m_canvas) for y in range(world_h)] for x in range(world_w)]
+ temp_normalized_index_functor = lambda population_cell: 1
+ temp_color_functor = lambda index: QColor(255, 255, 255)
+ for cell_info_item in self.m_cell_info:
+ cell_info_item.checkNormalizedIndexUsingFunctor(temp_normalized_index_functor, 0, 0)
+ cell_info_item.updateColorUsingFunctor(temp_color_functor)
+ #for x in range(world_w):
+ # for y in range(world_h):
+ # self.m_cell_info[x][y].setBrush(QBrush(QColor(x*255/world_w, y*255/world_h, x*y*255/(world_w*world_h))))
+ # self.m_cell_info[x][y].setPen(QPen(QColor(x*255/world_w, y*255/world_h, x*y*255/(world_w*world_h))))
+ # self.m_cell_info[x][y].show()
+ self.m_thread_work_cell_item_index = 0
+
def mousePressEvent(self,e):
if e.button() != Qt.LeftButton:
return
@@ -148,7 +200,7 @@
self.m_petri_dish_layout.setResizeMode(QLayout.Minimum)
self.m_canvas_view = QCanvasView(None, self,"m_canvas_view")
self.m_petri_dish_layout.addWidget(self.m_canvas_view)
-
+
def calcColorScale(self):
self.m_cs_min_value = 0
self.m_cs_value_range = self.m_avida.m_population.GetStats().GetMaxFitness()
@@ -178,19 +230,70 @@
state = self.doubleToColor(dbl)
return state
+ def doSomeWork(self, avida):
+ def temp_normalized_index_functor(population_cell):
+ dbl = 0.0
+ if population_cell.IsOccupied():
+ dbl = population_cell.GetOrganism().GetPhenotype().GetFitness()
+ return dbl
+
+ for x in range(10):
+ if self.m_cell_info.count() < self.m_thread_work_cell_item_index:
+ self.m_thread_work_cell_item_index = 0
+ return False
+ else:
+ if self.m_cell_info[self.m_thread_work_cell_item_index].checkNormalizedIndexUsingFunctor(
+ temp_normalized_index_functor,
+ self.m_cs_min_value,
+ self.m_cs_value_range
+ ):
+ self.m_changed_cell_items.append(cell_info_item)
+ self.m_thread_work_cell_item_index += 1
+ return True
+
+
def avidaUpdatedSlot(self):
print "pyPetriDishCtrl.avidaUpdatedSlot() : called."
+
+ def temp_normalized_index_functor(population_cell):
+ dbl = 0.0
+ if population_cell.IsOccupied():
+ dbl = population_cell.GetOrganism().GetPhenotype().GetFitness()
+ return dbl
+
+ def temp_color_functor(index):
+ def sigmoid(w, midpoint, steepness):
+ val = steepness*(w-midpoint)
+ return exp(val)/(1+exp(val))
+ h = (index*360.0 + 100.0) % 360.0
+ v = sigmoid(index, 0.3, 10.0) * 255.0
+ s = sigmoid(1.0 - index, 0.1, 30.0) * 255.0
+ return QColor(h, s, v, QColor.Hsv)
+
self.calcColorScale()
- world_w = cConfig.GetWorldX()
- world_h = cConfig.GetWorldY()
- for x in range(world_w):
- for y in range(world_h):
- cell = self.m_avida.m_population.GetCell(x + world_w*y)
- color = self.calcCellState(cell)
- xm = (x + world_w/2) % world_w
- ym = (y + world_h/2) % world_h
- self.m_cell_info[xm][ym].setBrush(QBrush(color))
- self.m_cell_info[xm][ym].setPen(QPen(color))
+
+ self.m_changed_cell_items = []
+ for cell_info_item in self.m_cell_info:
+ if cell_info_item.checkNormalizedIndexUsingFunctor(
+ temp_normalized_index_functor,
+ self.m_cs_min_value,
+ self.m_cs_value_range
+ ):
+ self.m_changed_cell_items.append(cell_info_item)
+
+ for cell_info_item in self.m_changed_cell_items:
+ cell_info_item.updateColorUsingFunctor(temp_color_functor)
+
+ #world_w = cConfig.GetWorldX()
+ #world_h = cConfig.GetWorldY()
+ #for x in range(world_w):
+ # for y in range(world_h):
+ # cell = self.m_avida.m_population.GetCell(x + world_w*y)
+ # color = self.calcCellState(cell)
+ # xm = (x + world_w/2) % world_w
+ # ym = (y + world_h/2) % world_h
+ # self.m_cell_info[xm][ym].setBrush(QBrush(color))
+ # self.m_cell_info[xm][ym].setPen(QPen(color))
self.m_canvas.update()
def extractPopulationSlot(self):
More information about the Avida-cvs
mailing list