Skip to content

Commit

Permalink
Remove unused discrete domain
Browse files Browse the repository at this point in the history
  • Loading branch information
aleics committed Oct 1, 2024
1 parent 0518f3a commit 9842d05
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 126 deletions.
36 changes: 2 additions & 34 deletions bruc-core/src/graph/node/axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
scene::{SceneAxisRule, SceneAxisTick, SceneItem},
spec::axis::{Axis, AxisOrientation},
};
use bruc_expression::data::DataItem;

use super::shape::SceneWindow;

Expand All @@ -25,27 +24,11 @@ impl AxisOperator {
}

fn apply_interval(&self, domain: (f32, f32)) -> SinglePulse {
let scene_item = self.linear_axis(domain);
let scene_item = self.create_axis(domain);
SinglePulse::Shapes(vec![scene_item])
}

fn apply_discrete(&self, domain: (f32, f32), values: Vec<DataItem>) -> SinglePulse {
let mut points = Vec::new();

for value in values {
if let Some(num) = value.get_number().copied() {
points.push(num);
}
}

if points.is_empty() {
return SinglePulse::Shapes(Vec::new());
}

SinglePulse::Shapes(vec![self.discrete_axis(points, domain)])
}

fn linear_axis(&self, domain: (f32, f32)) -> SceneItem {
fn create_axis(&self, domain: (f32, f32)) -> SceneItem {
let ticks = self.scale.ticks(domain);
SceneItem::axis(
self.create_ruler(),
Expand All @@ -54,20 +37,6 @@ impl AxisOperator {
)
}

fn discrete_axis(&self, points: Vec<f32>, domain: (f32, f32)) -> SceneItem {
let range = self.scale.range();

let step = (range.1 - range.0) / (points.len() as f32);
let padding = step / 2.0;
let scale = Scale::linear((range.0 + padding, range.1 - padding));

SceneItem::axis(
self.create_ruler(),
self.create_ticks(Vec::new()),
self.axis.orientation,
)
}

fn create_ticks(&self, ticks: Vec<(f32, f32)>) -> Vec<SceneAxisTick> {
ticks
.into_iter()
Expand Down Expand Up @@ -107,7 +76,6 @@ impl Evaluation for AxisOperator {

let pulse = match domain {
ResolvedDomain::Interval(_, _) => self.apply_interval(interval),
ResolvedDomain::Discrete { values } => self.apply_discrete(interval, values),
};

Pulse::Single(pulse)
Expand Down
8 changes: 1 addition & 7 deletions bruc-core/src/graph/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{

use self::axis::AxisOperator;
use self::data::ConstantOperator;
use self::scale::{BandOperator, DomainDiscreteOperator, DomainIntervalOperator};
use self::scale::{BandOperator, DomainIntervalOperator};
use self::shape::BarOperator;
use self::transform::{FilterOperator, MapOperator};
use self::{data::DataOperator, transform::GroupOperator};
Expand Down Expand Up @@ -67,7 +67,6 @@ pub enum Operator {
Pie(PieOperator),
Axis(AxisOperator),
DomainInterval(DomainIntervalOperator),
DomainDiscrete(DomainDiscreteOperator),
Linear(LinearOperator),
Log(LogOperator),
Band(BandOperator),
Expand Down Expand Up @@ -153,10 +152,6 @@ impl Operator {
Operator::DomainInterval(DomainIntervalOperator::new(domain))
}

pub(crate) fn domain_discrete(domain: Domain) -> Self {
Operator::DomainDiscrete(DomainDiscreteOperator::new(domain))
}

/// Evaluate the operator for a certain `Pulse`.
pub async fn evaluate(&self, pulse: Pulse) -> Pulse {
match self {
Expand All @@ -170,7 +165,6 @@ impl Operator {
Operator::Pie(pie) => pie.evaluate(pulse).await,
Operator::Axis(axis) => axis.evaluate(pulse).await,
Operator::DomainInterval(domain_interval) => domain_interval.evaluate(pulse).await,
Operator::DomainDiscrete(domain_discrete) => domain_discrete.evaluate(pulse).await,
Operator::Linear(linear) => linear.evaluate(pulse).await,
Operator::Log(log) => log.evaluate(pulse).await,
Operator::Band(band) => band.evaluate(pulse).await,
Expand Down
61 changes: 0 additions & 61 deletions bruc-core/src/graph/node/scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,51 +68,6 @@ impl Evaluation for DomainIntervalOperator {
}
}

#[derive(Debug, PartialEq)]
pub struct DomainDiscreteOperator {
domain: Domain,
}

impl DomainDiscreteOperator {
pub(crate) fn new(domain: Domain) -> Self {
DomainDiscreteOperator { domain }
}

fn resolve_domain(&self, values: &[DataValue]) -> Vec<DataItem> {
match &self.domain {
Domain::Literal(values) => values
.iter()
.map(|value| DataItem::Number(*value))
.collect(),
Domain::DataField { field, .. } => values
.iter()
.flat_map(|value| value.get(field))
.cloned()
.collect(),
}
}

fn apply(&self, pulse: &SinglePulse) -> Vec<DataItem> {
let SinglePulse::Data(values) = pulse else {
return Vec::new();
};

self.resolve_domain(values)
}
}

impl Evaluation for DomainDiscreteOperator {
async fn evaluate_single(&self, single: SinglePulse) -> Pulse {
Pulse::domain(ResolvedDomain::Discrete {
values: self.apply(&single),
})
}

async fn evaluate_multi(&self, multi: MultiPulse) -> Pulse {
self.evaluate_single(multi.aggregate()).await
}
}

/// `LinearOperator` represents an operator of the graph, which linearly scales data values from a
/// certain `field` reference, and creates a new field in the defined `output` field.
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -532,22 +487,6 @@ mod tests {
)
}

#[tokio::test]
async fn band_handles_empty_domain() {
let data = SinglePulse::Data(vec![
DataValue::from_pairs(vec![("a", 0.0.into()), ("b", 5.0.into())]),
DataValue::from_pairs(vec![("a", 1.0.into()), ("b", 2.0.into())]),
]);
let domain = SinglePulse::Domain(ResolvedDomain::Discrete { values: Vec::new() });

let operator = BandOperator::new((0.0, 1.0), "a", "x");
let pulse = operator
.evaluate(Pulse::multi(vec![data.clone(), domain]))
.await;

assert_eq!(pulse, Pulse::Single(data))
}

#[tokio::test]
async fn log_applies_multi_pulse() {
let first_pulse = SinglePulse::Data(vec![
Expand Down
24 changes: 0 additions & 24 deletions bruc-core/src/graph/pulse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use bruc_expression::data::DataItem;

use crate::{data::DataValue, scene::SceneItem};

/// `Pulse` represents the current state of a node in the graph for a certain evaluation.
Expand Down Expand Up @@ -132,34 +130,12 @@ impl MultiPulse {
#[derive(Debug, Clone, PartialEq)]
pub enum ResolvedDomain {
Interval(f32, f32),
Discrete { values: Vec<DataItem> },
}

impl ResolvedDomain {
pub(crate) fn interval(&self) -> Option<(f32, f32)> {
match self {
ResolvedDomain::Interval(min, max) => Some((*min, *max)),
ResolvedDomain::Discrete { values, .. } => {
if values.is_empty() {
return None;
}

let mut domain = (f32::MAX, 0.0);

for value in values {
if let Some(num) = value.get_number().copied() {
if num < domain.0 {
domain.0 = num;
}

if num > domain.1 {
domain.1 = num;
}
}
}

Some(domain)
}
}
}
}

0 comments on commit 9842d05

Please sign in to comment.