Exemplo n.º 1
0
 def _func(*args, **kwargs):
     name = kwargs.pop("result_basename", func.__name__)
     rv = func(*args, **kwargs)
     if hasattr(rv, "convert_to_base"):
         rv.convert_to_base()
         _rv = rv.ndarray_view()
     else:
         _rv = rv
     vals = (_rv.min(), _rv.max(), _rv.std(dtype="float64"),
             _rv.sum(dtype="float64"), _rv.size,
             hashlib.md5(_rv.tostring()).hexdigest())
     fn = "func_results_ref_%s.cpkl" % (name)
     if not os.path.exists(fn):
         print("Answers need to be created with --answer-reference .")
         return False
     with open(fn, "rb") as f:
         ref = cPickle.load(f)
     print("Sizes: %s (%s, %s)" % (vals[4] == ref[4], vals[4], ref[4]))
     assert_allclose(vals[0], ref[0], 1e-8, err_msg="min")
     assert_allclose(vals[1], ref[1], 1e-8, err_msg="max")
     assert_allclose(vals[2], ref[2], 1e-8, err_msg="std")
     assert_allclose(vals[3], ref[3], 1e-8, err_msg="sum")
     assert_equal(vals[4], ref[4])
     print("Hashes equal: %s" % (vals[-1] == ref[-1]))
     return rv
Exemplo n.º 2
0
def test_save_load_pickle():
    """Main test for loading pickled objects"""
    return # Until boolean regions are implemented we can't test this
    test_ds = fake_random_ds(64)

    # create extracted region from boolean (fairly complex object)
    center = (test_ds.domain_left_edge + test_ds.domain_right_edge) / 2
    sp_outer = test_ds.sphere(center, test_ds.domain_width[0])
    sp_inner = test_ds.sphere(center, test_ds.domain_width[0] / 10.0)
    sp_boolean = test_ds.boolean([sp_outer, "NOT", sp_inner])

    minv, maxv = sp_boolean.quantities["Extrema"]("density")[0]
    contour_threshold = min(minv * 10.0, 0.9 * maxv)

    contours = sp_boolean.extract_connected_sets(
        "density", 1, contour_threshold, maxv + 1, log_space=True, cache=True)

    # save object
    cpklfile = tempfile.NamedTemporaryFile(delete=False)
    cPickle.dump(contours[1][0], cpklfile)
    cpklfile.close()

    # load object
    test_load = cPickle.load(open(cpklfile.name, "rb"))

    assert_equal.description = \
        "%s: File was pickle-loaded succesfully" % __name__
    yield assert_equal, test_load is not None, True
    assert_equal.description = \
        "%s: Length of pickle-loaded connected set object" % __name__
    yield assert_equal, len(contours[1][0]), len(test_load)

    os.remove(cpklfile.name)
Exemplo n.º 3
0
 def _func(*args, **kwargs):
     name = kwargs.pop("result_basename", func.__name__)
     rv = func(*args, **kwargs)
     if hasattr(rv, "convert_to_cgs"):
         rv.convert_to_cgs()
         _rv = rv.ndarray_view()
     else:
         _rv = rv
     vals = (_rv.min(),
             _rv.max(),
             _rv.std(dtype="float64"),
             _rv.sum(dtype="float64"),
             _rv.size,
             hashlib.md5(_rv.tostring()).hexdigest() )
     fn = "func_results_ref_%s.cpkl" % (name)
     if not os.path.exists(fn):
         print("Answers need to be created with --answer-reference .")
         return False
     with open(fn, "rb") as f:
         ref = cPickle.load(f)
     print("Sizes: %s (%s, %s)" % (vals[4] == ref[4], vals[4], ref[4]))
     assert_allclose(vals[0], ref[0], 1e-8, err_msg="min")
     assert_allclose(vals[1], ref[1], 1e-8, err_msg="max")
     assert_allclose(vals[2], ref[2], 1e-8, err_msg="std")
     assert_allclose(vals[3], ref[3], 1e-8, err_msg="sum")
     assert_equal(vals[4], ref[4])
     print("Hashes equal: %s" % (vals[-1] == ref[-1]))
     return rv
Exemplo n.º 4
0
 def __getitem__(self, test_name):
     if not os.path.exists(self._fn(test_name)):
         raise FileNotExistException(self._fn(test_name))
     f = open(self._fn(test_name), "rb")
     tr = cPickle.load(f)
     f.close()
     return tr
Exemplo n.º 5
0
 def __getitem__(self, test_name):
     if not os.path.exists(self._fn(test_name)):
         raise FileNotExistException(self._fn(test_name))
     f = open(self._fn(test_name), "rb")
     tr = cPickle.load(f)
     f.close()
     return tr
Exemplo n.º 6
0
def test_ytarray_pickle():
    ds = fake_random_ds(64, nprocs=1)
    test_data = [ds.quan(12.0, 'code_length'),
                 ds.arr([1, 2, 3], 'code_length')]

    for data in test_data:
        tempf = tempfile.NamedTemporaryFile(delete=False)
        pickle.dump(data, tempf)
        tempf.close()

        with open(tempf.name, "rb") as fname:
            loaded_data = pickle.load(fname)
        os.unlink(tempf.name)

        assert_array_equal(data, loaded_data)
        assert_equal(data.units, loaded_data.units)
        assert_array_equal(array(data.in_cgs()), array(loaded_data.in_cgs()))
        assert_equal(float(data.units.base_value), float(loaded_data.units.base_value))
def test_ytarray_pickle():
    ds = fake_random_ds(64, nprocs=1)
    test_data = [ds.quan(12.0, 'code_length'),
                 ds.arr([1, 2, 3], 'code_length')]

    for data in test_data:
        tempf = tempfile.NamedTemporaryFile(delete=False)
        pickle.dump(data, tempf)
        tempf.close()

        with open(tempf.name, "rb") as fname:
            loaded_data = pickle.load(fname)
        os.unlink(tempf.name)

        yield assert_array_equal, data, loaded_data
        yield assert_equal, data.units, loaded_data.units
        yield assert_array_equal, array(data.in_cgs()), \
            array(loaded_data.in_cgs())
        yield assert_equal, float(data.units.base_value), \
            float(loaded_data.units.base_value)
Exemplo n.º 8
0
 def load_tree(self, filename):
     self.redshifts, self.relationships = \
                     cPickle.load(open(filename, "rb"))
 def load(self, storage):
     return pickle.load(open(storage, 'r'))
 def load(self, storage):
     return pickle.load(open(storage, 'r'))
 def load_tree(self, filename):
     self.redshifts, self.relationships = \
                     cPickle.load(open(filename, "rb"))