From 5cc8215f714826a7502c8300173b27cf521e4f85 Mon Sep 17 00:00:00 2001 From: daslu Date: Fri, 8 Dec 2023 18:40:54 +0200 Subject: [PATCH] moved hanami-related functions to a dedicated namespace --- CHANGELOG.md | 1 + notebooks/visualization.clj | 112 +++++++++--------- .../noj/v1/{vis.clj => vis/hanami.clj} | 59 ++++----- 3 files changed, 87 insertions(+), 85 deletions(-) rename src/scicloj/noj/v1/{vis.clj => vis/hanami.clj} (74%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95cbdf8..18e5df8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/notebooks/visualization.clj b/notebooks/visualization.clj index 139613d..e349ca3 100644 --- a/notebooks/visualization.clj +++ b/notebooks/visualization.clj @@ -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] @@ -34,13 +34,13 @@ ;; 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) @@ -48,14 +48,14 @@ ;; 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 @@ -63,41 +63,41 @@ (-> 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 @@ -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} @@ -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" @@ -159,7 +159,7 @@ ;; Alternatively: (-> random-walk - (vis/hanami-combined-plot + (hanami/combined-plot ht/hconcat-chart {:HEIGHT 100 :WIDTH 100} @@ -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 @@ -191,7 +191,7 @@ ;; ### Histogram (-> datasets/iris - (vis/hanami-histogram :sepal-width + (hanami/histogram :sepal-width {:nbins 10})) ;; ### Combining a few things together @@ -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 @@ -226,7 +226,7 @@ {:Y :mpg-prediction :MSIZE 5 :YTITLE :mpg}]])))) - (vis/hanami-vconcat nil {})))) + (hanami/vconcat nil {})))) ;; A similar example with histograms: @@ -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). @@ -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 diff --git a/src/scicloj/noj/v1/vis.clj b/src/scicloj/noj/v1/vis/hanami.clj similarity index 74% rename from src/scicloj/noj/v1/vis.clj rename to src/scicloj/noj/v1/vis/hanami.clj index 15263cd..38424ad 100644 --- a/src/scicloj/noj/v1/vis.clj +++ b/src/scicloj/noj/v1/vis/hanami.clj @@ -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] @@ -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 @@ -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 @@ -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}})))