Now, what if we want to generate the probability fields with PDL functions (and funky broadcasting) instead of using Imager::Heatmap? Why didn't we do that before the other stuff? Because the relevant GSL function (gsl_ran_bivariate_gaussian_pdf) didn't have a binding in PDL::GSL::RNG. It, along with most of the other PDF functions, does now on git master (to be released soon).
This uses some moderately fancy dimension stuff (the slice is to achieve appropriate dummy dims) to achieve broadcasting over the whole frame's calculation at once; because there are 300x300x100 = 9e6 elements in each proto-frame before it gets sumover-ed, that exceeds the 1e6 default limit for pthreading, so it automatically pthreads. That doesn't go a vast amount faster on my little machine, but I would expect speedups on bigger ones - mainly because it currently doesn't use the windowing trick that Imager::Heatmap does in only calculating probabilities above epsilon, though it could with a bit more fancy footwork.
use strict;
use warnings;
use PDL;
use PDL::GSL::RNG qw(ran_bivariate_gaussian_pdf);
my @insert = sample_data();
my ($XSIZE, $YSIZE) = (300, 300);
my ($xcoord, $ycoord, $weight) = pdl(\@insert) # xyw nweights
->slice(",*$XSIZE,*$YSIZE,") # xyw nx ny nweights
->using(0..2); # nx ny nweights
my $xbase = xvals($XSIZE)->slice(",*$YSIZE"); # nx ny
my $ybase = xvals($YSIZE)->slice("*$XSIZE,"); # nx ny
$| = 1; print "Generating GIF frame ";
# replace Imager::Heatmap::insert_datas
my @hms = map {
my $h = (
$weight * ran_bivariate_gaussian_pdf(
$xcoord-$xbase, $ycoord-$ybase, $_, $_, 0
) # nx ny nweights
)->mv(-1,0)->sumover; # nx ny
print ".";
$h;
} 1..90;
my $all_vals = pdl(\@hms); # dims: nx ny frame
# broadcasting version of Imager::Heatmap::draw
use PDL::Graphics::ColorSpace;
my $frame_max = $all_vals->clump(0,1)->maxover; # dims: nxy frame, the
+n frame
$all_vals /= $frame_max->slice("*1,*1,"); # nx ny frame
my $hue = (1 - $all_vals)*240;
my $d = cat($hue, pdl(1), pdl(1))->mv(-1,0); # hsv nx ny fram
+e
my $all_hms = (hsv_to_rgb($d) * 255)->byte; # rgb nx ny fram
+e
# Imager::write_multi
unlink($_), $all_hms->wmpeg($_) for 'heatmapb.gif';
print "\n";
sub sample_data {
my @insert = ();
while (<DATA>) {
chomp; push @insert, [ split /\s+/ ]
}
return @insert
}
__DATA__
...
|