Skip to content

Commit

Permalink
Fixes clj-java transformation bugs
Browse files Browse the repository at this point in the history
Fixes the transformation from clj to java for the sum of squares
(in numeric targets) and fixes transformations for group targets.
  • Loading branch information
ashenfad committed Oct 20, 2012
1 parent 962437b commit e44be2c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion 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"
Expand Down
22 changes: 11 additions & 11 deletions src/clj/histogram/core.clj
Expand Up @@ -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]
Expand Down Expand Up @@ -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))
Expand Down
12 changes: 6 additions & 6 deletions src/java/com/bigml/histogram/NumericTarget.java
Expand Up @@ -5,8 +5,8 @@
import org.json.simple.JSONArray;

public class NumericTarget extends Target<NumericTarget> {
private NumericTarget(Double target, Double sumSquares, double missingCount) {

public NumericTarget(Double target, Double sumSquares, double missingCount) {
_sum = target;
_sumSquares = sumSquares;
_missingCount = missingCount;
Expand All @@ -19,7 +19,7 @@ public NumericTarget(Double target, double missingCount) {
}
_missingCount = missingCount;
}

public NumericTarget(Double target) {
this(target, target == null ? 1 : 0);
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -83,7 +83,7 @@ protected NumericTarget sum(NumericTarget target) {
_missingCount += target.getMissingCount();
return this;
}

@Override
protected NumericTarget mult(double multiplier) {
if (_sum != null) {
Expand Down
6 changes: 6 additions & 0 deletions test/histogram/test/core.clj
Expand Up @@ -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])
Expand Down

0 comments on commit e44be2c

Please sign in to comment.