Skip to content

Commit

Permalink
Merge pull request #462 from achilleas-k/file-references
Browse files Browse the repository at this point in the history
File references
  • Loading branch information
jgrewe authored May 11, 2020
2 parents 9b63d54 + f76a283 commit 7ce8de0
Show file tree
Hide file tree
Showing 24 changed files with 618 additions and 277 deletions.
52 changes: 29 additions & 23 deletions nixio/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@

class Block(Entity):

def __init__(self, nixparent, h5group, compression=Compression.Auto):
super(Block, self).__init__(nixparent, h5group)
def __init__(self, nixfile, nixparent, h5group,
compression=Compression.Auto):
super(Block, self).__init__(nixfile, nixparent, h5group)
self._groups = None
self._data_arrays = None
self._tags = None
Expand All @@ -49,9 +50,10 @@ def __init__(self, nixparent, h5group, compression=Compression.Auto):
self._data_frames = None

@classmethod
def _create_new(cls, nixparent, h5parent, name, type_, compression):
newentity = super(Block, cls)._create_new(nixparent, h5parent,
name, type_)
def create_new(cls, nixparent, h5parent, name, type_, compression):
nixfile = nixparent # file is parent
newentity = super(Block, cls).create_new(nixfile, nixparent, h5parent,
name, type_)
newentity._compr = compression
return newentity

Expand Down Expand Up @@ -101,8 +103,8 @@ def create_multi_tag(self, name="", type_="", positions=None,
"{}-extents".format(type_),
data=extents)
extcreated = True
mtag = MultiTag._create_new(self, multi_tags,
name, type_, positions)
mtag = MultiTag.create_new(self.file, self, multi_tags,
name, type_, positions)
except Exception as e:
msg = "MultiTag Creation Failed"
if poscreated:
Expand Down Expand Up @@ -150,7 +152,7 @@ def create_tag(self, name="", type_="", position=0,
tags = self._h5group.open_group("tags")
if name in tags:
raise exceptions.DuplicateName("create_tag")
tag = Tag._create_new(self, tags, name, type_, position)
tag = Tag.create_new(self.file, self, tags, name, type_, position)
return tag

# Source
Expand All @@ -170,7 +172,7 @@ def create_source(self, name, type_):
sources = self._h5group.open_group("sources")
if name in sources:
raise exceptions.DuplicateName("create_source")
src = Source._create_new(self, sources, name, type_)
src = Source.create_new(self.file, self, sources, name, type_)
return src

# Group
Expand All @@ -190,7 +192,7 @@ def create_group(self, name, type_):
groups = self._h5group.open_group("groups")
if name in groups:
raise exceptions.DuplicateName("open_group")
grp = Group._create_new(self, groups, name, type_)
grp = Group.create_new(self.file, self, groups, name, type_)
return grp

def create_data_array(self, name="", array_type="", dtype=None, shape=None,
Expand Down Expand Up @@ -250,8 +252,8 @@ def create_data_array(self, name="", array_type="", dtype=None, shape=None,
raise exceptions.DuplicateName("create_data_array")
if compression == Compression.Auto:
compression = self._compr
da = DataArray._create_new(self, data_arrays, name, array_type,
dtype, shape, compression)
da = DataArray.create_new(self.file, self, data_arrays, name, array_type,
dtype, shape, compression)
if data is not None:
da.write_direct(data)
return da
Expand Down Expand Up @@ -328,7 +330,7 @@ def create_data_frame(self, name="", type_="", col_dict=None,
)
else: # col_dtypes is None and data is None
raise ValueError(
"The data type of each column have to be specified"
"The data type of each column have to be specified"
)
if len(col_names) != len(col_dict):
raise exceptions.DuplicateColumnName
Expand All @@ -348,7 +350,7 @@ def create_data_frame(self, name="", type_="", col_dict=None,
# data is None or type(data[0]) != np.void
# data_type doesnt matter
raise ValueError(
"No information about column names is provided!"
"No information about column names is provided!"
)

if col_dict is not None:
Expand All @@ -362,8 +364,8 @@ def create_data_frame(self, name="", type_="", col_dict=None,
dt_arr = list(col_dict.items())
col_dtype = np.dtype(dt_arr)

df = DataFrame._create_new(self, data_frames, name,
type_, shape, col_dtype, compression)
df = DataFrame.create_new(self.file, self, data_frames, name,
type_, shape, col_dtype, compression)

if data is not None:
if type(data[0]) == np.void:
Expand Down Expand Up @@ -510,7 +512,7 @@ def sources(self):
This is a read only attribute.
"""
if self._sources is None:
self._sources = SourceContainer("sources", self, Source)
self._sources = SourceContainer("sources", self.file, self, Source)
return self._sources

@property
Expand All @@ -522,7 +524,8 @@ def multi_tags(self):
This is a read only attribute.
"""
if self._multi_tags is None:
self._multi_tags = Container("multi_tags", self, MultiTag)
self._multi_tags = Container("multi_tags", self.file,
self, MultiTag)
return self._multi_tags

@property
Expand All @@ -534,7 +537,7 @@ def tags(self):
This is a read only attribute.
"""
if self._tags is None:
self._tags = Container("tags", self, Tag)
self._tags = Container("tags", self.file, self, Tag)
return self._tags

@property
Expand All @@ -547,13 +550,15 @@ def data_arrays(self):
This is a read only attribute.
"""
if self._data_arrays is None:
self._data_arrays = Container("data_arrays", self, DataArray)
self._data_arrays = Container("data_arrays", self.file,
self, DataArray)
return self._data_arrays

@property
def data_frames(self):
if self._data_frames is None:
self._data_frames = Container("data_frames", self, DataFrame)
self._data_frames = Container("data_frames", self.file,
self, DataFrame)
return self._data_frames

@property
Expand All @@ -565,7 +570,7 @@ def groups(self):
This is a read only attribute.
"""
if self._groups is None:
self._groups = Container("groups", self, Group)
self._groups = Container("groups", self.file, self, Group)
return self._groups

# metadata
Expand All @@ -579,7 +584,8 @@ def metadata(self):
:type: Section
"""
if "metadata" in self._h5group:
return Section(None, self._h5group.open_group("metadata"))
return Section(self.file, None,
self._h5group.open_group("metadata"))
else:
return None

Expand Down
22 changes: 9 additions & 13 deletions nixio/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ class Container(object):
checking and instantiations)
"""

def __init__(self, name, parent, itemclass):
def __init__(self, name, nixfile, parent, itemclass):
self._backend = parent._h5group.open_group(name)
self._itemclass = itemclass
self._file = nixfile
self._parent = parent
self._name = name

def _inst_item(self, item):
return self._itemclass(self._parent, item)
return self._itemclass(self._file, self._parent, item)

def __len__(self):
return len(self._backend)
Expand All @@ -56,10 +57,7 @@ def __delitem__(self, item):
self._itemclass.__name__)
)

root = self._backend.h5root
if not root:
root = self._parent._h5group
root.delete_all([item.id])
self._file._h5group.delete_all([item.id])

def __iter__(self):
for group in self._backend:
Expand Down Expand Up @@ -121,8 +119,7 @@ def __delitem__(self, item):
# the root block
secids = [s.id for s in item.find_sections()]

root = self._backend.file
root.delete_all(secids)
self._file._h5group.delete_all(secids)


class SourceContainer(Container):
Expand All @@ -145,9 +142,7 @@ def __delitem__(self, item):
# the root block
srcids = [s.id for s in item.find_sources()]
srcids.append(item.id)

root = self._backend.h5root
root.delete_all(srcids)
self._file._h5group.delete_all(srcids)


class LinkContainer(Container):
Expand Down Expand Up @@ -180,7 +175,8 @@ class LinkContainer(Container):
"""

def __init__(self, name, parent, itemclass, itemstore):
super(LinkContainer, self).__init__(name, parent, itemclass)
super(LinkContainer, self).__init__(name, parent.file,
parent, itemclass)
self._itemstore = itemstore

def __delitem__(self, item):
Expand Down Expand Up @@ -249,7 +245,7 @@ def __contains__(self, item):
return False

def _inst_item(self, item):
return self._itemclass(self._itemstore._parent, item)
return self._itemclass(self._file, self._itemstore._parent, item)

@staticmethod
def _item_key(item):
Expand Down
Loading

0 comments on commit 7ce8de0

Please sign in to comment.