-
Notifications
You must be signed in to change notification settings - Fork 0
Feat add matrix market load #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VanyaGlazunov
wants to merge
3
commits into
main
Choose a base branch
from
feat-add-MatrixMarket-load
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| //! MatrixMarket directory format loader. | ||
| //! | ||
| //! An edge-labeled graph is stored in a directory with the following layout: | ||
| //! | ||
| //! ```text | ||
| //! <dir>/ | ||
| //! vertices.txt — one line per node: `<node_name> <1-based-index>` | ||
| //! edges.txt — one line per label: `<label_name> <1-based-index>` | ||
| //! 1.txt — MM adjacency matrix for the label with index 1 | ||
| //! 2.txt — MM adjacency matrix for the label with index 2 | ||
| //! … | ||
| //! ``` | ||
| //! | ||
| //! # Example | ||
| //! | ||
| //! ```no_run | ||
| //! use pathrex::graph::{Graph, InMemory, GraphDecomposition}; | ||
| //! use pathrex::formats::mm::MatrixMarket; | ||
| //! | ||
| //! let graph = Graph::<InMemory>::try_from( | ||
| //! MatrixMarket::from_dir("path/to/graph/dir") | ||
| //! ).unwrap(); | ||
| //! println!("Nodes: {}", graph.num_nodes()); | ||
| //! ``` | ||
|
|
||
| use std::collections::HashMap; | ||
| use std::ffi::CString; | ||
| use std::fs::File; | ||
| use std::io::{BufRead, BufReader}; | ||
| use std::os::fd::IntoRawFd; | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| use crate::formats::FormatError; | ||
| use crate::graph::{GraphError, ensure_grb_init}; | ||
| use crate::la_ok; | ||
| use crate::lagraph_sys::{FILE, GrB_Matrix, LAGraph_MMRead}; | ||
|
|
||
| /// Read a single MatrixMarket file and return the raw [`GrB_Matrix`]. | ||
| pub fn load_mm_file(path: impl AsRef<Path>) -> Result<GrB_Matrix, FormatError> { | ||
| let path = path.as_ref(); | ||
|
|
||
| ensure_grb_init().map_err(|e| match e { | ||
| GraphError::LAGraph(info, msg) => FormatError::MatrixMarket { | ||
| code: info, | ||
| message: msg, | ||
| }, | ||
| _ => FormatError::MatrixMarket { | ||
| code: crate::lagraph_sys::GrB_Info::GrB_PANIC, | ||
| message: "Failed to initialize GraphBLAS".to_string(), | ||
| }, | ||
| })?; | ||
|
|
||
| let file = File::open(path)?; | ||
| let fd = file.into_raw_fd(); | ||
|
|
||
| let c_mode = CString::new("r").unwrap(); | ||
| let f = unsafe { libc::fdopen(fd, c_mode.as_ptr()) }; | ||
| if f.is_null() { | ||
| unsafe { libc::close(fd) }; | ||
| return Err(std::io::Error::last_os_error().into()); | ||
| } | ||
|
|
||
| let mut matrix: GrB_Matrix = std::ptr::null_mut(); | ||
|
|
||
| let err = la_ok!(LAGraph_MMRead(&mut matrix, f as *mut FILE)); | ||
| unsafe { libc::fclose(f) }; | ||
|
|
||
| match err { | ||
| Ok(_) => Ok(matrix), | ||
| Err(GraphError::LAGraph(info, msg)) => Err(FormatError::MatrixMarket { | ||
| code: info, | ||
| message: msg, | ||
| }), | ||
| _ => unreachable!("should be either mm read error or ok"), | ||
| } | ||
| } | ||
|
|
||
| /// Parse a `<name> <1-based-index>` mapping file. | ||
| pub(crate) fn parse_index_map( | ||
| path: &Path, | ||
| ) -> Result<(HashMap<usize, String>, HashMap<String, usize>), FormatError> { | ||
| let file_name = path | ||
| .file_name() | ||
| .map(|n| n.to_string_lossy().into_owned()) | ||
| .unwrap_or_else(|| path.display().to_string()); | ||
|
|
||
| let reader = BufReader::new(File::open(path)?); | ||
| let mut by_idx: HashMap<usize, String> = HashMap::new(); | ||
| let mut by_name: HashMap<String, usize> = HashMap::new(); | ||
|
|
||
| for (line_no, line) in reader.lines().enumerate() { | ||
| let line = line?; | ||
| let line = line.trim(); | ||
| if line.is_empty() { | ||
| continue; | ||
| } | ||
|
|
||
| let (name, idx_str) = | ||
| line.rsplit_once(char::is_whitespace) | ||
| .ok_or_else(|| FormatError::InvalidFormat { | ||
| file: file_name.clone(), | ||
| line: line_no + 1, | ||
| reason: "expected '<name> <index>' but found no whitespace".into(), | ||
| })?; | ||
|
|
||
| let idx: usize = idx_str | ||
| .trim() | ||
| .parse() | ||
| .map_err(|_| FormatError::InvalidFormat { | ||
| file: file_name.clone(), | ||
| line: line_no + 1, | ||
| reason: format!("index '{}' is not a valid positive integer", idx_str.trim()), | ||
| })?; | ||
|
|
||
| let name = name.trim().to_owned(); | ||
| by_idx.insert(idx, name.clone()); | ||
| by_name.insert(name, idx); | ||
| } | ||
|
|
||
| Ok((by_idx, by_name)) | ||
| } | ||
|
|
||
| /// A MatrixMarket directory data source. | ||
| /// | ||
| /// Reads the graph from a directory that contains: | ||
| /// - `vertices.txt` — `<node_name> <1-based-index>` mapping | ||
| /// - `edges.txt` — `<label_name> <1-based-index>` mapping | ||
| /// - `<n>.txt` — one MM adjacency matrix per label index | ||
| /// # Example | ||
| /// | ||
| /// ```no_run | ||
| /// use pathrex::graph::{Graph, InMemory, GraphDecomposition}; | ||
| /// use pathrex::formats::mm::MatrixMarket; | ||
| /// | ||
| /// let graph = Graph::<InMemory>::try_from( | ||
| /// MatrixMarket::from_dir("path/to/graph/dir") | ||
| /// ).unwrap(); | ||
| /// println!("Nodes: {}", graph.num_nodes()); | ||
| /// ``` | ||
| pub struct MatrixMarket { | ||
| pub(crate) dir: PathBuf, | ||
| } | ||
|
|
||
| impl MatrixMarket { | ||
| /// Create a `MatrixMarket` source that will load from `dir`. | ||
| pub fn from_dir(dir: impl Into<PathBuf>) -> Self { | ||
| Self { dir: dir.into() } | ||
| } | ||
|
|
||
| pub(crate) fn mm_path(&self, idx: usize) -> PathBuf { | ||
| self.dir.join(format!("{}.txt", idx)) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use std::io::Write; | ||
| use tempfile::TempDir; | ||
|
|
||
| fn write_file(dir: &Path, name: &str, content: &str) { | ||
| let mut f = File::create(dir.join(name)).unwrap(); | ||
| f.write_all(content.as_bytes()).unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_parse_index_map_basic() { | ||
| let tmp = TempDir::new().unwrap(); | ||
| write_file( | ||
| tmp.path(), | ||
| "vertices.txt", | ||
| "<Article1> 1\n<Paul_Erdoes> 2\n<1940> 3\n", | ||
| ); | ||
| let (by_idx, by_name) = parse_index_map(&tmp.path().join("vertices.txt")).unwrap(); | ||
| assert_eq!(by_idx[&1], "<Article1>"); | ||
| assert_eq!(by_idx[&2], "<Paul_Erdoes>"); | ||
| assert_eq!(by_idx[&3], "<1940>"); | ||
| assert_eq!(by_name["<Article1>"], 1); | ||
| assert_eq!(by_name["<Paul_Erdoes>"], 2); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_parse_index_map_empty_lines_ignored() { | ||
| let tmp = TempDir::new().unwrap(); | ||
| write_file(tmp.path(), "edges.txt", "\n<journal> 1\n\n<creator> 2\n"); | ||
| let (by_idx, _) = parse_index_map(&tmp.path().join("edges.txt")).unwrap(); | ||
| assert_eq!(by_idx.len(), 2); | ||
| assert_eq!(by_idx[&1], "<journal>"); | ||
| assert_eq!(by_idx[&2], "<creator>"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_parse_index_map_bad_index_returns_error() { | ||
| let tmp = TempDir::new().unwrap(); | ||
| write_file(tmp.path(), "bad.txt", "<foo> notanumber\n"); | ||
| let err = parse_index_map(&tmp.path().join("bad.txt")).unwrap_err(); | ||
| assert!( | ||
| matches!(err, FormatError::InvalidFormat { .. }), | ||
| "expected InvalidFormat, got {:?}", | ||
| err | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_parse_index_map_missing_whitespace_returns_error() { | ||
| let tmp = TempDir::new().unwrap(); | ||
| write_file(tmp.path(), "bad.txt", "nospacehere\n"); | ||
| let err = parse_index_map(&tmp.path().join("bad.txt")).unwrap_err(); | ||
| assert!(matches!(err, FormatError::InvalidFormat { .. })); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_load_nonexistent_mm_file_returns_io_error() { | ||
| let result = load_mm_file("/nonexistent/path/to/file.txt"); | ||
| assert!( | ||
| matches!(result, Err(FormatError::Io(_))), | ||
| "expected Io error for missing file, got: {:?}", | ||
| result | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_from_dir_stores_path() { | ||
| let src = MatrixMarket::from_dir("/some/path"); | ||
| assert_eq!(src.dir, PathBuf::from("/some/path")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_mm_path() { | ||
| let src = MatrixMarket::from_dir("/graph"); | ||
| assert_eq!(src.mm_path(3), PathBuf::from("/graph/3.txt")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we differ matrix formats and queries formats? Maybe it'll be good refactoring:
formats/queries/...--- forcsvandntformats/datasets/..--- formm