Skip to content

Commit

Permalink
First cut.
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentMazare committed Nov 13, 2018
1 parent 4cb7e9b commit 55a5dbd
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
*.annot
*.cmo
*.cma
*.cmi
*.a
*.o
*.cmx
*.cmxs
*.cmxa
*.swp
*.ipynb_checkpoints

# ocamlbuild working directory
_build/

# dataset directory
data/

# ocamlbuild targets
*.byte
*.native

# oasis generated files
setup.data
setup.log
torch.install

# Merlin configuring file for Vim and Emacs
.merlin
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Laurent Mazare

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
all:
dune build @install

plot: .FORCE
dune build examples/plot.exe
_build/default/examples/plot.exe

clean:
rm -Rf _build

.FORCE:
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 1.4)
3 changes: 3 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(executables
(names plot)
(libraries base matplotlib))
15 changes: 15 additions & 0 deletions examples/plot.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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
Matplotlib.xlabel plt "x";
Matplotlib.ylabel plt "sin(x)";
Matplotlib.grid plt true;
Matplotlib.plot plt ~xys;
Matplotlib.savefig plt "test.png";
Matplotlib.show plt
32 changes: 32 additions & 0 deletions matplotlib.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
opam-version: "2.0"
name: "matplotlib"
bug-reports: "https://github.com/LaurentMazare/ocaml-matplotlib/issues"
homepage: "https://github.com/LaurentMazare/ocaml-matplotlib"
dev-repo: "git+https://github.com/LaurentMazare/ocaml-matplotlib.git"
maintainer: "Laurent Mazare <[email protected]>"
authors: [ "Laurent Mazare" ]

version: "dev"

build: [["dune" "build" "-j" jobs "@install"]]

install: []
remove: []

depends: [
"base" {>= "0.11.0"}
"stdio"
"pyml"
"jupyter"
"dune" {build}
"ocaml" {>= "4.06"}
]

depopts: [
]

conflicts: [
]

available: [
]
5 changes: 5 additions & 0 deletions src/matplotlib/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name matplotlib)
(public_name matplotlib)
(flags (-linkall))
(libraries base pyml))
42 changes: 42 additions & 0 deletions src/matplotlib/matplotlib.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
open Base
open Pyops

module Backend = struct
type t =
| Agg
| Default

let to_string_option = function
| Agg -> Some "Agg"
| Default -> None
end

type t = Py.Object.t

let init backend =
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 xlabel t label =
ignore (t.&("xlabel")[| Py.String.of_string label |])

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

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

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

let plot 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
ignore (t.&("plot")[| xs; ys |])

let show t =
ignore (t.&("show")[| |])
15 changes: 15 additions & 0 deletions src/matplotlib/matplotlib.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type t

module Backend : sig
type t =
| Agg
| Default
end

val init : Backend.t -> t
val xlabel : t -> string -> unit
val ylabel : t -> string -> unit
val grid : t -> bool -> unit
val plot : t -> xys:(float * float) list -> unit
val show : t -> unit
val savefig : t -> string -> unit

0 comments on commit 55a5dbd

Please sign in to comment.