-
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fs/jquinn (Sourcery refactored) #42
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
def stratStats(model, strat, nvot, ncand, niter, stratArgs={}, numWinners=1, pollingError=0.2, | ||
usePolls=True, pickiness=0.7, pollFilter=None): | ||
bulletCount, totalScore = 0, 0 | ||
for i in range(niter): | ||
for _ in range(niter): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
electorate = model(nvot, ncand) | ||
if usePolls: | ||
pollBallots = [Approval.zeroInfoBallot(v, pickiness=pickiness) for v in electorate] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,15 +139,15 @@ def influentialBlocs(voters, method, numWinners=1, utilChange=0.1, numBuckets=5, | |
pollingMethod = method | ||
pollingStrat = method.honBallot | ||
basePollBallots = [pollingStrat(v, numWinners=numWinners, **pollingStratArgs) for v in voters] | ||
pollErrors = [random.gauss(0, pollingError/2) for i in range(numCands)] | ||
pollErrors = [random.gauss(0, pollingError/2) for _ in range(numCands)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
polls = [min(1, max(0, r + e)) for r, e in zip(pollingMethod.results(basePollBallots), pollErrors)] | ||
#polls = media(pollingMethod.results(pollBallots), pollingError) #method.results can't depend on numWinners; this may need to be changed | ||
baseBallots = [strat(v, polls=polls, electabilities=polls, numWinners=numWinners, **stratArgs) for v in voters] | ||
if isinstance(sorter, type): | ||
sorter = sorter(voters) | ||
baseWinners = method.winnerSet(baseBallots, numWinners) | ||
baseResults = method.results(baseBallots) | ||
isIncentive = [[] for i in range(numCands)] | ||
isIncentive = [[] for _ in range(numCands)] | ||
#isIncentive[c][b] is 1 if candidate c is incentivized to appeal to the bth bucket of voters, 0 otherwise | ||
for cand in range(numCands): | ||
utilShifts = [0]*numCands | ||
|
@@ -205,7 +205,7 @@ def __init__(self, model, methodsAndStrats, nvot, ncand, niter, nwinners=1, | |
elif len(m) == 2: | ||
ms.append((m[0], m[1], {})) | ||
else: ms.append(m) | ||
self.mNames = [m[0].__name__ + ':' + m[1].__name__ + str(m[2]) for m in ms] | ||
self.mNames = [f'{m[0].__name__}:{m[1].__name__}{str(m[2])}' for m in ms] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.rows = [] | ||
args = (model, nvot, ncand, ms, nwinners, utilChange, numBuckets, sorter, pollingMethod, pollingError, pollAfterPert) | ||
with multiprocessing.Pool(processes=7) as pool: | ||
|
@@ -233,7 +233,10 @@ def chart(self, methodOnly=True): | |
fig, ax = plt.subplots() | ||
incentFracts = self.summarize()[0] | ||
if methodOnly: | ||
incentFracts = {re.match(".*(?=:)", name).group(0): data for name, data in incentFracts.items()} | ||
incentFracts = { | ||
re.match(".*(?=:)", name)[0]: data | ||
for name, data in incentFracts.items() | ||
} | ||
Comment on lines
-236
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
Comment on lines
-236
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
for name, data in incentFracts.items(): | ||
ax.plot([i/self.numBuckets for i in range(1, self.numBuckets+1)], data, label=name) | ||
#ax.set_xlim(1, self.numBuckets) | ||
|
@@ -258,16 +261,17 @@ def saveFile(self, baseName="cidResults", newFile=True): | |
fields = ['name', 'baseResult'] + list(range(self.numBuckets)) + list(universalInfo.keys()) | ||
resultTypeIndices = {'all': 1, 'loss': 2, 'win': 3} | ||
|
||
myFile = open(baseName + (str(i) if newFile else "") + ".csv", "w") | ||
dw = csv.DictWriter(myFile, fields, restval="data missing") | ||
dw.writeheader() | ||
for outcome, index in resultTypeIndices.items(): | ||
for name, results in self.summarize()[index].items(): | ||
row = {'name': name, 'baseResult': outcome} | ||
row.update({i: result for i, result in enumerate(results)}) | ||
row.update(universalInfo) | ||
dw.writerow(row) | ||
myFile.close() | ||
with open(baseName + (str(i) if newFile else "") + ".csv", "w") as myFile: | ||
dw = csv.DictWriter(myFile, fields, restval="data missing") | ||
dw.writeheader() | ||
for outcome, index in resultTypeIndices.items(): | ||
for name, results in self.summarize()[index].items(): | ||
row = ( | ||
{'name': name, 'baseResult': outcome} | ||
| enumerate(results) | ||
| universalInfo | ||
) | ||
dw.writerow(row) | ||
Comment on lines
-261
to
+274
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
Comment on lines
-261
to
+274
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def simOneElectorate(model, nvot, ncand, ms, nwinners, utilChange, numBuckets, sorter, | ||
pollingMethod, pollingError, pollAfterPert, baseSeed=None, i = 0): | ||
|
@@ -280,9 +284,17 @@ def simOneElectorate(model, nvot, ncand, ms, nwinners, utilChange, numBuckets, s | |
for method, strat, stratArgs in ms: | ||
allIncentives, baseWinners, baseResults = influentialBlocs(electorate, method, nwinners, utilChange, numBuckets, | ||
sorter, strat, stratArgs, pollingMethod, pollAfterPert=pollAfterPert, pollingError=pollingError) | ||
for i, candIncentives in enumerate(allIncentives): | ||
results.append(dict(incentives=candIncentives, isWinner=i in baseWinners, | ||
method=method.__name__, strat=strat.__name__, stratArgs=stratArgs, voterModel=str(model))) | ||
results.extend( | ||
dict( | ||
incentives=candIncentives, | ||
isWinner=i in baseWinners, | ||
method=method.__name__, | ||
strat=strat.__name__, | ||
stratArgs=stratArgs, | ||
voterModel=str(model), | ||
) | ||
for i, candIncentives in enumerate(allIncentives) | ||
) | ||
Comment on lines
-283
to
+297
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
Comment on lines
-283
to
+297
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return results | ||
|
||
def showChart(fileName, norm=1, methodOnly=True, forResult='all', percentages=True, wholeOnly=True): | ||
|
@@ -298,7 +310,7 @@ def showChart(fileName, norm=1, methodOnly=True, forResult='all', percentages=Tr | |
elif norm == 'max': | ||
normFactor = max(rawData) | ||
data = [d/normFactor for d in rawData] | ||
name = re.match(".*(?=:)", row['name']).group(0) if methodOnly else row['name'] | ||
name = re.match(".*(?=:)", row['name'])[0] if methodOnly else row['name'] | ||
Comment on lines
-301
to
+313
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
Comment on lines
-301
to
+313
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
ax.plot([(i+.5)*100/buckets for i in range(buckets)], data, label=name) | ||
ax.set_xlabel("Voter's support for candidate") | ||
ax.set_ylabel("Candidate's incentive to appeal to voter") | ||
|
@@ -308,9 +320,9 @@ def yFormatFunc(value, position): | |
if value == 1: return "Average" | ||
if value == 0: return "0" | ||
if wholeOnly: | ||
if value % 1 != 0: return "" | ||
return f'{int(value)}x Avg' | ||
return "" if value % 1 != 0 else f'{int(value)}x Avg' | ||
return f'{value:1.1f}x Avg' | ||
|
||
ax.yaxis.set_major_formatter(mtick.FuncFormatter(yFormatFunc)) | ||
ax.grid(True) | ||
ax.legend() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,10 +103,9 @@ def realisticBullets( | |
r = random.Random(utils.id).random() | ||
if r < baseBullets + slope * margin: | ||
return cls.bulletBallot(utils) | ||
else: | ||
if otherStrat is None: | ||
otherStrat = cls.honBallot | ||
return otherStrat(utils, electabilities=electabilities) | ||
if otherStrat is None: | ||
otherStrat = cls.honBallot | ||
return otherStrat(utils, electabilities=electabilities) | ||
Comment on lines
-106
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
Comment on lines
-106
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@classmethod | ||
def diehardBallot( | ||
|
@@ -212,7 +211,7 @@ def stratThresholdSearch( | |
bgBallots + fgBallots[:midpoint] + fgBaselineBallots[midpoint:] | ||
) | ||
midpointWinner = cls.winner(cls.results(midpointBallots)) | ||
if not any(midpointWinner == w for w, _ in winnersFound): | ||
if all(midpointWinner != w for w, _ in winnersFound): | ||
Comment on lines
-215
to
+214
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
Comment on lines
-215
to
+214
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
winnersFound.append((midpointWinner, midpoint)) | ||
if midpointWinner == targetWinner: | ||
maxThreshold = midpoint | ||
|
@@ -225,16 +224,16 @@ def resultsFor(cls, voters): | |
"""Create (honest/naive) ballots and get results. | ||
Again, test on subclasses. | ||
""" | ||
return cls.results(list(cls.honBallot(v) for v in voters)) | ||
return cls.results([cls.honBallot(v) for v in voters]) | ||
Comment on lines
-228
to
+227
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
Comment on lines
-228
to
+227
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@staticmethod | ||
def stratTarget2(places): | ||
((frontId, frontResult), (targId, targResult)) = places[0:2] | ||
((frontId, frontResult), (targId, targResult)) = places[:2] | ||
Comment on lines
-232
to
+231
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return (frontId, frontResult, targId, targResult) | ||
|
||
@staticmethod | ||
def stratTarget3(places): | ||
((frontId, frontResult), (targId, targResult)) = places[0:3:2] | ||
((frontId, frontResult), (targId, targResult)) = places[:3:2] | ||
Comment on lines
-237
to
+236
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
return (frontId, frontResult, targId, targResult) | ||
|
||
|
@@ -379,7 +378,7 @@ def strat(voter, polls=None, electabilities=None, candToHelp=None, candToHurt=No | |
|
||
strat.__name__ = strategy.__name__ | ||
for key, value in kw.items(): | ||
strat.__name__ += "_" + str(key)[:4] + str(value)[:4] | ||
strat.__name__ += f"_{str(key)[:4]}{str(value)[:4]}" | ||
Comment on lines
-382
to
+381
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return strat | ||
|
||
|
||
|
@@ -391,7 +390,7 @@ def selectAB(candA, candB): # candA and candB are candidate IDs | |
def fgSelect(voter, **kw): | ||
return max(voter[candA] - voter[candB], 0) | ||
|
||
fgSelect.__name__ = "select" + str(candA) + str(candB) | ||
fgSelect.__name__ = f"select{str(candA)}{str(candB)}" | ||
Comment on lines
-394
to
+393
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return fgSelect | ||
|
||
|
||
|
@@ -467,26 +466,26 @@ def selectV(v, **kw): | |
"factionFraction", | ||
] | ||
for prefix in ["", "min", "t1", "o"]: | ||
for columnName in [ | ||
"fgUtil", | ||
"fgUtilDiff", | ||
"fgSize", | ||
"fgNumHelped", | ||
"fgHelpedUtil", | ||
"fgHelpedUtilDiff", | ||
"fgNumHarmed", | ||
"fgHarmedUtil", | ||
"fgHarmedUtilDiff", | ||
"helpCandElected", | ||
"hurtCandElectedR1", | ||
]: | ||
resultColumns.append(prefix + columnName) | ||
resultColumns.extend( | ||
prefix + columnName | ||
for columnName in [ | ||
"fgUtil", | ||
"fgUtilDiff", | ||
"fgSize", | ||
"fgNumHelped", | ||
"fgHelpedUtil", | ||
"fgHelpedUtilDiff", | ||
"fgNumHarmed", | ||
"fgHarmedUtil", | ||
"fgHarmedUtilDiff", | ||
"helpCandElected", | ||
"hurtCandElectedR1", | ||
] | ||
) | ||
Comment on lines
-470
to
+484
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
|
||
|
||
def makeResults(**kw): | ||
results = {c: kw.get(c, None) for c in resultColumns} | ||
results.update(kw) | ||
return results | ||
return {c: kw.get(c, None) for c in resultColumns} | kw | ||
Comment on lines
-487
to
+488
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
Comment on lines
-487
to
+488
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def makePartialResults(fgVoters, winner, r1Winner, prefix, candToHelp, candToHurt): | ||
|
@@ -703,10 +702,7 @@ def tieFor2Estimate(probs): | |
|
||
|
||
def adaptiveTieFor2(polls, uncertainty=0.15): | ||
if False and len(polls) < 6: | ||
return tieFor2Probs(polls, uncertainty) | ||
else: | ||
return tieFor2Estimate(tuple(pollsToProbs(polls, uncertainty))) | ||
return tieFor2Estimate(tuple(pollsToProbs(polls, uncertainty))) | ||
Comment on lines
-706
to
+705
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
Comment on lines
-706
to
+705
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def appendResults(filename, resultsList, globalComment=dict()): | ||
|
@@ -724,19 +720,7 @@ def appendResults(filename, resultsList, globalComment=dict()): | |
|
||
with open(baseName + str(i) + ".csv", "a") as myFile: | ||
if needsHeader: | ||
print( | ||
"# " + str(globalComment), | ||
# dict( | ||
# media=self.media.__name__, | ||
# version=self.repo_version, | ||
# seed=self.seed, | ||
## model=self.model, | ||
# methods=self.methods, | ||
# nvot=self.nvot, | ||
# ncand=self.ncand, | ||
# niter=self.niter)), | ||
file=myFile, | ||
) | ||
print(f"# {str(globalComment)}", file=myFile) | ||
Comment on lines
-727
to
+723
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
dw = csv.DictWriter(myFile, keys, restval="NA") | ||
dw.writeheader() | ||
for r in resultsList: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
stratStats
refactored with the following changes:for-index-underscore
)