source: trunk/quest-core-coverage/src/main/java/de/ugoe/cs/quest/coverage/CoverageCalculatorObserved.java @ 655

Last change on this file since 655 was 655, checked in by pharms, 12 years ago
  • removed old copyright file header
  • Property svn:mime-type set to text/plain
File size: 8.6 KB
Line 
1package de.ugoe.cs.quest.coverage;
2
3import java.security.InvalidParameterException;
4import java.util.Collection;
5import java.util.LinkedHashSet;
6import java.util.List;
7import java.util.Map;
8
9import de.ugoe.cs.quest.eventcore.Event;
10import de.ugoe.cs.quest.usageprofiles.IStochasticProcess;
11
12/**
13 * <p>
14 * This class calculates various types of sequence coverage in relation to a collection of observed
15 * sequences.
16 * </p>
17 *
18 * @author Steffen Herbold
19 * @version 1.0
20 */
21public class CoverageCalculatorObserved {
22
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 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>> subSeqsGenerated = null;
50
51    /**
52     * <p>
53     * All subsequences of {@link #length} of {@link #observedSequences}.
54     * </p>
55     */
56    private Collection<List<Event>> subSeqsObserved = null;
57
58    /**
59     * <p>
60     * Constructor. Creates a new CoverageCalculatorObserved for given collections of observed
61     * sequences and generated sequences.
62     * </p>
63     *
64     * @param observedSequences
65     *            observed sequences in relation to which the coverage is calculated; must not be
66     *            null
67     * @param sequences
68     *            sequences for which the coverage is calculated; must not be null
69     * @param length
70     *            length of the subsequences for which the coverage is analyzed; must be >0
71     * @throws InvalidParameterException
72     *             thrown if observedSequences or sequences is null or length less than or equal to
73     *             0
74     */
75    public CoverageCalculatorObserved(Collection<List<Event>> observedSequences,
76                                      Collection<List<Event>> sequences,
77                                      int length)
78    {
79        if (observedSequences == null) {
80            throw new InvalidParameterException("observed sequences must not be null");
81        }
82        if (sequences == null) {
83            throw new InvalidParameterException("sequences must not be null");
84        }
85        if (length <= 0) {
86            throw new InvalidParameterException("length must be >0; actual value: " + length);
87        }
88        this.observedSequences = observedSequences;
89        this.sequences = sequences;
90        this.length = length;
91    }
92
93    /**
94     * <p>
95     * Calculates the percentage of subsequences of length k that occur, with reference to those
96     * that were observed.
97     * </p>
98     *
99     * @return coverage percentage
100     */
101    public double getCoverageObserved() {
102        createSubSeqs();
103        Collection<List<Event>> subSeqsObservedCopy =
104            new LinkedHashSet<List<Event>>(subSeqsObserved);
105        subSeqsObservedCopy.retainAll(subSeqsGenerated);
106        return ((double) subSeqsObservedCopy.size()) / subSeqsObserved.size();
107    }
108
109    /**
110     * <p>
111     * Calculates the weight of subsequences of length k that occur, with reference to those that
112     * were observed.
113     * </p>
114     *
115     * @param process
116     *            stochastic process in reference to which the weight is calculated
117     * @return coverage percentage
118     */
119
120    public double getCoverageObservedWeigth(IStochasticProcess process) {
121        createSubSeqs();
122        Map<List<Event>, Double> weightMap =
123            SequenceTools.generateWeights(process, subSeqsObserved);
124
125        Collection<List<Event>> subSeqsObservedCopy =
126            new LinkedHashSet<List<Event>>(subSeqsObserved);
127        subSeqsObservedCopy.retainAll(subSeqsGenerated);
128        double weight = 0.0d;
129        for (List<Event> subSeq : subSeqsObservedCopy) {
130            weight += weightMap.get(subSeq);
131        }
132        return weight;
133    }
134
135    /**
136     * <p>
137     * Calculates the percentage of generated subsequences of length k that occur and have not been
138     * observed, with reference to all generated subsequences.
139     * </p>
140     *
141     * @return coverage percentage
142     */
143    public double getNewPercentage() {
144        createSubSeqs();
145        Collection<List<Event>> subSeqsGeneratedCopy =
146            new LinkedHashSet<List<Event>>(subSeqsGenerated);
147        subSeqsGeneratedCopy.removeAll(subSeqsObserved);
148        return ((double) subSeqsGeneratedCopy.size()) / subSeqsGenerated.size();
149    }
150
151    /**
152     * <p>
153     * Calculates the percentage of generated subsequences of length k that occur and have not been
154     * observed, with references to all possible new subsequences.
155     * </p>
156     *
157     * @param process
158     *            stochastic process which is used to determine which subsequences are possible
159     * @return coverage percentage
160     * @throws InvalidParameterException
161     *             thrown if process is null
162     */
163    public double getCoveragePossibleNew(IStochasticProcess process) {
164        if (process == null) {
165            throw new InvalidParameterException("process must not be null");
166        }
167        createSubSeqs();
168        Collection<List<Event>> subSeqsGeneratedCopy =
169            new LinkedHashSet<List<Event>>(subSeqsGenerated);
170        Collection<List<Event>> subSeqsPossible = process.generateSequences(length);
171        subSeqsGeneratedCopy.removeAll(subSeqsObserved);
172        subSeqsPossible.removeAll(subSeqsObserved);
173        int possibleSize = subSeqsPossible.size();
174        subSeqsPossible.retainAll(subSeqsGeneratedCopy);
175        return ((double) subSeqsPossible.size()) / possibleSize;
176    }
177
178    /**
179     * <p>
180     * Calculates the weight of generated subsequences of length k that occur and have not been
181     * observed, with references to all possible new subsequences.
182     * </p>
183     *
184     * @param process
185     *            stochastic process which is used to determine the weights and which subsequences
186     *            are possible
187     * @return coverage percentage
188     * @throws InvalidParameterException
189     *             thrown if process is null
190     */
191    public double getCoveragePossibleNewWeight(IStochasticProcess process) {
192        if (process == null) {
193            throw new InvalidParameterException("process must not be null");
194        }
195        createSubSeqs();
196        Collection<List<Event>> subSeqsGeneratedCopy =
197            new LinkedHashSet<List<Event>>(subSeqsGenerated);
198        Collection<List<Event>> subSeqsPossible = process.generateSequences(length);
199        subSeqsGeneratedCopy.removeAll(subSeqsObserved);
200        subSeqsPossible.removeAll(subSeqsObserved);
201        Map<List<Event>, Double> weightMap =
202            SequenceTools.generateWeights(process, subSeqsPossible);
203        double weight = 0.0d;
204        for (List<Event> subSeq : subSeqsGeneratedCopy) {
205            Double currentWeight = weightMap.get(subSeq);
206            if (currentWeight != null) {
207                weight += currentWeight;
208            }
209        }
210        return weight;
211    }
212
213    /**
214     * <p>
215     * Returns the number of covered subsequences of length k.
216     * </p>
217     *
218     * @return number of covered subsequences
219     */
220    public int getNumObserved() {
221        createSubSeqs();
222        return subSeqsObserved.size();
223    }
224
225    /**
226     * <p>
227     * Returns the number of covered subsequences of length k.
228     * </p>
229     *
230     * @return number of covered subsequences
231     */
232    public int getNumCovered() {
233        createSubSeqs();
234        return subSeqsGenerated.size();
235    }
236
237    public int getNumNew() {
238        createSubSeqs();
239        Collection<List<Event>> subSeqsGeneratedCopy =
240            new LinkedHashSet<List<Event>>(subSeqsGenerated);
241        subSeqsGeneratedCopy.removeAll(subSeqsObserved);
242        return subSeqsGeneratedCopy.size();
243    }
244
245    /**
246     * <p>
247     * Helper function that calcuates the subsequences of length k that have been observed and
248     * generated.
249     * </p>
250     */
251    private void createSubSeqs() {
252        if (subSeqsObserved == null) {
253            subSeqsObserved = SequenceTools.containedSubSequences(observedSequences, length);
254        }
255        if (subSeqsGenerated == null) {
256            subSeqsGenerated = SequenceTools.containedSubSequences(sequences, length);
257        }
258    }
259}
Note: See TracBrowser for help on using the repository browser.