Skip to content

Commit

Permalink
Issue 20 visualize (#25)
Browse files Browse the repository at this point in the history
* approaching #20

* solving #20

* fighting #20

* solved #20

* solved #20
  • Loading branch information
hamazaki1990 authored and heavywatal committed May 19, 2017
1 parent 1274b2b commit e89ca72
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 24 deletions.
8 changes: 4 additions & 4 deletions hamazaki1990/individual.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from decimal import Decimal


class Individual:
def __init__(self, n, f=1.0):
self._id = n
Expand All @@ -11,17 +14,14 @@ def get_fitness(self):

def __repr__(self):
return str(self._id)
return Decimal(self._fitness)


def main():
ind = Individual(42)
print(ind.get_id())
print(ind.get_fitness())

ind2 = Individual(30, 0.8)
print(ind2.get_id())
print(ind2.get_fitness())


if __name__ == '__main__':
main()
60 changes: 40 additions & 20 deletions hamazaki1990/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ def __init__(self, n, mutantrate=0, s=0):
wild_inds = [Individual(x) for x in range(num_mutant, n)]
self._inds = mutant_inds + wild_inds

def print_ids(self):
print(self._inds)
def get_ids(self):
return self._inds

def print_fitness(self):
def get_fitnesses(self):
fitness = [x.get_fitness() for x in self._inds]
print(fitness)
return fitness

def get_mutantfreq(self):
fitness = [x.get_fitness() for x in self._inds]
mutantfreq = 1 - fitness.count(1.0)/len(fitness)
return mutantfreq

def next_genwf(self):
fitness = [x.get_fitness() for x in self._inds]
Expand All @@ -46,45 +51,60 @@ def is_not_fixed(self):
else:
return False

def mutation_is_not_fixed(self):
fitness = [x.get_fitness() for x in self._inds]
for x in range(1, len(fitness)):
if fitness[0] != fitness[x]:
return True
else:
return False


def main():
p1_1 = Population(10)
p1_1.print_ids()
p1_1.print_fitness()
print(p1_1.get_ids())
print(p1_1.get_fitnesses())
print(p1_1.is_not_fixed())
print(p1_1.mutation_is_not_fixed())

for x in range(20):
p1_1.next_genwf()
p1_1.print_ids()
p1_1.print_fitness()
print(p1_1.get_ids())
print(p1_1.get_fitnesses())
print(p1_1.is_not_fixed())
print(p1_1.mutation_is_not_fixed())

p1_2 = Population(10, 0.3, 0.2)
p1_2.print_ids()
p1_2.print_fitness()
print(p1_2.get_ids())
print(p1_2.get_fitnesses())
print(p1_2.is_not_fixed())
print(p1_2.mutation_is_not_fixed())
print(p1_2.get_mutantfreq())

for x in range(20):
p1_2.next_genwf()
p1_2.print_ids()
p1_2.print_fitness()
print(p1_2.get_ids())
print(p1_2.get_fitnesses())
print(p1_2.is_not_fixed())
print(p1_2.mutation_is_not_fixed())

p2_1 = Population(10)
p2_1.print_ids()
p2_1.print_fitness()
print(p2_1.get_ids())
print(p2_1.get_fitnesses())

for x in range(20):
p2_1.next_genmo()
p2_1.print_ids()
p2_1.print_fitness()
print(p2_1.get_ids())
print(p2_1.get_fitnesses())

p2_2 = Population(10, 0.3, 0.2)
p2_2.print_ids()
p2_2.print_fitness()
print(p2_2.get_ids())
print(p2_2.get_fitnesses())

for x in range(20):
p2_2.next_genmo()
p2_2.print_ids()
p2_2.print_fitness()
print(p2_2.get_ids())
print(p2_2.get_fitnesses())


if __name__ == '__main__':
Expand Down
38 changes: 38 additions & 0 deletions hamazaki1990/visualize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import csv
from population import Population


def change_allelewf(population):
t = 0
fixprocess = [[t, population.get_mutantfreq()]]
while population.mutation_is_not_fixed():
t += 1
population.next_genwf()
fixprocess.append([t, population.get_mutantfreq()])
return fixprocess


def change_allelemo(population):
t = 0
fixprocess = [[t, population.get_mutantfreq()]]
while population.mutation_is_not_fixed():
t += 1
population.next_genmo()
fixprocess.append([t, population.get_mutantfreq()])
return fixprocess


def output_allelechange(filename, population, function):
with open(filename, "w", encoding="utf-8") as outfile:
writer = csv.writer(outfile)
writer.writerow(["generation", "derived_allele_frequency"])
fixprocess = function(population)
for x in range(len(fixprocess)):
writer.writerow(fixprocess[x])


p1 = Population(10, 0.1, 0.2)
output_allelechange("allelefreqwf.csv", p1, change_allelewf)

p2 = Population(10, 0.1, 0.5)
output_allelechange("allelefreqmo.csv", p2, change_allelemo)
13 changes: 13 additions & 0 deletions hamazaki1990/visualize.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
library("tidyverse")
setwd("~/github/practice-py/hamazaki1990/")
freq_wf <- read_csv("allelefreqwf.csv") %>%
dplyr::mutate(model = "wf")
freq_wf
freq_mo <- read_csv("allelefreqmo.csv") %>%
dplyr::mutate(model = "moran")
freq_mo
freq <- dplyr::bind_rows(freq_wf, freq_mo)
print(freq)
freqplot <- ggplot(freq, aes(x=generation, y=derived_allele_frequency, group=model, color=model))
freqplot <-freqplot + geom_line()
print(freqplot)

0 comments on commit e89ca72

Please sign in to comment.