Skip to content

Commit

Permalink
Refactor things a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentMazare committed Nov 13, 2018
1 parent abc1262 commit 91245dc
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 46 deletions.
1 change: 1 addition & 0 deletions .merlin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PKG base pyml
31 changes: 13 additions & 18 deletions examples/plot.ml
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
open Base

let () =
let plt = Matplotlib.init Default in
let xys =
List.init 120 ~f:(fun i ->
let i = Float.of_int i in
i, Float.sin (i /. 20.))
in
let xys2 =
List.init 120 ~f:(fun i ->
let i = Float.of_int i in
i, Float.cos (i /. 12.))
in
Matplotlib.xlabel plt "x";
Matplotlib.ylabel plt "sin(x)";
Matplotlib.grid plt true;
Matplotlib.plot plt ~color:Red ~xys;
Matplotlib.plot plt ~color:Green ~linestyle:Dotted ~linewidth:2. ~xys:xys2;
Matplotlib.savefig plt "test.png";
Matplotlib.show plt
let xs = List.init 120 ~f:Float.of_int in
let ys1 = List.map xs ~f:(fun i -> Float.sin (i /. 20.)) in
let ys2 = List.map xs ~f:(fun i -> Float.cos (i /. 12.)) in
let xs = Matplotlib.V.l xs in
let ys1 = Matplotlib.V.l ys1 in
let ys2 = Matplotlib.V.l ys2 in
Matplotlib.xlabel "x";
Matplotlib.ylabel "sin(x)";
Matplotlib.grid true;
Matplotlib.plot ~color:Red ~xs ys1;
Matplotlib.plot ~color:Green ~linestyle:Dotted ~linewidth:2. ~xs ys2;
Matplotlib.savefig "test.png";
Matplotlib.show ()
75 changes: 57 additions & 18 deletions src/matplotlib/matplotlib.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
open Base
open Pyops

let plt_module = ref None

let maybe_py_init () =
if not (Py.is_initialized ())
then Py.initialize ()

module Backend = struct
type t =
| Agg
Expand All @@ -13,6 +19,25 @@ module Backend = struct
| Other str -> Some str
end

let init_ backend =
maybe_py_init ();
let plt = Py.import "matplotlib.pyplot" in
Option.iter (Backend.to_string_option backend) ~f:(fun backend_str ->
ignore (plt.&("switch_backend")[| Py.String.of_string backend_str |]));
plt

let init backend =
let plt = init_ backend in
plt_module := Some plt

let maybe_init () =
match !plt_module with
| Some t -> t
| None ->
let t = init_ Default in
plt_module := Some t;
t

module Color = struct
type t =
| Red
Expand Down Expand Up @@ -55,40 +80,54 @@ module Linestyle = struct
Py.String.of_string str
end

type t = Py.Object.t
module V = struct
type t = Py.Object.t

let init backend =
if not (Py.is_initialized ())
then Py.initialize ();
let plt = Py.import "matplotlib.pyplot" in
Option.iter (Backend.to_string_option backend) ~f:(fun backend_str ->
ignore (plt.&("switch_backend")[| Py.String.of_string backend_str |]));
plt
let l xs =
maybe_py_init ();
Py.List.of_list_map Py.Float.of_float xs

let a xs =
maybe_py_init ();
Py.List.of_array_map Py.Float.of_float xs
end

let xlabel t label =
let xlabel label =
let t = maybe_init () in
ignore (t.&("xlabel")[| Py.String.of_string label |])

let ylabel t label =
let title label =
let t = maybe_init () in
ignore (t.&("title")[| Py.String.of_string label |])

let ylabel label =
let t = maybe_init () in
ignore (t.&("ylabel")[| Py.String.of_string label |])

let grid t b =
let grid b =
let t = maybe_init () in
ignore (t.&("grid")[| Py.Bool.of_bool b |])

let savefig t filename =
let savefig filename =
let t = maybe_init () in
ignore (t.&("savefig")[| Py.String.of_string filename |])

let plot ?color ?linewidth ?linestyle t ~xys =
let xs, ys = List.unzip xys in
let xs = List.map xs ~f:Py.Float.of_float |> Py.List.of_list in
let ys = List.map ys ~f:Py.Float.of_float |> Py.List.of_list in
let plot ?color ?linewidth ?linestyle ?xs ys =
let t = maybe_init () in
let keywords =
List.filter_opt
[ Option.map color ~f:(fun color -> "color", Color.to_pyobject color)
; Option.map linewidth ~f:(fun lw -> "linewidth", Py.Float.of_float lw)
; Option.map linestyle ~f:(fun ls -> "linestyle", Linestyle.to_pyobject ls)
]
in
ignore (Py.Module.get_function_with_keywords t "plot" [| xs; ys |] keywords)
let args =
match xs with
| Some xs -> [| xs; ys |]
| None -> [| ys |]
in
ignore (Py.Module.get_function_with_keywords t "plot" args keywords)

let show t =
let show () =
let t = maybe_init () in
ignore (t.&("show")[| |])
26 changes: 16 additions & 10 deletions src/matplotlib/matplotlib.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
type t

module Backend : sig
type t =
| Agg
Expand All @@ -26,18 +24,26 @@ module Linestyle : sig
| Other of string
end

val init : Backend.t -> t
val xlabel : t -> string -> unit
val ylabel : t -> string -> unit
val grid : t -> bool -> unit
(* Vector of float *)
module V : sig
type t
val l : float list -> t
val a : float array -> t
end

val init : Backend.t -> unit
val xlabel : string -> unit
val ylabel : string -> unit
val grid : bool -> unit
val title : string -> unit

val plot
: ?color:Color.t
-> ?linewidth:float
-> ?linestyle:Linestyle.t
-> t
-> xys:(float * float) list
-> ?xs:V.t
-> V.t
-> unit

val show : t -> unit
val savefig : t -> string -> unit
val show : unit -> unit
val savefig : string -> unit

0 comments on commit 91245dc

Please sign in to comment.