List of usage examples for org.apache.commons.collections15 Bag uniqueSet
Set<E> uniqueSet();
From source file:com.diversityarrays.kdxplore.vistool.VisToolData.java
static private void appendLines(String fmt, Bag<String> bag, List<String> lines) { for (String s : bag.uniqueSet()) { lines.add(" " + MessageFormat.format(fmt, s, bag.getCount(s))); //$NON-NLS-1$ }//from ww w . ja v a 2 s . c o m }
From source file:com.diversityarrays.kdxplore.curate.TypedSampleMeasurement.java
/** * Return null if all the DeviceNames are unique else * the shortFormat if it is sufficient to uniquely identify the TypedSampleMeasurements * @param coll//from w ww .jav a2s . com * @param shortFormat * @param longFormat * @return */ static public DateFormat getDateFormatForUniqueIdent(Collection<TypedSampleMeasurement> coll, DateFormat shortFormat, DateFormat longFormat) { Map<String, Bag<String>> datesByDeviceName = new HashMap<>(); boolean foundDuplicateDeviceName = false; for (TypedSampleMeasurement tsm : coll) { if (DeviceType.KDSMART == tsm.deviceIdentifier.getDeviceType()) { String deviceName = tsm.deviceIdentifier.getDeviceName(); if (datesByDeviceName.containsKey(deviceName)) { foundDuplicateDeviceName = true; } else { datesByDeviceName.put(deviceName, new HashBag<>()); } } } if (!foundDuplicateDeviceName) { return null; } for (TypedSampleMeasurement tsm : coll) { if (DeviceType.KDSMART == tsm.deviceIdentifier.getDeviceType()) { String deviceName = tsm.deviceIdentifier.getDeviceName(); Bag<String> bag = datesByDeviceName.get(deviceName); if (tsm.sampleGroupDate != null) { String s = longFormat.format(tsm.sampleGroupDate); bag.add(s); } } } for (Bag<String> bag : datesByDeviceName.values()) { for (String key : bag.uniqueSet()) { if (bag.getCount(key) > 1) { return longFormat; } } } return shortFormat; }
From source file:com.diversityarrays.kdxplore.stats.StatsUtil.java
static public List<String> computeMode(Bag<String> svalues, NumericTraitValidationProcessor tvp) { List<String> result; Map<Integer, List<String>> valuesByCount = new HashMap<Integer, List<String>>(); for (String v : svalues.uniqueSet()) { Integer count = svalues.getCount(v); List<String> list = valuesByCount.get(count); if (list == null) { list = new ArrayList<String>(); valuesByCount.put(count, list); }/*from w ww .ja v a 2 s . c o m*/ list.add(v); } List<Integer> counts = new ArrayList<Integer>(valuesByCount.keySet()); int nCounts = counts.size(); if (nCounts < 1) { result = new ArrayList<>(); ; } else if (nCounts == 1) { result = valuesByCount.get(counts.get(0)); } else { Collections.sort(counts); int lastIndex = nCounts - 1; int countMax = counts.get(lastIndex); result = valuesByCount.get(countMax); } return result; }
From source file:com.davidsoergel.stats.Multinomial.java
public Multinomial(Bag<T> counts) throws DistributionException { this();/*w w w .ja v a 2 s. c o m*/ for (T k : counts.uniqueSet()) { put(k, counts.getCount(k)); } normalize(); }
From source file:com.diversityarrays.kdxplore.importdata.bms.BmsExcelImportCallable.java
public void report(PrintStream ps) { ps.println("Collected " + plots.size() + " plots"); Bag<String> plotTypeCounts = new HashBag<>(); for (Plot plot : plots) { plotTypeCounts.add(plot.getPlotType()); }/*from www . ja v a 2 s . c om*/ ps.println("Found PlotTypes:"); for (String plotType : plotTypeCounts.uniqueSet()) { ps.println("\t" + plotType + ": " + plotTypeCounts.getCount(plotType)); } ps.println("Created " + traitByName.size() + " Traits"); for (Trait trait : traitByName.values()) { ps.println("\t" + trait + " [id=" + trait.getTraitId() + "]"); } ps.println("Created " + plotAttributeByName.size() + " Plot Attributes"); for (PlotAttribute pa : plotAttributeByName.values()) { ps.println("\t" + pa); } ps.println("Created " + nPlotAttributeValues + " Plot Attribute Values"); ps.println("Created " + nSamples + " Samples"); if (!lineNumbersByMessage.isEmpty()) { ps.println("Warnings:"); for (String msg : lineNumbersByMessage.keySet()) { Set<Integer> range = lineNumbersByMessage.get(msg); ps.println(range.size() + " times: " + msg); } } }
From source file:com.diversityarrays.kdxplore.curate.TraitsAndInstancesPanel2.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private void describe(StringBuilder html, String which, Bag outliers) { html.append(which);//from www .j a v a 2 s . co m if (Check.isEmpty(outliers)) { html.append("<BR>").append("None"); } else { for (Object t : outliers.uniqueSet()) { html.append("<BR>").append(t); int count = outliers.getCount(t); if (count > 1) { html.append(" (").append(count).append(')'); } } } }
From source file:com.diversityarrays.kdxplore.trials.TrialOverviewPanel.java
private void doAddTraitInstances(Trial selectedTrial, Map<Trait, List<Integer>> instanceNumbersByTrait) throws IOException { List<Trait> unusedTraits = offlineData.getKdxploreDatabase().getAllTraits(); Map<Integer, Trait> traitById = unusedTraits.stream() .collect(Collectors.toMap(Trait::getTraitId, Function.identity())); List<TraitInstance> instances = this.getAllTraitInstances(selectedTrial); Map<Trait, Integer> instancesByTrait = new HashMap<>(); for (TraitInstance ti : instances) { Trait trait = traitById.get(ti.getTraitId()); Integer inst = instancesByTrait.get(trait); if (inst == null) { inst = ti.getInstanceNumber(); } else {//from w w w. jav a2s . c om inst++; } instancesByTrait.put(trait, inst); } for (Trait trait : instanceNumbersByTrait.keySet()) { List<Integer> newInstances = instanceNumbersByTrait.get(trait); if (!Check.isEmpty(newInstances)) { Integer currentCount = instancesByTrait.get(trait); if (currentCount != null) { List<Integer> toUse = new ArrayList<>(); newInstances.stream().forEach(i -> toUse.add(i += currentCount)); instanceNumbersByTrait.put(trait, toUse); } } } if (instanceNumbersByTrait.isEmpty()) { return; } int trialId = selectedTrial.getTrialId(); int maxSso = 0; Bag<Integer> ssoUseCount = new HashBag<>(); Map<Trait, Integer> traitSsoMap = trialTraitsTableModel.getTraitsSsoForTrial(selectedTrial); if (!Check.isEmpty(traitSsoMap)) { for (Integer sso : traitSsoMap.values()) { if (sso != null) { maxSso = Math.max(maxSso, sso); ssoUseCount.add(sso); } } } TraitNameStyle traitNameStyle = selectedTrial.getTraitNameStyle(); final int firstInstanceNumber = traitNameStyle.getFirstInstanceNumber(); int maxSsoIncrement = 1; if (ssoUseCount.uniqueSet().size() == 1 && maxSso == 0 && ssoUseCount.getCount(maxSso) > 1) { // Special hack for Trials that haven't yet been "sorted". // (i.e. all the SSOs are the same value maxSsoIncrement = 0; } List<Trait> ignored = new ArrayList<>(); Map<TraitLevel, List<TraitInstance>> instancesByLevel = new HashMap<>(); boolean multipleTraitInstances = false; Set<TraitInstance> traitInstances = new LinkedHashSet<>(); for (Trait trait : instanceNumbersByTrait.keySet()) { switch (trait.getTraitLevel()) { case PLOT: case SPECIMEN: break; default: ignored.add(trait); continue; } maxSso += maxSsoIncrement; List<Integer> instancesNumbersWanted = instanceNumbersByTrait.get(trait); if (instancesNumbersWanted.size() > 1) { multipleTraitInstances = true; } for (Integer instanceNumber : instancesNumbersWanted) { TraitInstance ti = new TraitInstance(); // if (firstInstanceNumber <= 0) { // ti.setInstanceNumber(instanceNumber - 1); // } // else { ti.setInstanceNumber(instanceNumber); // } ti.setScoringSortOrder(maxSso); ti.setTraitId(trait.getTraitId()); ti.setTrialId(trialId); ti.setUsedForScoring(true); traitInstances.add(ti); List<TraitInstance> list = instancesByLevel.get(trait.getTraitLevel()); if (list == null) { list = new ArrayList<>(); instancesByLevel.put(trait.getTraitLevel(), list); } list.add(ti); } } KdxploreDatabase kdxdb = offlineData.getKdxploreDatabase(); KDSmartDatabase kdsdb = kdxdb.getKDXploreKSmartDatabase(); Map<Integer, DeviceIdentifier> devidMap = kdxdb.getDeviceIdentifierMap(); Optional<DeviceIdentifier> opt_devid = devidMap.values().stream() .filter(devid -> DeviceType.EDITED.equals(devid.getDeviceType())).findFirst(); List<KdxSample> samples; if (opt_devid.isPresent()) { DeviceIdentifier curated = opt_devid.get(); List<SampleGroup> sampleGroups = kdxdb.getSampleGroups(trialId, KdxploreDatabase.WithSamplesOption.WITH_SAMPLES); int curatedSampleGroupId = curated.getDeviceIdentifierId(); Optional<SampleGroup> opt_sg = sampleGroups.stream() .filter(sg -> curatedSampleGroupId == sg.getDeviceIdentifierId()).findFirst(); if (opt_sg.isPresent()) { samples = new ArrayList<>(); List<TraitInstance> plotInstances = instancesByLevel.get(TraitLevel.PLOT); List<TraitInstance> subPlotInstances = instancesByLevel.get(TraitLevel.SPECIMEN); Consumer<KdxSample> sampleConsumer = new Consumer<KdxSample>() { @Override public void accept(KdxSample sample) { samples.add(sample); } }; TrialItemVisitor<Plot> plotVisitor = new TrialItemVisitor<Plot>() { @Override public void setExpectedItemCount(int count) { } @Override public boolean consumeItem(Plot plot) { DatabaseUtil.createSamples(trialId, plot, curatedSampleGroupId, null, // previousSamplesByKey plotInstances, subPlotInstances, sampleConsumer); return true; } }; kdsdb.visitPlotsForTrial(trialId, SampleGroupChoice.NO_TAGS_SAMPLE_GROUP, KDSmartDatabase.WithPlotAttributesOption.WITHOUT_PLOT_ATTRIBUTES, plotVisitor); } else { samples = null; } } else { samples = null; } BatchHandler<Void> batchHandler = new BatchHandler<Void>() { @Override public Void call() throws Exception { kdsdb.saveTraitInstances(traitInstances); if (!Check.isEmpty(samples)) { kdsdb.saveMultipleSamples(samples, false); } return null; } @Override public boolean checkSuccess(Void t) { return true; } }; boolean saved = true; Either<Exception, Void> either = kdsdb.doBatch(batchHandler); if (either.isLeft()) { saved = false; MsgBox.error(TrialOverviewPanel.this, either.left(), "Database Error"); } refreshTrialTableModel(offlineData.getKdxploreDatabase()); trialTraitsTableModel.setSelectedTrial(selectedTrial); if (saved) { String prefix = multipleTraitInstances ? "Trait Instances Added:\n" : "Traits Added:\n"; Function<Trait, String> nameFactory = new Function<Trait, String>() { @Override public String apply(Trait trait) { List<Integer> instanceNumbers = instanceNumbersByTrait.get(trait); if (instanceNumbers == null) { return null; } else { return instanceNumbers.stream().map(inum -> inum.toString()) .collect(Collectors.joining(",", trait.getTraitName() + ": ", "")); } } }; String selection = instanceNumbersByTrait.keySet().stream().map(nameFactory).filter(n -> n != null) .collect(Collectors.joining("\n", prefix, "\n------")); //$NON-NLS-1$//$NON-NLS-2$ messagePrinter.println(selection); if (samples != null) { messagePrinter.println("Samples Added: " + samples.size()); } } }
From source file:com.diversityarrays.kdxplore.trials.SampleGroupViewer.java
private String getBriefSummary(SampleGroup sampleGroup) throws IOException { Bag<Integer> plotIdsWithSpecimens = new HashBag<>(); Set<Integer> plotIdsWithScores = new HashSet<>(); int[] results = new int[3]; java.util.function.Predicate<KdxSample> visitor = new java.util.function.Predicate<KdxSample>() { @Override/* www . ja v a2 s.c om*/ public boolean test(KdxSample s) { plotIdsWithScores.add(s.getPlotId()); int snum = s.getSpecimenNumber(); if (snum <= 0) { ++results[0]; // Plot level sample count } else { ++results[1]; // Individual level sample count plotIdsWithSpecimens.add(s.getPlotId()); results[2] = Math.max(results[2], snum); // maximum specimen number } return true; } }; boolean scored = true; kdxdb.visitKdxSamplesForSampleGroup(sampleGroup, KdxploreDatabase.SampleLevel.BOTH, scored, visitor); int nPlotSamples = results[0]; int nSpecimenSamples = results[1]; int totalScoredSamples = nPlotSamples + nSpecimenSamples; int maxSpecimenNumber = results[2]; int nPlotsWithSpecimens = plotIdsWithSpecimens.size(); int nPlotsWithScores = plotIdsWithScores.size(); int maxCount = 0; for (Integer plotId : plotIdsWithSpecimens.uniqueSet()) { int count = plotIdsWithSpecimens.getCount(plotId); maxCount = Math.max(maxCount, count); } StringBuilder sb = new StringBuilder("<HTML>"); sb.append("<br><B>Scored Samples:</b> ").append(totalScoredSamples); sb.append("<br><B>Plot Level Samples:</b> ").append(nPlotSamples); sb.append("<br><B>Individual Samples:</b> ").append(nSpecimenSamples); sb.append("<br>"); sb.append("<br><B>Max Individual number:</b> ").append(maxSpecimenNumber); sb.append("<br><B># Plots with Individuals:</b> ").append(nPlotsWithSpecimens); sb.append("<br>"); sb.append("<br><B># Plots with scored samples:</b> ").append(nPlotsWithScores); sb.append("<br><B>Max # individual scores per plot:</b> ").append(maxCount); return sb.toString(); }
From source file:com.diversityarrays.kdxplore.curate.TrialDataEditor.java
private void checkForInvalidTraits() { SampleGroup db_smdata = curationData.getDatabaseSampleGroup(); if (db_smdata == null) { return;//from ww w. j a va 2 s.c o m } Bag<Integer> traitIdCountsFromDatabase = new HashBag<>(); for (KdxSample sm : db_smdata.getSamples()) { traitIdCountsFromDatabase.add(sm.getTraitId()); } Set<Integer> errorTraitIds = new HashSet<>(traitIdCountsFromDatabase.uniqueSet()); Set<Integer> traitIds = CurationData.getTraitIds(curationData); errorTraitIds.removeAll(traitIds); if (!errorTraitIds.isEmpty()) { for (Integer id : errorTraitIds) { if (id == null) { continue; } Trait trait = this.traitProvider.apply(id); if (trait == null) { trait = new Trait(); trait.setTraitId(id); trait.setTraitName("Unknown Trait#" + id); } errorTraitsById.put(id, trait); } } }
From source file:net.sqs2.util.PathUtil.java
public static Set<File> getSharedSuperDirectorySet(Iterable<File> files) { Bag<File> bag = new HashBag<File>(); for (File file : files) { File superdir = file;//from w w w . j a v a 2 s. c o m do { // System.err.print("SRC:"+file); bag.add(superdir); // System.err.print("\t"); // System.err.print(superdir); } while ((superdir = superdir.getParentFile()) != null); } Set<File> ret = new HashSet<File>(); for (File file : bag.uniqueSet()) { if (1 < bag.getCount(file)) { ret.add(file); } } return ret; }