Skip to content

Commit

Permalink
Merge pull request #26 from behrica/generateTests
Browse files Browse the repository at this point in the history
Auto generate tests
  • Loading branch information
daslu authored Sep 20, 2024
2 parents 49b09c0 + 85fc004 commit f23801a
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 982 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ temp
*.qmd
book

/.clj-kondo/
test/noj_book/

21 changes: 19 additions & 2 deletions build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
(:require [clojure.tools.build.api :as b]
[deps-deploy.deps-deploy :as dd]))



(def lib 'org.scicloj/noj)
(def version "2-alpha6")
(def snapshot (str version "-SNAPSHOT"))
Expand All @@ -22,7 +24,7 @@
(defn test "Run all the tests." [opts]
(doseq [alias [:1.11 :1.12 :master]]
(println "\nRunning tests for Clojure" (name alias))
(let [basis (b/create-basis {:aliases [:test alias]})
(let [basis (b/create-basis {:aliases (concat (opts :aliases [:test alias]))})
cmds (b/java-command
{:basis basis
:main 'clojure.main
Expand Down Expand Up @@ -50,8 +52,19 @@
:src-dirs ["src"]
:pom-data (pom-template version))))

(defn generate-tests [opts]
(let [basis (b/create-basis {:aliases [:gen-tests :dev]})

cmds (b/java-command
{:basis basis
:main 'clojure.main
:main-args ["-e" "(require '[gen-tests])(gen-tests/do-generate-tests)(System/exit 0)"]})
{:keys [exit]} (b/process cmds)]
(when-not (zero? exit) (throw (ex-info "Tests generation failed" {})))))
(def opts {})
(defn ci "Run the CI pipeline of tests (and build the JAR)." [opts]
(test opts)
(generate-tests (assoc opts :aliases [:dev]))
(test (assoc opts :aliases [:dev :gen-tests :test]))
(b/delete {:path "target"})
(let [opts (jar-opts opts)]
(println "\nWriting pom.xml...")
Expand All @@ -67,3 +80,7 @@
(dd/deploy {:installer :remote :artifact (b/resolve-path jar-file)
:pom-file (b/pom-path (select-keys opts [:lib :class-dir]))}))
opts)




18 changes: 18 additions & 0 deletions build/gen_tests.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns gen-tests
(:require
[scicloj.clay.v2.api :as clay]
[clojure.java.io :as io]
[clojure.string :as str]))

(defn do-generate-tests []
(->>
(file-seq (io/file "notebooks/noj_book"))
(filter #(str/ends-with? (.toPath %) ".clj"))
(run!
#(let [p (.getAbsolutePath %)]
(println :generate-tests p)
(clay/make! {:source-path p
:show false}))))
(shutdown-agents))

(clay/make! {:source-path "notebooks/noj_book/automl.clj" :show false})
9 changes: 7 additions & 2 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
org.scicloj/kind-pyplot {:mvn/version "1-beta1"}
scicloj/clojisr {:mvn/version "1.0.0"}}
:aliases
{:build {:deps {io.github.clojure/tools.build {:mvn/version "0.9.6"}
slipset/deps-deploy {:mvn/version "0.2.1"}}
{:gen-tests {:extra-paths ["build"]
:extra-deps { org.scicloj/clay {:mvn/version "2-beta16"}}}

:build {:deps {io.github.clojure/tools.build {:mvn/version "0.9.6"}
slipset/deps-deploy {:mvn/version "0.2.1"}}


:ns-default build}
:test {:extra-paths ["test" "notebooks"]
:extra-deps {org.clojure/test.check {:mvn/version "1.1.1"}
Expand Down
44 changes: 44 additions & 0 deletions notebooks/lr.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(ns lr
(:require [tablecloth.api :as tc]
[tech.v3.tensor :as dt]
[scicloj.metamorph.ml :as ml]
[scicloj.ml.smile.regression]
[scicloj.metamorph.ml.loss :as loss]
[tech.v3.dataset.modelling :as dsmod]
[tech.v3.dataset.tensor :as dst]
[tech.v3.dataset :as ds]
[tech.v3.datatype.functional :as dtf]))

(def x (dt/->tensor [[1 1] [1 2] [2 2] [2 3]]))
(def y
(dtf/+ 3
(dt/reduce-axis x (fn [row] (dtf/dot-product row (dt/->tensor [1 2]))))))

(def y-ds
(->
(dst/tensor->dataset x)
(tc/add-column :y y)
(dsmod/set-inference-target :y)))

(def reg
(ml/train y-ds
{:model-type :smile.regression/ordinary-least-square}))

(- 1 (loss/rmse y (:y (ml/predict y-ds reg))));; => 1.0
(seq (.coefficients (ml/thaw-model reg))) ;; => (2.9999999999999996 0.9999999999999998 2.0000000000000004)
(seq (:y (ml/predict (tc/dataset [[3 5]]) reg)));; => (16.0)


(defn Xy->dataset
"Combines tensor X and vector y to a dataset
where y is set to be the inference target
x need to be a tensor
y a seq or 1-D tensor
"
[x y]

(->
(dst/tensor->dataset x)
(ds/add-column (ds/new-column :y y))
(dsmod/set-inference-target :y)))

4 changes: 2 additions & 2 deletions notebooks/noj_book/interactions_ols.clj
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ $$sales = b0 + b1 * youtube + b2 * facebook$$")
(md "We have the following metrics:")
(md "$RMSE$")
(-> evaluations flatten first :test-transform :metric)
(kindly/check = 0.933077510748531)
(kindly/check = 1.772159024927988)

(md "$R^2$")
(-> evaluations flatten first :test-transform :other-metrices first :metric)
(kindly/check = 0.9747551116991899)
(kindly/check = 0.9094193687523886)

(md "## Interaction effects")
(md "Now we add interaction effects to it, resulting in this model equation:
Expand Down
40 changes: 37 additions & 3 deletions notebooks/noj_book/ml_basic.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
(:require [tablecloth.api :as tc]
[scicloj.metamorph.ml.toydata :as data]
[tech.v3.dataset :as ds]
[scicloj.metamorph.ml :as ml]
[camel-snake-kebab.core :as csk]
[scicloj.kindly.v4.kind :as kind]
[scicloj.kindly.v4.api :as kindly]))




;; ## Inspect data
;;
;; The titanic data is part of `metamorph.ml` and in the form of a
Expand Down Expand Up @@ -91,8 +95,27 @@
(ds-cat/fit-categorical-map relevant-titanic-data :pclass [0 1 2] :float64)
(ds-cat/fit-categorical-map relevant-titanic-data :embarked ["S" "Q" "C"] :float64)])


cat-maps

(kind/test-last (fn [cat-maps]
(every?
true?
(map
#(.equals %1 %2)
cat-maps
[
{:lookup-table {"male" 0, "female" 1},
:src-column :sex,
:result-datatype :float64}
{:lookup-table {0 0, 1 1, 2 2, 3 3},
:src-column :pclass,
:result-datatype :float64}
{:lookup-table {"S" 0, "Q" 1, "C" 2},
:src-column :embarked,
:result-datatype :float64}]))))


;; After the mappings are applied, we have a numeric dataset, as expected
;; by most models.
(def numeric-titanic-data
Expand All @@ -103,6 +126,16 @@ cat-maps
(tc/head
numeric-titanic-data)


(kind/test-last (fn [ds]
(=
[[0.0 3.0 0.0 0.0]
[1.0 1.0 2.0 1.0]
[1.0 3.0 0.0 1.0]
[1.0 1.0 0.0 1.0]
[0.0 3.0 0.0 0.0]]
(ds/rowvecs ds))))

;; Split data into train and test set
;; Now we split the data into train and test. By we use
;; a :holdout strategy, so will get a single split in training an test data.
Expand Down Expand Up @@ -143,7 +176,7 @@ split
(loss/classification-accuracy
(:survived (ds-cat/reverse-map-categorical-xforms (:test split)))
(:survived (ds-cat/reverse-map-categorical-xforms dummy-prediction)))
;; It's performance is poor, even worse the coin flip.
;; It's performance is poor, even worse then coin flip.


;; ## Logistic regression
Expand All @@ -166,7 +199,8 @@ split
(:survived (ds-cat/reverse-map-categorical-xforms (:test split)))
(:survived (ds-cat/reverse-map-categorical-xforms lreg-prediction)))

;; Its performance is better, 60 %
(kind/test-last [= 0.7373737373737373])
;; Its performance is better, 73 %

;; ## Random forest
;; Next is random forest
Expand Down
Empty file added test/.keep
Empty file.
Loading

0 comments on commit f23801a

Please sign in to comment.