PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/org.integratedmodelling.aries.core/src/org/integratedmodelling/aries/core/implementations/datasources/SPANDistributionState.java

https://github.com/kbagstad/aries
Java | 144 lines | 96 code | 29 blank | 19 comment | 28 complexity | 190ee5e894cf8b833211882c67ec2ce5 MD5 | raw file
  1. package org.integratedmodelling.aries.core.implementations.datasources;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5. import org.integratedmodelling.corescience.context.ObservationContext;
  6. import org.integratedmodelling.corescience.implementations.datasources.MemDoubleContextualizedDatasource;
  7. import org.integratedmodelling.corescience.interfaces.IContext;
  8. import org.integratedmodelling.corescience.interfaces.IState;
  9. import org.integratedmodelling.corescience.metadata.Metadata;
  10. import org.integratedmodelling.thinklab.exception.ThinklabRuntimeException;
  11. import org.integratedmodelling.thinklab.interfaces.knowledge.IConcept;
  12. import org.integratedmodelling.thinklab.transformations.ITransformation;
  13. import org.integratedmodelling.thinklab.transformations.TransformationFactory;
  14. import clojure.lang.IFn;
  15. public class SPANDistributionState extends MemDoubleContextualizedDatasource
  16. implements IState {
  17. // how many times the stdev should encompass the data range before we declare
  18. // complete uncertainty.
  19. // TODO this should encompass the POTENTIAL data range, not the actual.
  20. private static final double STD_UNCERTAINTY_MULTIPLIER = 1.6;
  21. private IFn closure = null;
  22. private int rows;
  23. private int cols;
  24. // for serializer only
  25. public SPANDistributionState() {
  26. }
  27. public SPANDistributionState(IConcept type, int rows, int cols, IFn clojure, IContext context) {
  28. super(type, rows*cols, context);
  29. this.closure = clojure;
  30. this.rows = rows;
  31. this.cols = cols;
  32. metadata.put(Metadata.CONTINUOUS, Boolean.TRUE);
  33. // TODO make it lazy by computing values only when getDataAsDoubles is called
  34. computeValues(clojure);
  35. }
  36. private void computeValues(IFn clojure) {
  37. try {
  38. Map<?,?> map = (Map<?, ?>) closure.invoke();
  39. ITransformation transformation =
  40. TransformationFactory.get().getTransformation(getObservableClass());
  41. /*
  42. * we get a different distribution than the one we originally set in,
  43. * so we store mean and standard deviation for now. Later we can get
  44. * the whole thing and use it for more.
  45. */
  46. if (map != null) {
  47. double mmax = 0.0; double mmin = 0.0;
  48. double smax = 0.0; double smin = 0.0;
  49. boolean first = true;
  50. double[] uncertainty = null;
  51. for (Map.Entry<?,?> e : map.entrySet()) {
  52. // LazilyPersistentVector
  53. Iterable<?> location = (Iterable<?>) e.getKey();
  54. // PersistentHashMap
  55. Map<?,?> distribu = (Map<?, ?>) e.getValue();
  56. if (distribu == null || location == null)
  57. continue;
  58. Iterator<?> it = location.iterator();
  59. int x = ((Number)(it.next())).intValue();
  60. int y = ((Number)(it.next())).intValue();
  61. double mean = 0.0; double std = 0.0;
  62. for (Entry<?, ?> en : distribu.entrySet()) {
  63. if (en.getValue() == null || en.getKey() == null)
  64. continue;
  65. double v = ((Number)en.getKey()).doubleValue();
  66. double p = ((Number)en.getValue()).doubleValue();
  67. mean += v*p;
  68. std += v*v*p;
  69. }
  70. std -= mean*mean; std = Math.sqrt(std);
  71. /*
  72. * set mean and std in proper location. If std == 0 it was
  73. * a deterministic source.
  74. */
  75. if (first) {
  76. first = false;
  77. mmax = mmin = mean;
  78. } else {
  79. if (mean > mmax) mmax = mean;
  80. if (mean < mmin) mmin = mean;
  81. }
  82. data[(x*cols) + y] =
  83. transformation == null ?
  84. mean :
  85. transformation.transform(new Double(mean), null);
  86. if (Double.compare(std, 0.0) != 0) {
  87. if (uncertainty == null) {
  88. uncertainty = new double[this.rows*this.cols];
  89. smax = smin = std;
  90. } else {
  91. if (std > smax) smax = std;
  92. if (std < smin) smin = std;
  93. }
  94. uncertainty[(x*cols) + y] = std;
  95. }
  96. }
  97. /*
  98. * normalize uncertainty and insert it
  99. */
  100. if (uncertainty != null) {
  101. double range = (mmax - mmin) * STD_UNCERTAINTY_MULTIPLIER;
  102. for (int i = 0; i < uncertainty.length; i++) {
  103. uncertainty[i] = uncertainty[i]/range;
  104. if (uncertainty[i] > 1.0)
  105. uncertainty[i] = 1.0;
  106. uncertainty[i] = 1 - uncertainty[i];
  107. }
  108. getMetadata().put(Metadata.UNCERTAINTY, uncertainty);
  109. }
  110. }
  111. } catch (Exception e) {
  112. throw new ThinklabRuntimeException(e);
  113. }
  114. }
  115. }