Skip to content

Commit

Permalink
Applied Suggested Changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Modularius committed Nov 27, 2023
1 parent 41b7ec2 commit 6df618d
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 103 deletions.
4 changes: 2 additions & 2 deletions trace-to-events/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod pulse_detection;
use anyhow::Result;
use clap::Parser;
use kagiyama::{AlwaysReady, Watcher};
use parameters::{Mode, SaveOptions};
use rdkafka::{
consumer::{stream_consumer::StreamConsumer, CommitMode, Consumer},
message::Message,
Expand All @@ -15,7 +16,6 @@ use std::{net::SocketAddr, time::Duration};
use streaming_types::dat1_digitizer_analog_trace_v1_generated::{
digitizer_analog_trace_message_buffer_has_identifier, root_as_digitizer_analog_trace_message,
};
use parameters::{Mode, SaveOptions};

#[derive(Debug, Parser)]
#[clap(author, version, about)]
Expand Down Expand Up @@ -45,7 +45,7 @@ struct Cli {
save_file_name: Option<String>,

#[command(subcommand)]
pub mode: Mode,
pub(crate) mode: Mode,
}

#[tokio::main]
Expand Down
30 changes: 15 additions & 15 deletions trace-to-events/src/parameters.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;
use crate::pulse_detection::{detectors::threshold_detector::ThresholdDuration, Real};
use anyhow::{anyhow, Error};
use clap::{Parser, Subcommand};
use std::str::FromStr;

#[derive(Default, Debug, Clone)]
pub(crate) struct ThresholdDurationWrapper(pub(crate) ThresholdDuration);
Expand All @@ -26,62 +26,62 @@ impl FromStr for ThresholdDurationWrapper {
}

#[derive(Default, Debug, Clone, Parser)]
pub struct ConstantPhaseDiscriminatorParameters {
pub(crate) struct ConstantPhaseDiscriminatorParameters {
#[clap(
long,
help = "constant phase threshold for detecting muon events, use format (threshold,duration,cool_down). See README.md."
)]
pub threshold_trigger: ThresholdDurationWrapper,
pub(crate) threshold_trigger: ThresholdDurationWrapper,
}

pub struct SaveOptions<'a> {
pub save_path: &'a str,
pub file_name: &'a str,
pub(crate) struct SaveOptions<'a> {
pub(crate) save_path: &'a str,
pub(crate) file_name: &'a str,
}

#[derive(Default, Debug, Clone, Parser)]
pub struct AdvancedMuonDetectorParameters {
pub(crate) struct AdvancedMuonDetectorParameters {
#[clap(
long,
help = "Differential threshold for detecting muon onset (threshold,duration,cool_down). See README.md."
)]
pub muon_onset: ThresholdDurationWrapper,
pub(crate) muon_onset: ThresholdDurationWrapper,

#[clap(
long,
help = "Differential threshold for detecting muon peak (threshold,duration,cool_down). See README.md."
)]
pub muon_fall: ThresholdDurationWrapper,
pub(crate) muon_fall: ThresholdDurationWrapper,

#[clap(
long,
help = "Differential threshold for detecting muon termination (threshold,duration,cool_down). See README.md."
)]
pub muon_termination: ThresholdDurationWrapper,
pub(crate) muon_termination: ThresholdDurationWrapper,

#[clap(
long,
help = "Size of initial portion of the trace to use for determining the baseline. Initial portion should be event free."
)]
pub baseline_length: Option<usize>,
pub(crate) baseline_length: Option<usize>,

#[clap(
long,
help = "Size of the moving average window to use for the lopass filter."
)]
pub smoothing_window_size: Option<usize>,
pub(crate) smoothing_window_size: Option<usize>,

#[clap(
long,
help = "Optional parameter which (if set) filters out events whose peak is greater than the given value."
)]
pub max_amplitude: Option<Real>,
pub(crate) max_amplitude: Option<Real>,

#[clap(
long,
help = "Optional parameter which (if set) filters out events whose peak is less than the given value."
)]
pub min_amplitude: Option<Real>,
pub(crate) min_amplitude: Option<Real>,
}

#[derive(Subcommand, Debug)]
Expand Down
18 changes: 9 additions & 9 deletions trace-to-events/src/processing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
use crate::parameters::{
AdvancedMuonDetectorParameters, ConstantPhaseDiscriminatorParameters, Mode, SaveOptions,
};
use crate::pulse_detection::{
basic_muon_detector::{BasicMuonAssembler, BasicMuonDetector},
threshold_detector::{ThresholdAssembler, ThresholdDetector, UpperThreshold},
window::{Baseline, FiniteDifferences, SmoothingWindow, WindowFilter},
AssembleFilter, EventFilter, Real, SaveToFileFilter,
};
use common::{Channel, EventData, Intensity, Time};
use streaming_types::{
dat1_digitizer_analog_trace_v1_generated::{ChannelTrace, DigitizerAnalogTraceMessage},
Expand All @@ -8,15 +17,6 @@ use streaming_types::{
flatbuffers::FlatBufferBuilder,
frame_metadata_v1_generated::{FrameMetadataV1, FrameMetadataV1Args},
};
use crate::pulse_detection::{
basic_muon_detector::{BasicMuonAssembler, BasicMuonDetector},
threshold_detector::{ThresholdAssembler, ThresholdDetector, UpperThreshold},
window::{Baseline, FiniteDifferences, SmoothingWindow, WindowFilter},
AssembleFilter, EventFilter, Real, SaveToFileFilter,
};
use crate::parameters::{
AdvancedMuonDetectorParameters, ConstantPhaseDiscriminatorParameters, Mode, SaveOptions,
};

struct ChannnelEvents {
channel_number: Channel,
Expand Down
2 changes: 1 addition & 1 deletion trace-to-events/src/pulse_detection/datatype/eventpoint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt::Debug;
use super::{EventData, Temporal};
use std::fmt::Debug;

pub(crate) trait EventPoint: Debug + Clone {
type TimeType: Temporal;
Expand Down
6 changes: 2 additions & 4 deletions trace-to-events/src/pulse_detection/datatype/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::fmt::{Debug, Display};

use common::Intensity;

use super::Real;
use common::Intensity;
use std::fmt::{Debug, Display};

pub(crate) mod eventdata;
pub(crate) mod eventpoint;
Expand Down
2 changes: 1 addition & 1 deletion trace-to-events/src/pulse_detection/datatype/tracepoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ where
fn get_value(&self) -> &Self::ValueType {
&self.1
}

fn take_value(self) -> Self::ValueType {
self.1
}
Expand Down
24 changes: 10 additions & 14 deletions trace-to-events/src/pulse_detection/datatype/tracevalue.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::Real;
use std::{
fmt::{Debug, Display, Formatter, Result},
ops::{Index, IndexMut},
};
use super::Real;

/// An abstraction of the types that represent values processed by the various filters
/// This differs from the TracePoint type in that TracePoint must represent a time value,
Expand All @@ -14,7 +14,7 @@ use super::Real;
/// * Methods
/// - get_value(): returns an immutable reference to the value of the data point.
/// - take_value(): destructs the data point and gives the caller ownership of the value.
pub trait TraceValue: Default + Clone + Debug + Display {
pub(crate) trait TraceValue: Default + Clone + Debug + Display {
type ContentType: Default + Clone + Debug + Display;

fn get_value(&self) -> &Self::ContentType;
Expand All @@ -36,15 +36,15 @@ impl TraceValue for Real {
/// This type allows the use of static arrays of TraceValue types as TraceValues
/// that can be used in the pipeline.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TraceArray<const N: usize, T>(pub [T; N])
pub(crate) struct TraceArray<const N: usize, T>(pub(crate) [T; N])
where
T: TraceValue;

impl<const N: usize, T> TraceArray<N, T>
where
T: TraceValue,
{
pub fn new(value: [T; N]) -> Self {
pub(crate) fn new(value: [T; N]) -> Self {
Self(value)
}
}
Expand Down Expand Up @@ -104,14 +104,14 @@ impl<const N: usize, T: TraceValue + Copy> TraceValue for TraceArray<N, T> {
}

/// In practice arrays of Real types are mostly used.
pub type RealArray<const N: usize> = TraceArray<N, Real>;
pub(crate) type RealArray<const N: usize> = TraceArray<N, Real>;

/// This type allows contains descriptive statistical data.
#[derive(Default, Clone, Debug)]
pub struct Stats {
pub value: Real,
pub mean: Real,
pub variance: Real,
pub(crate) struct Stats {
pub(crate) value: Real,
pub(crate) mean: Real,
pub(crate) variance: Real,
}

impl From<Real> for Stats {
Expand All @@ -126,11 +126,7 @@ impl From<Real> for Stats {

impl Display for Stats {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(
f,
"({}, {}, {})",
self.value, self.mean, self.variance
)
write!(f, "({}, {}, {})", self.value, self.mean, self.variance)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::fmt::Display;

use super::{
threshold_detector::{LowerThreshold, ThresholdDetector, ThresholdDuration, UpperThreshold},
Assembler, Detector, EventPoint, TimeValue, EventData, Pulse, Real, RealArray
Assembler, Detector, EventData, EventPoint, Pulse, Real, RealArray, TimeValue,
};
use std::fmt::Display;

#[derive(Default, Debug, Clone, PartialEq)]
pub enum Class {
pub(crate) enum Class {
#[default]
Onset,
Peak,
Expand All @@ -31,13 +30,15 @@ pub(crate) struct Data {
}

impl Data {
pub fn get_class(&self) -> Class {
pub(crate) fn get_class(&self) -> Class {
self.class.clone()
}
pub fn get_value(&self) -> Real {

pub(crate) fn get_value(&self) -> Real {
self.value
}
pub fn get_superlative(&self) -> Option<TimeValue<RealArray<2>>> {

pub(crate) fn get_superlative(&self) -> Option<TimeValue<RealArray<2>>> {
self.superlative.clone()
}
}
Expand All @@ -61,6 +62,7 @@ impl SuperlativeValue {
value: Real::default(),
}
}

fn _from_max(time: Real) -> SuperlativeValue {
TimeValue {
time,
Expand All @@ -78,7 +80,7 @@ impl SuperlativeDiff {
value: RealArray::new([Real::default(), Real::default()]),
}
}

fn _from_max(time: Real) -> SuperlativeDiff {
TimeValue {
time,
Expand Down Expand Up @@ -137,7 +139,7 @@ pub(crate) struct BasicMuonDetector {
}

impl BasicMuonDetector {
pub fn new(
pub(crate) fn new(
onset: &ThresholdDuration,
fall: &ThresholdDuration,
termination: &ThresholdDuration,
Expand Down Expand Up @@ -243,7 +245,7 @@ enum AssemblerMode {
}

#[derive(Default, Clone)]
pub struct BasicMuonAssembler {
pub(crate) struct BasicMuonAssembler {
mode: AssemblerMode,
}

Expand Down Expand Up @@ -323,21 +325,11 @@ impl Assembler for BasicMuonAssembler {

#[cfg(test)]
mod tests {
//use itertools::Itertools;

use super::*;
use crate::pulse_detection::{
datatype::tracevalue::TraceArray, window::FiniteDifferences, EventFilter, WindowFilter,
};

//use crate::processing;
use super::*;

#[test]
fn zero_data() {

//assert!(results.is_empty());
}

#[test]
fn test_gate_zero_threshold() {
let data = [4, 3, 2, 5, 6, 1, 5, 7, 2, 4];
Expand Down
5 changes: 4 additions & 1 deletion trace-to-events/src/pulse_detection/detectors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
pub mod basic_muon_detector;
pub mod threshold_detector;

use super::{pulse::{TimeValue, TimeValueOptional}, Real, RealArray, TracePoint, EventData, EventPoint, Pulse};
use super::{
pulse::{TimeValue, TimeValueOptional},
EventData, EventPoint, Pulse, Real, RealArray, TracePoint,
};

pub(crate) trait Detector: Default + Clone {
type TracePointType: TracePoint;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
use super::{Assembler, Detector, EventData, EventPoint, Pulse, Real, TimeValueOptional};
use std::fmt::Display;
use std::marker::PhantomData;

use super::{Assembler, Detector, EventPoint};

use super::{
Pulse,
TimeValueOptional,
EventData, Real,
};

#[derive(Default, Debug, Clone, PartialEq)]
pub(crate) struct Data {}

Expand Down Expand Up @@ -112,9 +105,8 @@ impl<Class: ThresholdClass> Assembler for ThresholdAssembler<Class> {

#[cfg(test)]
mod tests {
use crate::pulse_detection::EventFilter;

use super::*;
use crate::pulse_detection::EventFilter;

#[test]
fn zero_data() {
Expand Down
5 changes: 3 additions & 2 deletions trace-to-events/src/pulse_detection/iterators/save_to_file.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use super::{Pulse, Temporal};
use std::{
env,
fmt::Display,
fs::{create_dir_all, File},
io::{Error, Write},
};

use super::{Pulse, Temporal};

fn create_file(folder: &str, name: &str) -> Result<File, Error> {
let cd = env::current_dir()?;
let path = cd.join(folder);
Expand All @@ -17,6 +16,7 @@ fn create_file(folder: &str, name: &str) -> Result<File, Error> {
pub(crate) trait SavablePoint {
fn write_to_file(&self, file: &mut File) -> Result<(), Error>;
}

impl<T, E> SavablePoint for (T, E)
where
T: Temporal,
Expand All @@ -26,6 +26,7 @@ where
writeln!(file, "{0},{1}", self.0, self.1)
}
}

impl SavablePoint for Pulse {
fn write_to_file(&self, file: &mut File) -> Result<(), Error> {
writeln!(file, "{0}", self)
Expand Down
Loading

0 comments on commit 6df618d

Please sign in to comment.