IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / modules / ml / src / main / java / org / apache / ignite / ml / dataset / primitive / builder / data / SimpleDatasetDataBuilder.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.ignite.ml.dataset.primitive.builder.data;
19
20 import java.io.Serializable;
21 import java.util.Iterator;
22 import org.apache.ignite.ml.dataset.PartitionDataBuilder;
23 import org.apache.ignite.ml.dataset.UpstreamEntry;
24 import org.apache.ignite.ml.dataset.primitive.data.SimpleDatasetData;
25 import org.apache.ignite.ml.math.Vector;
26 import org.apache.ignite.ml.math.functions.IgniteBiFunction;
27
28 /**
29 * A partition {@code data} builder that makes {@link SimpleDatasetData}.
30 *
31 * @param <K> Type of a key in <tt>upstream</tt> data.
32 * @param <V> Type of a value in <tt>upstream</tt> data.
33 * @param <C> Type of a partition <tt>context</tt>.
34 */
35 public class SimpleDatasetDataBuilder<K, V, C extends Serializable>
36 implements PartitionDataBuilder<K, V, C, SimpleDatasetData> {
37 /** */
38 private static final long serialVersionUID = 756800193212149975L;
39
40 /** Function that extracts features from an {@code upstream} data. */
41 private final IgniteBiFunction<K, V, Vector> featureExtractor;
42
43 /**
44 * Construct a new instance of partition {@code data} builder that makes {@link SimpleDatasetData}.
45 *
46 * @param featureExtractor Function that extracts features from an {@code upstream} data.
47 */
48 public SimpleDatasetDataBuilder(IgniteBiFunction<K, V, Vector> featureExtractor) {
49 this.featureExtractor = featureExtractor;
50 }
51
52 /** {@inheritDoc} */
53 @Override public SimpleDatasetData build(Iterator<UpstreamEntry<K, V>> upstreamData, long upstreamDataSize, C ctx) {
54 // Prepares the matrix of features in flat column-major format.
55 int cols = -1;
56 double[] features = null;
57
58 int ptr = 0;
59 while (upstreamData.hasNext()) {
60 UpstreamEntry<K, V> entry = upstreamData.next();
61 Vector row = featureExtractor.apply(entry.getKey(), entry.getValue());
62
63 if (cols < 0) {
64 cols = row.size();
65 features = new double[Math.toIntExact(upstreamDataSize * cols)];
66 }
67 else
68 assert row.size() == cols : "Feature extractor must return exactly " + cols + " features";
69
70 for (int i = 0; i < cols; i++)
71 features[Math.toIntExact(i * upstreamDataSize + ptr)] = row.get(i);
72
73 ptr++;
74 }
75
76 return new SimpleDatasetData(features, Math.toIntExact(upstreamDataSize));
77 }
78 }