Ignore:
Timestamp:
08/17/12 09:05:19 (12 years ago)
Author:
sherbold
Message:
  • adapted to quest coding style
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/quest-core-coverage/src/main/java/de/ugoe/cs/quest/coverage/CoverageCalculatorObserved.java

    r547 r559  
     1 
    12package de.ugoe.cs.quest.coverage; 
    23 
     
    1213/** 
    1314 * <p> 
    14  * This class calculates various types of sequence coverage in relation to a 
    15  * collection of observed sequences. 
     15 * This class calculates various types of sequence coverage in relation to a collection of observed 
     16 * sequences. 
    1617 * </p> 
    1718 *  
     
    2122public class CoverageCalculatorObserved { 
    2223 
    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    } 
    272260} 
Note: See TracChangeset for help on using the changeset viewer.