Skip to content

Commit

Permalink
data procsser for psd
Browse files Browse the repository at this point in the history
  • Loading branch information
SamedVossberg committed Nov 26, 2024
1 parent 70ede93 commit bd76651
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 150 deletions.
Binary file modified gui_dev/bun.lockb
Binary file not shown.
22 changes: 22 additions & 0 deletions gui_dev/data_processor/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions gui_dev/data_processor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ serde = { version = "1.0", features = ["derive"] }
serde_cbor = "0.11"
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
serde-wasm-bindgen = "0.6.5"
web-sys = {version = "0.3.72" , features = ["console"]}
console_error_panic_hook = "0.1"


[profile.release]
opt-level = "z" # Optimize for size (can be adjusted to 3 for speed)
lto = true # Enable Link-Time Optimization
codegen-units = 1 # Single codegen unit improves optimizations
lto = true
codegen-units = 1
77 changes: 73 additions & 4 deletions gui_dev/data_processor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,81 @@
use wasm_bindgen::prelude::*;
use serde_cbor::Value;
use serde_wasm_bindgen::to_value;
use serde::Serialize;
use std::collections::{BTreeMap, BTreeSet};

#[wasm_bindgen]
pub fn decode_cbor(data: &[u8]) -> JsValue {
match serde_cbor::from_slice::<Value>(data) {
Ok(value) => to_value(&value).unwrap_or(JsValue::NULL),
Err(_) => JsValue::NULL,
pub fn process_cbor_data(data: &[u8]) -> JsValue {
match serde_cbor::from_slice::<BTreeMap<String, Value>>(data) {
Ok(decoded_data) => {
let mut data_by_channel: BTreeMap<String, ChannelData> = BTreeMap::new();
let mut all_features_set: BTreeSet<u32> = BTreeSet::new();

for (key, value) in decoded_data {
let (channel_name, feature_name) = get_channel_and_feature(&key);

if channel_name.is_empty() {
continue;
}

if !feature_name.starts_with("fft_psd_") {
continue;
}

let feature_number = &feature_name["fft_psd_".len()..];
let feature_index = match feature_number.parse::<u32>() {
Ok(n) => n,
Err(_) => continue,
};

all_features_set.insert(feature_index);

let channel_data = data_by_channel
.entry(channel_name.clone())
.or_insert_with(|| ChannelData {
channel_name: channel_name.clone(),
feature_map: BTreeMap::new(),
});

channel_data.feature_map.insert(feature_index, value);
}

let all_features: Vec<u32> = all_features_set.into_iter().collect();

let result = ProcessedData {
data_by_channel,
all_features,
};

to_value(&result).unwrap_or(JsValue::NULL)
}
Err(e) => {
// Optionally log the error for debugging
JsValue::NULL
},
}
}

fn get_channel_and_feature(key: &str) -> (String, String) {
// Adjusted to split at the "_fft_psd_" pattern
let pattern = "_fft_psd_";
if let Some(pos) = key.find(pattern) {
let channel_name = &key[..pos];
let feature_name = &key[pos + 1..]; // Skip the underscore
(channel_name.to_string(), feature_name.to_string())
} else {
("".to_string(), key.to_string())
}
}

#[derive(Serialize)]
struct ChannelData {
channel_name: String,
feature_map: BTreeMap<u32, Value>,
}

#[derive(Serialize)]
struct ProcessedData {
data_by_channel: BTreeMap<String, ChannelData>,
all_features: Vec<u32>,
}
8 changes: 4 additions & 4 deletions gui_dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"preview": "vite preview"
},
"dependencies": {
"@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0",
"@emotion/react": "^11.13.5",
"@emotion/styled": "^11.13.5",
"@mui/icons-material": "latest",
"@mui/material": "latest",
"cbor-js": "^0.1.0",
Expand All @@ -29,11 +29,11 @@
"@babel/eslint-parser": "^7.25.9",
"@babel/preset-env": "^7.26.0",
"@babel/preset-react": "^7.25.9",
"@eslint/compat": "^1.2.2",
"@eslint/compat": "^1.2.3",
"@vitejs/plugin-react": "^4.3.3",
"@welldone-software/why-did-you-render": "^8.0.3",
"babel-plugin-react-compiler": "latest",
"eslint": "^9.14.0",
"eslint": "^9.15.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jsdoc": "^50.5.0",
"eslint-plugin-react": "^7.37.2",
Expand Down
Loading

0 comments on commit bd76651

Please sign in to comment.