def loads__(obj): """ De-serializes an object previously serialized with dumps. The returned object is a python Object with attributes: `data`: a numpy array with the object data, and `stats`: an object with a field called 'delta', denoting the distance (on the x scale) of each data value (As of July 2016, data is supposed to stem from equally sampled recordings), and a field called 'starttime' or 'startfreq', denoting the start value of data on the x-scale. This method first attempt is to use obsoy.read function, so it is safe to pass obspy recognized objects Note that if the data type passed in 'dumps' was 'obspytrace' or 'mseed', an obspy Trace or Stream object is returned. This still conforms to the description of the objects attributes and types given above As of July 2016, 'startfreq' is used when the data type passed in 'dumps' was 'fft' (i.e., frequency x-scale), 'starttime' in any other case For forward compatibility (allowing us to be flexible and implement new versions and types) the stats object might contain ANY kind of additional values. The only two reserved keywords, used for the stats default attributes described above, are 'dx' and 'x0' """ # WRONG: FIXME: rewrite doc!! # test: given a an array of 1000 floats # t1 = Trace(np.array(a)) # t2 = {'data':a, 'starttime':t1.stats.starttime.timestamp, 'delta':t1.stats.delta} # len(pickle.dumps(t1)) = 29698 # len(pickle.dumps(t2)) = 6063 # there seems to be a factor of 5 (!!!) # # The main cause is numpy: # np_a = np.array(a) # len(pickle.dumps(np_a)) = 29190 # len(pickle.dumps(a)) = 6006 try: return obspy_read(StringIO(obj)) except TypeError: data = np.load(StringIO(obj)) # instanitate an object ot have access to their attributes # discussion here # http://stackoverflow.com/questions/2827623/python-create-object-and-add-attributes-to-it class DummyTrace(object): """dummy object emulating an Obspy Trace""" pass ret = DummyTrace() for k in data.keys(): setattr(ret, k, data[k]) return ret
def loads(bytestr): """De-serializes the given byte string into a python object. The argument must be a byte string as returned from the `dumps` method of this module, or a bytes string representing an obspy Stream object (using `obspy.core.stream.read` function) :param bytestr: a sequence of bytes from e.g., file or database. :return: a python object :raises: ValueError if bytestr is compressed and a compression error occurred, or if `bytestr` could not be read as `Stream` object """ try: bytestr = decompress(bytestr) except(IOError, zipfile.BadZipfile, zlib.error) as exc: pass # we might actually have uncompressed data which starts accidentally with the wrong # bytes try: return obspy_read(StringIO(bytestr)) except (TypeError, AttributeError, ValueError) as exc: raise ValueError(str(exc))