Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_list_order():
""" https://github.com/telegraphic/hickle/issues/26 """
d = [np.arange(n + 1) for n in range(20)]
hickle.dump(d, 'test.h5')
d_hkl = hickle.load('test.h5')
try:
for ii, xx in enumerate(d):
assert d[ii].shape == d_hkl[ii].shape
for ii, xx in enumerate(d):
assert np.allclose(d[ii], d_hkl[ii])
except AssertionError:
print(d[ii], d_hkl[ii])
raise
def test_ndarray():
a = np.array([1,2,3])
b = np.array([2,3,4])
z = (a, b)
print "Original:"
pprint(z)
dump(z, 'test.hkl', mode='w')
print "\nReconstructed:"
z = load('test.hkl')
pprint(z)
def test_embedded_array():
""" See https://github.com/telegraphic/hickle/issues/24 """
d_orig = [[np.array([10., 20.]), np.array([10, 20, 30])], [np.array([10, 2]), np.array([1.])]]
hickle.dump(d_orig, 'test.h5')
d_hkl = hickle.load('test.h5')
for ii, xx in enumerate(d_orig):
for jj, yy in enumerate(xx):
assert np.allclose(d_orig[ii][jj], d_hkl[ii][jj])
print(d_hkl)
print(d_orig)
sm0 = csr_matrix((3, 4), dtype=np.int8).toarray()
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
sm1 = csr_matrix((data, (row, col)), shape=(3, 3))
sm2 = csc_matrix((data, (row, col)), shape=(3, 3))
indptr = np.array([0, 2, 3, 6])
indices = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6]).repeat(4).reshape(6, 2, 2)
sm3 = bsr_matrix((data,indices, indptr), shape=(6, 6))
hickle.dump(sm1, 'test_sp.h5')
sm1_h = hickle.load('test_sp.h5')
hickle.dump(sm2, 'test_sp2.h5')
sm2_h = hickle.load('test_sp2.h5')
hickle.dump(sm3, 'test_sp3.h5')
sm3_h = hickle.load('test_sp3.h5')
assert isinstance(sm1_h, csr_matrix)
assert isinstance(sm2_h, csc_matrix)
assert isinstance(sm3_h, bsr_matrix)
assert np.allclose(sm1_h.data, sm1.data)
assert np.allclose(sm2_h.data, sm2.data)
assert np.allclose(sm3_h.data, sm3.data)
assert sm1_h. shape == sm1.shape
assert sm2_h. shape == sm2.shape
assert sm3_h. shape == sm3.shape
lines = [line.rstrip() for line in lines]
feats = []
labels = []
for line_i, line in enumerate(lines):
img_path, label = line.split()
img = caffe.io.load_image(img_path)
feat = net.extract_feature(img)
feats.append(feat)
label = int(label)
labels.append(label)
if (line_i + 1) % 100 == 0:
print "processed", line_i + 1
feats = np.asarray(feats)
labels = np.asarray(labels)
hkl.dump(feats, dbprefix + "_features.hkl", mode="w")
hkl.dump(labels, dbprefix + "_labels.hkl", mode="w")
def mask_path_from_index(self, index, gt_mask):
"""
given image index, cache high resolution mask and return full path of masks
:param index: index of a specific image
:return: full path of this mask
"""
if self.image_set == 'val':
return []
cache_file = os.path.join(self.cache_path, 'VOCMask')
if not os.path.exists(cache_file):
os.makedirs(cache_file)
# instance level segmentation
gt_mask_file = os.path.join(cache_file, index + '.hkl')
if not os.path.exists(gt_mask_file):
hkl.dump(gt_mask.astype('bool'), gt_mask_file, mode='w', compression='gzip')
# cache flip gt_masks
gt_mask_flip_file = os.path.join(cache_file, index + '_flip.hkl')
if not os.path.exists(gt_mask_flip_file):
hkl.dump(gt_mask[:, :, ::-1].astype('bool'), gt_mask_flip_file, mode='w', compression='gzip')
return gt_mask_file
im_list = []
source_list = [] # corresponds to recording that image came from
for category, folder in splits[split]:
im_dir = os.path.join(DATA_DIR, 'raw/', category, folder, folder[:10], folder, 'image_03/data/')
files = list(os.walk(im_dir, topdown=False))[-1][-1]
im_list += [im_dir + f for f in sorted(files)]
source_list += [category + '-' + folder] * len(files)
print( 'Creating ' + split + ' data: ' + str(len(im_list)) + ' images')
X = np.zeros((len(im_list),) + desired_im_sz + (3,), np.uint8)
for i, im_file in enumerate(im_list):
im = imread(im_file)
X[i] = process_im(im, desired_im_sz)
hkl.dump(X, os.path.join(DATA_DIR, 'X_' + split + '.hkl'))
hkl.dump(source_list, os.path.join(DATA_DIR, 'sources_' + split + '.hkl'))
tag = datestring
obj = {'gatevalues': gv, 'snapshot': snapshot,
'datestring': datestring, 'data': data}
if verbose:
print('save_state: writing to file %s, tag %s' % (statefile, tag))
with h5py.File(statefile, 'a') as h5group:
# implementation using dev branch of hickle
if (tag in h5group):
if overwrite:
del h5group[tag]
else:
raise Exception(
'tag %s already exists in state file %s' % (tag, statefile))
hickle.dump(obj, h5group, path=tag)
def save_hkl_file(filename, data):
hkl_filename = filename + '.hkl'
try:
hkl.dump(data, hkl_filename, mode="w")
return True
except Exception:
if os.path.isfile(filename):
os.remove(hkl_filename)
path = os.path.dirname(os.path.abspath(self.dataset_path))
train_file = os.path.join(path, 'train{0}.p'.format(suffix))
test_file = os.path.join(path, 'test{0}.p'.format(suffix))
validate_file = os.path.join(path, 'validate{0}.p'.format(suffix))
params_file = os.path.join(path, 'params{0}.p'.format(suffix))
mapping_file = os.path.join(path, 'mapping{0}.p'.format(suffix))
numpy.save('x_{0}'.format(train_file), self.trainset[0])
numpy.save('y_{0}'.format(train_file), self.trainset[1])
# hickle.dump(self.trainset, file(train_file, 'w'))
hickle.dump(self.testset, file(test_file, 'w'))
hickle.dump(self.tuning, file(validate_file, 'w'))
hickle.dump(self.data_set_parameters, file(params_file, 'w'))
hickle.dump({'map': self.x_y_mapping, 'reduce': self.x_reduce}, file(mapping_file, 'w'))