-
Notifications
You must be signed in to change notification settings - Fork 27
/
plot.pl
executable file
·77 lines (67 loc) · 1.4 KB
/
plot.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
#!/usr/bin/perl --
use strict;
use warnings;
use Getopt::Std;
sub check_prereq{
if( !`which gnuplot` ){
die "gnuplot needs installed in \$PATH$/";
}
if( !`which psql` ){
die "psql needs installed in \$PATH$/";
}
}
sub usage{
print STDERR <<_HELP;
Usage: $0 [-h] [-d dbname] [-k number]
-h: print this help
-d: database to connect
-k: number of class
_HELP
0;
}
sub main{
&check_prereq();
my %opt;
getopts('hd:k:', \%opt);
if( $opt{'h'} ){
return &usage;
}
my $dbname = $opt{'d'} || "db1";
my $k = $opt{'k'} || 5;
my $sql = <<_SQL;
SELECT kmeans(ARRAY[val1, val2], $k) OVER (), val1, val2
FROM testdata
ORDER BY 1
_SQL
$sql = <<_SQL;
SELECT kmeans(ARRAY[longitude, latitude], 2, array[135.0, 35.0, 139.0, 38.0]) OVER (), longitude, latitude
FROM pref
WHERE latitude IS NOT NULL AND longitude IS NOT NULL
ORDER BY 1
_SQL
print $sql . $/;
open my $fh, "-|", qq(psql -A -F ' ' -t -c "$sql" $dbname) or die $!;
open my $out, ">", "tmp.dat" or die $!;
my $prev_k = 0;
while( <$fh> ){
chomp;
my( $k, $v1, $v2 ) = split / /, $_;
if( $k != $prev_k ){
print $out "\n\n";
$prev_k = $k;
}
print $out "$v1 $v2\n";
}
close $out;
close $fh;
my @buf;
for my $i ( 0 .. $prev_k ){
push @buf, "\"tmp.dat\" index $i:$i using 1:2";
}
my $plotcmd = 'gnuplot -e \'plot ' . join( ", ", @buf ) . '; pause -1\'';
print $plotcmd . $/;
return `$plotcmd`;
}
if( $0 eq __FILE__ ){
&main( @ARGV );
}