Skip to content

Commit

Permalink
Refactor datasets/observations on FE and BE, add new events, fix bugs...
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrej33 committed Oct 5, 2024
1 parent a0615bc commit 5675875
Show file tree
Hide file tree
Showing 23 changed files with 505 additions and 113 deletions.
3 changes: 3 additions & 0 deletions data/real_cases/arabidopsis_via_dataset.json
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,17 @@
"datasets": [
{
"id": "dataset_1",
"name": "dataset_1",
"observations": [
{
"id": "Observation1",
"name": "Observation1",
"dataset": "dataset_1",
"values": "100110011111001001000"
},
{
"id": "Observation2",
"name": "Observation2",
"dataset": "dataset_1",
"values": "011101100001110111111"
}
Expand Down
6 changes: 6 additions & 0 deletions data/simple_sketch.json
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,11 @@
"datasets": [
{
"id": "dataset1",
"name": "dataset1",
"observations": [
{
"id": "Observation1",
"name": "Observation1",
"dataset": "dataset1",
"values": "101010101"
}
Expand All @@ -313,19 +315,23 @@
},
{
"id": "dataset0",
"name": "dataset0",
"observations": [
{
"id": "Observation1",
"name": "Observation1",
"dataset": "dataset0",
"values": "101010101"
},
{
"id": "Observation2",
"name": "Observation2",
"dataset": "dataset0",
"values": "010101010"
},
{
"id": "Observation3",
"name": "Observation3",
"dataset": "dataset0",
"values": "101010101"
}
Expand Down
6 changes: 6 additions & 0 deletions data/test_data/test_model_with_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,17 @@
"datasets": [
{
"id": "data_mts",
"name": "data_mts",
"observations": [
{
"id": "abc",
"name": "abc",
"dataset": "data_mts",
"values": "111*"
},
{
"id": "ab",
"name": "ab",
"dataset": "data_mts",
"values": "11**"
}
Expand All @@ -166,14 +169,17 @@
},
{
"id": "data_fp",
"name": "data_fp",
"observations": [
{
"id": "ones",
"name": "ones",
"dataset": "data_fp",
"values": "1111"
},
{
"id": "zeros",
"name": "zeros",
"dataset": "data_fp",
"values": "0000"
}
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/algorithms/eval_dynamic/processed_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn process_dynamic_props(sketch: &Sketch) -> Result<Vec<ProcessedDynProp>, S
let observation = dataset.get_obs(obs_id)?.clone();
let var_names = dataset.variable_names();
let var_names_ref = var_names.iter().map(|v| v.as_str()).collect();
dataset = Dataset::new(vec![observation], var_names_ref)?;
dataset = Dataset::new("trap_space_data", vec![observation], var_names_ref)?;
}

ProcessedDynProp::mk_trap_space(
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/sketchbook/_tests_events/_observations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ fn prepare_dataset_3v_2o() -> Dataset {
let obs2 = Observation::try_from_str("000", "o2").unwrap();
let obs_list = vec![obs1, obs2];
let var_names = vec!["a", "b", "c"];
Dataset::new(obs_list.clone(), var_names.clone()).unwrap()
Dataset::new("dataset_3v_2o", obs_list.clone(), var_names.clone()).unwrap()
}

/// Prepare a simple dataset with 2 variables and 1 observation.
fn prepare_dataset_2v_1o() -> Dataset {
let obs1 = Observation::try_from_str("11", "o1").unwrap();
let obs_list = vec![obs1];
let var_names = vec!["v1", "v2"];
Dataset::new(obs_list.clone(), var_names.clone()).unwrap()
Dataset::new("dataset_2v_1o", obs_list.clone(), var_names.clone()).unwrap()
}

#[test]
Expand Down
10 changes: 7 additions & 3 deletions src-tauri/src/sketchbook/data_structs/_dataset_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ use serde::{Deserialize, Serialize};
/// instead of more complex typesafe structs) to allow for easier (de)serialization.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DatasetData {
pub name: String,
pub id: String,
pub observations: Vec<ObservationData>,
pub variables: Vec<String>,
}

/// Structure for sending *metadata* about `Dataset`. This includes id, variable names,
/// Structure for sending *metadata* about `Dataset`. This includes name, id, variable names,
/// but excludes all observations.
///
/// Some fields simplified compared to original typesafe versions (e.g., pure `Strings` are used
/// instead of more complex typesafe structs) to allow for easier (de)serialization.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DatasetMetaData {
pub name: String,
pub id: String,
pub variables: Vec<String>,
}
Expand All @@ -39,6 +41,7 @@ impl DatasetData {
.collect();
let variables = dataset.variables().iter().map(|v| v.to_string()).collect();
DatasetData {
name: dataset.get_name().to_string(),
id: id.to_string(),
observations,
variables,
Expand All @@ -54,7 +57,7 @@ impl DatasetData {
.map(|o| o.to_observation())
.collect::<Result<Vec<Observation>, String>>()?;
let variables = self.variables.iter().map(|v| v.as_str()).collect();
Dataset::new(observations, variables)
Dataset::new(self.name.as_str(), observations, variables)
}
}

Expand All @@ -63,6 +66,7 @@ impl DatasetMetaData {
pub fn from_dataset(id: &DatasetId, dataset: &Dataset) -> DatasetMetaData {
let variables = dataset.variables().iter().map(|v| v.to_string()).collect();
DatasetMetaData {
name: dataset.get_name().to_string(),
id: id.to_string(),
variables,
}
Expand All @@ -81,7 +85,7 @@ mod tests {
let dataset_id = DatasetId::new("d").unwrap();
let obs1 = Observation::try_from_str("*1", "o1").unwrap();
let obs2 = Observation::try_from_str("00", "o2").unwrap();
let dataset_before = Dataset::new(vec![obs1, obs2], vec!["a", "b"]).unwrap();
let dataset_before = Dataset::new("d", vec![obs1, obs2], vec!["a", "b"]).unwrap();
let dataset_data = DatasetData::from_dataset(&dataset_id, &dataset_before);
let dataset_after = dataset_data.to_dataset().unwrap();

Expand Down
7 changes: 5 additions & 2 deletions src-tauri/src/sketchbook/data_structs/_observation_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ObservationData {
pub id: String,
pub name: String,
pub dataset: String,
pub values: String,
}
Expand All @@ -19,9 +20,10 @@ impl<'de> JsonSerde<'de> for ObservationData {}

impl ObservationData {
/// Create new `ObservationData` instance given `id` and values string slices.
pub fn new(obs_id: &str, dataset_id: &str, values: &str) -> ObservationData {
pub fn new(obs_id: &str, name: &str, dataset_id: &str, values: &str) -> ObservationData {
ObservationData {
id: obs_id.to_string(),
name: name.to_string(),
dataset: dataset_id.to_string(),
values: values.to_string(),
}
Expand All @@ -32,6 +34,7 @@ impl ObservationData {
pub fn from_obs(obs: &Observation, dataset_id: &DatasetId) -> ObservationData {
ObservationData::new(
obs.get_id().as_str(),
obs.get_name(),
dataset_id.as_str(),
&obs.to_values_string(),
)
Expand All @@ -40,7 +43,7 @@ impl ObservationData {
/// Extract the corresponding `Observation` from the `ObservationData`.
/// There is a syntax check just to make sure that the data are valid.
pub fn to_observation(&self) -> Result<Observation, String> {
Observation::try_from_str(&self.values.clone(), &self.id)
Observation::try_from_str_named(&self.values.clone(), &self.id, &self.name)
}
}

Expand Down
Loading

0 comments on commit 5675875

Please sign in to comment.