Skip to content

Commit

Permalink
Merge remote-tracking branch 'gnode/no-bindings-dev' into v1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
achilleas-k committed Sep 22, 2018
2 parents 911c93e + 5a64cb3 commit 54d55af
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 9 deletions.
3 changes: 3 additions & 0 deletions nixio/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self, nixparent, h5group, compression=Compression.Auto):
self._tags = None
self._multi_tags = None
self._sources = None
self._compr = compression

@classmethod
def _create_new(cls, nixparent, h5parent, name, type_, compression):
Expand Down Expand Up @@ -175,6 +176,8 @@ def create_data_array(self, name, array_type, dtype=None, shape=None,
data_arrays = self._h5group.open_group("data_arrays")
if name in data_arrays:
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)
if data is not None:
Expand Down
4 changes: 2 additions & 2 deletions nixio/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
# Redistribution and use in source and binary forms, with or without
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the Project.
from enum import Enum

# TODO: Convert to enum
class Compression(object):
class Compression(Enum):
No = "None"
DeflateNormal = "DeflateNormal"
Auto = "Auto"
7 changes: 5 additions & 2 deletions nixio/data_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .dimensions import (Dimension, SampledDimension, RangeDimension,
SetDimension, DimensionType, DimensionContainer)
from . import util
from .compression import Compression

from .exceptions import InvalidUnit
from .section import Section
Expand All @@ -39,8 +40,10 @@ def _create_new(cls, nixparent, h5parent, name, type_, data_type, shape,
compression):
newentity = super(DataArray, cls)._create_new(nixparent, h5parent,
name, type_)
newentity._h5group.create_dataset("data", shape, data_type,
compression)
datacompr = False
if compression == Compression.DeflateNormal:
datacompr = True
newentity._h5group.create_dataset("data", shape, data_type, datacompr)
return newentity

def _read_data(self, sl=None):
Expand Down
12 changes: 8 additions & 4 deletions nixio/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ def __init__(self, path, mode=FileMode.ReadWrite,
:param path: Path to file
:param mode: FileMode ReadOnly, ReadWrite, or Overwrite.
(default: ReadWrite)
:param compression: No, DeflateNormal, Auto
(default: Auto)
:param compression: No, DeflateNormal, Auto (default: Auto)
:return: nixio.File object
"""
try:
Expand Down Expand Up @@ -130,6 +129,8 @@ def __init__(self, path, mode=FileMode.ReadWrite,
self.force_created_at()
if "updated_at" not in self._h5file.attrs:
self.force_updated_at()
if compression == Compression.Auto:
compression = Compression.No
self._compr = compression

# make container props but don't initialise
Expand Down Expand Up @@ -274,21 +275,24 @@ def close(self):
self._h5file.close()

# Block
def create_block(self, name, type_):
def create_block(self, name, type_, compression=Compression.Auto):
"""
Create a new block inside the file.
:param name: The name of the block to create.
:type name: str
:param type_: The type of the block.
:type type_: str
:param compression: No, DeflateNormal, Auto (default: Auto)
:returns: The newly created block.
:rtype: Block
"""
if name in self._data:
raise ValueError("Block with the given name already exists!")
block = Block._create_new(self, self._data, name, type_, self._compr)
if compression == Compression.Auto:
compression = self._compr
block = Block._create_new(self, self._data, name, type_, compression)
return block

# Section
Expand Down
2 changes: 1 addition & 1 deletion nixio/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _create_new(cls, nixparent, h5parent, name, type_, oid=None):
return newentity

# Section
def create_section(self, name, type_, oid=None):
def create_section(self, name, type_="undefined", oid=None):
"""
Creates a new subsection that is a child of this section entity.
Expand Down
77 changes: 77 additions & 0 deletions nixio/test/test_compression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
# Copyright © 2017, German Neuroinformatics Node (G-Node)
#
# All rights reserved.
#
# Redistribution and use in section and binary forms, with or without
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the Project.
import os
import random
import nixio as nix
import unittest
from .tmp import TempDir



def compr_enabled(da):
grp = da._h5group.group
h5data = grp.require_dataset("data", shape=(1,), dtype=int)
return h5data.compression == "gzip"


class TestCompression(unittest.TestCase):

def setUp(self):
self.tmpdir = TempDir("compressiontest")
self.testfilename = os.path.join(self.tmpdir.path,
"compressiontest.nix")

def tearDown(self):
pass

def test_compress_dataarray(self):

exception = ValueError("Unknown compression value in test")
for filecompr in nix.Compression:
nf = nix.File.open(self.testfilename, nix.FileMode.Overwrite,
compression=filecompr)
for blockcompr in nix.Compression:
block = nf.create_block("block-{}".format(blockcompr),
"block", compression=blockcompr)
for dacompr in nix.Compression:
da = block.create_data_array("da-{}".format(dacompr),
"data", data=[0],
compression=dacompr)

if dacompr == nix.Compression.No:
comprenabled = False
elif dacompr == nix.Compression.DeflateNormal:
comprenabled = True
elif dacompr == nix.Compression.Auto:
# inherited from Block setting
if blockcompr == nix.Compression.No:
comprenabled = False
elif blockcompr == nix.Compression.DeflateNormal:
comprenabled = True
elif blockcompr == nix.Compression.Auto:
# inherited from File setting
if filecompr == nix.Compression.No:
comprenabled = False
elif filecompr == nix.Compression.DeflateNormal:
comprenabled = True
elif filecompr == nix.Compression.Auto:
comprenabled = False
else:
raise exception
else:
raise exception
else:
raise exception

errmsg = ("Compression: File [{}] Block [{}] Data [{}] "
"Expected compression {}".format(
filecompr, blockcompr, dacompr, comprenabled
))
self.assertEqual(compr_enabled(da), comprenabled, errmsg)
nf.close()

0 comments on commit 54d55af

Please sign in to comment.