Skip to content

Commit

Permalink
moved hanami-related functions to a dedicated namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
daslu committed Dec 8, 2023
1 parent 093b540 commit 5cc8215
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 85 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. This change
- added `hanami-combined-plot` API
- refactored and added information to regression models
- added model information in prediction columnw metadata
- moved hanami-related functions to a dedicated namespace

## [1-alpha18] - 2023-12-06
- removed the obsolete user namespace
Expand Down
112 changes: 56 additions & 56 deletions notebooks/visualization.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[aerial.hanami.common :as hc]
[aerial.hanami.templates :as ht]
[scicloj.noj.v1.vis.hanami.templates :as vht]
[scicloj.noj.v1.vis :as vis]
[scicloj.noj.v1.vis.hanami :as hanami]
[scicloj.noj.v1.stats :as stats]
[scicloj.noj.v1.datasets :as datasets]
[tech.v3.datatype :as dtype]
Expand Down Expand Up @@ -34,70 +34,70 @@
;; We can plot a Tablecloth datasete using a Hanami template:

(-> random-walk
(vis/hanami-plot ht/point-chart
{:MSIZE 200}))
(hanami/plot ht/point-chart
{:MSIZE 200}))

;; Let us look inside the resulting vega-lite space. We can see the dataset is included as CSV:

(-> random-walk
(vis/hanami-plot ht/point-chart
(hanami/plot ht/point-chart
{:MSIZE 200})
kind/pprint)

;; We can used Hanami temples from the namespace `[aerial.hanami.templates :as ht]'
;; as well as the additional templates at Noj's `[scicloj.noj.v1.vis.hanami.templates :as vht]`.

(-> datasets/iris
(vis/hanami-plot vht/rule-chart
{:X :sepal-width
:Y :sepal-length
:X2 :petal-width
:Y2 :petal-length
:OPACITY 0.2
:SIZE 3
:COLOR "species"}))
(hanami/plot vht/rule-chart
{:X :sepal-width
:Y :sepal-length
:X2 :petal-width
:Y2 :petal-length
:OPACITY 0.2
:SIZE 3
:COLOR "species"}))

;; ### Grouped datasets

;; Grouped datasets are handled automatically with a table view.

(-> datasets/iris
(tc/group-by [:species])
(vis/hanami-plot vht/rule-chart
{:X :sepal-width
:Y :sepal-length
:X2 :petal-width
:Y2 :petal-length
:OPACITY 0.2
:SIZE 3}))
(hanami/plot vht/rule-chart
{:X :sepal-width
:Y :sepal-length
:X2 :petal-width
:Y2 :petal-length
:OPACITY 0.2
:SIZE 3}))

;; ### Additional Hanami templates

;; The `scicloj.noj.v1.vis.hanami.templates` namespace add Hanami templates to Hanami's own collection.

(-> datasets/mtcars
(vis/hanami-plot vht/boxplot-chart
{:X :gear
:XTYPE :nominal
:Y :mpg}))
(hanami/plot vht/boxplot-chart
{:X :gear
:XTYPE :nominal
:Y :mpg}))

;; ### Layers

(-> random-walk
(vis/hanami-layers
(hanami/layers
{:TITLE "points and a line"}
[(vis/hanami-plot nil
ht/point-chart
{:MSIZE 400})
(vis/hanami-plot nil
ht/line-chart
{:MSIZE 4
:MCOLOR "brown"})]))
[(hanami/plot nil
ht/point-chart
{:MSIZE 400})
(hanami/plot nil
ht/line-chart
{:MSIZE 4
:MCOLOR "brown"})]))

;; Alternatively:

(-> random-walk
(vis/hanami-combined-plot
(hanami/combined-plot
ht/layer-chart
{:TITLE "points and a line"}
:LAYER [[nil
Expand All @@ -113,24 +113,24 @@
;; Vertical

(-> random-walk
(vis/hanami-vconcat
(hanami/vconcat
{}
[(vis/hanami-plot nil
ht/point-chart
{:MSIZE 400
:HEIGHT 100
:WIDTH 100})
(vis/hanami-plot nil
ht/line-chart
{:MSIZE 4
:MCOLOR "brown"
:HEIGHT 100
:WIDTH 100})]))
[(hanami/plot nil
ht/point-chart
{:MSIZE 400
:HEIGHT 100
:WIDTH 100})
(hanami/plot nil
ht/line-chart
{:MSIZE 4
:MCOLOR "brown"
:HEIGHT 100
:WIDTH 100})]))

;; Alternatively:

(-> random-walk
(vis/hanami-combined-plot
(hanami/combined-plot
ht/vconcat-chart
{:HEIGHT 100
:WIDTH 100}
Expand All @@ -143,14 +143,14 @@
;; Horizontal

(-> random-walk
(vis/hanami-hconcat
(hanami/hconcat
{}
[(vis/hanami-plot nil
[(hanami/plot nil
ht/point-chart
{:MSIZE 400
:HEIGHT 100
:WIDTH 100})
(vis/hanami-plot nil
(hanami/plot nil
ht/line-chart
{:MSIZE 4
:MCOLOR "brown"
Expand All @@ -159,7 +159,7 @@

;; Alternatively:
(-> random-walk
(vis/hanami-combined-plot
(hanami/combined-plot
ht/hconcat-chart
{:HEIGHT 100
:WIDTH 100}
Expand All @@ -174,7 +174,7 @@
(-> datasets/mtcars
(stats/add-predictions :mpg [:wt]
{:model-type :smile.regression/ordinary-least-square})
(vis/hanami-combined-plot
(hanami/combined-plot
ht/layer-chart
{:X :wt
:MSIZE 200
Expand All @@ -191,7 +191,7 @@
;; ### Histogram

(-> datasets/iris
(vis/hanami-histogram :sepal-width
(hanami/histogram :sepal-width
{:nbins 10}))

;; ### Combining a few things together
Expand All @@ -212,7 +212,7 @@
(stats/add-predictions :mpg [:wt]
{:model-type :smile.regression/ordinary-least-square})
(tc/select-columns [:gear :wt :mpg :mpg-prediction])
(vis/hanami-combined-plot
(hanami/combined-plot
ht/layer-chart
{:TITLE (str "grear=" group-name)
:X :wt
Expand All @@ -226,7 +226,7 @@
{:Y :mpg-prediction
:MSIZE 5
:YTITLE :mpg}]]))))
(vis/hanami-vconcat nil {}))))
(hanami/vconcat nil {}))))

;; A similar example with histograms:

Expand All @@ -239,9 +239,9 @@
(map-indexed
(fn [i [group-name ds]]
(-> ds
(vis/hanami-histogram :sepal-width
(hanami/histogram :sepal-width
{:nbins 10}))))
(vis/hanami-vconcat nil {}))))
(hanami/vconcat nil {}))))

;; Scatterplots and regression lines again, this time using Vega-Lite for layout and coloring (using its "facet" option).

Expand All @@ -251,7 +251,7 @@
{:model-type :smile.regression/ordinary-least-square})
(tc/ungroup)
(tc/select-columns [:gear :wt :mpg :mpg-prediction])
(vis/hanami-combined-plot
(hanami/combined-plot
ht/layer-chart
{}
:LAYER [[ht/point-chart
Expand Down
59 changes: 30 additions & 29 deletions src/scicloj/noj/v1/vis.clj → src/scicloj/noj/v1/vis/hanami.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns scicloj.noj.v1.vis
(ns scicloj.noj.v1.vis.hanami
(:require [tech.v3.dataset :as tmd]
[aerial.hanami.templates :as ht]
[aerial.hanami.common :as hc]
Expand All @@ -9,7 +9,8 @@
[scicloj.noj.v1.stats :as stats]
[tablecloth.api :as tc]))

(defn hanami-data [data]

(defn prepare-data [data]
(when data
(cond (string? data) (if (paths/url? data) {:UDATA data}
;; not a url -- assuming a local path
Expand All @@ -30,49 +31,49 @@
slurp))}
:else {:DATA data})))

(defn hanami-plot [data template options]
(defn plot [data template options]
(if (tc/grouped? data)
(-> data
(tc/aggregate {:plot (fn [group-data]
[(-> group-data
(hanami-plot template
options))])})
(plot template
options))])})
kind/table)
(-> data
hanami-data
prepare-data
(merge options)
(->> (apply concat)
(apply hc/xform template))
kind/vega-lite)))

(defn hanami-collector [template template-key]
(defn collector [template template-key]
(fn [common-data
options
plots]
(-> common-data
(hanami-plot template
(merge {template-key plots}
options)))))
(plot template
(merge {template-key plots}
options)))))

(def hanami-layers
(hanami-collector ht/layer-chart
:LAYER))
(def layers
(collector ht/layer-chart
:LAYER))

(def hanami-vconcat
(hanami-collector ht/vconcat-chart
:VCONCAT))
(def vconcat
(collector ht/vconcat-chart
:VCONCAT))

(def hanami-hconcat
(hanami-collector ht/hconcat-chart
:HCONCAT))
(def hconcat
(collector ht/hconcat-chart
:HCONCAT))

(defn hanami-combined-plot [dataset
(defn combined-plot [dataset
combining-template
options
template-key
plot-specs]
(-> dataset
(hanami-plot
(plot
combining-template
(assoc options
template-key
Expand All @@ -88,17 +89,17 @@
([inner-dataset
inner-template
inner-options]
(hanami-plot inner-dataset
(plot inner-dataset
inner-template
(merge options inner-options)))))))))))

(defn hanami-histogram [dataset column-name options]
(defn histogram [dataset column-name options]
(-> column-name
dataset
(stats/histogram options)
(hanami-plot vht/rect-chart
{:X :left
:X2 :right
:Y :count
:Y2 0
:XSCALE {:zero false}})))
(plot vht/rect-chart
{:X :left
:X2 :right
:Y :count
:Y2 0
:XSCALE {:zero false}})))

0 comments on commit 5cc8215

Please sign in to comment.