From 3d63ded48e237f809a282864ebcd554e3daf5e86 Mon Sep 17 00:00:00 2001 From: bclayton-usgs Date: Wed, 22 May 2019 12:43:39 -0600 Subject: [PATCH] moved tree to nshmp-lib repo --- .../usgs/earthquake/nshmp/tree/Branch.java | 19 --- .../usgs/earthquake/nshmp/tree/LogicTree.java | 104 ------------ .../earthquake/nshmp/tree/RegularBranch.java | 38 ----- .../nshmp/tree/RegularLogicTree.java | 49 ------ .../nshmp/tree/SingleBranchTree.java | 40 ----- .../earthquake/nshmp/tree/LogicTreeTest.java | 150 ------------------ 6 files changed, 400 deletions(-) delete mode 100644 src/gov/usgs/earthquake/nshmp/tree/Branch.java delete mode 100644 src/gov/usgs/earthquake/nshmp/tree/LogicTree.java delete mode 100644 src/gov/usgs/earthquake/nshmp/tree/RegularBranch.java delete mode 100644 src/gov/usgs/earthquake/nshmp/tree/RegularLogicTree.java delete mode 100644 src/gov/usgs/earthquake/nshmp/tree/SingleBranchTree.java delete mode 100644 test/gov/usgs/earthquake/nshmp/tree/LogicTreeTest.java diff --git a/src/gov/usgs/earthquake/nshmp/tree/Branch.java b/src/gov/usgs/earthquake/nshmp/tree/Branch.java deleted file mode 100644 index 1efaa25f6..000000000 --- a/src/gov/usgs/earthquake/nshmp/tree/Branch.java +++ /dev/null @@ -1,19 +0,0 @@ -package gov.usgs.earthquake.nshmp.tree; - -/** - * A logic tree branch. - * - * @author Brandon Clayton - */ -public interface Branch { - - /** The branch id. */ - String id(); - - /** The branch value. */ - T value(); - - /** The branch weight. */ - double weight(); - -} diff --git a/src/gov/usgs/earthquake/nshmp/tree/LogicTree.java b/src/gov/usgs/earthquake/nshmp/tree/LogicTree.java deleted file mode 100644 index 4d215a40e..000000000 --- a/src/gov/usgs/earthquake/nshmp/tree/LogicTree.java +++ /dev/null @@ -1,104 +0,0 @@ -package gov.usgs.earthquake.nshmp.tree; - -import static com.google.common.base.Preconditions.checkState; - -import java.util.List; - -import com.google.common.collect.ImmutableList; - -import gov.usgs.earthquake.nshmp.data.Data; - -/** - * Logic tree interface that supports the iteration and sampling of individual - * branches. - * - * @author Brandon Clayton - */ -public interface LogicTree extends Iterable> { - - /* - * developer notes: - * - * For further consideration: serialization=, consider adding - * LogicTree.asGraph(), see Guava graph classes - * - * specialized trees and builders: threePointTree (THREE_POINT - * | THREE_POINT_262) takes values (id, T, σ) | (id, double, σ) - */ - - /** - * Return a logic tree branch corresponding to the supplied probability. - * - *

Note: this method does not check that {@code 0.0 ≤ p < 1.0}. - * - * @param probability in the range [0..1) - */ - Branch sample(double probability); - - /** - * Return an immutable list of logic tree branches corresponding to the - * supplied probabilities. - * - *

Note: this method does not check that each - * {@code 0.0 ≤ p < 1.0}. - * - * @param probabilities in the range [0..1) - */ - List> sample(double[] probabilities); - - /** - * Return a new {@code SingleBranchTree}. - * - * @param id the branch identifier - * @param value the branch value - */ - static SingleBranchTree singleBranch(String id, T value) { - return new SingleBranchTree<>(new RegularBranch(id, value, 1.0)); - } - - /** Return a new logic tree builder. */ - static Builder builder() { - return new Builder(); - } - - /** A logic tree builder. */ - static class Builder { - private ImmutableList.Builder> branches; - private boolean built; - - private Builder() { - branches = ImmutableList.builder(); - built = false; - } - - /** - * Add a {@code Branch} to the {@code LogicTree}. - * - * @param id the branch identifier - * @param weight the branch weight - * @param value the branch value - * @return this builder - */ - public Builder add(String id, T value, double weight) { - branches.add(new RegularBranch(id, value, weight)); - return this; - } - - /** Return a new {@code LogicTree}. */ - public LogicTree build() { - checkState(!built); - built = true; - - ImmutableList> branches = this.branches.build(); - checkState(!branches.isEmpty()); - - double[] weights = branches.stream() - .mapToDouble(Branch::weight) - .toArray(); - Data.checkWeights(weights, false); - Data.cumulate(weights); - - return new RegularLogicTree(branches, weights); - } - } -} diff --git a/src/gov/usgs/earthquake/nshmp/tree/RegularBranch.java b/src/gov/usgs/earthquake/nshmp/tree/RegularBranch.java deleted file mode 100644 index be3a6cd75..000000000 --- a/src/gov/usgs/earthquake/nshmp/tree/RegularBranch.java +++ /dev/null @@ -1,38 +0,0 @@ -package gov.usgs.earthquake.nshmp.tree; - -import static com.google.common.base.Preconditions.checkNotNull; - -import gov.usgs.earthquake.nshmp.data.Data; - -/** - * Basic logic tree branch implementation. - * - * @author Brandon Clayton - */ -class RegularBranch implements Branch { - private final String id; - private final double weight; - private final T value; - - RegularBranch(String id, T value, double weight) { - this.id = checkNotNull(id); - this.weight = Data.checkWeight(weight); - this.value = checkNotNull(value); - } - - @Override - public String id() { - return id; - } - - @Override - public T value() { - return value; - } - - @Override - public double weight() { - return weight; - } - -} diff --git a/src/gov/usgs/earthquake/nshmp/tree/RegularLogicTree.java b/src/gov/usgs/earthquake/nshmp/tree/RegularLogicTree.java deleted file mode 100644 index 5763bbac6..000000000 --- a/src/gov/usgs/earthquake/nshmp/tree/RegularLogicTree.java +++ /dev/null @@ -1,49 +0,0 @@ -package gov.usgs.earthquake.nshmp.tree; - -import java.util.Iterator; -import java.util.List; - -import com.google.common.collect.ImmutableList; - -/** - * Basic logic tree implementation. - * - * @author Brandon Clayton - * @author Peter Powers - */ -class RegularLogicTree implements LogicTree { - - private final List> branches; - private final double[] cumulativeWeights; - - RegularLogicTree(List> branches, double[] cumulativeWeights) { - this.branches = branches; - this.cumulativeWeights = cumulativeWeights; - } - - @Override - public Branch sample(double probability) { - for (int i = 0; i < cumulativeWeights.length; i++) { - if (probability < cumulativeWeights[i]) { - return branches.get(i); - } - } - return branches.get(cumulativeWeights.length - 1); - } - - @Override - public List> sample(double[] probabilities) { - ImmutableList.Builder> samples = - ImmutableList.builderWithExpectedSize(probabilities.length); - for (double probability : probabilities) { - samples.add(sample(probability)); - } - return samples.build(); - } - - @Override - public Iterator> iterator() { - return branches.iterator(); - } - -} diff --git a/src/gov/usgs/earthquake/nshmp/tree/SingleBranchTree.java b/src/gov/usgs/earthquake/nshmp/tree/SingleBranchTree.java deleted file mode 100644 index 69860e48e..000000000 --- a/src/gov/usgs/earthquake/nshmp/tree/SingleBranchTree.java +++ /dev/null @@ -1,40 +0,0 @@ -package gov.usgs.earthquake.nshmp.tree; - -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -import com.google.common.collect.Iterators; - -/** - * Basic logic tree with a single branch. - * - *

Use {@link LogicTree#singleBranch(String, double, Object)} for new - * instance. - * - * @author Brandon Clayton - */ -class SingleBranchTree implements LogicTree { - - private final Branch branch; - - SingleBranchTree(Branch branch) { - this.branch = branch; - } - - @Override - public Branch sample(double probability) { - return branch; - } - - @Override - public List> sample(double[] probabilities) { - return Collections.nCopies(probabilities.length, branch); - } - - @Override - public Iterator> iterator() { - return Iterators.singletonIterator(branch); - } - -} diff --git a/test/gov/usgs/earthquake/nshmp/tree/LogicTreeTest.java b/test/gov/usgs/earthquake/nshmp/tree/LogicTreeTest.java deleted file mode 100644 index dd93e40ac..000000000 --- a/test/gov/usgs/earthquake/nshmp/tree/LogicTreeTest.java +++ /dev/null @@ -1,150 +0,0 @@ -package gov.usgs.earthquake.nshmp.tree; - -import static org.junit.Assert.assertEquals; - -import java.util.List; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import gov.usgs.earthquake.nshmp.gmm.DefaultScalarGroundMotion; -import gov.usgs.earthquake.nshmp.tree.Branch; -import gov.usgs.earthquake.nshmp.tree.LogicTree.Builder; - -/** - * JUnit tests for LogicTree - */ -@SuppressWarnings("javadoc") -public class LogicTreeTest { - - private static final String[] KEYS = new String[] { - "Branch1", - "Branch2", - "Branch3", - "Branch4" }; - - private static final double[] WEIGHTS = new double[] { 0.40, 0.30, 0.20, 0.10 }; - - private static final double[] CUML_WEIGHTS = new double[] { 0.39, 0.69, 0.89, 0.99 }; - - private static final DefaultScalarGroundMotion GM = DefaultScalarGroundMotion - .create(1.0, 0.5); - - private static final LogicTree TREE = LogicTree - . builder() - .add(KEYS[0], GM, WEIGHTS[0]) - .add(KEYS[1], GM, WEIGHTS[1]) - .add(KEYS[2], GM, WEIGHTS[2]) - .add(KEYS[3], GM, WEIGHTS[3]) - .build(); - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public final void builderEmpty() { - thrown.expect(IllegalStateException.class); - - LogicTree. builder().build(); - } - - @Test - public final void builderAlreadyBuild() { - thrown.expect(IllegalStateException.class); - - Builder builder = LogicTree - . builder() - .add(KEYS[0], GM, 1.0); - - builder.build(); - builder.build(); - } - - @Test - public final void builderNullKey() { - thrown.expect(NullPointerException.class); - - LogicTree. builder() - .add(null, GM, 1.0) - .build(); - } - - @Test - public final void builderNullValue() { - thrown.expect(NullPointerException.class); - - LogicTree. builder() - .add(KEYS[0], null, 1.0) - .build(); - } - - @Test - public final void builderBadWeight() { - thrown.expect(IllegalArgumentException.class); - - LogicTree. builder() - .add(KEYS[0], GM, 2.0); - } - - @Test - public final void builderBuildBadWeights() { - thrown.expect(IllegalArgumentException.class); - - LogicTree. builder() - .add(KEYS[0], GM, 1.0) - .add(KEYS[1], GM, 1.0) - .build(); - } - - @Test - public final void equals() { - int index = 0; - - List> sampleBranches = TREE.sample(CUML_WEIGHTS); - assertEquals(CUML_WEIGHTS.length, sampleBranches.size(), 0); - - for (Branch branch : TREE) { - String key = KEYS[index]; - double weight = WEIGHTS[index]; - Branch sampleBranch = TREE.sample(CUML_WEIGHTS[index]); - - checkBranch(key, weight, GM, branch); - checkBranch(key, weight, GM, sampleBranch); - checkBranch(key, weight, GM, sampleBranches.get(index)); - - index++; - } - - Branch sampleBranch = TREE.sample(1.1); - checkBranch(KEYS[3], WEIGHTS[3], GM, sampleBranch); - } - - @Test - public final void singleBranchEquals() { - String key = KEYS[0]; - double weight = 1.0; - - SingleBranchTree tree = LogicTree.singleBranch(key, GM); - tree.forEach((branch) -> checkBranch(key, weight, GM, branch)); - - Branch sampleBranch = tree.sample(2.0); - checkBranch(key, weight, GM, sampleBranch); - - List> sampleBranches = tree.sample(WEIGHTS); - sampleBranches.forEach((branch) -> checkBranch(key, weight, GM, branch)); - assertEquals(WEIGHTS.length, sampleBranches.size()); - } - - private static void checkBranch( - String key, - double weight, - DefaultScalarGroundMotion value, - Branch branch) { - assertEquals(key, branch.id()); - assertEquals(weight, branch.weight(), 0); - assertEquals(value.mean(), branch.value().mean(), 0); - assertEquals(value.sigma(), branch.value().sigma(), 0); - } - -}