-
Notifications
You must be signed in to change notification settings - Fork 3
Backend abstraction: introduce DataConnection/ProcessConnection ABCs #523
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
samc24
wants to merge
8
commits into
dev
Choose a base branch
from
sameerc/backend-abc
base: dev
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f217240
Add DataConnection and ProcessConnection ABCs
samc24 3d0dbdb
Adapt MDSConnection to implement DataConnection ABC
samc24 7ec6546
Split XarrayConnection into process-level and per-shot classes
samc24 e457c5a
Update consumers to use DataConnection and ProcessConnection ABCs
samc24 b8b2bb6
Merge branch 'dev' into sameerc/backend-abc
gtrevisan f70ae59
add shebangs
gtrevisan 2f9a71f
Address PR #523 review feedback
samc24 54bbba6
Merge branch 'dev' into sameerc/backend-abc
gtrevisan 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,7 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| """Data connection abstractions and implementations.""" | ||
|
|
||
| from disruption_py.inout.base import DataConnection, ProcessConnection | ||
|
|
||
| __all__ = ["DataConnection", "ProcessConnection"] |
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,125 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| """ | ||
| Abstract base classes for data connections. | ||
|
|
||
| DataConnection: per-shot data access (get_data, get_data_with_dims, get_dims). | ||
| ProcessConnection: per-process factory that creates DataConnection instances. | ||
| """ | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from typing import List, Tuple | ||
|
|
||
| import numpy as np | ||
|
|
||
| from disruption_py.machine.tokamak import Tokamak | ||
|
|
||
|
|
||
| class DataConnection(ABC): | ||
samc24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Per-shot data access interface. | ||
|
|
||
| Each instance is bound to a single shot. Implementations must provide | ||
| get_data, get_data_with_dims, get_dims, and cleanup. The reconnect | ||
| method is optional (default no-op). | ||
| """ | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def shot_id(self) -> int: | ||
| """The shot ID this connection is bound to.""" | ||
|
|
||
| @abstractmethod | ||
| def get_data(self, path: str, group: str = None, **kwargs) -> np.ndarray: | ||
| """Get data at path. | ||
samc24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Parameters | ||
| ---------- | ||
| path : str | ||
| Data path (node path for MDSplus, variable name for Xarray). | ||
| group : str, optional | ||
| Container name (tree for MDSplus, group for Xarray). | ||
| **kwargs | ||
| Backend-specific options. | ||
|
|
||
| Returns | ||
| ------- | ||
| np.ndarray | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def get_data_with_dims( | ||
| self, | ||
| path: str, | ||
| group: str = None, | ||
| dim_nums: List = None, | ||
| **kwargs, | ||
| ) -> Tuple: | ||
| """Get data and dimension arrays. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| path : str | ||
| Data path. | ||
| group : str, optional | ||
| Container name. | ||
| dim_nums : List, optional | ||
| Dimension indices to retrieve. Default [0]. | ||
| **kwargs | ||
| Backend-specific options. | ||
|
|
||
| Returns | ||
| ------- | ||
| Tuple | ||
| (data, dim0, dim1, ...) as numpy arrays. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def get_dims( | ||
| self, | ||
| path: str, | ||
| group: str = None, | ||
| dim_nums: List = None, | ||
| **kwargs, | ||
| ) -> Tuple: | ||
| """Get only dimension arrays. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| path : str | ||
| Data path. | ||
| group : str, optional | ||
| Container name. | ||
| dim_nums : List, optional | ||
| Dimension indices to retrieve. Default [0]. | ||
| **kwargs | ||
| Backend-specific options. | ||
|
|
||
| Returns | ||
| ------- | ||
| Tuple | ||
| Requested dimensions. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def cleanup(self) -> None: | ||
| """Release resources for this shot.""" | ||
|
|
||
| def reconnect(self) -> None: | ||
| """Reconnect after error. Default no-op. | ||
|
|
||
| Xarray opens a new DataTree per shot so there is nothing to | ||
| reconnect. MDSplus overrides this to call conn.reconnect(). | ||
| """ | ||
|
|
||
|
|
||
| class ProcessConnection(ABC): | ||
| """Per-process factory that creates DataConnection instances.""" | ||
|
|
||
| @abstractmethod | ||
| def get_shot_connection(self, shot_id: int) -> DataConnection: | ||
| """Create a per-shot DataConnection for the given shot.""" | ||
|
|
||
| @classmethod | ||
| @abstractmethod | ||
| def from_config(cls, tokamak: Tokamak) -> "ProcessConnection": | ||
| """Create a ProcessConnection from tokamak configuration.""" | ||
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.
Uh oh!
There was an error while loading. Please reload this page.