Skip to content

Commit

Permalink
[viz] add git commit history heatmap example, move circle-cell shape fn
Browse files Browse the repository at this point in the history
to main ns (previously example only)
  • Loading branch information
postspectacular committed Jun 23, 2015
1 parent 15060f1 commit a5658cf
Showing 1 changed file with 90 additions and 9 deletions.
99 changes: 90 additions & 9 deletions geom-viz/src/core.org
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [[#plain-intervals][Plain intervals]]
- [[#categorized-timeline][Categorized timeline]]
- [[#heatmap][Heatmap]]
- [[#github-commit-history][Github commit history]]
- [[#contour-plot][Contour plot]]
- [[#gis-terrain-contours-w-elevation-color-gradient][GIS terrain contours w/ elevation color gradient]]
- [[#visualization-spec-format][Visualization spec format]]
Expand Down Expand Up @@ -104,8 +105,7 @@ be run with this command and will create a number of SVG files in the
same directory.

#+BEGIN_SRC clojure
(doseq [f (rest (file-seq (java.io.File. "examples")))]
(load-file (.getAbsolutePath f)))
(doseq [f (rest (file-seq (java.io.File. "examples")))] (load-file (.getAbsolutePath f)))
#+END_SRC

*** Namespaces required by all examples
Expand All @@ -114,7 +114,7 @@ same directory.
(require '[thi.ng.geom.viz.core :as viz] :reload)
(require '[thi.ng.geom.svg.core :as svg])
(require '[thi.ng.geom.core.vector :as v])
(require '[thi.ng.math.core :as m :refer [PI]])
(require '[thi.ng.math.core :as m :refer [PI TWO_PI]])
#+END_SRC

*** Scatter plot
Expand Down Expand Up @@ -301,7 +301,7 @@ Same overall visualization setup, only using polar coordinate transform and rede

(def viz-spec
{:x-axis (viz/linear-axis
{:domain [0 5] :range [0 (* 2 (- 1.0 (/ 1 6)) PI)] :major 1
{:domain [0 5] :range [0 (* (/ 5 6) TWO_PI)] :major 1
:label-dist 20 :pos 260
:format (comp name domain->category)})
:y-axis (viz/linear-axis
Expand Down Expand Up @@ -551,12 +551,89 @@ for list of available presets & how to define new gradients.
| :rainbow2 w/ custom shape fn | :rainbow2, polar projection, custom shape fn |

#+BEGIN_SRC clojure :tangle ../babel/examples/heatmap.clj :noweb yes :mkdirp yes :padline no
;; custom shape function applied for each matrix cell
(defn circle-cell
[a b c d col] (svg/circle (gu/centroid [a b c d]) (* 0.5 (g/dist a b)) {:fill col}))
;; using custom shape function applied for each matrix cell
;; (a circle fitting within the 4 points defining a grid cell)
(cartesian-viz "hms" :rainbow2 {:shape viz/circle-cell})
(polar-viz "hmsp" :rainbow2 {:shape viz/circle-cell})
#+END_SRC

(cartesian-viz "hms" :rainbow2 {:shape circle-cell})
(polar-viz "hmsp" :rainbow2 {:shape circle-cell})
*** Github commit history

This example requires [[https://github.com/Raynes/tentacles][raynes/tentacles 0.3.0]] to be added to your
project in order to download the commit history of a given project.

#+BEGIN_SRC clojure :tangle ../babel/examples/hm-github.clj :mkdirp yes :padline no
(require '[tentacles.repos :as repos])
(require '[thi.ng.ndarray.core :as nd])
(require '[clojure.string :as str])
(require '[clojure.java.shell :refer [sh]])

(def day (* 24 60 60 1000))
(def week (* 7 day))
(def fmt-iso8601 (java.text.SimpleDateFormat. "yyyy-MM-dd'T'HH:mm:ssX"))
(def fmt-month (java.text.SimpleDateFormat. "MMM"))
(def fmt-year (java.text.SimpleDateFormat. "yyyy"))
(def ->epoch #(try (.getTime (.parse fmt-iso8601 %)) (catch Exception e)))

(defn month-or-year
[from]
#(let [d (java.util.Date. (long (+ from (* % week))))]
(.format (if (zero? (.getMonth d)) fmt-year fmt-month) d)))

(defn load-commits-gh
[user repo]
(prn (str "loading GH commit history: " user "/" repo))
(->> (repos/commits user repo {:all-pages true})
(map #(->epoch (get-in % [:commit :author :date])))
(filter identity)))

(defn load-commits-fs
[repo-path]
(->> (sh "git" "log" "--pretty=format:%aI" :dir repo-path)
:out
str/split-lines
(map ->epoch)
(filter identity)))

(defn commits-per-week-day
[t0 commits]
(->> (for [c commits
:let [t (- c t0)
w (int (/ t week))
d (int (/ (rem t week) day))]]
[w d])
(frequencies)
(sort-by first)))

(defn commits->matrix
[commits]
(let [weeks (inc (- (ffirst (last commits)) (first (ffirst commits))))
mat (nd/ndarray :int8 (byte-array (* 7 weeks)) [7 weeks])]
(doseq [[[w d] n] commits] (nd/set-at mat d w n))
mat))

(let [commits (load-commits-fs ".")
;;commits (load-commits-gh "thi-ng" "geom")
[from to] (viz/value-domain-bounds commits)
from (* (long (/ from week)) week)
to (* (inc (long (/ to week))) week)
mat (commits->matrix (commits-per-week-day from commits))
weeks (last (nd/shape mat))
max-x (+ 50 (* weeks 10))]
(->> {:x-axis (viz/linear-axis
{:domain [0 weeks] :range [50 max-x] :major 4 :minor 1 :pos 85
:format (month-or-year from)})
:y-axis (viz/linear-axis {:domain [0 7] :range [10 80] :visible false})
:data [{:matrix mat
:value-domain [1 (reduce max mat)]
:palette (->> :yellow-red (grad/cosine-schemes) (apply grad/cosine-gradient 100))
:palette-scale viz/linear-scale
:layout viz/svg-heatmap
:shape viz/circle-cell}]}
(viz/svg-plot2d-cartesian)
(svg/svg {:width (+ 70 max-x) :height 120})
(svg/serialize)
(spit "commit-history.svg")))
#+END_SRC

*** Contour plot
Expand Down Expand Up @@ -1519,6 +1596,10 @@ file).
{:fill (fill item) :rx r :ry r})
(if (< min-width (- bx ax))
(svg/text [ax (+ base-line ay)] (label item)))))))

(defn circle-cell
[a b c d col]
(svg/circle (gu/centroid [a b c d]) (* 0.5 (g/dist a b)) {:fill col}))
#+END_SRC

** TODO 3D Plotting
Expand Down

0 comments on commit a5658cf

Please sign in to comment.