diff --git a/src/schnetpack/data/atoms.py b/src/schnetpack/data/atoms.py index 46ed7da9..f01f866f 100644 --- a/src/schnetpack/data/atoms.py +++ b/src/schnetpack/data/atoms.py @@ -374,13 +374,14 @@ def _get_properties( return properties # Metadata - @property def metadata(self): - return self.conn.metadata + with connect(self.datapath, use_lock_file=False) as conn: + return conn.metadata def _set_metadata(self, val: Dict[str, Any]): - self.conn.metadata = val + with connect(self.datapath, use_lock_file=False) as conn: + conn.metadata = val def update_metadata(self, **kwargs): assert all( @@ -477,7 +478,7 @@ def add_system( `available_properties` of the dataset. """ - self._add_system(self.conn, atoms, atoms_metadata, **properties) + self._add_system(atoms, atoms_metadata, **properties) def add_systems( self, @@ -511,7 +512,6 @@ def add_systems( atoms_list, property_list, atoms_metadata_list ): self._add_system( - self.conn, atoms, atoms_metadata, **prop, @@ -519,7 +519,6 @@ def add_systems( def _add_system( self, - conn, atoms: Optional[Atoms] = None, atoms_metadata: Optional[Dict[str, Any]] = None, **properties, @@ -543,28 +542,30 @@ def _add_system( if atoms_metadata is None: atoms_metadata = {} - # add available properties to database - valid_props = set().union( - conn.metadata["_property_unit_dict"].keys(), - [structure.Z, structure.R, structure.cell, structure.pbc], - ) - for pname in properties: - if pname not in valid_props: - logger.warning( - f"Property `{pname}` is not a defined property for this dataset and " - + f"will be ignored. If it should be included, it has to be " - + f"provided together with its unit when calling " - + f"AseAtomsData.create()." - ) - - data = {} - for pname in conn.metadata["_property_unit_dict"].keys(): - if pname in properties: - data[pname] = properties[pname] - else: - raise AtomsDataError("Required property missing:" + pname) + with connect(self.datapath, use_lock_file=False) as conn: + prop_keys = conn.metadata["_property_unit_dict"].keys() - conn.write(atoms, data=data, key_value_pairs=atoms_metadata) + valid_props = set().union( + prop_keys, + [structure.Z, structure.R, structure.cell, structure.pbc], + ) + for pname in properties: + if pname not in valid_props: + logger.warning( + f"Property `{pname}` is not a defined property for this dataset and " + + f"will be ignored. If it should be included, it has to be " + + f"provided together with its unit when calling " + + f"AseAtomsData.create()." + ) + + data = {} + for pname in prop_keys: + if pname in properties: + data[pname] = properties[pname] + else: + raise AtomsDataError("Required property missing:" + pname) + + conn.write(atoms, data=data, key_value_pairs=atoms_metadata) def create_dataset(