Data I/O¶
Introduction¶
The hcn.io module provides a unified interface for data persistence and quick access to source files.
It simplifies the transition between Python objects, local files, and system explorers.
System Exploration¶
code(input_): Locates and opens a file, function, module, or class in VS Code.finder(input_): Opens the target's directory in Finder (macOS) or File Explorer (Windows).
Support Matrix¶
| Extension | Library | Notes |
|---|---|---|
.npy, .npz |
numpy |
.npz saves with compression; requires dict. |
.json |
json |
Requires dict for saving; supports indent=2. |
.csv |
pandas |
Loaded as np.array without headers/index. |
.jpg, .jpeg |
imageio |
Supports Quality Map (xs to xl). Lossy only. |
.png |
imageio |
Supports ZLIB Compression Map (xs to xl). |
.bmp |
imageio |
Lossless only (Compression is ignored). |
.tif, .tiff |
tifffile |
Uses zlib plugin; supports Compression Map. |
.avi |
imageio |
Read-only support for video frames. |
Examples¶
In [1]:
Copied!
from hcn.preamble import *
from hcn.io import load, save, code
from hcn.preamble import *
from hcn.io import load, save, code
In [2]:
Copied!
# --- NumPy Dictionary (.npz) ---
data = {'x': np.arange(10), 'y': np.sin(np.arange(10))}
path = save('test.npz', data)
print(f'Loaded keys: {list(load(path).keys())}')
os.remove(path)
# --- NumPy Dictionary (.npz) ---
data = {'x': np.arange(10), 'y': np.sin(np.arange(10))}
path = save('test.npz', data)
print(f'Loaded keys: {list(load(path).keys())}')
os.remove(path)
Loaded keys: ['x', 'y']
In [3]:
Copied!
# --- Image with Compression (.jpg) ---
# compress: 'xs', 'sm', 'md', 'lg', 'xl'
img = np.random.randint(0, 255, (64, 64, 3), dtype=np.uint8)
path = save('test.jpg', img, compress='lg')
print(f'Loaded image shape: {load(path).shape}')
os.remove(path)
# --- Image with Compression (.jpg) ---
# compress: 'xs', 'sm', 'md', 'lg', 'xl'
img = np.random.randint(0, 255, (64, 64, 3), dtype=np.uint8)
path = save('test.jpg', img, compress='lg')
print(f'Loaded image shape: {load(path).shape}')
os.remove(path)
Loaded image shape: (64, 64, 3)
In [4]:
Copied!
# --- Metadata/Config (.json) ---
path = save('test.json', {'status': 'success', 'code': 200})
print(f'Loaded JSON: {load(path)}')
os.remove(path)
# --- Metadata/Config (.json) ---
path = save('test.json', {'status': 'success', 'code': 200})
print(f'Loaded JSON: {load(path)}')
os.remove(path)
Loaded JSON: {'status': 'success', 'code': 200}