-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix NPE during completeness calculation; refactor completeness calcul…
…ation
- Loading branch information
Showing
6 changed files
with
116 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
abecto-core/src/main/java/de/uni_jena/cs/fusion/abecto/measure/Completeness.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/*- | ||
* Copyright © 2019-2022 Heinz Nixdorf Chair for Distributed Information Systems, | ||
* Friedrich Schiller University Jena (http://www.fusion.uni-jena.de/) | ||
* Copyright © 2023-2024 Jan Martin Keil ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
-*/ | ||
|
||
package de.uni_jena.cs.fusion.abecto.measure; | ||
|
||
import de.uni_jena.cs.fusion.abecto.Aspect; | ||
import de.uni_jena.cs.fusion.abecto.Metadata; | ||
import de.uni_jena.cs.fusion.abecto.ResourcePair; | ||
import de.uni_jena.cs.fusion.abecto.vocabulary.AV; | ||
import de.uni_jena.cs.fusion.abecto.vocabulary.OM; | ||
import org.apache.jena.rdf.model.Model; | ||
import org.apache.jena.rdf.model.Resource; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.util.*; | ||
|
||
public class Completeness extends Ratio<Resource> { | ||
|
||
public Completeness() { | ||
super(AV.marCompletenessThomas08, OM.one); | ||
} | ||
|
||
public static Completeness calculate(SymmetricPerDatasetPairCount absoluteCoverage, PerDatasetCount deduplicatedCount) { | ||
Set<ResourcePair> datasetPairs = getDatasetPairsWithSufficientData(absoluteCoverage, deduplicatedCount); | ||
long totalPairwiseOverlap = calculateTotalPairwiseOverlap(datasetPairs, absoluteCoverage); | ||
if (totalPairwiseOverlap != 0) { | ||
BigDecimal estimatedPopulationSize = calculateEstimatedPopulationSize(datasetPairs, deduplicatedCount, totalPairwiseOverlap); | ||
Set<Resource> datasets = ResourcePair.getResourcesOfPairs(datasetPairs); | ||
return calculateCompleteness(datasets, deduplicatedCount, estimatedPopulationSize); | ||
} | ||
return new Completeness(); // empty | ||
} | ||
|
||
private static Set<ResourcePair> getDatasetPairsWithSufficientData(SymmetricPerDatasetPairCount absoluteCoverage, PerDatasetCount deduplicatedCount) { | ||
Set<ResourcePair> datasetPairs = new HashSet<>(absoluteCoverage.keySet()); | ||
Set<Resource> datasetsWithDeduplicatedCount = deduplicatedCount.keySet(); | ||
datasetPairs.removeIf(pair -> notBothContainedIn(pair, datasetsWithDeduplicatedCount)); | ||
return datasetPairs; | ||
} | ||
|
||
private static boolean notBothContainedIn(ResourcePair pair, Collection<Resource> collection) { | ||
return !collection.contains(pair.first) || !collection.contains(pair.second); | ||
} | ||
|
||
private static long calculateTotalPairwiseOverlap(Iterable<ResourcePair> datasetPairs, SymmetricPerDatasetPairCount absoluteCoverage) { | ||
long totalPairwiseOverlap = 0L; | ||
for (ResourcePair datasetPair : datasetPairs) { | ||
if (absoluteCoverage.contains(datasetPair)) { | ||
totalPairwiseOverlap += absoluteCoverage.get(datasetPair); | ||
} | ||
} | ||
return totalPairwiseOverlap; | ||
} | ||
|
||
private static BigDecimal calculateEstimatedPopulationSize(Iterable<ResourcePair> datasetPairs, PerDatasetCount deduplicatedCount, long totalPairwiseOverlap) { | ||
BigDecimal estimatedPopulationSize = BigDecimal.ZERO; | ||
for (ResourcePair datasetPair : datasetPairs) { | ||
BigDecimal deduplicatedCount1 = BigDecimal.valueOf(deduplicatedCount.get(datasetPair.first)); | ||
BigDecimal deduplicatedCount2 = BigDecimal.valueOf(deduplicatedCount.get(datasetPair.second)); | ||
estimatedPopulationSize = estimatedPopulationSize.add(deduplicatedCount1.multiply(deduplicatedCount2)); | ||
} | ||
estimatedPopulationSize = estimatedPopulationSize.divide(BigDecimal.valueOf(totalPairwiseOverlap), SCALE, | ||
RoundingMode.HALF_UP); | ||
return estimatedPopulationSize; | ||
} | ||
|
||
private static Completeness calculateCompleteness(Iterable<Resource> datasets, PerDatasetCount deduplicatedCount, BigDecimal estimatedPopulationSize) { | ||
Completeness completeness = new Completeness(); | ||
for (Resource dataset : datasets) { | ||
BigDecimal numerator = BigDecimal.valueOf(deduplicatedCount.get(dataset)); | ||
BigDecimal completenessOfDataset = numerator.divide(estimatedPopulationSize, SCALE, ROUNDING_MODE); | ||
completeness.set(dataset, completenessOfDataset); | ||
} | ||
return completeness; | ||
} | ||
|
||
public void storeInModel(Aspect aspect, Map<Resource, Model> outputModelsMap) { | ||
for (Resource dataset : values.keySet()) { | ||
Collection<Resource> otherDatasets = new HashSet<>(values.keySet()); | ||
otherDatasets.remove(dataset); | ||
Metadata.addQualityMeasurement(quantity, get(dataset), unit, dataset, variable, | ||
otherDatasets, aspect.getIri(), outputModelsMap.get(dataset)); | ||
} | ||
} | ||
} |
54 changes: 0 additions & 54 deletions
54
abecto-core/src/main/java/de/uni_jena/cs/fusion/abecto/measure/PerDatasetRatio.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters