in reply to Animated Heatmap

Imager::Heatmap makes pretty pictures, but what if we want to only use it to generate its data, then render it to an animated GIF using PDL instead of Imager? Uses a modified, broadcasting version of Heatmap in PDL to make heatmaps, and PDL animation of bouncing ball to write.
use strict; use warnings; use Imager::Heatmap; use PDL; my @insert = sample_data(); $| = 1; print "Generating GIF frame "; my %size = (xsize => 300, ysize => 300); my $t0 = time; my @hms = map { my $h = Imager::Heatmap->new(%size, xsigma => $_, ysigma => $_); $h->insert_datas(@insert); print "."; $h; } 1..90; print " time now=", time - $t0; my $all_vals = pdl map $_->matrix, @hms; # broadcasting version of Imager::Heatmap::draw use PDL::Graphics::ColorSpace; my $frame_max = $all_vals->maxover; $all_vals /= $frame_max->dummy(0); # dims: x*y frame my $hue = (1 - $all_vals)*240; my $d = cat($hue, pdl(1), pdl(1))->mv(-1,0); # dims: hsv x*y frame my $all_hms = (hsv_to_rgb($d) * 255)->byte # rgb x*y frame ->splitdim(1,300); # rgb x y frame # Imager::write_multi $all_hms->wmpeg('heatmapb.gif'); print " and now=", time - $t0, "\n"; sub sample_data { my @insert = (); while (<DATA>) { chomp; push @insert, [ split /\s+/ ] } return @insert } __DATA__ ...

Replies are listed 'Best First'.
Re^2: Animated Heatmap
by etj (Priest) on Aug 16, 2024 at 19:43 UTC
    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__ ...