diff options
author | Thomas Jourdan <b.vehikel@googlemail.com> | 2009-12-06 12:40:41 (GMT) |
---|---|---|
committer | Thomas Jourdan <b.vehikel@googlemail.com> | 2009-12-06 12:40:41 (GMT) |
commit | 7ce7155dead3893e572006588fc342fb3af7ec60 (patch) | |
tree | 2bb234d6d159aa797767bf1ceccea53117dc773a /ep_layer_markovchain.py | |
parent | bcde11455168a07de8a3b17f2a4d77ce8931e75d (diff) |
Layers are now arranged as a tree data structure.
Diffstat (limited to 'ep_layer_markovchain.py')
-rw-r--r-- | ep_layer_markovchain.py | 89 |
1 files changed, 49 insertions, 40 deletions
diff --git a/ep_layer_markovchain.py b/ep_layer_markovchain.py index 3eabda3..a73ca7c 100644 --- a/ep_layer_markovchain.py +++ b/ep_layer_markovchain.py @@ -17,9 +17,11 @@ import random import exon_color +import model_locus import model_layer import model_constraintpool -import ka_random +import ka_debug +import model_random import ka_factory #NUMBER_OF_TILES_CONSTRAINT = 'number_of_tiles_constraint' @@ -61,7 +63,7 @@ class MarkovChainLayer(model_layer.Layer): self.states = 1 self.cell_colors = [exon_color.Color(self.path, 0, 0, 0, 1)] self.probability = [[1.0 / self.states] * self.states - for i in range(self.states)] + for dummy in range(self.states)] self._init_states(2) # self.number_of_tiles = 2 @@ -73,6 +75,17 @@ class MarkovChainLayer(model_layer.Layer): stamp_key = stamp_factory.keys()[0] self.stamp = stamp_factory.create(stamp_key, self.path) + def dot(self): + result = "" + anchor = ka_debug.dot_id(self) + ' -> ' + for ref in self.cell_colors: + result += ka_debug.dot_ref(anchor, ref) + for ref in self.probability: + result += ka_debug.dot_ref(anchor, ref) + for ref in [self.sampler, self.stamp]: + result += ka_debug.dot_ref(anchor, ref) + return result + def _init_states(self, number_of_states): """ pre: number_of_states >= 2 @@ -94,13 +107,13 @@ class MarkovChainLayer(model_layer.Layer): """ diff = number_of_states - self.states # append random colors - for cix in range(diff): + for dummy in range(diff): color = exon_color.Color(self.path, 0, 0, 0, 1) color.randomize() self.cell_colors.append(color) # completely recalculate probabilities self.probability = [[1.0 / number_of_states] * number_of_states - for i in range(number_of_states)] + for dummy in range(number_of_states)] for row, row_probabilities in enumerate(self.probability): for col in range(len(row_probabilities)): self.probability[row][col] = random.random() / self.states @@ -111,18 +124,18 @@ class MarkovChainLayer(model_layer.Layer): pre: self.states > number_of_states """ # copy remaining cell colors - cell_colors = [None] * number_of_states + copy_cell_colors = [None] * number_of_states for cix in range(number_of_states): - cell_colors[cix] = self.cell_colors[cix].copy() - self.cell_colors = cell_colors + copy_cell_colors[cix] = self.cell_colors[cix].copy() + self.cell_colors = copy_cell_colors # copy remaining probabilities - probability = [[1.0] * number_of_states - for i in range(number_of_states)] - for row, row_probabilities in enumerate(probability): + copy_probability = [[1.0] * number_of_states + for dummy in range(number_of_states)] + for row, row_probabilities in enumerate(copy_probability): for col in range(len(row_probabilities)): - probability[row][col] = self.probability[row][col] + copy_probability[row][col] = self.probability[row][col] self._normalize_row(row) - self.probability = probability + self.probability = copy_probability def _normalize_row(self, row): row_probabilities = self.probability[row] @@ -156,11 +169,11 @@ class MarkovChainLayer(model_layer.Layer): super(MarkovChainLayer, self).randomize() cpool = model_constraintpool.ConstraintPool.get_pool() number_of_states_constraint = cpool.get(self, NUMBER_OF_STATES_CONSTRAINT) - self._init_states(ka_random.randint_constrained( + self._init_states(model_random.randint_constrained( number_of_states_constraint)) # number_of_tiles_constraint = cpool.get(self, NUMBER_OF_TILES_CONSTRAINT) -# self.number_of_tiles = ka_random.randint_constrained( +# self.number_of_tiles = model_random.randint_constrained( # number_of_tiles_constraint) sampler_factory = ka_factory.get_factory('sampler') @@ -180,7 +193,7 @@ class MarkovChainLayer(model_layer.Layer): super(MarkovChainLayer, self).mutate() cpool = model_constraintpool.ConstraintPool.get_pool() number_of_states_constraint = cpool.get(self, NUMBER_OF_STATES_CONSTRAINT) - changed = self._init_states(ka_random.jitter_discret_constrained( + changed = self._init_states(model_random.jitter_discret_constrained( self.states, number_of_states_constraint)) for cix in range(len(self.cell_colors)): @@ -190,29 +203,21 @@ class MarkovChainLayer(model_layer.Layer): for row, row_probabilities in enumerate(self.probability): for col in range(len(row_probabilities)): self.probability[row][col] += \ - ka_random.jitter(1.0 / self.states) + model_random.jitter(1.0 / self.states) self._normalize_row(row) -# number_of_tiles_constraint = cpool.get(self, NUMBER_OF_TILES_CONSTRAINT) -# self.number_of_tiles = ka_random.jitter_discret_constrained( -# self.number_of_tiles, number_of_tiles_constraint) - self.sampler.mutate() self.stamp.mutate() - def shuffle(self): - """Shuffle similar componets. - For example exchange foreground and background color.""" - super(MarkovChainLayer, self).shuffle() - if ka_random.is_shuffling(): - random.shuffle(self.cell_colors) - if ka_random.is_shuffling(): - random.shuffle(self.probability) - for row_probabilities in self.probability: - if ka_random.is_shuffling(): - random.shuffle(row_probabilities) - if ka_random.is_shuffling(): - self.sampler.shuffle() + def swap_places(self): + """Shuffle similar components.""" + model_random.swap_places(self.cell_colors) + model_random.swap_places(self.probability) + for row, row_probabilities in enumerate(self.probability): + model_random.swap_places(row_probabilities) + self._normalize_row(row) + self.sampler.swap_places() + self.stamp.swap_places() def crossingover(self, other): """ @@ -221,14 +226,17 @@ class MarkovChainLayer(model_layer.Layer): # check for distinct references, needs to copy content, not references post: __return__ is not self post: __return__ is not other + post: model_locus.unique_check(__return__, self, other) == '' """ new_one = MarkovChainLayer(self.get_trunk()) cross_sequence = self.crossingover_base(new_one, other, \ 2 + len(self.cell_colors)) # new_one.number_of_tiles = self.number_of_tiles \ # if cross_sequence[0] else other.number_of_tiles - new_one.sampler = self.sampler if cross_sequence[0] else other.sampler - new_one.stamp = self.stamp if cross_sequence[1] else other.stamp + new_one.sampler = model_random.crossingover_elem(self.sampler, + other.sampler) + new_one.stamp = model_random.crossingover_elem(self.stamp, + other.stamp) if cross_sequence[1]: probability = self.probability @@ -239,7 +247,7 @@ class MarkovChainLayer(model_layer.Layer): cell_colors = other.cell_colors new_one.states = other.states new_one.probability = [[1.0 / new_one.states] * new_one.states - for i in range(new_one.states)] + for dummy in range(new_one.states)] for row, row_probabilities in enumerate(probability): for col, cell_probability in enumerate(row_probabilities): new_one.probability[row][col] = cell_probability @@ -284,17 +292,17 @@ class MarkovChainLayer(model_layer.Layer): return next_cell_state def explain(self, formater): -# super(MarkovChainLayer, self).explain(formater) + super(MarkovChainLayer, self).explain(formater) formater.color_array(self.cell_colors, 'cell colors:') formater.text_item('number of states: ' + str(self.states)) text, surface, descr = self.sampler.explain() if surface is not None: - formater.surface_item(surface, 'sampling points:' + text, descr) + formater.surface_item(surface, 'sampling points: ' + text, descr) else: formater.text_item(text) text, surface, descr = self.stamp.explain() if surface is not None: - formater.surface_item(surface, 'stamp:' + text, descr) + formater.surface_item(surface, 'stamp: ' + text, descr) else: formater.text_item(text) @@ -310,7 +318,8 @@ class MarkovChainLayer(model_layer.Layer): new_one.cell_colors = [None] * self.states for cix in range(len(self.cell_colors)): new_one.cell_colors[cix] = self.cell_colors[cix].copy() - new_one.probability = [[0.0] * self.states for i in range(self.states)] + new_one.probability = [[0.0] * self.states + for dummy in range(self.states)] for row, row_probabilities in enumerate(self.probability): for col, cell_probability in enumerate(row_probabilities): new_one.probability[row][col] = cell_probability |