IGNITE-8907: [ML] Using vectors in featureExtractor
[ignite.git] / modules / ml / src / test / java / org / apache / ignite / ml / selection / scoring / cursor / CacheBasedLabelPairCursorTest.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.UUID;
21 import org.apache.ignite.Ignite;
22 import org.apache.ignite.IgniteCache;
23 import org.apache.ignite.internal.util.IgniteUtils;
24 import org.apache.ignite.ml.math.VectorUtils;
25 import org.apache.ignite.ml.selection.scoring.LabelPair;
26 import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
27
28 /**
29 * Tests for {@link CacheBasedLabelPairCursor}.
30 */
31 public class CacheBasedLabelPairCursorTest extends GridCommonAbstractTest {
32 /** Number of nodes in grid. */
33 private static final int NODE_COUNT = 4;
34
35 /** Ignite instance. */
36 private Ignite ignite;
37
38 /** {@inheritDoc} */
39 @Override protected void beforeTestsStarted() throws Exception {
40 for (int i = 1; i <= NODE_COUNT; i++)
41 startGrid(i);
42 }
43
44 /** {@inheritDoc} */
45 @Override protected void afterTestsStopped() {
46 stopAllGrids();
47 }
48
49 /** {@inheritDoc} */
50 @Override protected void beforeTest() {
51 /* Grid instance. */
52 ignite = grid(NODE_COUNT);
53 ignite.configuration().setPeerClassLoadingEnabled(true);
54 IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
55 }
56
57 /** */
58 public void testIterate() {
59 IgniteCache<Integer, Integer> data = ignite.createCache(UUID.randomUUID().toString());
60
61 for (int i = 0; i < 1000; i++)
62 data.put(i, i);
63
64 LabelPairCursor<Integer> cursor = new CacheBasedLabelPairCursor<>(
65 data,
66 (k, v) -> v % 2 == 0,
67 (k, v) -> VectorUtils.of(v),
68 (k, v) -> v,
69 vec -> (int)vec.get(0)
70 );
71
72 int cnt = 0;
73 for (LabelPair<Integer> e : cursor) {
74 assertEquals(e.getPrediction(), e.getTruth());
75 cnt++;
76 }
77 assertEquals(500, cnt);
78 }
79 }