plot.pl 2.04 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3 4
:- module(
  plot,
  [
    plot/0,
Thierry Martinez's avatar
Thierry Martinez committed
5 6 7
    set_plot_driver/1,
    gnu_plot/0,
    plot_png/0,
Thierry Martinez's avatar
Thierry Martinez committed
8 9
    export_plot/1,
    export_plot_to_png/1
Thierry Martinez's avatar
Thierry Martinez committed
10 11 12 13 14 15
  ]
).

plot :-
  biocham_command,
  doc('plots the current trace.'),
Thierry Martinez's avatar
Thierry Martinez committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29
  doc('\\example'),
  get_plot_driver(Driver),
  Driver.


get_plot_driver(Driver) :-
  nb_getval(plot_driver, Driver).


set_plot_driver(Driver) :-
  nb_setval(plot_driver, Driver).


gnu_plot :-
Thierry Martinez's avatar
Thierry Martinez committed
30
  export_plot(plot),
Thierry Martinez's avatar
Thierry Martinez committed
31 32 33
  execute_plot(['-persist', 'plot.plot']).


Thierry Martinez's avatar
Thierry Martinez committed
34 35 36 37 38 39 40
plot_png :-
  count(plot_png, Index),
  format(atom(Filename), 'plot~d.png', [Index]),
  export_plot_to_png(Filename),
  format('<img src="~a">', [Filename]).


Thierry Martinez's avatar
Thierry Martinez committed
41 42
execute_plot(Options) :-
  process_create(path(gnuplot), Options, []).
Thierry Martinez's avatar
Thierry Martinez committed
43 44 45 46 47


export_plot(FileTemplate) :-
  biocham_command,
  doc('
Thierry Martinez's avatar
Thierry Martinez committed
48
    saves the current trace into two files:
Thierry Martinez's avatar
Thierry Martinez committed
49 50
    \\argument{FileTemplate}\\texttt{.csv} and \\texttt{.plot}.
  '),
Thierry Martinez's avatar
Thierry Martinez committed
51 52 53 54 55 56 57 58 59 60 61 62
  export_plot(FileTemplate, []).


export_plot_to_png(OutputFile) :-
  biocham_command,
  type(OutputFile, output_file),
  doc('plots the current trace in a PNG file'),
  export_plot(plot, [png(OutputFile)]),
  execute_plot(['plot.plot']).


export_plot(FileTemplate, Options) :-
Thierry Martinez's avatar
Thierry Martinez committed
63 64 65
  format(atom(PlotFile), '~a.plot', [FileTemplate]),
  format(atom(CsvFile), '~a.csv', [FileTemplate]),
  export_trace(CsvFile),
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
66 67
  get_trace_data([HeaderRow | _ ]),
  HeaderRow =.. [row, _TimeHeader | Headers],
Thierry Martinez's avatar
Thierry Martinez committed
68 69
  setup_call_cleanup(
    open(PlotFile, write, Stream),
Thierry Martinez's avatar
Thierry Martinez committed
70
    export_plot_stream(Stream, CsvFile, Headers, Options),
Thierry Martinez's avatar
Thierry Martinez committed
71 72 73
    close(Stream)
  ).

Thierry Martinez's avatar
Thierry Martinez committed
74 75 76 77 78 79 80 81 82 83 84
export_plot_stream(Stream, CsvFile, Headers, Options) :-
  (
    member(png(Filename), Options)
  ->
    format(Stream, '\c
set term png
set output "~a"
', [Filename])
  ;
    true
  ),
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
85
  write(Stream, '\c
Thierry Martinez's avatar
Thierry Martinez committed
86 87
set style data lines
set datafile separator ","
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
plot '),
  \+ (
    nth1(Index, Headers, Header),
    \+ (
      (
        Index > 1
      ->
        write(Stream, ', ')
      ;
        true
      ),
      ColumnIndex is Index + 1,
      format(
        Stream,
        '"~a" using 1:~d title "~a"',
        [CsvFile, ColumnIndex, Header]
      )
    )
  ),
  write(Stream, '\n').