BoPeng / simuPOP

A general-purpose forward-time population genetics simulation environment.
http://bopeng.github.io/simuPOP/
GNU General Public License v2.0
30 stars 12 forks source link

Value Error: matting.cpp: 1728 #75

Closed natashaserrao closed 5 years ago

natashaserrao commented 5 years ago

Hi Bo,

I am so sorry for all of my questions and that I am struggling with the trivial stuff- I am having a really hard time trying to create my simulation!

My code was working fine till I assigned ages to the individuals using a specified error, after which I kept getting the following message, "ValueError: mating.cpp: 1728 An exact (all negative) weight system is used, but does not fill offspring subpopulation."

Thoughts?

Thank you, Natasha

import simuPOP as sim
import simuOpt
import numpy as np
#import random for random number generators
import random
#import simupop.utils module (allow for exporting files)
import simuPOP.utils as simp
# seed number set so that each time simulation is run it will output same numbers
sim.setRNG(seed=12349)

# set population size to 1000 individuals, ploidy to 2, and a total of 17 loci
pop=sim.Population(size=[72, 928], ploidy=2, loci=[1]*17, infoFields=['age'])
# create age structuring in the population
#set age of fishers randomly in a pouplation
val = np.random.choice(np.arange(0, 6), p=[0.261,0.20,0.169,0.144,0.122,0.104])
pop.setIndInfo([val for x in range(1000)], 'age')
# create age structuring in the population
pop.setVirtualSplitter(sim.InfoSplitter(field='age', cutoff=[2, 6.1]))
#evolve the population using specified proportions for sex, genotype frequencies, and ages
pop.evolve(
           #initialize population with starting M/F ratios and Genotype proportions (applied before evolution)
            #randomly set individual ages between 0 and 6 yers old
        initOps=[
                sim.InitSex(maleProp=0.57),
                sim.InitGenotype(prop=[.057,.093,.321,.150,.379], loci=0), 
                sim.InitGenotype(prop=[.036,.121,.300,.379,.164], loci=1),
                sim.InitGenotype(prop=[.375,.125,.417,.083], loci=2),
                sim.InitGenotype(prop=[.479,.250,.236,.028,.007], loci=3), 
                sim.InitGenotype(prop=[.340,.014,.250,.354,.042], loci=4),
                sim.InitGenotype(prop=[.338, .155, .465, .042], loci=5),
                sim.InitGenotype(prop=[.028, .417, .458, .097], loci=6),
                sim.InitGenotype(prop=[.229, .500, .014, .160, .063, .034], loci=7), 
                sim.InitGenotype(prop=[.014, .410, .465, .111], loci=8),
                sim.InitGenotype(prop=[.965, .014, .021], loci=9),
                sim.InitGenotype(prop=[.042, .218, .049, .120, .106, .141, .169, .042, .092, .014, .007], loci=10),
                sim.InitGenotype(prop=[.160, .104, .257, .347, .111, .014, .007], loci=11),
                sim.InitGenotype(prop=[.431, .500, .069], loci=12),
                sim.InitGenotype(prop=[.028, .035, .250, .632, .035, .006, .014], loci=13),
                sim.InitGenotype(prop=[.069, .771, .063, .097], loci=14),
                sim.InitGenotype(prop=[.022, .123, .087, .312, .181, .152, .123], loci=15),
                sim.InitGenotype(prop=[.375, .625], loci=16),
                                ],
        # Created a population split at T=10 generations to simulate ONP populations being simulated from BC populations
        # Added migration rate to simulate translocation efforts
        preOps= [sim.InfoExec('age += 1')
                 ],

        #Polygamous mating scheme implemented; male as poly sex, mating with 2 females each,under
        # Mendelian inheritance style
        matingScheme =sim.HeteroMating(matingSchemes=[
                sim.CloneMating(subPops=[(0, 0), (0,1), (1,0), (1,1)], weight=-1),
                sim.PolygamousMating(subPops=[(0,1), (1,1)], polySex=sim.MALE, polyNum=2, ops=[sim.MendelianGenoTransmitter()]
                  )
            ]),
        #Calculation of allele frequences over 17 loci; took the the sum of lengths of frequencies 
        # across 17 loci and divided it by 17 to get an average number of alleles over time 
        postOps = [
        sim.Stat(pop, alleleFreq=range(17), vars=['alleleFreq_sp'], step=1),
        sim.PyEval('gen', step=1, reps=-1),
        # this is calculating allele loss over for the BC populations over time
        sim.PyEval(r"'%s, %.2f, %.2f' % (gen, sum([len(subPop[0]['alleleFreq'][x])"
                    "for x in range(16)])/16., sum([len(subPop[1]['alleleFreq'][x])"
                    "for x in range(16)])/16.)", step=1, output='>>AGAF2.txt'),
        sim.PyOutput('\n', reps=-1, step=1, output='>>AGAF2.txt'),
        sim.PyEval('gen', step=1, reps=-1),
        ],   
        gen=25
        )

import matplotlib.pyplot as plt
import csv

x1=[]
y1=[]
y2=[]

#open just BC population over time
with open('AGAF2.txt', 'r') as csvfile:
    plots=csv.reader(csvfile, delimiter=',')
    for row in plots:
        x1.append(float(row[0]))
        y1.append(float(row[1]))
        y2.append(float(row[2]))

plt.plot(x1,y1, 'r--', label='ONP population-migrants')
plt.plot(x1,y2, 'b--', label='BC population')
plt.xlabel('Number of Generations')
plt.ylabel('Number of Alleles')
plt.ylim(0,6)
plt.title('Loss of Alleles over Time')
plt.legend()
plt.show()
BoPeng commented 5 years ago

The error message says

  1. You have weight -1 for clone mating, which copies all individuals to the offspring generation.
  2. The second mating scheme should have weight 0, and generate the rest of the offspring. If you get an error, it probably means the parental virtual subpopulation is empty.

The best way to debug this is to print the size of all (virtual) subpopulations before mating (using a preMating operator, or in a demo function).

natashaserrao commented 5 years ago

Thanks Bo!

I have resolved that issue, and get a new error, TypeError: in method 'new_HomoMating', argument 3 of type 'simuPOP::uintListFunc const &'.

I am trying to use the ExpansionEvent model and am running into problems. I can do this successfully without age structuring, but as soon as I add in the age structuring, it gives me that error.

I would love any insight on how to debug this error!

Thank you for your sustained help, Natasha

import simuPOP as sim
import simuOpt
from simuPOP.demography import *
#import random for random number generators
import random
#import simupop.utils module (allow for exporting files)
import simuPOP.utils as simp
# seed number set so that each time simulation is run it will output same numbers
sim.setRNG(seed=12349)

model = ExpansionEvent(rates=[0.05, 0.01], capacity=[92,10000], begin=12)

# set population size to 1000 individuals, ploidy to 2, and a total of 17 loci
pop=sim.Population(size=[1000], ploidy=2, loci=[1]*17, infoFields=['age'])
# create age structuring in the population
#set age of fishers randomly in a pouplation
pop.setIndInfo([random.randint(0,6) for x in range(1000)], 'age')
# create age structuring in the population
pop.setVirtualSplitter(sim.InfoSplitter(field='age', cutoff=[2, 6.1]))
#evolve the population using specified proportions for sex, genotype frequencies, and ages
pop.evolve(
           #initialize population with starting M/F ratios and Genotype proportions (applied before evolution)
            #randomly set individual ages between 0 and 6 yers old
        initOps=[
                sim.InitSex(maleProp=0.57),
                sim.InitGenotype(prop=[.057,.093,.321,.150,.379], loci=0), 
                sim.InitGenotype(prop=[.036,.121,.300,.379,.164], loci=1),
                sim.InitGenotype(prop=[.375,.125,.417,.083], loci=2),
                sim.InitGenotype(prop=[.479,.250,.236,.028,.007], loci=3), 
                sim.InitGenotype(prop=[.340,.014,.250,.354,.042], loci=4),
                sim.InitGenotype(prop=[.338, .155, .465, .042], loci=5),
                sim.InitGenotype(prop=[.028, .417, .458, .097], loci=6),
                sim.InitGenotype(prop=[.229, .500, .014, .160, .063, .034], loci=7), 
                sim.InitGenotype(prop=[.014, .410, .465, .111], loci=8),
                sim.InitGenotype(prop=[.965, .014, .021], loci=9),
                sim.InitGenotype(prop=[.042, .218, .049, .120, .106, .141, .169, .042, .092, .014, .007], loci=10),
                sim.InitGenotype(prop=[.160, .104, .257, .347, .111, .014, .007], loci=11),
                sim.InitGenotype(prop=[.431, .500, .069], loci=12),
                sim.InitGenotype(prop=[.028, .035, .250, .632, .035, .006, .014], loci=13),
                sim.InitGenotype(prop=[.069, .771, .063, .097], loci=14),
                sim.InitGenotype(prop=[.022, .123, .087, .312, .181, .152, .123], loci=15),
                sim.InitGenotype(prop=[.375, .625], loci=16),
                                ],
        # Created a population split at T=10 generations to simulate ONP populations being simulated from BC populations
        # Added migration rate to simulate translocation efforts
        preOps= [sim.SplitSubPops(subPops=0, sizes=[72, 928], at=10),
                 sim.Stat(popSize=True),
                 sim.PyEval(r'"%s\n" % subPopSize'),
                 sim.InfoExec('age += 1')
                 ],

        #Polygamous mating scheme implemented; male as poly sex, mating with 2 females each,under
        # Mendelian inheritance style
        matingScheme =sim.HeteroMating(matingSchemes=[
                sim.CloneMating(subPops=[(0, 0), (0,1), (1,0), (1,1)], weight=-1),
                sim.PolygamousMating(subPops=[(0,1), (1,1)],subPopSize=model, polySex=sim.MALE, polyNum=2, ops=[sim.MendelianGenoTransmitter()]
                  )
            ]),
        #Calculation of allele frequences over 17 loci; took the the sum of lengths of frequencies 
        # across 17 loci and divided it by 17 to get an average number of alleles over time 
        postOps = [
        sim.Stat(pop, alleleFreq=range(17), vars=['alleleFreq_sp'], step=1),
        sim.PyEval('gen', step=1, reps=-1),
        # this is calculating allele loss over for the BC populations over time
        sim.PyEval(r"'%s, %.2f, %.2f' % (gen, sum([len(subPop[0]['alleleFreq'][x])"
                    "for x in range(16)])/16., sum([len(subPop[1]['alleleFreq'][x])"
                    "for x in range(16)])/16.)", begin=10,step=1, output='>>AGAF2.txt'),
        sim.PyOutput('\n', reps=-1, step=1, output='>>AGAF2.txt'),
        sim.PyEval('gen', step=1, reps=-1),
        ],   
        gen=100
        )
BoPeng commented 5 years ago

Please read the manual more carefully. ExpansionEvent by itself is a event and should be used inside a EventBasedModel.

natashaserrao commented 5 years ago

Thank you Bo!