Wrapped some noisy operations in (capture-out-err) which captures and discards stdout and stderr.

This commit is contained in:
Joshua Eckroth 2013-08-04 08:57:49 -04:00
parent db70ee980f
commit 19d3772bc0
3 changed files with 46 additions and 30 deletions

View file

@ -281,11 +281,12 @@
(defn make-classifier-with
#^{:skip-wiki true}
[kind algorithm ^Class classifier-class options]
(let [options-read (if (empty? options) {} (first options))
^Classifier classifier (.newInstance classifier-class)
opts (into-array String (make-classifier-options kind algorithm options-read))]
(.setOptions classifier opts)
classifier))
(capture-out-err
(let [options-read (if (empty? options) {} (first options))
^Classifier classifier (.newInstance classifier-class)
opts (into-array String (make-classifier-options kind algorithm options-read))]
(.setOptions classifier opts)
classifier)))
(defmulti make-classifier
"Creates a new classifier for the given kind algorithm and options.
@ -677,26 +678,28 @@
(defmethod classifier-evaluate :dataset
([^Classifier classifier mode & [training-data test-data]]
(letfn [(eval-fn [c]
(let [evaluation (new Evaluation training-data)
class-labels (dataset-class-labels training-data)]
(.evaluateModel evaluation c test-data (into-array []))
(collect-evaluation-results class-labels evaluation)))]
(if (seq? classifier)
(last (sort-by :correct (map eval-fn classifier)))
(eval-fn classifier)))))
(capture-out-err
(letfn [(eval-fn [c]
(let [evaluation (new Evaluation training-data)
class-labels (dataset-class-labels training-data)]
(.evaluateModel evaluation c test-data (into-array []))
(collect-evaluation-results class-labels evaluation)))]
(if (seq? classifier)
(last (sort-by :correct (map eval-fn classifier)))
(eval-fn classifier))))))
(defmethod classifier-evaluate :cross-validation
([classifier mode & [training-data folds]]
(letfn [(eval-fn [c]
(let [evaluation (new Evaluation training-data)
class-labels (dataset-class-labels training-data)]
(.crossValidateModel evaluation c training-data folds
(new Random (.getTime (new Date))) (into-array []))
(collect-evaluation-results class-labels evaluation)))]
(if (seq? classifier)
(last (sort-by :correct (map eval-fn classifier)))
(eval-fn classifier)))))
(capture-out-err
(letfn [(eval-fn [c]
(let [evaluation (new Evaluation training-data)
class-labels (dataset-class-labels training-data)]
(.crossValidateModel evaluation c training-data folds
(new Random (.getTime (new Date))) (into-array []))
(collect-evaluation-results class-labels evaluation)))]
(if (seq? classifier)
(last (sort-by :correct (map eval-fn classifier)))
(eval-fn classifier))))))
;; Classifying instances

View file

@ -567,14 +567,15 @@
For examples on how to use the filters, especially the clojure filters, you may
refer to filters_test.clj of clj-ml."
[kind options]
(let [^Filter filter (if-let [^Class class (kind filter-aliases)]
(let [^OptionHandler f (.newInstance class)]
(.setOptions f (into-array String (make-filter-options kind options)))
f)
(case kind
:clj-streamable (ClojureStreamFilter. (:process options) (:determine-dataset-format options))
:clj-batch (ClojureBatchFilter. (:process options) (:determine-dataset-format options))))]
(doto filter (.setInputFormat (:dataset-format options)))))
(capture-out-err
(let [^Filter filter (if-let [^Class class (kind filter-aliases)]
(let [^OptionHandler f (.newInstance class)]
(.setOptions f (into-array String (make-filter-options kind options)))
f)
(case kind
:clj-streamable (ClojureStreamFilter. (:process options) (:determine-dataset-format options))
:clj-batch (ClojureBatchFilter. (:process options) (:determine-dataset-format options))))]
(doto filter (.setInputFormat (:dataset-format options))))))
;; Processing the filtering of data

View file

@ -117,3 +117,15 @@
obj (.readObject is)]
(.close is)
obj)))
;; capture stdout and stderr for noisy operations
(defmacro capture-out-err [& body]
`(let [console-out# System/out
console-err# System/err
ps# (java.io.PrintStream. (java.io.ByteArrayOutputStream.))]
(System/setErr ps#)
(System/setOut ps#)
(let [result# (do ~@body)]
(System/setErr console-err#)
(System/setOut console-out#)
result#)))