Commit 8aeb4c34 authored by Martino Bertoni's avatar Martino Bertoni 🌋
Browse files

handling overwriting existing detination

parent e55c0074
......@@ -68,6 +68,10 @@ class Signaturizer():
features = len(self.modules) * 128
results = SignaturizerResult(len(smiles), destination,
features)
if results.readonly:
raise Exception(
'Destination file already exists, ' +
'delete or rename to proceed.')
# predict by chunk
all_chunks = range(0, len(smiles), chunk_size)
for i in tqdm(all_chunks, disable=self.verbose):
......@@ -128,6 +132,7 @@ class SignaturizerResult():
be saved.
"""
self.dst = destination
self.readonly = False
if self.dst is None:
# simple numpy arrays
self.h5 = None
......@@ -138,6 +143,7 @@ class SignaturizerResult():
print('HDF5 file %s exists, opening in read-only.' % self.dst)
# this avoid overwriting by mistake
self.h5 = h5py.File(self.dst, 'r')
self.readonly = True
else:
# create the datasets
self.h5 = h5py.File(self.dst, 'w')
......
......@@ -87,3 +87,12 @@ class TestSignaturizer(unittest.TestCase):
res = module.predict(self.test_smiles)
self.assertEqual(res.signature.shape[0], 2)
self.assertEqual(res.signature.shape[1], 128 * 25)
def test_overwrite(self):
module_dir = os.path.join(self.data_dir, 'B1')
module = Signaturizer(module_dir)
destination = os.path.join(self.tmp_dir, 'pred.h5')
module.predict(self.test_smiles, destination)
# repeating writing will result in an exception
with self.assertRaises(Exception):
module.predict(self.test_smiles, destination)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment