def _old2new(self, row): if not self._allow_reading_old_format: raise IOError('Please convert to new format. ' + 'Use: python -m ase.db.convert ' + self.filename) extra = decode(row[25]) return row[:-1] + (encode( extra['keywords']), encode( extra['key_value_pairs']), encode(extra['data']), 42)
def _old2new(self, row): if not self._allow_reading_old_format: raise IOError('Please convert to new format. ' + 'Use: python -m ase.db.convert ' + self.filename) extra = decode(row[25]) return row[:-1] + (encode(extra['keywords']), encode(extra['key_value_pairs']), encode(extra['data']), 42)
def _old2new(self, row): if not self._allow_reading_old_format: raise IOError('Please convert to new format. ' + 'Use: python -m ase.db.convert ' + self.filename) if len(row) == 26: extra = decode(row[25]) return row[:-1] + (encode( extra['keywords']), encode( extra['key_value_pairs']), encode(extra['data']), 42) else: keywords = decode(row[-4]) kvp = decode(row[-3]) kvp.update(dict((keyword, 1) for keyword in keywords)) return row[:-4] + (encode(kvp), ) + row[-2:]
def _old2new(self, row): if not self._allow_reading_old_format: raise IOError('Please convert to new format. ' + 'Use: python -m ase.db.convert ' + self.filename) if len(row) == 26: extra = decode(row[25]) return row[:-1] + (encode(extra['keywords']), encode(extra['key_value_pairs']), encode(extra['data']), 42) else: keywords = decode(row[-4]) kvp = decode(row[-3]) kvp.update(dict((keyword, 1) for keyword in keywords)) return row[:-4] + (encode(kvp),) + row[-2:]
def sync(self): """Write data dictionary. Write bool, int, float, complex and str data, shapes and dtypes for ndarrays and class names for other objects.""" assert self.shape[0] == 0 i = self.fd.tell() s = encode(self.data).encode() writeint(self.fd, len(s)) self.fd.write(s) n = len(self.offsets) if self.nitems >= n: offsets = np.zeros(n * N1, np.int64) offsets[:n] = self.offsets self.itemoffsets = align(self.fd) offsets.tofile(self.fd) writeint(self.fd, self.itemoffsets, 24) self.offsets = offsets self.offsets[self.nitems] = i writeint(self.fd, i, self.itemoffsets + self.nitems * 8) self.nitems += 1 writeint(self.fd, self.nitems, 16) self.fd.flush() self.fd.seek(0, 2) # end of file self.data = {}
def _write(self, atoms, keywords, key_value_pairs, data): Database._write(self, atoms, keywords, key_value_pairs, data) con = self._connect() self._initialize(con) cur = con.cursor() id = None if isinstance(atoms, dict): dct = atoms unique_id = dct['unique_id'] cur.execute('SELECT id FROM systems WHERE unique_id=?', (unique_id,)) rows = cur.fetchall() if rows: id = rows[0][0] self._delete(cur, [id], ['keywords', 'text_key_values', 'number_key_values']) dct['mtime'] = now() else: dct = self.collect_data(atoms) if 'constraints' in dct: constraints = encode(dct['constraints']) else: constraints = None numbers = dct.get('numbers') row = (dct['unique_id'], dct['ctime'], dct['mtime'], dct['user'], blob(numbers), blob(dct.get('positions')), blob(dct.get('cell')), int(np.dot(dct.get('pbc'), [1, 2, 4])), blob(dct.get('initial_magmoms')), blob(dct.get('initial_charges')), blob(dct.get('masses')), blob(dct.get('tags')), blob(dct.get('momenta')), constraints) if 'calculator' in dct: row += (dct['calculator'], encode(dct['calculator_parameters'])) else: row += (None, None) magmom = dct.get('magmom') if magmom is not None: # magmom can be one or three numbers (non-collinear case) magmom = np.array(magmom) row += (dct.get('energy'), dct.get('free_energy'), blob(dct.get('forces')), blob(dct.get('stress')), blob(dct.get('dipole')), blob(dct.get('magmoms')), blob(magmom), blob(dct.get('charges')), encode(keywords), encode(key_value_pairs), encode(data), len(numbers)) if id is None: q = self.default + ', ' + ', '.join('?' * len(row)) cur.execute('INSERT INTO systems VALUES ({0})'.format(q), row) else: q = ', '.join(line.split()[0].lstrip() + '=?' for line in init_statements[0].splitlines()[2:]) cur.execute('UPDATE systems SET {0} WHERE id=?'.format(q), row + (id,)) if id is None: id = self.get_last_id(cur) if len(numbers) > 0: count = np.bincount(numbers) unique_numbers = count.nonzero()[0] species = [(int(Z), int(count[Z]), id) for Z in unique_numbers] cur.executemany('INSERT INTO species VALUES (?, ?, ?)', species) text_key_values = [] number_key_values = [] for key, value in key_value_pairs.items(): if isinstance(value, (float, int)): number_key_values.append([key, float(value), id]) else: assert isinstance(value, (str, unicode)) text_key_values.append([key, value, id]) cur.executemany('INSERT INTO text_key_values VALUES (?, ?, ?)', text_key_values) cur.executemany('INSERT INTO number_key_values VALUES (?, ?, ?)', number_key_values) cur.executemany('INSERT INTO keywords VALUES (?, ?)', [(keyword, id) for keyword in keywords]) # Insert keys in keywords table also so that it is easy to query # for the existance of keys: cur.executemany('INSERT INTO keywords VALUES (?, ?)', [(key, id) for key in key_value_pairs]) con.commit() con.close() return id