<commit_before>(ns pca-test
  (:require [clojure.test :refer :all]
            [polismath.named-matrix :refer :all]
            [incanter.core :refer :all]
            [polismath.pca :refer :all]))


(defn almost-equal? [xs ys & {:keys [tol] :or {tol 1e-2}}]
  (< (apply + (map #(abs (- %1 %2)) xs ys)) tol))


(deftest powerit
  (testing "Should generally work"
    (let [data (matrix [[ 1 0  0  ]
                        [-1 1  0.1]
                        [ 0 1  0.1]
                        [ 0 1 -0.1]])
          expected [-0.34217 0.93906 0.032633]
          pc-from-start (fn [start] (power-iteration data 2 start))]

      (testing "from scratch"
               (is (= true (almost-equal? (pc-from-start nil) expected))))

      (testing "from scratch"
               (is (almost-equal? (pc-from-start [1 1 1]) expected)))

      (testing "from scratch"
               (is (almost-equal? (pc-from-start [1 1]) expected))))))
<commit_msg>Swapped out incanter in tests; added some wrapped-pca shape tests<commit_after>(ns pca-test
  (:require [clojure.test :refer :all]
            [polismath.named-matrix :refer :all]
            [clojure.math.numeric-tower :refer :all]
            [clojure.core.matrix :as m]
            [polismath.pca :refer :all]))


(defn almost-equal? [xs ys & {:keys [tol] :or {tol 1e-2}}]
  (< (apply + (map #(abs (- %1 %2)) xs ys)) tol))


(deftest powerit
  (testing "Should generally work"
    (let [data (m/matrix [[ 1 0  0  ]
                        [-1 1  0.1]
                        [ 0 1  0.1]
                        [ 0 1 -0.1]])
          expected [-0.34217 0.93906 0.032633]
          pc-from-start (fn [start] (power-iteration data 2 start))]

      (testing "from scratch"
               (is (= true (almost-equal? (pc-from-start nil) expected))))

      (testing "from scratch"
               (is (almost-equal? (pc-from-start [1 1 1]) expected)))

      (testing "from scratch"
               (is (almost-equal? (pc-from-start [1 1]) expected))))))


(deftest wrapped-pca-test
  (letfn [(right-shape [data]
            (let [res (wrapped-pca (m/matrix data) 2)]
              (and (:center res) (:comps res))))]
    (testing "Should not fail and have the right shape for for"
      (testing "1x1"
        (is (right-shape [[1]])))
      (testing "1x2"
        (is (right-shape [[1 0]])))
      (testing "2x1"
        (is (right-shape [[1 0]]))))))


{'fraction_non_alphanumeric': 0.1625677365568987, 'fraction_numerical': 0.041684035014589414, 'mean_word_length': 1.8368794326241136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '610374', 'n_tokens_mistral': 892, 'n_tokens_neox': 817, 'n_words': 240}