def test_persistent_offsets_readonly(self): os.remove(XDR.offsets_filename(self.traj)) assert_equal(os.path.exists( XDR.offsets_filename(self.trajectory.filename)), False) os.chmod(self.tmpdir.name, 0o555) self.trajectory._read_offsets(store=True) assert_equal(os.path.exists( XDR.offsets_filename(self.trajectory.filename)), False)
def test_persistent_offsets_readonly(self): os.remove(XDR.offsets_filename(self.traj)) assert_equal( os.path.exists(XDR.offsets_filename(self.trajectory.filename)), False) os.chmod(self.tmpdir.name, 0o555) self.trajectory._read_offsets(store=True) assert_equal( os.path.exists(XDR.offsets_filename(self.trajectory.filename)), False)
def test_offsets(self, trajectory, traj): trajectory._read_offsets(store=True) assert_almost_equal( trajectory._xdr.offsets, self.ref_offsets, err_msg="wrong frame offsets") outfile_offsets = XDR.offsets_filename(traj) saved_offsets = XDR.read_numpy_offsets(outfile_offsets) assert isinstance(saved_offsets, dict), \ "read_numpy_offsets did not return a dict" assert_almost_equal( trajectory._xdr.offsets, saved_offsets['offsets'], err_msg="error saving frame offsets") assert_almost_equal( self.ref_offsets, saved_offsets['offsets'], err_msg="saved frame offsets don't match " "the known ones") trajectory._load_offsets() assert_almost_equal( trajectory._xdr.offsets, self.ref_offsets, err_msg="error loading frame offsets") assert_equal(saved_offsets['ctime'], os.path.getctime(traj)) assert_equal(saved_offsets['size'], os.path.getsize(traj))
def test_nonexistant_offsets_file(self, traj): # assert that a nonexistant file returns False during read-in outfile_offsets = XDR.offsets_filename(traj) with patch.object(np, "load") as np_load_mock: np_load_mock.side_effect = IOError saved_offsets = XDR.read_numpy_offsets(outfile_offsets) assert_equal(saved_offsets, False)
def test_offsets(self, trajectory, traj): trajectory._read_offsets(store=True) assert_almost_equal( trajectory._xdr.offsets, self.ref_offsets, err_msg="wrong frame offsets") outfile_offsets = XDR.offsets_filename(traj) saved_offsets = XDR.read_numpy_offsets(outfile_offsets) assert_almost_equal( trajectory._xdr.offsets, saved_offsets['offsets'], err_msg="error saving frame offsets") assert_almost_equal( self.ref_offsets, saved_offsets['offsets'], err_msg="saved frame offsets don't match " "the known ones") trajectory._load_offsets() assert_almost_equal( trajectory._xdr.offsets, self.ref_offsets, err_msg="error loading frame offsets") assert_equal(saved_offsets['ctime'], os.path.getctime(traj)) assert_equal(saved_offsets['size'], os.path.getsize(traj))
def test_persistent_offsets_readonly(self, tmpdir): shutil.copy(self.filename, str(tmpdir)) os.chmod(str(tmpdir), 0o555) filename = str(tmpdir.join(os.path.basename(self.filename))) # try to write a offsets file self._reader(filename) assert_equal(os.path.exists(XDR.offsets_filename(filename)), False)
def test_persistent_offsets_natoms_mismatch(self, traj): # check that stored offsets are not loaded when trajectory # ctime differs from stored ctime fname = XDR.offsets_filename(traj) saved_offsets = XDR.read_numpy_offsets(fname) saved_offsets['n_atoms'] += 1 np.savez(fname, **saved_offsets) with pytest.warns(UserWarning, match="Reload offsets"): self._reader(traj)
def test_persistent_offsets_size_mismatch(self, traj): # check that stored offsets are not loaded when trajectory # size differs from stored size fname = XDR.offsets_filename(traj) saved_offsets = XDR.read_numpy_offsets(fname) saved_offsets['size'] += 1 with open(fname, 'wb') as f: np.savez(f, **saved_offsets) with pytest.warns(UserWarning, match="Reload offsets"): self._reader(traj)
def test_unsupported_format(self, traj): fname = XDR.offsets_filename(traj) saved_offsets = XDR.read_numpy_offsets(fname) idx_frame = 3 saved_offsets.pop('n_atoms') np.savez(fname, **saved_offsets) # ok as long as this doesn't throw reader = self._reader(traj) reader[idx_frame]
def test_persistent_offsets_last_frame_wrong(self, traj): fname = XDR.offsets_filename(traj) saved_offsets = XDR.read_numpy_offsets(fname) idx_frame = 3 saved_offsets['offsets'][idx_frame] += 42 np.savez(fname, **saved_offsets) with pytest.warns(UserWarning, match="seek failed"): reader = self._reader(traj) reader[idx_frame]
def test_unsupported_format(self): fname = XDR.offsets_filename(self.traj) saved_offsets = XDR.read_numpy_offsets(fname) idx_frame = 3 saved_offsets.pop('n_atoms') np.savez(fname, **saved_offsets) # ok as long as this doesn't throw reader = self._reader(self.traj) reader[idx_frame]
def test_persistent_offsets_natoms_mismatch(self): # check that stored offsets are not loaded when trajectory # ctime differs from stored ctime fname = XDR.offsets_filename(self.traj) saved_offsets = XDR.read_numpy_offsets(fname) saved_offsets['n_atoms'] += 1 np.savez(fname, **saved_offsets) with warnings.catch_warnings(record=True) as warn: warnings.simplefilter('always') self._reader(self.traj) assert_equal(warn[0].message.args, ('Reload offsets from trajectory\n ctime or size or n_atoms did not match', ))
def test_persistent_offsets_last_frame_wrong(self, traj): fname = XDR.offsets_filename(traj) saved_offsets = XDR.read_numpy_offsets(fname) assert isinstance(saved_offsets, dict), \ "read_numpy_offsets did not return a dict" idx_frame = 3 saved_offsets['offsets'][idx_frame] += 42 np.savez(fname, **saved_offsets) with pytest.warns(UserWarning, match="seek failed"): reader = self._reader(traj) reader[idx_frame]
def test_persistent_offsets_natoms_mismatch(self): # check that stored offsets are not loaded when trajectory # ctime differs from stored ctime fname = XDR.offsets_filename(self.traj) saved_offsets = XDR.read_numpy_offsets(fname) saved_offsets['n_atoms'] += 1 np.savez(fname, **saved_offsets) with warnings.catch_warnings(record=True) as warn: warnings.simplefilter('always') self._reader(self.traj) assert_equal(warn[0].message.args, ( 'Reload offsets from trajectory\n ctime or size or n_atoms did not match', ))
def test_persistent_offsets_readonly(self, tmpdir): shutil.copy(self.filename, str(tmpdir)) if os.name == 'nt': # Windows platform has a unique way to deny write access subprocess.call("icacls {fname} /deny Users:W".format(fname=tmpdir), shell=True) else: os.chmod(str(tmpdir), 0o555) filename = str(tmpdir.join(os.path.basename(self.filename))) # try to write a offsets file self._reader(filename) assert_equal(os.path.exists(XDR.offsets_filename(filename)), False)
def test_unsupported_format(self, traj): fname = XDR.offsets_filename(traj) saved_offsets = XDR.read_numpy_offsets(fname) assert isinstance(saved_offsets, dict), \ "read_numpy_offsets did not return a dict" idx_frame = 3 saved_offsets.pop('n_atoms') np.savez(fname, **saved_offsets) # ok as long as this doesn't throw reader = self._reader(traj) reader[idx_frame]
def test_persistent_offsets_last_frame_wrong(self): fname = XDR.offsets_filename(self.traj) saved_offsets = XDR.read_numpy_offsets(fname) idx_frame = 3 saved_offsets['offsets'][idx_frame] += 42 np.savez(fname, **saved_offsets) with warnings.catch_warnings(record=True) as warn: warnings.simplefilter('always') reader = self._reader(self.traj) reader[idx_frame] assert_equal(warn[0].message.args[0], 'seek failed, recalculating offsets and retrying')
def test_persistent_offsets_ctime_mismatch(self, traj): # check that stored offsets are not loaded when trajectory # ctime differs from stored ctime fname = XDR.offsets_filename(traj) saved_offsets = XDR.read_numpy_offsets(fname) assert isinstance(saved_offsets, dict), \ "read_numpy_offsets did not return a dict" saved_offsets['ctime'] += 1 with open(fname, 'wb') as f: np.savez(f, **saved_offsets) with pytest.warns(UserWarning, match="Reload offsets"): self._reader(traj)
def test_offsets(self): self.trajectory._read_offsets(store=True) assert_array_almost_equal(self.trajectory._xdr.offsets, self.ref_offsets, err_msg="wrong frame offsets") outfile_offsets = XDR.offsets_filename(self.traj) saved_offsets = XDR.read_numpy_offsets(outfile_offsets) assert_array_almost_equal(self.trajectory._xdr.offsets, saved_offsets['offsets'], err_msg="error saving frame offsets") assert_array_almost_equal(self.ref_offsets, saved_offsets['offsets'], err_msg="saved frame offsets don't match " "the known ones") self.trajectory._load_offsets() assert_array_almost_equal(self.trajectory._xdr.offsets, self.ref_offsets, err_msg="error loading frame offsets") assert_equal(saved_offsets['ctime'], os.path.getctime(self.traj)) assert_equal(saved_offsets['size'], os.path.getsize(self.traj))