Skullplot.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary Skullplot/ClassifyColumns.pm »
1 package Graphics::Skullplot;
2 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Graphics::Skullplot - Plot the result of an SQL select (e.g. from an emacs shell window)
8  
9 =head1 VERSION
10  
11 Version 0.01
12  
13 =cut
14  
15 our $VERSION = '0.02';
16 my $DEBUG = 0;
17  
18 =head1 SYNOPSIS
19  
20 # To use this from emacs, see scripts/skullplot.el.
21 # That elisp code accesses the perl script: scripts/skullplot.pl
22  
23 # the code used by skullplot.pl
24 my $plot_hints = { indie_count => $indie_count,
25 dependent_requested => $dependent_requested,
26 independent_requested => $independent_requested,
27 };
28 my %gsp_args =
29 ( input_file => $dbox_file,
30 plot_hints => $plot_hints, );
31 $gsp_args{ working_area } = $working_area if $working_area;
32 $gsp_args{ image_viewer } = $image_viewer if $image_viewer;
33 my $gsp = Graphics::Skullplot->new( %gsp_args );
34  
35 $gsp->show_plot_and_exit(); # does an exec
36  
37 =head1 DESCRIPTION
38  
39 Graphics::Skullplot is a module that works with the result from a database
40 select in the common tabular text "data box" format. It has routines
41 to generate and display plots of the data in png format.
42  
43 Internally it uses the L<Table::BoxFormat> module to parse the text table,
44 and the L<Graphics::Skullplot::ClassifyColumns> module to determine the types of the columns.
45  
46 The default image viewer is the ImageMagick "display" command.
47  
48 The immediate use for this code is to act as the back-end for the included
49 Emacs package scripts/skullplot.el, so that database select results
50 generated in an emacs shell window can be immediately plotted.
51  
52 This elisp code calls scripts/skullplot.pl, which might be used in
53 other contexts.
54  
55 =head1 METHODS
56  
57 =over
58  
59 =cut
60  
61 2 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶use File::Basename qw( fileparse basename dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
65 # use List::Util qw( first max maxstr min minstr reduce shuffle sum );
66 # use List::MoreUtils qw( any zip uniq );
67  
68 0 ▶use Image::Magick;
Subroutine BEGIN: called CC=1 SCAR=2.6
69  
70 use lib "../../../Table-Classify/lib";
71 use lib "../../../Data-BoxFormat/lib";
72  
73 use Table::BoxFormat;
74 use Graphics::Skullplot::ClassifyColumns;
75  
76 =item new
77  
78 Creates a new Graphics::Skullplot object.
79 Object attributes:
80  
81 =over
82  
83 =item working_area
84  
85 Scratch location where intermediate files are created.
86 Defaults to "/tmp".
87  
88 =item image_viewer
89  
90 Defaults to 'display', the ImageMagick viewer
91 (a dependency on Image::Magick ensures it's available)
92  
93 =back
94  
95 =cut
96  
97 # required arguments to new
98 has input_file => ( is => 'ro', isa => Str, required => 1); # must be dbox format
99 has plot_hints => ( is => 'ro', isa => HashRef, required => 1);
100  
101 has working_area => ( is => 'rw', isa => Maybe[Str], default => "/tmp" );
102 has image_viewer => ( is => 'rw', isa => Maybe[Str], lazy => 1, builder => "builder_image_viewer" );
103  
104 # mostly for internal use
105 has naming => ( is => 'rw', isa => HashRef, lazy => 1, builder => "generate_output_filenames" );
106  
107 =item builder methods (largely for internal use)
108  
109 builder_image_viewer Currently just returns a hardcoded selection
110 (the ImageMagick "display" program).
111  
112 =cut
113  
114 sub builder_image_viewer {
115 my $self = shift;
116 ($DEBUG) && print STDERR "Running _builder_image_viewer... \n";
117 return "display";
118 }
119  
120 =item generate_output_filenames
121  
122 Example usage:
123  
124 # relies on object settings: "input_file" and "working area"
125 my $fn =
126 generate_filenames();
127 my $basename = $fn->{ base };
128 # full paths to file in $working_area
129 my $tsv_file = $fn->{ tsv };
130 my $png_file = $fn->{ png };
131  
132 =cut
133  
134 sub generate_output_filenames {
135 my $self = shift;
136 my $input_file = $self->input_file || shift;
137 my $working_area = $self->working_area || shift;
138
139 my $basename = basename( $input_file ); # includes file-extension
140  
141 my ($short_base, $ext);
142 if( ( $short_base = $basename ) =~ s{ \. (.*) $ }{}x ) {
143 $ext = $1;
144 }
145  
146 my $tsv_name = $short_base . '.tsv';
147 my $rscript_name = $short_base . '.r';
148 my $png_name = $short_base . '.png';
149
150 my $tsv_file = "$working_area/$tsv_name";
151 my $rscript_file = "$working_area/$rscript_name";
152 my $png_file = "$working_area/$png_name";
153  
154 my %filenames =
155 (
156 base => $basename,
157 base_sans_ext => $short_base,
158 ext => $ext,
159 tsv => $tsv_file,
160 rscript => $rscript_file,
161 png => $png_file
162 );
163 # $self->naming( \%filenames ); # Tue November 13, 2018 20:04 tango
164 return \%filenames;
165 }
166  
167  
168 =item plot_tsv_to_png
169  
170 Generate the r-code to plot the tsv file data as the png file.
171 Takes one argument, a hash of "field metadata".
172  
173 The file names (tsv, png, plus internal formats) come from the
174 "naming" object field.
175  
176 Example usages:
177  
178 $self->plot_tsv_to_png( $plot_cols );
179  
180 =cut
181  
182 sub plot_tsv_to_png {
183 my $self = shift;
184 my $cd = shift;
185 my $fn = $self->naming || shift;
186  
187 # my ($x_field, $y_field, $gb_cats) = @{ $cd->{ qw( x y gb_cats ) }}; # hash slice (mangled)
188  
189 my $x_field = $cd->{ indie_x };
190 my $y_field = $cd->{ y } || $cd->{ dependents_y }->[0] ;
191 my $gb_cats = $cd->{ gb_cats };
192  
193 my ($gb_cat1, $gb_cat2);
194 $gb_cat1 = $gb_cats->[0] if $gb_cats->[0];
195 $gb_cat2 = $gb_cats->[1] if $gb_cats->[1];
196  
197 # plot code
198 my $pc = 'ggplot( skull, ' ;
199 $pc .= ' aes(' ;
200 $pc .= " x = $x_field," ;
201 $pc .= " y = $y_field, " ;
202 $pc .= " colour = $gb_cat1," if $gb_cat1;
203 $pc .= " shape = $gb_cat2 " if $gb_cat2;
204 $pc .= ' ))' ;
205 $pc .= ' + geom_point( ' ;
206 $pc .= " size = 2.5 " ;
207 $pc .= ' ) ' ;
208  
209 $self->generate_png_file( $pc, $fn );
210 }
211  
212 =item generate_png_file
213  
214 Example usage:
215  
216 $self->generate_png_file( $pc, $fn );
217  
218 Runs the given plot code (first argument) using the file-name metadata
219 (second argument, defaults to object's L<naming>), saving the
220 plot as a png file ($fn->{png}).
221  
222 This generates a file of R code to run with an Rscript call.
223 In debug mode, this generates a standalone unix script. ($DEBUG).
224  
225 =cut
226  
227 sub generate_png_file {
228 my $self = shift;
229 my $pc = shift;
230 my $fn = shift || $self->naming;
231  
232 my $tsv_file = $fn->{ tsv };
233 my $rscript_file = $fn->{ rscript };
234 my $png_file = $fn->{ png };
235  
236 # Generate the file of R code to run with an Rscript call
237 # (in debug mode, make it a standalone unix script)
238 my $r_code;
239 $r_code = qq{#!/usr/bin/Rscript} . "\n" if $DEBUG;
240  
241 $r_code .=<<"__END_R_CODE";
242 library(ggplot2)
243 skull <- read.delim("$tsv_file", header=TRUE)
244 png("$png_file") # send plot output to png
245 $pc
246 graphics.off() # doesn't chatter like dev.off
247 __END_R_CODE
248  
249 print $r_code, "\n" if $DEBUG;
250  
251 open my $out_fh, '>', $rscript_file;
252 print { $out_fh } $r_code;
253 close( $out_fh );
254  
255 # in case you want to run the rscript standalone
256 chmod 0755, $rscript_file;
257  
258 # chdir( "$HOME/tmp" ) or die "$!";
259  
260 my $erroff = '2>/dev/null';
261 $erroff = '' if $DEBUG;
262  
263 my $cmd = "Rscript $rscript_file $erroff";
264  
265 print STDERR "cmd:\n$cmd\n" if $DEBUG;
266 system( $cmd );
267 }
268  
269  
270  
271  
272 =item display_png_and_exit
273  
274 Open the given png file in an image viewer
275 Defaults to "png" field in object's "naming".
276  
277 This internally does an exec: it should be
278 the last thing called.
279  
280 The image viewer can be set as the second, optional field.
281 The default image viewer is ImageMagick's "display".
282  
283 Example usage:
284  
285 my $naming = $self->naming;
286 my $png_file = $naming->{ png };
287 $self->display_png_and_exit( $png_file );
288  
289 =cut
290  
291 sub display_png_and_exit {
292 my $self = shift;
293 my $fn = $self->naming;
294 my $png_file = shift || $fn->{ png };
295 my $basename = $fn->{ base } || '';
296  
297 my $image_viewer = $self->image_viewer || shift;
298  
299 my $erroff = ' 2>/dev/null';
300 $erroff = '' if $DEBUG;
301  
302 my $vcmd;
303 if ($image_viewer eq 'display') {
304 my $title = "skullplot";
305 # $title .= ": $basename" if $basename;
306 $vcmd = qq{ display -title $title $png_file $erroff };
307 } else {
308 $vcmd = qq{ $image_viewer $png_file $erroff };
309 }
310 exec( $vcmd );
311 }
312  
313  
314  
315 =item show_plot_and_exit
316  
317 The method called by the skullplot.pl script to actually
318 plot the data from a "data box format" file, using the
319 plot_hints.
320  
321 It's expected that the dbox file (L<input_file>) and the
322 L<plot_hints> will be defined at object creation, but at
323 present those settings may be overridden here and given as
324 first and second arguments.
325  
326 This should be used at the end of the program (internally
327 it does an "exec").
328  
329 =cut
330  
331 sub show_plot_and_exit {
332 my $self = shift;
333 if ($DEBUG) { say "Running show_plot_and_exit: "; $self->dumporama() };
334  
335 my $dbox_file = $self->input_file || shift;
336  
337 my $naming = $self->naming;
338  
339 my $dbox_name = $naming->{ base };
340 my $tsv_file = $naming->{ tsv };
341 ($DEBUG) && print "input dbox name: $dbox_name\nintermediate tsv_file: $tsv_file\n";
342  
343 # the input from the dbox file output directly to a tsv file
344 my $dbx = Table::BoxFormat->new( input_file => $dbox_file );
345 my $data = $dbx->output_to_tsv( $tsv_file ); # also returns a ref to an array of arrays
346  
347 my $plot_cols = $self->classify_columns( $data );
348  
349 $self->plot_tsv_to_png( $plot_cols ); # Note: uses naming from object
350  
351 if ($DEBUG) { say "About to display png: "; $self->dumporama() };
352 $self->display_png_and_exit();
353 }
354  
355  
356  
357 =item classify_columns
358  
359 Given a reference to the tabular data in the form of an array of arrays,
360 returns metadata for each column to be used in deciding how to plot
361 the data.
362  
363 Example usage:
364  
365 my $plot_cols = $self->classify_columns( $data );
366  
367  
368 Classify the columns from the tabular data, returning a "fields_metadata" hash ref.
369  
370 This is a wrapper around a provisional technique to make it easier to swap in
371 better ones later.
372  
373 At present, the metadata fields are:
374  
375 x => $x_field (( rename indie_x ))
376 y => $y_field
377 gb_cats => [ @gb_cats ]
378 dependents_y => [ @dependents_y ]
379  
380 =cut
381  
382 sub classify_columns {
383 my $self = shift;
384 my $data = shift;
385  
386 my $opt = $self->plot_hints || shift;
387 # my $indie_count = $opt->{ indie_count };
388  
389 # use the tsv data to analysis the column types,
390 # determine what to try to plot
391 my $dc = Graphics::Skullplot::ClassifyColumns->new( data => $data );
392 my $plot_cols =
393 $dc->classify_columns_simple( $opt );
394  
395 return $plot_cols;
396 }
397  
398  
399  
400  
401  
402 =item dumporama
403  
404 Report on state of object fields.
405  
406 =cut
407  
408 sub dumporama {
409 my $self = shift;
410 say "Graphics::Skullplot self: ", Dumper( $self );
411 printf "input_file: %s\n" , $self->input_file;
412 # printf "plot_hints: %s\n" , $self->fryhash( $self->plot_hints );
413 printf "plot_hints: %s\n" , Dumper( $self->plot_hints );
414 printf "working_area: %s\n" , $self->working_area;
415 printf "image_viewer: %s\n" , $self->image_viewer;
416 # printf "naming: %s\n" , $self->fryhash( $self->naming );
417 printf "naming: %s\n" , Dumper( $self->naming );
418 }
419  
420  
421  
422 =item fryhash
423  
424 =cut
425  
426 sub fryhash {
427 my $self = shift;
428 my $href = shift;
429  
430 my @keys = keys %{ $href };
431 my $rep;
432 $rep = "\n" if @keys;
433 foreach my $k (@keys) {
434 my $val = $href->{ $k } || '';
435 if( ref $val eq 'HASH') {
436 $rep .= $self->fryhash( $val );
437 } elsif( ref $val eq 'ARRAY') {
438 $rep .= join " ", @{ $val };
439 } else {
440 $rep .= sprintf " %25s: %s\n", $k, $val;
441 }
442 }
443 return $rep;
444 }
445  
446  
447  
448 =back
449  
450 =head1 NOTES
451  
452 =head2 TODO
453  
454 =over
455  
456 =item *
457  
458 Limited to two group by categories (in addition to the x-axis): used with colour & shape
459 If there's more than 2, fuse them together into a compound, use with colour
460  
461 =item *
462  
463 See R Graphics Cookbook, p.205: setting up the tics and labels.
464  
465 $pc .= 'p + scale_x_date';
466 $pc .= '';
467  
468 =item *
469  
470 Currently this defaults to viewing images using the "display" program.
471 Alternately, the builder_image_viewer could scan through a list of
472 likely viewers and pick the first that's installed.
473  
474 =back
475  
476  
477 =head1 AUTHOR
478  
479 Joseph Brenner, E<lt>doom@kzsu.stanford.eduE<gt>,
480 16 Nov 2016
481  
482 =head1 COPYRIGHT AND LICENSE
483  
484 Copyright (C) 2016 by Joseph Brenner
485  
486 This program is free software; you can redistribute it and/or modify it
487 under the terms of either: the GNU General Public License as published
488 by the Free Software Foundation; or the Artistic License.
489  
490 See http://dev.perl.org/licenses/ for more information.
491  
492 =cut
493  
494 1;
↑ Summary Skullplot/ClassifyColumns.pm »