Skip to content

Instantly share code, notes, and snippets.

@romunov
Created April 22, 2015 11:03
Show Gist options
  • Save romunov/4bc695e168cc14f3f0b8 to your computer and use it in GitHub Desktop.
Save romunov/4bc695e168cc14f3f0b8 to your computer and use it in GitHub Desktop.
write Ne_LD_sp results to a file
def splitCalcMerge(pop, param):
# Calculate population sizes to help with splitting (see below).
pop_size_croatia = pop.subPopSize(0)
pop_size_slovenia = pop.subPopSize(1)
pop_sizes = {"1":pop_size_croatia, "3":pop_size_slovenia}
# Split population into two. First subpopulation is the main one minus the
# sample size (ss).
sim.splitSubPops(pop = pop, subPops = 0, sizes = [pop_size_croatia - param["croatia"], param["croatia"]])
sim.splitSubPops(pop = pop, subPops = 2, sizes = [pop_size_slovenia - param["slovenia"], param["slovenia"]])
sim.stat(pop = pop, popSize = "subPopSize")
sim.stat(pop = pop, effectiveSize = sim.ALL_AVAIL, vars = "Ne_LD_sp")
# Define levels in "x", namely mean, lower and upper confidence intervals.
x_name = ["fit", "lci", "uci"]
# Construct a dictionary that can be read in columns:
# gen, pop_num, pop_name, x, 0.0, 0.01, 0.02, 0.05, actual_size
# generation ____/ / / / / / / / /
# population num ______/ / / / / / / /
# population name ___________/ / / / / / /
# variable designation _____________/ / / / / /
# value without a cutoff ______________/ / / / /
# value at cutoff of 0.01 __________________/ / / /
# value at cutoff of 0.02 _______________________/ / /
# value at cutoff of 0.05 ____________________________/ /
# actual subpopulation size ________________________________/
# Output results into a results.txt file.
with open("results.txt", mode = "a") as rf: #rf = result file
# Run only through the "sample" population.
for p in [1, 3]:
data = []
for x in range(3): # fit, lci, uci
v = []
for y in [0.0, 0.01, 0.02, 0.05]:
v.append(pop.dvars().subPop[p]["Ne_LD"][y][x])
data.append(v)
to_print = "{gen}, {pop}, {popname}, {x}, {data0}, {data01}, {data02}, {data05} {actual_size}".format(
gen = pop.dvars().gen, pop = pop.subPopNames()[p], popname = p, x = x_name[x], data0 = data[x][0],
data01 = data[x][1], data02 = data[x][2], data05 = data[x][3], actual_size = pop_sizes[str(p)])
print >> rf, to_print
sim.mergeSubPops(pop = pop, subPops = [0, 1], name = "croatia")
sim.mergeSubPops(pop = pop, subPops = [1, 2], name = "slovenia")
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment