Graphics-Skullplot

 view release on metacpan or  search on metacpan

lib/Graphics/Skullplot.pm  view on Meta::CPAN

Graphics::Skullplot is a module that works with the result from a database 
select in the common tabular text "data box" format. It has routines 
to generate and display plots of the data in png format.

Internally it uses the L<Table::BoxFormat> module to parse the text table,
and the L<Graphics::Skullplot::ClassifyColumns> module to determine the types of the columns.

The default image viewer is the ImageMagick "display" command.

The immediate use for this code is to act as the back-end for the included 
Emacs package scripts/skullplot.el, so that database select results 
generated in an emacs shell window can be immediately plotted.  

This elisp code calls scripts/skullplot.pl, which might be used in
other contexts.

=head1 METHODS

=over

=cut

use 5.10.0;
use Carp;
use Data::Dumper;
use File::Basename  qw( fileparse basename dirname );
# use List::Util      qw( first max maxstr min minstr reduce shuffle sum );
# use List::MoreUtils qw( any zip uniq );

use Image::Magick;

use lib "../../../Table-Classify/lib";   
use lib "../../../Data-BoxFormat/lib";  

use Table::BoxFormat;
use Graphics::Skullplot::ClassifyColumns;

=item new

Creates a new Graphics::Skullplot object.
Object attributes:

=over

=item working_area

Scratch location where intermediate files are created.
Defaults to "/tmp".

=item image_viewer

Defaults to 'display', the ImageMagick viewer
(a dependency on Image::Magick ensures it's available)

=back

=cut

# required arguments to new 
has input_file => ( is => 'ro', isa => Str,      required => 1);  # must be dbox format 
has plot_hints => ( is => 'ro', isa => HashRef,  required => 1);

has working_area => ( is => 'rw', isa => Maybe[Str], default => "/tmp" );
has image_viewer => ( is => 'rw', isa => Maybe[Str], lazy => 1, builder => "builder_image_viewer" );  

# mostly for internal use
has naming         => ( is => 'rw', isa => HashRef, lazy => 1, builder => "generate_output_filenames" ); 

=item builder methods (largely for internal use)

builder_image_viewer Currently just returns a hardcoded selection
(the ImageMagick "display" program).

=cut 

sub builder_image_viewer {
  my $self = shift;
  ($DEBUG) && print STDERR "Running _builder_image_viewer... \n";
  return "display";
}

=item generate_output_filenames

Example usage: 

  # relies on object settings: "input_file" and "working area"
  my $fn = 
    generate_filenames();
  my $basename = $fn->{ base };
  # full paths to file in $working_area
  my $tsv_file  = $fn->{ tsv };  
  my $png_file  = $fn->{ png };  

=cut 

sub generate_output_filenames {
  my $self = shift;
  my $input_file   = $self->input_file   || shift;
  my $working_area = $self->working_area || shift;
  
  my $basename = basename( $input_file ); # includes file-extension

  my ($short_base, $ext);
  if( ( $short_base = $basename ) =~ s{ \. (.*) $ }{}x ) { 
    $ext = $1;
  }

  my $tsv_name     = $short_base . '.tsv';
  my $rscript_name = $short_base . '.r';
  my $png_name     = $short_base . '.png';
  
  my $tsv_file     = "$working_area/$tsv_name";
  my $rscript_file = "$working_area/$rscript_name";
  my $png_file     = "$working_area/$png_name";

  my %filenames =
    (
     base             => $basename,
     base_sans_ext    => $short_base,
     ext              => $ext,
     tsv              => $tsv_file,



( run in 2.309 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )