diff options
author | Thomas Jourdan <b.vehikel@googlemail.com> | 2009-08-25 08:39:33 (GMT) |
---|---|---|
committer | Thomas Jourdan <b.vehikel@googlemail.com> | 2009-08-25 08:39:33 (GMT) |
commit | 2b7768ebf9d320dcf4b906bcb453092a45c4e1a2 (patch) | |
tree | 14ea4825289f98fb1833a2ce9d9c63a52c537428 | |
parent | 96ed4036391c8ee3cb995fe760708be2cffad11b (diff) |
Added Voronoi diagram with low resolution sampling.
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | ep_directionconstraint_none.py | 2 | ||||
-rw-r--r-- | ep_layer_voronoidiagram.py | 253 | ||||
-rw-r--r-- | ep_merger_alphaimage.py | 2 | ||||
-rw-r--r-- | ep_merger_border.py | 2 | ||||
-rw-r--r-- | ep_merger_flip.py | 2 | ||||
-rw-r--r-- | ep_merger_layermask.py | 2 | ||||
-rw-r--r-- | ep_merger_mask.py | 2 | ||||
-rw-r--r-- | ep_merger_rectangulartile.py | 2 | ||||
-rw-r--r-- | ep_sampler_grid.py | 125 | ||||
-rw-r--r-- | ka_random.py | 53 | ||||
-rw-r--r-- | ka_task.py | 4 | ||||
-rw-r--r-- | model_constraintpool.py | 2 |
13 files changed, 442 insertions, 13 deletions
@@ -13,7 +13,7 @@ *.[Jj][Pp][Gg] *.[Jj][Pp][Ee][Gg] testoutput_* -# no covarage datas +# no coverage data coverage.lst .coverage # no project files @@ -26,5 +26,5 @@ coverage.lst *.gpg *.txt # allow logo -!activity-kandid.svg +#!activity/activity-kandid.svg diff --git a/ep_directionconstraint_none.py b/ep_directionconstraint_none.py index 2965c0b..35fb6c1 100644 --- a/ep_directionconstraint_none.py +++ b/ep_directionconstraint_none.py @@ -30,7 +30,7 @@ class NoneDirectionConstraint(model_locus.Locus): cdef = [{'bind' : DISTANCE_CONSTRAINT, 'name' : 'Distance per hop', 'domain': model_constraintpool.FLOAT_RANGE, - 'min' : 0.2, 'max': 0.5, + 'min' : 0.0, 'max': 0.5, }, {'bind' : RADIAN_CONSTRAINT, 'name' : 'Rotation in radians', diff --git a/ep_layer_voronoidiagram.py b/ep_layer_voronoidiagram.py new file mode 100644 index 0000000..fb431e9 --- /dev/null +++ b/ep_layer_voronoidiagram.py @@ -0,0 +1,253 @@ +# coding: UTF8 +# Copyright 2009 Thomas Jourdan +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +import math +import ka_random +import ka_factory +import model_layer +import model_constraintpool +import exon_position +import exon_color + +ORDER_CONSTRAINT = 'orderconstaint' +SAMPLERTYPE_CONSTRAINT = 'samplertypeconstraint' +NUMBER_OF_SITES_CONSTRAINT = 'sitenumberofconstraint' + +class VoronoiDiagramLayer(model_layer.Layer): + """VoronoiDiagramLayer + inv: len(self.sites) > 0 + inv: self.sampler is not None + """ + + cdef = [{'bind' : ORDER_CONSTRAINT, + 'name' : 'Natural logarithm of order p used in Minkowski distance', + 'domain': model_constraintpool.FLOAT_RANGE, + 'min' : -4.0, 'max': 10.0}, + {'bind' : NUMBER_OF_SITES_CONSTRAINT, + 'name' : 'Number of site points', + 'domain': model_constraintpool.INT_RANGE, + 'min' : 2, 'max': 10}, + {'bind' : SAMPLERTYPE_CONSTRAINT, + 'name' : 'Permitted layer types', + 'domain': model_constraintpool.STRING_1_OF_N, + 'enum' : ka_factory.get_factory('sampler').keys()}, + ] + + def __init__(self, trunk): + """Voronoi diagram layer constructor""" + super(VoronoiDiagramLayer, self).__init__(trunk) + self.sites = [ (exon_position.Position(self.path, 0.0, 0.0), + exon_color.Color(self.path, 0.0, 0.0, 0.0, 0.0)) ] +# self._distance = VoronoiDiagramLayer._euclidean_square_distance + self.order = 2 # euclidean distance + sampler_factory = ka_factory.get_factory('sampler') + sampler_key = sampler_factory.keys()[0] + self.sampler = sampler_factory.create(sampler_key, self.path) + + def __eq__(self, other): + """Equality """ + equal = isinstance(other, VoronoiDiagramLayer) \ + and model_layer.Layer.__eq__(self, other) \ + and len(self.sites) == len(other.sites) \ + and self.order == other.order \ + and self.sampler == other.sampler + if equal: + for index, site in enumerate(self.sites): + equal = equal and site[0] == other.sites[index][0] \ + and site[1] == other.sites[index][1] + return equal + + def randomize(self): + """Randomize the layers components.""" + super(VoronoiDiagramLayer, self).randomize() + cpool = model_constraintpool.ConstraintPool.get_pool() + number_of_constraint = cpool.get(self, NUMBER_OF_SITES_CONSTRAINT) + + order_constraint = cpool.get(self, ORDER_CONSTRAINT) + self.order = ka_random.uniform_constrained(order_constraint) + + sampler_factory = ka_factory.get_factory('sampler') + samplertype_constraint = cpool.get(self, SAMPLERTYPE_CONSTRAINT) + self.sampler = sampler_factory.create_random(samplertype_constraint, + self.path) + self.sampler.randomize() + + self.sites = [] + for i in range(ka_random.randint_constrained(number_of_constraint)): + site_point = exon_position.Position(self.path, 0.0, 0.0) + site_point.randomize() + site_color = exon_color.Color(self.path, 0.0, 0.0, 0.0, 0.0) + site_color.randomize() + self.sites.append( (site_point, site_color) ) + + def mutate(self): + """Make small random changes to the layers components.""" + super(VoronoiDiagramLayer, self).mutate() + cpool = model_constraintpool.ConstraintPool.get_pool() + for site in self.sites: + site[0].mutate() + site[1].mutate() + order_constraint = cpool.get(self, ORDER_CONSTRAINT) + self.order += ka_random.jitter_constrained(order_constraint) + self.sampler.mutate() + + def shuffle(self): + """Shuffle similar components.""" + super(VoronoiDiagramLayer, self).shuffle() + + def crossingover(self, other): + """ + pre: isinstance(other, VoronoiDiagramLayer) + pre: isinstance(self, VoronoiDiagramLayer) + # check for distinct references, needs to copy content, not references + post: __return__ is not self + post: __return__ is not other + """ + new_one = VoronoiDiagramLayer(self.get_trunk()) + new_one.sites = ka_random.crossingover(self.sites, other.sites) + cross_sequence = ka_random.crossing_sequence(2) + new_one.order = self.order if cross_sequence[0] else other.order + new_one.sampler = self.sampler if cross_sequence[1] else other.sampler + return new_one + + def draw(self, ctx, width, height): + """ + pre: ctx is not None + pre: width > 0 + pre: height > 0 + pre: width == height + """ + self.begin_draw(ctx, width, height) + for point in self.sampler.get_sample_points(width, height): + rgba = self._site_color_min_dist(point).rgba + ctx.set_source_rgba(rgba[0], rgba[1], rgba[2], rgba[3]) + ctx.arc(point[0]-0.5, point[1]-0.5, 0.1, 0.0, 2.0*math.pi) + ctx.fill() + +# data = array.array('B', chr(0) * width * height * 4) +# stride = width * 4 +# dx = 1.0 / width +# dy = 1.0 / height +# py = 0.0 +# for iy in xrange(0, height*stride, stride): +# px = 0.0 +# for ix in xrange(0, width*4, 4): +# ii = ix+iy +# color = self._site_color_min_dist(px, py) +# data[ii] = int(color.rgba[0] * 255.0) # blue +# data[ii+1] = int(color.rgba[1] * 255.0) # green +# data[ii+2] = int(color.rgba[2] * 255.0) # red +# data[ii+3] = int(color.rgba[3] * 255.0) # alpha +# px += dx +# py += dy + +# for dy in xrange(0, height*stride, stride): +# for dx in xrange(0, width*4, 4): +# dd = dx+dy +# data[dd] = 0 # blue +# data[dd+1] = 255 # green +# data[dd+2] = 0 # red +# data[dd+3] = 64 # alpha +# for dy in xrange(0, height*stride, stride): +# dd = (width-1)*4+dy +# data[dd] = 0 # blue +# data[dd+1] = 0 # green +# data[dd+2] = 255 # red +# data[dd+3] = 255 # alpha +# for dx in xrange(0, width*4, 4): +# dd = dx+(height-1)*stride +# data[dd] = 255 # blue +# data[dd+1] = 0 # green +# data[dd+2] = 0 # red +# data[dd+3] = 255 # alpha + +# surface = cairo.ImageSurface.create_for_data( +# data, cairo.FORMAT_ARGB32, width, height, stride) +# ctx.scale(1.0/width, 1.0/height) +# ctx.set_source_surface(surface) +# ctx.paint() + + def _site_color_min_dist(self, point): + """ Minkowski distance + see http://en.wikipedia.org/wiki/Minkowski_distance + pre: len(point) == 2 + """ + + min_distance, at_index = 999999.9, 0 + for index, site in enumerate(self.sites): +# distance = self._distance(point, site[0]) + # + p = math.exp(self.order) + distance = (math.fabs(point[0]-site[0].x_pos)**p + + math.fabs(point[1]-site[0].y_pos)**p) ** (1.0/p) + if(distance < min_distance): + min_distance, at_index = distance, index + return self.sites[at_index][1] + +# @staticmethod +# def _euclidean_square_distance(point, site): +# """ +# Like Euclidean distance distance but with square root. +# see http://en.wikipedia.org/wiki/Euclidean_distance +# x-coordinate is stored at index [0]. +# y-coordinate is stored at index [1]. +# """ +# return (point[0]-site.x_pos)**2 + (point[1]-site.y_pos)**2 +# +# @staticmethod +# def _manhattan_distance(point, site): +# """ Taxicab distance, Manhattan distance) +# see http://en.wikipedia.org/wiki/Manhattan_distance +# x-coordinate is stored at index [0]. +# y-coordinate is stored at index [1]. +# """ +# return math.fabs(point[0]-site.x_pos) + math.fabs(point[1]-site.y_pos) +# +# @staticmethod +# def _chebyshev_distance(point, site): +# """ Chebyshev distance +# see http://en.wikipedia.org/wiki/Chebyshev_distance +# x-coordinate is stored at index [0]. +# y-coordinate is stored at index [1]. +# """ +# dx, dy = math.fabs(point[0]-site.x_pos), math.fabs(point[1]-site.y_pos) +# return dx if dx > dy else dy + + def explain(self, formater): + super(VoronoiDiagramLayer, self).explain(formater) + formater.text_item('Natural logarithm of order p used in Minkowski distance: ' + str(self.order)) + site_points = [s[0] for s in self.sites] + formater.position_array(site_points, 'center points for sites:') + site_colors = [s[1] for s in self.sites] + formater.color_array(site_colors, 'site colors:') + text, surface, descr = self.sampler.explain() + if surface is not None: + formater.surface_item(surface, 'sampling points:' + text, descr) + else: + formater.text_item(text) + + def copy(self): + """The Voronoi diagram layers copy constructor. + # check for distinct references, needs to copy content, not references + post: __return__ is not self + """ + new_one = VoronoiDiagramLayer(self.get_trunk()) + self.copy_base(new_one) + new_one.sites = ka_random.copy_tuple_list(self.sites) + new_one.order = self.order + new_one.sampler = self.sampler.copy() + return new_one diff --git a/ep_merger_alphaimage.py b/ep_merger_alphaimage.py index 7e0c052..2677424 100644 --- a/ep_merger_alphaimage.py +++ b/ep_merger_alphaimage.py @@ -89,7 +89,7 @@ class AlphaImageMerger(model_allele.Allele): def explain(self): """ - post len(__return__) == 3 + post: len(__return__) == 3 """ png_surface = cairo.ImageSurface.create_from_png(self.selected_image) width = height = 48 diff --git a/ep_merger_border.py b/ep_merger_border.py index 548cb31..135d98d 100644 --- a/ep_merger_border.py +++ b/ep_merger_border.py @@ -113,7 +113,7 @@ class BorderMerger(model_allele.Allele): def explain(self): """ - post len(__return__) == 3 + post: len(__return__) == 3 """ return u'Border merger, border weight=' + unicode(self.border_weight) \ + u' alfa=' + unicode(self.border_alfa), \ diff --git a/ep_merger_flip.py b/ep_merger_flip.py index 19c5bb9..305c17c 100644 --- a/ep_merger_flip.py +++ b/ep_merger_flip.py @@ -84,7 +84,7 @@ class FlipMerger(model_allele.Allele): def explain(self): """ - post len(__return__) == 3 + post: len(__return__) == 3 """ if self.xFlip and self.yFlip: text = u'Flip merger: flip horizontally and vertically.' diff --git a/ep_merger_layermask.py b/ep_merger_layermask.py index 98272e5..8e42103 100644 --- a/ep_merger_layermask.py +++ b/ep_merger_layermask.py @@ -115,7 +115,7 @@ class LayermaskMerger(model_allele.Allele): def explain(self): """ - post len(__return__) == 3 + post: len(__return__) == 3 """ return 'Layermask merger, scale_x**2=' + unicode(self.scale_x**2) \ + u' scale_y**2=' + unicode(self.scale_y**2), \ diff --git a/ep_merger_mask.py b/ep_merger_mask.py index 910ab4d..728beea 100644 --- a/ep_merger_mask.py +++ b/ep_merger_mask.py @@ -112,7 +112,7 @@ class MaskMerger(model_allele.Allele): def explain(self): """ - post len(__return__) == 3 + post: len(__return__) == 3 """ return u'Mask merger, center=' + self.center.explain() \ + u' direction=' + self.direction.explain(), \ diff --git a/ep_merger_rectangulartile.py b/ep_merger_rectangulartile.py index 1db1736..0258146 100644 --- a/ep_merger_rectangulartile.py +++ b/ep_merger_rectangulartile.py @@ -93,7 +93,7 @@ class RectangularTileMerger(model_allele.Allele): def explain(self): """ - post len(__return__) == 3 + post: len(__return__) == 3 """ return u'Rectangular tile merger: %d*x, %d*y' \ % (self.xTiles, self.yTiles), \ diff --git a/ep_sampler_grid.py b/ep_sampler_grid.py new file mode 100644 index 0000000..5c7aec7 --- /dev/null +++ b/ep_sampler_grid.py @@ -0,0 +1,125 @@ +# coding: UTF8 +# Copyright 2009 Thomas Jourdan +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +import ka_random +import model_allele +import model_constraintpool + +X_TILES_CONSTRAINT = 'xtilesconstraint' +Y_TILES_CONSTRAINT = 'ytilesconstraint' + +class GridSampler(model_allele.Allele): + """GridSampler: Reverse the layer horizontally or vertically. + inv: self.x_tiles > 0 + inv: self.y_tiles > 0 + """ + + cdef = [{'bind' : X_TILES_CONSTRAINT, + 'name' : 'Number of tiles in x direction', + 'domain': model_constraintpool.INT_RANGE, + 'min' : 1, 'max': 24}, + {'bind' : Y_TILES_CONSTRAINT, + 'name' : 'Number of tiles in x direction', + 'domain': model_constraintpool.INT_RANGE, + 'min' : 1, 'max': 24}, + ] + + def __init__(self, trunk): + """Constructor for a flip merger.""" + super(GridSampler, self).__init__(trunk) + self.x_tiles = 1 + self.y_tiles = 1 + + def __eq__(self, other): + """Equality based on fliping horizontally or vertically.""" + equal = isinstance(other, GridSampler) \ + and self.x_tiles == other.x_tiles \ + and self.y_tiles == other.y_tiles + return equal + + def randomize(self): + """Randomize the layers components.""" + cpool = model_constraintpool.ConstraintPool.get_pool() + x_tiles_constraint = cpool.get(self, X_TILES_CONSTRAINT) + self.x_tiles = ka_random.randint_constrained(x_tiles_constraint) + y_tiles_constraint = cpool.get(self, Y_TILES_CONSTRAINT) + self.y_tiles = ka_random.randint_constrained(y_tiles_constraint) + + def mutate(self): + """Make small random changes to the layers components.""" + cpool = model_constraintpool.ConstraintPool.get_pool() + if ka_random.is_mutating(): + x_tiles_constraint = cpool.get(self, X_TILES_CONSTRAINT) + self.x_tiles = ka_random.jitter_discret_constrained(self.x_tiles, + x_tiles_constraint) + if ka_random.is_mutating(): + y_tiles_constraint = cpool.get(self, Y_TILES_CONSTRAINT) + self.y_tiles = ka_random.jitter_discret_constrained(self.y_tiles, + y_tiles_constraint) + + def shuffle(self): + """Shuffle similar componets.""" + if ka_random.is_shuffling(): + temp = self.x_tiles + self.x_tiles, self.y_tiles = self.y_tiles, temp + + def crossingover(self, other): + """ + pre: isinstance(other, GridSampler) + pre: isinstance(self, GridSampler) + # check for distinct references, needs to copy content, not references + post: __return__ is not self + post: __return__ is not other + """ + new_one = GridSampler(self.get_trunk()) + cross_sequence = ka_random.crossing_sequence(2) + new_one.x_tiles = self.x_tiles if cross_sequence[0] else other.x_tiles + new_one.y_tiles = self.y_tiles if cross_sequence[1] else other.y_tiles + return new_one + + def get_sample_points(self, width, height): + """ + pre: width > 0 + pre: height > 0 + pre: width == height + """ + sample_points = [] + dx, dy = 1.0 / self.x_tiles, 1.0 / self.y_tiles + for tx in range(self.x_tiles): + for ty in range(self.y_tiles): + sample_points.append( (tx*dx+0.5*dx, ty*dy+0.5*dy) ) + return sample_points + + def explain(self): + """ + post: len(__return__) == 3 + """ + return u'Grid sampler: %d*x, %d*y' \ + % (self.x_tiles, self.y_tiles), \ + None, \ + None + + def copy(self): + """A copy constructor. + post: isinstance(__return__, GridSampler) + # check for distinct references, needs to copy content, not references + post: __return__ is not self + """ + new_one = GridSampler(self.get_trunk()) + new_one.x_tiles = self.x_tiles + new_one.y_tiles = self.y_tiles + return new_one diff --git a/ka_random.py b/ka_random.py index bdbd172..3940c97 100644 --- a/ka_random.py +++ b/ka_random.py @@ -38,6 +38,20 @@ def get_flurry(): """ return _flurry * 10.0 +def binomial(n, probability): + """ Generates a binomial distributed random value. + Returns a random number between 0 (inclusive) and n (inclusive). + Code is from http://geovistastudio.sourceforge.net/ + pre: n >= 0 + pre: 0.0 <= probability <= 1.0 + post: 0 <= __return__ <= n + """ + r = 0 + for _ in range(n): + if random.random() < probability: + r += 1 + return r + def jitter(sigma): """ """ @@ -52,10 +66,30 @@ def jitter_constrained(constraint): sigma = 0.03 * _flurry * (constraint[1] - constraint[0]) return random.gauss(0.0, sigma) +def jitter_discret_constrained(value, constraint): + """ + pre: len(constraint) == 2 + pre: constraint[0] <= constraint[1] + post: constraint[0] <= __return__ <= constraint[1] + """ + interval = constraint[1] - constraint[0] + delta = binomial(interval, _flurry * 0.25) * random.choice([-1, 1]) + print 'delta', delta + return limitate_range(value+delta, constraint[0], constraint[1]) + +def randint_constrained(constraint): + """ + pre: len(constraint) == 2 + pre: constraint[0] <= constraint[1] + post: constraint[0] <= __return__ <= constraint[1] + """ + return random.randint(constraint[0], constraint[1]) + def uniform_constrained(constraint): """ pre: len(constraint) == 2 pre: constraint[0] <= constraint[1] + post: constraint[0] <= __return__ <= constraint[1] """ return random.uniform(constraint[0], constraint[1]) @@ -181,6 +215,21 @@ def copy_list(other_list): new_list = [] for element in other_list: new_list.append(element.copy()) -# for i in range(len(other_list)): -# print other_list[i] == new_list[i], other_list[i], new_list[i] + return new_list + +def copy_tuple_list(other_list): + """Make a deep copy of a list and its tuple elements. + Assumes every tuple has two cells + pre: other_list is not None + pre: len(other_list) == 0 or forall([len(e) == 2 for e in other_list]) + post: __return__ is not other_list + post: len(__return__) == len(other_list) + post: forall([other_list[i][0] is not __return__[i][0] for i in range(len(other_list))]) + post: forall([other_list[i][0] == __return__[i][0] for i in range(len(other_list))]) + post: forall([other_list[i][1] is not __return__[i][1] for i in range(len(other_list))]) + post: forall([other_list[i][1] == __return__[i][1] for i in range(len(other_list))]) + """ + new_list = [] + for element in other_list: + new_list.append( (element[0].copy(), element[1].copy()) ) return new_list @@ -16,6 +16,7 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import sys +import traceback import threading import gobject import ka_debug @@ -69,8 +70,9 @@ class GeneratorTask(object): print 'result', result gobject.idle_add(self._on_task_completed, result) except: + traceback.print_exc(file=sys.__stderr__) ka_debug.err('failed calculating [%s] [%s] [%s]' % \ - (self._task_function, sys.exc_info()[0], sys.exc_info()[1])) + (self._task_function, sys.exc_info()[0], sys.exc_info()[1])) finally: _leave() diff --git a/model_constraintpool.py b/model_constraintpool.py index 7bad1be..69fa81b 100644 --- a/model_constraintpool.py +++ b/model_constraintpool.py @@ -131,7 +131,7 @@ class ConstraintPool(object): def _my_defaults(self): #TODO read from persistence, provide an constraint editor - self.set('/Protozoon', 'layertypeconstraint', ['image', 'letterpress', 'markovchain', 'randomwalkspline', ]) + self.set('/Protozoon', 'layertypeconstraint', ['image', 'letterpress', 'markovchain', 'randomwalkspline', 'voronoidiagram', ]) # self.set('/Protozoon', 'layertypeconstraint', ['randomwalkspline', 'letterpress', 'markovchain', ]) # self.set('/Protozoon', 'layertypeconstraint', ['randomwalkspline', ]) # self.set('/Protozoon', 'layertypeconstraint', ['voronoidiagram', ]) |