Example usage for org.apache.commons.math3.util Pair getKey

List of usage examples for org.apache.commons.math3.util Pair getKey

Introduction

In this page you can find the example usage for org.apache.commons.math3.util Pair getKey.

Prototype

public K getKey() 

Source Link

Document

Get the key.

Usage

From source file:ldbc.snb.datagen.generator.distribution.utils.Bucket.java

public static ArrayList<Bucket> bucketizeHistogram(ArrayList<Pair<Integer, Integer>> histogram,
        int num_buckets) {

    ArrayList<Bucket> buckets = new ArrayList<Bucket>();
    int population = 0;
    int num_edges = 0;
    for (Pair<Integer, Integer> i : histogram) {
        population += i.getValue();/*from  w w  w. j a  va  2 s. co m*/
        num_edges += i.getValue() * i.getKey();
    }
    num_edges /= 2;

    int avgDegreeAt1B = 200;
    int avgDegree = num_edges / population;
    double aCoeff = Math.log(avgDegreeAt1B) / Math.log(1000000000);
    double bCoeff = (aCoeff - (Math.log(avgDegree) / Math.log(population))) / Math.log10(population);

    int target_mean = (int) Math.round(
            Math.pow(DatagenParams.numPersons, (aCoeff - bCoeff * Math.log10(DatagenParams.numPersons))));
    System.out.println("Distribution mean degree: " + avgDegree + " Distribution target mean " + target_mean);
    int bucket_size = (int) (Math.ceil(population / (double) (num_buckets)));
    int current_histogram_index = 0;
    int current_histogram_left = histogram.get(current_histogram_index).getValue();
    for (int i = 0; i < num_buckets && (current_histogram_index < histogram.size()); ++i) {
        int current_bucket_count = 0;
        int min = population;
        int max = 0;
        while (current_bucket_count < bucket_size && current_histogram_index < histogram.size()) {
            int degree = histogram.get(current_histogram_index).getKey();
            min = degree < min ? degree : min;
            max = degree > max ? degree : max;
            if ((bucket_size - current_bucket_count) > current_histogram_left) {
                current_bucket_count += current_histogram_left;
                current_histogram_index++;
                if (current_histogram_index < histogram.size()) {
                    current_histogram_left = histogram.get(current_histogram_index).getValue();
                }
            } else {
                current_histogram_left -= (bucket_size - current_bucket_count);
                current_bucket_count = bucket_size;
            }
        }
        min = (int) (min * target_mean / (double) avgDegree);
        max = (int) (max * target_mean / (double) avgDegree);
        buckets.add(new Bucket(min, max));
    }
    return buckets;
}

From source file:com.insightml.utils.Collections.java

public static <K> Collection<K> keys(final List<? extends Pair<K, ?>> subList) {
    final List<K> keys = new LinkedList<>();
    for (final Pair<K, ?> entry : subList) {
        keys.add(entry.getKey());
    }// w w  w.j  a va 2s. c o m
    return keys;
}

From source file:io.IOTools.java

/**
 * @param algoParameters/*from www  .  ja v a2s  .c  om*/
 * @param fourLetterCode that must be lower case
 * @return Can return null
 */
public static Pair<String, MyStructureIfc> getMyStructureIfc(AlgoParameters algoParameters,
        char[] fourLetterCode) {
    BiojavaReader reader = new BiojavaReader(algoParameters);
    Pair<String, Structure> pairPathStructure = null;
    try {
        // TODO change to char[]
        pairPathStructure = reader.readFromPDBFolder(String.valueOf(fourLetterCode),
                algoParameters.getPATH_TO_REMEDIATED_PDB_MMCIF_FOLDER(),
                algoParameters.getPATH_TO_CHEMCOMP_FOLDER());
    } catch (IOException | ExceptionInIOPackage e) {
        return null;
    }
    String pdbFileHash = null;
    try {
        pdbFileHash = HashTablesTools.getMD5hash(pairPathStructure.getKey());
    } catch (NoSuchAlgorithmException | IOException e) {
        e.printStackTrace();
    }
    AdapterBioJavaStructure adapterBioJavaStructure = new AdapterBioJavaStructure(algoParameters);
    MyStructureIfc mystructure = null;
    try {
        mystructure = adapterBioJavaStructure.getMyStructureAndSkipHydrogens(pairPathStructure.getValue(),
                pdbFileHash);
    } catch (ExceptionInMyStructurePackage | ReadingStructurefileException | ExceptionInConvertFormat e) {
        return null;
    }
    Pair<String, MyStructureIfc> pairPathMyStructure = new Pair(pairPathStructure.getKey(), mystructure);
    return pairPathMyStructure;
}

From source file:io.IOTools.java

public static Pair<String, MyStructureIfc> getMyStructureIfc(AlgoParameters algoParameters, String pathToFile) {

    BiojavaReader reader = new BiojavaReader(algoParameters);
    Pair<String, Structure> pairPathStructure = null;
    try {/*from   w w w.j  ava2s  .  co  m*/
        pairPathStructure = reader.read(pathToFile, algoParameters.getPATH_TO_CHEMCOMP_FOLDER());
    } catch (IOException | ExceptionInIOPackage e) {
        return null;
    }
    AdapterBioJavaStructure adapterBioJavaStructure = new AdapterBioJavaStructure(algoParameters);
    MyStructureIfc mystructure = null;
    String pdbFileHash = null;
    try {
        pdbFileHash = HashTablesTools.getMD5hash(pathToFile);
    } catch (NoSuchAlgorithmException | IOException e) {
        e.printStackTrace();
    }
    try {

        mystructure = adapterBioJavaStructure.getMyStructureAndSkipHydrogens(pairPathStructure.getValue(),
                pdbFileHash);
    } catch (ExceptionInMyStructurePackage | ReadingStructurefileException | ExceptionInConvertFormat e) {
        return null;
    }
    Pair<String, MyStructureIfc> pairPathMyStructure = new Pair(pairPathStructure.getKey(), mystructure);
    return pairPathMyStructure;
}

From source file:io.ReadFilesGetsHashCodeTest.java

@Test
public void readFileAndCheckHashCode() throws IOException, ParsingConfigFileException {

    AlgoParameters algoParameters = Tools.generateModifiedAlgoParametersForTestWithTestFolders();

    String fourLetterCode = "1di9";
    Pair<String, MyStructureIfc> pathAndMyStructure = IOTools.getMyStructureIfc(algoParameters,
            fourLetterCode.toCharArray());

    String path = pathAndMyStructure.getKey();
    System.out.println(path);/*  ww w.  j  a  v a2  s  .  c om*/
    assertTrue(path.contains(Tools.testFolderName)); // check if file really coming from test folder

    String pdbFileHashFromPathFromMyStructure = pathAndMyStructure.getValue().getPdbFileHash();
    try {
        String pdbFileHashFromPath = HashTablesTools.getMD5hash(path);
        assertTrue(pdbFileHashFromPath.equals(pdbFileHashFromPathFromMyStructure));
    } catch (NoSuchAlgorithmException e) {
        assertTrue(false);
    }
}

From source file:io.warp10.script.functions.OPTDTW.java

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
    Object o = stack.pop();//from  w w  w  .  j a  va2 s  .c  o  m

    if (!(o instanceof Number)) {
        throw new WarpScriptException(
                getName() + " expects a count of best restults to return on top of the stack.");
    }

    int count = ((Number) o).intValue();

    o = stack.pop();

    if (!(o instanceof List)) {
        throw new WarpScriptException(getName() + " expects a numeric list to use as query below the count.");
    }

    double[] query = new double[((List) o).size()];
    int i = 0;
    for (Object oo : (List) o) {
        query[i++] = ((Number) oo).doubleValue();
    }

    // Z-Normalize query
    double[] musigma = DoubleUtils.musigma(query, true);
    for (i = 0; i < query.length; i++) {
        query[i] = (query[i] - musigma[0]) / musigma[1];
    }

    o = stack.pop();

    if (!(o instanceof List)) {
        throw new WarpScriptException(getName()
                + " expects a numeric list as the sequence in which to find best matches below the 'query' list.");
    }

    double[] sequence = new double[((List) o).size()];
    i = 0;
    for (Object oo : (List) o) {
        sequence[i++] = ((Number) oo).doubleValue();
    }

    if (sequence.length <= query.length) {
        throw new WarpScriptException(
                getName() + " expects the query list to be shorter than the sequence list.");
    }

    double mindist = 0.0;

    PriorityQueue<Pair<Integer, Double>> distances = new PriorityQueue<Pair<Integer, Double>>(
            new Comparator<Pair<Integer, Double>>() {
                @Override
                public int compare(Pair<Integer, Double> o1, Pair<Integer, Double> o2) {
                    return o1.getValue().compareTo(o2.getValue());
                }
            });

    double[] subsequence = new double[query.length];

    for (i = 0; i <= sequence.length - query.length; i++) {
        System.arraycopy(sequence, i, subsequence, 0, query.length);
        // Z-Normalize the subsequence
        musigma = DoubleUtils.musigma(subsequence, true);
        for (int j = 0; j < subsequence.length; j++) {
            subsequence[j] = (subsequence[j] - musigma[0]) / musigma[1];
        }
        double dist = dtw.compute(query, 0, query.length, subsequence, 0, query.length, mindist);

        if (dist < 0) {
            continue;
        }

        distances.add(new Pair<Integer, Double>(i, dist));

        //
        // If the priority queue is of 'count' size, retrieve the largest distance and
        // use it as the threshold for the DTW computation
        //

        if (count > 0 && distances.size() >= count) {
            Object adist[] = distances.toArray();
            mindist = ((Pair<Integer, Double>) adist[count - 1]).getValue();
        }
    }

    List<List<Object>> results = new ArrayList<List<Object>>();

    while (!distances.isEmpty()) {

        Pair<Integer, Double> entry = distances.poll();

        List<Object> result = new ArrayList<Object>();
        result.add(entry.getKey());
        result.add(entry.getValue());
        results.add(result);

        if (count > 0 && count == results.size()) {
            break;
        }
    }

    stack.push(results);

    return stack;
}

From source file:com.yahoo.egads.models.tsmm.OlympicModel2.java

@Override
public void predict(final DataSequence sequence) throws Exception {
    if (model == null || model.isEmpty()) {
        throw new IllegalStateException("Model was empty. 'train()' may " + "not have been called.");
    }/*from   ww  w.j av  a2  s .  c  o m*/
    // TODO - proper... setting... uggg!!
    int x = 0;
    for (int i = 0; i < sequence.size(); i++) {
        while (x < model.size() && sequence.get(i).time > model.get(x).getKey()) {
            ++x;
        }
        if (x >= model.size()) {
            break;
        }
        if (sequence.get(i).time == model.get(x).getKey()) {
            final Pair<Long, Double> dp = model.get(x++);
            sequence.set(i, new Entry(dp.getKey(), (float) (double) dp.getValue()));
        }
    }
}

From source file:convertformat.AdapterBiojavaStructureNucleosideCovalentlyBoundTest.java

@Test
public void testGenerateSequenceFromMyStructureWithProblemInStoringInSequenceDB()
        throws ParsingConfigFileException, IOException {

    AlgoParameters algoParameters = Tools.generateModifiedAlgoParametersForTestWithTestFolders();

    String fourLetterCode = "5a07";
    BiojavaReader reader = new BiojavaReader(algoParameters);
    Pair<String, Structure> pathAndmmcifStructure = null;
    try {/*from  ww  w  .  j  a v a  2s  .  c  o  m*/
        pathAndmmcifStructure = reader.readFromPDBFolder(fourLetterCode, Tools.testPDBFolder,
                Tools.testChemcompFolder);
    } catch (IOException | ExceptionInIOPackage e) {
        assertTrue(false);
    }
    String hash = null;
    try {
        hash = HashTablesTools.getMD5hash(pathAndmmcifStructure.getKey());
    } catch (NoSuchAlgorithmException e) {
        assertTrue(false);
    }

    AdapterBioJavaStructure adapterBioJavaStructure = new AdapterBioJavaStructure(algoParameters);
    MyStructureIfc mystructure = null;
    try {
        mystructure = adapterBioJavaStructure.getMyStructureAndSkipHydrogens(pathAndmmcifStructure.getValue(),
                hash);
    } catch (ExceptionInMyStructurePackage | ReadingStructurefileException | ExceptionInConvertFormat e) {
        assertTrue(false);
    }

    Group mmcifGDP = pathAndmmcifStructure.getValue().getChain(0).getAtomGroup(395);
    assertTrue(mmcifGDP.getPDBName().equals("GDP"));
    GroupType type = mmcifGDP.getType();
    assertTrue(type == GroupType.NUCLEOTIDE);

    assertTrue(mystructure.getAllAminochains().length == 2);
    // Empty nucleosides chains should had been removed
    assertTrue(mystructure.getAllNucleosidechains().length == 0);

    MyMonomerIfc myStructureGDP = mystructure.getAminoChain(0).getMyMonomerByRank(395);
    assertTrue(Arrays.equals(myStructureGDP.getThreeLetterCode(), "GDP".toCharArray()));
    assertTrue(Arrays.equals(myStructureGDP.getType(), MyMonomerType.NUCLEOTIDE.getType()));
    assertTrue(myStructureGDP.isWasHetatm() == false);
}

From source file:convertformat.AdapterBiojavaStructureVariousCheckCovalentHetatmInsertion.java

@Test
public void testconvertStructureToMyStructureWithPTRcovalentLigand()
        throws ParsingConfigFileException, IOException {

    AlgoParameters algoParameters = Tools.generateModifiedAlgoParametersForTestWithTestFolders();

    String fourLetterCode = "2mrk";
    BiojavaReader reader = new BiojavaReader(algoParameters);
    Pair<String, Structure> pathAndmmcifStructure = null;
    try {//  www  . j  a  v  a2 s.co  m
        pathAndmmcifStructure = reader.readFromPDBFolder(fourLetterCode, Tools.testPDBFolder,
                Tools.testChemcompFolder);
    } catch (IOException | ExceptionInIOPackage e) {
        assertTrue(false);
    }
    String hash = null;
    try {
        hash = HashTablesTools.getMD5hash(pathAndmmcifStructure.getKey());
    } catch (NoSuchAlgorithmException e) {
        assertTrue(false);
    }

    AdapterBioJavaStructure adapterBioJavaStructure = new AdapterBioJavaStructure(algoParameters);
    MyStructureIfc mystructure = null;
    try {
        mystructure = adapterBioJavaStructure.getMyStructureAndSkipHydrogens(pathAndmmcifStructure.getValue(),
                hash);
    } catch (ExceptionInMyStructurePackage | ReadingStructurefileException | ExceptionInConvertFormat e) {
        assertTrue(false);
    }

    Group mmcifPTR = pathAndmmcifStructure.getValue().getChain(1).getAtomGroup(3);
    assertTrue(mmcifPTR.getPDBName().equals("PTR"));
    GroupType type = mmcifPTR.getType();
    assertTrue(type == GroupType.AMINOACID);
    MyMonomerIfc myStructurePTR = mystructure.getAminoMyChain("B".toCharArray()).getMyMonomerByRank(3);
    assertTrue(Arrays.equals(myStructurePTR.getThreeLetterCode(), "PTR".toCharArray()));
    assertTrue(Arrays.equals(myStructurePTR.getType(), MyMonomerType.AMINOACID.getType()));
    assertTrue(myStructurePTR.isWasHetatm() == false);
}

From source file:convertformat.AdapterBiojavaStructureVariousCheckCovalentHetatmInsertion.java

@Test
public void testconvertStructureToMyStructureWithcovalentPSOLigandToNucleosides()
        throws ParsingConfigFileException, IOException {

    AlgoParameters algoParameters = Tools.generateModifiedAlgoParametersForTestWithTestFolders();

    String fourLetterCode = "203d";
    BiojavaReader reader = new BiojavaReader(algoParameters);
    Pair<String, Structure> pathAndmmcifStructure = null;
    try {//from w w w.j  a v a  2  s  . c o m
        pathAndmmcifStructure = reader.readFromPDBFolder(fourLetterCode, Tools.testPDBFolder,
                Tools.testChemcompFolder);
    } catch (IOException | ExceptionInIOPackage e) {
        assertTrue(false);
    }
    String hash = null;
    try {
        hash = HashTablesTools.getMD5hash(pathAndmmcifStructure.getKey());
    } catch (NoSuchAlgorithmException e) {
        assertTrue(false);
    }

    AdapterBioJavaStructure adapterBioJavaStructure = new AdapterBioJavaStructure(algoParameters);
    MyStructureIfc mystructure = null;
    try {
        mystructure = adapterBioJavaStructure.getMyStructureAndSkipHydrogens(pathAndmmcifStructure.getValue(),
                hash);
    } catch (ExceptionInMyStructurePackage | ReadingStructurefileException | ExceptionInConvertFormat e) {
        assertTrue(false);
    }

    // PSO is integrated with cutoff bond distance 1.4but not at 1.6.
    // It looks really tightly bound in the structure

    Group mmcifPSO = pathAndmmcifStructure.getValue().getChain(0).getAtomGroup(8);
    assertTrue(mmcifPSO.getPDBName().equals("PSO"));
    GroupType type = mmcifPSO.getType();
    assertTrue(type == GroupType.HETATM);
    MyMonomerIfc myStructurePSO = mystructure.getNucleosideChain(("A").toCharArray()).getMyMonomerByRank(8);
    assertTrue(Arrays.equals(myStructurePSO.getThreeLetterCode(), "PSO".toCharArray()));
    // PSO is moved, changed to AminoAcid type and the was Hetatm is set to true
    assertTrue(Arrays.equals(myStructurePSO.getType(), MyMonomerType.AMINOACID.getType()));
    assertTrue(myStructurePSO.isWasHetatm() == true);
}