<commit_before>/*-
 *  * Copyright 2016 Skymind, Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 */

package org.datavec.api.transform.transform;

import org.datavec.api.transform.DataAction;
import org.datavec.api.transform.MathOp;
import org.datavec.api.transform.ReduceOp;
import org.datavec.api.transform.TransformProcess;
import org.datavec.api.transform.analysis.DataAnalysis;
import org.datavec.api.transform.analysis.columns.CategoricalAnalysis;
import org.datavec.api.transform.analysis.columns.DoubleAnalysis;
import org.datavec.api.transform.analysis.columns.StringAnalysis;
import org.datavec.api.transform.condition.ConditionOp;
import org.datavec.api.transform.condition.column.DoubleColumnCondition;
import org.datavec.api.transform.condition.column.NullWritableColumnCondition;
import org.datavec.api.transform.filter.ConditionFilter;
import org.datavec.api.transform.filter.FilterInvalidValues;
import org.datavec.api.transform.reduce.Reducer;
import org.datavec.api.transform.schema.Schema;
import org.datavec.api.transform.sequence.comparator.NumericalColumnComparator;
import org.datavec.api.transform.sequence.comparator.StringComparator;
import org.datavec.api.transform.sequence.split.SequenceSplitTimeSeparation;
import org.datavec.api.transform.sequence.window.OverlappingTimeWindowFunction;
import org.datavec.api.transform.transform.integer.ReplaceEmptyIntegerWithValueTransform;
import org.datavec.api.transform.transform.integer.ReplaceInvalidWithIntegerTransform;
import org.datavec.api.transform.transform.string.MapAllStringsExceptListTransform;
import org.datavec.api.transform.transform.string.ReplaceEmptyStringTransform;
import org.datavec.api.transform.transform.string.ReplaceStringTransform;
import org.datavec.api.transform.transform.string.StringListToCategoricalSetTransform;
import org.datavec.api.transform.transform.time.DeriveColumnsFromTimeTransform;
import org.datavec.api.writable.DoubleWritable;
import org.datavec.api.writable.comparator.LongWritableComparator;
import org.joda.time.DateTimeFieldType;
import org.joda.time.DateTimeZone;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;

/**
 * Created by Alex on 18/07/2016.
 */
public class TestJsonYaml {

    @Test
    public void testToFromJsonYaml() {

        Schema schema = new Schema.Builder().addColumnCategorical("Cat", "State1", "State2")
                        .addColumnCategorical("Cat2", "State1", "State2").addColumnDouble("Dbl")
                        .addColumnDouble("Dbl2", null, 100.0, true, false).addColumnInteger("Int")
                        .addColumnInteger("Int2", 0, 10).addColumnLong("Long").addColumnLong("Long2", -100L, null)
                        .addColumnString("Str").addColumnString("Str2", "someregexhere", 1, null)
                        .addColumnTime("TimeCol", DateTimeZone.UTC)
                        .addColumnTime("TimeCol2", DateTimeZone.UTC, null, 1000L).build();

        Map<String, String> map = new HashMap<>();
        map.put("from", "to");
        map.put("anotherFrom", "anotherTo");

        TransformProcess tp =
                        new TransformProcess.Builder(schema).categoricalToInteger("Cat").categoricalToOneHot("Cat2")
                                        .integerToCategorical("Cat", Arrays.asList("State1", "State2"))
                                        .stringToCategorical("Str", Arrays.asList("State1", "State2"))
                                        .duplicateColumn("Str", "Str2a").removeColumns("Str2a")
                                        .renameColumn("Str2", "Str2a").reorderColumns("Cat", "Dbl")
                                        .conditionalCopyValueTransform("Dbl", "Dbl2",
                                                        new DoubleColumnCondition("Dbl", ConditionOp.Equal, 0.0))
                                        .conditionalReplaceValueTransform("Dbl", new DoubleWritable(1.0),
                                                        new DoubleColumnCondition("Dbl", ConditionOp.Equal, 1.0))
                                        .doubleColumnsMathOp("NewDouble", MathOp.Add, "Dbl", "Dbl2")
                                        .doubleMathOp("Dbl", MathOp.Add, 1.0)
                                        .integerColumnsMathOp("NewInt", MathOp.Subtract, "Int", "Int2")
                                        .integerMathOp("Int", MathOp.Multiply, 2)
                                        .transform(new ReplaceEmptyIntegerWithValueTransform("Int", 1))
                                        .transform(new ReplaceInvalidWithIntegerTransform("Int", 1))
                                        .longColumnsMathOp("Long", MathOp.Multiply, "Long", "Long2")
                                        .longMathOp("Long", MathOp.ScalarMax, 0)
                                        .transform(new MapAllStringsExceptListTransform("Str", "Other",
                                                        Arrays.asList("Ok", "SomeVal")))
                                        .stringRemoveWhitespaceTransform("Str")
                                        .transform(new ReplaceEmptyStringTransform("Str", "WasEmpty"))
                                        .transform(new ReplaceStringTransform("Str", map))
                                        .transform(new StringListToCategoricalSetTransform("Str",
                                                        Arrays.asList("StrA", "StrB"), Arrays.asList("StrA", "StrB"),
                                                        ","))
                                        .stringMapTransform("Str2a", map)
                                        .transform(new DeriveColumnsFromTimeTransform.Builder("TimeCol")
                                                        .addIntegerDerivedColumn("Hour", DateTimeFieldType.hourOfDay())
                                                        .addStringDerivedColumn("Date", "YYYY-MM-dd", DateTimeZone.UTC)
                                                        .build())
                                        .stringToTimeTransform("Str2a", "YYYY-MM-dd hh:mm:ss", DateTimeZone.UTC)
                                        .timeMathOp("TimeCol2", MathOp.Add, 1, TimeUnit.HOURS)

                                        //Filters:
                                        .filter(new FilterInvalidValues("Cat", "Str2a"))
                                        .filter(new ConditionFilter(new NullWritableColumnCondition("Long")))

                                        //Convert to/from sequence
                                        .convertToSequence("Int", new NumericalColumnComparator("TimeCol2"))
                                        .convertFromSequence()

                                        //Sequence split
                                        .convertToSequence("Int", new StringComparator("Str2a"))
                                        .splitSequence(new SequenceSplitTimeSeparation("TimeCol2", 1, TimeUnit.HOURS))
<commit_msg>Update replace string transform call to use builder method.<commit_after>/*-
 *  * Copyright 2016 Skymind, Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 */

package org.datavec.api.transform.transform;

import org.datavec.api.transform.DataAction;
import org.datavec.api.transform.MathOp;
import org.datavec.api.transform.ReduceOp;
import org.datavec.api.transform.TransformProcess;
import org.datavec.api.transform.analysis.DataAnalysis;
import org.datavec.api.transform.analysis.columns.CategoricalAnalysis;
import org.datavec.api.transform.analysis.columns.DoubleAnalysis;
import org.datavec.api.transform.analysis.columns.StringAnalysis;
import org.datavec.api.transform.condition.ConditionOp;
import org.datavec.api.transform.condition.column.DoubleColumnCondition;
import org.datavec.api.transform.condition.column.NullWritableColumnCondition;
import org.datavec.api.transform.filter.ConditionFilter;
import org.datavec.api.transform.filter.FilterInvalidValues;
import org.datavec.api.transform.reduce.Reducer;
import org.datavec.api.transform.schema.Schema;
import org.datavec.api.transform.sequence.comparator.NumericalColumnComparator;
import org.datavec.api.transform.sequence.comparator.StringComparator;
import org.datavec.api.transform.sequence.split.SequenceSplitTimeSeparation;
import org.datavec.api.transform.sequence.window.OverlappingTimeWindowFunction;
import org.datavec.api.transform.transform.integer.ReplaceEmptyIntegerWithValueTransform;
import org.datavec.api.transform.transform.integer.ReplaceInvalidWithIntegerTransform;
import org.datavec.api.transform.transform.string.MapAllStringsExceptListTransform;
import org.datavec.api.transform.transform.string.ReplaceEmptyStringTransform;
import org.datavec.api.transform.transform.string.StringListToCategoricalSetTransform;
import org.datavec.api.transform.transform.time.DeriveColumnsFromTimeTransform;
import org.datavec.api.writable.DoubleWritable;
import org.datavec.api.writable.comparator.LongWritableComparator;
import org.joda.time.DateTimeFieldType;
import org.joda.time.DateTimeZone;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;

/**
 * Created by Alex on 18/07/2016.
 */
public class TestJsonYaml {

    @Test
    public void testToFromJsonYaml() {

        Schema schema = new Schema.Builder().addColumnCategorical("Cat", "State1", "State2")
                        .addColumnCategorical("Cat2", "State1", "State2").addColumnDouble("Dbl")
                        .addColumnDouble("Dbl2", null, 100.0, true, false).addColumnInteger("Int")
                        .addColumnInteger("Int2", 0, 10).addColumnLong("Long").addColumnLong("Long2", -100L, null)
                        .addColumnString("Str").addColumnString("Str2", "someregexhere", 1, null)
                        .addColumnTime("TimeCol", DateTimeZone.UTC)
                        .addColumnTime("TimeCol2", DateTimeZone.UTC, null, 1000L).build();

        Map<String, String> map = new HashMap<>();
        map.put("from", "to");
        map.put("anotherFrom", "anotherTo");

        TransformProcess tp =
                        new TransformProcess.Builder(schema).categoricalToInteger("Cat").categoricalToOneHot("Cat2")
                                        .integerToCategorical("Cat", Arrays.asList("State1", "State2"))
                                        .stringToCategorical("Str", Arrays.asList("State1", "State2"))
                                        .duplicateColumn("Str", "Str2a").removeColumns("Str2a")
                                        .renameColumn("Str2", "Str2a").reorderColumns("Cat", "Dbl")
                                        .conditionalCopyValueTransform("Dbl", "Dbl2",
                                                        new DoubleColumnCondition("Dbl", ConditionOp.Equal, 0.0))
                                        .conditionalReplaceValueTransform("Dbl", new DoubleWritable(1.0),
                                                        new DoubleColumnCondition("Dbl", ConditionOp.Equal, 1.0))
                                        .doubleColumnsMathOp("NewDouble", MathOp.Add, "Dbl", "Dbl2")
                                        .doubleMathOp("Dbl", MathOp.Add, 1.0)
                                        .integerColumnsMathOp("NewInt", MathOp.Subtract, "Int", "Int2")
                                        .integerMathOp("Int", MathOp.Multiply, 2)
                                        .transform(new ReplaceEmptyIntegerWithValueTransform("Int", 1))
                                        .transform(new ReplaceInvalidWithIntegerTransform("Int", 1))
                                        .longColumnsMathOp("Long", MathOp.Multiply, "Long", "Long2")
                                        .longMathOp("Long", MathOp.ScalarMax, 0)
                                        .transform(new MapAllStringsExceptListTransform("Str", "Other",
                                                        Arrays.asList("Ok", "SomeVal")))
                                        .stringRemoveWhitespaceTransform("Str")
                                        .transform(new ReplaceEmptyStringTransform("Str", "WasEmpty"))
                                        .replaceStringTransform("Str", map)
                                        .transform(new StringListToCategoricalSetTransform("Str",
                                                        Arrays.asList("StrA", "StrB"), Arrays.asList("StrA", "StrB"),
                                                        ","))
                                        .stringMapTransform("Str2a", map)
                                        .transform(new DeriveColumnsFromTimeTransform.Builder("TimeCol")
                                                        .addIntegerDerivedColumn("Hour", DateTimeFieldType.hourOfDay())
                                                        .addStringDerivedColumn("Date", "YYYY-MM-dd", DateTimeZone.UTC)
                                                        .build())
                                        .stringToTimeTransform("Str2a", "YYYY-MM-dd hh:mm:ss", DateTimeZone.UTC)
                                        .timeMathOp("TimeCol2", MathOp.Add, 1, TimeUnit.HOURS)

                                        //Filters:
                                        .filter(new FilterInvalidValues("Cat", "Str2a"))
                                        .filter(new ConditionFilter(new NullWritableColumnCondition("Long")))

                                        //Convert to/from sequence
                                        .convertToSequence("Int", new NumericalColumnComparator("TimeCol2"))
                                        .convertFromSequence()

                                        //Sequence split
                                        .convertToSequence("Int", new StringComparator("Str2a"))
                                        .splitSequence(new SequenceSplitTimeSeparation("TimeCol2", 1, TimeUnit.HOURS))

{'fraction_non_alphanumeric': 0.10287718386698708, 'fraction_numerical': 0.009742157563161654, 'mean_word_length': 1.887305456591037, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 96, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4498198', 'n_tokens_mistral': 3709, 'n_tokens_neox': 3540, 'n_words': 653}