Skip to main content

Frame

Struct Frame 

Source
pub struct Frame {
    pub meta: HashMap<String, String>,
    pub simbox: Option<SimBox>,
    /* private fields */
}
Expand description

A dictionary from string keys to Blocks.

Frame provides a simple container for organizing multiple blocks of data, typically representing different aspects of a molecular system (e.g., atoms, bonds, velocities). Each block can have different numbers of rows and different column types.

Fields§

§meta: HashMap<String, String>

Arbitrary key-value metadata associated with the frame.

§simbox: Option<SimBox>

Simulation box defining periodic boundary conditions.

Implementations§

Source§

impl Frame

Source

pub fn new() -> Frame

Creates an empty Frame.

§Examples
use molrs_core::frame::Frame;

let frame = Frame::new();
assert!(frame.is_empty());
Source

pub fn with_capacity(cap: usize) -> Frame

Creates an empty Frame with the specified capacity for blocks.

§Examples
use molrs_core::frame::Frame;

let frame = Frame::with_capacity(10);
assert!(frame.is_empty());
Source

pub fn from_map(map: HashMap<String, Block>) -> Frame

Creates a Frame from an existing HashMap of blocks.

§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("atoms".to_string(), Block::new());

let frame = Frame::from_map(map);
assert_eq!(frame.len(), 1);
Source

pub fn into_inner( self, ) -> (HashMap<String, Block>, HashMap<String, Grid>, HashMap<String, String>, Option<SimBox>)

Consumes the Frame and returns the inner HashMap of blocks, grids, metadata, and simbox.

§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;

let mut frame = Frame::new();
frame.insert("atoms", Block::new());
frame.meta.insert("title".into(), "Test".into());

let (blocks, grids, meta, simbox) = frame.into_inner();
assert_eq!(blocks.len(), 1);
assert!(grids.is_empty());
assert_eq!(meta.get("title").unwrap(), "Test");
assert!(simbox.is_none());
Source

pub fn len(&self) -> usize

Number of blocks (keys) in the frame.

§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;

let mut frame = Frame::new();
assert_eq!(frame.len(), 0);

frame.insert("atoms", Block::new());
assert_eq!(frame.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the frame contains no blocks.

Note: This only checks blocks, not metadata.

Source

pub fn contains_key(&self, key: &str) -> bool

Returns true if the frame contains the specified key.

Source

pub fn get(&self, key: &str) -> Option<&Block>

Gets an immutable reference to the block for key if present.

For a panicking version, use the Index trait: &frame["key"].

Source

pub fn get_mut(&mut self, key: &str) -> Option<&mut Block>

Gets a mutable reference to the block for key if present.

For a panicking version, use the IndexMut trait: &mut frame["key"].

Source

pub fn insert(&mut self, key: impl Into<String>, block: Block) -> Option<Block>

Inserts a block under key. Returns the previous block if any.

§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;

let mut frame = Frame::new();
let old = frame.insert("atoms", Block::new());
assert!(old.is_none());

let old = frame.insert("atoms", Block::new());
assert!(old.is_some());
Source

pub fn remove(&mut self, key: &str) -> Option<Block>

Removes and returns the block for key, if present.

Source

pub fn clear(&mut self)

Clears the frame, removing all blocks.

Note: This does NOT clear metadata. Use clear_all() to clear both.

§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;

let mut frame = Frame::new();
frame.insert("atoms", Block::new());
frame.meta.insert("title".into(), "Test".into());

frame.clear();
assert!(frame.is_empty());
assert!(!frame.meta.is_empty()); // metadata preserved
Source

pub fn clear_all(&mut self)

Clears both blocks and metadata.

§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;

let mut frame = Frame::new();
frame.insert("atoms", Block::new());
frame.meta.insert("title".into(), "Test".into());

frame.clear_all();
assert!(frame.is_empty());
assert!(frame.meta.is_empty());
Source

pub fn insert_grid( &mut self, name: impl Into<String>, grid: Grid, ) -> Option<Grid>

Insert or replace a named grid.

Source

pub fn remove_grid(&mut self, name: &str) -> Option<Grid>

Remove a named grid.

Source

pub fn get_grid(&self, name: &str) -> Option<&Grid>

Borrow a grid by name.

Source

pub fn get_grid_mut(&mut self, name: &str) -> Option<&mut Grid>

Borrow a grid mutably by name.

Source

pub fn has_grid(&self, name: &str) -> bool

Returns true if the frame contains a named grid.

Source

pub fn grids(&self) -> impl Iterator<Item = (&str, &Grid)>

Returns an iterator over (name, grid) pairs.

Source

pub fn grid_keys(&self) -> impl Iterator<Item = &str>

Returns an iterator over grid names.

Source

pub fn rename_column( &mut self, block_key: &str, old_col_key: &str, new_col_key: &str, ) -> bool

Renames a column in the specified block.

Returns true if the column was successfully renamed, false if the block doesn’t exist, the old column key doesn’t exist, or the new column key already exists.

§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
use molrs_core::types::F;
use ndarray::Array1;

let mut frame = Frame::new();
let mut atoms = Block::new();
atoms.insert("x", Array1::from_vec(vec![1.0 as F, 2.0 as F]).into_dyn()).unwrap();
frame.insert("atoms", atoms);

assert!(frame.rename_column("atoms", "x", "position_x"));
assert!(!frame["atoms"].contains_key("x"));
assert!(frame["atoms"].contains_key("position_x"));
Source

pub fn rename_block(&mut self, old_key: &str, new_key: &str) -> bool

Renames a block in the frame.

Returns true if the block was successfully renamed, false if the old block doesn’t exist or the new block name already exists.

§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
use molrs_core::types::F;
use ndarray::Array1;

let mut frame = Frame::new();
let mut atoms = Block::new();
atoms.insert("x", Array1::from_vec(vec![1.0 as F]).into_dyn()).unwrap();
frame.insert("atoms", atoms);

assert!(frame.rename_block("atoms", "molecules"));
assert!(!frame.contains_key("atoms"));
assert!(frame.contains_key("molecules"));
Source

pub fn iter(&self) -> impl Iterator<Item = (&str, &Block)>

Returns an iterator over (&str, &Block).

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (&str, &mut Block)>

Returns a mutable iterator over (&str, &mut Block).

§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
use molrs_core::types::F;
use ndarray::Array1;

let mut frame = Frame::new();
let mut atoms = Block::new();
atoms.insert("x", Array1::from_vec(vec![1.0 as F]).into_dyn()).unwrap();
frame.insert("atoms", atoms);

for (_name, block) in frame.iter_mut() {
    // Can mutate blocks
    if let Some(x) = block.get_float_mut("x") {
        x[[0]] = 99.0 as F;
    }
}
Source

pub fn keys(&self) -> impl Iterator<Item = &str>

Returns an iterator over keys.

Source

pub fn values(&self) -> impl Iterator<Item = &Block>

Returns an iterator over block references.

Source

pub fn values_mut(&mut self) -> impl Iterator<Item = &mut Block>

Returns a mutable iterator over block references.

Source

pub fn validate(&self) -> Result<(), MolRsError>

Validates cross-block consistency.

This method checks for common consistency issues:

  • All blocks with “atoms” prefix should have the same nrows
  • Bond indices (if present) should reference valid atoms
§Returns
  • Ok(()) if validation passes
  • Err(MolRsError::Validation) with details if validation fails
§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;
use molrs_core::types::F;
use ndarray::Array1;

let mut frame = Frame::new();
let mut atoms = Block::new();
atoms.insert("x", Array1::from_vec(vec![1.0 as F, 2.0 as F, 3.0 as F]).into_dyn()).unwrap();
atoms.insert("y", Array1::from_vec(vec![0.0 as F, 1.0 as F, 2.0 as F]).into_dyn()).unwrap();
frame.insert("atoms", atoms);

assert!(frame.validate().is_ok());
Source

pub fn is_consistent(&self) -> bool

Checks if the frame is consistent without returning an error.

This is a non-panicking version of validate() that returns a boolean.

§Examples
use molrs_core::frame::Frame;
use molrs_core::block::Block;

let frame = Frame::new();
assert!(frame.is_consistent());

Trait Implementations§

Source§

impl Clone for Frame

Source§

fn clone(&self) -> Frame

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Frame

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Frame

Source§

fn default() -> Frame

Returns the “default value” for a type. Read more
Source§

impl FrameAccess for Frame

Source§

fn get_float( &self, block_key: &str, col_key: &str, ) -> Option<ArrayBase<ViewRepr<&f64>, Dim<IxDynImpl>>>

Gets a float array view from a column inside a block.
Source§

fn get_int( &self, block_key: &str, col_key: &str, ) -> Option<ArrayBase<ViewRepr<&i32>, Dim<IxDynImpl>>>

Gets an int array view from a column inside a block.
Source§

fn get_bool( &self, block_key: &str, col_key: &str, ) -> Option<ArrayBase<ViewRepr<&bool>, Dim<IxDynImpl>>>

Gets a bool array view from a column inside a block.
Source§

fn get_uint( &self, block_key: &str, col_key: &str, ) -> Option<ArrayBase<ViewRepr<&u32>, Dim<IxDynImpl>>>

Gets a uint array view from a column inside a block.
Source§

fn get_u8( &self, block_key: &str, col_key: &str, ) -> Option<ArrayBase<ViewRepr<&u8>, Dim<IxDynImpl>>>

Gets a u8 array view from a column inside a block.
Source§

fn get_string( &self, block_key: &str, col_key: &str, ) -> Option<ArrayBase<ViewRepr<&String>, Dim<IxDynImpl>>>

Gets a string array view from a column inside a block.
Source§

fn simbox_ref(&self) -> Option<&SimBox>

Returns a reference to the simulation box, if present.
Source§

fn meta_ref(&self) -> &HashMap<String, String>

Returns a reference to the metadata map.
Source§

fn block_keys(&self) -> Vec<&str>

Returns block keys as a Vec.
Source§

fn contains_block(&self, key: &str) -> bool

Returns true if the frame contains the specified block key.
Source§

fn block_count(&self) -> usize

Number of blocks.
Source§

fn is_empty(&self) -> bool

Returns true if the frame contains no blocks.
Source§

fn visit_block<R>( &self, key: &str, f: impl FnOnce(&dyn BlockAccess) -> R, ) -> Option<R>

Visits a block by key through the BlockAccess trait, using the visitor pattern to avoid lifetime/return-type issues. Returns None if the block does not exist.
Source§

impl<'a> From<&'a Frame> for FrameView<'a>

Source§

fn from(frame: &'a Frame) -> FrameView<'a>

Converts to this type from the input type.
Source§

impl Index<&str> for Frame

Source§

type Output = Block

The returned type after indexing.
Source§

fn index(&self, key: &str) -> &<Frame as Index<&str>>::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<&str> for Frame

Source§

fn index_mut(&mut self, key: &str) -> &mut <Frame as Index<&str>>::Output

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl Freeze for Frame

§

impl !RefUnwindSafe for Frame

§

impl Send for Frame

§

impl Sync for Frame

§

impl Unpin for Frame

§

impl UnsafeUnpin for Frame

§

impl !UnwindSafe for Frame

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.