plot.pl 1.64 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
    export_plot/1,
    export_plot_to_png/1
Thierry Martinez's avatar
Thierry Martinez committed
7 8 9 10 11 12 13
  ]
).

plot :-
  biocham_command,
  doc('plots the current trace.'),
  export_plot(plot),
Thierry Martinez's avatar
Thierry Martinez committed
14 15 16 17 18
  execute_plot(['-persist', 'plot.plot']).


execute_plot(Options) :-
  process_create(path(gnuplot), Options, []).
Thierry Martinez's avatar
Thierry Martinez committed
19 20 21 22 23


export_plot(FileTemplate) :-
  biocham_command,
  doc('
Thierry Martinez's avatar
Thierry Martinez committed
24
    saves the current trace into two files:
Thierry Martinez's avatar
Thierry Martinez committed
25 26
    \\argument{FileTemplate}\\texttt{.csv} and \\texttt{.plot}.
  '),
Thierry Martinez's avatar
Thierry Martinez committed
27 28 29 30 31 32 33 34 35 36 37 38
  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
39 40 41
  format(atom(PlotFile), '~a.plot', [FileTemplate]),
  format(atom(CsvFile), '~a.csv', [FileTemplate]),
  export_trace(CsvFile),
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
42 43
  get_trace_data([HeaderRow | _ ]),
  HeaderRow =.. [row, _TimeHeader | Headers],
Thierry Martinez's avatar
Thierry Martinez committed
44 45
  setup_call_cleanup(
    open(PlotFile, write, Stream),
Thierry Martinez's avatar
Thierry Martinez committed
46
    export_plot_stream(Stream, CsvFile, Headers, Options),
Thierry Martinez's avatar
Thierry Martinez committed
47 48 49
    close(Stream)
  ).

Thierry Martinez's avatar
Thierry Martinez committed
50 51 52 53 54 55 56 57 58 59 60
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
61
  write(Stream, '\c
Thierry Martinez's avatar
Thierry Martinez committed
62 63
set style data lines
set datafile separator ","
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
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').