Skip to content

Commit

Permalink
Fix compatibility with Python 3.11 #117
Browse files Browse the repository at this point in the history
  • Loading branch information
BoPeng committed Mar 29, 2024
1 parent 6932bcc commit ec8a6ea
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@
MALE, AFFECTED, tagID, getRNG

import random
def random_shuffle(x):
random.shuffle(x, getRNG().randUniform)

def isSequence(obj):
return hasattr(obj, '__iter__')
Expand Down Expand Up @@ -173,7 +171,7 @@ def drawSamples(self, pop, numOfSamples):
'''
if numOfSamples < 0:
raise ValueError("Negative number of samples are unacceptable")
#
#
return [self.drawSample(pop) for x in range(numOfSamples)]


Expand All @@ -200,7 +198,7 @@ def drawSample(self, input_pop):
size = self.pop.popSize()
# randomly choose size individuals
values = list(range(self.pop.popSize()))
random_shuffle(values)
random.shuffle(values)
indexes = values[:size]
else:
indexes = []
Expand All @@ -210,7 +208,7 @@ def drawSample(self, input_pop):
print('Warning: sample size (%d) at subpopulation %d is greater than subpopulation size %d ' \
% (size, sp, self.pop.subPopSize(sp)))
values = list(range(self.pop.subPopBegin(sp), self.pop.subPopEnd(sp)))
random_shuffle(values)
random.shuffle(values)
indexes.extend(values[:size])
return self.pop.extractIndividuals(indexes = indexes)

Expand Down Expand Up @@ -308,14 +306,14 @@ def drawSample(self, input_pop):
self.prepareSample(input_pop)
#
if not isSequence(self.cases):
random_shuffle(self.affected)
random_shuffle(self.unaffected)
random.shuffle(self.affected)
random.shuffle(self.unaffected)
indexes = self.affected[:self.cases] + self.unaffected[:self.controls]
else:
indexes = []
for sp in range(self.pop.numSubPop()):
random_shuffle(self.affected[sp])
random_shuffle(self.unaffected[sp])
random.shuffle(self.affected[sp])
random.shuffle(self.unaffected[sp])
indexes.extend(self.affected[sp][:self.cases[sp]])
indexes.extend(self.unaffected[sp][:self.controls[sp]])
return self.pop.extractIndividuals(indexes = indexes)
Expand All @@ -332,7 +330,7 @@ def drawCaseControlSample(pop, cases, controls, subPops=ALL_AVAIL):
specified (virtual) subpopulations. This function returns a population with
all extracted individuals.
'''
return CaseControlSampler(cases, controls, subPops).drawSample(pop)
return CaseControlSampler(cases, controls, subPops).drawSample(pop)


def drawCaseControlSamples(pop, cases, controls, numOfSamples=1, subPops=ALL_AVAIL):
Expand All @@ -341,7 +339,7 @@ def drawCaseControlSamples(pop, cases, controls, numOfSamples=1, subPops=ALL_AVA
populations. Please refer to function ``drawCaseControlSample`` for a
detailed descriptions of parameters.
'''
return CaseControlSampler(cases, controls, subPops).drawSamples(pop, numOfSamples)
return CaseControlSampler(cases, controls, subPops).drawSamples(pop, numOfSamples)


class PedigreeSampler(BaseSampler):
Expand Down Expand Up @@ -399,7 +397,7 @@ def drawSample(self, input_pop):
% (self.families, len(self.selectedIDs)))
# a tuple might be returned
self.selectedIDs = list(self.selectedIDs)
random_shuffle(self.selectedIDs)
random.shuffle(self.selectedIDs)
# we select families one by one to exclude overlapping families.
IDs = set()
cnt = 0
Expand All @@ -422,7 +420,7 @@ def drawSample(self, input_pop):
#
# a tuple might be returned
self.selectedIDs[sp] = list(self.selectedIDs[sp])
random_shuffle(self.selectedIDs[sp])
random.shuffle(self.selectedIDs[sp])
# we select families one by one to exclude overlapping families.
cnt = 0
for id in self.selectedIDs[sp]:
Expand Down Expand Up @@ -475,7 +473,7 @@ def prepareSample(self, input_pop):
subPops=sp))


def drawAffectedSibpairSample(pop, families, subPops=ALL_AVAIL,
def drawAffectedSibpairSample(pop, families, subPops=ALL_AVAIL,
idField='ind_id', fatherField='father_id', motherField='mother_id'):
'''Draw affected sibpair samples from a population. If a single
``families`` is given, affected sibpairs and their parents are drawn
Expand All @@ -488,9 +486,9 @@ def drawAffectedSibpairSample(pop, families, subPops=ALL_AVAIL,
'''
return AffectedSibpairSampler(families, subPops, idField, fatherField,
motherField).drawSample(pop)


def drawAffectedSibpairSamples(pop, families, numOfSamples=1, subPops=ALL_AVAIL,

def drawAffectedSibpairSamples(pop, families, numOfSamples=1, subPops=ALL_AVAIL,
idField='ind_id', fatherField='father_id', motherField='mother_id'):
'''Draw ``numOfSamples`` affected sibpair samplesa from population ``pop`` and
return a list of populations. Please refer to function
Expand Down Expand Up @@ -625,7 +623,7 @@ def drawNuclearFamilySample(pop, families, numOffspring, affectedParents=0,
'''
return NuclearFamilySampler(families, numOffspring, affectedParents,
affectedOffspring, subPops, idField, fatherField, motherField).drawSample(pop)


def drawNuclearFamilySamples(pop, families, numOffspring, affectedParents=0,
affectedOffspring=0, numOfSamples=1, subPops=ALL_AVAIL, idField='ind_id',
Expand Down Expand Up @@ -777,7 +775,7 @@ def drawThreeGenFamilySample(pop, families, numOffspring, pedSize, numOfAffected
'''
return ThreeGenFamilySampler(families, numOffspring, pedSize, numOfAffected,
subPops, idField, fatherField, motherField).drawSample(pop)


def drawThreeGenFamilySamples(pop, families, numOffspring, pedSize, numOfAffected=0,
numOfSamples=1, subPops=ALL_AVAIL, idField='ind_id', fatherField='father_id',
Expand Down Expand Up @@ -836,4 +834,3 @@ def drawCombinedSamples(pop, samplers, numOfSamples=1, idField='ind_id'):
parameters ``samplers`` and ``idField``.
'''
return CombinedSampler(samplers, idField=idField).drawSamples(pop, numOfSamples)

0 comments on commit ec8a6ea

Please sign in to comment.