Skip to content

Commit

Permalink
add serde
Browse files Browse the repository at this point in the history
  • Loading branch information
Ameyanagi committed Feb 6, 2024
1 parent ce88628 commit 0d32374
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 30 deletions.
262 changes: 257 additions & 5 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ data_reader = "0.5.0"
easyfft = "0.4.0"
errorfunctions = "0.2.0"
fftconvolve = "0.1.1"
ndarray = { version = "0.15.6", features = ["approx"] }
ndarray = { version = "0.15.6", features = ["approx", "serde"] }
itertools = "0.12.0"
lazy_static = "1.4.0"
levenberg-marquardt = "0.13.1"
nalgebra = "0.32.3"
num-complex = "0.4.3"
num-complex = { version = "0.4.3", features = ["serde"] }
polyfit-rs = "0.2.1"
rusty-fitpack = "0.1.1"
rayon = "1.8.0"
serde = { version = "1.0.193", features = ["derive"] }
serde_arrow = { version = "0.8.0", features = ["arrow2-0-17", "arrow-46"] }


[dependencies.enterpolation]
Expand Down
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
use ndarray::{array, Array1};
use xraytsubaki::xafs::mathutils::MathUtils;

use serde_arrow::{
arrow2::{serialize_into_arrays, serialize_into_fields},
schema::TracingOptions,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
use xraytsubaki::xafs::io;
use xraytsubaki::xafs::xafsutils::find_energy_step;
Expand All @@ -19,11 +24,22 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let k = xafs_test_group.get_k().unwrap();
let chi = xafs_test_group.get_chi().unwrap();

// println!("k: {:?}", k);
// println!("chi: {:?}", chi);

k.iter().zip(chi.iter()).for_each(|(k, chi)| {
println!("[{}, {}],", k, chi);
});

// let fields = serialize_into_fields(&xafs_test_group, TracingOptions::default())?;

// println!("fields: {:?}", fields);

// println!("k: {:?}", k);
// println!("chi: {:?}", chi);
// println!("e0: {:?}", xafs_test_group.normalization.unwrap().get_e0());

println!("{:?}", xafs_test_group);
// println!("{:?}", xafs_test_group);

Ok(())

Expand Down
7 changes: 4 additions & 3 deletions src/xafs/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use levenberg_marquardt::{LeastSquaresProblem, LevenbergMarquardt};
use nalgebra::{DMatrix, DVector, Dyn, Owned};
use ndarray::{Array1, ArrayBase, Axis, Ix1, OwnedRepr};
use rusty_fitpack;
use serde::{Deserialize, Serialize};

// Import internal dependencies
use super::lmutils::LMParameters;
Expand All @@ -24,7 +25,7 @@ use super::{xafsutils, xrayfft};
/// Enum for background subtraction methods
/// AUTOBK: M. Newville, P. Livins, Y. Yacoby, J. J. Rehr, and E. A. Stern. Near-edge x-ray-absorption fine structure of Pb: A comparison of theory and experiment. Phys. Rev. B, 47:14126–14131, Jun 1993. doi:10.1103/PhysRevB.47.14126.
/// ILPBkg: To be implemented
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BackgroundMethod {
AUTOBK(AUTOBK),
ILPBkg(ILPBkg),
Expand Down Expand Up @@ -90,7 +91,7 @@ impl BackgroundMethod {
/// Struct for AUTOBK
///
/// Parameters and the output are stored in this struct
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AUTOBK {
/// Edge energy in eV (this is used for starting point of k). If None, it will be determined.
pub ek0: Option<f64>,
Expand Down Expand Up @@ -813,7 +814,7 @@ impl LeastSquaresProblem<f64, Dyn, Dyn> for AUTOBKSpline {
}

/// TODO: Implement ILPBkg
#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct ILPBkg {}

/// TODO: Implement ILPBkg
Expand Down
4 changes: 0 additions & 4 deletions src/xafs/mathutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,11 @@ pub fn bessel_I0(x: f64) -> f64 {
///
///
pub fn splev_jacobian(t: Vec<f64>, c: Vec<f64>, k: usize, x: Vec<f64>, e: usize) -> DMatrix<f64> {
let mut y: Vec<f64> = vec![0.0; x.len()];

let k1: usize = k + 1;
let k2: usize = k1 + 1;
let nk1: usize = t.len() - k1;
let tb: f64 = t[k1 - 1];
let te: f64 = t[nk1];
let mut l: usize = k1;
let mut l1: usize = l + 1;

let mut derivatives: Vec<Vec<f64>> = vec![vec![0.0; c.len()]; x.len()];

Expand Down
7 changes: 4 additions & 3 deletions src/xafs/normalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::error::Error;
// Import external dependencies
use ndarray::{Array1, ArrayBase, Ix1, OwnedRepr};
use polyfit_rs::polyfit_rs;
use serde::{Deserialize, Serialize};

// Import internal dependencies
use super::mathutils::{self, MathUtils};
Expand Down Expand Up @@ -44,7 +45,7 @@ pub trait Normalization {
///
/// let mut normalization_method = NormalizationMethod::new();
/// ```
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum NormalizationMethod {
PrePostEdge(PrePostEdge),
MBack(MBack),
Expand Down Expand Up @@ -167,7 +168,7 @@ impl NormalizationMethod {
///
/// This is the standard normalization method used in athena and larch.
///
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PrePostEdge {
pub pre_edge_start: Option<f64>,
pub pre_edge_end: Option<f64>,
Expand Down Expand Up @@ -488,7 +489,7 @@ impl Normalization for PrePostEdge {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MBack {
pub e0: Option<f64>,
pub edge_step: Option<f64>,
Expand Down
20 changes: 14 additions & 6 deletions src/xafs/xafsutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@
#![allow(unused_imports)]
#![allow(unused_variables)]

use crate::xafs::mathutils::index_of;
// Standard library dependencies
use std::cmp;
use std::error::Error;
// External dependencies
use fftconvolve::{fftconvolve, Mode};
use ndarray::{Array, Array1, ArrayBase, Axis, Ix1, OwnedRepr, Slice};
use serde::{Deserialize, Serialize};

// load dependencies
use super::bessel_i0;
use super::io;

// Load local traits
use super::mathutils::MathUtils;

use fftconvolve::{fftconvolve, Mode};
use ndarray::{Array, Array1, ArrayBase, Axis, Ix1, OwnedRepr, Slice};
use std::cmp;
use std::error::Error;
// Load local functions
use crate::xafs::mathutils::index_of;

// Constants
pub const TINY_ENERGY: f64 = 0.005;

/// Physical constants used in xraytsubaki
Expand Down Expand Up @@ -481,7 +489,7 @@ pub fn _find_e0<T: Into<ArrayBase<OwnedRepr<f64>, Ix1>> + Clone>(
Ok((en[imax], imax, estep))
}

#[derive(Debug, Clone, Copy, Default, PartialEq)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
pub enum FTWindow {
#[default]
Hanning, // Hanning window, cosine-squared tamper
Expand Down
3 changes: 2 additions & 1 deletion src/xafs/xasgroup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::mem;

// External dependencies
use rayon::prelude::*;
use serde::{Deserialize, Serialize};

// load dependencies
use super::xasspectrum;
Expand All @@ -17,7 +18,7 @@ use itertools::Itertools;
// Load local traits
use xasspectrum::XASSpectrum;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct XASGroup {
pub spectra: Vec<XASSpectrum>,
}
Expand Down
5 changes: 3 additions & 2 deletions src/xafs/xasspectrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
// Standard library dependencies
use std::error::Error;

use easyfft::dyn_size::realfft::DynRealDft;
// External dependencies
use easyfft::dyn_size::realfft::DynRealDft;
use ndarray::{ArrayBase, Axis, Ix1, OwnedRepr};
use serde::{Deserialize, Serialize};

// load dependencies
use super::background;
Expand All @@ -28,7 +29,7 @@ use normalization::Normalization;
/// # Examples
///
/// TODO: Add examples
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct XASSpectrum {
pub name: Option<String>,
pub raw_energy: Option<ArrayBase<OwnedRepr<f64>, Ix1>>,
Expand Down
34 changes: 31 additions & 3 deletions src/xafs/xrayfft.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// Standard library dependencies

// External dependencies
use easyfft::prelude::{DynRealFft, DynRealIfft};
use easyfft::{dyn_size::realfft::DynRealDft, num_complex::Complex};
use nalgebra::{DVector, Owned};
use ndarray::{Array, Array1, ArrayBase, Axis, Ix, Ix1, OwnedRepr};
use num_complex::Complex64;
use serde::{Deserialize, Serialize};

use crate::xafs::xafsutils::FTWindow;
// load dependencies

// Load local traits
use super::mathutils::MathUtils;
use super::xafsutils::ftwindow;
use crate::xafs::xafsutils::FTWindow;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct XrayFFTF {
pub rmax_out: Option<f64>,
pub window: Option<FTWindow>,
Expand All @@ -20,6 +27,8 @@ pub struct XrayFFTF {
pub nfft: Option<usize>,
pub kstep: Option<f64>,
pub r: Option<ArrayBase<OwnedRepr<f64>, Ix1>>,
// currently asking for serde support in the easyfft crate
#[serde(skip)]
pub chir: Option<DynRealDft<f64>>,
pub chir_mag: Option<ArrayBase<OwnedRepr<f64>, Ix1>>,
pub kwin: Option<ArrayBase<OwnedRepr<f64>, Ix1>>,
Expand Down Expand Up @@ -209,7 +218,7 @@ impl XrayFFTF {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct XrayFFTR {
pub qmax_out: Option<f64>,
pub window: Option<FTWindow>,
Expand Down Expand Up @@ -564,6 +573,25 @@ impl FFTUtils<DVector<f64>> for [Complex<f64>] {
}
}

// #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
// pub struct DynRealDftWrapper {
// zeroth_bin: f64,

// frequency_bins: Vec<Complex<f64>>,

// original_length: usize,
// }

// impl From<DynRealDft<f64>> for DynRealDftWrapper {
// fn from(dft: DynRealDft<f64>) -> Self {
// DynRealDftWrapper {
// zeroth_bin: dft.get_offset().clone(),
// frequency_bins: dft.get_frequency_bins().to_vec(),
// original_length: dft.get_original_length(),
// }
// }
// }

// impl PartialEq for DynRealDft<f64> {
// fn eq(&self, other: &Self) -> bool {
// self.len() == other.len()
Expand Down

0 comments on commit 0d32374

Please sign in to comment.