Skip to content

Commit

Permalink
Improve performance by returning dataset immediately in create_dataset
Browse files Browse the repository at this point in the history
Also update create_group and __getitem__ to use a call to use the
common _group and _dataset functions.
  • Loading branch information
dragly committed Mar 10, 2018
1 parent accb276 commit b3db1f5
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions exdir/core/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def create_dataset(self, name, shape=None, dtype=None,
exob._create_object_directory(dataset_directory, exob.DATASET_TYPENAME)

# TODO DRY violation, same as dataset._reset_data, but we have already called _prepare_write
dataset = self[name]
dataset = self._dataset(name)
np.save(dataset.data_filename, data)
dataset.attrs = attrs
dataset.meta["plugins"] = meta
Expand All @@ -117,6 +117,9 @@ def create_group(self, name):

group_directory = self.directory / path
exob._create_object_directory(group_directory, exob.GROUP_TYPENAME)
return self._group(name)

def _group(self, name):
return Group(
root_directory=self.root_directory,
parent_path=self.relative_path,
Expand Down Expand Up @@ -241,30 +244,26 @@ def __getitem__(self, name):
with meta_filename.open("r", encoding="utf-8") as meta_file:
meta_data = yaml.safe_load(meta_file)
if meta_data[exob.EXDIR_METANAME][exob.TYPE_METANAME] == exob.DATASET_TYPENAME:
return ds.Dataset(
root_directory=self.root_directory,
parent_path=self.relative_path,
object_name=name,
io_mode=self.io_mode,
name_validation=self.name_validation,
plugin_manager=self.plugin_manager
)
return self._dataset(name)
elif meta_data[exob.EXDIR_METANAME][exob.TYPE_METANAME] == exob.GROUP_TYPENAME:
return Group(
root_directory=self.root_directory,
parent_path=self.relative_path,
object_name=name,
io_mode=self.io_mode,
name_validation=self.name_validation,
plugin_manager=self.plugin_manager
)
return self._group(name)
else:
print(
"Object", name, "has data type",
meta_data[exob.EXDIR_METANAME][exob.TYPE_METANAME]
)
raise NotImplementedError("Cannot open objects of this type")

def _dataset(self, name):
return ds.Dataset(
root_directory=self.root_directory,
parent_path=self.relative_path,
object_name=name,
io_mode=self.io_mode,
name_validation=self.name_validation,
plugin_manager=self.plugin_manager
)

def __setitem__(self, name, value):
path = utils.path.name_to_asserted_group_path(name)
if len(path.parts) > 1:
Expand Down

0 comments on commit b3db1f5

Please sign in to comment.