phoenix_gui/
data.rs

1use crate::matrix::Matrix;
2
3static DATA_FILE: &str = include_str!("resources/data3.txt");
4
5/// * 0 - black
6/// * 1 - white
7/// * 2 - blue
8/// * 3 - green
9/// * 4 - yellow
10/// * 5 - red
11/// * 6 - nothing
12pub fn get_data() -> (Vec<Matrix>, Vec<Matrix>, f32) {
13    // format of data: 7x{ 100x{x, x, x, x} }
14    let mut input: Vec<Matrix> = vec![];
15    // these are just 7x1 matrices
16    // for the first 100 targets its [1, 0, 0, 0, 0, 0, 0]
17    // then for the next 100 its [0, 1, 0, 0, 0, 0, 0]
18    // etc
19    let mut targets: Vec<Matrix> = vec![];
20    let mut index = 0;
21    let data_string = DATA_FILE.replace('\n', "");
22    let data_string = data_string.replace(' ', "");
23    for _color_index in 0..7 {
24        // println!("color_index: {}", color_index);
25        // consume the first {
26        index = consume(&data_string[index..], '{') + index + 1;
27        for _ in 0..100 {
28            // println!("i: {}", i);
29            // println!("curr_code: {}", data_string[index..index + 10].to_string());
30            // consume the first {
31            index = consume(&data_string[index..], '{') + index + 1;
32            // println!("curr_code: {}", data_string[index..index + 10].to_string());
33            let mut matrix = Matrix::new(4, 1);
34            for row in 0..4 {
35                let col = 0;
36                let start = index;
37                index = consume(&data_string[index..], ',') + index;
38                let mut end = index;
39                if &data_string[end - 1..end] == "}" {
40                    end -= 1;
41                    index -= 2;
42                }
43                // println!("number: {}", data_string[start..end].to_string());
44                let value = data_string[start..end].parse::<f32>().unwrap();
45                matrix.set(row, col, value);
46                index += 1;
47            }
48            // println!("curr_code: {}", data_string[index..index + 10].to_string());
49            // consume the last }
50            index = consume(&data_string[index..], '}') + index + 1;
51            input.push(matrix);
52        }
53        // consume the last }
54        index = consume(&data_string[index..], '}') + index + 1;
55    }
56    let max_value = input
57        .iter()
58        .flat_map(|matrix| matrix.data.iter())
59        .fold(0.0, |acc, &x| if x > acc { x } else { acc });
60    // println!("max_value: {}", max_value);
61    for matrix in &mut input {
62        for i in 0..matrix.data.len() {
63            matrix.data[i] /= max_value / 2.0;
64            matrix.data[i] -= 1.0;
65        }
66    }
67    // create targets
68    for color_index in 0..7 {
69        for _ in 0..100 {
70            let mut matrix = Matrix::new(7, 1);
71            matrix.set(color_index, 0, 1.0);
72            targets.push(matrix);
73        }
74    }
75
76    (input, targets, max_value)
77}
78
79// returns the index of the next occurrence of c
80#[allow(dead_code)]
81fn consume(code: &str, c: char) -> usize {
82    for (index, char) in code.chars().enumerate() {
83        if char == c {
84            return index;
85        }
86    }
87    panic!("Could not find {} in {}", c, code);
88}