[plot] works for 1 and 2d categories
This commit is contained in:
@@ -28,10 +28,10 @@ class DataView():
|
||||
for i, x in enumerate(zip(self._offset, valid_count)):
|
||||
if i == self._cut_dim:
|
||||
new_ofst[i] = sum(x)
|
||||
|
||||
|
||||
self._offset = tuple(new_ofst)
|
||||
self._fetched_data = tuple([sum(x) for x in zip(self._fetched_data, valid_count)])
|
||||
|
||||
|
||||
def init_chunking(self):
|
||||
"""decides on the chunks size for reading. Heuristic is based on the dimensionality of the data and the "best xdim" if available.
|
||||
If data is 2D the best xdim is loaded in chunks (if necessary) while the other is fully loaded. For 3D and more it is the last dimension that is cut. If the number of data points in the first n-1 dimensions exceeds the maximum chunksize (settings) an error will be thrown.
|
||||
@@ -59,16 +59,15 @@ class DataView():
|
||||
self._buffer = np.empty(self._full_shape)
|
||||
except:
|
||||
raise ValueError("Error reserving buffer! Cannot handle so many data points!") #FIXME
|
||||
print("init buffer")
|
||||
|
||||
@property
|
||||
def fully_loaded(self):
|
||||
return np.all(self._buffer is not None and self._fetched_data == self._full_shape)
|
||||
|
||||
|
||||
@property
|
||||
def full_shape(self):
|
||||
return self._full_shape
|
||||
|
||||
|
||||
@property
|
||||
def current_shape(self):
|
||||
return self._fetched_data
|
||||
@@ -79,4 +78,3 @@ class DataView():
|
||||
r += " max chunk size: " + str(self._count)
|
||||
r += " is fully loaded: " + str(self.fully_loaded)
|
||||
return r
|
||||
|
||||
@@ -8,6 +8,7 @@ from nixview.util.enums import NodeType, PlotterTypes
|
||||
|
||||
class Singleton(type):
|
||||
_instances = {}
|
||||
|
||||
def __call__(cls, *args, **kwargs):
|
||||
if cls not in cls._instances:
|
||||
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
||||
@@ -19,29 +20,29 @@ class EntityBuffer():
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._buffer = {}
|
||||
|
||||
|
||||
def put(self, entity):
|
||||
if not hasattr(entity, "id"):
|
||||
return
|
||||
id = entity.id
|
||||
if id not in self._buffer.keys():
|
||||
self._buffer[id] = entity
|
||||
|
||||
|
||||
def has(self, id):
|
||||
return id in self._buffer.keys()
|
||||
|
||||
|
||||
def get(self, id):
|
||||
if self.has(id):
|
||||
return self._buffer[id]
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def clear(self):
|
||||
self._buffer.clear()
|
||||
|
||||
|
||||
class FileHandler(metaclass=Singleton):
|
||||
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._filename = None
|
||||
@@ -50,24 +51,24 @@ class FileHandler(metaclass=Singleton):
|
||||
self._entity_buffer = EntityBuffer()
|
||||
self._file_descriptor = None
|
||||
self._file_version = None
|
||||
|
||||
|
||||
def open(self, filename):
|
||||
self.close()
|
||||
|
||||
|
||||
if not os.path.exists(filename):
|
||||
return False, "File %s could not be found!" % filename
|
||||
try:
|
||||
self._nix_file = nix.File.open(filename, nix.FileMode.ReadOnly)
|
||||
self._filename = filename
|
||||
self._file_descriptor = FileDescriptor(self.filename, self._nix_file.format, self._nix_file.version,
|
||||
self._nix_file.created_at, self._nix_file.updated_at, os.path.getsize(self.filename)/1e+6)
|
||||
self._file_descriptor = FileDescriptor(self.filename, self._nix_file.format, self._nix_file.version,
|
||||
self._nix_file.created_at, self._nix_file.updated_at, os.path.getsize(self.filename) / 1e+6)
|
||||
self.file_descriptor.block_count = len(self._nix_file.blocks)
|
||||
for b in self._nix_file.blocks:
|
||||
self.file_descriptor.data_array_count += len(b.data_arrays)
|
||||
self.file_descriptor.group_count += len(b.groups)
|
||||
self.file_descriptor.tag_count += len(b.tags)
|
||||
self.file_descriptor.tag_count += len(b.multi_tags)
|
||||
if hasattr(b, "data_frames"):
|
||||
if hasattr(b, "data_frames"):
|
||||
self.file_descriptor.data_frame_count += len(b.data_frames)
|
||||
self._file_version = self._nix_file.version
|
||||
return True, "Successfully opened file %s." % filename.split(os.sep)[-1]
|
||||
@@ -92,11 +93,11 @@ class FileHandler(metaclass=Singleton):
|
||||
@property
|
||||
def is_valid(self):
|
||||
return self._nix_file is not None and self._nix_file.is_open()
|
||||
|
||||
|
||||
@property
|
||||
def filename(self):
|
||||
return self._filename
|
||||
|
||||
|
||||
def valid_count(self, shape, offset, count):
|
||||
valid_count = np.empty(len(shape), dtype=int)
|
||||
for i, (o, c) in enumerate(zip(offset, count)):
|
||||
@@ -105,13 +106,13 @@ class FileHandler(metaclass=Singleton):
|
||||
else:
|
||||
valid_count[i] = c
|
||||
return valid_count
|
||||
|
||||
|
||||
def count_is_valid(self, shape, offset, count):
|
||||
res = True
|
||||
for s, o, c in zip(shape, offset, count):
|
||||
res = res and o + c <= s
|
||||
return res
|
||||
|
||||
|
||||
def request_data(self, entity_descriptor, offset=None, count=None):
|
||||
entity = self._entity_buffer.get(entity_descriptor.id)
|
||||
if entity is None:
|
||||
@@ -131,7 +132,7 @@ class FileHandler(metaclass=Singleton):
|
||||
else:
|
||||
item = ItemDescriptor(fs.name, fs.id, fs.type, definition=fs.definition, entity_type=NodeType.Section)
|
||||
return item
|
||||
|
||||
|
||||
def request_metadata(self, root_id=None, depth=1):
|
||||
"""[summary]
|
||||
|
||||
@@ -145,19 +146,19 @@ class FileHandler(metaclass=Singleton):
|
||||
self._entity_buffer.put(s)
|
||||
sub_sections.append(ItemDescriptor(s.name, s.id, s.type, definition=s.definition, entity_type=NodeType.Section))
|
||||
return sub_sections
|
||||
|
||||
|
||||
def get_properties(section):
|
||||
props = []
|
||||
for p in section.props:
|
||||
value = ""
|
||||
if self._file_version < (1,1,1):
|
||||
vals = p.values
|
||||
if self._file_version < (1, 1, 1):
|
||||
vals = p.values
|
||||
if len(vals) > 1:
|
||||
value += "["
|
||||
value += ",".join(map(str, [v.value for v in vals]))
|
||||
value += "]"
|
||||
else:
|
||||
value = str(vals[0].value)
|
||||
value = str(vals[0].value)
|
||||
else:
|
||||
vals = p.values
|
||||
value += "["
|
||||
@@ -207,7 +208,7 @@ class FileHandler(metaclass=Singleton):
|
||||
point_or_segment = "segment" if e.extent else "point"
|
||||
start = str(e.position)
|
||||
end = ("to " + str(tuple(np.array(e.position) + np.array(e.extent)))) if e.extent else ""
|
||||
itd.value = "tags %s %s %s" %(point_or_segment, start, end)
|
||||
itd.value = "tags %s %s %s" % (point_or_segment, start, end)
|
||||
# TODO set the value to something meaningful for the various entity types
|
||||
return infos
|
||||
|
||||
@@ -219,7 +220,7 @@ class FileHandler(metaclass=Singleton):
|
||||
if not b:
|
||||
b = self._nix_file.blocks[id]
|
||||
return b
|
||||
|
||||
|
||||
def request_data_arrays(self, block_id):
|
||||
b = self.get_block(block_id)
|
||||
return self._entity_info(b.data_arrays, block_id, NodeType.DataArray)
|
||||
@@ -262,10 +263,11 @@ class FileHandler(metaclass=Singleton):
|
||||
dimensions = []
|
||||
for i, d in enumerate(da.dimensions):
|
||||
dim_name = "%i. dim: %s" % (i+1, d.label if hasattr(d, "label") else "")
|
||||
dim_type= "%s %s" % (d.dimension_type, "dimension")
|
||||
dim_type = "%s %s" % (d.dimension_type, "dimension")
|
||||
unit = d.unit if hasattr(d, "unit") else None
|
||||
label = d.label if hasattr(d, "label") else None
|
||||
dimensions.append(ItemDescriptor(dim_name, type=dim_type, entity_type=NodeType.Dimension, block_id=block_id, unit=unit, label=label))
|
||||
dimensions.append(ItemDescriptor(dim_name, type=dim_type, entity_type=NodeType.Dimension,
|
||||
block_id=block_id, unit=unit, label=label))
|
||||
return dimensions
|
||||
|
||||
def request_axis(self, block_id, array_id, dimension_index, count, start=0):
|
||||
@@ -273,6 +275,7 @@ class FileHandler(metaclass=Singleton):
|
||||
if da is None:
|
||||
b = self.get_block(block_id)
|
||||
da = b.data_arrays[array_id]
|
||||
|
||||
dim = da.dimensions[dimension_index]
|
||||
if dim.dimension_type == nix.DimensionType.Set:
|
||||
labels = dim.labels
|
||||
@@ -280,7 +283,7 @@ class FileHandler(metaclass=Singleton):
|
||||
raise ValueError("Invalid argument for start or count for SetDimension")
|
||||
axis = labels[start:start + count]
|
||||
if len(labels) == 0:
|
||||
axis = list(map(str, range(start, start+count)))
|
||||
axis = list(map(str, range(start, start+count)))
|
||||
else:
|
||||
axis = np.asarray(dim.axis(count, start))
|
||||
return axis
|
||||
@@ -311,7 +314,7 @@ class FileHandler(metaclass=Singleton):
|
||||
for src in srcs:
|
||||
sources.extend(get_subsources(src))
|
||||
return sources
|
||||
|
||||
|
||||
def guess_best_xdim(self, array):
|
||||
data_extent = array.shape
|
||||
if len(data_extent) > 2:
|
||||
@@ -320,23 +323,24 @@ class FileHandler(metaclass=Singleton):
|
||||
if len(data_extent) == 1:
|
||||
return 0
|
||||
|
||||
d1 = array.dimensions[0]
|
||||
d2 = array.dimensions[1]
|
||||
d0 = array.dimensions[0]
|
||||
d1 = array.dimensions[1]
|
||||
shape = array.data_extent
|
||||
|
||||
if d1.dimension_type == nix.DimensionType.Sample:
|
||||
if d0.dimension_type == nix.DimensionType.Sample:
|
||||
return 0
|
||||
elif d2.dimension_type == nix.DimensionType.Sample:
|
||||
elif d1.dimension_type == nix.DimensionType.Sample:
|
||||
return 1
|
||||
else:
|
||||
if (d1.dimension_type == nix.DimensionType.Set) and \
|
||||
(d2.dimension_type == nix.DimensionType.Range):
|
||||
if (d0.dimension_type == nix.DimensionType.Set) and \
|
||||
(d1.dimension_type == nix.DimensionType.Range):
|
||||
return 1
|
||||
elif (d1.dimension_type == nix.DimensionType.Range) and \
|
||||
(d2.dimension_type == nix.DimensionType.Set):
|
||||
return 0
|
||||
elif (d0.dimension_type == nix.DimensionType.Set) and \
|
||||
(d1.dimension_type == nix.DimensionType.Set):
|
||||
return int(np.argmax(shape))
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
def suggested_plotter(self, array):
|
||||
if len(array.dimensions) > 3:
|
||||
print("cannot handle more than 3D")
|
||||
@@ -358,19 +362,19 @@ class FileHandler(metaclass=Singleton):
|
||||
elif dim_count == 2:
|
||||
if dim_types[0] == nix.DimensionType.Sample:
|
||||
if dim_types[1] == nix.DimensionType.Sample or \
|
||||
dim_types[1] == nix.DimensionType.Range:
|
||||
dim_types[1] == nix.DimensionType.Range:
|
||||
return PlotterTypes.ImagePlotter
|
||||
else:
|
||||
return PlotterTypes.LinePlotter
|
||||
elif dim_types[0] == nix.DimensionType.Range:
|
||||
if dim_types[1] == nix.DimensionType.Sample or \
|
||||
dim_types[1] == nix.DimensionType.Range:
|
||||
dim_types[1] == nix.DimensionType.Range:
|
||||
return PlotterTypes.ImagePlotter
|
||||
else:
|
||||
return PlotterTypes.LinePlotter
|
||||
elif dim_types[0] == nix.DimensionType.Set:
|
||||
if dim_types[1] == nix.DimensionType.Sample or \
|
||||
dim_types[1] == nix.DimensionType.Range:
|
||||
dim_types[1] == nix.DimensionType.Range:
|
||||
return PlotterTypes.LinePlotter
|
||||
else:
|
||||
return PlotterTypes.CategoryPlotter
|
||||
|
||||
Reference in New Issue
Block a user