def read_info(self, framedir, name, split=None): """Read information about file contents without reading the data. Information is a dictionary containing as aminimum the shape and type. """ fn = os.path.join(framedir, name + '.ulm') if split is None or os.path.exists(fn): f = ulmopen(fn, 'r') info = odict() info['shape'] = f.shape info['type'] = f.dtype info['stored_as'] = f.stored_as info['identical'] = f.all_identical f.close() return info else: info = odict() for i in range(split): fn = os.path.join(framedir, name + '_' + str(i) + '.ulm') f = ulmopen(fn, 'r') if i == 0: info['shape'] = list(f.shape) info['type'] = f.dtype info['stored_as'] = f.stored_as info['identical'] = f.all_identical else: info['shape'][0] += f.shape[0] assert info['type'] == f.dtype info['identical'] = info['identical'] and f.all_identical f.close() info['shape'] = tuple(info['shape']) return info
def _open(self, filename, mode): from ase.io.ulm import ulmopen, DummyWriter if mode not in 'aw': raise ValueError('mode must be "w" or "a".') if self.master: self.backend = ulmopen(filename, mode, tag='ASE-Trajectory') if len(self.backend) > 0: r = ulmopen(filename) self.numbers = r.numbers self.pbc = r.pbc else: self.backend = DummyWriter()
def _open(self, filename): from ase.io.ulm import ulmopen, InvalidULMFileError try: self.backend = ulmopen(filename, 'r') except InvalidULMFileError: raise RuntimeError('This is not a valid ASE trajectory file. ' 'If this is an old-format (version <3.9) ' 'PickleTrajectory file you can convert it ' 'with ase.io.trajectory.convert("%s") ' 'or:\n\n $ python -m ase.io.trajectory %s' % (filename, filename)) self._read_header()
def read(self, framedir, name): "Read data from separate file." fn = os.path.join(framedir, name + '.ulm') f = ulmopen(fn, 'r') if f.all_identical: # Only a single data value data = np.zeros(f.shape, dtype=getattr(np, f.dtype)) + f.data elif f.dtype == f.stored_as: # Easy, the array can be returned as-is. data = f.data else: # Cast the data back data = f.data.astype(getattr(np, f.dtype)) f.close() return data
def write(self, framedir, name, data): "Write data to separate file." if self.writelarge: shape = data.shape dtype = str(data.dtype) stored_as = dtype all_identical = False # Check if it a type that can be stored with less space if np.issubdtype(data.dtype, np.integer): # An integer type, we may want to convert minval = data.min() maxval = data.max() # ulm cannot write np.bool_: all_identical = bool(minval == maxval) if all_identical: data = int(data.flat[0]) # Convert to standard integer else: for typ in self.integral_dtypes: if (minval >= self.int_minval[typ] and maxval <= self.int_maxval[typ] and data.itemsize > self.int_itemsize[typ]): # Convert to smaller type stored_as = typ data = data.astype(self.int_dtype[typ]) elif data.dtype == np.float32 or data.dtype == np.float64: all_identical = bool(data.min() == data.max()) if all_identical: data = float(data.flat[0]) # Convert to standard float elif data.dtype == np.float64 and self.singleprecision: # Downconvert double to single precision stored_as = 'float32' data = data.astype(np.float32) fn = os.path.join(framedir, name + '.ulm') f = ulmopen(fn, 'w') f.write(shape=shape, dtype=dtype, stored_as=stored_as, all_identical=all_identical, data=data) f.close()
def read_small(self, framedir): "Read small data." f = ulmopen(os.path.join(framedir, 'smalldata.ulm'), 'r') data = f.asdict() f.close() return data
def write_small(self, framedir, smalldata): "Write small data to be written jointly." if self.writesmall: f = ulmopen(os.path.join(framedir, 'smalldata.ulm'), 'w') f.write(**smalldata) f.close()
def read_gpw(filename): try: reader = ulm.ulmopen(filename) except ulm.InvalidULMFileError: return read_old_gpw(filename) return read_atoms(reader.atoms)