plot.pl 2.66 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


40 41 42 43 44 45 46 47
gnu_plot_canvas :-
  count(plot_canvas, Index),
  format(atom(Filename), 'plot~d.js', [Index]),
  format(atom(BaseFilename), 'plot~d', [Index]),
  export_plot_to_canvas(Filename, BaseFilename),
  view_image(Filename).


Thierry Martinez's avatar
Thierry Martinez committed
48 49
execute_plot(Options) :-
  process_create(path(gnuplot), Options, []).
Thierry Martinez's avatar
Thierry Martinez committed
50 51 52 53 54


export_plot(FileTemplate) :-
  biocham_command,
  doc('
Thierry Martinez's avatar
Thierry Martinez committed
55
    saves the current trace into two files:
Thierry Martinez's avatar
Thierry Martinez committed
56 57
    \\argument{FileTemplate}\\texttt{.csv} and \\texttt{.plot}.
  '),
Thierry Martinez's avatar
Thierry Martinez committed
58 59 60 61 62 63 64 65 66 67 68
  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']).


69 70 71 72 73 74 75 76
export_plot_to_canvas(OutputFile, BaseOutputFile) :-
  biocham_command,
  type(OutputFile, output_file),
  doc('plots the current trace as a canvas element in an HTML file'),
  export_plot(plot, [canvas(OutputFile, BaseOutputFile)]),
  execute_plot(['plot.plot']).


Thierry Martinez's avatar
Thierry Martinez committed
77
export_plot(FileTemplate, Options) :-
Thierry Martinez's avatar
Thierry Martinez committed
78 79
  format(atom(PlotFile), '~a.plot', [FileTemplate]),
  format(atom(CsvFile), '~a.csv', [FileTemplate]),
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
80
  export_table(CsvFile),
81
  get_table_headers([_Time | Headers]),
Thierry Martinez's avatar
Thierry Martinez committed
82
  absolute_file_name(PlotFile, AbsolutePlotFile),
Thierry Martinez's avatar
Thierry Martinez committed
83
  setup_call_cleanup(
Thierry Martinez's avatar
Thierry Martinez committed
84
    open(AbsolutePlotFile, write, Stream),
Thierry Martinez's avatar
Thierry Martinez committed
85
    export_plot_stream(Stream, CsvFile, Headers, Options),
Thierry Martinez's avatar
Thierry Martinez committed
86 87 88
    close(Stream)
  ).

Thierry Martinez's avatar
Thierry Martinez committed
89 90 91 92 93 94 95 96
export_plot_stream(Stream, CsvFile, Headers, Options) :-
  (
    member(png(Filename), Options)
  ->
    format(Stream, '\c
set term png
set output "~a"
', [Filename])
97 98 99 100 101 102 103
  ;
    member(canvas(Filename, BaseFilename), Options)
  ->
    format(Stream, '\c
set term canvas name "~a"
set output "~a"
', [BaseFilename, Filename])
Thierry Martinez's avatar
Thierry Martinez committed
104 105 106
  ;
    true
  ),
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
107
  write(Stream, '\c
Thierry Martinez's avatar
Thierry Martinez committed
108 109
set style data lines
set datafile separator ","
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
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').