From 22b7067f105acba1e845419081e867af1975cbbd Mon Sep 17 00:00:00 2001 From: Ryan Boldi Date: Mon, 14 Mar 2022 19:02:47 -0400 Subject: [PATCH] add and test helper functions --- src/propeller/tools/math.cljc | 5 +++++ src/propeller/tools/metrics.cljc | 15 +++++++++++++++ src/propeller/utils.cljc | 15 +++++++++++++++ test/propeller/tools/metrics_test.cljc | 13 ++++++++++++- 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/propeller/tools/math.cljc b/src/propeller/tools/math.cljc index 2b42be1..0e5f6ac 100755 --- a/src/propeller/tools/math.cljc +++ b/src/propeller/tools/math.cljc @@ -107,3 +107,8 @@ [x] #?(:clj (Math/tan x) :cljs (js/Math.tan x))) + +(defn transpose + "returns a vector containing the transpose of a coll of colls" + [x] + (apply map vector x)) \ No newline at end of file diff --git a/src/propeller/tools/metrics.cljc b/src/propeller/tools/metrics.cljc index 156265f..3019edd 100755 --- a/src/propeller/tools/metrics.cljc +++ b/src/propeller/tools/metrics.cljc @@ -1,11 +1,26 @@ (ns propeller.tools.metrics (:require [propeller.tools.math :as math])) +(defn argmax + "returns the index of the maximum value in a list" + [coll] + ;(prn {:func :argmax :coll coll}) + (->> coll + (map-indexed vector) + (apply max-key second) + first)) + (defn mean "Returns the mean of a collection." [coll] (if (empty? coll) 0 (math/div (apply + coll) (count coll)))) +(defn mean-of-colls + "returns the mean of multiple colls" + [coll] + ;(prn {:func :mean-of-colls :coll coll}) + (map mean (math/transpose coll))) + (defn median "Returns the median of a collection." [coll] diff --git a/src/propeller/utils.cljc b/src/propeller/utils.cljc index 5f0b231..c1332f5 100755 --- a/src/propeller/utils.cljc +++ b/src/propeller/utils.cljc @@ -1,6 +1,21 @@ (ns propeller.utils (:require [clojure.zip :as zip])) +(defn filter-by-index + "filters a collection by a list of indices" + [coll idxs] + ;(prn {:func :filter-by-index :coll coll :idxs idxs}) + (keep-indexed #(when ((set idxs) %1) %2) + coll)) + +(defn drop-nth + "drops the nth element from a collection" + [n coll] + ;(prn {:func :drop-nth :n n :coll coll}) + (concat + (take n coll) + (nthrest coll (inc n)))) + (defn first-non-nil "Returns the first non-nil values from the collection, or returns `nil` if the collection is empty or only contains `nil`." diff --git a/test/propeller/tools/metrics_test.cljc b/test/propeller/tools/metrics_test.cljc index 686856b..b35b7f1 100644 --- a/test/propeller/tools/metrics_test.cljc +++ b/test/propeller/tools/metrics_test.cljc @@ -3,6 +3,17 @@ [propeller.tools.metrics :as m] [propeller.tools.math :as a])) +(t/deftest argmax-test + (t/is (= (m/argmax '(1 2 3 4)) 3)) + (t/is (= 1 (nth '(1 1 1 1) (m/argmax '(1 1 1 1))))) + (t/is (= 3 (nth '(1 3 3 1) (m/argmax '(1 3 3 1))))) + (t/is (= 0 (m/argmax '(1))))) + +(t/deftest mean-of-colls-test + (t/is (= (m/mean-of-colls '((1 2 3 4) (4 3 2 1))) '(2.5 2.5 2.5 2.5))) + (t/is (= (m/mean-of-colls '((1 2 3) (4 3 2 1))) '(2.5 2.5 2.5))) + (t/is (= (m/mean-of-colls '((1))) '(1.0)))) + (t/deftest mean-test (t/is (= (m/mean '(1 2 3 4)) 2.5)) (t/is (= (m/mean '()) 0))) @@ -15,7 +26,7 @@ (t/deftest levenshtein-distance-test (t/is (= (m/levenshtein-distance "kitten" "sipping") 5)) - (t/is (= (m/levenshtein-distance "" "hello")) 5)) + (t/is (= (m/levenshtein-distance "" "hello") 5))) (t/deftest sequence-similarity-test (t/is (a/approx= (m/sequence-similarity "kitten" "sipping") 0.2857 0.001))