'''
Created on Jun 10, 2020
@author: Brett Paufler
(c) Copyright Brett Paufler
Start: 2020-06-10
Finish: 2020-06-17
Added Data in Alt Text: 2020-06-24
1: Combine Author Joined to Single List
2: For Each Judge
+1/-1 Each Other Judge
Including Me
Normalized to +1.0 to -1.0
3: 10 Graphs
ERROR: CASE-62
Sotomayor Both Author and Joining
Not Major
But it is an error
My first assemblage of mass did not include Paufler
Quick to Change (like ten minutes)
But Code Reasoned Prior to Inclusion
'''
from judges_utilities import CASE, OPINION
from judges_utilities import Bag_O_Judges
from judges_utilities import load_data
from collections import OrderedDict
import matplotlib.pyplot as plt
def Bag_With_Paufler():
'''A Bag_O_Judges including a Trailing Paufler'''
this_bag = Bag_O_Judges()
this_bag['Paufler'] = 0
return this_bag
#print Bag_With_Paufler()
#Provides Two List of Lists
#All Judges in Agreement for Each Opinion
#And all those not in agreement
clusters = [] #Agree with Opinion
anti_clusters = [] #Disagree with Opinion
for a_case in load_data():
#print a_case
for a_opinion in a_case.Opinions:
#print a_opinion
#Add Author and Joining
judges = list()
judges.append(a_opinion.author)
judges += a_opinion.joining
#Add Paufler if a Good Opinion
if a_opinion.good:
judges.append('Paufler')
#Removes 'Per Curium'
if 'Per Curiam' in judges:
judges.remove('Per Curiam')
if len(judges) == 0:
#print judges
assert True == False
if 'None' in judges:
judges.remove('None')
#print judges
assert len(judges) <= 10
#Creates anti_judges as a compliment of judges
anti_judges = Bag_With_Paufler().keys()
for j in judges:
#print j
#print a_case
anti_judges.remove(j)
#print judges
#print anti_judges
#More Tests
#Data looks Validated
assert 10 == len(judges + anti_judges)
assert 10 == len(set(judges + anti_judges))
clusters.append(judges)
anti_clusters.append(anti_judges)
#looking Good
assert len(clusters) == len(anti_clusters)
#More Quality Control
for yes,no in zip(clusters, anti_clusters):
#print yes, no
assert 10 == len(yes + no)
assert 10 == len(set(yes + no))
assert set(yes + no) == set(Bag_With_Paufler().keys())
for j in Bag_With_Paufler().keys():
assert j in yes + no
num_opinions = len(clusters)
assert 168 == len(clusters)
assert 168 == len(anti_clusters)
#print num_opinions #168 - As Per Previous
#A Bag_of_Judges for each Judge
mass = OrderedDict()
for j in Bag_With_Paufler().keys():
mass[j] = Bag_With_Paufler()
# +1 to each member in group
for this_judge in Bag_With_Paufler().keys():
#+1 if agreeing for all in agreement
for cluster in clusters:
if this_judge in cluster:
for c in cluster:
mass[this_judge][c] += 1
#-1 if disagreeing for all in disagreement
for a_cluster in anti_clusters:
if this_judge in a_cluster:
for c in a_cluster:
mass[this_judge][c] += 1
#print mass
#ALL Judges Agree with themselves 168 Times
for this_judge in Bag_With_Paufler().keys():
assert 168 == mass[this_judge][this_judge]
#Normalize from 0 to 168 to 0.0 to 1.0
for first in mass.keys():
for second in mass[first].keys():
#print mass[first][second]
mass[first][second] = float(mass[first][second]) / 168.0
#print mass[first][second]
#print mass
# # # # # # # # # # # # # # # # # # #
#
# GRAPHING: One Per Judge
#
# # # # # # # # # # # # # # # # # # #
min_agree = 1.0
max_agree = 0.0
#Absolute Scale .4 equals 40% agreement
for judge, relations in mass.items():
#print judge, relations
peers = relations.keys()
ratings = relations.values()
#print peers
#print ratings
x_div = range(len(peers))
#print x_div, peers
plt.subplots(figsize=(15,2.5))
plt.bar(x_div, ratings)
plt.xticks(x_div, peers)
#plt.title('Supreme Court: 2018 Term Year\nRelative Agreement')
#plt.xlabel('Opinion Page Length')
plt.ylabel(judge + '\n\n')
#plt.show()
save_name = './output/2018_A5_%s.png' % judge
plt.savefig(save_name)
plt.clf()
'''
#This was all to find min/max values
#To properly truncate output in next graph
#min_agree = min(ratings + [min_agree])
#ratings.remove(1.0)
#max_agree = max(ratings + [max_agree])
#print 'min_agree', min_agree
#min_agree = 0.422619047619
#print 'max_agree', max_agree
#max_agree 0.875
'''
#Normalized & Zeroes self out
for judge, relations in mass.items():
#print judge, relations
#Brings Self Value down to minimum
relations[judge] = 0.422619047619
peers = relations.keys()
ratings = relations.values()
#Push Lower Bound to Zero
#Stretch Upper Bound to Top
ratings = [(r - 0.422619047619)
/ (0.875 - 0.422619047619)
for r in ratings]
x_div = range(len(peers))
#print x_div, peers
plt.subplots(figsize=(15,2.5))
plt.bar(x_div, ratings)
plt.xticks(x_div, peers)
plt.yticks([0.0, 0.25, 0.5, 0.75, 1.0])
#plt.title('Supreme Court: 2018 Term Year\nRelative Agreement')
#plt.xlabel('Opinion Page Length')
plt.ylabel(judge + '\n\n')
#plt.show()
save_name = './output/2018_A5n_%s.png' % judge
plt.savefig(save_name)
plt.clf()
#
# img tags for HTML Page
# I could do this inline, above
# But it will be clearer this way
#
#The form is the same except an 'n'
# Which I'll add with a replace
#1st = './output/2018_A5_%s.png' % judge
#2nd = './output/2018_A5n_%s.png' % judge
text = ''
#Top Loop
#Loop Creating an img tag for each judge/image
for judge, relations in mass.items():
#print judge, relations
img_text = '2018_A5_%s.png' % judge
text += '
\n'
text += '
\n'
text += '\n'
text += '\n'
text += 'Data Above - Normalized Below\n'
text += '\n'
text += '\n'
#Bottom Loop
#Normalized Values
#Loop Creating an img tag for each judge/image
for judge, relations in mass.items():
#print judge, relations
img_text = '2018_A5n_%s.png' % judge
text += '
\n'
text += '
\n'
txt_file = './output/text_img_tags.txt'
with open(txt_file, 'w') as f:
f.write(text)