Example usage for org.apache.commons.lang ArrayUtils remove

List of usage examples for org.apache.commons.lang ArrayUtils remove

Introduction

In this page you can find the example usage for org.apache.commons.lang ArrayUtils remove.

Prototype

private static Object remove(Object array, int index) 

Source Link

Document

Removes the element at the specified position from the specified array.

Usage

From source file:examples.cnn.cifar.Cifar10Classification.java

public static void main(String[] args) {

    CifarReader.downloadAndExtract();/*from www. j  ava  2 s . c  o  m*/

    int numLabels = 10;

    SparkConf conf = new SparkConf();
    conf.setMaster(String.format("local[%d]", NUM_CORES));
    conf.setAppName("Cifar-10 CNN Classification");
    conf.set(SparkDl4jMultiLayer.AVERAGE_EACH_ITERATION, String.valueOf(true));

    try (JavaSparkContext sc = new JavaSparkContext(conf)) {

        NetworkTrainer trainer = new NetworkTrainer.Builder().model(ModelLibrary.net2)
                .networkToSparkNetwork(net -> new SparkDl4jMultiLayer(sc, net)).numLabels(numLabels)
                .cores(NUM_CORES).build();

        JavaPairRDD<String, PortableDataStream> files = sc.binaryFiles("data/cifar-10-batches-bin");

        JavaRDD<double[]> imagesTrain = files
                .filter(f -> ArrayUtils.contains(CifarReader.TRAIN_DATA_FILES, extractFileName.apply(f._1)))
                .flatMap(f -> CifarReader.rawDouble(f._2.open()));

        JavaRDD<double[]> imagesTest = files
                .filter(f -> CifarReader.TEST_DATA_FILE.equals(extractFileName.apply(f._1)))
                .flatMap(f -> CifarReader.rawDouble(f._2.open()));

        JavaRDD<DataSet> testDataset = imagesTest.map(i -> {
            INDArray label = FeatureUtil.toOutcomeVector(Double.valueOf(i[0]).intValue(), numLabels);
            double[] arr = Arrays.stream(ArrayUtils.remove(i, 0)).boxed().map(normalize2)
                    .mapToDouble(Double::doubleValue).toArray();
            INDArray features = Nd4j.create(arr, new int[] { 1, arr.length });
            return new DataSet(features, label);
        }).cache();
        log.info("Number of test images {}", testDataset.count());

        JavaPairRDD<INDArray, double[]> labelsWithDataTrain = imagesTrain.mapToPair(i -> {
            INDArray label = FeatureUtil.toOutcomeVector(Double.valueOf(i[0]).intValue(), numLabels);
            double[] arr = Arrays.stream(ArrayUtils.remove(i, 0)).boxed().map(normalize2)
                    .mapToDouble(Double::doubleValue).toArray();
            return new Tuple2<>(label, arr);
        });

        JavaRDD<DataSet> flipped = labelsWithDataTrain.map(t -> {
            double[] arr = t._2;
            int idx = 0;
            double[] farr = new double[arr.length];
            for (int i = 0; i < arr.length; i += trainer.getWidth()) {
                double[] temp = Arrays.copyOfRange(arr, i, i + trainer.getWidth());
                ArrayUtils.reverse(temp);
                for (int j = 0; j < trainer.getHeight(); ++j) {
                    farr[idx++] = temp[j];
                }
            }
            INDArray features = Nd4j.create(farr, new int[] { 1, farr.length });
            return new DataSet(features, t._1);
        });

        JavaRDD<DataSet> trainDataset = labelsWithDataTrain.map(t -> {
            INDArray features = Nd4j.create(t._2, new int[] { 1, t._2.length });
            return new DataSet(features, t._1);
        }).union(flipped).cache();
        log.info("Number of train images {}", trainDataset.count());

        trainer.train(trainDataset, testDataset);
    }
}

From source file:com.yahoo.egads.utilities.AutoSensitivity.java

public static Float getLowDensitySensitivity(Float[] data, float sDAutoSensitivy, float amntAutoSensitivity) {
    Float toReturn = Float.POSITIVE_INFINITY;
    Arrays.sort(data, Collections.reverseOrder());
    while (data.length > 0) {

        ArrayList<Float> fData = new ArrayList<Float>();
        fData.add(data[0]);//from ww w . j a  v  a 2 s . co m
        data = ((Float[]) ArrayUtils.remove(data, 0));

        Float centroid = (float) fData.get(0);
        Float maxDelta = (float) sDAutoSensitivy * StatsUtils.getSD(data, StatsUtils.getMean(data));

        logger.debug("AutoSensitivity: Adding: " + fData.get(0) + " SD: " + maxDelta);

        // Add points while it's in the same cluster or not part of the other cluster.
        String localDebug = null;
        while (data.length > 0 && (centroid - data[0]) <= ((float) (maxDelta))) {
            float maxDeltaInit = maxDelta;
            fData.add(data[0]);
            data = ((Float[]) ArrayUtils.remove(data, 0));
            Float[] tmp = new Float[fData.size()];
            tmp = fData.toArray(tmp);
            centroid = StatsUtils.getMean(tmp);

            if (data.length > 0) {
                Float sdOtherCluster = (float) StatsUtils.getSD(data, StatsUtils.getMean(data));
                maxDelta = sDAutoSensitivy * sdOtherCluster;
                logger.debug(
                        "AutoSensitivity: Adding: " + data[0] + " SD: " + maxDeltaInit + " SD': " + maxDelta);
            }
        }
        if (data.length > 0) {
            logger.debug("AutoSensitivity: Next Point I would have added is " + data[0]);
        }

        if (((double) fData.size() / (double) data.length) > amntAutoSensitivity) {
            // Cannot do anomaly detection.
            logger.debug("AutoSensitivity: Returning " + toReturn + " data size: " + data.length
                    + " fData.size: " + fData.size());
            return toReturn;
        }

        toReturn = fData.get(fData.size() - 1);
        logger.debug("AutoSensitivity: Updating toReturn:  " + toReturn + " SD: " + maxDelta);
        return toReturn;
    }
    return toReturn;
}

From source file:com.asual.summer.core.util.StringUtils.java

public static String ellipsis(String str, int length) {
    String space = " ";
    String ellipsis = "...";
    length = length - ellipsis.length();
    if (str.length() > length) {
        String[] words = str.substring(0, length).split(space);
        if (words.length > 1) {
            str = join(ArrayUtils.remove(words, words.length - 1), space);
        } else {/*from  www  . j a  va2  s . c  om*/
            str = words[0];
        }
        String end = str.substring(str.length() - 1, str.length());
        if (end.matches("\\.|\\?|\\!|,")) {
            str = str.substring(0, str.length() - 1);
        }
        return str + ellipsis;
    }
    return str;
}

From source file:com.joeygallegos.skypebot.commands.core.Commands.java

public static void parseMessage(ChatMessage message) throws Exception {

    String command = message.getContent();
    System.out.println("Received chat message: " + command);

    // IF NOT COMMAND
    if (!command.startsWith(Resource.COMMAND_PREFIX)) {
        ChatUtil.fuckingCheck(message);// w  ww  .  ja  v a2 s  . c  o  m
        return;
    }

    command = command.substring(1);
    String[] commandSplit = command.split(" ");

    if (commandSplit.length == 0) {
        System.out.println("Nothing found");
        return;
    }

    BotCommand cmd = null;
    for (BotCommand botCommand : getCommands()) {
        if (botCommand.getName().equalsIgnoreCase(commandSplit[0])) {
            cmd = botCommand;
        }
    }

    if (cmd != null) {
        long difference = System.currentTimeMillis() - lastCommand;
        if (difference <= 4000L) {
            Resource.message("Please wait " + TimeUtil.calculateTime(difference) + " before using this again!");
        } else {
            lastCommand = System.currentTimeMillis();
            if (cmd.isAdminOnly()) {
                if (!Arrays.asList(Resource.ADMINS).contains(message.getSenderId())) {
                    Resource.message("You can't use this command!");
                    return;
                }
            }
            cmd.run(message, (String[]) ArrayUtils.remove(commandSplit, 0));
        }
    } else {
        Resource.message("That command wasn't found!");
    }
}

From source file:name.richardson.james.bukkit.utilities.command.context.NestedCommandContext.java

/**
 * Constructs a NestedCommandContext from the provided arguments and CommandSender.
 *
 * @param arguments provided arguments/*from  www  .ja  v  a  2s .c  o  m*/
 * @param sender the CommandSender executing the command
 */
public NestedCommandContext(String[] arguments, CommandSender sender) {
    super((String[]) ArrayUtils.remove(arguments, 0), sender);
}

From source file:com.google.gdt.eclipse.designer.launch.JUnitGwtLaunchConfigurationDelegate.java

@Override
public String[] getClasspath(ILaunchConfiguration configuration) throws CoreException {
    String[] classpath = getClasspathAll(configuration);
    // remove patch from launch class path
    for (int i = 0; i < classpath.length; i++) {
        String element = classpath[i];
        if (element.indexOf("gwt-user-patch.jar") != -1) {
            classpath = (String[]) ArrayUtils.remove(classpath, i);
            break;
        }//from   w w w.  j  a v a 2s  .  com
    }
    return classpath;
}

From source file:indexing.Lemma.java

private static String[] tokenize(String untokenized) {
    untokenized = untokenized.replaceAll("'", " '");
    String[] tokens = untokenized.split(" ");
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[0].isEmpty()) {
            tokens = (String[]) ArrayUtils.remove(tokens, i);
        }//from  www  .j  a  va  2  s .  c  o m
    }

    return tokens;
}

From source file:at.tuwien.ifs.somtoolbox.data.ESOMInputData.java

@Override
protected void readVectorFile(String vectorFileName, boolean sparse) {
    try {/*from   w  w  w.  j av  a  2  s.  c o  m*/
        BufferedReader br = FileUtils.openFile("ESOM input data file", vectorFileName);
        // ignore comment lines
        String line = FileUtils.consumeHeaderComments(br);

        // first line: numVectors
        numVectors = Integer.parseInt(line.trim().substring(1).trim());
        // second line: dimensionality. Also includes the index/label field, thus we store the value -1
        dim = Integer.parseInt(br.readLine().trim().substring(1).trim()) - 1;

        initDataStructures(false);

        // third line - column types
        // TODO: process it
        line = br.readLine();

        // fourth line - component names => construct a template vector
        line = br.readLine();
        String[] componentNames = line.split(StringUtils.REGEX_SPACE_OR_TAB);
        templateVector = new SOMLibTemplateVector(numVectors, (String[]) ArrayUtils.remove(componentNames, 0));

        // all the other lines are data
        int index = 0;
        while ((line = br.readLine()) != null) {
            line = line.trim();
            if (line.length() == 0) {
                continue;
            }
            String[] lineElements = line.split(StringUtils.REGEX_SPACE_OR_TAB);
            // TODO: add a sanity check for lineElements.length == dim (or dim+1 if we have classes)
            for (int ve = 0; ve < dim; ve++) {
                setMatrixValue(index, ve, parseDouble(lineElements[ve + 1]));
            }
            addInstance(index, lineElements[0]);
            index++;
        }

    } catch (Exception e) {
        Logger.getLogger("at.tuwien.ifs.somtoolbox").severe(ERROR_MESSAGE_FILE_FORMAT_CORRUPT);
        e.printStackTrace();
        throw new IllegalArgumentException(e.getMessage());
    }
    Logger.getLogger("at.tuwien.ifs.somtoolbox").info("ESOM vector file seems to be correct. Riding on ...");
}

From source file:info.magnolia.cms.util.Rule.java

/**
 * remove from allow list//from w  w w .  j  a  va 2s .  c  o m
 * @param nodeType
 */
public void removeAllowType(String nodeType) {
    if (nodeType != null) {
        for (int j = 0; j < allowedTypes.length; j++) {
            if (nodeType.equals(allowedTypes[j])) {
                this.allowedTypes = (String[]) ArrayUtils.remove(allowedTypes, j);
                break;
            }
        }
    }
}

From source file:gda.device.detector.countertimer.TfgScalerWithLogValues.java

/**
 * When set to true ln(I0/It) and ln(I0/Iref) will be added to the output columns
 * /* w ww. jav a  2s.  c  o m*/
 * @param outputLogValues
 *            The outputLogValues to set.
 */

public void setOutputLogValues(boolean outputLogValues) {
    this.outputLogValues = outputLogValues;

    // adjust the extraNmaes and outputFormat arrays
    if (!configured) {
        return;
    }
    if (outputLogValues) {
        if (!ArrayUtils.contains(extraNames, LNI0IT_LABEL)) {
            extraNames = (String[]) ArrayUtils.add(extraNames, LNI0IT_LABEL);
            outputFormat = (String[]) ArrayUtils.add(outputFormat, "%.5f");
        }
        if (!ArrayUtils.contains(extraNames, LNITIREF_LABEL)) {
            extraNames = (String[]) ArrayUtils.add(extraNames, LNITIREF_LABEL);
            outputFormat = (String[]) ArrayUtils.add(outputFormat, "%.5f");
        }
    } else {
        int numInputs = inputNames.length;
        if (ArrayUtils.contains(extraNames, LNI0IT_LABEL)) {
            int index = ArrayUtils.indexOf(extraNames, LNI0IT_LABEL);
            extraNames = (String[]) ArrayUtils.remove(extraNames, index);
            outputFormat = (String[]) ArrayUtils.remove(outputFormat, index + numInputs);
        }
        if (ArrayUtils.contains(extraNames, LNITIREF_LABEL)) {
            int index = ArrayUtils.indexOf(extraNames, LNITIREF_LABEL);
            extraNames = (String[]) ArrayUtils.remove(extraNames, index);
            outputFormat = (String[]) ArrayUtils.remove(outputFormat, index + numInputs);
        }

    }
}