Path Interpretation
interpret_path
def interpret_path(path: Union[str, Path], **kwargs) -> Path
Interprets a file path and returns a CloudFiles instance for cloud or local storage.
File path (supports local paths, gs:// for Google Cloud Storage, file:// for local files)
Additional keyword arguments passed to CloudFiles
Returns: Tuple[CloudFiles, str]
- CloudFiles instance
- File name (or None if path is a directory)
from meshmash.io import interpret_path
# Google Cloud Storage
cf, filename = interpret_path("gs://my-bucket/data/features.npz")
# Local file
cf, filename = interpret_path("/path/to/local/file.npz")
Feature I/O
save_condensed_features
def save_condensed_features(
path: Union[str, Path],
features: pd.DataFrame,
labels: np.ndarray,
feature_dtype: type = np.float32,
label_dtype: type = np.int32,
check_header: bool = True,
)
Saves features and labels in a condensed compressed format with header validation.
Output path for the compressed features file
Feature matrix as a DataFrame
Whether to validate column headers against existing header.txt file
Returns: None
The function reindexes features to include index -1 and saves both features and labels in a compressed .npz format. A header.txt file is created/validated in the same directory.
import pandas as pd
import numpy as np
from meshmash.io import save_condensed_features
features = pd.DataFrame({
'x': [1.0, 2.0, 3.0],
'y': [4.0, 5.0, 6.0],
'z': [7.0, 8.0, 9.0]
})
labels = np.array([0, 1, 0])
save_condensed_features(
"gs://my-bucket/features/data.npz",
features,
labels,
feature_dtype=np.float32
)
read_condensed_features
def read_condensed_features(path: Union[str, Path]) -> tuple[pd.DataFrame, np.ndarray]
Reads condensed features and labels from a compressed file.
Path to the compressed features file
Returns: Tuple[pd.DataFrame, np.ndarray]
- Features as a DataFrame with columns from header.txt
- Labels as a numpy array
from meshmash.io import read_condensed_features
features, labels = read_condensed_features("gs://my-bucket/features/data.npz")
print(features.shape)
print(labels.shape)
Edge I/O
save_condensed_edges
def save_condensed_edges(
path: Union[str, Path], edges: pd.DataFrame, check_header: bool = True
)
Saves edge data in a condensed compressed format.
Output path for the compressed edges file
Edge DataFrame with ‘source’ and ‘target’ columns plus optional edge features
Whether to validate column headers against existing header.txt file
Returns: None
Edge list (source, target) is stored as int32, while edge features are stored as float32.
import pandas as pd
from meshmash.io import save_condensed_edges
edges = pd.DataFrame({
'source': [0, 1, 2],
'target': [1, 2, 0],
'weight': [1.0, 2.0, 1.5],
'distance': [0.5, 1.0, 0.8]
})
save_condensed_edges("gs://my-bucket/edges/data.npz", edges)
read_condensed_edges
def read_condensed_edges(path: Union[str, Path]) -> tuple[pd.DataFrame, pd.DataFrame]
Reads condensed edge data from a compressed file.
Path to the compressed edges file
Returns: pd.DataFrame - DataFrame containing source, target, and all edge features
from meshmash.io import read_condensed_edges
edges = read_condensed_edges("gs://my-bucket/edges/data.npz")
print(edges.columns) # ['source', 'target', 'weight', 'distance']
Graph I/O
save_condensed_graph
def save_condensed_graph(
path: Union[str, Path],
nodes: pd.DataFrame,
edges: pd.DataFrame,
nodes_dtype=np.float32,
edges_dtype=np.int32,
check_header: bool = True,
)
Saves a complete graph with nodes and edges in compressed format.
Output path for the compressed graph file
Data type for node features
Whether to validate headers
Returns: None
Creates two header files: nodes_header.txt and edges_header.txt in the same directory.
import pandas as pd
import numpy as np
from meshmash.io import save_condensed_graph
nodes = pd.DataFrame({
'x': [0.0, 1.0, 2.0],
'y': [0.0, 1.0, 0.0],
'feature': [1.0, 2.0, 3.0]
})
edges = pd.DataFrame({
'source': [0, 1],
'target': [1, 2]
})
save_condensed_graph(
"gs://my-bucket/graphs/graph.npz",
nodes,
edges
)
read_condensed_graph
def read_condensed_graph(path: Union[str, Path]) -> tuple[pd.DataFrame, pd.DataFrame]
Reads a condensed graph from a compressed file.
Path to the compressed graph file
Returns: Tuple[pd.DataFrame, pd.DataFrame]
- Nodes DataFrame
- Edges DataFrame
from meshmash.io import read_condensed_graph
nodes, edges = read_condensed_graph("gs://my-bucket/graphs/graph.npz")
print(f"Graph has {len(nodes)} nodes and {len(edges)} edges")
Mapping I/O
save_id_to_mesh_map
def save_id_to_mesh_map(path: Union[str, Path], id_to_mesh_map: np.ndarray)
Saves an ID-to-mesh mapping array.
Output path for the mapping file
Array of shape (N, 2) containing ID mappings
Returns: None
import numpy as np
from meshmash.io import save_id_to_mesh_map
mapping = np.array([
[0, 100],
[1, 101],
[2, 102]
])
save_id_to_mesh_map("gs://my-bucket/mappings/id_map.npz", mapping)
read_id_to_mesh_map
def read_id_to_mesh_map(path: Union[str, Path]) -> np.ndarray
Reads an ID-to-mesh mapping array.
Returns: np.ndarray - Array of shape (N, 2) containing ID mappings
from meshmash.io import read_id_to_mesh_map
mapping = read_id_to_mesh_map("gs://my-bucket/mappings/id_map.npz")
print(mapping.shape) # (N, 2)
Generic Array I/O
save_array
def save_array(path: Union[str, Path], array: np.ndarray)
Saves a numpy array in compressed format.
Output path for the array file
Returns: None
import numpy as np
from meshmash.io import save_array
data = np.random.rand(1000, 10)
save_array("gs://my-bucket/arrays/data.npz", data)
read_array
def read_array(path: Union[str, Path]) -> np.ndarray
Reads a numpy array from a compressed file.
Returns: np.ndarray - Loaded array
from meshmash.io import read_array
data = read_array("gs://my-bucket/arrays/data.npz")
print(data.shape)