use of eu.etaxonomy.cdm.model.description.QuantitativeData in project cdmlib by cybertaxonomy.
the class SDDDocumentBuilder method buildSummaryData.
/**
* Builds SummaryData associated with a CodedDescription
*/
public void buildSummaryData(ElementImpl element, TaxonDescription taxonDescription) throws ParseException {
// <SummaryData>
// <Categorical ref="c4">
// <State ref="s3"/>
// <State ref="s4"/>
// </Categorical>
ElementImpl summaryData = new ElementImpl(document, SUMMARY_DATA);
Set<DescriptionElementBase> elements = taxonDescription.getElements();
for (Iterator<DescriptionElementBase> deb = elements.iterator(); deb.hasNext(); ) {
DescriptionElementBase descriptionElement = deb.next();
if (descriptionElement instanceof CategoricalData) {
CategoricalData categoricalData = (CategoricalData) descriptionElement;
buildCategorical(summaryData, categoricalData);
}
if (descriptionElement instanceof QuantitativeData) {
QuantitativeData quantitativeData = (QuantitativeData) descriptionElement;
buildQuantitative(summaryData, quantitativeData);
}
if (descriptionElement instanceof TextData) {
TextData textData = (TextData) descriptionElement;
buildTextChar(summaryData, textData);
}
}
element.appendChild(summaryData);
}
use of eu.etaxonomy.cdm.model.description.QuantitativeData in project cdmlib by cybertaxonomy.
the class PolytomousKeyGenerator method getAllBranchesTaxa.
private Set<KeyTaxon> getAllBranchesTaxa(List<Feature> featuresLeft, Set<KeyTaxon> taxaCovered, Map<Feature, Set<State>> featureStatesFilter) {
Set<KeyTaxon> candidates = new HashSet<>(taxaCovered);
List<Feature> dependendFeatures = new ArrayList<>();
for (Feature feature : featuresLeft) {
if (feature.isSupportsCategoricalData()) {
Set<State> allStates = getAllStates(feature, taxaCovered, featureStatesFilter.get(feature));
Iterator<KeyTaxon> it = candidates.iterator();
while (it.hasNext()) {
Set<KeyTaxon> taxonSet = new HashSet<>(Arrays.asList(it.next()));
Set<State> taxonStates = getAllStates(feature, taxonSet, featureStatesFilter.get(feature));
if (allStates.size() > taxonStates.size()) {
it.remove();
}
}
if (candidates.isEmpty()) {
break;
} else {
addDependentFeatures(dependendFeatures, feature, new HashSet<>(), new ArrayList<>(allStates));
}
} else if (feature.isSupportsQuantitativeData()) {
Iterator<KeyTaxon> it = candidates.iterator();
while (it.hasNext()) {
BigDecimal min = BigDecimalUtil.MAX_BIGDECIMAL;
BigDecimal max = BigDecimalUtil.MIN_BIGDECIMAL;
Set<QuantitativeData> qds = it.next().quantitativeData.get(feature);
qds = qds == null ? new HashSet<>() : qds;
for (QuantitativeData qd : qds) {
BigDecimal qdMin = qd.getOverallMin();
if (qdMin != null) {
min = min.min(qdMin);
}
BigDecimal qdMax = qd.getOverallMax();
if (qdMax != null) {
max = max.max(qdMax);
}
}
boolean staysCandidate = true;
for (KeyTaxon taxon : taxaCovered) {
Set<QuantitativeData> tqds = taxon.quantitativeData.get(feature);
tqds = tqds == null ? new HashSet<>() : tqds;
for (QuantitativeData qd : tqds) {
staysCandidate &= qd.getOverallMin() == null || qd.getOverallMin().compareTo(min) >= 0;
staysCandidate &= qd.getOverallMax() == null || qd.getOverallMax().compareTo(max) <= 0;
}
if (!staysCandidate) {
break;
}
}
if (!staysCandidate) {
it.remove();
}
}
}
}
if (config.isUseDependencies() && !dependendFeatures.isEmpty() && !candidates.isEmpty()) {
Set<KeyTaxon> dependetCandidates = getAllBranchesTaxa(dependendFeatures, taxaCovered, featureStatesFilter);
candidates.retainAll(dependetCandidates);
}
return candidates;
}
use of eu.etaxonomy.cdm.model.description.QuantitativeData in project cdmlib by cybertaxonomy.
the class PolytomousKeyGenerator method quantitativeFeatureScore.
/**
* This function returns the score of a quantitative feature.
*/
private float quantitativeFeatureScore(Feature feature, Set<KeyTaxon> coveredTaxa, Map<Feature, BigDecimal> quantitativeFeaturesThresholds) {
List<BigDecimal> allValues = new ArrayList<>();
for (KeyTaxon td : coveredTaxa) {
for (QuantitativeData qd : td.getQuantitativeData(feature)) {
computeAllValues(allValues, qd);
}
}
int i, j;
BigDecimal threshold = BigDecimal.ZERO;
BigDecimal bestThreshold = BigDecimal.ZERO;
int difference = allValues.size();
int differenceMin = difference;
int bestTaxaBefore = 0;
int bestTaxaAfter = 0;
for (i = 0; i < allValues.size() / 2; i++) {
threshold = allValues.get(i * 2 + 1);
int taxaBefore = 0;
int taxaAfter = 0;
for (j = 0; j < allValues.size() / 2; j++) {
if (allValues.get(j * 2 + 1).compareTo(threshold) <= 0) {
taxaBefore++;
}
if (allValues.get(j * 2).compareTo(threshold) > 0) {
taxaAfter++;
}
}
difference = Math.abs(taxaBefore - taxaAfter);
if (difference < differenceMin) {
differenceMin = difference;
bestThreshold = threshold;
bestTaxaBefore = taxaBefore;
bestTaxaAfter = taxaAfter;
}
}
quantitativeFeaturesThresholds.put(feature, bestThreshold);
int defaultQuantitativeScore = 0;
for (i = 0; i < bestTaxaBefore; i++) {
defaultQuantitativeScore += bestTaxaAfter;
}
return defaultQuantitativeScore;
}
use of eu.etaxonomy.cdm.model.description.QuantitativeData in project cdmlib by cybertaxonomy.
the class StructuredDescriptionAggregation method aggregateWithinQuantitativeData.
/**
* Evaluates statistics for exact values collection and handles missing min and max values
*/
private QuantitativeData aggregateWithinQuantitativeData(QuantitativeData sourceQd) {
QuantitativeData aggQD = QuantitativeData.NewInstance(sourceQd.getFeature());
aggQD.setUnit(sourceQd.getUnit());
Set<BigDecimal> exactValues = sourceQd.getExactValues();
if (!exactValues.isEmpty()) {
// qd is not already aggregated
Comparator<BigDecimal> comp = Comparator.naturalOrder();
int exactValueSampleSize = exactValues.size();
BigDecimal exactValueMin = exactValues.stream().min(comp).get();
BigDecimal exactValueMax = exactValues.stream().max(comp).get();
BigDecimal exactValueAvg = BigDecimalUtil.average(exactValues);
// TODO also check for typical boundary data
if (sourceQd.getMin() == null && sourceQd.getMax() == null) {
aggQD.setSampleSize(new BigDecimal(exactValueSampleSize), null);
aggQD.setAverage(exactValueAvg, null);
}
aggQD.setMinimum(sourceQd.getMin() == null ? exactValueMin : sourceQd.getMin().min(exactValueMin), null);
aggQD.setMaximum(sourceQd.getMax() == null ? exactValueMax : sourceQd.getMax().max(exactValueMax), null);
} else {
// qd has only min, max, ... but no exact values
aggQD = sourceQd.clone();
aggQD = handleMissingValues(aggQD);
}
return aggQD;
}
use of eu.etaxonomy.cdm.model.description.QuantitativeData in project cdmlib by cybertaxonomy.
the class DescriptionBaseDto method fromDescription.
public static DescriptionBaseDto fromDescription(DescriptionBase desc) {
UuidAndTitleCache<Taxon> taxonUuidAndTitleCache = null;
UuidAndTitleCache<SpecimenOrObservationBase> specimenDto = null;
UuidAndTitleCache<TaxonName> nameUuidAndTitleCache = null;
if (desc instanceof TaxonDescription) {
Taxon taxon = HibernateProxyHelper.deproxy(((TaxonDescription) desc).getTaxon(), Taxon.class);
if (taxon != null) {
taxonUuidAndTitleCache = new UuidAndTitleCache<Taxon>(taxon.getUuid(), taxon.getId(), taxon.getTitleCache());
}
}
if (desc instanceof SpecimenDescription) {
SpecimenDescription specimenDesc = HibernateProxyHelper.deproxy(desc, SpecimenDescription.class);
SpecimenOrObservationBase specimen = specimenDesc.getDescribedSpecimenOrObservation();
specimenDto = new UuidAndTitleCache<>(specimen.getUuid(), specimen.getId(), specimen.getTitleCache());
// if (specimen != null){
// if (specimen instanceof FieldUnit){
// specimenDto = FieldUnitDTO.fromEntity((FieldUnit)specimen);
// }else{
// specimenDto = DerivedUnitDTO.fromEntity((DerivedUnit)specimen);
// }
// }
}
if (desc instanceof TaxonNameDescription) {
TaxonNameDescription nameDesc = HibernateProxyHelper.deproxy(desc, TaxonNameDescription.class);
TaxonName name = nameDesc.getTaxonName();
if (name != null) {
nameUuidAndTitleCache = new UuidAndTitleCache<TaxonName>(name.getUuid(), name.getId(), name.getTitleCache());
}
}
List<DescriptionElementDto> elements = new ArrayList<>();
for (Object element : desc.getElements()) {
if (element instanceof CategoricalData) {
Feature feature = ((CategoricalData) element).getFeature();
// FeatureDto featureDto = FeatureDto.fromFeature(feature);
CategoricalDataDto dto = CategoricalDataDto.fromCategoricalData((CategoricalData) element);
elements.add(dto);
}
if (element instanceof QuantitativeData) {
Feature feature = ((QuantitativeData) element).getFeature();
FeatureDto featureDto = FeatureDto.fromFeature(feature);
QuantitativeDataDto dto = QuantitativeDataDto.fromQuantitativeData((QuantitativeData) element);
elements.add(dto);
}
}
DescriptionBaseDto dto = new DescriptionBaseDto(desc.getUuid(), desc.getTitleCache(), taxonUuidAndTitleCache, specimenDto, nameUuidAndTitleCache, desc.getId(), elements, desc.getTypes());
return dto;
}
Aggregations