Search in sources :

Example 16 with QuantitativeData

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);
}
Also used : ElementImpl(org.apache.xerces.dom.ElementImpl) CategoricalData(eu.etaxonomy.cdm.model.description.CategoricalData) TextData(eu.etaxonomy.cdm.model.description.TextData) QuantitativeData(eu.etaxonomy.cdm.model.description.QuantitativeData) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase)

Example 17 with QuantitativeData

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;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) QuantitativeData(eu.etaxonomy.cdm.model.description.QuantitativeData) Feature(eu.etaxonomy.cdm.model.description.Feature) BigDecimal(java.math.BigDecimal) State(eu.etaxonomy.cdm.model.description.State) FeatureState(eu.etaxonomy.cdm.model.description.FeatureState) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 18 with QuantitativeData

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;
}
Also used : ArrayList(java.util.ArrayList) QuantitativeData(eu.etaxonomy.cdm.model.description.QuantitativeData) BigDecimal(java.math.BigDecimal)

Example 19 with QuantitativeData

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;
}
Also used : QuantitativeData(eu.etaxonomy.cdm.model.description.QuantitativeData) BigDecimal(java.math.BigDecimal)

Example 20 with QuantitativeData

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;
}
Also used : SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) TaxonNameDescription(eu.etaxonomy.cdm.model.description.TaxonNameDescription) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) ArrayList(java.util.ArrayList) QuantitativeData(eu.etaxonomy.cdm.model.description.QuantitativeData) Feature(eu.etaxonomy.cdm.model.description.Feature) CategoricalData(eu.etaxonomy.cdm.model.description.CategoricalData) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) FeatureDto(eu.etaxonomy.cdm.persistence.dto.FeatureDto)

Aggregations

QuantitativeData (eu.etaxonomy.cdm.model.description.QuantitativeData)25 Feature (eu.etaxonomy.cdm.model.description.Feature)19 CategoricalData (eu.etaxonomy.cdm.model.description.CategoricalData)13 StatisticalMeasurementValue (eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue)12 BigDecimal (java.math.BigDecimal)12 ArrayList (java.util.ArrayList)10 DescriptionElementBase (eu.etaxonomy.cdm.model.description.DescriptionElementBase)9 MeasurementUnit (eu.etaxonomy.cdm.model.description.MeasurementUnit)9 TaxonDescription (eu.etaxonomy.cdm.model.description.TaxonDescription)9 TextData (eu.etaxonomy.cdm.model.description.TextData)9 Taxon (eu.etaxonomy.cdm.model.taxon.Taxon)8 HashSet (java.util.HashSet)8 Set (java.util.Set)7 DescriptiveDataSet (eu.etaxonomy.cdm.model.description.DescriptiveDataSet)6 SpecimenDescription (eu.etaxonomy.cdm.model.description.SpecimenDescription)6 State (eu.etaxonomy.cdm.model.description.State)6 StateData (eu.etaxonomy.cdm.model.description.StateData)6 StatisticalMeasure (eu.etaxonomy.cdm.model.description.StatisticalMeasure)6 TermDto (eu.etaxonomy.cdm.persistence.dto.TermDto)5 UUID (java.util.UUID)5