|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.sysds.runtime.instructions.cp; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | + |
| 24 | +import org.apache.sysds.common.Builtins; |
| 25 | +import org.apache.sysds.common.Types.ValueType; |
| 26 | +import org.apache.sysds.runtime.DMLRuntimeException; |
| 27 | +import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; |
| 28 | +import org.apache.sysds.runtime.frame.data.FrameBlock; |
| 29 | +import org.apache.sysds.runtime.frame.data.columns.ColumnMetadata; |
| 30 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 31 | +import org.apache.sysds.runtime.matrix.operators.MultiThreadedOperator; |
| 32 | +import org.apache.sysds.runtime.transform.TfUtils.TfMethod; |
| 33 | +import org.apache.sysds.runtime.util.UtilFunctions; |
| 34 | +import org.apache.wink.json4j.JSONArray; |
| 35 | +import org.apache.wink.json4j.JSONObject; |
| 36 | + |
| 37 | +public class BinaryFrameScalarCPInstruction extends BinaryCPInstruction { |
| 38 | + // private static final Log LOG = LogFactory.getLog(BinaryFrameFrameCPInstruction.class.getName()); |
| 39 | + |
| 40 | + protected BinaryFrameScalarCPInstruction(MultiThreadedOperator op, CPOperand in1, CPOperand in2, CPOperand out, |
| 41 | + String opcode, String istr) { |
| 42 | + super(CPType.Binary, op, in1, in2, out, opcode, istr); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void processInstruction(ExecutionContext ec) { |
| 47 | + // get input frames |
| 48 | + FrameBlock inBlock1 = ec.getFrameInput(input1.getName()); |
| 49 | + ScalarObject spec = ec.getScalarInput(input2.getName(), ValueType.STRING, true); |
| 50 | + if(getOpcode().equals(Builtins.GET_CATEGORICAL_MASK.toString().toLowerCase())) { |
| 51 | + processGetCategorical(ec, inBlock1, spec); |
| 52 | + } |
| 53 | + else { |
| 54 | + throw new DMLRuntimeException("Unsupported operation"); |
| 55 | + } |
| 56 | + |
| 57 | + // Release the memory occupied by input frames |
| 58 | + ec.releaseFrameInput(input1.getName()); |
| 59 | + } |
| 60 | + |
| 61 | + public void processGetCategorical(ExecutionContext ec, FrameBlock f, ScalarObject spec) { |
| 62 | + try { |
| 63 | + |
| 64 | + // MatrixBlock ret = new MatrixBlock(); |
| 65 | + int nCol = f.getNumColumns(); |
| 66 | + |
| 67 | + // System.out.println(spec); |
| 68 | + JSONObject jSpec = new JSONObject(spec.getStringValue()); |
| 69 | + // System.out.println(jSpec); |
| 70 | + if(!jSpec.containsKey("ids") && jSpec.getBoolean("ids")) { |
| 71 | + throw new DMLRuntimeException("not supported non ID based spec for get_categorical_mask"); |
| 72 | + } |
| 73 | + |
| 74 | + String recode = TfMethod.RECODE.toString(); |
| 75 | + String dummycode = TfMethod.DUMMYCODE.toString(); |
| 76 | + String hashCode = TfMethod.HASH.toString(); |
| 77 | + |
| 78 | + System.out.println(jSpec.keySet()); |
| 79 | + |
| 80 | + int[] lengths = new int[nCol]; |
| 81 | + // assume all columns encode to at least one column. |
| 82 | + Arrays.fill(lengths, 1); |
| 83 | + boolean[] categorical = new boolean[nCol]; |
| 84 | + |
| 85 | + if(jSpec.containsKey(recode)) { |
| 86 | + JSONArray a = jSpec.getJSONArray(recode); |
| 87 | + for(Object aa : a) { |
| 88 | + int av = (Integer) aa - 1; |
| 89 | + categorical[av] = true; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if(jSpec.containsKey(dummycode)) { |
| 94 | + JSONArray a = jSpec.getJSONArray(dummycode); |
| 95 | + for(Object aa : a) { |
| 96 | + int av = (Integer) aa - 1; |
| 97 | + ColumnMetadata d = f.getColumnMetadata()[av]; |
| 98 | + String v = f.getString(0, av); |
| 99 | + int ndist; |
| 100 | + if(v.length() > 1 && v.charAt(0) == '¿') { |
| 101 | + ndist = UtilFunctions.parseToInt(v.substring(1)); |
| 102 | + } |
| 103 | + else { |
| 104 | + ndist = d.isDefault() ? 0 : (int) d.getNumDistinct(); |
| 105 | + } |
| 106 | + lengths[av] = ndist; |
| 107 | + categorical[av] = true; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // get total size after mapping |
| 112 | + |
| 113 | + int sumLengths = 0; |
| 114 | + for(int i : lengths) { |
| 115 | + sumLengths += i; |
| 116 | + } |
| 117 | + |
| 118 | + MatrixBlock ret = new MatrixBlock(1, sumLengths, false); |
| 119 | + ret.allocateDenseBlock(); |
| 120 | + int off = 0; |
| 121 | + for(int i = 0; i < lengths.length; i++) { |
| 122 | + for(int j = 0; j < lengths[i]; j++) { |
| 123 | + ret.set(0, off++, categorical[i] ? 1 : 0); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + ec.setMatrixOutput(output.getName(), ret); |
| 128 | + |
| 129 | + } |
| 130 | + catch(Exception e) { |
| 131 | + throw new DMLRuntimeException(e); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments