Workspace#
The Workspace class is the central API for managing data objects.
WorkspaceMode#
Workspace Class#
- class datalab_kernel.workspace.Workspace(backend: WorkspaceBackend | None = None)[source]#
Bases:
objectWorkspace API for data access and persistence.
The Workspace provides a unified interface to access, modify, and persist scientific data objects (signals and images). It automatically selects the appropriate backend:
Standalone mode: Local memory storage with HDF5 persistence
Live mode: Synchronized with a running DataLab instance
Example:
# List objects workspace.list() # Get an object img = workspace.get("i042") # Add a new object workspace.add("filtered", processed_img) # Save to file workspace.save("analysis.h5")
- resync() bool[source]#
Attempt to resync with DataLab via Web API.
If currently in standalone mode and DataLab Web API becomes available, switch to live mode. Objects in the standalone workspace are transferred to DataLab.
- Returns:
- True if switched to live mode, False if already live or
DataLab Web API is not available.
- connect(url: str | None = None, token: str | None = None) bool[source]#
Connect to DataLab Web API.
Attempts to establish a connection to DataLab using the Web API. If no URL/token are provided, auto-discovery is attempted using:
Environment variables (DATALAB_WORKSPACE_URL, DATALAB_WORKSPACE_TOKEN)
Connection file written by DataLab
URL query parameters (for JupyterLite)
Well-known port probing (http://127.0.0.1:18080)
If currently in standalone mode with objects, they will be transferred to the DataLab workspace.
- Parameters:
url – DataLab Web API URL (e.g., “http://127.0.0.1:18080”). If None, attempts auto-discovery.
token – Authentication token. If None, attempts auto-discovery. May be omitted if server allows localhost connections without token.
- Returns:
True if connected successfully, False otherwise.
Example:
# Auto-discover DataLab (recommended) workspace.connect() # Connect with explicit credentials workspace.connect("http://127.0.0.1:18080", "my-token")
- status() dict[source]#
Get current workspace status.
- Returns:
Dictionary with mode, backend type, and connection info.
Example:
>>> workspace.status() {'mode': 'live', 'backend': 'WebApiBackend', 'url': 'http://127.0.0.1:8080'}
- property mode: WorkspaceMode#
Get current execution mode.
- get(name: str) DataObject[source]#
Retrieve an object by name.
- Parameters:
name – Object name
- Returns:
The requested object (SignalObj or ImageObj)
- Raises:
KeyError – If object not found
- add(name: str, obj: DataObject, overwrite: bool = False) DataObject[source]#
Add an object to the workspace.
- Parameters:
name – Object name
obj – Object to add (SignalObj or ImageObj)
overwrite – If True, replace existing object with same name
- Returns:
The added object
- Raises:
ValueError – If object exists and overwrite=False
- remove(name: str) None[source]#
Remove an object from the workspace.
- Parameters:
name – Object name
- Raises:
KeyError – If object not found
- rename(old_name: str, new_name: str) None[source]#
Rename an object.
- Parameters:
old_name – Current object name
new_name – New object name
- Raises:
KeyError – If old_name not found
ValueError – If new_name already exists
- exists(name: str) bool[source]#
Check if an object exists.
- Parameters:
name – Object name
- Returns:
True if object exists
- save(filepath: str) None[source]#
Save workspace to HDF5 file.
- Parameters:
filepath – Path to save file (should end with .h5)
- load(filepath: str) None[source]#
Load workspace from HDF5 file.
- Parameters:
filepath – Path to HDF5 file
- select_objects(names: list[str], panel: str | None = None) tuple[list[str], str][source]#
Select objects by name in DataLab.
This method is only available in live mode. It selects the specified objects, making them the active selection for subsequent operations.
- Parameters:
names – List of object names/titles to select.
panel – Panel name (“signal” or “image”). None = auto-detect.
- Returns:
Tuple of (list of selected names, panel name).
- Raises:
RuntimeError – If not in live mode.
KeyError – If any object not found.
ValueError – If objects span multiple panels.
Example:
# Select objects before calling calc workspace.select_objects(["signal1", "signal2"]) workspace.calc("average")
- calc(name: str, param: object | None = None) object | None[source]#
Call a DataLab computation function.
This method is only available in live mode. It calls DataLab’s computation feature by name on the currently selected objects.
- Parameters:
name – Computation function name (e.g., “normalize”, “fft”, “denoise”)
param – Optional parameter DataSet or dict for the computation
- Returns:
Tuple of (success, list of new object names), or None if backend doesn’t support returning results.
- Raises:
RuntimeError – If not in live mode
ValueError – If computation function not found
Example:
# Simple computation (select objects first) workspace.select_objects(["my_signal"]) workspace.calc("normalize") # Computation with parameters workspace.calc("moving_average", {"n": 5}) # Or with DataSet from sigima.params import MovingAverageParam workspace.calc("moving_average", MovingAverageParam.create(n=5))