IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / modules / ml / src / main / java / org / apache / ignite / ml / selection / scoring / cursor / LocalLabelPairCursor.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.selection.scoring.cursor;
19
20 import java.util.Iterator;
21 import java.util.Map;
22 import java.util.NoSuchElementException;
23 import org.apache.ignite.lang.IgniteBiPredicate;
24 import org.apache.ignite.ml.Model;
25 import org.apache.ignite.ml.math.Vector;
26 import org.apache.ignite.ml.math.functions.IgniteBiFunction;
27 import org.apache.ignite.ml.selection.scoring.LabelPair;
28 import org.jetbrains.annotations.NotNull;
29
30 /**
31 * Truth with prediction cursor based on a locally stored data.
32 *
33 * @param <L> Type of a label (truth or prediction).
34 * @param <K> Type of a key in {@code upstream} data.
35 * @param <V> Type of a value in {@code upstream} data.
36 */
37 public class LocalLabelPairCursor<L, K, V, T> implements LabelPairCursor<L> {
38 /** Map with {@code upstream} data. */
39 private final Map<K, V> upstreamMap;
40
41 /** Filter for {@code upstream} data. */
42 private final IgniteBiPredicate<K, V> filter;
43
44 /** Feature extractor. */
45 private final IgniteBiFunction<K, V, Vector> featureExtractor;
46
47 /** Label extractor. */
48 private final IgniteBiFunction<K, V, L> lbExtractor;
49
50 /** Model for inference. */
51 private final Model<Vector, L> mdl;
52
53 /**
54 * Constructs a new instance of local truth with prediction cursor.
55 *
56 * @param upstreamMap Map with {@code upstream} data.
57 * @param filter Filter for {@code upstream} data.
58 * @param featureExtractor Feature extractor.
59 * @param lbExtractor Label extractor.
60 * @param mdl Model for inference.
61 */
62 public LocalLabelPairCursor(Map<K, V> upstreamMap, IgniteBiPredicate<K, V> filter,
63 IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, V, L> lbExtractor,
64 Model<Vector, L> mdl) {
65 this.upstreamMap = upstreamMap;
66 this.filter = filter;
67 this.featureExtractor = featureExtractor;
68 this.lbExtractor = lbExtractor;
69 this.mdl = mdl;
70 }
71
72 /** {@inheritDoc} */
73 @Override public void close() {
74 /* Do nothing. */
75 }
76
77 /** {@inheritDoc} */
78 @NotNull @Override public Iterator<LabelPair<L>> iterator() {
79 return new TruthWithPredictionIterator(upstreamMap.entrySet().iterator());
80 }
81
82 /**
83 * Util iterator that filters map entries and makes predictions using the model.
84 */
85 private class TruthWithPredictionIterator implements Iterator<LabelPair<L>> {
86 /** Base iterator. */
87 private final Iterator<Map.Entry<K, V>> iter;
88
89 /** Next found entry. */
90 private Map.Entry<K, V> nextEntry;
91
92 /**
93 * Constructs a new instance of truth with prediction iterator.
94 *
95 * @param iter Base iterator.
96 */
97 public TruthWithPredictionIterator(Iterator<Map.Entry<K, V>> iter) {
98 this.iter = iter;
99 }
100
101 /** {@inheritDoc} */
102 @Override public boolean hasNext() {
103 findNext();
104
105 return nextEntry != null;
106 }
107
108 /** {@inheritDoc} */
109 @Override public LabelPair<L> next() {
110 if (!hasNext())
111 throw new NoSuchElementException();
112
113 K key = nextEntry.getKey();
114 V val = nextEntry.getValue();
115
116 Vector features = featureExtractor.apply(key, val);
117 L lb = lbExtractor.apply(key, val);
118
119 nextEntry = null;
120
121 return new LabelPair<>(lb, mdl.apply(features));
122 }
123
124 /**
125 * Finds next entry using the specified filter.
126 */
127 private void findNext() {
128 while (nextEntry == null && iter.hasNext()) {
129 Map.Entry<K, V> entry = iter.next();
130
131 if (filter.apply(entry.getKey(), entry.getValue())) {
132 this.nextEntry = entry;
133 break;
134 }
135 }
136 }
137 }
138 }