Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions .ipynb_checkpoints/vse2-checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def __init__(self, model, methodsAndStrats,
fgs.extend([(paramStrat(m.diehardBallot, intensity=i), targetFunc) for i in m.diehardLevels]
+ [(paramStrat(m.compBallot, intensity=i), targetFunc) for i in m.compLevels])
fgs.append((swapPolls(m.lowInfoBallot), targetFunc))
for bg in [m.honBallot, m.lowInfoBallot]:
ms.append((m, bg, fgs))
ms.extend((m, bg, fgs) for bg in [m.honBallot, m.lowInfoBallot])
else:
ms.append(m)
for i in range(niter):
Expand All @@ -55,12 +54,11 @@ def saveFile(self, baseName="SimResults"):
i = 1
while os.path.isfile(baseName + str(i) + ".csv"):
i += 1
myFile = open(baseName + str(i) + ".csv", "w")
dw = csv.DictWriter(myFile, self.rows[0].keys(), restval="NA")
dw.writeheader()
for r in self.rows:
dw.writerow(r)
myFile.close()
with open(baseName + str(i) + ".csv", "w") as myFile:
dw = csv.DictWriter(myFile, self.rows[0].keys(), restval="NA")
dw.writeheader()
for r in self.rows:
dw.writerow(r)


def compareStrats(method, model, backgroundStrat, nvot, ncand, niter): pass
2 changes: 1 addition & 1 deletion ballotEval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Author

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:

Copy link
Author

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:

electorate = model(nvot, ncand)
if usePolls:
pollBallots = [Approval.zeroInfoBallot(v, pickiness=pickiness) for v in electorate]
Expand Down
52 changes: 32 additions & 20 deletions cid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function influentialBlocs refactored with the following changes:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function influentialBlocs refactored with the following changes:

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
Expand Down Expand Up @@ -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]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CID.__init__ refactored with the following changes:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CID.__init__ refactored with the following changes:

self.rows = []
args = (model, nvot, ncand, ms, nwinners, utilChange, numBuckets, sorter, pollingMethod, pollingError, pollAfterPert)
with multiprocessing.Pool(processes=7) as pool:
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CID.chart refactored with the following changes:

Comment on lines -236 to +239
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CID.chart refactored with the following changes:

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)
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CID.saveFile refactored with the following changes:

Comment on lines -261 to +274
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CID.saveFile refactored with the following changes:


def simOneElectorate(model, nvot, ncand, ms, nwinners, utilChange, numBuckets, sorter,
pollingMethod, pollingError, pollAfterPert, baseSeed=None, i = 0):
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function simOneElectorate refactored with the following changes:

Comment on lines -283 to +297
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function simOneElectorate refactored with the following changes:

return results

def showChart(fileName, norm=1, methodOnly=True, forResult='all', percentages=True, wholeOnly=True):
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function showChart refactored with the following changes:

Comment on lines -301 to +313
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function showChart refactored with the following changes:

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")
Expand All @@ -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()
Expand Down
72 changes: 28 additions & 44 deletions dataClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Method.realisticBullets refactored with the following changes:

Comment on lines -106 to +108
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Method.realisticBullets refactored with the following changes:


@classmethod
def diehardBallot(
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Method.stratThresholdSearch refactored with the following changes:

Comment on lines -215 to +214
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Method.stratThresholdSearch refactored with the following changes:

winnersFound.append((midpointWinner, midpoint))
if midpointWinner == targetWinner:
maxThreshold = midpoint
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Method.resultsFor refactored with the following changes:

Comment on lines -228 to +227
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Method.resultsFor refactored with the following changes:


@staticmethod
def stratTarget2(places):
((frontId, frontResult), (targId, targResult)) = places[0:2]
((frontId, frontResult), (targId, targResult)) = places[:2]
Comment on lines -232 to +231
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Method.stratTarget2 refactored with the following changes:

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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Method.stratTarget3 refactored with the following changes:


return (frontId, frontResult, targId, targResult)

Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function paramStrat refactored with the following changes:

return strat


Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function selectAB refactored with the following changes:

return fgSelect


Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 470-483 refactored with the following changes:



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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function makeResults refactored with the following changes:

Comment on lines -487 to +488
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function makeResults refactored with the following changes:



def makePartialResults(fgVoters, winner, r1Winner, prefix, candToHelp, candToHurt):
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function adaptiveTieFor2 refactored with the following changes:

Comment on lines -706 to +705
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function adaptiveTieFor2 refactored with the following changes:



def appendResults(filename, resultsList, globalComment=dict()):
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function appendResults refactored with the following changes:

This removes the following comments ( why? ):

#              version=self.repo_version,
#              seed=self.seed,
# media=self.media.__name__,
#              niter=self.niter)),
# dict(
#              nvot=self.nvot,
#              ncand=self.ncand,
#              methods=self.methods,
##              model=self.model,

dw = csv.DictWriter(myFile, keys, restval="NA")
dw.writeheader()
for r in resultsList:
Expand Down
Loading