Skip to content

Commit

Permalink
demo working
Browse files Browse the repository at this point in the history
  • Loading branch information
xasopheno committed Mar 8, 2023
1 parent 1ae51d4 commit 5de61be
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 221 deletions.
2 changes: 1 addition & 1 deletion ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum Op {
//
CSV1d {
path: String,
scales: Vec<Scale>,
scale: Option<Rational64>,
},
CSV2d {
path: String,
Expand Down
4 changes: 2 additions & 2 deletions ast/src/datagen/csv1d_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod eeg_test {

use crate::{
datagen::mod_1d::{
csv_to_normalform, eeg_data_to_normal_form, eeg_datum_to_point_op, CsvData,
csv1d_to_normalform, eeg_data_to_normal_form, eeg_datum_to_point_op, CsvData,
},
NameSet, NormalForm, PointOp,
};
Expand Down Expand Up @@ -57,7 +57,7 @@ mod eeg_test {
}
#[test]
fn test_csv_to_normalform() {
let result = csv_to_normalform(
let result = csv1d_to_normalform(
"./src/datagen/test_data.csv",
Some(Rational64::new(200_000_000_000_000, 1)),
)
Expand Down
67 changes: 26 additions & 41 deletions ast/src/datagen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use std::str::FromStr;
use std::{fs::File, path::Path};
use weresocool_error::Error;
use weresocool_ring_buffer::RingBuffer;
use weresocool_shared::helpers::r_to_f32;
mod csv1d_test;
mod csv2d_test;
mod mod_1d;
pub mod mod_1d;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Copy)]
pub struct Point {
Expand All @@ -33,20 +32,6 @@ impl Scale {
}
}

pub fn csv1d_to_normalform(filename: &str, scales: Vec<Scale>) -> Result<Term, Error> {
let data = get_data1d(filename.into(), scales[1].value)?;
let path = Path::new(&filename);
Ok(csv_data_to_normal_form(
&data,
scales,
path.file_name()
.unwrap()
.to_string_lossy()
.to_string()
.as_str(),
))
}

pub fn csv2d_to_normalform(filename: &str, scales: Vec<Scale>) -> Result<Term, Error> {
let data = get_data2d(filename.into())?;
let path = Path::new(&filename);
Expand Down Expand Up @@ -125,31 +110,31 @@ fn point_to_point_op(
})
}

fn get_data1d(filename: String, length: Rational64) -> Result<Vec<Vec<f32>>, Error> {
let length = r_to_f32(length);
let path = Path::new(&filename);
let cwd = std::env::current_dir()?;
let file = File::open(path).unwrap_or_else(|_| {
panic!(
"unable to read file: {}. current working directory is: {}",
path.display(),
cwd.display()
)
});

let mut rdr = csv::ReaderBuilder::new()
.has_headers(false)
.delimiter(b',')
.from_reader(file);

let deserialized: Vec<Vec<f32>> = rdr
.deserialize::<Vec<f32>>()
.map(|datum| datum.expect("Error deserializing datum"))
.collect();
let result: Vec<Vec<f32>> = deserialized[0].iter().map(|v| vec![*v, length]).collect();

Ok(result)
}
// fn get_data1d(filename: String, length: Rational64) -> Result<Vec<Vec<f32>>, Error> {
// let length = r_to_f32(length);
// let path = Path::new(&filename);
// let cwd = std::env::current_dir()?;
// let file = File::open(path).unwrap_or_else(|_| {
// panic!(
// "unable to read file: {}. current working directory is: {}",
// path.display(),
// cwd.display()
// )
// });

// let mut rdr = csv::ReaderBuilder::new()
// .has_headers(false)
// .delimiter(b',')
// .from_reader(file);

// let deserialized: Vec<Vec<f32>> = rdr
// .deserialize::<Vec<f32>>()
// .map(|datum| datum.expect("Error deserializing datum"))
// .collect();
// let result: Vec<Vec<f32>> = deserialized[0].iter().map(|v| vec![*v, length]).collect();

// Ok(result)
// }

fn get_data2d(filename: String) -> Result<Vec<Vec<f32>>, Error> {
let path = Path::new(&filename);
Expand Down
2 changes: 1 addition & 1 deletion ast/src/datagen/mod_1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct CsvData {
pub data: Vec<f32>,
}

pub fn csv_to_normalform(filename: &str, scale: Option<Rational64>) -> Result<NormalForm, Error> {
pub fn csv1d_to_normalform(filename: &str, scale: Option<Rational64>) -> Result<NormalForm, Error> {
let data = get_data(filename.into())?;
let path = Path::new(&filename);
Ok(vec_eeg_data_to_normal_form(
Expand Down
7 changes: 4 additions & 3 deletions ast/src/operations/normalize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::datagen::{csv1d_to_normalform, csv2d_to_normalform};
use crate::datagen::{csv2d_to_normalform, mod_1d::csv1d_to_normalform};
use crate::operations::Rational64;
use crate::operations::{
helpers::*, substitute::insert_function_args, GetLengthRatio, NormalForm, Normalize, Substitute,
Expand Down Expand Up @@ -35,8 +35,9 @@ impl Normalize<Term> for Op {
Op::Id(id) => {
handle_id_error(id, defs)?.apply_to_normal_form(input, defs)?;
}
Op::CSV1d { path, scales } => {
csv1d_to_normalform(path, scales.clone())?.apply_to_normal_form(input, defs)?;

Op::CSV1d { path, scale } => {
csv1d_to_normalform(path, *scale)?.apply_to_normal_form(input, defs)?;
}

Op::CSV2d { path, scales } => {
Expand Down
10 changes: 7 additions & 3 deletions mocks/data/csv1d.socool
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@


main = {
Csv1d(f: 2, l: 1/2) ./mocks/data/data1d.csv
Csv1d(2.0) ./mocks/data/data1d.csv
}

expect = {
-- remember there is a ring buffer of 50 samples involved here :)
Seq [
Fa 2/1, Fa 4/1, Fa 6/1
] | Lm 1/2
Fa 2/1, Fa 3/1, Fa 4/1
]
| Lm 1/50
| #data1d.csv

}
3 changes: 2 additions & 1 deletion parser/src/socool.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ Scale: Scale = {
}

BaseOperation: Term = {
"Csv1d" <scales: Parenthesized<Comma<Scale>>> <path: Import> => Term::Op(CSV1d { path, scales }),

"Csv1d" <scale: Parenthesized<Rational>?> <path: Import> => Term::Op(CSV1d { path, scale }),
"Csv2d" <scales: Parenthesized<Comma<Scale>>> <path: Import> => Term::Op(CSV2d { path, scales }),
//
r"\\|Lambda" <input_name: Parenthesized<Name>?> "{" <term: Operation> "}" => Term::Op(Lambda { term: Box::new(term),
Expand Down
Loading

0 comments on commit 5de61be

Please sign in to comment.