| 1 |
# Copyright (c) 2013-2016, Freja Nordsiek |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# Redistribution and use in source and binary forms, with or without |
| 5 |
# modification, are permitted provided that the following conditions are |
| 6 |
# met: |
| 7 |
# |
| 8 |
# 1. Redistributions of source code must retain the above copyright |
| 9 |
# notice, this list of conditions and the following disclaimer. |
| 10 |
# |
| 11 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
# notice, this list of conditions and the following disclaimer in the |
| 13 |
# documentation and/or other materials provided with the distribution. |
| 14 |
# |
| 15 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 18 |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 19 |
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 21 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
""" Module of Exceptions and low level read and write functions. |
| 27 |
|
| 28 |
""" |
| 29 |
|
| 30 |
import posixpath |
| 31 |
|
| 32 |
import numpy as np |
| 33 |
import h5py |
| 34 |
|
| 35 |
from hdf5storage.utilities import * |
| 36 |
|
| 37 |
|
| 38 |
class Hdf5storageError(IOError): |
| 39 |
""" Base class of hdf5storage package exceptions.""" |
| 40 |
pass |
| 41 |
|
| 42 |
|
| 43 |
class CantReadError(Hdf5storageError): |
| 44 |
""" Exception for a failure to read the desired data.""" |
| 45 |
pass |
| 46 |
|
| 47 |
|
| 48 |
class TypeNotMatlabCompatibleError(Hdf5storageError): |
| 49 |
""" Exception for trying to write non-MATLAB compatible data. |
| 50 |
|
| 51 |
In the event that MATLAB compatibility is being done |
| 52 |
(``Options.matlab_compatible``) and a Python type is not importable |
| 53 |
by MATLAB, the data is either not written or this exception is |
| 54 |
thrown depending on the value of |
| 55 |
``Options.action_for_matlab_incompatible``. |
| 56 |
|
| 57 |
See Also |
| 58 |
-------- |
| 59 |
hdf5storage.Options.matlab_compatible |
| 60 |
hdf5storage.Options.action_for_matlab_incompatible |
| 61 |
|
| 62 |
""" |
| 63 |
pass |
| 64 |
|
| 65 |
|
| 66 |
def write_data(f, grp, name, data, type_string, options): |
| 67 |
""" Writes a piece of data into an open HDF5 file. |
| 68 |
|
| 69 |
Low level function to store a Python type (`data`) into the |
| 70 |
specified Group. |
| 71 |
|
| 72 |
Parameters |
| 73 |
---------- |
| 74 |
f : h5py.File |
| 75 |
The open HDF5 file. |
| 76 |
grp : h5py.Group or h5py.File |
| 77 |
The Group to place the data in. |
| 78 |
name : str |
| 79 |
The name to write the data to. |
| 80 |
data : any |
| 81 |
The data to write. |
| 82 |
type_string : str or None |
| 83 |
The type string of the data, or ``None`` to deduce |
| 84 |
automatically. |
| 85 |
options : hdf5storage.core.Options |
| 86 |
The options to use when writing. |
| 87 |
|
| 88 |
Raises |
| 89 |
------ |
| 90 |
NotImplementedError |
| 91 |
If writing `data` is not supported. |
| 92 |
TypeNotMatlabCompatibleError |
| 93 |
If writing a type not compatible with MATLAB and |
| 94 |
`options.action_for_matlab_incompatible` is set to ``'error'``. |
| 95 |
|
| 96 |
See Also |
| 97 |
-------- |
| 98 |
hdf5storage.core.write : Higher level version. |
| 99 |
read_data |
| 100 |
hdf5storage.core.Options |
| 101 |
|
| 102 |
""" |
| 103 |
# Get the marshaller for type(data). |
| 104 |
|
| 105 |
tp = type(data) |
| 106 |
m = options.marshaller_collection.get_marshaller_for_type(tp) |
| 107 |
|
| 108 |
# If a marshaller was found, use it to write the data. Otherwise, |
| 109 |
# return an error. If we get something other than None back, then we |
| 110 |
# must recurse through the entries. Also, we must set the H5PATH |
| 111 |
# attribute to be the path to the containing group. |
| 112 |
|
| 113 |
if m is not None: |
| 114 |
m.write(f, grp, name, data, type_string, options) |
| 115 |
else: |
| 116 |
raise NotImplementedError('Can''t write data type: '+str(tp)) |
| 117 |
|
| 118 |
|
| 119 |
def read_data(f, grp, name, options): |
| 120 |
""" Writes a piece of data into an open HDF5 file. |
| 121 |
|
| 122 |
Low level function to read a Python type of the specified name from |
| 123 |
specified Group. |
| 124 |
|
| 125 |
Parameters |
| 126 |
---------- |
| 127 |
f : h5py.File |
| 128 |
The open HDF5 file. |
| 129 |
grp : h5py.Group or h5py.File |
| 130 |
The Group to read the data from. |
| 131 |
name : str |
| 132 |
The name of the data to read. |
| 133 |
options : hdf5storage.core.Options |
| 134 |
The options to use when reading. |
| 135 |
|
| 136 |
Returns |
| 137 |
------- |
| 138 |
data |
| 139 |
The data named `name` in Group `grp`. |
| 140 |
|
| 141 |
Raises |
| 142 |
------ |
| 143 |
CantReadError |
| 144 |
If the data cannot be read successfully. |
| 145 |
|
| 146 |
See Also |
| 147 |
-------- |
| 148 |
hdf5storage.core.read : Higher level version. |
| 149 |
write_data |
| 150 |
hdf5storage.core.Options |
| 151 |
|
| 152 |
""" |
| 153 |
# If name isn't found, return error. |
| 154 |
dsetgrp = grp.get(name) |
| 155 |
if dsetgrp is None: |
| 156 |
raise CantReadError('Could not find ' |
| 157 |
+ posixpath.join(grp.name, name)) |
| 158 |
|
| 159 |
# Get the different attributes that can be used to identify they |
| 160 |
# type, which are the type string and the MATLAB class. |
| 161 |
type_string = get_attribute_string(dsetgrp, 'Python.Type') |
| 162 |
matlab_class = get_attribute_string(dsetgrp, 'MATLAB_class') |
| 163 |
|
| 164 |
# If the type_string is present, get the marshaller for it. If it is |
| 165 |
# not, use the one for the matlab class if it is given. Otherwise, |
| 166 |
# use the fallback (NumpyScalarArrayMarshaller for both Datasets and |
| 167 |
# Groups). If calls to the marshaller collection to get the right |
| 168 |
# marshaller don't return one (return None), we also go to the |
| 169 |
# default). |
| 170 |
|
| 171 |
m = None |
| 172 |
mc = options.marshaller_collection |
| 173 |
if type_string is not None: |
| 174 |
m = mc.get_marshaller_for_type_string(type_string) |
| 175 |
elif matlab_class is not None: |
| 176 |
m = mc.get_marshaller_for_matlab_class(matlab_class) |
| 177 |
|
| 178 |
if m is None: |
| 179 |
m = mc.get_marshaller_for_type(np.uint8) |
| 180 |
|
| 181 |
# If a marshaller was found, use it to write the data. Otherwise, |
| 182 |
# return an error. |
| 183 |
|
| 184 |
if m is not None: |
| 185 |
return m.read(f, grp, name, options) |
| 186 |
else: |
| 187 |
raise CantReadError('Could not read ' + dsetgrp.name) |