Skip to content

Latest commit

 

History

History
341 lines (309 loc) · 12.9 KB

formal_conceptual_view.org

File metadata and controls

341 lines (309 loc) · 12.9 KB

Experiment: Formal Conceptual Simitlarity

Formal Conceptual Similarity

Imports

(ns obstsalat
  (:require [conexp.fca.contexts :as fast]
            [conexp.io.contexts :as fast]
            [conexp.fca.fast :as fast]
            [conexp.gui.draw :refer :all]
            [conexp.layouts.base :refer :all]
            [conexp.layouts.freese :refer :all]
            [clojure.string :as string]
            [clojure.core.async :as async]))

Input

For drawing with covering elements use the namespace from the decision tree scaling.

Read Data

(def r 
  (read-context
   "scales/fruit_class/resnet_16_w_bin.csv" 
   :named-binary-csv))

Interesting Object to draw

Interesting objects to draw are “Apple Pink Lady” “Apple Red 1”, “Cherry 1” “Plum”

Helper Methods

Positive Context

(defn positive-ctx
  [ctx]
  (make-context (objects ctx)
                (filter #(= "+" (str (first %))) (attributes ctx))
                (incidence ctx)))

object concept filter

(defn object-concept-lattice-filter
  "Computes all concepts containing object g and their covering
  concepts."
  [ctx g]
  (let [first-C [(context-object-closure ctx #{g}) 
                 (object-derivation ctx #{g})]]
    (loop [BV #{first-C} 
           queue #{first-C}]
      (if (empty? queue) 
        BV
        (let [C (first queue)]
          (let [covering-C (direct-upper-concepts ctx C)
                ;; those not containing g can be added since they are in cover with a concept containing c
                new-C (difference covering-C BV) 
                ;; only continue with those that contain g to ensure selection criteria
                ]
            (recur (into BV new-C) (into (disj queue C) new-C))))))))

Sizes

Here we use pcbo implementation to compute all concepts. Either put a pcbo executable in your path or remove :pcbo.

(doseq [f files]
  (let [ctx (read-context f :named-binary-csv)]
    (println (last (string/split f #"/"))
             (count (fast/concepts :pcbo ctx)))))

(doseq [f files]
  (let [ctx (read-context f :named-binary-csv)
        Pos (fn [A] (filter #(= "+" (-> % first str)) A))
        ctx (make-context (objects ctx)
                          (-> ctx attributes Pos)
                          (incidence-relation ctx))]
    (println (last (string/split f #"/")) " Pos "
             (count (fast/concepts :pcbo ctx)))))

(doseq [f files32]
  (let [ctx (read-context f :named-binary-csv)]
    (println (last (string/split f #"/"))
             (count (fast/concepts :pcbo ctx)))))

(doseq [f files32]
  (let [ctx (read-context f :named-binary-csv)
        Pos (fn [A] (filter #(= "+" (-> % first str)) A))
        ctx (make-context (objects ctx)
                          (-> ctx attributes Pos)
                          (incidence-relation ctx))]
    (println (last (string/split f #"/")) " Pos "
             (count (fast/concepts :pcbo ctx)))))
ModelN 16N 32
AllPosAllPos
base13096965173192044155416
Resnet5013313058723803799165009
vgg1612648752003498829193516
incv313410056703782226198152
effb013240365733767964150884

Formal Conceptual Similarity

Files

(def directory 
   (clojure.java.io/file "scales/fruit_class" ))
(def files 
       (filter #(= "16_w_bin.csv" 
                   (subs % 
                         (- (count %) (count "16_w_bin.csv"))))
       (map #(.getAbsolutePath %)(file-seq directory))))
(def files32
       (filter #(= "32_w_bin.csv" 
                   (subs % 
                         (- (count %) (count "32_w_bin.csv"))))
       (map #(.getAbsolutePath %)(file-seq directory))))

Similarities

(doseq [f files]
   (let [ctx (read-context f :named-binary-csv)
         ctx+ (positive-ctx ctx)]
         (println f "\n")
         (let [common #(count (intersection (set %1) (set %2)))
               cherry (future (object-concept-lattice-filter ctx "Cherry 1"))
               cherry+ (future (object-concept-lattice-filter ctx+ "Cherry 1"))
               plum (future (object-concept-lattice-filter ctx "Plum"))
               plum+ (future (object-concept-lattice-filter ctx+ "Plum"))
               pink-lady (future(object-concept-lattice-filter ctx "Apple Pink Lady"))
               pink-lady+ (future(object-concept-lattice-filter ctx+ "Apple Pink Lady"))
               apple-red (future(object-concept-lattice-filter ctx "Apple Red 1"))
               apple-red+ (future(object-concept-lattice-filter ctx+ "Apple Red 1"))]
               (println "Fruit,Filter,Cherry,Plum,Pink Lady,Apple Red")
               (let [cur @cherry+] (println "Cherry+,"(count cur)","(common cur @cherry+)","(common cur @plum+)","(common cur @pink-lady+)","(common cur @apple-red+)))
               (let [cur @plum+] (println "Plum+,"(count cur)","(common cur @cherry+)","(common cur @plum+)","(common cur @pink-lady+)","(common cur @apple-red+)))
               (let [cur @pink-lady+] (println "Pink Lady+,"(count cur)","(common cur @cherry+)","(common cur @plum+)","(common cur @pink-lady+)","(common cur @apple-red+)))
               (let [cur @apple-red+] (println "Apple Red+,"(count cur)","(common cur @cherry+)","(common cur @plum+)","(common cur @pink-lady+)","(common cur @apple-red+)))
               (let [cur @cherry] (println "Cherry,"(count cur)","(common cur @cherry)","(common cur @plum)","(common cur @pink-lady)","(common cur @apple-red)))
               (let [cur @plum] (println "Plum,"(count cur)","(common cur @cherry)","(common cur @plum)","(common cur @pink-lady)","(common cur @apple-red)))
               (let [cur @pink-lady] (println "Pink Lady,"(count cur)","(common cur @cherry)","(common cur @plum)","(common cur @pink-lady)","(common cur @apple-red)))
               (let [cur @apple-red] (println "Apple Red,"(count cur)","(common cur @cherry)","(common cur @plum)","(common cur @pink-lady)","(common cur @apple-red))))))

scales/effb0_16_w_bin.csv

FruitFilterCherryPlumPink LadyApple Red
Cherry+6060211
Plum+3223244
Pink Lady+32143232
Apple Red+32143232
Cherry619561951113232
Plum49401114940553553
Pink Lady63933255363936393
Apple Red63933255363936393

scales/resnet_16_w_bin.csv

FruitFilterCherryPlumPink LadyApple Red
Cherry+917917161164
Plum+1201612084
Pink Lady+21211682128
Apple Red+1644816
Cherry612761276295632
Plum6149626149123393
Pink Lady62379561236237651
Apple Red6317323936516317

scales/vgg16_16_w_bin.csv

FruitFilterCherryPlumPink LadyApple Red
Cherry+60608168
Plum+328321616
Pink Lady+233161623332
Apple Red+22181632221
Cherry59565956958620227
Plum58669585866966897
Pink Lady70686209667068621
Apple Red61932278976216193

scales/incv3_16_w_bin.csv

FruitFilterCherryPlumPink LadyApple Red
Cherry+3743743743232
Plum+3743743743232
Pink Lady+116323211659
Apple Red+346323259346
Cherry567856785678607216
Plum567856785678607216
Pink Lady612960760761291390
Apple Red574121621613905741

scales/base16_w_bin.csv

FruitFilterCherryPlumPink LadyApple Red
Cherry+64643228
Plum+39632396416
Pink Lady+3224324
Apple Red+60816460
Cherry55725572908115526
Plum6104908610462348
Pink Lady5895115625895333
Apple Red52355263483335235

Values for N=16, positives and all

effb0

61951113232
1114940553553
3255363936393
3255363936393

resnet

61276295632
626149123393
9561236237651
323936516317

vgg16

5956958620227
9585866966897
6209667068621
2278976216193

incv3

56785678607216
56785678607216
60760761291390
21621613905741

base

5572908115526
908610462348
115625895333
5263483335235

effb0 positives

60211
23244
143232
143232

resnet 50 positives

917161164
1612084
11682128
44816

vgg16 positives

608168
8321616
161623332
81632221

incv3 positives

3743743232
3743743232
323211659
323259346

base positives

643228
32396416
24324
816460

Lattice drawing

(def vgg16-plum+ (let [f "scales/fruit_class/vgg16_16_w_bin.csv"]
                               (let [ctx (read-context f :named-binary-csv)
                                     ctx+ (positive-ctx ctx)]
                                 (object-concept-lattice-filter ctx+ "Plum"))))

(draw-layout
     (update-valuations 
      (freese-layout 
       (make-lattice-nc 
       vgg16-plum+
        (fn [[a b] [a2 b2]] 
          (subset? a a2))))
       (fn [[A B]] (map #(apply str (map first (clojure.string/split % #" "))) (intersection #{"Cherry 1" "Apple Pink Lady" "Apple Red 1"} A)))))