''' Created on May 2, 2020 @author: Brett Paufler (c) Brett Paufler ''' ##################### # # ALL CASES # #################### #Cleaning this up doesn't seem to work #CASE & OPINION Need to Be Defined from judges_utilities import CASE, OPINION #Think of this import as a C Header or Iclude Directive from judges_utilities import load_data def all_cases(): '''2018 Term Year Cases Remember to Call This Fresh for Each Graph''' return load_data() #all_cases() ################################### # # Sanity Check # Number of Cases Appearing # #################################### from judges_utilities import Bag_O_Judges def judges_appearance_case_count(): '''Number of Cases in which Judge States Opinion This is a sanity check, as most should be 72 Kavanaugh a bit less One or two others recusing in one or two only''' total_count = Bag_O_Judges() for case in all_cases(): this_case = set() #a non-repeating list for opinion in case.Opinions: this_case.update([opinion.author]) this_case.update(opinion.joining) total_count.update(this_case) print case print total_count #Looks Good #I Found a Formatting Error in R-69: corrected and tests added #judges_appearance_case_count() #72 Cases for all, except ('Kavanaugh', 65) ##################################### # # Opinions Authored: Graph # Page 2018_A3 # ##################################### def judges_count_of_opinions_authored(): num = 0 #sanity check num_opinions = Bag_O_Judges() for case in all_cases(): for opinion in case.Opinions: num += 1 num_opinions.update([opinion.author]) #print num, sum(num_opinions.values()), num_opinions #Looks Reasonable at 168 Total return num_opinions #print sum(judges_count_of_opinions_authored().values()) #Clearly, this is a throwaway script import matplotlib.pyplot as plt def graph_judges_count_of_opinions_authored(): #Collate Data data = judges_count_of_opinions_authored() judges = [k for k in data.keys()] num_opin = [v for v in data.values()] x_div = range(len(judges)) #print judges #print num_opin #Output Graph plt.subplots(figsize=(15,5)) plt.bar(x_div, num_opin)#, color='black') plt.xticks(x_div, judges) plt.ylabel('Number of Opinions Authored') plt.title('Supreme Court\n2018 Term Year') #plt.show() plt.savefig('./output/2018_A3_opinions_number_authored.png') plt.clf() #graph_judges_count_of_opinions_authored() # # As Per Above But PAGES AUTHORED # def judges_count_of_pages_authored(): '''Page Count of Total Opinions per Judge''' num_pages = Bag_O_Judges() for case in all_cases(): for opinion in case.Opinions: num_pages[opinion.author] += opinion.pages #print num_pages return num_pages #judges_count_of_pages_authored() def graph_judges_pages_of_opinions_authored(): #Collate Data data = judges_count_of_pages_authored() judges = [k for k in data.keys()] num_opin = [v for v in data.values()] x_div = range(len(judges)) #print judges #print num_opin #Output Graph plt.subplots(figsize=(15,5)) plt.bar(x_div, num_opin)#, color='black') plt.xticks(x_div, judges) plt.ylabel('Pages of Opinions Authored') plt.title('Supreme Court\n2018 Term Year') #plt.show() plt.savefig('./output/2018_A3_opinion_pages_authored.png') plt.clf() #graph_judges_pages_of_opinions_authored() def graph_judges_average_opinion_length(): #Collate Data pages = judges_count_of_pages_authored() number = judges_count_of_opinions_authored() judges = [k for k in pages.keys()] pag = pages.values() num = number.values() ave = [] for i in range(len(number)): ave.append(float(pag[i])/float(num[i])) #num_opin = [v for v in data.values()] x_div = range(len(judges)) print judges print num print pag print ave #Output Graph plt.subplots(figsize=(15,5)) plt.bar(x_div, ave)#, color='black') plt.xticks(x_div, judges) plt.ylabel('Average Length of Opinions Authored') plt.title('Supreme Court\n2018 Term Year') #plt.show() plt.savefig('./output/2018_A3_authored_ave.png') plt.clf() #graph_judges_average_opinion_length() # Tabular Data for all Three def table_num_pages_authored(): label_num = judges_count_of_opinions_authored().keys() label_pag = judges_count_of_pages_authored().keys() for label in label_num: assert label in label_pag for label in label_pag: assert label in label_num #print label_num #print label_pag #Both Labels are Functionally Equivalent value_num = judges_count_of_opinions_authored().values() value_pag = judges_count_of_pages_authored().values() #print value_num #print value_pag #There is no sanity check for this #I assume if the preceding was correct, so is this #But this is a fairly good sanity check assert len(label_num) == len(label_pag) == len(value_num) == len(value_pag) #This is the Workhorse, Assemble the Text table = '\n' table += '\n' % ( 'Judge', 'Number', 'Pages', 'Average') for i in range(len(label_num)): table += '\n' % ( label_num[i], value_num[i], value_pag[i], float(value_pag[i])/float(value_num[i]), ) table +='
%s%s%s%s
%s%s%s%.2f
' #Output to Screen and File print table save_name = './output/2018_author_pag_num_table.txt' with open(save_name, 'w') as f: f.write(table) #table_num_pages_authored() ############################################### # # Opinion No Join # Count, Average Length # ############################################### ''' These are Opinions in which The Judge Stood alone ''' def judges_solitary_opinions(): '''Opinions with no Joiners Returns List of List [Judges, Count, Pages, Average] ''' sol_opin_count = Bag_O_Judges() sol_opin_pages = Bag_O_Judges() sol_opin_averg = Bag_O_Judges() for case in all_cases(): for opinion in case.Opinions: if 'None' in opinion.joining: #Some Tests: None and only None assert 1 == len(opinion.joining) for k in sol_opin_averg.keys(): #print opinion assert k not in opinion.joining #Assemle count and page count sol_opin_count[opinion.author] += 1 sol_opin_pages[opinion.author] += opinion.pages #Derive Average for k in sol_opin_averg.keys(): sol_opin_averg[k] = float(sol_opin_pages[k]) / float(sol_opin_count[k]) #Does It Look Good print 'sol_opin_count', sol_opin_count print 'Number of Solitary', sum(sol_opin_count.values()) print 'sol_opin_pages', sol_opin_pages print 'sol_opin_averg', sol_opin_averg #A List of List return [ sol_opin_count.keys(), sol_opin_count.values(), sol_opin_pages.values(), sol_opin_averg.values() ] #print judges_solitary_opinions() def solitary_decisions_output(): '''There is no point in splitting these up This function takes the previous and outputs human friendly table and images ''' #The Data judges, count, pages, average = judges_solitary_opinions() print 'judges', judges print 'count', count print 'pages', pages print 'average', average #It looks hunky dory #Create Table Text table = '\n' table += '\n' % ( 'Judge', 'Number', 'Pages', 'Average') for i in range(len(judges)): table += '\n' % ( judges[i], count[i], pages[i], average[i], ) table +='
%s%s%s%s
%s%s%s%.2f
' #Output to Screen and File print table save_name = './output/2018_solitary_decisions.txt' with open(save_name, 'w') as f: f.write(table) #Raw Count Graph x_div = range(len(judges)) plt.subplots(figsize=(15,5)) plt.bar(x_div, count) plt.xticks(x_div, judges) plt.ylabel('Solitary Decisions\nNumber of Unjoined Opinions') plt.title('Supreme Court\n2018 Term Year') #plt.show() plt.savefig('./output/2018_A3_solitary_count.png') plt.clf() #Clears the Graph #Average Length Graph x_div = range(len(judges)) plt.subplots(figsize=(15,5)) plt.bar(x_div, average) plt.xticks(x_div, judges) plt.ylabel('Solitary Decisions\nAverage Length of Unjoined Opinions') plt.title('Supreme Court\n2018 Term Year') #plt.show() plt.savefig('./output/2018_A3_solitary_average.png') plt.clf() #solitary_decisions_output() #This is probably it on the page #Start new file ''' 2020-05-10 End of Programming Brett Paufler Next Page to have a new file But will be cut and pasted heavily from here ''' #Nope, one more ############################################## # # Spread of Length: Pages by Count # ############################################# from collections import Counter def spread_of_pages(): '''Count of pages of various lenghts 1 page there are 5 2 page there are 16 returned as a list of tuples [(1, 5), (2, 16), (3, 11)... ''' page_count = Counter() for c in all_cases(): for op in c.Opinions: page_count[op.pages] += 1 #print page_count #print page_count.items() #print page_count.keys() return page_count #.items() #[(1, 5), (2, 16), (3, 11), (4, 3), (5, 5), #spread_of_pages() def graph_spread_scatter(): '''Scatterplot Graph of Above''' pages_count = spread_of_pages() print pages_count num_pages, count = zip(*pages_count.items()) print num_pages print count #Scatter Plot Length Spread plt.subplots(figsize=(15,5)) plt.scatter(num_pages, count) #plt.xticks(x_div, judges) plt.xlabel('Opinion Page Length') plt.ylabel('Number of Opinions') plt.title('Supreme Court\n2018 Term Year') #plt.show() plt.savefig('./output/2018_A3_spread_scatter.png') plt.clf() #graph_spread_scatter() from math import ceil def graph_spread_bar(): pages_count = spread_of_pages().items() print pages_count #lenght, count bin_counter = Counter() for p,c in pages_count: b = int(ceil(float(p)/5.0)) #print p, b bin_counter[b] += c print bin_counter bin_pages, count = zip(*bin_counter.items()) print bin_pages print count #Create the X Ticks so it looks like a range x_labels = ['%d-%d' % (int(x-1)*5, int(x)*5) for x in bin_pages] print x_labels #Bar of Spread, should be similar to previous x_div = range(len(bin_pages)) plt.subplots(figsize=(15,5)) plt.bar(x_div, count) plt.xticks(x_div, x_labels) plt.title('Supreme Court\n2018 Term Year') plt.xlabel('Opinion Page Length') plt.ylabel('Number of Opinions') #plt.show() plt.savefig('./output/2018_A3_spread_bar.png') plt.clf() #graph_spread_bar() ''' And that should be all 2020-05-11 Brett Paufler '''