pub struct GraphData {
pub x: Tensor,
pub edge_index: Tensor,
pub edge_attr: Option<Tensor>,
pub batch: Option<Tensor>,
pub num_nodes: usize,
pub num_edges: usize,
}Expand description
Graph data structure for GNNs
Fields§
§x: TensorNode feature matrix (num_nodes x num_features)
edge_index: TensorEdge index matrix (2 x num_edges)
edge_attr: Option<Tensor>Edge features (optional)
batch: Option<Tensor>Batch assignment vector (optional, for batched graphs)
num_nodes: usizeNumber of nodes
num_edges: usizeNumber of edges
Implementations§
Source§impl GraphData
impl GraphData
Sourcepub fn new(x: Tensor, edge_index: Tensor) -> Self
pub fn new(x: Tensor, edge_index: Tensor) -> Self
Create a new graph data structure
§Arguments
x- Node feature matrix with shape[num_nodes, num_features]edge_index- Edge connectivity with shape[2, num_edges]
§Returns
A new GraphData instance
§Example
use torsh_graph::GraphData;
use torsh_tensor::creation::from_vec;
use torsh_core::device::DeviceType;
// Create a simple triangle graph
let x = from_vec(vec![1.0, 2.0, 3.0], &[3, 1], DeviceType::Cpu).unwrap();
let edge_index = from_vec(
vec![0.0, 1.0, 2.0, 1.0, 2.0, 0.0], // src, dst
&[2, 3],
DeviceType::Cpu
).unwrap();
let graph = GraphData::new(x, edge_index);
assert_eq!(graph.num_nodes, 3);
assert_eq!(graph.num_edges, 3);Sourcepub fn with_edge_attr(self, edge_attr: Tensor) -> Self
pub fn with_edge_attr(self, edge_attr: Tensor) -> Self
Add edge attributes
Sourcepub fn with_batch(self, batch: Tensor) -> Self
pub fn with_batch(self, batch: Tensor) -> Self
Add batch assignment
Sourcepub fn with_edge_attr_opt(self, edge_attr: Option<Tensor>) -> Self
pub fn with_edge_attr_opt(self, edge_attr: Option<Tensor>) -> Self
Add edge attributes (optional chaining)
Sourcepub fn memory_stats(&self) -> GraphMemoryStats
pub fn memory_stats(&self) -> GraphMemoryStats
Get memory usage statistics
Sourcepub fn validate(&self) -> Result<(), GraphValidationError>
pub fn validate(&self) -> Result<(), GraphValidationError>
Validate graph structure
Checks that:
- All edge indices refer to valid nodes
- Edge attributes (if present) match the number of edges
§Returns
Ok(())- Graph structure is validErr(GraphValidationError)- Validation failed
§Example
use torsh_graph::GraphData;
use torsh_tensor::creation::from_vec;
use torsh_core::device::DeviceType;
let x = from_vec(vec![1.0, 2.0], &[2, 1], DeviceType::Cpu).unwrap();
let edge_index = from_vec(vec![0.0, 1.0], &[2, 1], DeviceType::Cpu).unwrap();
let graph = GraphData::new(x, edge_index);
assert!(graph.validate().is_ok());Trait Implementations§
Auto Trait Implementations§
impl !Freeze for GraphData
impl RefUnwindSafe for GraphData
impl Send for GraphData
impl Sync for GraphData
impl Unpin for GraphData
impl UnsafeUnpin for GraphData
impl UnwindSafe for GraphData
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more