Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 67 additions & 10 deletions src/batch/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::stats::Histogram;
use anyhow::{Error, Result};
use ndarray::Array1;
use numpy::{PyArray3, ToPyArray};
use pyo3::prelude::{pyclass, pymethods, Borrowed, Bound, FromPyObject, PyAny};
use pyo3::types::{PyInt, PyString};
use pyo3::prelude::{pyclass, pymethods, Borrowed, Bound, FromPyObject, PyAny, PyErr, Python};
use pyo3::types::{PyAnyMethods, PyDict, PyInt, PyModule, PyString};

pub type PyHist<'py> = Bound<'py, PyArray3<i32>>;

Expand Down Expand Up @@ -141,6 +141,11 @@ impl BatchData {
self.data_changed = vec![true; self.n_batches()]
}

/// Check if any data has changed since you last ran a calculation.
pub fn data_changed(&mut self) -> bool {
self.data_changed.contains(&true)
}

/// Set histogram settings for one or all filter sets.
///
/// Parameters
Expand Down Expand Up @@ -494,6 +499,20 @@ impl BatchData {
self.combinations(other)
}

/// Clear all filters.
///
/// Parameters
/// ----------
/// index: int | str
/// Either 'all', or the index of the filter set to modify.
pub fn clear_filters(&mut self, index: FilterIndex) -> Result<()> {
for i in self.resolve_indices(&index)? {
self.filters[i] = Filters::new();
self.data_changed[i] = true;
}
Ok(())
}

/// Save a filter set's result to a file.
///
/// Parameters
Expand Down Expand Up @@ -629,6 +648,35 @@ impl BatchData {
.collect())
}

/// Get the filter and histogram settings data as a dictionary.
///
/// Note this function is intended for the GUI frontend and is not stable.
Comment thread
AnthonyLim23 marked this conversation as resolved.
///
/// Parameters
/// ----------
/// index: int
/// The index of the data to get.
pub fn _dict<'py>(&mut self, index: usize, py: Python<'py>) -> Result<Bound<'py, PyDict>> {
// Import Python's json module
let json_module = PyModule::import(py, "json")?;
// Get the Filters object as a JSON and use Python JSON to make the dict out of it
let filters_json = serde_json::to_string(&self.filters[index])?;
// Call json.loads(json_str)
let result = json_module.call_method1("loads", (filters_json,))?;

// Downcast the returned PyAny into a PyDict
let dict = Bound::cast_into::<PyDict>(result).map_err(PyErr::from)?;

// add histogram settings to the JSON
let hist_settings = PyDict::new(py);
hist_settings.set_item("min_time", self.results[index].min_time)?;
hist_settings.set_item("max_time", self.results[index].max_time)?;
hist_settings.set_item("n_bins", self.results[index].n_bins)?;
dict.set_item("hist_settings", hist_settings)?;

Ok(dict)
}

fn __repr__(&self) -> String {
let mut string = match &self.dataset {
Some(data) => data.__repr__(),
Expand Down Expand Up @@ -838,6 +886,22 @@ impl BatchData {
}
}

#[cfg(test)]
impl BatchData {
/// Build a BatchData with `n_filter_sets` empty filter sets around an
/// already-loaded dataset, so tests don't need a real .nxs file.
pub fn from_dataset(dataset: NexusData, n_filter_sets: usize) -> BatchData {
BatchData {
dataset: Some(dataset),
results: (0..n_filter_sets)
.map(|_| Histogram::new(0, 32768, 2048))
.collect(),
filters: (0..n_filter_sets).map(|_| Filters::new()).collect(),
data_changed: vec![true; n_filter_sets],
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -849,14 +913,7 @@ mod tests {
fn make_batch(n_filter_sets: usize) -> BatchData {
let mock = MockData::new().unwrap();
let dataset = mock.create(64, 1048576).unwrap();
BatchData {
dataset: Some(dataset),
results: (0..n_filter_sets)
.map(|_| Histogram::new(0, 32768, 2048))
.collect(),
filters: (0..n_filter_sets).map(|_| Filters::new()).collect(),
data_changed: vec![true; n_filter_sets],
}
BatchData::from_dataset(dataset, n_filter_sets)
}

/// resolve_indices(All) should return every index in range.
Expand Down
3 changes: 2 additions & 1 deletion src/data/nexus_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct NexusData {
pub frame_times: Dataset,
pub periods: Dataset,
pub sample_logs: Group,
#[pyo3(get)]
pub sample_log_names: Vec<String>,
pub n_events: usize, // the total number of events
pub n_frames: usize,
Expand All @@ -35,7 +36,7 @@ pub struct NexusData {
#[pymethods]
impl NexusData {
/// used for testing
fn get_frame_times<'py>(slf: &Bound<'py, NexusData>) -> Bound<'py, PyArray1<u32>> {
fn get_frame_times<'py>(slf: &Bound<'py, NexusData>) -> Bound<'py, PyArray1<u64>> {
let py = slf.py();
slf.borrow().frame_times.read_1d().unwrap().to_pyarray(py)
}
Expand Down
10 changes: 10 additions & 0 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ impl Data {
self.inner.invalidate_cache()
}

/// Check if any data has changed since you last ran a calculation.
pub fn data_changed(&mut self) -> bool {
self.inner.data_changed()
}

/// Set histogram settings.
///
/// Parameters
Expand Down Expand Up @@ -202,6 +207,11 @@ impl Data {
self.inner.set_amps_baseline(FilterIndex::Index(0), amp)
}

/// Clear all filters.
fn clear_filters(&mut self) -> Result<()> {
self.inner.clear_filters(FilterIndex::Index(0))
}

/// Save to a file.
///
/// Parameters
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use batch::BatchData;
use batch::{slog_geomspace, slog_linspace, slog_range, time_geomspace, time_linspace, time_range};
mod consts;
mod utils;
use utils::_get_filter_times;

#[cfg(test)]
mod test_utils;
Expand All @@ -39,5 +40,6 @@ fn core(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(slog_linspace, m)?)?;
m.add_function(wrap_pyfunction!(slog_geomspace, m)?)?;
m.add_function(wrap_pyfunction!(slog_range, m)?)?;
m.add_function(wrap_pyfunction!(_get_filter_times, m)?)?;
Ok(())
}
Loading
Loading