plot.pl 2.01 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
    set_plot_driver/1,
    gnu_plot/0,
Thierry Martinez's avatar
Thierry Martinez committed
7
    gnu_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
  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
29
  export_plot(plot),
Thierry Martinez's avatar
Thierry Martinez committed
30 31 32
  execute_plot(['-persist', 'plot.plot']).


Thierry Martinez's avatar
Thierry Martinez committed
33
gnu_plot_png :-
Thierry Martinez's avatar
Thierry Martinez committed
34 35 36
  count(plot_png, Index),
  format(atom(Filename), 'plot~d.png', [Index]),
  export_plot_to_png(Filename),
Thierry Martinez's avatar
SBML  
Thierry Martinez committed
37
  view_image(Filename).
Thierry Martinez's avatar
Thierry Martinez committed
38 39


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


export_plot(FileTemplate) :-
  biocham_command,
  doc('
Thierry Martinez's avatar
Thierry Martinez committed
47
    saves the current trace into two files:
Thierry Martinez's avatar
Thierry Martinez committed
48 49
    \\argument{FileTemplate}\\texttt{.csv} and \\texttt{.plot}.
  '),
Thierry Martinez's avatar
Thierry Martinez committed
50 51 52 53 54 55 56 57 58 59 60 61
  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
62 63 64
  format(atom(PlotFile), '~a.plot', [FileTemplate]),
  format(atom(CsvFile), '~a.csv', [FileTemplate]),
  export_trace(CsvFile),
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
65 66
  get_trace_data([HeaderRow | _ ]),
  HeaderRow =.. [row, _TimeHeader | Headers],
Thierry Martinez's avatar
Thierry Martinez committed
67 68
  setup_call_cleanup(
    open(PlotFile, write, Stream),
Thierry Martinez's avatar
Thierry Martinez committed
69
    export_plot_stream(Stream, CsvFile, Headers, Options),
Thierry Martinez's avatar
Thierry Martinez committed
70 71 72
    close(Stream)
  ).

Thierry Martinez's avatar
Thierry Martinez committed
73 74 75 76 77 78 79 80 81 82 83
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
84
  write(Stream, '\c
Thierry Martinez's avatar
Thierry Martinez committed
85 86
set style data lines
set datafile separator ","
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
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').