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__ ...

In reply to Re^2: Animated Heatmap by etj
in thread Animated Heatmap by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.