diff --git a/project.clj b/project.clj index 8c24584..bc3e944 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject histogram "2.0.0" +(defproject histogram "2.0.1" :description "Dynamic/streaming histograms" :source-path "src/clj" :java-source-path "src/java" diff --git a/src/clj/histogram/core.clj b/src/clj/histogram/core.clj index 6cbe6d2..ac92754 100644 --- a/src/clj/histogram/core.clj +++ b/src/clj/histogram/core.clj @@ -38,16 +38,15 @@ (instance? Histogram hist)) (defn- java-target [target] - (let [target-val (or (:sum target) (:counts target)) - missing-count (:missing-count target)] - (cond (number? target-val) - (NumericTarget. target-val missing-count) - (map? target-val) - (MapCategoricalTarget. (HashMap. target-val) missing-count) - (sequential? target-val) - (GroupTarget. (ArrayList. (map java-target target-val))) - (nil? target) - SimpleTarget/TARGET))) + (if (sequential? target) + (GroupTarget. (ArrayList. (map java-target target))) + (let [{:keys [sum sum-squares missing-count counts]} target] + (cond (contains? target :sum) + (NumericTarget. sum sum-squares missing-count) + (contains? target :counts) + (MapCategoricalTarget. (HashMap. counts) missing-count) + (nil? target) + SimpleTarget/TARGET)))) (defn- java-bin [bin] (let [{:keys [mean count target]} bin] @@ -275,7 +274,8 @@ {:max-bins (.getMaxBins hist) :gap-weighted? (.isCountWeightedGaps hist) :freeze (.getFreezeThreshold hist) - :group-types (seq (.getGroupTypes hist)) + :group-types (map (comp keyword str) + (seq (.getGroupTypes hist))) :categories (seq (.getTargetCategories hist)) :bins (bins hist) :missing-bin (when (pos? (.getMissingCount hist)) diff --git a/src/java/com/bigml/histogram/NumericTarget.java b/src/java/com/bigml/histogram/NumericTarget.java index 7ab928e..34c7fea 100644 --- a/src/java/com/bigml/histogram/NumericTarget.java +++ b/src/java/com/bigml/histogram/NumericTarget.java @@ -5,8 +5,8 @@ import org.json.simple.JSONArray; public class NumericTarget extends Target { - - private NumericTarget(Double target, Double sumSquares, double missingCount) { + + public NumericTarget(Double target, Double sumSquares, double missingCount) { _sum = target; _sumSquares = sumSquares; _missingCount = missingCount; @@ -19,7 +19,7 @@ public NumericTarget(Double target, double missingCount) { } _missingCount = missingCount; } - + public NumericTarget(Double target) { this(target, target == null ? 1 : 0); } @@ -41,7 +41,7 @@ public double getMissingCount() { public TargetType getTargetType() { return Histogram.TargetType.numeric; } - + @Override public String toString() { return String.valueOf(_sum) + "," + String.valueOf(_sumSquares); @@ -56,7 +56,7 @@ protected void addJSON(JSONArray binJSON, DecimalFormat format) { binJSON.add(Double.valueOf(format.format(_sumSquares))); } } - + @Override protected NumericTarget init() { return new NumericTarget(0d); @@ -83,7 +83,7 @@ protected NumericTarget sum(NumericTarget target) { _missingCount += target.getMissingCount(); return this; } - + @Override protected NumericTarget mult(double multiplier) { if (_sum != null) { diff --git a/test/histogram/test/core.clj b/test/histogram/test/core.clj index 53ed2a1..a6d428c 100644 --- a/test/histogram/test/core.clj +++ b/test/histogram/test/core.clj @@ -256,6 +256,12 @@ (is (== 5 (maximum merged))))) (deftest transform-test + (let [hist (-> (create) + (insert! 1 [2 3 :a]) + (insert! 1 [9 2 :b]) + (insert! 4 [5 nil nil]))] + (is (= (hist-to-clj hist) + (hist-to-clj (clj-to-hist (hist-to-clj hist)))))) (let [hist1 (reduce (fn [h [x y]] (insert! h x y)) (create :bins 8 :gap-weighted? true :categories [:apple :orange :grape])