This repository has been archived by the owner on May 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repstat-vimg.pl
162 lines (134 loc) · 4.72 KB
/
repstat-vimg.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use XML::LibXML;
use GD::Graph::lines;
require 'util.pl';
$session = $ARGV[0];
if (!$session) { die "specify session"; }
$indir = "../../data/us/$session/repstats.person";
$outdir = "../../data/us/$session/repstats/images/people";
$outdir2 = "../../data/us/$session/stats/person/missedvotes";
system("mkdir -p $outdir");
system("mkdir -p $outdir2");
# Compute some percentiles for each time period.
# Load in ALL of the statistical data.
foreach my $pid (ScanDir($indir)) {
$pid =~ s/\.xml$//;
my $stats = $XMLPARSER->parse_file("$indir/$pid.xml");
for my $catnode ($stats->documentElement->findnodes('*')) {
for my $histnode ($catnode->findnodes('hist-stat')) {
my $time = $histnode->getAttribute('time');
for my $attrnode ($histnode->findnodes('@*')) {
if ($attrnode->nodeName eq 'time') { next; }
push @{$alldata{$catnode->nodeName}{$attrnode->nodeName}{$time}}, $attrnode->nodeValue;
}
}
}
}
# Generate percentiles.
foreach my $cat (keys(%alldata)) {
foreach my $attr (keys(%{ $alldata{$cat} })) {
foreach my $time (keys(%{ $alldata{$cat}{$attr} })) {
my @vals = sort({$a <=> $b} @{$alldata{$cat}{$attr}{$time}});
for (my $pctile = 0; $pctile < 100; $pctile++) {
$percentile{$cat}{$attr}{$time}[$pctile] = $vals[int($pctile/100 * scalar(@vals))];
}
}
}
}
DrawGraph("novote", "NoVotePct", "votes", undef, "Missed Votes", undef, "Percent of Votes Missed", 50, 90, "percent");
DrawGraph("leaderfollower", "LeaderFollower", "leaderfollower", undef, "Leadership Score", undef, "Leadership Score", undef, undef, "percentile");
sub DrawGraph {
my $cat = shift;
my $attr = shift;
my $filename = shift;
my $shorttitle = shift;
my $longtitle = shift;
my $shortyaxis = shift;
my $longyaxis = shift;
my $percentile1 = shift;
my $percentile2 = shift;
my $scale = shift;
foreach my $pid (ScanDir($indir)) {
$pid =~ s/\.xml$//;
$stats = $XMLPARSER->parse_file("$indir/$pid.xml");
my $smallwidth = 120;
my $resizefactor = 3;
for my $width ($smallwidth, 550) {
my @data = ([], [], [], []);
my $npoints = 0;
for my $node ($stats->documentElement->findnodes("$cat/hist-stat")) {
my $x = $node->getAttribute('time');
my $y = $node->getAttribute($attr);
push @{$data[0]}, NiceQuarter($x);
if ($scale eq 'percent') {
push @{$data[1]}, $y*100;
push @{$data[2]}, $percentile{$cat}{$attr}{$x}[$percentile1]*100;
push @{$data[3]}, $percentile{$cat}{$attr}{$x}[$percentile2]*100;
} elsif ($scale eq 'percentile') {
for (my $pctile = 0; $pctile < 100; $pctile++) {
if ($percentile{$cat}{$attr}{$x}[$pctile] > $y) {
push @{$data[1]}, $pctile;
last;
}
}
}
$npoints++;
}
if (!$npoints) { next; }
my $graph = GD::Graph::lines->new($width * $resizefactor, $width/550*250 * $resizefactor);
$graph->set(
x_label => undef,
y_label => ($width == $smallwidth ? $shortyaxis : $longyaxis),
title => ($width == $smallwidth ? $shorttitle : $longtitle),
x_label_skip => ($npoints / 15),
x_label_position => .5,
x_labels_vertical => 1,
x_plot_values => ($width == $smallwidth ? 0 : 1),
y_plot_values => ($width == $smallwidth ? 0 : 1),
text_space => ($width == $smallwidth ? 0 : undef),
#y_tick_number => 5,
#y_label_skip=> 1,
#y_long_ticks => 1,
#skip_undef => 1,
line_width => $resizefactor*2,
) or die $graph->error;
#$graph->set_legend('This Person');
if ($scale eq 'percentile') {
$graph->set(
y_min_value => 0,
y_max_value => 100);
}
$graph->set_title_font($TTF, 10*$resizefactor);
$graph->set_legend_font($TTF, 8*$resizefactor);
$graph->set_x_label_font($TTF, 8*$resizefactor);
$graph->set_x_axis_font($TTF, 8*$resizefactor);
$graph->set_y_label_font($TTF, ($width == $smallwidth ? 6 : 8)*$resizefactor);
$graph->set_y_axis_font($TTF, 8*$resizefactor);
$graph->set( line_types => [1, 3, 3, 3] ); # 3 = dotted
$graph->set( dclrs => [ qw(red gray gray gray) ] );
$graph->set(transparent => 0);
my $gd_larger = $graph->plot(\@data) or die $graph->error;
my $gd = new GD::Image($width, $width/550*250, 1);
$gd->copyResampled($gd_larger, 0,0, 0,0, $width,$width/550*250, $width * $resizefactor, $width/550*250 * $resizefactor);
my $thumb = '';
if ($width == $smallwidth) { $thumb = '-thumb'; }
open(IMG, ">$outdir/$filename-$pid$thumb.png") or die $graph->error;
binmode IMG;
print IMG $gd->png;
close(IMG);
if ($filename eq "votes") {
open(IMG, ">$outdir2/$pid$thumb.png") or die $graph->error;
binmode IMG;
print IMG $gd->png;
close(IMG);
}
}
}
}
sub NiceQuarter {
my $x = $_[0];
$x =~ s/-Q1/ Jan-Mar/;
$x =~ s/-Q2/ Apr-Jun/;
$x =~ s/-Q3/ Jul-Sep/;
$x =~ s/-Q4/ Oct-Dec/;
return $x;
}