Skip to content

Commit

Permalink
modify :json context schema so that empty attribute columns are not d…
Browse files Browse the repository at this point in the history
…ropped any more (#92)
  • Loading branch information
jana-fischer committed Jun 29, 2022
1 parent 3cb86b4 commit 2c7b589
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
24 changes: 15 additions & 9 deletions src/main/clojure/conexp/io/contexts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,10 @@
(add-context-input-format :csv
(fn [rdr]
(try
(re-matches #"^[^,]+,[^,]+$" (read-line))
(let [first-line (read-line)]
(and (re-matches #"^[^,]+,[^,]+$" first-line)
;; do not read empty json context as csv
(not= first-line "{\"attributes\":[],\"adjacency-list\":[]}")))
(catch Exception _))))

(define-context-input-format :csv
Expand Down Expand Up @@ -624,11 +627,13 @@
"Returns a formal context as a map that can easily be converted into json format.
Example:
{formal_context: {
object: \"b\",
attributes: [\"1\", \"2\"]}}"
{:attributes (\"1\", \"2\")
:adjacency-list
[{:object \"b\",
:attributes (\"1\", \"2\")}]}"
[ctx]
{:formal_context
{:attributes (into () (attributes ctx))
:adjacency-list
(mapv (partial object->json ctx) (objects ctx))})

(defn- json-ctx->incidence
Expand All @@ -639,9 +644,10 @@
(defn json->ctx
"Returns a Context object for the given json context."
[json-ctx]
(let [objects (map :object json-ctx)
attributes (distinct (flatten (map :attributes json-ctx)))
incidence (apply union (mapv json-ctx->incidence json-ctx))]
(let [attributes (:attributes json-ctx)
ctx-list (:adjacency-list json-ctx)
objects (map :object ctx-list)
incidence (apply union (mapv json-ctx->incidence ctx-list))]
(make-context objects attributes incidence)))

;; Json Format (src/main/resources/schemas/context_schema_v1.0.json)
Expand All @@ -660,7 +666,7 @@
[file]
(with-in-reader file
(let [file-content (json/read *in* :key-fn keyword)
json-ctx (:formal_context file-content)
json-ctx file-content
schema-file "src/main/resources/schemas/context_schema_v1.0.json"]
(assert (matches-schema? file-content schema-file)
(str "The input file does not match the schema given at " schema-file "."))
Expand Down
2 changes: 1 addition & 1 deletion src/main/clojure/conexp/io/fcas.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
(let [json-ctx (:context json-fca)
json-lattice (:lattice json-fca)
json-implication-sets (:implication_sets json-fca)]
(cond-> {:context (json->ctx (:formal_context json-ctx))}
(cond-> {:context (json->ctx json-ctx)}
(some? json-lattice) (assoc :lattice (json->lattice json-lattice))
(some? json-implication-sets) (assoc :implication-sets (map json->implications json-implication-sets)))))

Expand Down
12 changes: 10 additions & 2 deletions src/main/resources/schemas/context_schema_v1.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@
}
},
"required": [
"formal_context"
"attributes",
"adjacency-list"
],
"properties": {
"formal_context": {
"attributes": {
"type": "array",
"description": "List of all attributes",
"items": {
"$ref": "fca_schema_v1.0.json#/$defs/attribute"
}
},
"adjacency-list": {
"type": "array",
"items": {
"$ref": "#/$defs/context_item"
Expand Down
9 changes: 8 additions & 1 deletion src/test/clojure/conexp/io/contexts_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@
fmt #{:fcalgs}]
(= ctx (out-in-without-format ctx 'context fmt))))

;;;
(deftest test-ctx->json
;; test that attributes with empty column are not dropped
(let [K (make-context-from-matrix [1 2] [:a :b] [1 0 0 0])]
(is (= (ctx->json K)
{:attributes '(:a :b)
:adjacency-list
[{:object 1, :attributes '(:a)}
{:object 2, :attributes '()}]}))))

nil
2 changes: 1 addition & 1 deletion testing-data/digits-context.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"formal_context":[{"object":"9","attributes":["d","f","a","b","g"]},{"object":"3","attributes":["f","a","b","g","c"]},{"object":"4","attributes":["d","f","b","g"]},{"object":"8","attributes":["d","f","e","a","b","g","c"]},{"object":"7","attributes":["f","a","g"]},{"object":"5","attributes":["d","a","b","g","c"]},{"object":"6","attributes":["d","e","b","g","c"]},{"object":"1","attributes":["f","g"]},{"object":"0","attributes":["d","f","e","a","g","c"]},{"object":"2","attributes":["f","e","a","b","c"]}]}
{"attributes":["a","b","c","d","e","f","g"],"adjacency-list":[{"object":"9","attributes":["d","f","a","b","g"]},{"object":"3","attributes":["f","a","b","g","c"]},{"object":"4","attributes":["d","f","b","g"]},{"object":"8","attributes":["d","f","e","a","b","g","c"]},{"object":"7","attributes":["f","a","g"]},{"object":"5","attributes":["d","a","b","g","c"]},{"object":"6","attributes":["d","e","b","g","c"]},{"object":"1","attributes":["f","g"]},{"object":"0","attributes":["d","f","e","a","g","c"]},{"object":"2","attributes":["f","e","a","b","c"]}]}
3 changes: 2 additions & 1 deletion testing-data/digits-fca.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"context": {
"formal_context": [
"attributes": ["a","b","c","d","e","f","g"],
"adjacency-list": [
{
"object": "9",
"attributes": [
Expand Down

0 comments on commit 2c7b589

Please sign in to comment.