Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 14 additions & 36 deletions nbs_viewer/models/data/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def __init__(self, run, key, catalog, parent=None, chunk_cache=None):
self._descriptors = None
self._stream = "primary"
self._max_1d_cache_items = 100

self._accessor = None

# Setup metadata first since it's always available
self.setup()

Expand All @@ -94,9 +95,11 @@ def refresh(self):
self._shape_cache.clear()
self._dim_cache.clear()
self._run_keys_cache = None
self._has_data = None
self._start_doc = None
self._stop_doc = None
self._descriptors = None
self._accessor = None
super().refresh()

def setup(self):
Expand Down Expand Up @@ -153,19 +156,10 @@ def get_md_value(self, keys, default=None):
return value

@property
def num_points(self):
"""
Returns the number of points in the run.

Returns
-------
int
Number of points, or -1 if not defined in metadata
"""
value = self.get_md_value(["stop", "num_events", "primary"], None)
if value is None:
value = self.get_md_value(["start", "num_points"], -1)
return value
def data_accessor(self):
if self._accessor is None:
self._accessor = self._run['primary/data']
return self._accessor

@property
def date(self):
Expand Down Expand Up @@ -223,7 +217,7 @@ def getShape(self, key):
logging.debug(f"Getting shape for key {key}")
try:
# Try to get shape from metadata first
shape = self._run["/".join(["primary", "data", key])].shape
shape = self.data_accessor[key].shape
self._shape_cache[key] = shape
logging.debug("Got shape from metadata")
except (KeyError, AttributeError):
Expand Down Expand Up @@ -320,7 +314,7 @@ def getData(self, key, slice_info=None):
return self._data_cache[key]

try:
data_accessor = self._run["/".join(["primary", "data", key])]
data_accessor = self.data_accessor[key]
data = data_accessor.read()
self._data_cache[key] = data
self._manage_cache(self._data_cache, self._max_1d_cache_items)
Expand All @@ -336,7 +330,7 @@ def getData(self, key, slice_info=None):
if self._chunk_cache is None:
# Fallback to loading full data if no chunk cache available
try:
data_accessor = self._run["/".join(["primary", "data", key])]
data_accessor = self.data_accessor[key]
data = data_accessor.read()
if slice_info is not None:
return data[slice_info]
Expand Down Expand Up @@ -434,7 +428,7 @@ def getRunKeys(self):
category="catalog"
)
try:
all_keys = list(self._run["/".join(["primary", "data"])].keys())
all_keys = list(self.data_accessor.keys())
self._has_data = True
except Exception as e:
print(
Expand Down Expand Up @@ -650,22 +644,6 @@ def get_cache_size(self):

return {"1d_cache": size_1d, "nd_cache": size_nd, "total": size_1d + size_nd}

def _primary_data_path(self, key: str) -> str:
"""
Build the canonical path for a key in the primary data group.

Parameters
----------
key : str
Data key name.

Returns
-------
str
Path into the run object.
"""
return "/".join(["primary", "data", key])

def _infer_dims_from_shape(self, key: str, shape: Tuple[int, ...]) -> Tuple[str, ...]:
"""
Infer dimension names when Tiled structure metadata has no dims.
Expand Down Expand Up @@ -707,7 +685,7 @@ def _infer_dims_from_shape(self, key: str, shape: Tuple[int, ...]) -> Tuple[str,
has_time_key = False
try:
has_time_key = "time" in list(
self._run["/".join(["primary", "data"])].keys()
self.data_accessor.keys()
)
except Exception:
has_time_key = False
Expand Down Expand Up @@ -738,7 +716,7 @@ def _resolve_dims(self, key: str) -> Tuple[str, ...]:
shape = tuple(self.getShape(key))
raw_dims = None
try:
raw_dims = self._run[self._primary_data_path(key)].dims
raw_dims = self.data_accessor[key].dims
except Exception as ex:
print_debug(
"BlueskyRun._resolve_dims",
Expand Down
Loading