Skip to content
Snippets Groups Projects
Commit acc4e36f authored by Edward Fauchon-Jones's avatar Edward Fauchon-Jones
Browse files

Add `lvcnrcheck` test helper module

parent 43e6765f
No related branches found
No related tags found
No related merge requests found
# Copyright (C) 2016 Edward Fauchon-Jones
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from subprocess import Popen, PIPE, STDOUT
from tempfile import NamedTemporaryFile
import os
from lvcnrpy.format.format import format1, format2, format3
from lvcnrpy.format.specs import Spec, DatasetSpec
import h5py as h5
from collections import OrderedDict
import numpy as np
def lvcnrcheck(args):
"""Software API adapter for `lvcnrcheck`
API to `lvcnrcheck` since `lvcnrcheck` only provides a CLI.
Parameters
----------
args: list
List of command line argument to call `lvcnrcheck` with. Typically this
should end with a path to a HDF5 file to validate against the LVCNR
Wavform Reopsitory format specification.
Returns
-------
output: str
Standard output that would be recived from calling `lvcnrcheck` through
the CLI interface with `h5Path`.
"""
pipe = Popen(['lvcnrcheck']+args, stdout=PIPE, stderr=STDOUT)
output = pipe.communicate()[0]
return output
def getFormat1HDF5():
"""Create a temporary HDF5 file that is a valid format 1 file.
Returns
-------
file: NamedTemporaryFile
`NamedTemporaryFile` object that is initialized as a HDF5 file that is
a valid format 1 file.
"""
f = NamedTemporaryFile()
h5f = h5.File(f.name, 'w')
def createLeaves(nodes):
for nodeKey, node in nodes.items():
if isinstance(node, OrderedDict):
createLeaves(node)
elif isinstance(node(), Spec):
if node.dtype == h5.Group:
h5f.create_group(node.name)
continue
elif isinstance(node(), DatasetSpec):
el = [float(i) for i in range(node.componentCount)]
data = np.array([el for i in range(10)])
h5f.create_dataset(node.name, data=data)
continue
elif node.dtype == int:
val = node.values[0] if node.values is not None else 1
elif node.dtype == float:
val = node.values[0] if node.values is not None else 1.0
elif node.dtype == basestring:
val = node.values[0] if node.values is not None else "1.0"
h5f.attrs[node.name] = val
createLeaves(format1)
createLeaves(format2)
createLeaves(format3)
h5f.close()
return f
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment