(ns forensics.row_pruning (:use (forensics utils mesov4))) ;(def conflict []) ;(def no-conflict []) (defn classify1 [tree instance q] ;instance here is the sphere-mean "Classify one instance" (loop [tr tree] (if (= (first tr) 'a) (find-nearest-sphere! instance (second tr) numeric-euclidean-distance!) (recur (let [val (find-nearest-sphere! instance (apply vector (map #(first %) tr)) numeric-euclidean-distance!) branch (second (first (filter #(= val (first %)) tr)))] branch))))) (defn fns1 [spheres spheres1 q] (let [sphere (first spheres) others (apply vector (remove #(= % sphere) spheres1)) tree (meso-tree others q) nearest-sphere (classify1 tree (first sphere) q)] [sphere nearest-sphere])) (defn find-neighbors [spheres q] (loop [s spheres neighbors []] (if (empty? s) neighbors (recur (rest s) (conj neighbors (fns1 s spheres q)))))) (defn major-class [partitions] ;after compressing (second (first (reverse (sort-by first partitions))))) (defn conflict? [neighbors] (let [one1 (second (first neighbors)) two1 (second (second neighbors)) major1 (if (= (count one1) 1) (last (first one1)) (major-class (compress (sort (last (Transpose one1)))))) major2 (if (= (count two1) 1) (last (first two1)) (major-class (compress (sort (last (Transpose two1))))))] (if (= major1 major2) 'false 'true))) (defn remove-bad-neighbors [all-neighbors] (doseq [one all-neighbors] (if (conflict? one) (def conflict (conj conflict one)) (def no-conflict (conj no-conflict one)))) (let [result no-conflict] (def no-conflict []) (def conflict []) result)) (defn remove-matching-pairs [no-conflict] (loop [l no-conflict clean-up []] (if (empty? l) (apply vector (remove #(= 'nil %) clean-up)) (recur (rest l) (conj clean-up (if (= (count (filter #(= % (reverse (first l))) clean-up)) 0) (first l))))))) (defn no-neighbors [no-conflict] "de-neighborize using result from above function" (let [spheres (apply vector (apply concat no-conflict))] (loop [s spheres clean []] (if (empty? s) (apply vector (apply concat (map #(second %) (apply vector (remove #(= 'nil %) clean))))) (recur (rest s) (conj clean (if (= (count (filter #(= % (first s)) clean)) 0) (first s)))))))) ;main (defn row-pruning ([D] (row-pruning D 8)) ([D q] (let [S (ssc D) N (remove-matching-pairs (remove-bad-neighbors (find-neighbors S q))) I (no-neighbors N)] I)))