-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathDataConversionHelper.java
270 lines (244 loc) · 11.3 KB
/
DataConversionHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 com.amazonaws.sagemaker.helper;
import com.amazonaws.sagemaker.dto.DataSchema;
import com.amazonaws.sagemaker.dto.ColumnSchema;
import com.amazonaws.sagemaker.type.BasicDataType;
import com.amazonaws.sagemaker.type.DataStructureType;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.stream.Collectors;
import ml.combust.mleap.core.types.BasicType;
import ml.combust.mleap.core.types.DataType;
import ml.combust.mleap.core.types.ListType;
import ml.combust.mleap.core.types.ScalarType;
import ml.combust.mleap.core.types.StructField;
import ml.combust.mleap.core.types.StructType;
import ml.combust.mleap.core.types.TensorType;
import ml.combust.mleap.runtime.frame.ArrayRow;
import ml.combust.mleap.runtime.frame.DefaultLeapFrame;
import ml.combust.mleap.runtime.frame.Row;
import ml.combust.mleap.runtime.javadsl.LeapFrameBuilder;
import ml.combust.mleap.runtime.javadsl.LeapFrameBuilderSupport;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Converter class to convert data between input to MLeap expected types and convert back MLeap helper to Java types
* for output.
*/
@Component
public class DataConversionHelper {
private final LeapFrameBuilderSupport support;
private final LeapFrameBuilder leapFrameBuilder;
@Autowired
public DataConversionHelper(final LeapFrameBuilderSupport support, final LeapFrameBuilder leapFrameBuilder) {
this.support = Preconditions.checkNotNull(support);
this.leapFrameBuilder = Preconditions.checkNotNull(leapFrameBuilder);
}
/**
* Parses the input payload in CSV format to a list of Objects
* @param csvInput, the input received from the request in CSV format
* @param schema, the data schema retrieved from environment variable
* @return List of Objects, where each Object correspond to one feature of the input data
* @throws IOException, if there is an exception thrown in the try-with-resources block
*/
public List<List<Object>> convertCsvToObjectList(final String csvInput, final DataSchema schema) throws IOException {
try (final StringReader sr = new StringReader(csvInput)) {
final CSVParser parser = CSVFormat.DEFAULT.parse(sr);
// We don not supporting multiple CSV lines as input currently
final List<CSVRecord> records = parser.getRecords();
final int inputLength = schema.getInput().size();
final List<List<Object>> returnList = Lists.newArrayList();
for(CSVRecord record : records) {
final List<Object> valueList = Lists.newArrayList();
for (int idx = 0; idx < inputLength; ++idx) {
ColumnSchema sc = schema.getInput().get(idx);
// For CSV input, each value is treated as an individual feature by default
valueList.add(this.convertInputDataToJavaType(sc.getType(), DataStructureType.BASIC, record.get(idx)));
}
returnList.add(valueList);
}
return returnList;
}
}
/**
* Convert input object to DefaultLeapFrame
*
* @param schema, the input schema received from request or environment variable
* @param datas , the input datas received from request as a list of objects
* @return the DefaultLeapFrame object which MLeap transformer expects
*/
public DefaultLeapFrame convertInputToLeapFrame(final DataSchema schema, final List<List<Object>> datas) {
final int inputLength = schema.getInput().size();
final List<StructField> structFieldList = Lists.newArrayList();
for (int idx = 0; idx < inputLength; ++idx) {
ColumnSchema sc = schema.getInput().get(idx);
structFieldList
.add(new StructField(sc.getName(), this.convertInputToMLeapInputType(sc.getType(), sc.getStruct())));
}
final StructType mleapSchema = leapFrameBuilder.createSchema(structFieldList);
final List<Row> rows = Lists.newArrayList();
for(Object data : datas)
{
final Row currentRow = getRow(schema, (List) data, inputLength);
rows.add(currentRow);
}
return leapFrameBuilder.createFrame(mleapSchema, rows);
}
private Row getRow(DataSchema schema, List<Object> data, int inputLength) {
final List<Object> valueList = Lists.newArrayList();
for (int idx = 0; idx < inputLength; ++idx) {
ColumnSchema sc = schema.getInput().get(idx);
valueList.add(this.convertInputDataToJavaType(sc.getType(), sc.getStruct(), data.get(idx)));
}
return support.createRowFromIterable(valueList);
}
/**
* Convert basic types in the MLeap helper to Java types for output.
*
* @param predictionRow, the ArrayRow from MLeapResponse
* @param type, the basic type to which the helper should be casted, provided by user via input
* @return the proper Java type
*/
public Object convertMLeapBasicTypeToJavaType(final Row predictionRow, final String type) {
switch (type) {
case BasicDataType.INTEGER:
return predictionRow.getInt(0);
case BasicDataType.LONG:
return predictionRow.getLong(0);
case BasicDataType.FLOAT:
case BasicDataType.DOUBLE:
return predictionRow.getDouble(0);
case BasicDataType.BOOLEAN:
return predictionRow.getBool(0);
case BasicDataType.BYTE:
return predictionRow.getByte(0);
case BasicDataType.SHORT:
return predictionRow.getShort(0);
case BasicDataType.STRING:
return predictionRow.getString(0);
default:
throw new IllegalArgumentException("Given type is not supported");
}
}
@SuppressWarnings("unchecked")
@VisibleForTesting
protected Object convertInputDataToJavaType(final String type, final String structure, final Object value) {
if (StringUtils.isBlank(structure) || StringUtils.equals(structure, DataStructureType.BASIC)) {
switch (type) {
case BasicDataType.INTEGER:
return new Integer(value.toString());
case BasicDataType.LONG:
return new Long(value.toString());
case BasicDataType.FLOAT:
return new Float(value.toString());
case BasicDataType.DOUBLE:
return new Double(value.toString());
case BasicDataType.BOOLEAN:
return Boolean.valueOf(value.toString());
case BasicDataType.BYTE:
return new Byte(value.toString());
case BasicDataType.SHORT:
return new Short(value.toString());
case BasicDataType.STRING:
return value.toString();
default:
throw new IllegalArgumentException("Given type is not supported");
}
} else {
List<Object> listOfObjects;
try {
listOfObjects = (List<Object>) value;
} catch (ClassCastException cce) {
throw new IllegalArgumentException("Input val is not a list but struct passed is vector or array");
}
switch (type) {
case BasicDataType.INTEGER:
return listOfObjects.stream().map(elem -> (Integer) elem).collect(Collectors.toList());
case BasicDataType.LONG:
return listOfObjects.stream().map(elem -> (Long) elem).collect(Collectors.toList());
case BasicDataType.FLOAT:
case BasicDataType.DOUBLE:
return listOfObjects.stream().map(elem -> (Double) elem).collect(Collectors.toList());
case BasicDataType.BOOLEAN:
return listOfObjects.stream().map(elem -> (Boolean) elem).collect(Collectors.toList());
case BasicDataType.BYTE:
return listOfObjects.stream().map(elem -> (Byte) elem).collect(Collectors.toList());
case BasicDataType.SHORT:
return listOfObjects.stream().map(elem -> (Short) elem).collect(Collectors.toList());
case BasicDataType.STRING:
return listOfObjects.stream().map(elem -> (String) elem).collect(Collectors.toList());
default:
throw new IllegalArgumentException("Given type is not supported");
}
}
}
@VisibleForTesting
protected DataType convertInputToMLeapInputType(final String type, final String structure) {
BasicType basicType;
switch (type) {
case BasicDataType.INTEGER:
basicType = support.createInt();
break;
case BasicDataType.LONG:
basicType = support.createLong();
break;
case BasicDataType.FLOAT:
basicType = support.createFloat();
break;
case BasicDataType.DOUBLE:
basicType = support.createDouble();
break;
case BasicDataType.BOOLEAN:
basicType = support.createBoolean();
break;
case BasicDataType.BYTE:
basicType = support.createByte();
break;
case BasicDataType.SHORT:
basicType = support.createShort();
break;
case BasicDataType.STRING:
basicType = support.createString();
break;
default:
basicType = null;
}
if (basicType == null) {
throw new IllegalArgumentException("Data type passed in the request is wrong for one or more columns");
}
if (StringUtils.isNotBlank(structure)) {
switch (structure) {
case DataStructureType.VECTOR:
return new TensorType(basicType, true);
case DataStructureType.ARRAY:
return new ListType(basicType, true);
case DataStructureType.BASIC:
return new ScalarType(basicType, true);
}
}
// if structure field is not passed, it is by default basic
return new ScalarType(basicType, true);
}
}