Skip to content

Commit

Permalink
added dynamic var to disable validation
Browse files Browse the repository at this point in the history
  • Loading branch information
behrica committed May 4, 2024
1 parent aa1ac73 commit 69af33d
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions src/scicloj/metamorph/ml/tidy_models.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,55 @@
[clojure.set :as set]
[tech.v3.dataset :as ds]))


(def allowed-glance-columns
(def ^:dynamic
^{:doc "Controls if the result columns of the tidy fns of a model
(glance-fn, tidy-fn, augment-fn is validated against these base
https://github.com/scicloj/metamorph.ml/tree/main/resources/*.edn
and if on violation they fail."
:added "1.0"}
*validate-tidy-fns* true)

(defn allowed-glance-columns []
(keys
(edn/read-string (slurp "resources/columms-glance.edn"))))
(edn/read-string (slurp "https://raw.githubusercontent.com/scicloj/metamorph.ml/linearRegression/resources/columms-glance.edn"))))

(def allowed-tidy-columns
(defn allowed-tidy-columns []
(keys
(edn/read-string (slurp "resources/columms-tidy.edn"))))
(edn/read-string (slurp "https://raw.githubusercontent.com/scicloj/metamorph.ml/linearRegression/resources/columms-tidy.edn"))))

(def allowed-augment-columns
(defn allowed-augment-columns []
(keys
(edn/read-string (slurp "resources/columms-augment.edn"))))
(edn/read-string (slurp "https://raw.githubusercontent.com/scicloj/metamorph.ml/linearRegression/resources/columms-augment.edn"))))

(defn _get-allowed-keys []
{:glance (allowed-glance-columns)
:tidy (allowed-tidy-columns)
:augment (allowed-augment-columns)})

(def get-allowed-keys (memoize _get-allowed-keys))



(defn- validate-ds [ds allowed-columns fn-name]
(let [
invalid-keys
(set/difference
(into #{} (ds/column-names ds))
(into #{} allowed-columns))]
(if (empty? invalid-keys)
ds
(throw (Exception. (format "invalid keys from %s: %s" fn-name invalid-keys))))))
(if (true? *validate-tidy-fns*)
(let [
invalid-keys
(set/difference
(into #{} (ds/column-names ds))
(into #{} allowed-columns))]
(if (empty? invalid-keys)
ds
(throw (Exception. (format "invalid keys from %s: %s" fn-name invalid-keys)))))
ds))

(defn validate-tidy-ds [ds]
(validate-ds ds allowed-tidy-columns "tidy-fn"))
(validate-ds ds (:tidy (get-allowed-keys)) "tidy-fn"))

(defn validate-glance-ds [ds]
(validate-ds ds allowed-glance-columns "glance-fn"))


(validate-ds ds (:glance (get-allowed-keys)) "glance-fn"))

(defn validate-augment-ds [ds data]
(validate-ds
ds
(concat allowed-augment-columns (ds/column-names data))
(concat (:augment (get-allowed-keys)) (ds/column-names data))
"augment-fn"))

0 comments on commit 69af33d

Please sign in to comment.