Changeset 559 for trunk/quest-core-coverage/src/main/java/de
- Timestamp:
- 08/17/12 09:05:19 (12 years ago)
- Location:
- trunk/quest-core-coverage/src/main/java/de/ugoe/cs/quest/coverage
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/quest-core-coverage/src/main/java/de/ugoe/cs/quest/coverage/CoverageCalculatorObserved.java
r547 r559 1 1 2 package de.ugoe.cs.quest.coverage; 2 3 … … 12 13 /** 13 14 * <p> 14 * This class calculates various types of sequence coverage in relation to a 15 * collection of observedsequences.15 * This class calculates various types of sequence coverage in relation to a collection of observed 16 * sequences. 16 17 * </p> 17 18 * … … 21 22 public class CoverageCalculatorObserved { 22 23 23 /** 24 * <p> 25 * Sequences for which the coverage is calculated. 26 * </p> 27 */ 28 private final Collection<List<Event>> sequences; 29 30 /** 31 * <p> 32 * Observed sequences that are baseline for the coverage calculation. 33 * </p> 34 */ 35 private final Collection<List<Event>> observedSequences; 36 37 /** 38 * <p> 39 * Length of the subsequences in relation to which the covarage is 40 * calculated. 41 * </p> 42 */ 43 private final int length; 44 45 /** 46 * <p> 47 * All subsequences of {@link #length} of {@link #sequences}. 48 * </p> 49 */ 50 private Collection<List<Event>> subSeqsGenerated = null; 51 52 /** 53 * <p> 54 * All subsequences of {@link #length} of {@link #observedSequences}. 55 * </p> 56 */ 57 private Collection<List<Event>> subSeqsObserved = null; 58 59 /** 60 * <p> 61 * Constructor. Creates a new CoverageCalculatorObserved for given 62 * collections of observed sequences and generated sequences. 63 * </p> 64 * 65 * @param observedSequences 66 * observed sequences in relation to which the coverage is 67 * calculated; must not be null 68 * @param sequences 69 * sequences for which the coverage is calculated; must not be 70 * null 71 * @param length 72 * length of the subsequences for which the coverage is analyzed; 73 * must be >0 74 * @throws InvalidParameterException 75 * thrown if observedSequences or sequences is null or length 76 * less than or equal to 0 77 */ 78 public CoverageCalculatorObserved( 79 Collection<List<Event>> observedSequences, 80 Collection<List<Event>> sequences, int length) { 81 if (observedSequences == null) { 82 throw new InvalidParameterException( 83 "observed sequences must not be null"); 84 } 85 if (sequences == null) { 86 throw new InvalidParameterException("sequences must not be null"); 87 } 88 if (length <= 0) { 89 throw new InvalidParameterException( 90 "length must be >0; actual value: " + length); 91 } 92 this.observedSequences = observedSequences; 93 this.sequences = sequences; 94 this.length = length; 95 } 96 97 /** 98 * <p> 99 * Calculates the percentage of subsequences of length k that occur, with 100 * reference to those that were observed. 101 * </p> 102 * 103 * @return coverage percentage 104 */ 105 public double getCoverageObserved() { 106 createSubSeqs(); 107 Collection<List<Event>> subSeqsObservedCopy = new LinkedHashSet<List<Event>>( 108 subSeqsObserved); 109 subSeqsObservedCopy.retainAll(subSeqsGenerated); 110 return ((double) subSeqsObservedCopy.size()) / subSeqsObserved.size(); 111 } 112 113 /** 114 * <p> 115 * Calculates the weight of subsequences of length k that occur, with 116 * reference to those that were observed. 117 * </p> 118 * 119 * @param process 120 * stochastic process in reference to which the weight is 121 * calculated 122 * @return coverage percentage 123 */ 124 125 public double getCoverageObservedWeigth(IStochasticProcess process) { 126 createSubSeqs(); 127 Map<List<Event>, Double> weightMap = SequenceTools 128 .generateWeights(process, subSeqsObserved); 129 130 Collection<List<Event>> subSeqsObservedCopy = new LinkedHashSet<List<Event>>( 131 subSeqsObserved); 132 subSeqsObservedCopy.retainAll(subSeqsGenerated); 133 double weight = 0.0d; 134 for (List<Event> subSeq : subSeqsObservedCopy) { 135 weight += weightMap.get(subSeq); 136 } 137 return weight; 138 } 139 140 /** 141 * <p> 142 * Calculates the percentage of generated subsequences of length k that 143 * occur and have not been observed, with reference to all generated 144 * subsequences. 145 * </p> 146 * 147 * @return coverage percentage 148 */ 149 public double getNewPercentage() { 150 createSubSeqs(); 151 Collection<List<Event>> subSeqsGeneratedCopy = new LinkedHashSet<List<Event>>( 152 subSeqsGenerated); 153 subSeqsGeneratedCopy.removeAll(subSeqsObserved); 154 return ((double) subSeqsGeneratedCopy.size()) / subSeqsGenerated.size(); 155 } 156 157 /** 158 * <p> 159 * Calculates the percentage of generated subsequences of length k that 160 * occur and have not been observed, with references to all possible new 161 * subsequences. 162 * </p> 163 * 164 * @param process 165 * stochastic process which is used to determine which 166 * subsequences are possible 167 * @return coverage percentage 168 * @throws InvalidParameterException 169 * thrown if process is null 170 */ 171 public double getCoveragePossibleNew(IStochasticProcess process) { 172 if (process == null) { 173 throw new InvalidParameterException("process must not be null"); 174 } 175 createSubSeqs(); 176 Collection<List<Event>> subSeqsGeneratedCopy = new LinkedHashSet<List<Event>>( 177 subSeqsGenerated); 178 Collection<List<Event>> subSeqsPossible = process 179 .generateSequences(length); 180 subSeqsGeneratedCopy.removeAll(subSeqsObserved); 181 subSeqsPossible.removeAll(subSeqsObserved); 182 int possibleSize = subSeqsPossible.size(); 183 subSeqsPossible.retainAll(subSeqsGeneratedCopy); 184 return ((double) subSeqsPossible.size()) / possibleSize; 185 } 186 187 /** 188 * <p> 189 * Calculates the weight of generated subsequences of length k that occur 190 * and have not been observed, with references to all possible new 191 * subsequences. 192 * </p> 193 * 194 * @param process 195 * stochastic process which is used to determine the weights and 196 * which subsequences are possible 197 * @return coverage percentage 198 * @throws InvalidParameterException 199 * thrown if process is null 200 */ 201 public double getCoveragePossibleNewWeight(IStochasticProcess process) { 202 if (process == null) { 203 throw new InvalidParameterException("process must not be null"); 204 } 205 createSubSeqs(); 206 Collection<List<Event>> subSeqsGeneratedCopy = new LinkedHashSet<List<Event>>( 207 subSeqsGenerated); 208 Collection<List<Event>> subSeqsPossible = process 209 .generateSequences(length); 210 subSeqsGeneratedCopy.removeAll(subSeqsObserved); 211 subSeqsPossible.removeAll(subSeqsObserved); 212 Map<List<Event>, Double> weightMap = SequenceTools 213 .generateWeights(process, subSeqsPossible); 214 double weight = 0.0d; 215 for (List<Event> subSeq : subSeqsGeneratedCopy) { 216 Double currentWeight = weightMap.get(subSeq); 217 if( currentWeight!=null ) { 218 weight += currentWeight; 219 } 220 } 221 return weight; 222 } 223 224 /** 225 * <p> 226 * Returns the number of covered subsequences of length k. 227 * </p> 228 * 229 * @return number of covered subsequences 230 */ 231 public int getNumObserved() { 232 createSubSeqs(); 233 return subSeqsObserved.size(); 234 } 235 236 /** 237 * <p> 238 * Returns the number of covered subsequences of length k. 239 * </p> 240 * 241 * @return number of covered subsequences 242 */ 243 public int getNumCovered() { 244 createSubSeqs(); 245 return subSeqsGenerated.size(); 246 } 247 248 public int getNumNew() { 249 createSubSeqs(); 250 Collection<List<Event>> subSeqsGeneratedCopy = new LinkedHashSet<List<Event>>( 251 subSeqsGenerated); 252 subSeqsGeneratedCopy.removeAll(subSeqsObserved); 253 return subSeqsGeneratedCopy.size(); 254 } 255 256 /** 257 * <p> 258 * Helper function that calcuates the subsequences of length k that have 259 * been observed and generated. 260 * </p> 261 */ 262 private void createSubSeqs() { 263 if (subSeqsObserved == null) { 264 subSeqsObserved = SequenceTools.containedSubSequences( 265 observedSequences, length); 266 } 267 if (subSeqsGenerated == null) { 268 subSeqsGenerated = SequenceTools.containedSubSequences(sequences, 269 length); 270 } 271 } 24 /** 25 * <p> 26 * Sequences for which the coverage is calculated. 27 * </p> 28 */ 29 private final Collection<List<Event>> sequences; 30 31 /** 32 * <p> 33 * Observed sequences that are baseline for the coverage calculation. 34 * </p> 35 */ 36 private final Collection<List<Event>> observedSequences; 37 38 /** 39 * <p> 40 * Length of the subsequences in relation to which the covarage is calculated. 41 * </p> 42 */ 43 private final int length; 44 45 /** 46 * <p> 47 * All subsequences of {@link #length} of {@link #sequences}. 48 * </p> 49 */ 50 private Collection<List<Event>> subSeqsGenerated = null; 51 52 /** 53 * <p> 54 * All subsequences of {@link #length} of {@link #observedSequences}. 55 * </p> 56 */ 57 private Collection<List<Event>> subSeqsObserved = null; 58 59 /** 60 * <p> 61 * Constructor. Creates a new CoverageCalculatorObserved for given collections of observed 62 * sequences and generated sequences. 63 * </p> 64 * 65 * @param observedSequences 66 * observed sequences in relation to which the coverage is calculated; must not be 67 * null 68 * @param sequences 69 * sequences for which the coverage is calculated; must not be null 70 * @param length 71 * length of the subsequences for which the coverage is analyzed; must be >0 72 * @throws InvalidParameterException 73 * thrown if observedSequences or sequences is null or length less than or equal to 74 * 0 75 */ 76 public CoverageCalculatorObserved(Collection<List<Event>> observedSequences, 77 Collection<List<Event>> sequences, 78 int length) 79 { 80 if (observedSequences == null) { 81 throw new InvalidParameterException("observed sequences must not be null"); 82 } 83 if (sequences == null) { 84 throw new InvalidParameterException("sequences must not be null"); 85 } 86 if (length <= 0) { 87 throw new InvalidParameterException("length must be >0; actual value: " + length); 88 } 89 this.observedSequences = observedSequences; 90 this.sequences = sequences; 91 this.length = length; 92 } 93 94 /** 95 * <p> 96 * Calculates the percentage of subsequences of length k that occur, with reference to those 97 * that were observed. 98 * </p> 99 * 100 * @return coverage percentage 101 */ 102 public double getCoverageObserved() { 103 createSubSeqs(); 104 Collection<List<Event>> subSeqsObservedCopy = 105 new LinkedHashSet<List<Event>>(subSeqsObserved); 106 subSeqsObservedCopy.retainAll(subSeqsGenerated); 107 return ((double) subSeqsObservedCopy.size()) / subSeqsObserved.size(); 108 } 109 110 /** 111 * <p> 112 * Calculates the weight of subsequences of length k that occur, with reference to those that 113 * were observed. 114 * </p> 115 * 116 * @param process 117 * stochastic process in reference to which the weight is calculated 118 * @return coverage percentage 119 */ 120 121 public double getCoverageObservedWeigth(IStochasticProcess process) { 122 createSubSeqs(); 123 Map<List<Event>, Double> weightMap = 124 SequenceTools.generateWeights(process, subSeqsObserved); 125 126 Collection<List<Event>> subSeqsObservedCopy = 127 new LinkedHashSet<List<Event>>(subSeqsObserved); 128 subSeqsObservedCopy.retainAll(subSeqsGenerated); 129 double weight = 0.0d; 130 for (List<Event> subSeq : subSeqsObservedCopy) { 131 weight += weightMap.get(subSeq); 132 } 133 return weight; 134 } 135 136 /** 137 * <p> 138 * Calculates the percentage of generated subsequences of length k that occur and have not been 139 * observed, with reference to all generated subsequences. 140 * </p> 141 * 142 * @return coverage percentage 143 */ 144 public double getNewPercentage() { 145 createSubSeqs(); 146 Collection<List<Event>> subSeqsGeneratedCopy = 147 new LinkedHashSet<List<Event>>(subSeqsGenerated); 148 subSeqsGeneratedCopy.removeAll(subSeqsObserved); 149 return ((double) subSeqsGeneratedCopy.size()) / subSeqsGenerated.size(); 150 } 151 152 /** 153 * <p> 154 * Calculates the percentage of generated subsequences of length k that occur and have not been 155 * observed, with references to all possible new subsequences. 156 * </p> 157 * 158 * @param process 159 * stochastic process which is used to determine which subsequences are possible 160 * @return coverage percentage 161 * @throws InvalidParameterException 162 * thrown if process is null 163 */ 164 public double getCoveragePossibleNew(IStochasticProcess process) { 165 if (process == null) { 166 throw new InvalidParameterException("process must not be null"); 167 } 168 createSubSeqs(); 169 Collection<List<Event>> subSeqsGeneratedCopy = 170 new LinkedHashSet<List<Event>>(subSeqsGenerated); 171 Collection<List<Event>> subSeqsPossible = process.generateSequences(length); 172 subSeqsGeneratedCopy.removeAll(subSeqsObserved); 173 subSeqsPossible.removeAll(subSeqsObserved); 174 int possibleSize = subSeqsPossible.size(); 175 subSeqsPossible.retainAll(subSeqsGeneratedCopy); 176 return ((double) subSeqsPossible.size()) / possibleSize; 177 } 178 179 /** 180 * <p> 181 * Calculates the weight of generated subsequences of length k that occur and have not been 182 * observed, with references to all possible new subsequences. 183 * </p> 184 * 185 * @param process 186 * stochastic process which is used to determine the weights and which subsequences 187 * are possible 188 * @return coverage percentage 189 * @throws InvalidParameterException 190 * thrown if process is null 191 */ 192 public double getCoveragePossibleNewWeight(IStochasticProcess process) { 193 if (process == null) { 194 throw new InvalidParameterException("process must not be null"); 195 } 196 createSubSeqs(); 197 Collection<List<Event>> subSeqsGeneratedCopy = 198 new LinkedHashSet<List<Event>>(subSeqsGenerated); 199 Collection<List<Event>> subSeqsPossible = process.generateSequences(length); 200 subSeqsGeneratedCopy.removeAll(subSeqsObserved); 201 subSeqsPossible.removeAll(subSeqsObserved); 202 Map<List<Event>, Double> weightMap = 203 SequenceTools.generateWeights(process, subSeqsPossible); 204 double weight = 0.0d; 205 for (List<Event> subSeq : subSeqsGeneratedCopy) { 206 Double currentWeight = weightMap.get(subSeq); 207 if (currentWeight != null) { 208 weight += currentWeight; 209 } 210 } 211 return weight; 212 } 213 214 /** 215 * <p> 216 * Returns the number of covered subsequences of length k. 217 * </p> 218 * 219 * @return number of covered subsequences 220 */ 221 public int getNumObserved() { 222 createSubSeqs(); 223 return subSeqsObserved.size(); 224 } 225 226 /** 227 * <p> 228 * Returns the number of covered subsequences of length k. 229 * </p> 230 * 231 * @return number of covered subsequences 232 */ 233 public int getNumCovered() { 234 createSubSeqs(); 235 return subSeqsGenerated.size(); 236 } 237 238 public int getNumNew() { 239 createSubSeqs(); 240 Collection<List<Event>> subSeqsGeneratedCopy = 241 new LinkedHashSet<List<Event>>(subSeqsGenerated); 242 subSeqsGeneratedCopy.removeAll(subSeqsObserved); 243 return subSeqsGeneratedCopy.size(); 244 } 245 246 /** 247 * <p> 248 * Helper function that calcuates the subsequences of length k that have been observed and 249 * generated. 250 * </p> 251 */ 252 private void createSubSeqs() { 253 if (subSeqsObserved == null) { 254 subSeqsObserved = SequenceTools.containedSubSequences(observedSequences, length); 255 } 256 if (subSeqsGenerated == null) { 257 subSeqsGenerated = SequenceTools.containedSubSequences(sequences, length); 258 } 259 } 272 260 } -
trunk/quest-core-coverage/src/main/java/de/ugoe/cs/quest/coverage/CoverageCalculatorProcess.java
r547 r559 1 1 2 package de.ugoe.cs.quest.coverage; 2 3 … … 11 12 /** 12 13 * <p> 13 * This class calculates various types of sequence coverage in relation to a 14 * stochastic process. 14 * This class calculates various types of sequence coverage in relation to a stochastic process. 15 15 * </p> 16 16 * … … 20 20 public class CoverageCalculatorProcess { 21 21 22 /** 23 * <p> 24 * Stochastic process that is the foundation for probabilistic coverages and 25 * coverages with reference to all possible sequences. 26 * </p> 27 */ 28 private final IStochasticProcess process; 29 30 /** 31 * <p> 32 * Sequences for which the coverage is calculated. 33 * </p> 34 */ 35 private Collection<List<Event>> sequences; 36 37 /** 38 * <p> 39 * Length of the subsequences in relation to which the coverage is 40 * calculated. 41 * </p> 42 */ 43 private final int length; 44 45 /** 46 * <p> 47 * All subsequences of {@link #length} of {@link #sequences}. 48 * </p> 49 */ 50 private Collection<List<Event>> containedSubSeqs = null; 51 52 /** 53 * <p> 54 * All subsequences of {@link #length} that can be generated by 55 * {@link #process}. 56 * </p> 57 */ 58 private Collection<List<Event>> allPossibleSubSeqs = null; 59 60 /** 61 * <p> 62 * The probabilities of all subsequences of {@link #length} according to 63 * {@link #process}. 64 * </p> 65 */ 66 private Map<List<Event>, Double> subSeqWeights = null; 67 68 /** 69 * <p> 70 * Constructor. Creates a new CoverageCalculatorProcess for a given 71 * stochastic process and generated sequences. 72 * </p> 73 * 74 * @param process 75 * stochastic process used for coverage calculations; must not be 76 * null 77 * @param sequences 78 * sequences for which the coverage is calculated; must not be 79 * null 80 * @param length 81 * length of the subsequences for which the coverage is analyzed; 82 * must be >0 83 * @throws InvalidParameterException 84 * thrown if process or sequences is null or length less than or equal to 0 85 */ 86 public CoverageCalculatorProcess(IStochasticProcess process, 87 Collection<List<Event>> sequences, int length) { 88 if (process == null) { 89 throw new InvalidParameterException("process must not be null"); 90 } 91 if (sequences == null) { 92 throw new InvalidParameterException("sequences must not be null"); 93 } 94 if (length <= 0) { 95 throw new InvalidParameterException( 96 "length must be >0; actual value: " + length); 97 } 98 this.process = process; 99 this.sequences = sequences; 100 this.length = length; 101 } 102 103 /** 104 * <p> 105 * Calculates the percentage of subsequences of length k that occur, 106 * including those that cannot be generated by {@link #process}. 107 * </p> 108 * 109 * @return coverage percentage 110 */ 111 public double getCoverageAllNoWeight() { 112 if (containedSubSeqs == null) { 113 containedSubSeqs = SequenceTools.containedSubSequences(sequences, 114 length); 115 } 116 return ((double) containedSubSeqs.size()) 117 / SequenceTools.numSequences(process, length); 118 } 119 120 /** 121 * <p> 122 * Calculates the percentage of subsequences of length k that occur and can 123 * generated by {@link #process}. 124 * </p> 125 * 126 * @return coverage percentage 127 */ 128 public double getCoveragePossibleNoWeight() { 129 if (containedSubSeqs == null) { 130 containedSubSeqs = SequenceTools.containedSubSequences(sequences, 131 length); 132 } 133 if (allPossibleSubSeqs == null) { 134 allPossibleSubSeqs = process.generateSequences(length); 135 } 136 return ((double) containedSubSeqs.size()) / allPossibleSubSeqs.size(); 137 } 138 139 /** 140 * <p> 141 * Calculates the weight of the subsequences that occur with relation to 142 * {@link #process}, i.e., the mass of the subsequence probability covered 143 * by the subsequences. 144 * </p> 145 * 146 * @return coverage weight 147 */ 148 public double getCoveragePossibleWeight() { 149 if (containedSubSeqs == null) { 150 containedSubSeqs = SequenceTools.containedSubSequences(sequences, 151 length); 152 } 153 if (allPossibleSubSeqs == null) { 154 allPossibleSubSeqs = process.generateSequences(length); 155 } 156 if (subSeqWeights == null) { 157 subSeqWeights = SequenceTools.generateWeights(process, 158 allPossibleSubSeqs); 159 } 160 double weight = 0.0; 161 for (List<Event> subSeq : containedSubSeqs) { 162 Double curWeight = subSeqWeights.get(subSeq); 163 if( curWeight!=null ) { 164 weight += curWeight; 165 } 166 } 167 return weight; 168 } 169 170 /** 171 * <p> 172 * Returns the number of covered subsequences of length k. 173 * </p> 174 * 175 * @return number of covered subsequences 176 */ 177 public int getNumCovered() { 178 if (containedSubSeqs == null) { 179 containedSubSeqs = SequenceTools.containedSubSequences(sequences, 180 length); 181 } 182 return containedSubSeqs.size(); 183 } 184 185 /** 186 * <p> 187 * Returns the number of possible subsequences of length k according to the 188 * stochastic process. 189 * </p> 190 * 191 * @return number of possible subsequences 192 */ 193 public int getNumPossible() { 194 if (allPossibleSubSeqs == null) { 195 allPossibleSubSeqs = process.generateSequences(length); 196 } 197 return allPossibleSubSeqs.size(); 198 } 199 200 /** 201 * <p> 202 * Sets a new collection of sequences for which the coverage is analyzed. 203 * </p> 204 * 205 * @param newSequences 206 * new collection of sequences 207 * @throws InvalidParameterException 208 * thrown is newSequences is null 209 */ 210 public void setSequences(Collection<List<Event>> newSequences) { 211 if (newSequences == null) { 212 throw new InvalidParameterException("sequences must not be null"); 213 } 214 this.sequences = newSequences; 215 containedSubSeqs = null; 216 } 22 /** 23 * <p> 24 * Stochastic process that is the foundation for probabilistic coverages and coverages with 25 * reference to all possible sequences. 26 * </p> 27 */ 28 private final IStochasticProcess process; 29 30 /** 31 * <p> 32 * Sequences for which the coverage is calculated. 33 * </p> 34 */ 35 private Collection<List<Event>> sequences; 36 37 /** 38 * <p> 39 * Length of the subsequences in relation to which the coverage is calculated. 40 * </p> 41 */ 42 private final int length; 43 44 /** 45 * <p> 46 * All subsequences of {@link #length} of {@link #sequences}. 47 * </p> 48 */ 49 private Collection<List<Event>> containedSubSeqs = null; 50 51 /** 52 * <p> 53 * All subsequences of {@link #length} that can be generated by {@link #process}. 54 * </p> 55 */ 56 private Collection<List<Event>> allPossibleSubSeqs = null; 57 58 /** 59 * <p> 60 * The probabilities of all subsequences of {@link #length} according to {@link #process}. 61 * </p> 62 */ 63 private Map<List<Event>, Double> subSeqWeights = null; 64 65 /** 66 * <p> 67 * Constructor. Creates a new CoverageCalculatorProcess for a given stochastic process and 68 * generated sequences. 69 * </p> 70 * 71 * @param process 72 * stochastic process used for coverage calculations; must not be null 73 * @param sequences 74 * sequences for which the coverage is calculated; must not be null 75 * @param length 76 * length of the subsequences for which the coverage is analyzed; must be >0 77 * @throws InvalidParameterException 78 * thrown if process or sequences is null or length less than or equal to 0 79 */ 80 public CoverageCalculatorProcess(IStochasticProcess process, 81 Collection<List<Event>> sequences, 82 int length) 83 { 84 if (process == null) { 85 throw new InvalidParameterException("process must not be null"); 86 } 87 if (sequences == null) { 88 throw new InvalidParameterException("sequences must not be null"); 89 } 90 if (length <= 0) { 91 throw new InvalidParameterException("length must be >0; actual value: " + length); 92 } 93 this.process = process; 94 this.sequences = sequences; 95 this.length = length; 96 } 97 98 /** 99 * <p> 100 * Calculates the percentage of subsequences of length k that occur, including those that cannot 101 * be generated by {@link #process}. 102 * </p> 103 * 104 * @return coverage percentage 105 */ 106 public double getCoverageAllNoWeight() { 107 if (containedSubSeqs == null) { 108 containedSubSeqs = SequenceTools.containedSubSequences(sequences, length); 109 } 110 return ((double) containedSubSeqs.size()) / SequenceTools.numSequences(process, length); 111 } 112 113 /** 114 * <p> 115 * Calculates the percentage of subsequences of length k that occur and can generated by 116 * {@link #process}. 117 * </p> 118 * 119 * @return coverage percentage 120 */ 121 public double getCoveragePossibleNoWeight() { 122 if (containedSubSeqs == null) { 123 containedSubSeqs = SequenceTools.containedSubSequences(sequences, length); 124 } 125 if (allPossibleSubSeqs == null) { 126 allPossibleSubSeqs = process.generateSequences(length); 127 } 128 return ((double) containedSubSeqs.size()) / allPossibleSubSeqs.size(); 129 } 130 131 /** 132 * <p> 133 * Calculates the weight of the subsequences that occur with relation to {@link #process}, i.e., 134 * the mass of the subsequence probability covered by the subsequences. 135 * </p> 136 * 137 * @return coverage weight 138 */ 139 public double getCoveragePossibleWeight() { 140 if (containedSubSeqs == null) { 141 containedSubSeqs = SequenceTools.containedSubSequences(sequences, length); 142 } 143 if (allPossibleSubSeqs == null) { 144 allPossibleSubSeqs = process.generateSequences(length); 145 } 146 if (subSeqWeights == null) { 147 subSeqWeights = SequenceTools.generateWeights(process, allPossibleSubSeqs); 148 } 149 double weight = 0.0; 150 for (List<Event> subSeq : containedSubSeqs) { 151 Double curWeight = subSeqWeights.get(subSeq); 152 if (curWeight != null) { 153 weight += curWeight; 154 } 155 } 156 return weight; 157 } 158 159 /** 160 * <p> 161 * Returns the number of covered subsequences of length k. 162 * </p> 163 * 164 * @return number of covered subsequences 165 */ 166 public int getNumCovered() { 167 if (containedSubSeqs == null) { 168 containedSubSeqs = SequenceTools.containedSubSequences(sequences, length); 169 } 170 return containedSubSeqs.size(); 171 } 172 173 /** 174 * <p> 175 * Returns the number of possible subsequences of length k according to the stochastic process. 176 * </p> 177 * 178 * @return number of possible subsequences 179 */ 180 public int getNumPossible() { 181 if (allPossibleSubSeqs == null) { 182 allPossibleSubSeqs = process.generateSequences(length); 183 } 184 return allPossibleSubSeqs.size(); 185 } 186 187 /** 188 * <p> 189 * Sets a new collection of sequences for which the coverage is analyzed. 190 * </p> 191 * 192 * @param newSequences 193 * new collection of sequences 194 * @throws InvalidParameterException 195 * thrown is newSequences is null 196 */ 197 public void setSequences(Collection<List<Event>> newSequences) { 198 if (newSequences == null) { 199 throw new InvalidParameterException("sequences must not be null"); 200 } 201 this.sequences = newSequences; 202 containedSubSeqs = null; 203 } 217 204 218 205 } -
trunk/quest-core-coverage/src/main/java/de/ugoe/cs/quest/coverage/SequenceTools.java
r547 r559 1 1 2 package de.ugoe.cs.quest.coverage; 2 3 … … 22 23 */ 23 24 public class SequenceTools { 24 25 /**26 * <p>27 * Private constructor to prevent initializing of the class.28 * </p>29 */30 private SequenceTools() {31 32 }33 25 34 /** 35 * <p> 36 * Calculates the weights for all sequences passed to this function as 37 * defined by the stochastic process and stores them in a {@link Map}. The 38 * weights are normalized, i.e., the sum of all weights contained in this 39 * map is 1. 40 * </p> 41 * 42 * @param process 43 * process used for weight calculation 44 * @param sequences 45 * sequences for which the weights are calculated 46 * @return {@link Map} of weights 47 */ 48 public static Map<List<Event>, Double> generateWeights( 49 IStochasticProcess process, 50 Collection<List<Event>> sequences) { 51 Map<List<Event>, Double> subSeqWeights = new LinkedHashMap<List<Event>, Double>(); 52 if (sequences != null && !sequences.isEmpty()) { 53 if (process != null) { 54 double sum = 0.0; 55 for (List<Event> sequence : sequences) { 56 double prob = process.getProbability(sequence); 57 subSeqWeights.put(sequence, prob); 58 sum += prob; 59 } 60 if (sum < 1.0) { 61 for (Map.Entry<List<Event>, Double> entry : subSeqWeights 62 .entrySet()) { 63 entry.setValue(entry.getValue() / sum); 64 } 65 } 66 } else { 67 for( List<Event> sequence : sequences ) { 68 subSeqWeights.put(sequence, 0.0d); 69 } 70 } 71 } 72 return subSeqWeights; 73 } 26 /** 27 * <p> 28 * Private constructor to prevent initializing of the class. 29 * </p> 30 */ 31 private SequenceTools() { 74 32 75 /** 76 * <p> 77 * Calculates the number of all existing sequences of a given length, 78 * regardless whether they are possible or not. 79 * </p> 80 * 81 * @param process 82 * stochastic process whose symbols are the basis for this 83 * calculation 84 * @param length 85 * lenght of the sequences 86 * @return numStates^length 87 * @throws InvalidParameterException 88 * thrown if length less or equal to 0 89 */ 90 public static long numSequences(IStochasticProcess process, int length) { 91 if (length <= 0) { 92 throw new InvalidParameterException( 93 "length must be a positive integer"); 94 } 95 long result = 0; 96 if (process != null) { 97 result = (long) Math.pow(process.getNumSymbols(), length); 98 } 99 return result; 100 } 33 } 101 34 102 /** 103 * <p> 104 * Creates a {@link Set} of all subsequences of a given length that are 105 * contained in a sequence collection. 106 * </p> 107 * 108 * @param sequences 109 * sequences from which the subsequences are extracted 110 * @param length 111 * length of the subsequences 112 * @return {@link Set} of all subsequences 113 * @throws InvalidParameterException 114 * thrown if length less or equal to 0 115 */ 116 public static Set<List<Event>> containedSubSequences( 117 Collection<List<Event>> sequences, int length) { 118 if (length <= 0) { 119 throw new InvalidParameterException( 120 "length must be a positive integer"); 121 } 122 Set<List<Event>> containedSubSeqs = new LinkedHashSet<List<Event>>(); 123 if (sequences != null) { 124 for (List<Event> sequence : sequences) { 125 List<Event> subSeq = new LinkedList<Event>(); 126 boolean minLengthReached = false; 127 for (Event event : sequence) { 128 subSeq.add(event); 129 if (!minLengthReached) { 130 if (subSeq.size() == length) { 131 minLengthReached = true; 132 } 133 } else { 134 subSeq.remove(0); 135 } 136 if (minLengthReached) { 137 if (!containedSubSeqs.contains(subSeq)) { 138 containedSubSeqs.add(new LinkedList<Event>( 139 subSeq)); 140 } 141 } 142 } 143 } 144 } 145 return containedSubSeqs; 146 } 35 /** 36 * <p> 37 * Calculates the weights for all sequences passed to this function as defined by the stochastic 38 * process and stores them in a {@link Map}. The weights are normalized, i.e., the sum of all 39 * weights contained in this map is 1. 40 * </p> 41 * 42 * @param process 43 * process used for weight calculation 44 * @param sequences 45 * sequences for which the weights are calculated 46 * @return {@link Map} of weights 47 */ 48 public static Map<List<Event>, Double> generateWeights(IStochasticProcess process, 49 Collection<List<Event>> sequences) 50 { 51 Map<List<Event>, Double> subSeqWeights = new LinkedHashMap<List<Event>, Double>(); 52 if (sequences != null && !sequences.isEmpty()) { 53 if (process != null) { 54 double sum = 0.0; 55 for (List<Event> sequence : sequences) { 56 double prob = process.getProbability(sequence); 57 subSeqWeights.put(sequence, prob); 58 sum += prob; 59 } 60 if (sum < 1.0) { 61 for (Map.Entry<List<Event>, Double> entry : subSeqWeights.entrySet()) { 62 entry.setValue(entry.getValue() / sum); 63 } 64 } 65 } 66 else { 67 for (List<Event> sequence : sequences) { 68 subSeqWeights.put(sequence, 0.0d); 69 } 70 } 71 } 72 return subSeqWeights; 73 } 74 75 /** 76 * <p> 77 * Calculates the number of all existing sequences of a given length, regardless whether they 78 * are possible or not. 79 * </p> 80 * 81 * @param process 82 * stochastic process whose symbols are the basis for this calculation 83 * @param length 84 * lenght of the sequences 85 * @return numStates^length 86 * @throws InvalidParameterException 87 * thrown if length less or equal to 0 88 */ 89 public static long numSequences(IStochasticProcess process, int length) { 90 if (length <= 0) { 91 throw new InvalidParameterException("length must be a positive integer"); 92 } 93 long result = 0; 94 if (process != null) { 95 result = (long) Math.pow(process.getNumSymbols(), length); 96 } 97 return result; 98 } 99 100 /** 101 * <p> 102 * Creates a {@link Set} of all subsequences of a given length that are contained in a sequence 103 * collection. 104 * </p> 105 * 106 * @param sequences 107 * sequences from which the subsequences are extracted 108 * @param length 109 * length of the subsequences 110 * @return {@link Set} of all subsequences 111 * @throws InvalidParameterException 112 * thrown if length less or equal to 0 113 */ 114 public static Set<List<Event>> containedSubSequences(Collection<List<Event>> sequences, 115 int length) 116 { 117 if (length <= 0) { 118 throw new InvalidParameterException("length must be a positive integer"); 119 } 120 Set<List<Event>> containedSubSeqs = new LinkedHashSet<List<Event>>(); 121 if (sequences != null) { 122 for (List<Event> sequence : sequences) { 123 List<Event> subSeq = new LinkedList<Event>(); 124 boolean minLengthReached = false; 125 for (Event event : sequence) { 126 subSeq.add(event); 127 if (!minLengthReached) { 128 if (subSeq.size() == length) { 129 minLengthReached = true; 130 } 131 } 132 else { 133 subSeq.remove(0); 134 } 135 if (minLengthReached) { 136 if (!containedSubSeqs.contains(subSeq)) { 137 containedSubSeqs.add(new LinkedList<Event>(subSeq)); 138 } 139 } 140 } 141 } 142 } 143 return containedSubSeqs; 144 } 147 145 }
Note: See TracChangeset
for help on using the changeset viewer.